Skip to content

Commit b5039db

Browse files
iabervonLinus Torvalds
authored andcommitted
[PATCH] Switch implementations of merge-base, port to parsing
This switches to my implementation of merge-base, but with the new parsing library. Signed-Off-By: Daniel Barkalow <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent ff5ebe3 commit b5039db

File tree

2 files changed

+82
-44
lines changed

2 files changed

+82
-44
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ check-files: check-files.o read-cache.o
6464
ls-tree: ls-tree.o read-cache.o
6565
$(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
6666

67-
merge-base: merge-base.o read-cache.o
68-
$(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o $(LIBS)
67+
merge-base: merge-base.o read-cache.o object.o commit.o tree.o blob.o
68+
$(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
6969

7070
read-cache.o: cache.h
7171
show-diff.o: cache.h

merge-base.c

Lines changed: 80 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,95 @@
1+
#include <stdlib.h>
12
#include "cache.h"
2-
#include "revision.h"
3+
#include "commit.h"
34

4-
/*
5-
* This is stupid. We could have much better heurstics, I bet.
6-
*/
7-
static int better(struct revision *new, struct revision *old)
5+
static struct commit *process_list(struct commit_list **list_p, int this_mark,
6+
int other_mark)
87
{
9-
return new->date > old->date;
8+
struct commit_list *parent, *temp;
9+
struct commit_list *posn = *list_p;
10+
*list_p = NULL;
11+
while (posn) {
12+
parse_commit(posn->item);
13+
if (posn->item->object.flags & this_mark) {
14+
/*
15+
printf("%d already seen %s %x\n",
16+
this_mark
17+
sha1_to_hex(posn->parent->sha1),
18+
posn->parent->flags);
19+
*/
20+
/* do nothing; this indicates that this side
21+
* split and reformed, and we only need to
22+
* mark it once.
23+
*/
24+
} else if (posn->item->object.flags & other_mark) {
25+
return posn->item;
26+
} else {
27+
/*
28+
printf("%d based on %s\n",
29+
this_mark,
30+
sha1_to_hex(posn->parent->sha1));
31+
*/
32+
posn->item->object.flags |= this_mark;
33+
34+
parent = posn->item->parents;
35+
while (parent) {
36+
temp = malloc(sizeof(struct commit_list));
37+
temp->next = *list_p;
38+
temp->item = parent->item;
39+
*list_p = temp;
40+
parent = parent->next;
41+
}
42+
}
43+
posn = posn->next;
44+
}
45+
return NULL;
1046
}
1147

12-
static struct revision *common_parent(struct revision *rev1, struct revision *rev2)
48+
struct commit *common_ancestor(struct commit *rev1, struct commit *rev2)
1349
{
14-
int i;
15-
struct revision *best = NULL;
50+
struct commit_list *rev1list = malloc(sizeof(struct commit_list));
51+
struct commit_list *rev2list = malloc(sizeof(struct commit_list));
52+
53+
rev1list->item = rev1;
54+
rev1list->next = NULL;
1655

17-
mark_reachable(rev1, 1);
18-
mark_reachable(rev2, 2);
19-
for (i = 0; i < nr_revs ;i++) {
20-
struct revision *rev = revs[i];
21-
if ((rev->flags & 3) != 3)
22-
continue;
23-
if (!best) {
24-
best = rev;
25-
continue;
56+
rev2list->item = rev2;
57+
rev2list->next = NULL;
58+
59+
while (rev1list || rev2list) {
60+
struct commit *ret;
61+
ret = process_list(&rev1list, 0x1, 0x2);
62+
if (ret) {
63+
/* XXXX free lists */
64+
return ret;
65+
}
66+
ret = process_list(&rev2list, 0x2, 0x1);
67+
if (ret) {
68+
/* XXXX free lists */
69+
return ret;
2670
}
27-
if (better(rev, best))
28-
best = rev;
2971
}
30-
return best;
72+
return NULL;
3173
}
3274

3375
int main(int argc, char **argv)
3476
{
35-
unsigned char rev1[20], rev2[20];
36-
struct revision *common;
77+
struct commit *rev1, *rev2, *ret;
78+
unsigned char rev1key[20], rev2key[20];
3779

38-
if (argc != 3 || get_sha1_hex(argv[1], rev1) || get_sha1_hex(argv[2], rev2))
39-
usage("merge-base <commit1> <commit2>");
40-
41-
/*
42-
* We will eventually want to include a revision cache file
43-
* that "rev-tree.c" has generated, since this is going to
44-
* otherwise be quite expensive for big trees..
45-
*
46-
* That's some time off, though, and in the meantime we know
47-
* that we have a solution to the eventual expense.
48-
*/
49-
parse_commit(rev1);
50-
parse_commit(rev2);
51-
52-
common = common_parent(lookup_rev(rev1), lookup_rev(rev2));
53-
if (!common)
54-
die("no common parent found");
55-
printf("%s\n", sha1_to_hex(common->sha1));
56-
return 0;
80+
if (argc != 3 ||
81+
get_sha1_hex(argv[1], rev1key) ||
82+
get_sha1_hex(argv[2], rev2key)) {
83+
usage("merge-base <commit-id> <commit-id>");
84+
}
85+
rev1 = lookup_commit(rev1key);
86+
rev2 = lookup_commit(rev2key);
87+
ret = common_ancestor(rev1, rev2);
88+
if (ret) {
89+
printf("%s\n", sha1_to_hex(ret->object.sha1));
90+
return 0;
91+
} else {
92+
return 1;
93+
}
94+
5795
}

0 commit comments

Comments
 (0)