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
|
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* 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 "pool.h"
#include "revwalk.h"
#include "git2/revparse.h"
#include "merge.h"
#include <regex.h>
git_commit_list_node *git_revwalk__commit_lookup(
git_revwalk *walk, const git_oid *oid)
{
git_commit_list_node *commit;
khiter_t pos;
int ret;
/* lookup and reserve space if not already present */
pos = kh_get(oid, walk->commits, oid);
if (pos != kh_end(walk->commits))
return kh_value(walk->commits, pos);
commit = git_commit_list_alloc_node(walk);
if (commit == NULL)
return NULL;
git_oid_cpy(&commit->oid, oid);
pos = kh_put(oid, walk->commits, &commit->oid, &ret);
assert(ret != 0);
kh_value(walk->commits, pos) = commit;
return commit;
}
static void mark_uninteresting(git_commit_list_node *commit)
{
unsigned short i;
assert(commit);
commit->uninteresting = 1;
/* This means we've reached a merge base, so there's no need to walk any more */
if ((commit->flags & (RESULT | STALE)) == RESULT)
return;
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, git_commit_list_node *commit, int hide)
{
int error;
if (hide)
mark_uninteresting(commit);
if (commit->seen)
return 0;
commit->seen = 1;
if ((error = git_commit_list_parse(walk, commit)) < 0)
return error;
return walk->enqueue(walk, commit);
}
static int process_commit_parents(git_revwalk *walk, git_commit_list_node *commit)
{
unsigned short i;
int error = 0;
for (i = 0; i < commit->out_degree && !error; ++i)
error = process_commit(walk, commit->parents[i], commit->uninteresting);
return error;
}
static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting)
{
git_object *obj;
git_otype type;
git_commit_list_node *commit;
if (git_object_lookup(&obj, walk->repo, oid, GIT_OBJ_ANY) < 0)
return -1;
type = git_object_type(obj);
git_object_free(obj);
if (type != GIT_OBJ_COMMIT) {
giterr_set(GITERR_INVALID, "Object is no commit object");
return -1;
}
commit = git_revwalk__commit_lookup(walk, oid);
if (commit == NULL)
return -1; /* error already reported by failed lookup */
commit->uninteresting = uninteresting;
if (walk->one == NULL && !uninteresting) {
walk->one = commit;
} else {
if (git_vector_insert(&walk->twos, commit) < 0)
return -1;
}
return 0;
}
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);
}
static int push_ref(git_revwalk *walk, const char *refname, int hide)
{
git_oid oid;
if (git_reference_name_to_id(&oid, walk->repo, refname) < 0)
return -1;
return push_commit(walk, &oid, hide);
}
struct push_cb_data {
git_revwalk *walk;
int hide;
};
static int push_glob_cb(const char *refname, void *data_)
{
struct push_cb_data *data = (struct push_cb_data *)data_;
return push_ref(data->walk, refname, data->hide);
}
static int push_glob(git_revwalk *walk, const char *glob, int hide)
{
git_buf buf = GIT_BUF_INIT;
struct push_cb_data data;
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)) {
giterr_set(GITERR_OS, "Regex failed to compile");
git_buf_free(&buf);
return -1;
}
if (regexec(&preg, glob, 0, NULL, 0))
git_buf_puts(&buf, "/*");
if (git_buf_oom(&buf))
goto on_error;
data.walk = walk;
data.hide = hide;
if (git_reference_foreach_glob(
walk->repo, git_buf_cstr(&buf), GIT_REF_LISTALL, push_glob_cb, &data) < 0)
goto on_error;
regfree(&preg);
git_buf_free(&buf);
return 0;
on_error:
regfree(&preg);
git_buf_free(&buf);
return -1;
}
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);
}
int git_revwalk_push_head(git_revwalk *walk)
{
assert(walk);
return push_ref(walk, GIT_HEAD_FILE, 0);
}
int git_revwalk_hide_head(git_revwalk *walk)
{
assert(walk);
return push_ref(walk, GIT_HEAD_FILE, 1);
}
int git_revwalk_push_ref(git_revwalk *walk, const char *refname)
{
assert(walk && refname);
return push_ref(walk, refname, 0);
}
int git_revwalk_push_range(git_revwalk *walk, const char *range)
{
git_object *left, *right;
int threedots;
int error = 0;
if ((error = git_revparse_rangelike(&left, &right, &threedots, walk->repo, range)))
return error;
if (threedots) {
/* TODO: support "<commit>...<commit>" */
giterr_set(GITERR_INVALID, "Symmetric differences not implemented in revwalk");
return GIT_EINVALIDSPEC;
}
if ((error = push_commit(walk, git_object_id(left), 1)))
goto out;
error = push_commit(walk, git_object_id(right), 0);
out:
git_object_free(left);
git_object_free(right);
return error;
}
int git_revwalk_hide_ref(git_revwalk *walk, const char *refname)
{
assert(walk && refname);
return push_ref(walk, refname, 1);
}
static int revwalk_enqueue_timesort(git_revwalk *walk, git_commit_list_node *commit)
{
return git_pqueue_insert(&walk->iterator_time, commit);
}
static int revwalk_enqueue_unsorted(git_revwalk *walk, git_commit_list_node *commit)
{
return git_commit_list_insert(commit, &walk->iterator_rand) ? 0 : -1;
}
static int revwalk_next_timesort(git_commit_list_node **object_out, git_revwalk *walk)
{
int error;
git_commit_list_node *next;
while ((next = git_pqueue_pop(&walk->iterator_time)) != NULL) {
if ((error = process_commit_parents(walk, next)) < 0)
return error;
if (!next->uninteresting) {
*object_out = next;
return 0;
}
}
giterr_clear();
return GIT_ITEROVER;
}
static int revwalk_next_unsorted(git_commit_list_node **object_out, git_revwalk *walk)
{
int error;
git_commit_list_node *next;
while ((next = git_commit_list_pop(&walk->iterator_rand)) != NULL) {
if ((error = process_commit_parents(walk, next)) < 0)
return error;
if (!next->uninteresting) {
*object_out = next;
return 0;
}
}
giterr_clear();
return GIT_ITEROVER;
}
static int revwalk_next_toposort(git_commit_list_node **object_out, git_revwalk *walk)
{
git_commit_list_node *next;
unsigned short i;
for (;;) {
next = git_commit_list_pop(&walk->iterator_topo);
if (next == NULL) {
giterr_clear();
return GIT_ITEROVER;
}
if (next->in_degree > 0) {
next->topo_delay = 1;
continue;
}
for (i = 0; i < next->out_degree; ++i) {
git_commit_list_node *parent = next->parents[i];
if (--parent->in_degree == 0 && parent->topo_delay) {
parent->topo_delay = 0;
if (git_commit_list_insert(parent, &walk->iterator_topo) == NULL)
return -1;
}
}
*object_out = next;
return 0;
}
}
static int revwalk_next_reverse(git_commit_list_node **object_out, git_revwalk *walk)
{
*object_out = git_commit_list_pop(&walk->iterator_reverse);
return *object_out ? 0 : GIT_ITEROVER;
}
static int prepare_walk(git_revwalk *walk)
{
int error;
unsigned int i;
git_commit_list_node *next, *two;
git_commit_list *bases = NULL;
/*
* If walk->one is NULL, there were no positive references,
* so we know that the walk is already over.
*/
if (walk->one == NULL) {
giterr_clear();
return GIT_ITEROVER;
}
/* first figure out what the merge bases are */
if (git_merge__bases_many(&bases, walk, walk->one, &walk->twos) < 0)
return -1;
git_commit_list_free(&bases);
if (process_commit(walk, walk->one, walk->one->uninteresting) < 0)
return -1;
git_vector_foreach(&walk->twos, i, two) {
if (process_commit(walk, two, two->uninteresting) < 0)
return -1;
}
if (walk->sorting & GIT_SORT_TOPOLOGICAL) {
unsigned short i;
while ((error = walk->get_next(&next, walk)) == 0) {
for (i = 0; i < next->out_degree; ++i) {
git_commit_list_node *parent = next->parents[i];
parent->in_degree++;
}
if (git_commit_list_insert(next, &walk->iterator_topo) == NULL)
return -1;
}
if (error != GIT_ITEROVER)
return error;
walk->get_next = &revwalk_next_toposort;
}
if (walk->sorting & GIT_SORT_REVERSE) {
while ((error = walk->get_next(&next, walk)) == 0)
if (git_commit_list_insert(next, &walk->iterator_reverse) == NULL)
return -1;
if (error != GIT_ITEROVER)
return error;
walk->get_next = &revwalk_next_reverse;
}
walk->walking = 1;
return 0;
}
int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
{
git_revwalk *walk;
walk = git__malloc(sizeof(git_revwalk));
GITERR_CHECK_ALLOC(walk);
memset(walk, 0x0, sizeof(git_revwalk));
walk->commits = git_oidmap_alloc();
GITERR_CHECK_ALLOC(walk->commits);
if (git_pqueue_init(&walk->iterator_time, 8, git_commit_list_time_cmp) < 0 ||
git_vector_init(&walk->twos, 4, NULL) < 0 ||
git_pool_init(&walk->commit_pool, 1,
git_pool__suggest_items_per_page(COMMIT_ALLOC) * COMMIT_ALLOC) < 0)
return -1;
walk->get_next = &revwalk_next_unsorted;
walk->enqueue = &revwalk_enqueue_unsorted;
walk->repo = repo;
if (git_repository_odb(&walk->odb, repo) < 0) {
git_revwalk_free(walk);
return -1;
}
*revwalk_out = walk;
return 0;
}
void git_revwalk_free(git_revwalk *walk)
{
if (walk == NULL)
return;
git_revwalk_reset(walk);
git_odb_free(walk->odb);
git_oidmap_free(walk->commits);
git_pool_clear(&walk->commit_pool);
git_pqueue_free(&walk->iterator_time);
git_vector_free(&walk->twos);
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;
git_commit_list_node *next;
assert(walk && oid);
if (!walk->walking) {
if ((error = prepare_walk(walk)) < 0)
return error;
}
error = walk->get_next(&next, walk);
if (error == GIT_ITEROVER) {
git_revwalk_reset(walk);
giterr_clear();
return GIT_ITEROVER;
}
if (!error)
git_oid_cpy(oid, &next->oid);
return error;
}
void git_revwalk_reset(git_revwalk *walk)
{
git_commit_list_node *commit;
assert(walk);
kh_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);
git_commit_list_free(&walk->iterator_topo);
git_commit_list_free(&walk->iterator_rand);
git_commit_list_free(&walk->iterator_reverse);
walk->walking = 0;
walk->one = NULL;
git_vector_clear(&walk->twos);
}
|