-
Notifications
You must be signed in to change notification settings - Fork 1
/
git.c
264 lines (198 loc) · 7.37 KB
/
git.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
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
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/time.h>
#include "util.h"
#include "git.h"
#include "common.h"
static git_odb *odb;
static git_repository *repo;
static struct timeval begin, end;
static char **ref_names;
static void mne_git_initialize();
static void mne_git_walk_head(mne_git_walk_ctx*);
static void mne_git_cleanup_iter(gpointer, gpointer, gpointer);
static void mne_git_walk_tags(mne_git_walk_ctx*, git_strarray*);
static int mne_git_get_tag_commit_oid(const git_oid**, git_tag*);
static int mne_git_tree_entry_cb(const char*, git_tree_entry*, void*);
static int mne_git_get_tag_tree(git_tree**, git_reference**, const char*);
static void mne_git_walk_tree(git_tree*, git_reference*, mne_git_walk_ctx*);
void mne_git_cleanup() {
mne_git_cleanup_ctx ctx;
/* The same sha1 strings are used as keys for all hashes. */
ctx.free_key = 1;
g_hash_table_foreach(blobs, mne_git_cleanup_iter, &ctx);
ctx.free_key = 0;
g_hash_table_foreach(paths, mne_git_cleanup_iter, &ctx);
g_hash_table_foreach(refs, mne_git_cleanup_iter, &ctx);
int i;
for (i = 0; i < total_refs; i++)
free(ref_names[i]);
free(ref_names);
g_hash_table_destroy(blobs);
g_hash_table_destroy(paths);
g_hash_table_destroy(refs);
}
void mne_git_load_blobs(const char *path) {
mne_git_initialize();
printf("\nLoading blobs...\n\n");
gettimeofday(&begin, NULL);
git_repository_open(&repo, path);
git_repository_odb(&odb, repo);
git_strarray tag_names;
git_tag_list(&tag_names, repo);
total_refs = tag_names.count + 1; /* + 1 for HEAD. */
ref_names = malloc(sizeof(char*) * total_refs);
mne_git_walk_ctx ctx;
ctx.bytes = 0;
ctx.ref_index = 0;
mne_git_walk_head(&ctx);
mne_git_walk_tags(&ctx, &tag_names);
git_strarray_free(&tag_names);
git_repository_free(repo);
gettimeofday(&end, NULL);
float mb = ctx.bytes / 1048576.0;
printf("\nLoaded %d blobs (%.2fmb) ", g_hash_table_size(blobs), mb);
mne_print_duration(&end, &begin);
printf(".\n");
}
static int mne_git_tree_entry_cb(const char *root, git_tree_entry *entry, void *arg) {
mne_git_walk_ctx *ctx = (mne_git_walk_ctx*)arg;
git_otype type = git_tree_entry_type(entry);
if (likely(type == GIT_OBJ_BLOB)) {
const git_oid *blob_oid = git_tree_entry_id(entry);
char *sha1 = malloc(sizeof(char) * GIT_OID_HEXSZ + 1);
assert(sha1 != NULL);
git_oid_tostr(sha1, GIT_OID_HEXSZ + 1, blob_oid);
git_odb_object *blob_odb_object;
git_odb_read(&blob_odb_object, odb, blob_oid);
gpointer blob = g_hash_table_lookup(blobs, (gpointer)sha1);
char **sha1_refs;
if (blob == NULL) {
ctx->distinct_blobs++;
char *tmp_data = (char*)git_odb_object_data(blob_odb_object);
int data_len = strlen(tmp_data);
char *data = malloc(sizeof(char) * (data_len + 1));
memcpy(data, tmp_data, data_len);
data[data_len] = 0;
assert((strlen(root) + strlen(git_tree_entry_name(entry))) < MNE_MAX_PATH_LENGTH);
char *path = malloc(sizeof(char) * MNE_MAX_PATH_LENGTH);
assert(path != NULL);
strcpy(path, root);
strcat(path, git_tree_entry_name(entry));
/* TOOD: Check that the blob <-> path mapping is 1-1. */
g_hash_table_insert(paths, (gpointer)sha1, (gpointer)path);
g_hash_table_insert(blobs, (gpointer)sha1, (gpointer)data);
ctx->bytes += (unsigned long)(sizeof(char) * strlen(data));
sha1_refs = malloc(sizeof(char*) * total_refs);
assert(sha1_refs != NULL);
int i;
for (i = 0; i < total_refs; i++)
sha1_refs[i] = NULL;
g_hash_table_insert(refs, (gpointer)sha1, (gpointer)sha1_refs);
} else {
sha1_refs = g_hash_table_lookup(refs, (gpointer)sha1);
free(sha1);
}
git_odb_object_free(blob_odb_object);
int i;
for (i = 0; i < total_refs; i++) {
if (sha1_refs[i] != NULL)
continue;
sha1_refs[i] = ctx->ref_name;
break;
}
}
return GIT_OK;
}
static int mne_git_get_tag_commit_oid(const git_oid **tag_commit_oid, git_tag* tag) {
git_object *tag_object;
int err = git_tag_peel(&tag_object, tag);
mne_check_error("git_tag_peel()", err, __FILE__, __LINE__);
const git_otype type = git_object_type(tag_object);
if (type != GIT_OBJ_COMMIT)
return MNE_GIT_TARGET_NOT_COMMIT;
*tag_commit_oid = git_object_id(tag_object);
assert(tag_commit_oid != NULL);
git_object_free(tag_object);
return MNE_GIT_OK;
}
static int mne_git_get_tag_tree(git_tree **tag_tree, git_reference **tag_ref, const char *ref_name) {
int err = git_reference_lookup(tag_ref, repo, ref_name);
mne_check_error("git_reference_lookup()", err, __FILE__, __LINE__);
const git_oid *tag_oid = git_reference_oid(*tag_ref);
assert(tag_oid != NULL);
git_tag *tag;
err = git_tag_lookup(&tag, repo, tag_oid);
const git_oid *tag_commit_oid;
if (err == GIT_ENOTFOUND) {
/* Not a tag, must be a commit. */
tag_commit_oid = tag_oid;
} else {
err = mne_git_get_tag_commit_oid(&tag_commit_oid, tag);
git_tag_free(tag);
if (err != GIT_OK)
return err;
}
git_commit *tag_commit;
err = git_commit_lookup(&tag_commit, repo, tag_commit_oid);
mne_check_error("git_commit_lookup()", err, __FILE__, __LINE__);
err = git_commit_tree(tag_tree, tag_commit);
mne_check_error("git_commit_tree()", err, __FILE__, __LINE__);
git_commit_free(tag_commit);
return MNE_GIT_OK;
}
static void mne_git_walk_tree(git_tree *tree, git_reference *ref, mne_git_walk_ctx *ctx) {
ctx->distinct_blobs = 0;
int ref_name_len = strlen(git_reference_name(ref));
ctx->ref_name = malloc(sizeof(char) * (ref_name_len + 1));
assert(ctx->ref_name != NULL);
strncpy(ctx->ref_name, git_reference_name(ref), ref_name_len);
ctx->ref_name[ref_name_len] = 0;
ref_names[ctx->ref_index] = ctx->ref_name;
printf(" * %-22s", ctx->ref_name);
fflush(stdout);
git_tree_walk(tree, &mne_git_tree_entry_cb, GIT_TREEWALK_POST, ctx);
printf(" ✔ +%d\n", ctx->distinct_blobs);
}
static void mne_git_initialize() {
total_refs = 0;
blobs = g_hash_table_new(g_str_hash, g_str_equal);
paths = g_hash_table_new(g_str_hash, g_str_equal);
refs = g_hash_table_new(g_str_hash, g_str_equal);
}
static void mne_git_walk_head(mne_git_walk_ctx *ctx) {
git_reference *head_ref;
int err = git_repository_head(&head_ref, repo);
mne_check_error("git_repository_head()", err, __FILE__, __LINE__);
const git_oid *head_oid = git_reference_oid(head_ref);
git_commit *head_commit;
git_commit_lookup(&head_commit, repo, head_oid);
git_tree *head_tree;
git_commit_tree(&head_tree, head_commit);
git_commit_free(head_commit);
mne_git_walk_tree(head_tree, head_ref, ctx);
git_tree_free(head_tree);
git_reference_free(head_ref);
}
static void mne_git_walk_tags(mne_git_walk_ctx *ctx, git_strarray *tag_names) {
int i, err;
for (i = 0; i < tag_names->count; i++) {
git_tree *tag_tree;
git_reference *tag_ref;
if ((err = mne_git_get_tag_tree(&tag_tree, &tag_ref, tag_names->strings[i])) == MNE_GIT_TARGET_NOT_COMMIT) {
printf(" ! %s does not target a commit? Skipping.\n", git_reference_name(tag_ref));
continue;
}
ctx->ref_index++;
mne_git_walk_tree(tag_tree, tag_ref, ctx);
git_tree_free(tag_tree);
git_reference_free(tag_ref);
}
}
static void mne_git_cleanup_iter(gpointer key, gpointer value, gpointer args) {
mne_git_cleanup_ctx *ctx = (mne_git_cleanup_ctx*)args;
if (ctx->free_key)
free(key);
free(value);
}