@matheusbsbs / matheus-bagliano / commits / 23d61f8343
Fork 0

Subject: [PATCH] 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 28, 2005 at 04:46 UTC 23d61f8343282643c830e1c446962b213dbcc09a
1 file changed +132 -32
fetch-pack.c
+132 -32
@@ -13,18 +13,130 @@ 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)
18 +#define SEEN (1U << 3)
19 +#define POPPED (1U << 4)
20 +
21 +static struct commit_list *rev_list = NULL;
22 +static int non_common_revs = 0;
23 +
24 +static void rev_list_push(struct commit *commit, int mark)
25 +{
26 + if (!(commit->object.flags & mark)) {
27 + commit->object.flags |= mark;
28 +
29 + if (!(commit->object.parsed))
30 + parse_commit(commit);
31 +
32 + insert_by_date(commit, &rev_list);
33 +
34 + if (!(commit->object.flags & COMMON))
35 + non_common_revs++;
36 + }
37 +}
38 +
39 +static int rev_list_insert_ref(const char *path, const unsigned char *sha1)
40 +{
41 + struct object *o = deref_tag(parse_object(sha1));
42 +
43 + if (o->type == commit_type)
44 + rev_list_push((struct commit *)o, SEEN);
45 +
46 + return 0;
47 +}
48 +
49 +/*
50 + This function marks a rev and its ancestors as common.
51 + In some cases, it is desirable to mark only the ancestors (for example
52 + when only the server does not yet know that they are common).
53 +*/
54 +
55 +static void mark_common(struct commit *commit,
56 + int ancestors_only, int dont_parse)
57 +{
58 + if (commit != NULL && !(commit->object.flags & COMMON)) {
59 + struct object *o = (struct object *)commit;
60 +
61 + if (!ancestors_only)
62 + o->flags |= COMMON;
63 +
64 + if (!(o->flags & SEEN))
65 + rev_list_push(commit, SEEN);
66 + else {
67 + struct commit_list *parents;
68 +
69 + if (!ancestors_only && !(o->flags & POPPED))
70 + non_common_revs--;
71 + if (!o->parsed && !dont_parse)
72 + parse_commit(commit);
73 +
74 + for (parents = commit->parents;
75 + parents;
76 + parents = parents->next)
77 + mark_common(parents->item, 0, dont_parse);
78 + }
79 + }
80 +}
81 +
82 +/*
83 + Get the next rev to send, ignoring the common.
84 +*/
85 +
86 +static const unsigned char* get_rev()
87 +{
88 + struct commit *commit = NULL;
89 +
90 + while (commit == NULL) {
91 + unsigned int mark;
92 + struct commit_list* parents;
93 +
94 + if (rev_list == NULL || non_common_revs == 0)
95 + return NULL;
96 +
97 + commit = rev_list->item;
98 + if (!(commit->object.parsed))
99 + parse_commit(commit);
100 + commit->object.flags |= POPPED;
101 + if (!(commit->object.flags & COMMON))
102 + non_common_revs--;
103 +
104 + parents = commit->parents;
105 +
106 + if (commit->object.flags & COMMON) {
107 + /* do not send "have", and ignore ancestors */
108 + commit = NULL;
109 + mark = COMMON | SEEN;
110 + } else if (commit->object.flags & COMMON_REF)
111 + /* send "have", and ignore ancestors */
112 + mark = COMMON | SEEN;
113 + else
114 + /* send "have", also for its ancestors */
115 + mark = SEEN;
116 +
117 + while (parents) {
118 + if (!(parents->item->object.flags & SEEN))
119 + rev_list_push(parents->item, mark);
120 + if (mark & COMMON)
121 + mark_common(parents->item, 1, 0);
122 + parents = parents->next;
123 + }
124 +
125 + rev_list = rev_list->next;
126 + }
127 +
128 + return commit->object.sha1;
129 +}
130
131 static int find_common(int fd[2], unsigned char *result_sha1,
132 struct ref *refs)
133 {
134 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;
135 + int count = 0, flushes = 0, retval;
136 + const unsigned char *sha1;
137 +
138 + for_each_ref(rev_list_insert_ref);
139
26 - strcpy(rev_command, "git-rev-list $(git-rev-parse --all)");
27 - rev_command_len = strlen(rev_command);
140 fetching = 0;
141 for ( ; refs ; refs = refs->next) {
142 unsigned char *remote = refs->old_sha1;
@@ -42,25 +154,18 @@ static int find_common(int fd[2], unsigned char *result_sha1,
154 */
155 if (((o = lookup_object(remote)) != NULL) &&
156 (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;
157 + o = deref_tag(o);
158 +
159 + if (o->type == commit_type) {
160 + struct commit *commit = (struct commit *)o;
161 +
162 + rev_list_push(commit, COMMON_REF | SEEN);
163 +
164 + mark_common(commit, 1, 1);
165 }
166 continue;
167 }
63 - repair:
168 +
169 packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
170 fetching++;
171 }
@@ -68,16 +173,9 @@ static int find_common(int fd[2], unsigned char *result_sha1,
173 if (!fetching)
174 return 1;
175
71 - revs = popen(rev_command, "r");
72 - if (!revs)
73 - die("unable to run 'git-rev-list'");
74 -
75 - flushes = 1;
176 + flushes = 0;
177 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");
178 + while ((sha1 = get_rev())) {
179 packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
180 if (verbose)
181 fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
@@ -101,10 +199,12 @@ static int find_common(int fd[2], unsigned char *result_sha1,
199 flushes--;
200 }
201 }
104 - pclose(revs);
202 +
203 packet_write(fd[1], "done\n");
204 if (verbose)
205 fprintf(stderr, "done\n");
206 + if (retval != 0)
207 + flushes++;
208 while (flushes) {
209 flushes--;
210 if (get_ack(fd[0], result_sha1)) {