-
Notifications
You must be signed in to change notification settings - Fork 2
/
gitconf.cpp
executable file
·414 lines (385 loc) · 11.3 KB
/
gitconf.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
/*
* Manage git config.
*/
#include "stdafx.h"
typedef struct _LGitEditConfigDialogParams {
LGitContext *ctx;
BOOL is_new;
wchar_t new_name[128];
wchar_t new_value[128];
} LGitEditConfigDialogParams;
static void InitConfigEditView(HWND hwnd, LGitEditConfigDialogParams* params)
{
SetDlgItemTextW(hwnd, IDC_CONFIG_EDIT_NAME, params->new_name);
HWND focus_on;
if (params->is_new) {
focus_on = GetDlgItem(hwnd, IDC_CONFIG_EDIT_NAME);
} else {
SendDlgItemMessage(hwnd, IDC_CONFIG_EDIT_NAME, EM_SETREADONLY, TRUE, 0);
/* no point in changing the name */
focus_on = GetDlgItem(hwnd, IDC_CONFIG_EDIT_VALUE);
}
SetFocus(focus_on);
SetDlgItemTextW(hwnd, IDC_CONFIG_EDIT_VALUE, params->new_value);
}
static SetConfigEditParams(HWND hwnd, LGitEditConfigDialogParams* params)
{
if (params->is_new) {
GetDlgItemTextW(hwnd, IDC_CONFIG_EDIT_NAME, params->new_name, 128);
}
GetDlgItemTextW(hwnd, IDC_CONFIG_EDIT_VALUE, params->new_value, 128);
}
static BOOL CALLBACK ConfigEditorDialogProc(HWND hwnd,
unsigned int iMsg,
WPARAM wParam,
LPARAM lParam)
{
LGitEditConfigDialogParams *param;
switch (iMsg) {
case WM_INITDIALOG:
param = (LGitEditConfigDialogParams*)lParam;
SetWindowLong(hwnd, GWL_USERDATA, (long)param); /* XXX: 64-bit... */
InitConfigEditView(hwnd, param);
/* NOT an error to return false, but allows changing default focus */
return FALSE;
case WM_COMMAND:
param = (LGitEditConfigDialogParams*)GetWindowLong(hwnd, GWL_USERDATA);
switch (LOWORD(wParam)) {
case IDOK:
SetConfigEditParams(hwnd, param);
EndDialog(hwnd, 2);
return TRUE;
case IDCANCEL:
EndDialog(hwnd, 1);
return TRUE;
}
return FALSE;
default:
return FALSE;
}
}
typedef struct _LGitConfigDialogParams {
LGitContext *ctx;
git_config *config;
const char *title;
} LGitConfigDialogParams;
static const wchar_t *LevelName(git_config_level_t level)
{
switch (level) {
case GIT_CONFIG_LEVEL_PROGRAMDATA:
/* Windows system-wide, implied for compat only? */
return L"System (ProgramData)";
case GIT_CONFIG_LEVEL_SYSTEM:
/* system-wide config */
return L"System";
case GIT_CONFIG_LEVEL_XDG:
/* ~/.config/git/config; can this ever trigger on Windows? */
return L"User (XDG)";
case GIT_CONFIG_LEVEL_GLOBAL:
/* User level config, usually ~/.gitconfig */
return L"User (Global)";
case GIT_CONFIG_LEVEL_LOCAL:
/* Repo .git/config */
return L"Repository";
case GIT_CONFIG_LEVEL_APP:
/* defined by us */
return L"Application-Defined";
case GIT_CONFIG_HIGHEST_LEVEL:
/* XXX: shouldn't happen? */
return L"Highest Level";
default:
return L"Unknown";
}
}
static LVCOLUMN name_column = {
LVCF_TEXT | LVCF_WIDTH, 0, 150, "Name"
};
static LVCOLUMN value_column = {
LVCF_TEXT | LVCF_WIDTH, 0, 250, "Value"
};
static LVCOLUMN from_column = {
LVCF_TEXT | LVCF_WIDTH, 0, 100, "From"
};
typedef struct _LGitConfigIterArgs {
HWND lv;
int index;
} LGitConfigIterArgs;
int ConfigForeach(const git_config_entry *e, void *payload)
{
LGitConfigIterArgs *args = (LGitConfigIterArgs*)payload;
HWND lv = args->lv;
/*
LGitLog(" (depth %d level %S) %s -> %s\n",
e->include_depth,
LevelName(e->level),
e->name,
e->value);
*/
wchar_t buf[1024];
LGitUtf8ToWide(e->name, buf, 1024);
/*
* We should check if the item is already existant. If so, replace if the
* level is higher. Otherwise, it can be confusing because operations will
* take effect on the highest level config available.
*/
LVITEMW lvi;
ZeroMemory(&lvi, sizeof(LVITEM));
LVFINDINFOW lvfi;
lvfi.flags = LVFI_STRING;
lvfi.psz = buf;
int oldIndex = ListView_FindItem(lv, -1, &lvfi);
SendMessage(lv, LVM_FINDITEMW, -1, (LPARAM)&lvfi);
int oldLevel = 0;
if (oldIndex != -1) {
lvi.iItem = oldIndex;
lvi.iSubItem = 0;
lvi.mask = LVIF_PARAM;
SendMessage(lv, LVM_GETITEMW, 0, (LPARAM)&lvi);
oldLevel = lvi.lParam;
}
/* If the level is higher, update, else insert */
ZeroMemory(&lvi, sizeof(LVITEM));
if (oldIndex != -1 && e->level >= oldLevel) {
lvi.iItem = oldIndex;
lvi.iSubItem = 0;
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.pszText = buf;
lvi.lParam = e->level;
SendMessage(lv, LVM_SETITEMW, 0, (LPARAM)&lvi);
} else {
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.pszText = (wchar_t*)buf;
lvi.lParam = e->level;
lvi.iItem = args->index++;
lvi.iSubItem = 0;
SendMessage(lv, LVM_INSERTITEMW, 0, (LPARAM)&lvi);
if (lvi.iItem == -1) {
LGitLog(" ! ListView_InsertItem failed\n");
return -1;
}
}
/* now for the subitems... */
LGitUtf8ToWide(e->value, buf, 1024);
lvi.mask = LVIF_TEXT;
lvi.iSubItem = 1;
lvi.pszText = (wchar_t*)buf;
SendMessage(lv, LVM_SETITEMW, 0, (LPARAM)&lvi);
lvi.iSubItem = 2;
lvi.pszText = (wchar_t*)LevelName(e->level);
SendMessage(lv, LVM_SETITEMW, 0, (LPARAM)&lvi);
return 0;
}
static void InitConfigView(HWND hwnd, LGitConfigDialogParams *params)
{
HWND lv = GetDlgItem(hwnd, IDC_CONFIG_LIST);
ListView_SetUnicodeFormat(lv, TRUE);
SendMessage(lv, WM_SETFONT, (WPARAM)params->ctx->listviewFont, TRUE);
/* I think ANSI InsertColumn is still OK with Unicode for now */
ListView_InsertColumn(lv, 0, &name_column);
ListView_InsertColumn(lv, 1, &value_column);
ListView_InsertColumn(lv, 2, &from_column);
char new_title[128];
_snprintf(new_title, 128, "Git %s Config", params->title);
SetWindowText(hwnd, new_title);
}
static void FillConfigView(HWND hwnd, LGitConfigDialogParams *params)
{
HWND lv = GetDlgItem(hwnd, IDC_CONFIG_LIST);
LGitConfigIterArgs args;
args.lv = lv;
args.index = 0;
ListView_DeleteAllItems(lv);
if (git_config_foreach(params->config, ConfigForeach, &args) != 0) {
LGitLibraryError(hwnd, "git_config_foreach");
}
}
static BOOL GetSelectedConfig(HWND hwnd, wchar_t *buf, size_t bufsz)
{
HWND lv = GetDlgItem(hwnd, IDC_CONFIG_LIST);
if (lv == NULL) {
return FALSE;
}
int selected = ListView_GetNextItem(lv, -1, LVNI_SELECTED);
if (selected == -1) {
return FALSE;
}
LVITEMW lvi;
ZeroMemory(&lvi, sizeof(lvi));
lvi.mask = LVIF_TEXT;
lvi.iItem = selected; /* in case */
lvi.pszText = buf;
lvi.cchTextMax = bufsz;
return SendMessage(lv, LVM_GETITEMTEXTW, selected, (LPARAM)&lvi) > 0;
}
/**
* Name and value are used to pre-fill the dialog; they aren't written to.
*/
static void ConfigEditDialog(HWND hwnd, LGitConfigDialogParams *params, const wchar_t *name, const wchar_t *value, BOOL isNew)
{
LGitEditConfigDialogParams ec_params;
ZeroMemory(&ec_params, sizeof(LGitEditConfigDialogParams));
ec_params.ctx = params->ctx;
ec_params.is_new = isNew;
wcslcpy(ec_params.new_name, name, 128);
wcslcpy(ec_params.new_value, value, 128);
switch (DialogBoxParamW(params->ctx->dllInst,
MAKEINTRESOURCEW(IDD_CONFIG_EDIT),
hwnd,
ConfigEditorDialogProc,
(LPARAM)&ec_params)) {
case 0:
case -1:
LGitLog(" ! Uh-oh, dialog error\n");
return;
case 1:
return;
case 2:
break;
}
if (wcslen(ec_params.new_value) > 0) {
/* convert */
char new_name[256];
char new_value[256];
LGitWideToUtf8(ec_params.new_name, new_name, 256);
LGitWideToUtf8(ec_params.new_value, new_value, 256);
if (git_config_set_string(params->config, new_name, new_value) != 0) {
LGitLibraryError(hwnd, "git_config_set_string");
return;
}
}
FillConfigView(hwnd, params);
}
static void ConfigEdit(HWND hwnd, LGitConfigDialogParams *params)
{
wchar_t name[128];
char name_utf8[128];
if (!GetSelectedConfig(hwnd, name, 128)) {
LGitLog(" ! No config?\n");
return;
}
LGitWideToUtf8(name, name_utf8, 128);
/*
* XXX: Check if we're editing in the same level. If not, a new value is
* inserted at the higher level which supersedes the lower one.
*/
git_buf old_val_buf = {0};
wchar_t old_val_utf16[128];
if (git_config_get_string_buf(&old_val_buf, params->config, name_utf8) != 0) {
LGitLibraryError(hwnd, "git_config_get_string_buf");
return;
}
LGitUtf8ToWide(old_val_buf.ptr, old_val_utf16, 128);
LGitLog(" ! Editing %s?\n", name_utf8);
ConfigEditDialog(hwnd, params, name, old_val_utf16, FALSE);
git_buf_dispose(&old_val_buf);
}
static void ConfigRemove(HWND hwnd, LGitConfigDialogParams *params)
{
/* XXX: Multiple selection could be handy here */
wchar_t name[128];
char name_utf8[128];
if (!GetSelectedConfig(hwnd, name, 128)) {
LGitLog(" ! No config?\n");
return;
}
LGitWideToUtf8(name, name_utf8, 128);
if (MessageBox(hwnd,
"This configuration entry will be deleted from its source. "
"It may be replaced with an inherited configuration. Are you sure?",
"Remove Config Entry?",
MB_ICONWARNING | MB_YESNO) != IDYES) {
return;
}
if (git_config_delete_entry(params->config, name_utf8) != 0) {
LGitLibraryError(hwnd, "git_config_delete_entry");
return;
}
/* Optimization would be removing the list view item, if not for levels */
FillConfigView(hwnd, params);
}
static void ConfigAdd(HWND hwnd, LGitConfigDialogParams *params)
{
ConfigEditDialog(hwnd, params, L"", L"", TRUE);
}
static void UpdateConfigButtons(HWND hwnd, LGitConfigDialogParams *params)
{
HWND lv = GetDlgItem(hwnd, IDC_CONFIG_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_CONFIG_DELETE);
set_btn = GetDlgItem(hwnd, IDC_CONFIG_SET);
EnableWindow(remove_btn, selected > 0);
EnableWindow(set_btn, selected > 0);
}
static BOOL CALLBACK ConfigManagerDialogProc(HWND hwnd,
unsigned int iMsg,
WPARAM wParam,
LPARAM lParam)
{
LGitConfigDialogParams *param = (LGitConfigDialogParams*)GetWindowLong(hwnd, GWL_USERDATA);
switch (iMsg) {
case WM_INITDIALOG:
param = (LGitConfigDialogParams*)lParam;
SetWindowLong(hwnd, GWL_USERDATA, (long)param); /* XXX: 64-bit... */
InitConfigView(hwnd, param);
FillConfigView(hwnd, param);
UpdateConfigButtons(hwnd, param);
return TRUE;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDC_CONFIG_SET:
ConfigEdit(hwnd, param);
return TRUE;
case IDC_CONFIG_DELETE:
ConfigRemove(hwnd, param);
return TRUE;
case IDC_CONFIG_ADD:
ConfigAdd(hwnd, param);
return TRUE;
case IDOK:
case IDCANCEL:
EndDialog(hwnd, 1);
return TRUE;
}
return FALSE;
case WM_NOTIFY:
switch (wParam) {
case IDC_CONFIG_LIST:
LPNMHDR child_msg = (LPNMHDR)lParam;
switch (child_msg->code) {
case LVN_ITEMACTIVATE:
ConfigEdit(hwnd, param);
return TRUE;
case LVN_ITEMCHANGED:
UpdateConfigButtons(hwnd, param);
return TRUE;
}
}
return FALSE;
default:
return FALSE;
}
}
SCCRTN LGitManageConfig(LGitContext *ctx, HWND hwnd, git_config *config, const char *title)
{
LGitLog("**LGitManageConfig** Context=%p\n", ctx);
LGitConfigDialogParams params;
ZeroMemory(¶ms, sizeof(LGitConfigDialogParams));
params.ctx = ctx;
params.config = config;
params.title = title;
switch (DialogBoxParamW(ctx->dllInst,
MAKEINTRESOURCEW(IDD_GITCONFIG),
hwnd,
ConfigManagerDialogProc,
(LPARAM)¶ms)) {
case 0:
case -1:
LGitLog(" ! Uh-oh, dialog error\n");
return SCC_E_UNKNOWNERROR;
default:
break;
}
return SCC_OK;
}