-
Notifications
You must be signed in to change notification settings - Fork 2
/
checkout.cpp
executable file
·417 lines (393 loc) · 12.8 KB
/
checkout.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* Checkouts of various kind. The MSSCCI concept of checkout doesn't actually
* apply here, but "undo checkout" seems to conceptually map to "checkout to
* HEAD". Confusing!
*
* If an IDE supports multiple checkouts, we can perhaps map SccCheckout to
* a "controlled" "git checkout" dialog, so they can pick the thing to pull
* the file from.
*/
#include "stdafx.h"
static int AskIfCreateBranchFromRemote(HWND hwnd)
{
return MessageBox(hwnd,
"The remote branch must have a local branch in order to become HEAD. "
"Not doing so will detach HEAD. "
"Would you like to create a local branch?\r\n\r\n"
"(If you already have a local branch, select it instead of this one.)",
"Can't Make Remote Branch HEAD",
MB_ICONQUESTION | MB_YESNOCANCEL);
}
SCCRTN LGitCheckoutRef(LGitContext *ctx, HWND hwnd, git_reference *branch)
{
LGitLog("**LGitCheckoutRef** Context=%p\n", ctx);
/* Resolve the name to a commit for checkout. Example uses annotated... */
BOOL attached = TRUE;
int remote_question;
SCCRTN ret = SCC_E_NONSPECIFICERROR;
int rc;
git_reference *new_branch = NULL, *checkout_branch;
git_oid commit_oid;
git_commit *commit = NULL;
git_checkout_options co_opts;
/* For future reference */
const char *name = git_reference_name(branch);
/* XXX: Convert to _target */
if (git_reference_name_to_id(&commit_oid, ctx->repo, name) != 0) {
LGitLibraryError(hwnd, "git_reference_name_to_id");
goto err;
}
if (git_commit_lookup(&commit, ctx->repo, &commit_oid) != 0) {
LGitLibraryError(hwnd, "git_commit_lookup");
goto err;
}
git_checkout_options_init(&co_opts, GIT_CHECKOUT_OPTIONS_VERSION);
LGitInitCheckoutProgressCallback(ctx, &co_opts);
/* XXX: Allow setting force */
co_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
/* If we have a remote branch, create a local one from it. */
checkout_branch = branch;
if (git_reference_is_remote(branch)
/* Ask the user if they want a local branch from it. */
&& (remote_question = AskIfCreateBranchFromRemote(hwnd)) == IDYES) {
LGitLog(" ! %s is remote tracking\n", name);
/* Try to create a local branch from the remote that can be HEAD. */
int rc = git_branch_create(&new_branch,
ctx->repo,
git_reference_shorthand(branch),
commit,
0);
/* XXX: Check if it's because it already exists */
if (rc != 0) {
LGitLog("!! Failed to make local tracking branch (%x)\n", rc);
attached = FALSE;
} else {
LGitLog(" ! Made local tracking branch\n");
checkout_branch = new_branch;
}
} else if (!git_reference_is_branch(branch)) {
/* If it's a tag or similar, we can't track HEAD with it. */
LGitLog(" ! %s is not a local branch, no attach\n", name);
attached = FALSE;
}
/* React if it's a remote and the user cancelled */
if (remote_question == IDCANCEL) {
ret = SCC_I_OPERATIONCANCELED;
goto err;
}
/* Peeled to a tree */
LGitInitCheckoutNotifyCallbacks(ctx, hwnd, &co_opts);
LGitProgressInit(ctx, "Checking Out Files", 0);
LGitProgressStart(ctx, hwnd, TRUE);
rc = git_checkout_tree(ctx->repo, (const git_object *)commit, &co_opts);
if (rc == GIT_ECONFLICT) {
LGitProgressDeinit(ctx);
/* XXX: Specific error, but checkout notify UI will cover us anyways */
goto err;
} else if (rc != 0) {
LGitProgressDeinit(ctx);
LGitLibraryError(hwnd, "git_checkout_tree");
goto err;
}
/* This must be a local branch to become HEAD. */
if (attached && git_repository_set_head(ctx->repo, git_reference_name(checkout_branch)) != 0) {
LGitLog("!! Attached head fail\n");
/* XXX: Show error */
MessageBox(hwnd,
"Failed to set the new attached head. "
"Attempting to proceed with a detached head.",
"Couldn't Set HEAD",
MB_ICONERROR);
attached = FALSE;
}
if (!attached && git_repository_set_head_detached(ctx->repo, &commit_oid) != 0) {
LGitProgressDeinit(ctx);
LGitLibraryError(hwnd, "git_repository_set_head_detached");
goto err;
}
ret = SCC_OK;
LGitProgressDeinit(ctx);
err:
LGitFinishCheckoutNotify(ctx, hwnd, &co_opts);
if (new_branch != NULL) {
git_reference_free(new_branch);
}
if (commit != NULL) {
git_commit_free(commit);
}
return ret;
}
SCCRTN LGitCheckoutRefByName(LGitContext *ctx,
HWND hwnd,
const char *name)
{
LGitLog("**LGitCheckoutRefByName** Context=%p\n", ctx);
LGitLog(" refname %s\n", name);
git_reference *branch = NULL;
SCCRTN ret;
if (git_reference_lookup(&branch, ctx->repo, name) != 0) {
LGitLibraryError(hwnd, "git_reference_lookup");
goto err;
}
ret = LGitCheckoutRef(ctx, hwnd, branch);
err:
if (branch != NULL) {
git_reference_free(branch);
}
return ret;
}
/**
* This takes an OID that should be peeled to a tree, like a commit.
*/
SCCRTN LGitCheckoutTree(LGitContext *ctx,
HWND hwnd,
const git_oid *commit_oid)
{
LGitLog("**LGitCheckoutTree** Context=%p\n", ctx);
LGitLog(" oid %s\n", git_oid_tostr_s(commit_oid));
SCCRTN ret = SCC_E_NONSPECIFICERROR;
int rc;
git_commit *commit = NULL;
git_checkout_options co_opts;
if (git_commit_lookup(&commit, ctx->repo, commit_oid) != 0) {
LGitLibraryError(hwnd, "git_commit_lookup");
goto err;
}
git_checkout_options_init(&co_opts, GIT_CHECKOUT_OPTIONS_VERSION);
LGitInitCheckoutProgressCallback(ctx, &co_opts);
/* XXX: Allow setting force */
co_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
/* Peeled to a tree */
LGitProgressInit(ctx, "Checking Out Files", 0);
LGitProgressStart(ctx, hwnd, TRUE);
LGitInitCheckoutNotifyCallbacks(ctx, hwnd, &co_opts);
rc = git_checkout_tree(ctx->repo, (const git_object *)commit, &co_opts);
if (rc == GIT_ECONFLICT) {
LGitProgressDeinit(ctx);
/* XXX: Specific error, but checkout notify UI will cover us anyways */
goto err;
} else if (rc != 0) {
LGitProgressDeinit(ctx);
LGitLibraryError(hwnd, "git_checkout_tree");
goto err;
}
if (git_repository_set_head_detached(ctx->repo, commit_oid) != 0) {
LGitProgressDeinit(ctx);
LGitLibraryError(hwnd, "git_repository_set_head_detached");
goto err;
}
LGitProgressDeinit(ctx);
ret = SCC_OK;
err:
LGitFinishCheckoutNotify(ctx, hwnd, &co_opts);
if (commit != NULL) {
git_commit_free(commit);
}
return ret;
}
SCCRTN LGitCheckoutStaged(LGitContext *ctx, HWND hwnd, git_strarray *paths)
{
LGitLog("**LGitCheckoutStaged** Context=%p\n");
LGitLog(" paths count %u\n", paths->count);
SCCRTN ret = SCC_OK;
git_checkout_options co_opts;
git_checkout_options_init(&co_opts, GIT_CHECKOUT_OPTIONS_VERSION);
LGitInitCheckoutProgressCallback(ctx, &co_opts);
co_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
co_opts.paths.strings = paths->strings;
co_opts.paths.count = paths->count;
LGitProgressInit(ctx, "Checking Out Files", 0);
LGitProgressStart(ctx, hwnd, TRUE);
LGitInitCheckoutNotifyCallbacks(ctx, hwnd, &co_opts);
int rc = git_checkout_index(ctx->repo, NULL, &co_opts);
if (rc == GIT_ECONFLICT) {
LGitProgressDeinit(ctx);
/* XXX: Specific error, but checkout notify UI will cover us anyways */
ret = SCC_E_NONSPECIFICERROR;
} else if (rc != 0) {
LGitProgressDeinit(ctx);
LGitLibraryError(hwnd, "LGitCheckoutStaged git_checkout_index");
ret = SCC_E_NONSPECIFICERROR;
}
LGitProgressDeinit(ctx);
LGitFinishCheckoutNotify(ctx, hwnd, &co_opts);
return ret;
}
SCCRTN LGitCheckoutHead(LGitContext *ctx, HWND hwnd, git_strarray *paths)
{
LGitLog("**LGitStageCheckoutHead** Context=%p\n");
LGitLog(" paths count %u\n", paths->count);
SCCRTN ret = SCC_OK;
git_checkout_options co_opts;
git_checkout_options_init(&co_opts, GIT_CHECKOUT_OPTIONS_VERSION);
LGitInitCheckoutProgressCallback(ctx, &co_opts);
co_opts.checkout_strategy = GIT_CHECKOUT_FORCE;
co_opts.paths.strings = paths->strings;
co_opts.paths.count = paths->count;
LGitProgressInit(ctx, "Checking Out Files", 0);
LGitProgressStart(ctx, hwnd, TRUE);
LGitInitCheckoutNotifyCallbacks(ctx, hwnd, &co_opts);
int rc = git_checkout_head(ctx->repo, &co_opts);
if (rc == GIT_ECONFLICT) {
LGitProgressDeinit(ctx);
/* XXX: Specific error, but checkout notify UI will cover us anyways */
ret = SCC_E_NONSPECIFICERROR;
} else if (rc != 0) {
LGitProgressDeinit(ctx);
LGitLibraryError(hwnd, "LGitCheckoutHead git_checkout_head");
ret = SCC_E_NONSPECIFICERROR;
}
LGitProgressDeinit(ctx);
LGitFinishCheckoutNotify(ctx, hwnd, &co_opts);
return ret;
}
/**
* This checkout emulates the SCC API semantics (list of files, use HEAD)
*/
static SCCRTN LGitCheckoutInternal (LPVOID context,
HWND hWnd,
LONG nFiles,
LPCSTR* lpFileNames,
LONG dwFlags,
LPCMDOPTS pvOptions)
{
int i, path_count;
char **paths;
LGitContext *ctx = (LGitContext*)context;
/*
* Flags mean files under a directory or recursive directory. This handles
* the latter; the former could be done with adjustments in pathspec?
* (That is, SCC_GET_ALL or SCC_GET_RECURSIVE)
*/
LGitLog(" flags %x", dwFlags);
LGitLog(" files %d", nFiles);
paths = (char**)calloc(sizeof(char*), nFiles);
if (paths == NULL) {
return SCC_E_NONSPECIFICERROR;
}
path_count = 0;
for (i = 0; i < nFiles; i++) {
char *path = LGitAnsiToUtf8Alloc(lpFileNames[i]);
const char *raw_path = LGitStripBasePath(ctx, path);
if (raw_path == NULL) {
LGitLog(" Couldn't get base path for %s\n", path);
free(path);
continue;
}
/* Translate because libgit2 operates with forward slashes */
LGitTranslateStringChars(path, '\\', '/');
LGitLog(" %s\n", raw_path);
paths[path_count++] = (char*)raw_path;
LGitPopCheckout(ctx, raw_path);
}
/*
for (i = 0; i < path_count; i++) {
LGitLog(" In list, %s\n", path);
}
*/
git_strarray strpaths;
strpaths.strings = paths;
strpaths.count = path_count;
/* XXX: Use opts to check if we should checkout from idx or head */
SCCRTN ret = LGitCheckoutHead(ctx, hWnd, &strpaths);
LGitFreePathList(paths, path_count);
return SCC_OK;
}
/**
* Basically undoes modified changes by doing equivalent of "git checkout".
*/
SCCRTN SccUncheckout (LPVOID context,
HWND hWnd,
LONG nFiles,
LPCSTR* lpFileNames,
LONG dwFlags,
LPCMDOPTS pvOptions)
{
LGitLog("**SccUncheckout** Context=%p\n", context);
return LGitCheckoutInternal(context, hWnd, nFiles, lpFileNames, dwFlags, pvOptions);
}
/**
* Either:
* - Replaces files with those from HEAD.
* - Fetches/pulls. Will prompt and apply to all files.
*/
SCCRTN SccGet (LPVOID context,
HWND hWnd,
LONG nFiles,
LPCSTR* lpFileNames,
LONG dwFlags,
LPCMDOPTS pvOptions)
{
LGitLog("**SccGet** Context=%p\n", context);
LGitLog(" options %p", pvOptions);
LGitGetOpts *getOpts = (LGitGetOpts*)pvOptions;
if (pvOptions != NULL && getOpts->pull) {
LGitContext *ctx = (LGitContext*)context;
return LGitPullDialog(ctx, hWnd);
} else {
return LGitCheckoutInternal(context, hWnd, nFiles, lpFileNames, dwFlags, pvOptions);
}
}
/* param is ANSI, because it's what SCC provides */
static void LGitUnmarkReadOnly(LPCSTR fileName)
{
DWORD attr;
attr = GetFileAttributesA(fileName);
if (attr == INVALID_FILE_ATTRIBUTES) {
return;
}
attr &= ~FILE_ATTRIBUTE_READONLY;
SetFileAttributesA(fileName, attr);
}
void LGitPushCheckout(LGitContext *ctx, const char *fileName)
{
ctx->checkouts->insert(std::string(fileName));
}
BOOL LGitPopCheckout(LGitContext *ctx, const char *fileName)
{
std::string name = std::string(fileName);
BOOL exists = ctx->checkouts->count(name);
if (exists) {
ctx->checkouts->erase(name);
}
return exists;
}
BOOL LGitIsCheckout(LGitContext *ctx, const char *fileName)
{
return ctx->checkouts->count(std::string(fileName));
}
SCCRTN SccCheckout (LPVOID context,
HWND hWnd,
LONG nFiles,
LPCSTR* lpFileNames,
LPCSTR lpComment,
LONG dwFlags,
LPCMDOPTS pvOptions)
{
LGitContext *ctx = (LGitContext*)context;
int i;
LGitLog("**SccCheckout** Context=%p\n", context);
LGitLog(" flags %x", dwFlags);
LGitLog(" files %d", nFiles);
/*
* VB6 and especially VS.NET will mark files read-only after checkin or
* uncheckout. VS.NET will *also* try to check the read-only status of
* files, so what we'll do is just temporarily add them to a list.
*/
for (i = 0; i < nFiles; i++) {
char path[2048];
LGitAnsiToUtf8(lpFileNames[i], path, 2048);
const char *raw_path = LGitStripBasePath(ctx, path);
if (raw_path == NULL) {
LGitLog(" Couldn't get base path for %s\n", path);
continue;
}
/* Translate because libgit2 operates with forward slashes */
LGitTranslateStringChars(path, '\\', '/');
LGitLog(" Checking out %s\n", raw_path);
LGitPushCheckout(ctx, raw_path);
/* there's no point in converting */
LGitUnmarkReadOnly(lpFileNames[i]);
}
return SCC_OK;
}