-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-GitQuick.ps1
More file actions
213 lines (185 loc) · 5.39 KB
/
Copy pathGet-GitQuick.ps1
File metadata and controls
213 lines (185 loc) · 5.39 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<#
.SYNOPSIS
Enhanced git status with branch info.
.DESCRIPTION
Shows a quick, colorized git status with:
- Current branch
- Ahead/behind remote
- Modified/staged/untracked files
- Stash count
.PARAMETER AsJson
Output as JSON for MCP tools.
.EXAMPLE
gs
gs -AsJson
#>
[CmdletBinding()]
param(
[switch]$AsJson
)
# Check if in git repo
$isGitRepo = git rev-parse --is-inside-work-tree 2>$null
if ($isGitRepo -ne 'true') {
if ($AsJson) {
@{ error = 'Not a git repository' } | ConvertTo-Json
} else {
Write-Host "Not a git repository" -ForegroundColor Red
}
exit 1
}
# Gather git info
$branch = git branch --show-current 2>$null
$status = git status --porcelain 2>$null
$stashCount = (git stash list 2>$null | Measure-Object).Count
# Get ahead/behind
$ahead = 0
$behind = 0
$tracking = git rev-parse --abbrev-ref '@{upstream}' 2>$null
if ($tracking) {
$counts = git rev-list --left-right --count "HEAD...$tracking" 2>$null
if ($counts -match '(\d+)\s+(\d+)') {
$ahead = [int]$Matches[1]
$behind = [int]$Matches[2]
}
}
# Parse status
$staged = @()
$modified = @()
$untracked = @()
$deleted = @()
$conflicts = @()
foreach ($line in $status) {
if ($line.Length -lt 3) { continue }
$index = $line[0]
$worktree = $line[1]
$file = $line.Substring(3)
# Conflicts
if ($index -eq 'U' -or $worktree -eq 'U') {
$conflicts += $file
continue
}
# Staged changes
if ($index -match '[MADRC]') {
$staged += $file
}
# Working tree changes
if ($worktree -eq 'M') {
$modified += $file
} elseif ($worktree -eq 'D') {
$deleted += $file
} elseif ($index -eq '?' -and $worktree -eq '?') {
$untracked += $file
}
}
# Get remote URL
$remote = git remote get-url origin 2>$null
if ($remote) {
# Simplify GitHub/GitLab URLs
$remote = $remote -replace '\.git$', '' -replace '^git@github\.com:', 'github.com/' -replace '^https?://', ''
}
# JSON output
if ($AsJson) {
$result = [ordered]@{
branch = $branch
remote = $remote
tracking = $tracking
ahead = $ahead
behind = $behind
staged = $staged.Count
modified = $modified.Count
deleted = $deleted.Count
untracked = $untracked.Count
conflicts = $conflicts.Count
stashes = $stashCount
clean = ($status.Count -eq 0)
files = [ordered]@{
staged = $staged
modified = $modified
deleted = $deleted
untracked = $untracked
conflicts = $conflicts
}
}
$result | ConvertTo-Json -Depth 5
exit 0
}
# Pretty output
Write-Host ""
# Branch with status
Write-Host " Branch: " -NoNewline -ForegroundColor Gray
Write-Host $branch -NoNewline -ForegroundColor Cyan
# Ahead/behind
if ($ahead -gt 0 -or $behind -gt 0) {
Write-Host " [" -NoNewline -ForegroundColor DarkGray
if ($ahead -gt 0) {
Write-Host "↑$ahead" -NoNewline -ForegroundColor Green
}
if ($behind -gt 0) {
if ($ahead -gt 0) { Write-Host " " -NoNewline }
Write-Host "↓$behind" -NoNewline -ForegroundColor Red
}
Write-Host "]" -NoNewline -ForegroundColor DarkGray
}
Write-Host ""
# Remote
if ($remote) {
Write-Host " Remote: " -NoNewline -ForegroundColor Gray
Write-Host $remote -ForegroundColor DarkGray
}
Write-Host ""
# Status summary
$isClean = $status.Count -eq 0
if ($isClean) {
Write-Host " [OK] Working tree clean" -ForegroundColor Green
} else {
# Conflicts (highest priority)
if ($conflicts.Count -gt 0) {
Write-Host " [CONFLICT] $($conflicts.Count) conflict(s)" -ForegroundColor Red
$conflicts | Select-Object -First 3 | ForEach-Object {
Write-Host " ! $_" -ForegroundColor Red
}
}
# Staged
if ($staged.Count -gt 0) {
Write-Host " ● $($staged.Count) staged" -ForegroundColor Green
$staged | Select-Object -First 3 | ForEach-Object {
Write-Host " + $_" -ForegroundColor Green
}
if ($staged.Count -gt 3) {
Write-Host " ... and $($staged.Count - 3) more" -ForegroundColor DarkGray
}
}
# Modified
if ($modified.Count -gt 0) {
Write-Host " ○ $($modified.Count) modified" -ForegroundColor Yellow
$modified | Select-Object -First 3 | ForEach-Object {
Write-Host " ~ $_" -ForegroundColor Yellow
}
if ($modified.Count -gt 3) {
Write-Host " ... and $($modified.Count - 3) more" -ForegroundColor DarkGray
}
}
# Deleted
if ($deleted.Count -gt 0) {
Write-Host " [DEL] $($deleted.Count) deleted" -ForegroundColor Red
$deleted | Select-Object -First 3 | ForEach-Object {
Write-Host " - $_" -ForegroundColor Red
}
}
# Untracked
if ($untracked.Count -gt 0) {
Write-Host " ? $($untracked.Count) untracked" -ForegroundColor DarkGray
$untracked | Select-Object -First 3 | ForEach-Object {
Write-Host " ? $_" -ForegroundColor DarkGray
}
if ($untracked.Count -gt 3) {
Write-Host " ... and $($untracked.Count - 3) more" -ForegroundColor DarkGray
}
}
}
# Stashes
if ($stashCount -gt 0) {
Write-Host ""
Write-Host " [STASH] $stashCount stashes" -ForegroundColor Magenta
}
Write-Host ""