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; |
Junio C Hamano | 2245be3 | 2006-02-19 15:03:49 -0800 | [diff] [blame] | 15 | static int use_thin_pack = 0; |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 16 | |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 17 | static int is_zero_sha1(const unsigned char *sha1) |
| 18 | { |
| 19 | int i; |
| 20 | |
| 21 | for (i = 0; i < 20; i++) { |
| 22 | if (*sha1++) |
| 23 | return 0; |
| 24 | } |
| 25 | return 1; |
| 26 | } |
| 27 | |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 28 | static void exec_pack_objects(void) |
| 29 | { |
Junio C Hamano | 9201c70 | 2006-03-05 02:47:29 -0800 | [diff] [blame] | 30 | static const char *args[] = { |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 31 | "pack-objects", |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 32 | "--stdout", |
| 33 | NULL |
| 34 | }; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 35 | execv_git_cmd(args); |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 36 | die("git-pack-objects exec failed (%s)", strerror(errno)); |
| 37 | } |
| 38 | |
| 39 | static void exec_rev_list(struct ref *refs) |
| 40 | { |
Junio C Hamano | 797656e | 2006-02-21 18:59:37 -0800 | [diff] [blame] | 41 | struct ref *ref; |
Junio C Hamano | 9201c70 | 2006-03-05 02:47:29 -0800 | [diff] [blame] | 42 | static const char *args[1000]; |
Junio C Hamano | 797656e | 2006-02-21 18:59:37 -0800 | [diff] [blame] | 43 | int i = 0, j; |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 44 | |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 45 | args[i++] = "rev-list"; /* 0 */ |
Junio C Hamano | 2245be3 | 2006-02-19 15:03:49 -0800 | [diff] [blame] | 46 | if (use_thin_pack) /* 1 */ |
| 47 | args[i++] = "--objects-edge"; |
| 48 | else |
| 49 | args[i++] = "--objects"; |
Junio C Hamano | 797656e | 2006-02-21 18:59:37 -0800 | [diff] [blame] | 50 | |
| 51 | /* First send the ones we care about most */ |
| 52 | for (ref = refs; ref; ref = ref->next) { |
| 53 | if (900 < i) |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 54 | die("git-rev-list environment overflow"); |
Junio C Hamano | 797656e | 2006-02-21 18:59:37 -0800 | [diff] [blame] | 55 | if (!is_zero_sha1(ref->new_sha1)) { |
| 56 | char *buf = malloc(100); |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 57 | args[i++] = buf; |
Junio C Hamano | 797656e | 2006-02-21 18:59:37 -0800 | [diff] [blame] | 58 | snprintf(buf, 50, "%s", sha1_to_hex(ref->new_sha1)); |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 59 | buf += 50; |
Junio C Hamano | 797656e | 2006-02-21 18:59:37 -0800 | [diff] [blame] | 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)); |
| 65 | } |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 66 | } |
Junio C Hamano | 797656e | 2006-02-21 18:59:37 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /* Then a handful of the remainder |
| 70 | * NEEDSWORK: we would be better off if used the newer ones first. |
| 71 | */ |
| 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); |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 79 | args[i++] = buf; |
Junio C Hamano | 797656e | 2006-02-21 18:59:37 -0800 | [diff] [blame] | 80 | snprintf(buf, 42, "^%s", sha1_to_hex(ref->old_sha1)); |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 81 | } |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 82 | } |
| 83 | args[i] = NULL; |
Michal Ostrowski | 77cb17e | 2006-01-10 21:12:17 -0500 | [diff] [blame] | 84 | execv_git_cmd(args); |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 85 | die("git-rev-list exec failed (%s)", strerror(errno)); |
| 86 | } |
| 87 | |
| 88 | static void rev_list(int fd, struct ref *refs) |
| 89 | { |
| 90 | int pipe_fd[2]; |
| 91 | pid_t pack_objects_pid; |
| 92 | |
| 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"); |
| 104 | } |
| 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); |
| 112 | } |
| 113 | |
| 114 | static int pack_objects(int fd, struct ref *refs) |
| 115 | { |
| 116 | pid_t rev_list_pid; |
| 117 | |
| 118 | rev_list_pid = fork(); |
| 119 | if (!rev_list_pid) { |
| 120 | rev_list(fd, refs); |
| 121 | die("rev-list setup failed"); |
| 122 | } |
| 123 | if (rev_list_pid < 0) |
| 124 | die("rev-list fork failed"); |
| 125 | /* |
| 126 | * We don't wait for the rev-list pipeline in the parent: |
| 127 | * we end up waiting for the other end instead |
| 128 | */ |
Linus Torvalds | 7ec4e60 | 2005-07-03 10:00:01 -0700 | [diff] [blame] | 129 | return 0; |
Linus Torvalds | 94fdb7a | 2005-06-30 10:17:39 -0700 | [diff] [blame] | 130 | } |
Linus Torvalds | e4b5c7f | 2005-06-29 22:31:41 -0700 | [diff] [blame] | 131 | |
Junio C Hamano | 51b0fca | 2005-08-05 23:05:33 -0700 | [diff] [blame] | 132 | static void unmark_and_free(struct commit_list *list, unsigned int mark) |
| 133 | { |
| 134 | while (list) { |
| 135 | struct commit_list *temp = list; |
| 136 | temp->item->object.flags &= ~mark; |
| 137 | list = temp->next; |
| 138 | free(temp); |
| 139 | } |
| 140 | } |
| 141 | |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 142 | static int ref_newer(const unsigned char *new_sha1, |
| 143 | const unsigned char *old_sha1) |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 144 | { |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 145 | struct object *o; |
| 146 | struct commit *old, *new; |
Junio C Hamano | 51b0fca | 2005-08-05 23:05:33 -0700 | [diff] [blame] | 147 | struct commit_list *list, *used; |
| 148 | int found = 0; |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 149 | |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 150 | /* Both new and old must be commit-ish and new is descendant of |
| 151 | * old. Otherwise we require --force. |
| 152 | */ |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 153 | o = deref_tag(parse_object(old_sha1), NULL, 0); |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 154 | if (!o || o->type != commit_type) |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 155 | return 0; |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 156 | old = (struct commit *) o; |
| 157 | |
Junio C Hamano | 9534f40 | 2005-11-02 15:19:13 -0800 | [diff] [blame] | 158 | o = deref_tag(parse_object(new_sha1), NULL, 0); |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 159 | if (!o || o->type != commit_type) |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 160 | return 0; |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 161 | new = (struct commit *) o; |
| 162 | |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 163 | if (parse_commit(new) < 0) |
| 164 | return 0; |
Junio C Hamano | 51b0fca | 2005-08-05 23:05:33 -0700 | [diff] [blame] | 165 | |
| 166 | used = list = NULL; |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 167 | commit_list_insert(new, &list); |
Linus Torvalds | bdf2514 | 2005-07-26 20:04:22 -0700 | [diff] [blame] | 168 | while (list) { |
| 169 | new = pop_most_recent_commit(&list, 1); |
Junio C Hamano | 51b0fca | 2005-08-05 23:05:33 -0700 | [diff] [blame] | 170 | commit_list_insert(new, &used); |
| 171 | if (new == old) { |
| 172 | found = 1; |
| 173 | break; |
| 174 | } |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 175 | } |
Junio C Hamano | 51b0fca | 2005-08-05 23:05:33 -0700 | [diff] [blame] | 176 | unmark_and_free(list, 1); |
| 177 | unmark_and_free(used, 1); |
| 178 | return found; |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 181 | static struct ref *local_refs, **local_tail; |
| 182 | static struct ref *remote_refs, **remote_tail; |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 183 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 184 | static int one_local_ref(const char *refname, const unsigned char *sha1) |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 185 | { |
| 186 | struct ref *ref; |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 187 | int len = strlen(refname) + 1; |
| 188 | ref = xcalloc(1, sizeof(*ref) + len); |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 189 | memcpy(ref->new_sha1, sha1, 20); |
| 190 | memcpy(ref->name, refname, len); |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 191 | *local_tail = ref; |
| 192 | local_tail = &ref->next; |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 193 | return 0; |
| 194 | } |
| 195 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 196 | static void get_local_heads(void) |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 197 | { |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 198 | local_tail = &local_refs; |
| 199 | for_each_ref(one_local_ref); |
| 200 | } |
| 201 | |
Junio C Hamano | cfee10a | 2005-12-25 23:18:37 -0800 | [diff] [blame] | 202 | static int receive_status(int in) |
| 203 | { |
| 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; |
| 210 | } |
| 211 | if (memcmp(line, "unpack ok\n", 10)) { |
| 212 | fputs(line, stderr); |
| 213 | ret = -1; |
| 214 | } |
| 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; |
| 224 | } |
| 225 | if (!memcmp(line, "ok", 2)) |
| 226 | continue; |
| 227 | fputs(line, stderr); |
| 228 | ret = -1; |
| 229 | } |
| 230 | return ret; |
| 231 | } |
| 232 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 233 | static int send_pack(int in, int out, int nr_refspec, char **refspec) |
| 234 | { |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 235 | struct ref *ref; |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 236 | int new_refs; |
Petr Baudis | ed24928 | 2005-12-14 01:45:40 +0100 | [diff] [blame] | 237 | int ret = 0; |
Junio C Hamano | cfee10a | 2005-12-25 23:18:37 -0800 | [diff] [blame] | 238 | int ask_for_status_report = 0; |
| 239 | int expect_status_report = 0; |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 240 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 241 | /* No funny business with the matcher */ |
Junio C Hamano | 1a7141f | 2005-10-13 18:57:40 -0700 | [diff] [blame] | 242 | remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, 1); |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 243 | get_local_heads(); |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 244 | |
Junio C Hamano | cfee10a | 2005-12-25 23:18:37 -0800 | [diff] [blame] | 245 | /* Does the other end support the reporting? */ |
| 246 | if (server_supports("report-status")) |
| 247 | ask_for_status_report = 1; |
| 248 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 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; |
Daniel Barkalow | 4c353e8 | 2005-12-04 11:59:37 -0500 | [diff] [blame] | 255 | |
| 256 | if (!remote_refs) { |
| 257 | fprintf(stderr, "No refs in common and none specified; doing nothing.\n"); |
| 258 | return 0; |
| 259 | } |
| 260 | |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 261 | /* |
| 262 | * Finally, tell the other end! |
| 263 | */ |
| 264 | new_refs = 0; |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 265 | for (ref = remote_refs; ref; ref = ref->next) { |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 266 | char old_hex[60], *new_hex; |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 267 | if (!ref->peer_ref) |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 268 | continue; |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 269 | if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) { |
Linus Torvalds | 41f93a2 | 2005-12-20 18:13:02 -0800 | [diff] [blame] | 270 | if (verbose) |
| 271 | fprintf(stderr, "'%s': up-to-date\n", ref->name); |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 272 | continue; |
| 273 | } |
| 274 | |
| 275 | /* This part determines what can overwrite what. |
| 276 | * The rules are: |
| 277 | * |
Junio C Hamano | ff27adf | 2005-08-24 00:40:14 -0700 | [diff] [blame] | 278 | * (0) you can always use --force or +A:B notation to |
| 279 | * selectively force individual ref pairs. |
Junio C Hamano | 37fde87 | 2005-08-05 00:47:56 -0700 | [diff] [blame] | 280 | * |
| 281 | * (1) if the old thing does not exist, it is OK. |
| 282 | * |
| 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. |
| 286 | * |
| 287 | * (3) if both new and old are commit-ish, and new is a |
| 288 | * descendant of old, it is OK. |
| 289 | */ |
| 290 | |
Junio C Hamano | ff27adf | 2005-08-24 00:40:14 -0700 | [diff] [blame] | 291 | if (!force_update && |
| 292 | !is_zero_sha1(ref->old_sha1) && |
| 293 | !ref->force) { |
Junio C Hamano | 69310a3 | 2005-12-22 12:39:39 -0800 | [diff] [blame] | 294 | if (!has_sha1_file(ref->old_sha1) || |
| 295 | !ref_newer(ref->peer_ref->new_sha1, |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 296 | ref->old_sha1)) { |
Junio C Hamano | 69310a3 | 2005-12-22 12:39:39 -0800 | [diff] [blame] | 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. |
| 303 | */ |
| 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, |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 309 | ref->peer_ref->name); |
Petr Baudis | ed24928 | 2005-12-14 01:45:40 +0100 | [diff] [blame] | 310 | ret = -2; |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 311 | continue; |
| 312 | } |
| 313 | } |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 314 | memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20); |
| 315 | if (is_zero_sha1(ref->new_sha1)) { |
| 316 | error("cannot happen anymore"); |
Petr Baudis | ed24928 | 2005-12-14 01:45:40 +0100 | [diff] [blame] | 317 | ret = -3; |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 318 | continue; |
| 319 | } |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 320 | new_refs++; |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 321 | strcpy(old_hex, sha1_to_hex(ref->old_sha1)); |
| 322 | new_hex = sha1_to_hex(ref->new_sha1); |
Junio C Hamano | cfee10a | 2005-12-25 23:18:37 -0800 | [diff] [blame] | 323 | |
| 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; |
| 330 | } |
| 331 | else |
| 332 | packet_write(out, "%s %s %s", |
| 333 | old_hex, new_hex, ref->name); |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 338 | } |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 339 | |
Linus Torvalds | f3a3214 | 2005-06-29 20:50:15 -0700 | [diff] [blame] | 340 | packet_flush(out); |
Linus Torvalds | 584c6cc | 2005-07-08 13:58:40 -0700 | [diff] [blame] | 341 | if (new_refs) |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 342 | pack_objects(out, remote_refs); |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 343 | close(out); |
Junio C Hamano | cfee10a | 2005-12-25 23:18:37 -0800 | [diff] [blame] | 344 | |
| 345 | if (expect_status_report) { |
| 346 | if (receive_status(in)) |
| 347 | ret = -4; |
| 348 | } |
| 349 | |
| 350 | if (!new_refs && ret == 0) |
| 351 | fprintf(stderr, "Everything up-to-date\n"); |
Petr Baudis | ed24928 | 2005-12-14 01:45:40 +0100 | [diff] [blame] | 352 | return ret; |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Junio C Hamano | f88395a | 2005-08-03 16:35:29 -0700 | [diff] [blame] | 355 | |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 356 | int main(int argc, char **argv) |
| 357 | { |
| 358 | int i, nr_heads = 0; |
| 359 | char *dest = NULL; |
| 360 | char **heads = NULL; |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 361 | int fd[2], ret; |
| 362 | pid_t pid; |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 363 | |
Junio C Hamano | 5a32771 | 2005-11-26 00:47:59 -0800 | [diff] [blame] | 364 | setup_git_directory(); |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 365 | argv++; |
Linus Torvalds | d089391 | 2005-07-16 13:26:33 -0700 | [diff] [blame] | 366 | for (i = 1; i < argc; i++, argv++) { |
| 367 | char *arg = *argv; |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 368 | |
| 369 | if (*arg == '-') { |
| 370 | if (!strncmp(arg, "--exec=", 7)) { |
| 371 | exec = arg + 7; |
| 372 | continue; |
| 373 | } |
Linus Torvalds | d089391 | 2005-07-16 13:26:33 -0700 | [diff] [blame] | 374 | if (!strcmp(arg, "--all")) { |
| 375 | send_all = 1; |
| 376 | continue; |
| 377 | } |
Linus Torvalds | 2a9c3fe | 2005-07-19 07:03:47 -0400 | [diff] [blame] | 378 | if (!strcmp(arg, "--force")) { |
| 379 | force_update = 1; |
| 380 | continue; |
| 381 | } |
Linus Torvalds | 41f93a2 | 2005-12-20 18:13:02 -0800 | [diff] [blame] | 382 | if (!strcmp(arg, "--verbose")) { |
| 383 | verbose = 1; |
| 384 | continue; |
| 385 | } |
Junio C Hamano | 2245be3 | 2006-02-19 15:03:49 -0800 | [diff] [blame] | 386 | if (!strcmp(arg, "--thin")) { |
| 387 | use_thin_pack = 1; |
| 388 | continue; |
| 389 | } |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 390 | usage(send_pack_usage); |
| 391 | } |
Linus Torvalds | d089391 | 2005-07-16 13:26:33 -0700 | [diff] [blame] | 392 | if (!dest) { |
| 393 | dest = arg; |
| 394 | continue; |
| 395 | } |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 396 | heads = argv; |
Linus Torvalds | d089391 | 2005-07-16 13:26:33 -0700 | [diff] [blame] | 397 | nr_heads = argc - i; |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 398 | break; |
| 399 | } |
| 400 | if (!dest) |
| 401 | usage(send_pack_usage); |
Junio C Hamano | 0bc3cdf | 2005-08-02 12:20:27 -0700 | [diff] [blame] | 402 | if (heads && send_all) |
| 403 | usage(send_pack_usage); |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 404 | pid = git_connect(fd, dest, exec); |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 405 | if (pid < 0) |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 406 | return 1; |
Linus Torvalds | d0efc8a | 2005-06-30 12:28:24 -0700 | [diff] [blame] | 407 | ret = send_pack(fd[0], fd[1], nr_heads, heads); |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 408 | close(fd[0]); |
| 409 | close(fd[1]); |
Linus Torvalds | f719259 | 2005-07-04 11:57:58 -0700 | [diff] [blame] | 410 | finish_connect(pid); |
Linus Torvalds | 7f8e982 | 2005-06-29 22:50:48 -0700 | [diff] [blame] | 411 | return ret; |
Linus Torvalds | 6122147 | 2005-06-29 19:09:05 -0700 | [diff] [blame] | 412 | } |