-
Notifications
You must be signed in to change notification settings - Fork 2
/
project.cpp
executable file
·298 lines (275 loc) · 9.77 KB
/
project.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
/*
* Project management functions that handle the connection. Done before and
* after initialization and capabilities.
*/
#include "stdafx.h"
static SCCRTN LGitInitRepo(HWND hWnd, LPSTR lpProjName, LPCSTR lpLocalPath)
{
/* The repository is created, but we'll re-open in SccOpenProject */
git_repository *temp_repo;
int res;
char msg[512];
LGitLog(" ! Init/Import\n");
_snprintf(msg, 512,
"There is no Git repository in '%s' for the project '%s'. "
"Would you like to create one?",
lpLocalPath, lpProjName);
res = MessageBox(hWnd, msg, "Initialize Git Repository",
MB_ICONQUESTION | MB_YESNO);
switch (res) {
case IDYES:
/* XXX: use the _ext variant */
if (git_repository_init(&temp_repo, lpLocalPath, 0) != 0) {
LGitLibraryError(hWnd, "Repo Init");
return SCC_E_UNKNOWNERROR;
}
git_repository_free(temp_repo);
LGitGetProjectNameFromPath(lpProjName, lpLocalPath, SCC_PRJPATH_SIZE);
LGitLog(" ! New proj name is %s\n", lpProjName);
return SCC_OK;
case IDNO:
return SCC_I_OPERATIONCANCELED;
default:
return SCC_E_UNKNOWNERROR;
}
}
LGIT_API SCCRTN LGitOpenProject(LPVOID context,
HWND hWnd,
LPSTR lpUser,
LPSTR lpProjName, /* writeable, contrary to MSDN */
LPCSTR lpLocalProjPath,
LPSTR lpAuxProjPath,
LPCSTR lpComment,
LPTEXTOUTPROC lpTextOutProc,
LONG dwFlags)
{
int rc;
const char *workdir;
LGitContext *ctx = (LGitContext*)context;
LGitLog("**LGitOpenProject** Context=%p\n", ctx);
LGitLog(" user %s\n", lpUser);
LGitLog(" proj name %s\n", lpProjName);
LGitLog(" local proj path %s\n", lpLocalProjPath);
LGitLog(" aux proj path %s\n", lpAuxProjPath);
LGitLog(" comment %s\n", lpComment);
LGitLog(" flags %x\n", dwFlags);
if (ctx->active) {
LGitLog(" ! Context already is active\n");
}
// If flags & 1, it's probably init, but it could be called from existing
BOOL havent_init = TRUE;
init_again:
rc = git_repository_open_ext(&ctx->repo, lpLocalProjPath, 0, NULL);
if (rc == 0) {
LGitLog(" Got it\n");
// Repo already exists, connect to it
strlcpy(ctx->path, lpLocalProjPath, 1024);
} else if (rc == GIT_ENOTFOUND && dwFlags & SCC_OP_CREATEIFNEW && havent_init) {
// No repository, create/clone depending on bAllowChangePath
LGitLog(" Initializing\n");
/* This is ignored */
char proj[SCC_PRJPATH_SIZE];
SCCRTN init_ret = LGitInitRepo(hWnd, proj, lpLocalProjPath);
if (init_ret != 0) {
return SCC_E_COULDNOTCREATEPROJECT;
}
goto init_again;
} else if (rc == GIT_ENOTFOUND) {
LGitLog(" No repo\n");
return SCC_E_UNKNOWNPROJECT;
} else if (rc == -1) {
// Error opening
LGitLibraryError(hWnd, "SccOpenProject");
return SCC_E_UNKNOWNERROR;
}
workdir = git_repository_workdir(ctx->repo);
if (workdir == NULL) {
LGitLog("!! Workdir doesn't exist, bare repo");
MessageBox(hWnd, "The repository is bare.", "Can't open repository",
MB_ICONERROR);
return SCC_E_INVALIDFILEPATH;
}
strlcpy(ctx->workdir_path, workdir, 1024);
/*
* Translate the path to Windows-style backslashes. The IDE will return us
* backslashes, which will confuse PathCommonPrefix. We translate them back
* after the prefix check in each function.
*/
LGitTranslateStringChars(ctx->workdir_path, '/', '\\');
LGitUtf8ToWide(ctx->workdir_path, ctx->workdir_path_utf16, 1024);
LGitLog(" The workdir is %s\n", ctx->workdir_path);
ctx->active = TRUE;
ctx->textoutCb = lpTextOutProc;
strlcpy(ctx->username, lpUser, SCC_USER_LEN);
LGitGetProjectNameFromPath(lpProjName, ctx->workdir_path, SCC_PRJPATH_SIZE);
LGitLog(" ! New proj name is %s\n", lpProjName);
/* XXX: should init/deinit at project level? */
ctx->checkouts = new CheckoutQueue();
LGitInitializeFonts(ctx);
return SCC_OK;
}
/*
* Wrapper that converts to ANSI, because it would be unmanageable in the
* normal function due to how project name stuff is handled.
*/
SCCRTN SccOpenProject (LPVOID context,
HWND hWnd,
LPSTR lpUser,
LPSTR lpProjName, /* writeable, contrary to MSDN */
LPCSTR lpLocalProjPath,
LPSTR lpAuxProjPath,
LPCSTR lpComment,
LPTEXTOUTPROC lpTextOutProc,
LONG dwFlags)
{
char user[SCC_USER_SIZE];
char projName[SCC_NAME_SIZE];
char auxProjPath[SCC_PRJPATH_SIZE]; /* not auxlabel, according to hdr */
char localProjPath[SCC_PRJPATH_SIZE];
/* XXX: comment? not used yet */
LGitAnsiToUtf8(lpUser, user, SCC_USER_SIZE);
LGitAnsiToUtf8(lpProjName, projName, SCC_NAME_SIZE);
LGitAnsiToUtf8(lpAuxProjPath, auxProjPath, SCC_PRJPATH_SIZE);
/* r/o and not set after */
LGitAnsiToUtf8(lpLocalProjPath, localProjPath, SCC_PRJPATH_SIZE);
SCCRTN ret = LGitOpenProject(context, hWnd, user, projName, localProjPath, auxProjPath, lpComment, lpTextOutProc, dwFlags);
LGitUtf8ToAnsi(user, lpUser, SCC_USER_SIZE);
LGitUtf8ToAnsi(projName, lpProjName, SCC_NAME_SIZE);
LGitUtf8ToAnsi(auxProjPath, lpAuxProjPath, SCC_PRJPATH_SIZE);
return ret;
}
SCCRTN SccCloseProject (LPVOID context)
{
LGitContext *ctx = (LGitContext*)context;
LGitLog("**SccCloseProject** Context=%p\n", ctx);
LGitLog(" Active? %d\n", ctx->active);
if (context) {
LGitContext *ctx = (LGitContext*)context;
/* additional debug logs because VS traps segfault */
LGitUninitializeFonts(ctx);
if (ctx->repo) {
LGitLog(" ! Free repo\n");
git_repository_free(ctx->repo);
ctx->repo = NULL;
}
if (ctx->checkouts) {
LGitLog(" ! Free checkouts\n");
delete ctx->checkouts;
ctx->checkouts = NULL;
}
ctx->renameCb = NULL;
ctx->renameData = NULL;
ctx->textoutCb = NULL;
ZeroMemory(ctx->path, 1024);
ZeroMemory(ctx->workdir_path, 1024);
ZeroMemory(ctx->workdir_path_utf16, 1024 * sizeof(wchar_t));
ctx->active = FALSE;
LGitLog(" ! Cleared, now inactive\n");
}
return SCC_OK;
}
LGIT_API SCCRTN LGitGetProjPath(LPVOID context,
HWND hWnd,
LPSTR lpUser,
LPSTR lpProjName,
LPSTR lpLocalPath,
LPSTR lpAuxProjPath,
BOOL bAllowChangePath,
LPBOOL pbNew)
{
int rc;
git_repository *temp_repo;
LGitContext *ctx = (LGitContext*)context;
LGitLog("**LGitGetProjPath** Context=%p\n", context);
LGitLog(" user %s\n", lpUser);
LGitLog(" proj name %s\n", lpProjName);
LGitLog(" local path %s\n", lpLocalPath);
LGitLog(" aux proj path %s\n", lpAuxProjPath);
LGitLog(" allow change path? %x\n", bAllowChangePath);
LGitLog(" can make new? %x\n", *pbNew);
/* XXX: Employ git_repository_discover */
/* XXX: Also deref pbNew since it can be set incoming */
if (bAllowChangePath) {
// Can change path, probably clone
return LGitClone(ctx, hWnd, lpProjName, lpLocalPath, pbNew);
}
// If bAllowChangePath is called, we're likely cloning.
// Otherwise, we're initing probably. (or perhaps binding?)
// The init case is probably when SccOpenProject is called with 1.
rc = git_repository_open_ext(&temp_repo, lpLocalPath, 0, NULL);
if (rc == 0) {
// Repo already exists, connect to it
LGitLog(" ! Repo exists, connecting\n");
git_repository_free(temp_repo);
LGitGetProjectNameFromPath(lpProjName, lpLocalPath, SCC_PRJPATH_SIZE);
LGitLog(" ! New proj name is %s\n", lpProjName);
} else if (rc == GIT_ENOTFOUND && !bAllowChangePath) {
// Can't change path, probably initing and importing existing files
return LGitInitRepo(hWnd, lpProjName, lpLocalPath);
} else if (rc == -1) {
// Error opening
LGitLibraryError(hWnd, "SccGetProjPath");
return SCC_E_UNKNOWNERROR;
}
return SCC_OK;
}
SCCRTN SccGetProjPath (LPVOID context,
HWND hWnd,
LPSTR lpUser,
LPSTR lpProjName,
LPSTR lpLocalPath,
LPSTR lpAuxProjPath,
BOOL bAllowChangePath,
LPBOOL pbNew)
{
char user[SCC_USER_SIZE]; /* not used but in case */
char projName[SCC_NAME_SIZE];
char auxProjPath[SCC_PRJPATH_SIZE]; /* not auxlabel, according to hdr */
char localProjPath[SCC_PRJPATH_SIZE];
LGitAnsiToUtf8(lpUser, user, SCC_USER_SIZE);
LGitAnsiToUtf8(lpProjName, projName, SCC_NAME_SIZE);
LGitAnsiToUtf8(lpAuxProjPath, auxProjPath, SCC_PRJPATH_SIZE);
LGitAnsiToUtf8(lpLocalPath, localProjPath, SCC_PRJPATH_SIZE);
SCCRTN ret = LGitGetProjPath(context, hWnd, user, projName, localProjPath, auxProjPath, bAllowChangePath, pbNew);
LGitUtf8ToAnsi(user, lpUser, SCC_USER_SIZE);
LGitUtf8ToAnsi(projName, lpProjName, SCC_NAME_SIZE);
LGitUtf8ToAnsi(auxProjPath, lpAuxProjPath, SCC_PRJPATH_SIZE);
/* can be modified */
if (bAllowChangePath) {
LGitUtf8ToAnsi(localProjPath, lpLocalPath, SCC_PRJPATH_SIZE);
}
return ret;
}
/* Here be dragons (subprojects) */
SCCRTN SccGetParentProjectPath(LPVOID context,
HWND hWnd,
LPSTR lpUser,
LPCSTR lpProjPath,
LPSTR lpAuxProjPath,
LPSTR lpParentProjPath)
{
LGitContext *ctx = (LGitContext*)context;
LGitLog("**SccGetParentProjectPath** Context=%p\n", context);
LGitLog(" user %s\n", lpUser);
LGitLog(" proj path %s\n", lpProjPath);
LGitLog(" aux path (inout) %s\n", lpAuxProjPath);
LGitLog(" parent proj path (inout) %s\n", lpParentProjPath);
return SCC_E_OPNOTSUPPORTED;
}
SCCRTN SccCreateSubProject(LPVOID context,
HWND hWnd,
LPSTR lpUser,
LPCSTR lpParentProjPath,
LPCSTR lpSubProjName,
LPSTR lpAuxProjPath,
LPSTR lpSubProjPath)
{
LGitContext *ctx = (LGitContext*)context;
LGitLog("**SccCreateSubProject** Context=%p\n", context);
LGitLog(" user %s\n", lpUser);
LGitLog(" parent proj path %s\n", lpParentProjPath);
LGitLog(" subproject name %s\n", lpSubProjName);
LGitLog(" aux path (inout) %s\n", lpAuxProjPath);
LGitLog(" subproject path (inout) %s\n", lpSubProjPath);
return SCC_E_OPNOTSUPPORTED;
}