pack-objects: reuse data from existing packs.
[alt-git.git] / send-pack.c
blob990be3f1a338a34025afa4acbe25f4c67ea6ce30
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "refs.h"
5 #include "pkt-line.h"
6 #include "exec_cmd.h"
8 static const char send_pack_usage[] =
9 "git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
10 " --all and explicit <head> specification are mutually exclusive.";
11 static const char *exec = "git-receive-pack";
12 static int verbose = 0;
13 static int send_all = 0;
14 static int force_update = 0;
16 static int is_zero_sha1(const unsigned char *sha1)
18 int i;
20 for (i = 0; i < 20; i++) {
21 if (*sha1++)
22 return 0;
24 return 1;
27 static void exec_pack_objects(void)
29 static char *args[] = {
30 "pack-objects",
31 "--stdout",
32 NULL
34 execv_git_cmd(args);
35 die("git-pack-objects exec failed (%s)", strerror(errno));
38 static void exec_rev_list(struct ref *refs)
40 static char *args[1000];
41 int i = 0;
43 args[i++] = "rev-list"; /* 0 */
44 args[i++] = "--objects"; /* 1 */
45 while (refs) {
46 char *buf = malloc(100);
47 if (i > 900)
48 die("git-rev-list environment overflow");
49 if (!is_zero_sha1(refs->old_sha1) &&
50 has_sha1_file(refs->old_sha1)) {
51 args[i++] = buf;
52 snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
53 buf += 50;
55 if (!is_zero_sha1(refs->new_sha1)) {
56 args[i++] = buf;
57 snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
59 refs = refs->next;
61 args[i] = NULL;
62 execv_git_cmd(args);
63 die("git-rev-list exec failed (%s)", strerror(errno));
66 static void rev_list(int fd, struct ref *refs)
68 int pipe_fd[2];
69 pid_t pack_objects_pid;
71 if (pipe(pipe_fd) < 0)
72 die("rev-list setup: pipe failed");
73 pack_objects_pid = fork();
74 if (!pack_objects_pid) {
75 dup2(pipe_fd[0], 0);
76 dup2(fd, 1);
77 close(pipe_fd[0]);
78 close(pipe_fd[1]);
79 close(fd);
80 exec_pack_objects();
81 die("pack-objects setup failed");
83 if (pack_objects_pid < 0)
84 die("pack-objects fork failed");
85 dup2(pipe_fd[1], 1);
86 close(pipe_fd[0]);
87 close(pipe_fd[1]);
88 close(fd);
89 exec_rev_list(refs);
92 static int pack_objects(int fd, struct ref *refs)
94 pid_t rev_list_pid;
96 rev_list_pid = fork();
97 if (!rev_list_pid) {
98 rev_list(fd, refs);
99 die("rev-list setup failed");
101 if (rev_list_pid < 0)
102 die("rev-list fork failed");
104 * We don't wait for the rev-list pipeline in the parent:
105 * we end up waiting for the other end instead
107 return 0;
110 static void unmark_and_free(struct commit_list *list, unsigned int mark)
112 while (list) {
113 struct commit_list *temp = list;
114 temp->item->object.flags &= ~mark;
115 list = temp->next;
116 free(temp);
120 static int ref_newer(const unsigned char *new_sha1,
121 const unsigned char *old_sha1)
123 struct object *o;
124 struct commit *old, *new;
125 struct commit_list *list, *used;
126 int found = 0;
128 /* Both new and old must be commit-ish and new is descendant of
129 * old. Otherwise we require --force.
131 o = deref_tag(parse_object(old_sha1), NULL, 0);
132 if (!o || o->type != commit_type)
133 return 0;
134 old = (struct commit *) o;
136 o = deref_tag(parse_object(new_sha1), NULL, 0);
137 if (!o || o->type != commit_type)
138 return 0;
139 new = (struct commit *) o;
141 if (parse_commit(new) < 0)
142 return 0;
144 used = list = NULL;
145 commit_list_insert(new, &list);
146 while (list) {
147 new = pop_most_recent_commit(&list, 1);
148 commit_list_insert(new, &used);
149 if (new == old) {
150 found = 1;
151 break;
154 unmark_and_free(list, 1);
155 unmark_and_free(used, 1);
156 return found;
159 static struct ref *local_refs, **local_tail;
160 static struct ref *remote_refs, **remote_tail;
162 static int one_local_ref(const char *refname, const unsigned char *sha1)
164 struct ref *ref;
165 int len = strlen(refname) + 1;
166 ref = xcalloc(1, sizeof(*ref) + len);
167 memcpy(ref->new_sha1, sha1, 20);
168 memcpy(ref->name, refname, len);
169 *local_tail = ref;
170 local_tail = &ref->next;
171 return 0;
174 static void get_local_heads(void)
176 local_tail = &local_refs;
177 for_each_ref(one_local_ref);
180 static int receive_status(int in)
182 char line[1000];
183 int ret = 0;
184 int len = packet_read_line(in, line, sizeof(line));
185 if (len < 10 || memcmp(line, "unpack ", 7)) {
186 fprintf(stderr, "did not receive status back\n");
187 return -1;
189 if (memcmp(line, "unpack ok\n", 10)) {
190 fputs(line, stderr);
191 ret = -1;
193 while (1) {
194 len = packet_read_line(in, line, sizeof(line));
195 if (!len)
196 break;
197 if (len < 3 ||
198 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
199 fprintf(stderr, "protocol error: %s\n", line);
200 ret = -1;
201 break;
203 if (!memcmp(line, "ok", 2))
204 continue;
205 fputs(line, stderr);
206 ret = -1;
208 return ret;
211 static int send_pack(int in, int out, int nr_refspec, char **refspec)
213 struct ref *ref;
214 int new_refs;
215 int ret = 0;
216 int ask_for_status_report = 0;
217 int expect_status_report = 0;
219 /* No funny business with the matcher */
220 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, 1);
221 get_local_heads();
223 /* Does the other end support the reporting? */
224 if (server_supports("report-status"))
225 ask_for_status_report = 1;
227 /* match them up */
228 if (!remote_tail)
229 remote_tail = &remote_refs;
230 if (match_refs(local_refs, remote_refs, &remote_tail,
231 nr_refspec, refspec, send_all))
232 return -1;
234 if (!remote_refs) {
235 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
236 return 0;
240 * Finally, tell the other end!
242 new_refs = 0;
243 for (ref = remote_refs; ref; ref = ref->next) {
244 char old_hex[60], *new_hex;
245 if (!ref->peer_ref)
246 continue;
247 if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
248 if (verbose)
249 fprintf(stderr, "'%s': up-to-date\n", ref->name);
250 continue;
253 /* This part determines what can overwrite what.
254 * The rules are:
256 * (0) you can always use --force or +A:B notation to
257 * selectively force individual ref pairs.
259 * (1) if the old thing does not exist, it is OK.
261 * (2) if you do not have the old thing, you are not allowed
262 * to overwrite it; you would not know what you are losing
263 * otherwise.
265 * (3) if both new and old are commit-ish, and new is a
266 * descendant of old, it is OK.
269 if (!force_update &&
270 !is_zero_sha1(ref->old_sha1) &&
271 !ref->force) {
272 if (!has_sha1_file(ref->old_sha1) ||
273 !ref_newer(ref->peer_ref->new_sha1,
274 ref->old_sha1)) {
275 /* We do not have the remote ref, or
276 * we know that the remote ref is not
277 * an ancestor of what we are trying to
278 * push. Either way this can be losing
279 * commits at the remote end and likely
280 * we were not up to date to begin with.
282 error("remote '%s' is not a strict "
283 "subset of local ref '%s'. "
284 "maybe you are not up-to-date and "
285 "need to pull first?",
286 ref->name,
287 ref->peer_ref->name);
288 ret = -2;
289 continue;
292 memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
293 if (is_zero_sha1(ref->new_sha1)) {
294 error("cannot happen anymore");
295 ret = -3;
296 continue;
298 new_refs++;
299 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
300 new_hex = sha1_to_hex(ref->new_sha1);
302 if (ask_for_status_report) {
303 packet_write(out, "%s %s %s%c%s",
304 old_hex, new_hex, ref->name, 0,
305 "report-status");
306 ask_for_status_report = 0;
307 expect_status_report = 1;
309 else
310 packet_write(out, "%s %s %s",
311 old_hex, new_hex, ref->name);
312 fprintf(stderr, "updating '%s'", ref->name);
313 if (strcmp(ref->name, ref->peer_ref->name))
314 fprintf(stderr, " using '%s'", ref->peer_ref->name);
315 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
318 packet_flush(out);
319 if (new_refs)
320 pack_objects(out, remote_refs);
321 close(out);
323 if (expect_status_report) {
324 if (receive_status(in))
325 ret = -4;
328 if (!new_refs && ret == 0)
329 fprintf(stderr, "Everything up-to-date\n");
330 return ret;
334 int main(int argc, char **argv)
336 int i, nr_heads = 0;
337 char *dest = NULL;
338 char **heads = NULL;
339 int fd[2], ret;
340 pid_t pid;
342 setup_git_directory();
343 argv++;
344 for (i = 1; i < argc; i++, argv++) {
345 char *arg = *argv;
347 if (*arg == '-') {
348 if (!strncmp(arg, "--exec=", 7)) {
349 exec = arg + 7;
350 continue;
352 if (!strcmp(arg, "--all")) {
353 send_all = 1;
354 continue;
356 if (!strcmp(arg, "--force")) {
357 force_update = 1;
358 continue;
360 if (!strcmp(arg, "--verbose")) {
361 verbose = 1;
362 continue;
364 usage(send_pack_usage);
366 if (!dest) {
367 dest = arg;
368 continue;
370 heads = argv;
371 nr_heads = argc - i;
372 break;
374 if (!dest)
375 usage(send_pack_usage);
376 if (heads && send_all)
377 usage(send_pack_usage);
378 pid = git_connect(fd, dest, exec);
379 if (pid < 0)
380 return 1;
381 ret = send_pack(fd[0], fd[1], nr_heads, heads);
382 close(fd[0]);
383 close(fd[1]);
384 finish_connect(pid);
385 return ret;