forked from libgit2/libgit2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
batch_fetch.c
233 lines (208 loc) · 6.93 KB
/
batch_fetch.c
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
#include<git2.h>
#include<zlib.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<openssl/sha.h>
#include<sys/types.h>
#include<sys/stat.h>
static const char hex_chars[] = "0123456789abcdef";
void convert_hex(unsigned char *md, unsigned char *mdstr)
{
int i;
int j = 0;
unsigned int c;
for (i = 0; i < 20; i++) {
c = (md[i] >> 4) & 0x0f;
mdstr[j++] = hex_chars[c];
mdstr[j++] = hex_chars[md[i] & 0x0f];
}
mdstr[40] = '\0';
}
void recursive_mkdir(char *dir_path)
{
char *tmp = strrchr(dir_path, '/');
char *pdir = malloc(strlen(dir_path));
memset(pdir, 0, sizeof(pdir));
int i = 0;
for(i = 0; i < strlen(dir_path) - strlen(tmp); ++i) {
pdir[i] = dir_path[i];
}
pdir[i] = 0;
struct stat st = {0};
if(stat(pdir, &st) == -1) {
mkdir(pdir, 0700);
}
mkdir(dir_path, 0700);
if(pdir)
{
free(pdir);
}
}
FILE* my_fopen(char *path, unsigned char *mode)
{
char* tmp = strrchr(path, '/');
char *dir = malloc(strlen(path));
memset(dir, 0, sizeof(dir));
int i = 0;
for(i = 0; i < strlen(path) - strlen(tmp); ++i) {
dir[i] = path[i];
}
dir[i] = 0;
recursive_mkdir(dir);
if(dir) {
free(dir);
}
return fopen(path, mode);
}
int main(int argc, char *argv[])
{
git_libgit2_init();
git_repository *repo = NULL;
git_remote *remote;
git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
int error = 0;
if(argc < 5) {
printf("please provide 4 parameters: git url, local repository path and file name, packfile name\n");\
goto cleanup;
}
char *url = argv[1];
char *path = argv[2];
char *file_name = argv[3];
char *name = argv[4];
//part 1: initialize local repository
if((error = git_repository_init(&repo, path, 0)) < 0) {
fprintf(stderr, "initialize repository error: %s\n", giterr_last()->message);
goto cleanup;
}
//part 2: put prettyfied commit content into the .git/objects directory.
FILE *fp = fopen(file_name, "r");
char *l0 = NULL;
size_t size = 0;
char seps[] = ";";
//Here I assume that a sha;name;offset;length in a line
while(getline(&l0, &size, stdin) >= 0) {
char *sha = strtok(l0, seps);
int i;
char *heads_name = strtok(NULL, seps);
int offset = atoi(strtok(NULL, seps));
int length = atoi(strtok(NULL, seps));
fseek(fp, offset, SEEK_SET);
//allocate a little more space, just in case of some unknown problems:)
//form correct format
char *content = malloc(length + 10);
memset(content, 0, length + 10);
fread(content, length, 1, fp);
printf("%s\n", content);
char *header = malloc(20);
sprintf(header, "commit %d", length);
char *store = malloc(strlen(header) + strlen(content) + 5);
memset(store, 0, strlen(header) + strlen(content) + 5);
strcpy(store, header);
strcpy(store + strlen(header) + 1, content);
//then hash
unsigned char md[SHA_DIGEST_LENGTH];
bzero(md, SHA_DIGEST_LENGTH);
SHA1(store, strlen(header)+strlen(content)+1, md);
char mdstr[40];
bzero(mdstr, 40);
convert_hex(md, mdstr);
printf ("Result of SHA1 : %s\n", mdstr);
//must get sourceLen as the form, as store has a null pointer after header
uLong sourceLen = strlen(header) + strlen(content) + 1;
uLong compressSize = compressBound(sourceLen);
Bytef *dest = (Bytef *)malloc(compressSize);
memset(dest, 0, compressSize);
compress(dest, &compressSize, (Bytef *)store, sourceLen);
//write the compressed content to the file
char *obj_dir_path = malloc(strlen(path) + strlen("/.git/objects/") + 5);
memset(obj_dir_path, 0, sizeof(obj_dir_path));
strcpy(obj_dir_path, path);
strcat(obj_dir_path, "/.git/objects/");
char obj_dir_name[3];
for (i = 0; i < 2; i++) {
obj_dir_name[i] = mdstr[i];
}
obj_dir_name[2] = 0;
strcat(obj_dir_path, obj_dir_name);
//create dir if it doesn't exist
struct stat st = {0};
if(stat(obj_dir_path, &st) == -1) {
mkdir(obj_dir_path, 0700);
}
char *obj_file_path = malloc(strlen(obj_dir_path) + 50);
memset(obj_file_path, 0, strlen(obj_dir_path) + 50);
strcpy(obj_file_path, obj_dir_path);
strcat(obj_file_path, "/");
char obj_file_name[40];
for (i = 0; i < 38; i++) {
obj_file_name[i] = mdstr[i+2];
}
obj_file_name[38] = 0;
strcat(obj_file_path, obj_file_name);
FILE *out = fopen(obj_file_path, "wb");
fwrite(dest, compressSize, 1, out);
fclose(out);
char *refs_heads_path = malloc(strlen(path) + strlen("/.git/refs/heads/") + strlen(heads_name) + 10);
memset(refs_heads_path, 0, strlen(path) + strlen("/.git/refs/heads/") + strlen(heads_name) + 10);
strcpy(refs_heads_path, path);
strcat(refs_heads_path, "/.git/refs/heads/");
strcat(refs_heads_path, heads_name);
FILE *out2 = my_fopen(refs_heads_path, "w");
fwrite(sha, strlen(sha), 1, out2);
fwrite("\n", 1, 1, out2);
fclose(out2);
if(content) {
free(content);
}
if(header) {
free(header);
}
if(store){
free(store);
}
if(dest) {
free(dest);
}
if(obj_dir_path) {
free(obj_dir_path);
}
if(obj_file_path) {
free(obj_file_path);
}
if(refs_heads_path) {
free(refs_heads_path);
}
}
//part 3: fetch
if ((error = git_remote_lookup(&remote, repo, "origin")) < 0) {
if((error = git_remote_create(&remote, repo, "origin", url)) < 0) {
fprintf(stderr, "add remote error: %s\n", giterr_last()->message);
}
}
printf("%s\n", packfile_name);
packfile_name = name;
error = git_remote_fetch(remote, NULL, &fetch_opts, NULL);
if(error < 0) {
fprintf(stderr, "fetch error: %s\n", giterr_last()->message);
goto cleanup;
}
cleanup:
if (repo) {
git_repository_free(repo);
}
if (remote) {
git_remote_free(remote);
}
git_libgit2_shutdown();
return 0;
}
/*a test case:
./batch_fetch https://github.com/pidanself/Testbranch /home/kaigao/Testbranch /home/kaigao/txt /home/kaigao/packfile
082d5e91a657b93e15395d76074316b3b70e57e7;master;0;796;
be5a7ee90f39b3fc9b7fba0042706a6a33051603;branch2;796;255;
/home/kgao/Testbranch is the folder name for initialize a local repo
/home/kgao/txt is the file name where store the commit's content
/home/kgao/packfile is packfile's name. Here I assume that the file's located folder has existed before
next the input, folleing the format: sha;head's name;offset;length
*/