-
Notifications
You must be signed in to change notification settings - Fork 2
/
merge.cpp
executable file
·362 lines (324 loc) · 10.5 KB
/
merge.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
/*
* Convenience functions for merging..
*/
#include "stdafx.h"
SCCRTN LGitMergeFastForward(LGitContext *ctx, HWND hwnd, const git_oid *target_oid, BOOL is_unborn)
{
SCCRTN ret = SCC_OK;
LGitLog("**LGitMergeFastForward** Context=%p\n", ctx);
LGitLog(" oid %s\n", git_oid_tostr_s(target_oid));
/* adapted from perform_fastforward in examples/merge.c */
git_checkout_options ff_checkout_options;
git_checkout_options_init(&ff_checkout_options, GIT_CHECKOUT_OPTIONS_VERSION);
LGitInitCheckoutProgressCallback(ctx, &ff_checkout_options);
git_reference *target_ref = NULL;
git_reference *new_target_ref = NULL;
git_object *target = NULL;
int err = 0;
if (is_unborn) {
const char *symbolic_ref;
git_reference *head_ref;
/* HEAD reference is unborn, lookup manually so we don't try to resolve it */
err = git_reference_lookup(&head_ref, ctx->repo, "HEAD");
if (err != 0) {
LGitLibraryError(hwnd, "failed to lookup HEAD ref");
ret = SCC_E_UNKNOWNERROR;
goto fin;
}
/* Grab the reference HEAD should be pointing to */
symbolic_ref = git_reference_symbolic_target(head_ref);
/* Create our master reference on the target OID */
err = git_reference_create(&target_ref, ctx->repo, symbolic_ref, target_oid, 0, NULL);
if (err != 0) {
LGitLibraryError(hwnd, "failed to create HEAD reference");
ret = SCC_E_UNKNOWNERROR;
goto fin;
}
git_reference_free(head_ref);
} else {
/* HEAD exists, just lookup and resolve */
err = git_repository_head(&target_ref, ctx->repo);
if (err != 0) {
LGitLibraryError(hwnd, "failed to get HEAD reference");
ret = SCC_E_UNKNOWNERROR;
goto fin;
}
}
/* Lookup the target object */
err = git_object_lookup(&target, ctx->repo, target_oid, GIT_OBJECT_COMMIT);
if (err != 0) {
LGitLibraryError(hwnd, "failed to lookup OID");
ret = SCC_E_UNKNOWNERROR;
goto fin;
}
/* Checkout the result so the workdir is in the expected state */
ff_checkout_options.checkout_strategy = GIT_CHECKOUT_SAFE;
LGitInitCheckoutNotifyCallbacks(ctx, hwnd, &ff_checkout_options);
LGitProgressInit(ctx, "Fast-Forward", 0);
LGitProgressStart(ctx, hwnd, TRUE);
err = git_checkout_tree(ctx->repo, target, &ff_checkout_options);
LGitProgressDeinit(ctx);
if (err == GIT_ECONFLICT) {
LGitProgressDeinit(ctx);
/* XXX: Specific error, but checkout notify UI will cover us anyways */
ret = SCC_E_UNKNOWNERROR;
goto fin;
} else if (err != 0) {
LGitProgressDeinit(ctx);
LGitLibraryError(hwnd, "failed to checkout HEAD reference");
ret = SCC_E_UNKNOWNERROR;
goto fin;
}
/* Move the target reference to the target OID */
err = git_reference_set_target(&new_target_ref, target_ref, target_oid, NULL);
if (err != 0) {
LGitLibraryError(hwnd, "failed to move HEAD reference");
ret = SCC_E_UNKNOWNERROR;
goto fin;
}
fin:
LGitFinishCheckoutNotify(ctx, hwnd, &ff_checkout_options);
if (target_ref != NULL) {
git_reference_free(target_ref);
}
if (new_target_ref != NULL) {
git_reference_free(new_target_ref);
}
if (target!= NULL) {
git_object_free(target);
}
return ret;
}
SCCRTN LGitMergeNormal(LGitContext *ctx, HWND hwnd, const git_annotated_commit *ac, git_merge_preference_t preference)
{
LGitLog("**LGitMergeNormal** Context=%p\n", ctx);
/* adapted from perform_fastforward in examples/merge.c */
git_checkout_options co_opts;
git_checkout_options_init(&co_opts, GIT_CHECKOUT_OPTIONS_VERSION);
LGitInitCheckoutProgressCallback(ctx, &co_opts);
git_merge_options merge_opts;
git_merge_options_init(&merge_opts, GIT_MERGE_OPTIONS_VERSION);
merge_opts.flags = 0;
merge_opts.file_flags = GIT_MERGE_FILE_STYLE_DIFF3;
co_opts.checkout_strategy = GIT_CHECKOUT_FORCE|GIT_CHECKOUT_ALLOW_CONFLICTS;
if (preference & GIT_MERGE_PREFERENCE_FASTFORWARD_ONLY) {
MessageBox(hwnd,
"Fast-forward is preferred, but only a merge is possible.",
"Can't Merge",
MB_ICONERROR);
return SCC_E_UNKNOWNERROR;
}
LGitProgressInit(ctx, "Merging", 0);
LGitProgressStart(ctx, hwnd, TRUE);
int rc = git_merge(ctx->repo,
(const git_annotated_commit **)&ac, 1,
&merge_opts, &co_opts);
LGitProgressDeinit(ctx);
if (rc != 0) {
LGitLibraryError(hwnd, "git_merge");
return SCC_E_UNKNOWNERROR;
}
return SCC_OK;
}
SCCRTN LGitMerge(LGitContext *ctx, HWND hwnd, const git_annotated_commit *ann)
{
SCCRTN ret = SCC_OK;
git_merge_analysis_t merge_analysis;
git_merge_preference_t merge_preference;
git_index *index = NULL;
int repo_state;
LGitLog("**LGitMerge** Context=%p\n", ctx);
LGitLog(" annotated commit %p\n", ann);
repo_state = git_repository_state(ctx->repo);
LGitLog("! Repo state %d\n", repo_state);
if (GIT_REPOSITORY_STATE_NONE != repo_state) {
MessageBox(hwnd,
"The repository is in an unknown state; another operation may be in progress.",
"Invalid Repo State",
MB_ICONERROR);
ret = SCC_E_UNKNOWNERROR;
goto fin;
}
if (git_merge_analysis(&merge_analysis,
&merge_preference,
ctx->repo,
&ann, 1) != 0) {
LGitLibraryError(hwnd, "git_merge_analysis");
ret = SCC_E_UNKNOWNERROR;
goto fin;
}
LGitLog(" ! Analysis %x Preference %x\n", merge_analysis, merge_preference);
/* XXX: Do we need to provide file scope for SccGet to make sense? */
if (merge_analysis & GIT_MERGE_ANALYSIS_UP_TO_DATE) {
/* nothing to do */
goto fin;
} else if (merge_analysis & GIT_MERGE_ANALYSIS_FASTFORWARD) {
/* just set our branch then checkout */
const git_oid *target_oid = git_annotated_commit_id(ann);
LGitMergeFastForward(ctx,
hwnd,
target_oid,
merge_analysis & GIT_MERGE_ANALYSIS_UNBORN);
} else if (merge_analysis & GIT_MERGE_ANALYSIS_NORMAL) {
/* actually merge now */
LGitMergeNormal(ctx, hwnd, ann, merge_preference);
}
/* Check for conflicts now. */
if (git_repository_index(&index, ctx->repo) != 0) {
LGitLibraryError(hwnd, "git_merge_analysis");
ret = SCC_E_UNKNOWNERROR;
goto fin;
}
if (git_index_has_conflicts(index)) {
LGitShowMergeConflicts(ctx, hwnd, index);
} else {
/* XXX: if no conflicts, we can create a merge commit */
git_repository_state_cleanup(ctx->repo);
}
fin:
if (index != NULL) {
git_index_free(index);
}
return ret;
}
SCCRTN LGitMergeRefByName(LGitContext *ctx, HWND hwnd, const char *name)
{
SCCRTN ret = SCC_OK;
git_oid oid;
git_annotated_commit *ann = NULL;
LGitLog("**LGitMergeRefByName** Context=%p\n", ctx);
LGitLog(" name %s\n", name);
if (git_reference_name_to_id(&oid, ctx->repo, name) != 0) {
LGitLibraryError(hwnd, "git_reference_name_to_id");
ret = SCC_E_UNKNOWNERROR;
goto fin;
}
if (git_annotated_commit_lookup(&ann, ctx->repo, &oid) != 0) {
LGitLibraryError(hwnd, "git_annotated_commit_lookup");
ret = SCC_E_UNKNOWNERROR;
goto fin;
}
ret = LGitMerge(ctx, hwnd, ann);
fin:
if (ann != NULL) {
git_annotated_commit_free(ann);
}
return ret;
}
typedef struct _LGitMergeConflictDialogParams {
LGitContext *ctx;
git_index *index;
} LGitMergeConflictDialogParams;
static LVCOLUMN ancestor_column = {
LVCF_TEXT | LVCF_WIDTH, 0, 125, "Ancestor"
};
static LVCOLUMN ours_column = {
LVCF_TEXT | LVCF_WIDTH, 0, 125, "Ours"
};
static LVCOLUMN theirs_column = {
LVCF_TEXT | LVCF_WIDTH, 0, 125, "Theirs"
};
static void InitConflictView(HWND hwnd, LGitMergeConflictDialogParams* params)
{
HWND lv = GetDlgItem(hwnd, IDC_MERGE_CONFLICT_LIST);
ListView_SetExtendedListViewStyle(lv, LVS_EX_FULLROWSELECT
| LVS_EX_HEADERDRAGDROP
| LVS_EX_LABELTIP);
ListView_SetUnicodeFormat(lv, TRUE);
SendMessage(lv, WM_SETFONT, (WPARAM)params->ctx->listviewFont, TRUE);
ListView_InsertColumn(lv, 0, &ancestor_column);
ListView_InsertColumn(lv, 1, &ours_column);
ListView_InsertColumn(lv, 2, &theirs_column);
}
static void FillConflictView(HWND hwnd, LGitMergeConflictDialogParams* params)
{
git_index_conflict_iterator *conflicts = NULL;
const git_index_entry *ancestor = NULL;
const git_index_entry *our = NULL;
const git_index_entry *their = NULL;
if (git_index_conflict_iterator_new(&conflicts, params->index) != 0) {
LGitLibraryError(hwnd, "git_index_conflict_iterator_new");
return;
}
HWND lv = GetDlgItem(hwnd, IDC_MERGE_CONFLICT_LIST);
int index = 0, err = 0;
while ((err = git_index_conflict_next(&ancestor, &our, &their, conflicts)) == 0) {
LGitLog(" ! %d conflict: a:%s o:%s t:%s\n",
index,
ancestor ? ancestor->path : "NULL",
our->path ? our->path : "NULL",
their->path ? their->path : "NULL");
LVITEMW lvi;
wchar_t buf[1024];
ZeroMemory(&lvi, sizeof(LVITEMW));
lvi.mask = LVIF_TEXT;
LGitUtf8ToWide(ancestor ? ancestor->path : "", buf, 1024);
lvi.pszText = buf;
lvi.iItem = index++;
lvi.iSubItem = 0;
SendMessage(lv, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
if (lvi.iItem == -1) {
LGitLog(" ! ListView_InsertItem failed\n");
continue;
}
/* now for the subitems... */
lvi.iSubItem = 1;
LGitUtf8ToWide(our->path ? our->path : "", buf, 1024);
lvi.pszText = buf;
SendMessage(lv, LVM_SETITEMW, 0, (LPARAM)&lvi);
lvi.iSubItem = 2;
LGitUtf8ToWide(their->path ? their->path : "", buf, 1024);
lvi.pszText = buf;
SendMessage(lv, LVM_SETITEMW, 0, (LPARAM)&lvi);
}
LGitLog(" ! Finished enumeration, %d file(s), rc %d\n", index, err);
if (err != GIT_ITEROVER) {
LGitLibraryError(hwnd, "error iterating conflicts");
}
git_index_conflict_iterator_free(conflicts);
}
static BOOL CALLBACK MergeConflictDialogProc(HWND hwnd,
unsigned int iMsg,
WPARAM wParam,
LPARAM lParam)
{
LGitMergeConflictDialogParams *param;
switch (iMsg) {
case WM_INITDIALOG:
param = (LGitMergeConflictDialogParams*)lParam;
SetWindowLong(hwnd, GWL_USERDATA, (long)param); /* XXX: 64-bit... */
InitConflictView(hwnd, param);
FillConflictView(hwnd, param);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
case IDCANCEL:
EndDialog(hwnd, 1);
return TRUE;
}
return FALSE;
default:
return FALSE;
}
}
SCCRTN LGitShowMergeConflicts(LGitContext *ctx, HWND hwnd, git_index *index)
{
LGitLog("**LGitShowMergeConflicts** Context=%p\n", ctx);
LGitMergeConflictDialogParams params;
params.ctx = ctx;
params.index = index;
switch (DialogBoxParamW(ctx->dllInst,
MAKEINTRESOURCEW(IDD_MERGE_CONFLICTS),
hwnd,
MergeConflictDialogProc,
(LPARAM)¶ms)) {
case 0:
case -1:
LGitLog(" ! Uh-oh, dialog error\n");
return SCC_E_UNKNOWNERROR;
default:
break;
}
return SCC_OK;
}