-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathremote.cpp
executable file
·489 lines (465 loc) · 13.6 KB
/
remote.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
* Remote management and the dialog.
*/
#include "stdafx.h"
typedef struct _LGitEditRemoteDialogParams {
LGitContext *ctx;
BOOL is_new;
char new_name[128];
char new_url[128];
char new_push_url[128];
} LGitEditRemoteDialogParams;
static void InitRemoteEditView(HWND hwnd, LGitEditRemoteDialogParams* params)
{
wchar_t buf[128];
LGitUtf8ToWide(params->new_name, buf, 128);
SetDlgItemTextW(hwnd, IDC_REMOTE_EDIT_NAME, buf);
if (!params->is_new) {
SendDlgItemMessage(hwnd, IDC_REMOTE_EDIT_NAME, EM_SETREADONLY, TRUE, 0);
}
LGitUtf8ToWide(params->new_url, buf, 128);
SetDlgItemTextW(hwnd, IDC_REMOTE_EDIT_URL, buf);
LGitUtf8ToWide(params->new_push_url, buf, 128);
SetDlgItemTextW(hwnd, IDC_REMOTE_EDIT_PUSHURL, buf);
}
static SetRemoteEditParams(HWND hwnd, LGitEditRemoteDialogParams* params)
{
wchar_t buf[128];
if (params->is_new) {
GetDlgItemTextW(hwnd, IDC_REMOTE_EDIT_NAME, buf, 128);
LGitWideToUtf8(buf, params->new_name, 128);
}
GetDlgItemTextW(hwnd, IDC_REMOTE_EDIT_URL, buf, 128);
LGitWideToUtf8(buf, params->new_url, 128);
GetDlgItemTextW(hwnd, IDC_REMOTE_EDIT_PUSHURL, buf, 128);
LGitWideToUtf8(buf, params->new_push_url, 128);
}
static BOOL CALLBACK RemoteEditorDialogProc(HWND hwnd,
unsigned int iMsg,
WPARAM wParam,
LPARAM lParam)
{
LGitEditRemoteDialogParams *param;
switch (iMsg) {
case WM_INITDIALOG:
param = (LGitEditRemoteDialogParams*)lParam;
SetWindowLong(hwnd, GWL_USERDATA, (long)param); /* XXX: 64-bit... */
InitRemoteEditView(hwnd, param);
return TRUE;
case WM_COMMAND:
param = (LGitEditRemoteDialogParams*)GetWindowLong(hwnd, GWL_USERDATA);
switch (LOWORD(wParam)) {
case IDOK:
SetRemoteEditParams(hwnd, param);
EndDialog(hwnd, 2);
return TRUE;
case IDCANCEL:
EndDialog(hwnd, 1);
return TRUE;
}
return FALSE;
default:
return FALSE;
}
}
typedef struct _LGitRemoteDialogParams {
LGitContext *ctx;
/* for label editor */
wchar_t old_name[128];
BOOL editing;
} LGitRemoteDialogParams;
static LVCOLUMN name_column = {
LVCF_TEXT | LVCF_WIDTH, 0, 75, "Name"
};
static LVCOLUMN url_column = {
LVCF_TEXT | LVCF_WIDTH, 0, 250, "URL"
};
static LVCOLUMN pushurl_column = {
LVCF_TEXT | LVCF_WIDTH, 0, 225, "Push URL"
};
static void InitRemoteView(HWND hwnd, LGitRemoteDialogParams* params)
{
HWND lv = GetDlgItem(hwnd, IDC_REMOTE_LIST);
ListView_SetUnicodeFormat(lv, TRUE);
SendMessage(lv, WM_SETFONT, (WPARAM)params->ctx->listviewFont, TRUE);
ListView_InsertColumn(lv, 0, &name_column);
ListView_InsertColumn(lv, 1, &url_column);
ListView_InsertColumn(lv, 2, &pushurl_column);
}
static void FillRemoteView(HWND hwnd, LGitRemoteDialogParams* params)
{
HWND lv = GetDlgItem(hwnd, IDC_REMOTE_LIST);
size_t i, index = 0;
/* clear if we're replenishing */
ListView_DeleteAllItems(lv);
/* similar to the one in winutils.cpp */
git_strarray remotes;
ZeroMemory(&remotes, sizeof(git_strarray));
if (git_remote_list(&remotes, params->ctx->repo) != 0) {
LGitLibraryError(hwnd, "git_remote_list");
return;
}
LGitLog(" ! Got back %d remote(s)\n", remotes.count);
for (i = 0; i < remotes.count; i++) {
const char *name = remotes.strings[i];
LGitLog(" ! Adding remote %s\n", name);
/* our use for the remote object is short-lived */
git_remote *remote = NULL;
const char *url = NULL, *push_url = NULL;
/* in theory we could check for not found/invalid spec, but... */
if (git_remote_lookup(&remote, params->ctx->repo, name) != 0) {
LGitLibraryError(hwnd, "git_remote_list");
continue;
}
url = git_remote_url(remote);
if (url == NULL) {
url = "";
}
push_url = git_remote_pushurl(remote);
if (push_url == NULL) {
push_url = "";
}
wchar_t buf[128];
LVITEMW lvi;
ZeroMemory(&lvi, sizeof(LVITEMW));
lvi.mask = LVIF_TEXT;
LGitUtf8ToWide(name, buf, 128);
lvi.pszText = buf;
lvi.iItem = index++;
lvi.iSubItem = 0;
lvi.iItem = SendMessage(lv, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
if (lvi.iItem == -1) {
LGitLog(" ! ListView_InsertItem failed\n");
goto inner_fail;
}
/* now for the subitems... */
lvi.iSubItem = 1;
LGitUtf8ToWide(url, buf, 128);
lvi.pszText = buf;
SendMessage(lv, LVM_SETITEMW, 0, (LPARAM)&lvi);
lvi.iSubItem = 2;
LGitUtf8ToWide(push_url, buf, 128);
lvi.pszText = buf;
SendMessage(lv, LVM_SETITEMW, 0, (LPARAM)&lvi);
inner_fail:
git_remote_free(remote);
}
git_strarray_dispose(&remotes);
}
static BOOL GetSelectedRemote(HWND hwnd, char *buf, size_t bufsz)
{
HWND lv = GetDlgItem(hwnd, IDC_REMOTE_LIST);
if (lv == NULL) {
return FALSE;
}
int selected = ListView_GetNextItem(lv, -1, LVNI_SELECTED);
if (selected == -1) {
return FALSE;
}
wchar_t temp_buf[1024];
LVITEMW lvi;
ZeroMemory(&lvi, sizeof(lvi));
lvi.mask = LVIF_TEXT;
lvi.iItem = selected;
lvi.iSubItem = 0;
lvi.pszText = temp_buf;
lvi.cchTextMax = 1024;
if (SendMessage(lv, LVM_GETITEMTEXTW, selected, (LPARAM)&lvi) < 1) {
return FALSE;
}
LGitWideToUtf8(temp_buf, buf, bufsz);
return TRUE;
}
static void RemoteAdd(HWND hwnd, LGitRemoteDialogParams* params)
{
LGitEditRemoteDialogParams er_params;
ZeroMemory(&er_params, sizeof(LGitEditRemoteDialogParams));
er_params.ctx = params->ctx;
er_params.is_new = TRUE;
switch (DialogBoxParamW(params->ctx->dllInst,
MAKEINTRESOURCEW(IDD_REMOTE_EDIT),
hwnd,
RemoteEditorDialogProc,
(LPARAM)&er_params)) {
case 0:
LGitLog(" ! Uh-oh, dialog error\n");
return;
case 1:
return;
case 2:
break;
}
git_remote *remote;
switch (git_remote_create(&remote, params->ctx->repo, er_params.new_name, er_params.new_url)) {
case 0:
break;
case GIT_EINVALIDSPEC:
MessageBox(hwnd,
"The remote has an invalid name.",
"Invalid Remote",
MB_ICONERROR);
return;
case GIT_EEXISTS:
MessageBox(hwnd,
"The remote by that name already exists.",
"Invalid Remote",
MB_ICONERROR);
return;
default:
LGitLibraryError(hwnd, "git_remote_create");
return;
}
if (strlen(er_params.new_push_url) > 0) {
if (git_remote_set_pushurl(params->ctx->repo, er_params.new_name, er_params.new_push_url) != 0) {
LGitLibraryError(hwnd, "git_remote_set_pushurl");
goto fin;
}
}
/* Optimization would be adding the list view item */
FillRemoteView(hwnd, params);
fin:
if (remote != NULL) {
git_remote_free(remote);
}
}
static void RemoteEdit(HWND hwnd, LGitRemoteDialogParams* params)
{
char name[128];
if (!GetSelectedRemote(hwnd, name, 128)) {
LGitLog(" ! No remote?\n");
return;
}
LGitLog(" ! Editing %s?\n", name);
git_remote *remote = NULL;
if (git_remote_lookup(&remote, params->ctx->repo, name) != 0) {
LGitLibraryError(hwnd, "git_remote_lookup");
return;
}
LGitEditRemoteDialogParams er_params;
ZeroMemory(&er_params, sizeof(LGitEditRemoteDialogParams));
er_params.ctx = params->ctx;
er_params.is_new = FALSE;
strlcpy(er_params.new_name, name, 128);
const char *old_url = git_remote_url(remote);
strlcpy(er_params.new_url, old_url == NULL ? "" : old_url, 128);
const char *old_push_url = git_remote_pushurl(remote);
strlcpy(er_params.new_push_url, old_push_url == NULL ? "" : old_push_url, 128);
switch (DialogBoxParamW(params->ctx->dllInst,
MAKEINTRESOURCEW(IDD_REMOTE_EDIT),
hwnd,
RemoteEditorDialogProc,
(LPARAM)&er_params)) {
case 0:
case -1:
LGitLog(" ! Uh-oh, dialog error\n");
goto fin;
case 1:
goto fin;
case 2:
break;
}
if (strlen(er_params.new_url) > 0) {
if (git_remote_set_url(params->ctx->repo, name, er_params.new_url) != 0) {
LGitLibraryError(hwnd, "git_remote_set_url");
goto fin;
}
}
if (strlen(er_params.new_push_url) > 0) {
if (git_remote_set_pushurl(params->ctx->repo, name, er_params.new_push_url) != 0) {
LGitLibraryError(hwnd, "git_remote_set_pushurl");
goto fin;
}
}
/* Optimization would be editing the list view subitems. */
FillRemoteView(hwnd, params);
fin:
if (remote != NULL) {
git_remote_free(remote);
}
}
static void RemoveRemote(HWND hwnd, LGitRemoteDialogParams* params)
{
char name[128];
if (!GetSelectedRemote(hwnd, name, 128)) {
LGitLog(" ! No remote?\n");
return;
}
LGitLog(" ! Removing %s?\n", name);
if (MessageBox(hwnd,
"All remote tracking branches and settings for the remote will be removed.",
"Remove Remote?",
MB_ICONWARNING | MB_YESNO) != IDYES) {
return;
}
if (git_remote_delete(params->ctx->repo, name) != 0) {
LGitLibraryError(hwnd, "git_remote_delete");
return;
}
/* Optimization would be removing the list view item */
FillRemoteView(hwnd, params);
}
static BOOL BeginRemoteRename(HWND hwnd, LGitRemoteDialogParams* params, UINT index)
{
HWND lv = GetDlgItem(hwnd, IDC_REMOTE_LIST);
if (lv == NULL) {
return FALSE;
}
LGitLog(" ! Begin rename for %u\n", index);
LVITEMW lvi;
ZeroMemory(&lvi, sizeof(lvi));
lvi.mask = LVIF_TEXT;
lvi.iItem = index;
lvi.iSubItem = 0;
lvi.pszText = params->old_name;
lvi.cchTextMax = 128;
if (SendMessage(lv, LVM_GETITEMTEXTW, index, (LPARAM)&lvi) < 1) {
LGitLog("!! Failed to get rename text for %u\n", index);
return FALSE;
}
params->editing = TRUE;
return TRUE;
}
static BOOL RemoteRename(HWND hwnd, LGitRemoteDialogParams* params, const wchar_t *new_name_utf16)
{
LGitLog(" ! Renaming %S to %S\n", params->old_name, new_name_utf16);
params->editing = FALSE;
/* Check if it's the same name or null; lg2 will throw an error if so. */
if (new_name_utf16 == NULL || wcscmp(params->old_name, new_name_utf16) == 0) {
return FALSE;
}
/* keep semantics */
char new_name[128], old_name[128];
LGitWideToUtf8(new_name_utf16, new_name, 128);
LGitWideToUtf8(params->old_name, old_name, 128);
/*
* This is slightly awkward because we have the old name in params because
* of how the ListView label edit messages are structured.
*/
git_strarray problems = {0,0};
switch (git_remote_rename(&problems, params->ctx->repo, old_name, new_name)) {
case 0:
/* XXX: Report problems in their own view. For now, log */
if (problems.count > 0) {
for (size_t i = 0; i < problems.count; i++) {
LGitLog(" ! Problem renaming %s\n", problems.strings[i]);
}
MessageBox(hwnd,
"The remote was renamed successfully, but some refspecs couldn't be renamed.",
"Problems Renaming",
MB_ICONERROR);
}
git_strarray_dispose(&problems);
/* Optimization would be replacing the list view item label */
FillRemoteView(hwnd, params);
return TRUE;
/* XXX: These messages are common with New */
case GIT_EINVALIDSPEC:
MessageBox(hwnd,
"The remote has an invalid name.",
"Invalid Remote",
MB_ICONERROR);
return FALSE;
case GIT_EEXISTS:
MessageBox(hwnd,
"The remote by that name already exists.",
"Invalid Remote",
MB_ICONERROR);
return FALSE;
default:
LGitLibraryError(hwnd, "git_remote_rename");
return FALSE;
}
}
static void UpdateRemoteButtons(HWND hwnd, LGitRemoteDialogParams *params)
{
HWND lv = GetDlgItem(hwnd, IDC_REMOTE_LIST);
UINT selected = ListView_GetSelectedCount(lv);
/* there's no dlgitem ver. XXX: don't enable set if > 1, bc rm possible */
HWND remove_btn, set_btn;
remove_btn = GetDlgItem(hwnd, IDC_REMOTE_DELETE);
set_btn = GetDlgItem(hwnd, IDC_REMOTE_SETURL);
EnableWindow(remove_btn, selected > 0);
EnableWindow(set_btn, selected > 0);
}
static BOOL CALLBACK RemoteManagerDialogProc(HWND hwnd,
unsigned int iMsg,
WPARAM wParam,
LPARAM lParam)
{
LGitRemoteDialogParams *param;
switch (iMsg) {
case WM_INITDIALOG:
param = (LGitRemoteDialogParams*)lParam;
SetWindowLong(hwnd, GWL_USERDATA, (long)param); /* XXX: 64-bit... */
InitRemoteView(hwnd, param);
FillRemoteView(hwnd, param);
UpdateRemoteButtons(hwnd, param);
return TRUE;
case WM_COMMAND:
param = (LGitRemoteDialogParams*)GetWindowLong(hwnd, GWL_USERDATA);
if (param->editing) {
/* so editing in the label won't close the dialog */
return TRUE;
}
switch (LOWORD(wParam)) {
case IDC_REMOTE_SETURL:
RemoteEdit(hwnd, param);
return TRUE;
case IDC_REMOTE_DELETE:
RemoveRemote(hwnd, param);
return TRUE;
case IDC_REMOTE_ADD:
RemoteAdd(hwnd, param);
return TRUE;
case IDOK:
case IDCANCEL:
EndDialog(hwnd, 1);
return TRUE;
}
return FALSE;
case WM_NOTIFY:
param = (LGitRemoteDialogParams*)GetWindowLong(hwnd, GWL_USERDATA);
switch (wParam) {
case IDC_REMOTE_LIST:
LPNMHDR child_msg = (LPNMHDR)lParam;
LPNMITEMACTIVATE child_activate = (LPNMITEMACTIVATE)lParam;
NMLVDISPINFOW *child_edit = (NMLVDISPINFOW*)lParam;
HWND lv = (HWND)wParam;
switch (child_msg->code) {
case LVN_ITEMACTIVATE:
ListView_EditLabel(lv, child_activate->iItem);
return TRUE;
case LVN_BEGINLABELEDITW:
return BeginRemoteRename(hwnd, param, child_edit->item.iItem);
case LVN_ENDLABELEDITW:
return RemoteRename(hwnd, param, child_edit->item.pszText);
case LVN_ITEMCHANGED:
UpdateRemoteButtons(hwnd, param);
return TRUE;
}
}
return FALSE;
default:
return FALSE;
}
}
SCCRTN LGitShowRemoteManager(LGitContext *ctx, HWND hwnd)
{
LGitLog("**LGitShowRemoteManager** Context=%p\n", ctx);
LGitRemoteDialogParams params;
ZeroMemory(¶ms, sizeof(LGitRemoteDialogParams));
params.ctx = ctx;
switch (DialogBoxParamW(ctx->dllInst,
MAKEINTRESOURCEW(IDD_REMOTES),
hwnd,
RemoteManagerDialogProc,
(LPARAM)¶ms)) {
case 0:
case -1:
LGitLog(" ! Uh-oh, dialog error\n");
return SCC_E_UNKNOWNERROR;
default:
break;
}
return SCC_OK;
}