Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 1 | #include "cache.h" |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 2 | #include "commit.h" |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 3 | #include "tag.h" |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 4 | #include "refs.h" |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 5 | #include "pkt-line.h" |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 6 | #include "exec_cmd.h" |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 7 | |
Junio C Hamano | 2a24501 | 2005-07-14 00:10:05 -0700 | [diff] [blame] | 8 | static const char send_pack_usage[] = |
Junio C Hamano | 0bc3cdf | 2005-08-02 12:20:27 -0700 | [diff] [blame] | 9 | "git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n" |
| 10 | " --all and explicit <head> specification are mutually exclusive."; |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 11 | static const char *exec = "git-receive-pack"; |
Linus Torvalds | 41f93a2 | 2005-12-20 18:13:02 -0800 | [diff] [blame] | 12 | static int verbose = 0; |
Linus Torvalds | d089391 | 2005-07-16 13:26:33 -0700 | [diff] [blame] | 13 | static int send_all = 0; |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 14 | static int force_update = 0; |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 15 | |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 16 | static int is_zero_sha1(const unsigned char *sha1) |
| 17 | { |
| 18 | int i; |
| 19 | |
| 20 | for (i = 0; i < 20; i++) { |
| 21 | if (*sha1++) |
| 22 | return 0; |
| 23 | } |
| 24 | return 1; |
| 25 | } |
| 26 | |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 27 | static void exec_pack_objects(void) |
| 28 | { |
| 29 | static char *args[] = { |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 30 | "pack-objects", |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 31 | "--stdout", |
| 32 | NULL |
| 33 | }; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 34 | execv_git_cmd(args); |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 35 | die("git-pack-objects exec failed (%s)", strerror(errno)); |
| 36 | } |
| 37 | |
| 38 | static void exec_rev_list(struct ref *refs) |
| 39 | { |
| 40 | static char *args[1000]; |
| 41 | int i = 0; |
| 42 | |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 43 | args[i++] = "rev-list"; /* 0 */ |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 44 | args[i++] = "--objects"; /* 1 */ |
| 45 | while (refs) { |
| 46 | char *buf = malloc(100); |
| 47 | if (i > 900) |
| 48 | die("git-rev-list environment overflow"); |
Junio C Hamano | 40b64d4 | 2005-08-03 12:41:12 -0700 | [diff] [blame] | 49 | if (!is_zero_sha1(refs->old_sha1) && |
| 50 | has_sha1_file(refs->old_sha1)) { |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 51 | args[i++] = buf; |
| 52 | snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1)); |
| 53 | buf += 50; |
| 54 | } |
| 55 | if (!is_zero_sha1(refs->new_sha1)) { |
| 56 | args[i++] = buf; |
| 57 | snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1)); |
| 58 | } |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 59 | refs = refs->next; |
| 60 | } |
| 61 | args[i] = NULL; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 62 | execv_git_cmd(args); |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 63 | die("git-rev-list exec failed (%s)", strerror(errno)); |
| 64 | } |
| 65 | |
| 66 | static void rev_list(int fd, struct ref *refs) |
| 67 | { |
| 68 | int pipe_fd[2]; |
| 69 | pid_t pack_objects_pid; |
| 70 | |
| 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"); |
| 82 | } |
| 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); |
| 90 | } |
| 91 | |
| 92 | static int pack_objects(int fd, struct ref *refs) |
| 93 | { |
| 94 | pid_t rev_list_pid; |
| 95 | |
| 96 | rev_list_pid = fork(); |
| 97 | if (!rev_list_pid) { |
| 98 | rev_list(fd, refs); |
| 99 | die("rev-list setup failed"); |
| 100 | } |
| 101 | if (rev_list_pid < 0) |
| 102 | die("rev-list fork failed"); |
| 103 | /* |
| 104 | * We don't wait for the rev-list pipeline in the parent: |
| 105 | * we end up waiting for the other end instead |
| 106 | */ |
Linus Torvalds | 7ec4e60 | 2005-07-03 10:00:01 -0700 | [diff] [blame] | 107 | return 0; |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 108 | } |
Linus Torvalds | e4b5c7f | 2005-06-29 22:31:41 -0700 | [diff] [blame] | 109 | |
Junio C Hamano | 51b0fca | 2005-08-05 23:05:33 -0700 | [diff] [blame] | 110 | static void unmark_and_free(struct commit_list *list, unsigned int mark) |
| 111 | { |
| 112 | while (list) { |
| 113 | struct commit_list *temp = list; |
| 114 | temp->item->object.flags &= ~mark; |
| 115 | list = temp->next; |
| 116 | free(temp); |
| 117 | } |
| 118 | } |
| 119 | |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 120 | static int ref_newer(const unsigned char *new_sha1, |
| 121 | const unsigned char *old_sha1) |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 122 | { |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 123 | struct object *o; |
| 124 | struct commit *old, *new; |
Junio C Hamano | 51b0fca | 2005-08-05 23:05:33 -0700 | [diff] [blame] | 125 | struct commit_list *list, *used; |
| 126 | int found = 0; |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 127 | |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 128 | /* Both new and old must be commit-ish and new is descendant of |
| 129 | * old. Otherwise we require --force. |
| 130 | */ |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 131 | o = deref_tag(parse_object(old_sha1), NULL, 0); |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 132 | if (!o || o->type != commit_type) |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 133 | return 0; |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 134 | old = (struct commit *) o; |
| 135 | |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 136 | o = deref_tag(parse_object(new_sha1), NULL, 0); |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 137 | if (!o || o->type != commit_type) |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 138 | return 0; |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 139 | new = (struct commit *) o; |
| 140 | |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 141 | if (parse_commit(new) < 0) |
| 142 | return 0; |
Junio C Hamano | 51b0fca | 2005-08-05 23:05:33 -0700 | [diff] [blame] | 143 | |
| 144 | used = list = NULL; |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 145 | commit_list_insert(new, &list); |
Linus Torvalds | bdf2514 | 2005-07-26 20:04:22 -0700 | [diff] [blame] | 146 | while (list) { |
| 147 | new = pop_most_recent_commit(&list, 1); |
Junio C Hamano | 51b0fca | 2005-08-05 23:05:33 -0700 | [diff] [blame] | 148 | commit_list_insert(new, &used); |
| 149 | if (new == old) { |
| 150 | found = 1; |
| 151 | break; |
| 152 | } |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 153 | } |
Junio C Hamano | 51b0fca | 2005-08-05 23:05:33 -0700 | [diff] [blame] | 154 | unmark_and_free(list, 1); |
| 155 | unmark_and_free(used, 1); |
| 156 | return found; |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 159 | static struct ref *local_refs, **local_tail; |
| 160 | static struct ref *remote_refs, **remote_tail; |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 161 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 162 | static int one_local_ref(const char *refname, const unsigned char *sha1) |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 163 | { |
| 164 | struct ref *ref; |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 165 | int len = strlen(refname) + 1; |
| 166 | ref = xcalloc(1, sizeof(*ref) + len); |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 167 | memcpy(ref->new_sha1, sha1, 20); |
| 168 | memcpy(ref->name, refname, len); |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 169 | *local_tail = ref; |
| 170 | local_tail = &ref->next; |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 174 | static void get_local_heads(void) |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 175 | { |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 176 | local_tail = &local_refs; |
| 177 | for_each_ref(one_local_ref); |
| 178 | } |
| 179 | |
Junio C Hamano | cfee10a | 2005-12-25 23:18:37 -0800 | [diff] [blame] | 180 | static int receive_status(int in) |
| 181 | { |
| 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; |
| 188 | } |
| 189 | if (memcmp(line, "unpack ok\n", 10)) { |
| 190 | fputs(line, stderr); |
| 191 | ret = -1; |
| 192 | } |
| 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; |
| 202 | } |
| 203 | if (!memcmp(line, "ok", 2)) |
| 204 | continue; |
| 205 | fputs(line, stderr); |
| 206 | ret = -1; |
| 207 | } |
| 208 | return ret; |
| 209 | } |
| 210 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 211 | static int send_pack(int in, int out, int nr_refspec, char **refspec) |
| 212 | { |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 213 | struct ref *ref; |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 214 | int new_refs; |
Petr Baudis | ed24928 | 2005-12-14 01:45:40 +0100 | [diff] [blame] | 215 | int ret = 0; |
Junio C Hamano | cfee10a | 2005-12-25 23:18:37 -0800 | [diff] [blame] | 216 | int ask_for_status_report = 0; |
| 217 | int expect_status_report = 0; |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 218 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 219 | /* No funny business with the matcher */ |
Junio C Hamano | 1a7141f | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 220 | remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, 1); |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 221 | get_local_heads(); |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 222 | |
Junio C Hamano | cfee10a | 2005-12-25 23:18:37 -0800 | [diff] [blame] | 223 | /* Does the other end support the reporting? */ |
| 224 | if (server_supports("report-status")) |
| 225 | ask_for_status_report = 1; |
| 226 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 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; |
Daniel Barkalow | 4c353e8 | 2005-12-04 11:59:37 -0500 | [diff] [blame] | 233 | |
| 234 | if (!remote_refs) { |
| 235 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n"); |
| 236 | return 0; |
| 237 | } |
| 238 | |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 239 | /* |
| 240 | * Finally, tell the other end! |
| 241 | */ |
| 242 | new_refs = 0; |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 243 | for (ref = remote_refs; ref; ref = ref->next) { |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 244 | char old_hex[60], *new_hex; |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 245 | if (!ref->peer_ref) |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 246 | continue; |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 247 | if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) { |
Linus Torvalds | 41f93a2 | 2005-12-20 18:13:02 -0800 | [diff] [blame] | 248 | if (verbose) |
| 249 | fprintf(stderr, "'%s': up-to-date\n", ref->name); |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 250 | continue; |
| 251 | } |
| 252 | |
| 253 | /* This part determines what can overwrite what. |
| 254 | * The rules are: |
| 255 | * |
Junio C Hamano | ff27adf | 2005-08-24 00:40:14 -0700 | [diff] [blame] | 256 | * (0) you can always use --force or +A:B notation to |
| 257 | * selectively force individual ref pairs. |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 258 | * |
| 259 | * (1) if the old thing does not exist, it is OK. |
| 260 | * |
| 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. |
| 264 | * |
| 265 | * (3) if both new and old are commit-ish, and new is a |
| 266 | * descendant of old, it is OK. |
| 267 | */ |
| 268 | |
Junio C Hamano | ff27adf | 2005-08-24 00:40:14 -0700 | [diff] [blame] | 269 | if (!force_update && |
| 270 | !is_zero_sha1(ref->old_sha1) && |
| 271 | !ref->force) { |
Junio C Hamano | 69310a3 | 2005-12-22 12:39:39 -0800 | [diff] [blame] | 272 | if (!has_sha1_file(ref->old_sha1) || |
| 273 | !ref_newer(ref->peer_ref->new_sha1, |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 274 | ref->old_sha1)) { |
Junio C Hamano | 69310a3 | 2005-12-22 12:39:39 -0800 | [diff] [blame] | 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. |
| 281 | */ |
| 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, |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 287 | ref->peer_ref->name); |
Petr Baudis | ed24928 | 2005-12-14 01:45:40 +0100 | [diff] [blame] | 288 | ret = -2; |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 289 | continue; |
| 290 | } |
| 291 | } |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 292 | memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20); |
| 293 | if (is_zero_sha1(ref->new_sha1)) { |
| 294 | error("cannot happen anymore"); |
Petr Baudis | ed24928 | 2005-12-14 01:45:40 +0100 | [diff] [blame] | 295 | ret = -3; |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 296 | continue; |
| 297 | } |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 298 | new_refs++; |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 299 | strcpy(old_hex, sha1_to_hex(ref->old_sha1)); |
| 300 | new_hex = sha1_to_hex(ref->new_sha1); |
Junio C Hamano | cfee10a | 2005-12-25 23:18:37 -0800 | [diff] [blame] | 301 | |
| 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; |
| 308 | } |
| 309 | else |
| 310 | packet_write(out, "%s %s %s", |
| 311 | old_hex, new_hex, ref->name); |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 316 | } |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 317 | |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 318 | packet_flush(out); |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 319 | if (new_refs) |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 320 | pack_objects(out, remote_refs); |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 321 | close(out); |
Junio C Hamano | cfee10a | 2005-12-25 23:18:37 -0800 | [diff] [blame] | 322 | |
| 323 | if (expect_status_report) { |
| 324 | if (receive_status(in)) |
| 325 | ret = -4; |
| 326 | } |
| 327 | |
| 328 | if (!new_refs && ret == 0) |
| 329 | fprintf(stderr, "Everything up-to-date\n"); |
Petr Baudis | ed24928 | 2005-12-14 01:45:40 +0100 | [diff] [blame] | 330 | return ret; |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 333 | |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 334 | int main(int argc, char **argv) |
| 335 | { |
| 336 | int i, nr_heads = 0; |
| 337 | char *dest = NULL; |
| 338 | char **heads = NULL; |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 339 | int fd[2], ret; |
| 340 | pid_t pid; |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 341 | |
Junio C Hamano | 5a32771 | 2005-11-26 00:47:59 -0800 | [diff] [blame] | 342 | setup_git_directory(); |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 343 | argv++; |
Linus Torvalds | d089391 | 2005-07-16 13:26:33 -0700 | [diff] [blame] | 344 | for (i = 1; i < argc; i++, argv++) { |
| 345 | char *arg = *argv; |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 346 | |
| 347 | if (*arg == '-') { |
| 348 | if (!strncmp(arg, "--exec=", 7)) { |
| 349 | exec = arg + 7; |
| 350 | continue; |
| 351 | } |
Linus Torvalds | d089391 | 2005-07-16 13:26:33 -0700 | [diff] [blame] | 352 | if (!strcmp(arg, "--all")) { |
| 353 | send_all = 1; |
| 354 | continue; |
| 355 | } |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 356 | if (!strcmp(arg, "--force")) { |
| 357 | force_update = 1; |
| 358 | continue; |
| 359 | } |
Linus Torvalds | 41f93a2 | 2005-12-20 18:13:02 -0800 | [diff] [blame] | 360 | if (!strcmp(arg, "--verbose")) { |
| 361 | verbose = 1; |
| 362 | continue; |
| 363 | } |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 364 | usage(send_pack_usage); |
| 365 | } |
Linus Torvalds | d089391 | 2005-07-16 13:26:33 -0700 | [diff] [blame] | 366 | if (!dest) { |
| 367 | dest = arg; |
| 368 | continue; |
| 369 | } |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 370 | heads = argv; |
Linus Torvalds | d089391 | 2005-07-16 13:26:33 -0700 | [diff] [blame] | 371 | nr_heads = argc - i; |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 372 | break; |
| 373 | } |
| 374 | if (!dest) |
| 375 | usage(send_pack_usage); |
Junio C Hamano | 0bc3cdf | 2005-08-02 12:20:27 -0700 | [diff] [blame] | 376 | if (heads && send_all) |
| 377 | usage(send_pack_usage); |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 378 | pid = git_connect(fd, dest, exec); |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 379 | if (pid < 0) |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 380 | return 1; |
Linus Torvalds | d0efc8a | 2005-06-30 12:28:24 -0700 | [diff] [blame] | 381 | ret = send_pack(fd[0], fd[1], nr_heads, heads); |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 382 | close(fd[0]); |
| 383 | close(fd[1]); |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 384 | finish_connect(pid); |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 385 | return ret; |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 386 | } |