-
Notifications
You must be signed in to change notification settings - Fork 1
/
git.cpp
396 lines (341 loc) · 10.7 KB
/
git.cpp
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright (c) 2024 by Christopher Antos
// License: http://opensource.org/licenses/MIT
// vim: set et ts=4 sw=4 cino={0s:
#include "pch.h"
#include "git.h"
#include "filesys.h"
#include "output.h"
#include "colors.h"
RepoStatus::~RepoStatus()
{
for (const auto& s : status)
free(const_cast<WCHAR*>(s.first));
}
static bool IsUncPath(const WCHAR* p, const WCHAR** past_unc)
{
if (!IsPathSeparator(p[0]) || !IsPathSeparator(p[1]))
return false;
const WCHAR* const in = p;
while (IsPathSeparator(*p))
++p;
unsigned leading = unsigned(p - in);
// Check for device namespace.
if (p[0] == '.' && (!p[1] || IsPathSeparator(p[1])))
return false;
// Check for \\?\UNC\ namespace.
if (leading == 2 && p[0] == '?' && IsPathSeparator(p[1]))
{
++p;
while (IsPathSeparator(*p))
++p;
if (*p != 'U' && *p != 'u')
return false;
++p;
if (*p != 'N' && *p != 'n')
return false;
++p;
if (*p != 'C' && *p != 'c')
return false;
++p;
if (!IsPathSeparator(*p))
return false;
while (IsPathSeparator(*p))
++p;
}
if (past_unc)
{
// Skip server name.
while (*p && !IsPathSeparator(*p))
++p;
// Skip separator.
while (IsPathSeparator(*p))
++p;
// Skip share name.
while (*p && !IsPathSeparator(*p))
++p;
*past_unc = p;
}
return true;
}
static unsigned PastSsqs(const WCHAR* p)
{
const WCHAR* const orig = p;
if (!IsPathSeparator(*(p++)))
return 0;
if (!IsPathSeparator(*(p++)))
return 0;
if (*(p++) != '?')
return 0;
if (!IsPathSeparator(*(p++)))
return 0;
while (IsPathSeparator(*p))
++p;
return unsigned(p - orig);
}
static const WCHAR* PastDrive(const WCHAR* p)
{
if (!IsUncPath(p, &p))
{
p += PastSsqs(p);
if (iswalpha(p[0]) && p[1] == ':')
p += 2;
}
return p;
}
static bool PathToParent(StrW& out, StrW* child=nullptr)
{
unsigned orig_len = out.Length();
// Find end of drive or UNC root plus separator(s).
unsigned start = unsigned(PastDrive(out.Text()) - out.Text());
if (start && out.Text()[start - 1] == ':')
{
while (IsPathSeparator(out.Text()[start]))
++start;
}
// Trim separators at the end.
unsigned end = out.Length();
while (end > 0 && IsPathSeparator(out.Text()[end - 1]))
--end;
// Trim the last path component.
unsigned child_end = end;
while (end > 0 && !IsPathSeparator(out.Text()[end - 1]))
--end;
if (end < start)
end = start;
// Return the last path component.
if (child)
child->Set(out.Text() + end, child_end - end);
// Trim trailing separators.
while (end > start && IsPathSeparator(out.Text()[end - 1]))
--end;
out.SetLength(end);
return (out.Length() != orig_len);
}
bool IsUnderRepo(const WCHAR* _dir, StrW& root)
{
StrW dir(_dir);
StrW git_dir(_dir);
while (true)
{
git_dir.SetLength(dir.Length());
EnsureTrailingSlash(git_dir);
git_dir.Append(L".git");
if (GetFileType(git_dir.Text()) >= FileType::Dir)
{
root.Set(dir);
return true;
}
if (!PathToParent(dir))
return false;
}
}
GitFileState CharToState(char ch)
{
switch (ch)
{
case '?': return GitFileState::NEW;
case 'A': return GitFileState::NEW;
case 'M': return GitFileState::MODIFIED;
case 'T': return GitFileState::TYPECHANGE;
case 'D': return GitFileState::DELETED;
case 'R': return GitFileState::RENAMED;
case 'C': return GitFileState::NEW;
case '!': return GitFileState::IGNORED;
case 'U': return GitFileState::UNMERGED;
default: return GitFileState::NONE;
}
}
std::shared_ptr<RepoStatus> GitStatus(const WCHAR* _dir, bool need_ignored, bool walk_up)
{
std::shared_ptr<RepoStatus> status = std::make_shared<RepoStatus>();
StrW root;
StrW git_dir;
if (walk_up)
{
if (!IsUnderRepo(_dir, root))
{
failed:
return status;
}
need_ignored = true;
PathJoin(git_dir, root.Text(), L".git");
}
else
{
PathJoin(git_dir, _dir, L".git");
if (GetFileType(git_dir.Text()) < FileType::Dir)
goto failed;
root.Set(_dir);
}
if (g_debug)
Printf(L"debug: git status in '%s'%s\n", root.Text(), need_ignored ? L", plus ignored files" : L"");
// Prevent GVFS from printing progress gibberish even while output is
// redirected to a pipe.
SetEnvironmentVariable(L"GVFS_UNATTENDED", L"1");
StrW command;
command.Printf(L"2>nul git.exe --work-tree=\"%s\" --git-dir=\"%s\" status --porcelain --no-ahead-behind -unormal --branch %s",
root.Text(), git_dir.Text(), need_ignored ? L"--ignored" : L"");
FILE* pipe = _wpopen(command.Text(), L"rt");
if (!pipe)
goto failed;
const size_t buffer_size = 8192;
char* buffer = new char[buffer_size];
StrW wide;
wide.Reserve(buffer_size);
while (fgets(buffer, buffer_size, pipe))
{
char* eos = buffer + strlen(buffer);
while (--eos >= buffer)
{
if (*eos != '\r' && *eos != '\n')
break;
*eos = '\0';
}
if (buffer[0] == '#' && buffer[1] == '#' && buffer[2] == ' ')
{
if (status->branch.Empty())
{
MultiByteToWideChar(CP_UTF8, 0, buffer + 3, -1, wide.Reserve(), wide.Capacity());
wide.ResyncLength();
if (wide.EqualI(L"HEAD (no branch)"))
wide.Set(L"HEAD");
else if (wcsncmp(wide.Text(), L"No commits yet on ", 18) == 0)
wide.Clear();
else
{
const WCHAR* end = wcsstr(wide.Text(), L"...");
if (end)
wide.SetEnd(end);
}
status->branch.Set(wide.Text());
}
continue;
}
if (buffer[0] && buffer[1] && buffer[2] == ' ')
{
StrW name;
FileStatus filestatus;
filestatus.staged = CharToState(buffer[0]);
filestatus.working = CharToState(buffer[1]);
switch (filestatus.working)
{
case GitFileState::NEW:
switch (filestatus.staged)
{
case GitFileState::NEW:
filestatus.staged = GitFileState::NONE;
break;
}
break;
case GitFileState::IGNORED:
switch (filestatus.staged)
{
case GitFileState::IGNORED:
filestatus.staged = GitFileState::NONE;
break;
}
break;
}
const char* parse = buffer + 3;
if (filestatus.staged == GitFileState::RENAMED)
{
const char* arrow = strstr(parse, " -> ");
if (arrow)
parse = arrow + 4;
}
const bool quoted = (*parse == '\"');
if (quoted)
++parse;
MultiByteToWideChar(CP_UTF8, 0, parse, -1, wide.Reserve(), wide.Capacity());
name.Set(wide.Text());
if (quoted)
{
const WCHAR* end = wcschr(name.Text(), '\"');
if (!end)
continue;
name.SetEnd(end);
}
else
{
const WCHAR* space = wcschr(name.Text(), ' ');
if (space)
name.SetEnd(space);
}
StrW full;
PathJoin(full, root.Text(), name);
for (WCHAR* walk = full.Reserve(); *walk; ++walk)
if (*walk == '/')
*walk = '\\';
StripTrailingSlashes(full);
status->status.emplace(full.Detach(), filestatus);
}
}
_pclose(pipe);
free(buffer);
status->repo = true;
status->clean = status->status.empty();
status->main = status->branch.Equal(L"main") || status->branch.Equal(L"master");
status->root.Set(root);
// Finally (AFTER setting status->clean), add an implicit ignore entry for
// the .git directory.
FileStatus filestatus;
filestatus.staged = GitFileState::NONE;
filestatus.working = GitFileState::IGNORED;
status->status.emplace(git_dir.Detach(), filestatus);
if (g_debug)
{
Printf(L"debug: root: %s\n", status->root.Text());
Printf(L"debug: branch: %s\n", status->branch.Text());
Printf(L"debug: main: %s\n", status->main ? L"yes" : L"no");
Printf(L"debug: clean: %s\n", status->clean ? L"yes" : L"no");
for (auto iter : status->status)
{
wide.Clear();
const WCHAR* color1 = GetColorByKey(GitSymbol(iter.second.staged).color_key);
const WCHAR* color2 = GetColorByKey(GitSymbol(iter.second.working).color_key);
wide.AppendColor(color1);
wide.Append(GitSymbol(iter.second.staged).symbol);
wide.AppendColorElseNormalIf(color2, color1);
wide.Append(GitSymbol(iter.second.working).symbol);
wide.AppendNormalIf(color2);
Printf(L"debug: %s %s\n", wide.Text(), iter.first);
}
}
return status;
}
const GitStatusSymbol& GitSymbol(GitFileState state)
{
static GitStatusSymbol c_symbols[] =
{
{ '-', L"xx" },
{ 'N', L"ga" }, // GitFileState::NEW
{ 'M', L"gm" }, // GitFileState::MODIFIED
{ 'D', L"gd" }, // GitFileState::DELETED
{ 'R', L"gv" }, // GitFileState::RENAMED
{ 'T', L"gt" }, // GitFileState::TYPECHANGE
{ 'I', L"gi" }, // GitFileState::IGNORED
{ 'U', L"gc" }, // GitFileState::UNMERGED
};
static_assert(_countof(c_symbols) == unsigned(GitFileState::COUNT), "wrong number of GitFileState symbols");
assert(state >= GitFileState::NONE);
assert(state < GitFileState::COUNT);
return c_symbols[unsigned(state)];
}
void RepoMap::Add(std::shared_ptr<const RepoStatus> repo)
{
if (repo)
m_map.emplace(repo->root.Text(), repo);
}
void RepoMap::Remove(const WCHAR* dir)
{
const auto& iter = m_map.find(dir);
if (iter != m_map.end())
m_map.erase(iter);
}
std::shared_ptr<const RepoStatus> RepoMap::Find(const WCHAR* dir) const
{
const auto& iter = m_map.find(dir);
if (iter == m_map.end())
return nullptr;
return iter->second;
}