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
|
//
// Created by Anshul Gupta on 4/6/25.
//
#include "git.h"
#include <errno.h>
#include <grp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
extern char **environ;
/**
* Constructs the full path to the git repository based on the base path, owner,
* and name. If the name is NULL, it constructs the path to the owner's
* directory.
* @param base Base path for the git repository
* @param owner Owner of the repository
* @param name Name of the repository
* @return A string containing the full path to the git repository or owner's
* directory.
*/
static char *get_git_path(const char *base, const char *owner, const char *name)
{
if (!base || !owner)
return NULL;
const char *format = name ? "%s/%s/%s.git" : "%s/%s";
// Calculate the length of the string
const int len = snprintf(NULL, 0, format, base, owner, name);
if (len < 0)
return NULL;
// Allocate memory for the string
char *path = malloc(len + 1);
if (!path)
return NULL;
// Format the string
if (snprintf(path, len + 1, format, base, owner, name) < 0) {
free(path);
return NULL;
}
return path;
}
/**
* Prepares a repository URL for use with git.
* Adds authentication information to the HTTPS URL for git.
* Does nothing for ssh URLs.
* @param url The HTTPS URL to modify
* @param user The username for authentication
* @param token The token for authentication
* @return A new string containing the modified URL with authentication
* information
*/
static char *prepare_git_url(const char *url, const char *user,
const char *token)
{
const char *https_prefix = "https://";
const char *ssh_prefix = "ssh://";
const size_t http_prefix_len = strlen(https_prefix);
const size_t ssh_prefix_len = strlen(ssh_prefix);
size_t new_len;
char *new_url;
if (!url || !user || !token)
return NULL;
// Check if the URL starts with "ssh://"
if (strncmp(url, ssh_prefix, ssh_prefix_len) == 0) {
// If it's an SSH URL, return it unchanged
return strdup(url);
}
// Find the position of "https://"
if (strncmp(url, https_prefix, http_prefix_len) != 0) {
fprintf(stderr, "Error: URL does not start with https://\n");
return NULL;
}
// Calculate the length of the new URL
new_len = strlen(url) + strlen(user) + strlen(token) +
2; // 2 for "@" and ":"
// Allocate memory for the new URL
if (!((new_url = malloc(new_len + 1)))) {
perror("malloc");
return NULL;
}
// Construct the new URL
snprintf(new_url, new_len + 1, "https://%s:%s@%s", user, token,
url + http_prefix_len);
return new_url;
}
/**
* Checks if the git repository at the specified path is a mirror.
* @param path Path to the git repository
* @return 1 if the repository is a mirror, 0 if not
*/
static int contains_mirror(const char *path)
{
const pid_t pid = fork();
if (pid < 0) {
perror("fork");
return 0;
}
if (pid == 0) {
// Child process
// Redirect stdout to /dev/null
const int devnull = open("/dev/null", O_WRONLY);
if (devnull == -1) {
perror("open");
_exit(127);
}
if (dup2(devnull, STDOUT_FILENO) == -1) {
perror("dup2");
close(devnull);
_exit(127);
}
close(devnull);
// Run git
char *args[] = {
"git", "--git-dir", (char *) path,
"config", "--get", "remote.origin.mirror",
NULL,
};
execvp("git", args);
perror("execvp");
_exit(127); // execvp only returns on error
}
int status;
pid_t result;
while ((result = waitpid(pid, &status, 0)) == -1 && errno == EINTR) {
}
if (result == -1) {
perror("waitpid");
return 0;
}
if (WIFEXITED(status)) {
// Check if the exit status is 0 (success)
if (WEXITSTATUS(status) == 0)
return 1; // Repo exists
if (WEXITSTATUS(status) == 1)
return 0; // Repo does not exist
}
fprintf(stderr, "Error: git command failed with status %d\n",
WEXITSTATUS(status));
return 0; // Error occurred
}
/**
* Creates a mirror of the git repository at the specified path.
* @param path Full path to the git repository
* @param ctx Context containing the repository information
* @param quiet Suppress output if non-zero
* @return 0 on success, -1 on error
*/
static int create_mirror(const char *path, const struct repo_ctx *ctx,
const int quiet)
{
const pid_t pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
// Child process
// Convert the URL to a format that git can use
char *url = prepare_git_url(ctx->url, ctx->username,
ctx->token);
// Run git
char *args[7];
int i = 0;
args[i++] = "git";
args[i++] = "clone";
args[i++] = "--mirror";
if (quiet)
args[i++] = "--quiet";
args[i++] = url;
args[i++] = (char *) path;
args[i] = NULL;
execvp("git", args);
perror("execvp");
_exit(127); // execvp only returns on error
}
int status;
pid_t result;
while ((result = waitpid(pid, &status, 0)) == -1 && errno == EINTR) {
}
if (result == -1) {
perror("waitpid");
return -1;
}
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
return 0; // Success
fprintf(stderr, "Error: git clone failed with status %d\n",
WEXITSTATUS(status));
return -1; // Error occurred
}
/**
* Creates the directory structure for the git repository.
* @param ctx Repository context
* @return 0 on success, -1 on error
*/
static int create_git_path(const struct repo_ctx *ctx)
{
// Create owner directory if it doesn't exist
char *owner_path = get_git_path(ctx->git_base, ctx->owner, NULL);
if (!owner_path)
return -1;
if (mkdir(owner_path, 0755) == -1 && errno != EEXIST) {
perror("mkdir");
free(owner_path);
return -1;
}
free(owner_path);
// Create repo directory if it doesn't exist
char *repo_path = get_git_path(ctx->git_base, ctx->owner, ctx->name);
if (!repo_path)
return -1;
if (mkdir(repo_path, 0755) == -1 && errno != EEXIST) {
perror("mkdir");
free(repo_path);
return -1;
}
free(repo_path);
return 0;
}
/**
* Updates the mirror URL of the git repository at the specified path.
* @param path Full path to the git repository
* @param ctx Context containing the repository information
* @return 0 on success, -1 on error
*/
static int update_mirror_url(const char *path, const struct repo_ctx *ctx)
{
// Prepare the URL
char *url = prepare_git_url(ctx->url, ctx->username, ctx->token);
if (!url) {
perror("prepare_git_url");
return -1;
}
const pid_t pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
// Child process
char *args[8];
int i = 0;
args[i++] = "git";
args[i++] = "--git-dir";
args[i++] = (char *) path;
args[i++] = "remote";
args[i++] = "set-url";
args[i++] = "origin";
args[i++] = url;
args[i] = NULL;
execvp("git", args);
perror("execvp");
_exit(127); // execvp only returns on error
}
free(url);
int status;
pid_t result;
while ((result = waitpid(pid, &status, 0)) == -1 && errno == EINTR) {
}
if (result == -1) {
perror("waitpid");
return -1;
}
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
return 0; // Success
fprintf(stderr, "Error: git remote set-url failed with status %d\n",
WEXITSTATUS(status));
return -1; // Error occurred
}
/**
* Updates the git repository at the specified path from the remote.
* @param path Full path to the git repository
* @param quiet Suppress output if non-zero
* @return 0 on success, -1 on error
*/
static int update_mirror(const char *path, int quiet)
{
const pid_t pid = fork();
if (pid < 0) {
perror("fork");
return -1;
}
if (pid == 0) {
// Child process
char *args[9];
int i = 0;
args[i++] = "git";
args[i++] = "--git-dir";
args[i++] = (char *) path;
args[i++] = "fetch";
args[i++] = "--prune";
if (quiet)
args[i++] = "--quiet";
args[i++] = "--all";
args[i++] = "--tags";
args[i] = NULL;
execvp("git", args);
perror("execvp");
_exit(127); // execvp only returns on error
}
int status;
pid_t result;
while ((result = waitpid(pid, &status, 0)) == -1 && errno == EINTR) {
}
if (result == -1) {
perror("waitpid");
return -1;
}
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
return 0; // Success
fprintf(stderr, "Error: git remote update failed with status %d\n",
WEXITSTATUS(status));
return -1; // Error occurred
}
int git_mirror_repo(const struct repo_ctx *ctx, int quiet)
{
int ret = 0;
char *path = get_git_path(ctx->git_base, ctx->owner, ctx->name);
if (!path) {
perror("get_git_path");
return -1;
}
// Check whether repo exists
if (contains_mirror(path)) {
// Repo exists, so we can just update it
if (!quiet)
printf("Repo already exists, updating...\n");
if (update_mirror_url(path, ctx) == -1) {
perror("update_mirror_url");
ret = -1;
goto end;
}
if (update_mirror(path, quiet) == -1) {
perror("update_mirror");
ret = -1;
goto end;
}
goto end;
}
// Repo does not exist, so we need to clone it
if (!quiet)
printf("Repo does not exist, cloning...\n");
if (create_git_path(ctx) == -1) {
perror("create_git_path");
ret = -1;
goto end;
}
if (create_mirror(path, ctx, quiet) == -1) {
perror("create_mirror");
ret = -1;
}
end:
free(path);
return ret;
}
|