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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
|
/*
* Copyright (C) 2009-2012 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include "common.h"
#include "commit.h"
#include "odb.h"
#include "hashtable.h"
#include "pqueue.h"
#include "git2/revwalk.h"
#include <regex.h>
typedef struct commit_object {
git_oid oid;
uint32_t time;
unsigned int seen:1,
uninteresting:1,
topo_delay:1,
parsed:1;
unsigned short in_degree;
unsigned short out_degree;
struct commit_object **parents;
} commit_object;
typedef struct commit_list {
commit_object *item;
struct commit_list *next;
} commit_list;
struct git_revwalk {
git_repository *repo;
git_odb *odb;
git_hashtable *commits;
commit_list *iterator_topo;
commit_list *iterator_rand;
commit_list *iterator_reverse;
git_pqueue iterator_time;
int (*get_next)(commit_object **, git_revwalk *);
int (*enqueue)(git_revwalk *, commit_object *);
git_vector memory_alloc;
size_t chunk_size;
unsigned walking:1;
unsigned int sorting;
};
static commit_list *commit_list_insert(commit_object *item, commit_list **list_p)
{
commit_list *new_list = git__malloc(sizeof(commit_list));
if (new_list == NULL)
return NULL;
new_list->item = item;
new_list->next = *list_p;
*list_p = new_list;
return new_list;
}
static void commit_list_free(commit_list **list_p)
{
commit_list *list = *list_p;
while (list) {
commit_list *temp = list;
list = temp->next;
git__free(temp);
}
*list_p = NULL;
}
static commit_object *commit_list_pop(commit_list **stack)
{
commit_list *top = *stack;
commit_object *item = top ? top->item : NULL;
if (top) {
*stack = top->next;
git__free(top);
}
return item;
}
static int commit_time_cmp(void *a, void *b)
{
commit_object *commit_a = (commit_object *)a;
commit_object *commit_b = (commit_object *)b;
return (commit_a->time < commit_b->time);
}
static uint32_t object_table_hash(const void *key, int hash_id)
{
uint32_t r;
const git_oid *id = key;
memcpy(&r, id->id + (hash_id * sizeof(uint32_t)), sizeof(r));
return r;
}
#define COMMITS_PER_CHUNK 128
#define CHUNK_STEP 64
#define PARENTS_PER_COMMIT ((CHUNK_STEP - sizeof(commit_object)) / sizeof(commit_object *))
static int alloc_chunk(git_revwalk *walk)
{
void *chunk;
chunk = git__calloc(COMMITS_PER_CHUNK, CHUNK_STEP);
if (chunk == NULL)
return GIT_ENOMEM;
walk->chunk_size = 0;
return git_vector_insert(&walk->memory_alloc, chunk);
}
static commit_object *alloc_commit(git_revwalk *walk)
{
unsigned char *chunk;
if (walk->chunk_size == COMMITS_PER_CHUNK)
alloc_chunk(walk);
chunk = git_vector_get(&walk->memory_alloc, walk->memory_alloc.length - 1);
chunk += (walk->chunk_size * CHUNK_STEP);
walk->chunk_size++;
return (commit_object *)chunk;
}
static commit_object **alloc_parents(commit_object *commit, size_t n_parents)
{
if (n_parents <= PARENTS_PER_COMMIT)
return (commit_object **)((unsigned char *)commit + sizeof(commit_object));
return git__malloc(n_parents * sizeof(commit_object *));
}
static commit_object *commit_lookup(git_revwalk *walk, const git_oid *oid)
{
commit_object *commit;
if ((commit = git_hashtable_lookup(walk->commits, oid)) != NULL)
return commit;
commit = alloc_commit(walk);
if (commit == NULL)
return NULL;
git_oid_cpy(&commit->oid, oid);
if (git_hashtable_insert(walk->commits, &commit->oid, commit) < GIT_SUCCESS) {
git__free(commit);
return NULL;
}
return commit;
}
static int commit_quick_parse(git_revwalk *walk, commit_object *commit, git_rawobj *raw)
{
const int parent_len = strlen("parent ") + GIT_OID_HEXSZ + 1;
unsigned char *buffer = raw->data;
unsigned char *buffer_end = buffer + raw->len;
unsigned char *parents_start;
int i, parents = 0;
int commit_time;
buffer += strlen("tree ") + GIT_OID_HEXSZ + 1;
parents_start = buffer;
while (buffer + parent_len < buffer_end && memcmp(buffer, "parent ", strlen("parent ")) == 0) {
parents++;
buffer += parent_len;
}
commit->parents = alloc_parents(commit, parents);
if (commit->parents == NULL)
return GIT_ENOMEM;
buffer = parents_start;
for (i = 0; i < parents; ++i) {
git_oid oid;
if (git_oid_fromstr(&oid, (char *)buffer + strlen("parent ")) < GIT_SUCCESS)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse commit. Parent object is corrupted");
commit->parents[i] = commit_lookup(walk, &oid);
if (commit->parents[i] == NULL)
return GIT_ENOMEM;
buffer += parent_len;
}
commit->out_degree = (unsigned short)parents;
if ((buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse commit. Object is corrupted");
buffer = memchr(buffer, '>', buffer_end - buffer);
if (buffer == NULL)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse commit. Can't find author");
if (git__strtol32(&commit_time, (char *)buffer + 2, NULL, 10) < GIT_SUCCESS)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse commit. Can't parse commit time");
commit->time = (time_t)commit_time;
commit->parsed = 1;
return GIT_SUCCESS;
}
static int commit_parse(git_revwalk *walk, commit_object *commit)
{
git_odb_object *obj;
int error;
if (commit->parsed)
return GIT_SUCCESS;
if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to parse commit. Can't read object");
if (obj->raw.type != GIT_OBJ_COMMIT) {
git_odb_object_free(obj);
return git__throw(GIT_EOBJTYPE, "Failed to parse commit. Object is no commit object");
}
error = commit_quick_parse(walk, commit, &obj->raw);
git_odb_object_free(obj);
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse commit");
}
static void mark_uninteresting(commit_object *commit)
{
unsigned short i;
assert(commit);
commit->uninteresting = 1;
for (i = 0; i < commit->out_degree; ++i)
if (!commit->parents[i]->uninteresting)
mark_uninteresting(commit->parents[i]);
}
static int process_commit(git_revwalk *walk, commit_object *commit, int hide)
{
int error;
if (hide)
mark_uninteresting(commit);
if (commit->seen)
return GIT_SUCCESS;
commit->seen = 1;
if ((error = commit_parse(walk, commit)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to process commit");
return walk->enqueue(walk, commit);
}
static int process_commit_parents(git_revwalk *walk, commit_object *commit)
{
unsigned short i;
int error = GIT_SUCCESS;
for (i = 0; i < commit->out_degree && error == GIT_SUCCESS; ++i) {
error = process_commit(walk, commit->parents[i], commit->uninteresting);
}
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to process commit parents");
}
static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting)
{
commit_object *commit;
commit = commit_lookup(walk, oid);
if (commit == NULL)
return git__throw(GIT_ENOTFOUND, "Failed to push commit. Object not found");
return process_commit(walk, commit, uninteresting);
}
int git_revwalk_push(git_revwalk *walk, const git_oid *oid)
{
assert(walk && oid);
return push_commit(walk, oid, 0);
}
int git_revwalk_hide(git_revwalk *walk, const git_oid *oid)
{
assert(walk && oid);
return push_commit(walk, oid, 1);
}
struct push_cb_data {
git_revwalk *walk;
const char *glob;
int hide;
};
static int push_glob_cb(const char *refname, void *data_)
{
struct push_cb_data *data = (struct push_cb_data *)data_;
if (!git__fnmatch(data->glob, refname, 0)) {
git_reference *ref, *resolved;
int error;
error = git_reference_lookup(&ref, data->walk->repo, refname);
if (error < GIT_SUCCESS)
return error;
error = git_reference_resolve(&resolved, ref);
git_reference_free(ref);
if (error < GIT_SUCCESS)
return error;
error = push_commit(data->walk, git_reference_oid(resolved), data->hide);
git_reference_free(resolved);
return error;
}
return GIT_SUCCESS;
}
static int push_glob(git_revwalk *walk, const char *glob, int hide)
{
git_buf buf = GIT_BUF_INIT;
struct push_cb_data data;
int error;
regex_t preg;
assert(walk && glob);
/* refs/ is implied if not given in the glob */
if (strncmp(glob, GIT_REFS_DIR, strlen(GIT_REFS_DIR))) {
git_buf_printf(&buf, GIT_REFS_DIR "%s", glob);
} else {
git_buf_puts(&buf, glob);
}
/* If no '?', '*' or '[' exist, we append '/ *' to the glob */
memset(&preg, 0x0, sizeof(regex_t));
if (regcomp(&preg, "[?*[]", REG_EXTENDED)) {
error = git__throw(GIT_EOSERR, "Regex failed to compile");
goto cleanup;
}
if (regexec(&preg, glob, 0, NULL, 0))
git_buf_puts(&buf, "/*");
if (git_buf_oom(&buf)) {
error = GIT_ENOMEM;
goto cleanup;
}
data.walk = walk;
data.glob = git_buf_cstr(&buf);
data.hide = hide;
error = git_reference_foreach(walk->repo, GIT_REF_LISTALL, push_glob_cb, &data);
cleanup:
regfree(&preg);
git_buf_free(&buf);
return error;
}
int git_revwalk_push_glob(git_revwalk *walk, const char *glob)
{
assert(walk && glob);
return push_glob(walk, glob, 0);
}
int git_revwalk_hide_glob(git_revwalk *walk, const char *glob)
{
assert(walk && glob);
return push_glob(walk, glob, 1);
}
static int push_head(git_revwalk *walk, int hide)
{
git_reference *ref, *resolved;
int error;
error = git_reference_lookup(&ref, walk->repo, "HEAD");
if (error < GIT_SUCCESS) {
return error;
}
error = git_reference_resolve(&resolved, ref);
if (error < GIT_SUCCESS) {
return error;
}
git_reference_free(ref);
error = push_commit(walk, git_reference_oid(resolved), hide);
git_reference_free(resolved);
return error;
}
int git_revwalk_push_head(git_revwalk *walk)
{
assert(walk);
return push_head(walk, 0);
}
int git_revwalk_hide_head(git_revwalk *walk)
{
assert(walk);
return push_head(walk, 1);
}
static int revwalk_enqueue_timesort(git_revwalk *walk, commit_object *commit)
{
return git_pqueue_insert(&walk->iterator_time, commit);
}
static int revwalk_enqueue_unsorted(git_revwalk *walk, commit_object *commit)
{
return commit_list_insert(commit, &walk->iterator_rand) ? GIT_SUCCESS : GIT_ENOMEM;
}
static int revwalk_next_timesort(commit_object **object_out, git_revwalk *walk)
{
int error;
commit_object *next;
while ((next = git_pqueue_pop(&walk->iterator_time)) != NULL) {
if ((error = process_commit_parents(walk, next)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to load next revision");
if (!next->uninteresting) {
*object_out = next;
return GIT_SUCCESS;
}
}
return git__throw(GIT_EREVWALKOVER, "Failed to load next revision");
}
static int revwalk_next_unsorted(commit_object **object_out, git_revwalk *walk)
{
int error;
commit_object *next;
while ((next = commit_list_pop(&walk->iterator_rand)) != NULL) {
if ((error = process_commit_parents(walk, next)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to load next revision");
if (!next->uninteresting) {
*object_out = next;
return GIT_SUCCESS;
}
}
return git__throw(GIT_EREVWALKOVER, "Failed to load next revision");
}
static int revwalk_next_toposort(commit_object **object_out, git_revwalk *walk)
{
commit_object *next;
unsigned short i;
for (;;) {
next = commit_list_pop(&walk->iterator_topo);
if (next == NULL)
return git__throw(GIT_EREVWALKOVER, "Failed to load next revision");
if (next->in_degree > 0) {
next->topo_delay = 1;
continue;
}
for (i = 0; i < next->out_degree; ++i) {
commit_object *parent = next->parents[i];
if (--parent->in_degree == 0 && parent->topo_delay) {
parent->topo_delay = 0;
if (commit_list_insert(parent, &walk->iterator_topo) == NULL)
return GIT_ENOMEM;
}
}
*object_out = next;
return GIT_SUCCESS;
}
}
static int revwalk_next_reverse(commit_object **object_out, git_revwalk *walk)
{
*object_out = commit_list_pop(&walk->iterator_reverse);
return *object_out ? GIT_SUCCESS : GIT_EREVWALKOVER;
}
static int prepare_walk(git_revwalk *walk)
{
int error;
commit_object *next;
if (walk->sorting & GIT_SORT_TOPOLOGICAL) {
unsigned short i;
while ((error = walk->get_next(&next, walk)) == GIT_SUCCESS) {
for (i = 0; i < next->out_degree; ++i) {
commit_object *parent = next->parents[i];
parent->in_degree++;
}
if (commit_list_insert(next, &walk->iterator_topo) == NULL)
return GIT_ENOMEM;
}
if (error != GIT_EREVWALKOVER)
return git__rethrow(error, "Failed to prepare revision walk");
walk->get_next = &revwalk_next_toposort;
}
if (walk->sorting & GIT_SORT_REVERSE) {
while ((error = walk->get_next(&next, walk)) == GIT_SUCCESS)
if (commit_list_insert(next, &walk->iterator_reverse) == NULL)
return GIT_ENOMEM;
if (error != GIT_EREVWALKOVER)
return git__rethrow(error, "Failed to prepare revision walk");
walk->get_next = &revwalk_next_reverse;
}
walk->walking = 1;
return GIT_SUCCESS;
}
int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
{
int error;
git_revwalk *walk;
walk = git__malloc(sizeof(git_revwalk));
if (walk == NULL)
return GIT_ENOMEM;
memset(walk, 0x0, sizeof(git_revwalk));
walk->commits = git_hashtable_alloc(64,
object_table_hash,
(git_hash_keyeq_ptr)git_oid_cmp);
if (walk->commits == NULL) {
git__free(walk);
return GIT_ENOMEM;
}
git_pqueue_init(&walk->iterator_time, 8, commit_time_cmp);
git_vector_init(&walk->memory_alloc, 8, NULL);
alloc_chunk(walk);
walk->get_next = &revwalk_next_unsorted;
walk->enqueue = &revwalk_enqueue_unsorted;
walk->repo = repo;
error = git_repository_odb(&walk->odb, repo);
if (error < GIT_SUCCESS) {
git_revwalk_free(walk);
return error;
}
*revwalk_out = walk;
return GIT_SUCCESS;
}
void git_revwalk_free(git_revwalk *walk)
{
unsigned int i;
commit_object *commit;
if (walk == NULL)
return;
git_revwalk_reset(walk);
git_odb_free(walk->odb);
/* if the parent has more than PARENTS_PER_COMMIT parents,
* we had to allocate a separate array for those parents.
* make sure it's being free'd */
GIT_HASHTABLE_FOREACH_VALUE(walk->commits, commit, {
if (commit->out_degree > PARENTS_PER_COMMIT)
git__free(commit->parents);
});
git_hashtable_free(walk->commits);
git_pqueue_free(&walk->iterator_time);
for (i = 0; i < walk->memory_alloc.length; ++i)
git__free(git_vector_get(&walk->memory_alloc, i));
git_vector_free(&walk->memory_alloc);
git__free(walk);
}
git_repository *git_revwalk_repository(git_revwalk *walk)
{
assert(walk);
return walk->repo;
}
void git_revwalk_sorting(git_revwalk *walk, unsigned int sort_mode)
{
assert(walk);
if (walk->walking)
git_revwalk_reset(walk);
walk->sorting = sort_mode;
if (walk->sorting & GIT_SORT_TIME) {
walk->get_next = &revwalk_next_timesort;
walk->enqueue = &revwalk_enqueue_timesort;
} else {
walk->get_next = &revwalk_next_unsorted;
walk->enqueue = &revwalk_enqueue_unsorted;
}
}
int git_revwalk_next(git_oid *oid, git_revwalk *walk)
{
int error;
commit_object *next;
assert(walk && oid);
if (!walk->walking) {
if ((error = prepare_walk(walk)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to load next revision");
}
error = walk->get_next(&next, walk);
if (error == GIT_EREVWALKOVER) {
git_revwalk_reset(walk);
return GIT_EREVWALKOVER;
}
if (error < GIT_SUCCESS)
return git__rethrow(error, "Failed to load next revision");
git_oid_cpy(oid, &next->oid);
return GIT_SUCCESS;
}
void git_revwalk_reset(git_revwalk *walk)
{
commit_object *commit;
assert(walk);
GIT_HASHTABLE_FOREACH_VALUE(walk->commits, commit,
commit->seen = 0;
commit->in_degree = 0;
commit->topo_delay = 0;
commit->uninteresting = 0;
);
git_pqueue_clear(&walk->iterator_time);
commit_list_free(&walk->iterator_topo);
commit_list_free(&walk->iterator_rand);
commit_list_free(&walk->iterator_reverse);
walk->walking = 0;
}
|