-
Notifications
You must be signed in to change notification settings - Fork 2
/
clone.cpp
executable file
·256 lines (236 loc) · 7 KB
/
clone.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
/*
* Git clone dialog.
*/
#include "stdafx.h"
typedef struct _LGitCloneDialogParams {
LGitContext *ctx;
/* Path is in/out, rest all out */
char url[256];
char path[_MAX_PATH];
char branch[128];
BOOL pathWritten;
} LGitCloneDialogParams;
static void InitCloneView(HWND hwnd, LGitCloneDialogParams* params)
{
wchar_t path[_MAX_PATH];
LGitUtf8ToWide(params->path, path, _MAX_PATH);
SetDlgItemTextW(hwnd, IDC_CLONE_PATH, path);
}
static void BrowseForFolder(HWND hwnd, LGitCloneDialogParams* params)
{
wchar_t path[_MAX_PATH];
GetDlgItemTextW(hwnd, IDC_CLONE_PATH, path, _MAX_PATH);
if (LGitBrowseForFolder(hwnd, L"Browse for Repository Folder", path, _MAX_PATH)) {
SetDlgItemTextW(hwnd, IDC_CLONE_PATH, path);
LGitWideToUtf8(path, params->path, _MAX_PATH);
}
}
static BOOL ValidateAndSetParams(HWND hwnd, LGitCloneDialogParams* params)
{
int rc, valid;
wchar_t buf[1024];
GetDlgItemTextW(hwnd, IDC_CLONE_URL, buf, 1024);
LGitWideToUtf8(buf, params->url, 256);
if (strlen(params->url) == 0) {
MessageBox(hwnd,
"There was no URL given to clone.",
"Invalid URL", MB_ICONERROR);
return FALSE;
}
GetDlgItemTextW(hwnd, IDC_CLONE_PATH, buf, 1024);
LGitWideToUtf8(buf, params->path, _MAX_PATH);
if (strlen(params->path) == 0) {
MessageBox(hwnd,
"The path is empty.",
"Invalid Path", MB_ICONERROR);
return FALSE;
}
/*
* XXX: Validate that either everything leading up to this directory
* exists, or that the directory is empty.
*/
GetDlgItemTextW(hwnd, IDC_CLONE_BRANCH, buf, 1024);
LGitWideToUtf8(buf, params->branch, 128);
/* Empty branch name -> default */
if (strlen(params->branch) > 0) {
rc = git_branch_name_is_valid(&valid, params->branch);
if (rc != 0 || !valid) {
MessageBox(hwnd,
"The branch name is improperly formed.",
"Invalid Branch", MB_ICONERROR);
return FALSE;
}
}
return TRUE;
}
#if 0
/**
* Tries to append the most important component of the URL to the path.
* Disabled because there's no way to call it from a dialog that I know of.
*/
static void BuildPath(HWND hwnd, LGitCloneDialogParams* params)
{
HWND focused;
if (params->pathWritten == TRUE) {
return;
}
focused = GetFocus();
if (GetDlgItem(hwnd, IDC_CLONE_PATH) == focused) {
LGitLog(" ! Path written to custom, ignore it now\n");
params->pathWritten = TRUE;
} else if (GetDlgItem(hwnd, IDC_CLONE_URL) != focused) {
return;
}
char new_path[_MAX_PATH], url[256], *url_begin, *suffix;
GetDlgItemText(hwnd, IDC_CLONE_URL, url, 256);
LGitLog("!! %s\n", url);
url_begin = strrchr(url, '/');
if (url_begin == NULL || strlen(url_begin) == 0) {
return;
}
LGitLog("!! %s\n", url_begin);
/* remove the .git often seen on SSH urls */
suffix = strrchr(url_begin, '.');
if (suffix != NULL) {
suffix[0] = '\0';
}
LGitLog("!! %s\n", suffix);
strlcpy(new_path, params->path, _MAX_PATH);
strlcat(new_path, url_begin, _MAX_PATH);
SetDlgItemText(hwnd, IDC_CLONE_PATH, new_path);
}
#endif
static BOOL UseExisting(HWND hwnd, LGitCloneDialogParams* params)
{
wchar_t path[_MAX_PATH];
LGitUtf8ToWide(params->path, path, _MAX_PATH);
if (LGitBrowseForFolder(hwnd, L"Browse for Existing Repository Folder", path, _MAX_PATH)) {
SetDlgItemTextW(hwnd, IDC_CLONE_PATH, path);
LGitWideToUtf8(path, params->path, _MAX_PATH);
return TRUE;
}
return FALSE;
}
static BOOL CALLBACK CloneDialogProc(HWND hwnd,
unsigned int iMsg,
WPARAM wParam,
LPARAM lParam)
{
LGitCloneDialogParams *param;
/* TODO: We should try to derive a path from the URL until overriden */
switch (iMsg) {
case WM_INITDIALOG:
param = (LGitCloneDialogParams*)lParam;
param->pathWritten = FALSE;
SetWindowLong(hwnd, GWL_USERDATA, (long)param); /* XXX: 64-bit... */
InitCloneView(hwnd, param);
return TRUE;
case WM_COMMAND:
param = (LGitCloneDialogParams*)GetWindowLong(hwnd, GWL_USERDATA);
switch (LOWORD(wParam)) {
case IDC_CLONE_BROWSE:
BrowseForFolder(hwnd, param);
return TRUE;
case IDOK:
if (ValidateAndSetParams(hwnd, param)) {
EndDialog(hwnd, 2);
}
return TRUE;
case IDCANCEL:
EndDialog(hwnd, 1);
return TRUE;
case IDC_CLONE_EXISTING:
/* This exists for "Add Existing Project from Source Control" */
if (UseExisting(hwnd, param)) {
EndDialog(hwnd, 3);
}
return TRUE;
}
return FALSE;
default:
return FALSE;
}
}
LGIT_API SCCRTN LGitClone(LGitContext *ctx,
HWND hWnd,
LPSTR lpProjName,
LPSTR lpLocalPath,
LPBOOL pbNew)
{
SCCRTN ret = SCC_OK;
BOOL isNew = FALSE;
/* The repository is created, but we'll re-open in SccOpenProject */
git_repository *temp_repo;
git_clone_options clone_opts;
git_checkout_options co_opts;
git_fetch_options fetch_opts;
LGitCloneDialogParams params;
git_checkout_options_init(&co_opts, GIT_CHECKOUT_OPTIONS_VERSION);
co_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
LGitInitCheckoutProgressCallback(ctx, &co_opts);
git_fetch_options_init(&fetch_opts, GIT_FETCH_OPTIONS_VERSION);
git_clone_options_init(&clone_opts, GIT_CLONE_OPTIONS_VERSION);
clone_opts.checkout_opts = co_opts;
LGitInitRemoteCallbacks(ctx, hWnd, &fetch_opts.callbacks);
clone_opts.fetch_opts = fetch_opts;
LGitLog(" ! Clone\n");
ZeroMemory(¶ms, sizeof(LGitCloneDialogParams));
params.ctx = ctx;
strlcpy(params.path, lpLocalPath, _MAX_PATH);
switch (DialogBoxParamW(ctx->dllInst,
MAKEINTRESOURCEW(IDD_CLONE),
hWnd,
CloneDialogProc,
(LPARAM)¶ms)) {
case 0:
case -1:
LGitLog(" ! Uh-oh, dialog error\n");
ret = SCC_E_NONSPECIFICERROR;
goto fin;
case 1:
ret = SCC_I_OPERATIONCANCELED;
goto fin;
case 2:
isNew = TRUE;
break;
case 3:
/* Open existing. XXX: Test if repository exists? */
goto skip_clone;
}
if (strlen(params.branch) > 0) {
clone_opts.checkout_branch = params.branch;
}
/* Translate path for libgit2 */
LGitTranslateStringChars(params.path, '\\', '/');
LGitProgressInit(ctx, "Cloning Git Repository", 0);
LGitProgressStart(ctx, hWnd, TRUE);
if (git_clone(&temp_repo, params.url, params.path, &clone_opts) != 0) {
LGitProgressDeinit(ctx);
LGitLibraryError(hWnd, "Repo Init");
ret = SCC_E_NONSPECIFICERROR;
goto fin;
}
git_repository_free(temp_repo);
skip_clone:
/* At least DevStudio wants backslashes */
LGitTranslateStringChars(params.path, '/', '\\');
char project[SCC_PRJPATH_SIZE];
if (!LGitGetProjectNameFromPath(project, params.path, SCC_PRJPATH_SIZE)) {
LGitProgressDeinit(ctx);
MessageBox(hWnd,
"The project name couldn't be derived from the path.",
"Error Cloning", MB_ICONERROR);
ret = SCC_E_NONSPECIFICERROR;
goto fin;
}
LGitLog(" ! Project name: %s\n", project);
strlcpy(lpProjName, project, SCC_PRJPATH_LEN);
strlcpy(lpLocalPath, params.path, _MAX_PATH);
*pbNew = isNew;
LGitProgressDeinit(ctx);
fin:
if (fetch_opts.callbacks.payload != NULL) {
free(fetch_opts.callbacks.payload);
}
return ret;
}