@matheusbsbs / matheus-bagliano / commits / 69779a562a
Fork 0

git-fetch-pack: Do not use git-rev-list

The code used to call git-rev-list to enumerate the local revisions. A disadvantage of that method was that git-rev-list, lacking a control apart from the command line, would happily enumerate ancestors of acknowledged common commits, which was just taking unnecessary bandwidth. Therefore, do not use git-rev-list on the fetching side, but rather construct the list on the go. Send the revisions starting from the local heads, ignoring the revisions known to be common. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Oct 23, 2005 at 03:39 UTC 69779a562a171d9b70bbb17b89e076bd6b16ae6c
1 file changed +119 -32
fetch-pack.c
+119 -32
@@ -13,18 +13,123 @@ static const char fetch_pack_usage[] =
13 static const char *exec = "git-upload-pack";
14
15 #define COMPLETE (1U << 0)
16 +#define COMMON (1U << 1)
17 +#define COMMON_REF (1U << 2 | COMMON)
18 +#define SEEN (1U << 3)
19 +#define POPPED (1U << 4)
20 +
21 +static struct commit_list *rev_list = NULL;
22 +static struct commit_list *rev_list_end = NULL;
23 +static unsigned long non_common_revs = 0;
24 +
25 +static void rev_list_append(struct commit *commit, int mark)
26 +{
27 + if (!(commit->object.flags & mark)) {
28 + commit->object.flags |= mark;
29 +
30 + if (rev_list == NULL) {
31 + commit_list_insert(commit, &rev_list);
32 + rev_list_end = rev_list;
33 + } else {
34 + commit_list_insert(commit, &(rev_list_end->next));
35 + rev_list_end = rev_list_end->next;
36 + }
37 +
38 + if (!(commit->object.flags & COMMON))
39 + non_common_revs++;
40 + }
41 +}
42 +
43 +static int rev_list_append_sha1(const char *path, const unsigned char *sha1)
44 +{
45 + struct object *o = deref_tag(parse_object(sha1));
46 +
47 + if (o->type == commit_type)
48 + rev_list_append((struct commit *)o, SEEN);
49 +
50 + return 0;
51 +}
52 +
53 +static void mark_common(struct commit *commit)
54 +{
55 + if (commit != NULL && !(commit->object.flags & COMMON)) {
56 + struct object *o = (struct object *)commit;
57 + o->flags |= COMMON;
58 + if (!(o->flags & SEEN))
59 + rev_list_append(commit, SEEN);
60 + else {
61 + struct commit_list *parents;
62 +
63 + if (!(o->flags & POPPED))
64 + non_common_revs--;
65 + if (!o->parsed)
66 + parse_commit(commit);
67 + for (parents = commit->parents;
68 + parents;
69 + parents = parents->next)
70 + mark_common(parents->item);
71 + }
72 + }
73 +}
74 +
75 +/*
76 + Get the next rev to send, ignoring the common.
77 +*/
78 +
79 +static const unsigned char* get_rev()
80 +{
81 + struct commit *commit = NULL;
82 +
83 + while (commit == NULL) {
84 + unsigned int mark;
85 + struct commit_list* parents;
86 +
87 + if (rev_list == NULL || non_common_revs == 0)
88 + return NULL;
89 +
90 + commit = rev_list->item;
91 + if (!(commit->object.parsed))
92 + parse_commit(commit);
93 + commit->object.flags |= POPPED;
94 + if (!(commit->object.flags & COMMON))
95 + non_common_revs--;
96 +
97 + parents = commit->parents;
98 +
99 + if (commit->object.flags & COMMON) {
100 + /* do not send "have", and ignore ancestors */
101 + commit = NULL;
102 + mark = COMMON | SEEN;
103 + } else if (commit->object.flags & COMMON_REF)
104 + /* send "have", and ignore ancestors */
105 + mark = COMMON | SEEN;
106 + else
107 + /* send "have", also for its ancestors */
108 + mark = SEEN;
109 +
110 + while (parents) {
111 + if (mark & COMMON)
112 + mark_common(parents->item);
113 + else
114 + rev_list_append(parents->item, mark);
115 + parents = parents->next;
116 + }
117 +
118 + rev_list = rev_list->next;
119 + }
120 +
121 + return commit->object.sha1;
122 +}
123
124 static int find_common(int fd[2], unsigned char *result_sha1,
125 struct ref *refs)
126 {
127 int fetching;
21 - static char line[1000];
22 - static char rev_command[1024];
23 - int count = 0, flushes = 0, retval, rev_command_len;
24 - FILE *revs;
128 + int count = 0, flushes = 0, retval;
129 + const unsigned char *sha1;
130 +
131 + for_each_ref(rev_list_append_sha1);
132
26 - strcpy(rev_command, "git-rev-list $(git-rev-parse --all)");
27 - rev_command_len = strlen(rev_command);
133 fetching = 0;
134 for ( ; refs ; refs = refs->next) {
135 unsigned char *remote = refs->old_sha1;
@@ -42,25 +147,15 @@ static int find_common(int fd[2], unsigned char *result_sha1,
147 */
148 if (((o = lookup_object(remote)) != NULL) &&
149 (o->flags & COMPLETE)) {
45 - struct commit_list *p;
46 - struct commit *commit =
47 - (struct commit *) (o = deref_tag(o));
48 - if (!o)
49 - goto repair;
50 - if (o->type != commit_type)
51 - continue;
52 - p = commit->parents;
53 - while (p &&
54 - rev_command_len + 44 < sizeof(rev_command)) {
55 - snprintf(rev_command + rev_command_len, 44,
56 - " ^%s",
57 - sha1_to_hex(p->item->object.sha1));
58 - rev_command_len += 43;
59 - p = p->next;
60 - }
150 + o = deref_tag(o);
151 +
152 + if (o->type == commit_type)
153 + rev_list_append((struct commit *)o,
154 + COMMON_REF | SEEN);
155 +
156 continue;
157 }
63 - repair:
158 +
159 packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
160 fetching++;
161 }
@@ -68,16 +163,9 @@ static int find_common(int fd[2], unsigned char *result_sha1,
163 if (!fetching)
164 return 1;
165
71 - revs = popen(rev_command, "r");
72 - if (!revs)
73 - die("unable to run 'git-rev-list'");
74 -
166 flushes = 1;
167 retval = -1;
77 - while (fgets(line, sizeof(line), revs) != NULL) {
78 - unsigned char sha1[20];
79 - if (get_sha1_hex(line, sha1))
80 - die("git-fetch-pack: expected object name, got crud");
168 + while ((sha1 = get_rev())) {
169 packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
170 if (verbose)
171 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
@@ -101,7 +189,6 @@ static int find_common(int fd[2], unsigned char *result_sha1,
189 flushes--;
190 }
191 }
104 - pclose(revs);
192 packet_write(fd[1], "done\n");
193 if (verbose)
194 fprintf(stderr, "done\n");