gitweb: Remove $project from git_get_paging_nav arguments
[git.git] / send-pack.c
blob10bc8bc35935c6ba3af70456781d6194c55ba274
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;
15 static int use_thin_pack = 0;
17 static int is_zero_sha1(const unsigned char *sha1)
19 int i;
21 for (i = 0; i < 20; i++) {
22 if (*sha1++)
23 return 0;
25 return 1;
28 static void exec_pack_objects(void)
30 static const char *args[] = {
31 "pack-objects",
32 "--stdout",
33 NULL
35 execv_git_cmd(args);
36 die("git-pack-objects exec failed (%s)", strerror(errno));
39 static void exec_rev_list(struct ref *refs)
41 struct ref *ref;
42 static const char *args[1000];
43 int i = 0, j;
45 args[i++] = "rev-list"; /* 0 */
46 if (use_thin_pack) /* 1 */
47 args[i++] = "--objects-edge";
48 else
49 args[i++] = "--objects";
51 /* First send the ones we care about most */
52 for (ref = refs; ref; ref = ref->next) {
53 if (900 < i)
54 die("git-rev-list environment overflow");
55 if (!is_zero_sha1(ref->new_sha1)) {
56 char *buf = malloc(100);
57 args[i++] = buf;
58 snprintf(buf, 50, "%s", sha1_to_hex(ref->new_sha1));
59 buf += 50;
60 if (!is_zero_sha1(ref->old_sha1) &&
61 has_sha1_file(ref->old_sha1)) {
62 args[i++] = buf;
63 snprintf(buf, 50, "^%s",
64 sha1_to_hex(ref->old_sha1));
69 /* Then a handful of the remainder
70 * NEEDSWORK: we would be better off if used the newer ones first.
72 for (ref = refs, j = i + 16;
73 i < 900 && i < j && ref;
74 ref = ref->next) {
75 if (is_zero_sha1(ref->new_sha1) &&
76 !is_zero_sha1(ref->old_sha1) &&
77 has_sha1_file(ref->old_sha1)) {
78 char *buf = malloc(42);
79 args[i++] = buf;
80 snprintf(buf, 42, "^%s", sha1_to_hex(ref->old_sha1));
83 args[i] = NULL;
84 execv_git_cmd(args);
85 die("git-rev-list exec failed (%s)", strerror(errno));
88 static void rev_list(int fd, struct ref *refs)
90 int pipe_fd[2];
91 pid_t pack_objects_pid;
93 if (pipe(pipe_fd) < 0)
94 die("rev-list setup: pipe failed");
95 pack_objects_pid = fork();
96 if (!pack_objects_pid) {
97 dup2(pipe_fd[0], 0);
98 dup2(fd, 1);
99 close(pipe_fd[0]);
100 close(pipe_fd[1]);
101 close(fd);
102 exec_pack_objects();
103 die("pack-objects setup failed");
105 if (pack_objects_pid < 0)
106 die("pack-objects fork failed");
107 dup2(pipe_fd[1], 1);
108 close(pipe_fd[0]);
109 close(pipe_fd[1]);
110 close(fd);
111 exec_rev_list(refs);
114 static int pack_objects(int fd, struct ref *refs)
116 pid_t rev_list_pid;
118 rev_list_pid = fork();
119 if (!rev_list_pid) {
120 rev_list(fd, refs);
121 die("rev-list setup failed");
123 if (rev_list_pid < 0)
124 die("rev-list fork failed");
126 * We don't wait for the rev-list pipeline in the parent:
127 * we end up waiting for the other end instead
129 return 0;
132 static void unmark_and_free(struct commit_list *list, unsigned int mark)
134 while (list) {
135 struct commit_list *temp = list;
136 temp->item->object.flags &= ~mark;
137 list = temp->next;
138 free(temp);
142 static int ref_newer(const unsigned char *new_sha1,
143 const unsigned char *old_sha1)
145 struct object *o;
146 struct commit *old, *new;
147 struct commit_list *list, *used;
148 int found = 0;
150 /* Both new and old must be commit-ish and new is descendant of
151 * old. Otherwise we require --force.
153 o = deref_tag(parse_object(old_sha1), NULL, 0);
154 if (!o || o->type != OBJ_COMMIT)
155 return 0;
156 old = (struct commit *) o;
158 o = deref_tag(parse_object(new_sha1), NULL, 0);
159 if (!o || o->type != OBJ_COMMIT)
160 return 0;
161 new = (struct commit *) o;
163 if (parse_commit(new) < 0)
164 return 0;
166 used = list = NULL;
167 commit_list_insert(new, &list);
168 while (list) {
169 new = pop_most_recent_commit(&list, 1);
170 commit_list_insert(new, &used);
171 if (new == old) {
172 found = 1;
173 break;
176 unmark_and_free(list, 1);
177 unmark_and_free(used, 1);
178 return found;
181 static struct ref *local_refs, **local_tail;
182 static struct ref *remote_refs, **remote_tail;
184 static int one_local_ref(const char *refname, const unsigned char *sha1)
186 struct ref *ref;
187 int len = strlen(refname) + 1;
188 ref = xcalloc(1, sizeof(*ref) + len);
189 memcpy(ref->new_sha1, sha1, 20);
190 memcpy(ref->name, refname, len);
191 *local_tail = ref;
192 local_tail = &ref->next;
193 return 0;
196 static void get_local_heads(void)
198 local_tail = &local_refs;
199 for_each_ref(one_local_ref);
202 static int receive_status(int in)
204 char line[1000];
205 int ret = 0;
206 int len = packet_read_line(in, line, sizeof(line));
207 if (len < 10 || memcmp(line, "unpack ", 7)) {
208 fprintf(stderr, "did not receive status back\n");
209 return -1;
211 if (memcmp(line, "unpack ok\n", 10)) {
212 fputs(line, stderr);
213 ret = -1;
215 while (1) {
216 len = packet_read_line(in, line, sizeof(line));
217 if (!len)
218 break;
219 if (len < 3 ||
220 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
221 fprintf(stderr, "protocol error: %s\n", line);
222 ret = -1;
223 break;
225 if (!memcmp(line, "ok", 2))
226 continue;
227 fputs(line, stderr);
228 ret = -1;
230 return ret;
233 static int send_pack(int in, int out, int nr_refspec, char **refspec)
235 struct ref *ref;
236 int new_refs;
237 int ret = 0;
238 int ask_for_status_report = 0;
239 int expect_status_report = 0;
241 /* No funny business with the matcher */
242 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
243 get_local_heads();
245 /* Does the other end support the reporting? */
246 if (server_supports("report-status"))
247 ask_for_status_report = 1;
249 /* match them up */
250 if (!remote_tail)
251 remote_tail = &remote_refs;
252 if (match_refs(local_refs, remote_refs, &remote_tail,
253 nr_refspec, refspec, send_all))
254 return -1;
256 if (!remote_refs) {
257 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
258 return 0;
262 * Finally, tell the other end!
264 new_refs = 0;
265 for (ref = remote_refs; ref; ref = ref->next) {
266 char old_hex[60], *new_hex;
267 if (!ref->peer_ref)
268 continue;
269 if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
270 if (verbose)
271 fprintf(stderr, "'%s': up-to-date\n", ref->name);
272 continue;
275 /* This part determines what can overwrite what.
276 * The rules are:
278 * (0) you can always use --force or +A:B notation to
279 * selectively force individual ref pairs.
281 * (1) if the old thing does not exist, it is OK.
283 * (2) if you do not have the old thing, you are not allowed
284 * to overwrite it; you would not know what you are losing
285 * otherwise.
287 * (3) if both new and old are commit-ish, and new is a
288 * descendant of old, it is OK.
291 if (!force_update &&
292 !is_zero_sha1(ref->old_sha1) &&
293 !ref->force) {
294 if (!has_sha1_file(ref->old_sha1) ||
295 !ref_newer(ref->peer_ref->new_sha1,
296 ref->old_sha1)) {
297 /* We do not have the remote ref, or
298 * we know that the remote ref is not
299 * an ancestor of what we are trying to
300 * push. Either way this can be losing
301 * commits at the remote end and likely
302 * we were not up to date to begin with.
304 error("remote '%s' is not a strict "
305 "subset of local ref '%s'. "
306 "maybe you are not up-to-date and "
307 "need to pull first?",
308 ref->name,
309 ref->peer_ref->name);
310 ret = -2;
311 continue;
314 memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
315 if (is_zero_sha1(ref->new_sha1)) {
316 error("cannot happen anymore");
317 ret = -3;
318 continue;
320 new_refs++;
321 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
322 new_hex = sha1_to_hex(ref->new_sha1);
324 if (ask_for_status_report) {
325 packet_write(out, "%s %s %s%c%s",
326 old_hex, new_hex, ref->name, 0,
327 "report-status");
328 ask_for_status_report = 0;
329 expect_status_report = 1;
331 else
332 packet_write(out, "%s %s %s",
333 old_hex, new_hex, ref->name);
334 fprintf(stderr, "updating '%s'", ref->name);
335 if (strcmp(ref->name, ref->peer_ref->name))
336 fprintf(stderr, " using '%s'", ref->peer_ref->name);
337 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
340 packet_flush(out);
341 if (new_refs)
342 pack_objects(out, remote_refs);
343 close(out);
345 if (expect_status_report) {
346 if (receive_status(in))
347 ret = -4;
350 if (!new_refs && ret == 0)
351 fprintf(stderr, "Everything up-to-date\n");
352 return ret;
356 int main(int argc, char **argv)
358 int i, nr_heads = 0;
359 char *dest = NULL;
360 char **heads = NULL;
361 int fd[2], ret;
362 pid_t pid;
364 setup_git_directory();
365 git_config(git_default_config);
367 argv++;
368 for (i = 1; i < argc; i++, argv++) {
369 char *arg = *argv;
371 if (*arg == '-') {
372 if (!strncmp(arg, "--exec=", 7)) {
373 exec = arg + 7;
374 continue;
376 if (!strcmp(arg, "--all")) {
377 send_all = 1;
378 continue;
380 if (!strcmp(arg, "--force")) {
381 force_update = 1;
382 continue;
384 if (!strcmp(arg, "--verbose")) {
385 verbose = 1;
386 continue;
388 if (!strcmp(arg, "--thin")) {
389 use_thin_pack = 1;
390 continue;
392 usage(send_pack_usage);
394 if (!dest) {
395 dest = arg;
396 continue;
398 heads = argv;
399 nr_heads = argc - i;
400 break;
402 if (!dest)
403 usage(send_pack_usage);
404 if (heads && send_all)
405 usage(send_pack_usage);
406 pid = git_connect(fd, dest, exec);
407 if (pid < 0)
408 return 1;
409 ret = send_pack(fd[0], fd[1], nr_heads, heads);
410 close(fd[0]);
411 close(fd[1]);
412 finish_connect(pid);
413 return ret;