-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitutils.ps1
69 lines (66 loc) · 1.78 KB
/
gitutils.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# Git functions
# Mark Embling (http://www.markembling.info/)
# Is the current directory a git repository/working copy?
function isCurrentDirectoryGitRepository {
if ((Test-Path ".git") -eq $TRUE) {
return $TRUE
}
# Test within parent dirs
$checkIn = (Get-Item .).parent
while ($checkIn -ne $NULL) {
$pathToTest = $checkIn.fullname + '/.git'
if ((Test-Path $pathToTest) -eq $TRUE) {
return $TRUE
} else {
$checkIn = $checkIn.parent
}
}
return $FALSE
}
# Get the current branch
function gitBranchName {
$currentBranch = ''
git branch | foreach {
if ($_ -match "^\* (.*)") {
$currentBranch += $matches[1]
}
}
return $currentBranch
}
# Extracts status details about the repo
function gitStatus {
$untracked = $FALSE
$added = 0
$modified = 0
$deleted = 0
$ahead = $FALSE
$aheadCount = 0
$output = git status
$branchbits = $output[0].Split(' ')
$branch = $branchbits[$branchbits.length - 1]
$output | foreach {
if ($_ -match "^\#.*origin/.*' by (\d+) commit.*") {
$aheadCount = $matches[1]
$ahead = $TRUE
}
elseif ($_ -match "deleted:") {
$deleted += 1
}
elseif (($_ -match "modified:") -or ($_ -match "renamed:")) {
$modified += 1
}
elseif ($_ -match "new file:") {
$added += 1
}
elseif ($_ -match "Untracked files:") {
$untracked = $TRUE
}
}
return @{"untracked" = $untracked;
"added" = $added;
"modified" = $modified;
"deleted" = $deleted;
"ahead" = $ahead;
"aheadCount" = $aheadCount;
"branch" = $branch}
}