use name[len] in switch directly, instead of creating a shadowed variable.
[git/msvc.git] / send-pack.c
blobf7c0cfc6f67ba4d325ac6fc9f6c74fad78d1208e
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;
13 static int send_all;
14 static int force_update;
15 static int use_thin_pack;
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 void 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
131 static void unmark_and_free(struct commit_list *list, unsigned int mark)
133 while (list) {
134 struct commit_list *temp = list;
135 temp->item->object.flags &= ~mark;
136 list = temp->next;
137 free(temp);
141 static int ref_newer(const unsigned char *new_sha1,
142 const unsigned char *old_sha1)
144 struct object *o;
145 struct commit *old, *new;
146 struct commit_list *list, *used;
147 int found = 0;
149 /* Both new and old must be commit-ish and new is descendant of
150 * old. Otherwise we require --force.
152 o = deref_tag(parse_object(old_sha1), NULL, 0);
153 if (!o || o->type != OBJ_COMMIT)
154 return 0;
155 old = (struct commit *) o;
157 o = deref_tag(parse_object(new_sha1), NULL, 0);
158 if (!o || o->type != OBJ_COMMIT)
159 return 0;
160 new = (struct commit *) o;
162 if (parse_commit(new) < 0)
163 return 0;
165 used = list = NULL;
166 commit_list_insert(new, &list);
167 while (list) {
168 new = pop_most_recent_commit(&list, 1);
169 commit_list_insert(new, &used);
170 if (new == old) {
171 found = 1;
172 break;
175 unmark_and_free(list, 1);
176 unmark_and_free(used, 1);
177 return found;
180 static struct ref *local_refs, **local_tail;
181 static struct ref *remote_refs, **remote_tail;
183 static int one_local_ref(const char *refname, const unsigned char *sha1)
185 struct ref *ref;
186 int len = strlen(refname) + 1;
187 ref = xcalloc(1, sizeof(*ref) + len);
188 memcpy(ref->new_sha1, sha1, 20);
189 memcpy(ref->name, refname, len);
190 *local_tail = ref;
191 local_tail = &ref->next;
192 return 0;
195 static void get_local_heads(void)
197 local_tail = &local_refs;
198 for_each_ref(one_local_ref);
201 static int receive_status(int in)
203 char line[1000];
204 int ret = 0;
205 int len = packet_read_line(in, line, sizeof(line));
206 if (len < 10 || memcmp(line, "unpack ", 7)) {
207 fprintf(stderr, "did not receive status back\n");
208 return -1;
210 if (memcmp(line, "unpack ok\n", 10)) {
211 fputs(line, stderr);
212 ret = -1;
214 while (1) {
215 len = packet_read_line(in, line, sizeof(line));
216 if (!len)
217 break;
218 if (len < 3 ||
219 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
220 fprintf(stderr, "protocol error: %s\n", line);
221 ret = -1;
222 break;
224 if (!memcmp(line, "ok", 2))
225 continue;
226 fputs(line, stderr);
227 ret = -1;
229 return ret;
232 static int send_pack(int in, int out, int nr_refspec, char **refspec)
234 struct ref *ref;
235 int new_refs;
236 int ret = 0;
237 int ask_for_status_report = 0;
238 int expect_status_report = 0;
240 /* No funny business with the matcher */
241 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
242 get_local_heads();
244 /* Does the other end support the reporting? */
245 if (server_supports("report-status"))
246 ask_for_status_report = 1;
248 /* match them up */
249 if (!remote_tail)
250 remote_tail = &remote_refs;
251 if (match_refs(local_refs, remote_refs, &remote_tail,
252 nr_refspec, refspec, send_all))
253 return -1;
255 if (!remote_refs) {
256 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
257 return 0;
261 * Finally, tell the other end!
263 new_refs = 0;
264 for (ref = remote_refs; ref; ref = ref->next) {
265 char old_hex[60], *new_hex;
266 if (!ref->peer_ref)
267 continue;
268 if (!hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
269 if (verbose)
270 fprintf(stderr, "'%s': up-to-date\n", ref->name);
271 continue;
274 /* This part determines what can overwrite what.
275 * The rules are:
277 * (0) you can always use --force or +A:B notation to
278 * selectively force individual ref pairs.
280 * (1) if the old thing does not exist, it is OK.
282 * (2) if you do not have the old thing, you are not allowed
283 * to overwrite it; you would not know what you are losing
284 * otherwise.
286 * (3) if both new and old are commit-ish, and new is a
287 * descendant of old, it is OK.
290 if (!force_update &&
291 !is_zero_sha1(ref->old_sha1) &&
292 !ref->force) {
293 if (!has_sha1_file(ref->old_sha1) ||
294 !ref_newer(ref->peer_ref->new_sha1,
295 ref->old_sha1)) {
296 /* We do not have the remote ref, or
297 * we know that the remote ref is not
298 * an ancestor of what we are trying to
299 * push. Either way this can be losing
300 * commits at the remote end and likely
301 * we were not up to date to begin with.
303 error("remote '%s' is not a strict "
304 "subset of local ref '%s'. "
305 "maybe you are not up-to-date and "
306 "need to pull first?",
307 ref->name,
308 ref->peer_ref->name);
309 ret = -2;
310 continue;
313 memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
314 if (is_zero_sha1(ref->new_sha1)) {
315 error("cannot happen anymore");
316 ret = -3;
317 continue;
319 new_refs++;
320 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
321 new_hex = sha1_to_hex(ref->new_sha1);
323 if (ask_for_status_report) {
324 packet_write(out, "%s %s %s%c%s",
325 old_hex, new_hex, ref->name, 0,
326 "report-status");
327 ask_for_status_report = 0;
328 expect_status_report = 1;
330 else
331 packet_write(out, "%s %s %s",
332 old_hex, new_hex, ref->name);
333 fprintf(stderr, "updating '%s'", ref->name);
334 if (strcmp(ref->name, ref->peer_ref->name))
335 fprintf(stderr, " using '%s'", ref->peer_ref->name);
336 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
339 packet_flush(out);
340 if (new_refs)
341 pack_objects(out, remote_refs);
342 close(out);
344 if (expect_status_report) {
345 if (receive_status(in))
346 ret = -4;
349 if (!new_refs && ret == 0)
350 fprintf(stderr, "Everything up-to-date\n");
351 return ret;
355 int main(int argc, char **argv)
357 int i, nr_heads = 0;
358 char *dest = NULL;
359 char **heads = NULL;
360 int fd[2], ret;
361 pid_t pid;
363 setup_git_directory();
364 git_config(git_default_config);
366 argv++;
367 for (i = 1; i < argc; i++, argv++) {
368 char *arg = *argv;
370 if (*arg == '-') {
371 if (!strncmp(arg, "--exec=", 7)) {
372 exec = arg + 7;
373 continue;
375 if (!strcmp(arg, "--all")) {
376 send_all = 1;
377 continue;
379 if (!strcmp(arg, "--force")) {
380 force_update = 1;
381 continue;
383 if (!strcmp(arg, "--verbose")) {
384 verbose = 1;
385 continue;
387 if (!strcmp(arg, "--thin")) {
388 use_thin_pack = 1;
389 continue;
391 usage(send_pack_usage);
393 if (!dest) {
394 dest = arg;
395 continue;
397 heads = argv;
398 nr_heads = argc - i;
399 break;
401 if (!dest)
402 usage(send_pack_usage);
403 if (heads && send_all)
404 usage(send_pack_usage);
405 pid = git_connect(fd, dest, exec);
406 if (pid < 0)
407 return 1;
408 ret = send_pack(fd[0], fd[1], nr_heads, heads);
409 close(fd[0]);
410 close(fd[1]);
411 finish_connect(pid);
412 return ret;