Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 1 | #include "cache.h" |
Linus Torvalds | 2a9c3fe | 2005-07-19 11:03:47 | [diff] [blame] | 2 | #include "commit.h" |
Junio C Hamano | 37fde87 | 2005-08-05 07:47:56 | [diff] [blame] | 3 | #include "tag.h" |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 4 | #include "refs.h" |
Linus Torvalds | f3a3214 | 2005-06-30 03:50:15 | [diff] [blame] | 5 | #include "pkt-line.h" |
Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 6 | |
Junio C Hamano | 2a24501 | 2005-07-14 07:10:05 | [diff] [blame] | 7 | static const char send_pack_usage[] = |
Junio C Hamano | 0bc3cdf | 2005-08-02 19:20:27 | [diff] [blame] | 8 | "git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n" |
| 9 | " --all and explicit <head> specification are mutually exclusive."; |
Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 10 | static const char *exec = "git-receive-pack"; |
Linus Torvalds | d089391 | 2005-07-16 20:26:33 | [diff] [blame] | 11 | static int send_all = 0; |
Linus Torvalds | 2a9c3fe | 2005-07-19 11:03:47 | [diff] [blame] | 12 | static int force_update = 0; |
Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 13 | |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 14 | static int is_zero_sha1(const unsigned char *sha1) |
| 15 | { |
| 16 | int i; |
| 17 | |
| 18 | for (i = 0; i < 20; i++) { |
| 19 | if (*sha1++) |
| 20 | return 0; |
| 21 | } |
| 22 | return 1; |
| 23 | } |
| 24 | |
Linus Torvalds | 94fdb7a | 2005-06-30 17:17:39 | [diff] [blame] | 25 | static void exec_pack_objects(void) |
| 26 | { |
| 27 | static char *args[] = { |
| 28 | "git-pack-objects", |
| 29 | "--stdout", |
| 30 | NULL |
| 31 | }; |
| 32 | execvp("git-pack-objects", args); |
| 33 | die("git-pack-objects exec failed (%s)", strerror(errno)); |
| 34 | } |
| 35 | |
| 36 | static void exec_rev_list(struct ref *refs) |
| 37 | { |
| 38 | static char *args[1000]; |
| 39 | int i = 0; |
| 40 | |
| 41 | args[i++] = "git-rev-list"; /* 0 */ |
| 42 | args[i++] = "--objects"; /* 1 */ |
| 43 | while (refs) { |
| 44 | char *buf = malloc(100); |
| 45 | if (i > 900) |
| 46 | die("git-rev-list environment overflow"); |
Junio C Hamano | 40b64d4 | 2005-08-03 19:41:12 | [diff] [blame] | 47 | if (!is_zero_sha1(refs->old_sha1) && |
| 48 | has_sha1_file(refs->old_sha1)) { |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 49 | args[i++] = buf; |
| 50 | snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1)); |
| 51 | buf += 50; |
| 52 | } |
| 53 | if (!is_zero_sha1(refs->new_sha1)) { |
| 54 | args[i++] = buf; |
| 55 | snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1)); |
| 56 | } |
Linus Torvalds | 94fdb7a | 2005-06-30 17:17:39 | [diff] [blame] | 57 | refs = refs->next; |
| 58 | } |
| 59 | args[i] = NULL; |
| 60 | execvp("git-rev-list", args); |
| 61 | die("git-rev-list exec failed (%s)", strerror(errno)); |
| 62 | } |
| 63 | |
| 64 | static void rev_list(int fd, struct ref *refs) |
| 65 | { |
| 66 | int pipe_fd[2]; |
| 67 | pid_t pack_objects_pid; |
| 68 | |
| 69 | if (pipe(pipe_fd) < 0) |
| 70 | die("rev-list setup: pipe failed"); |
| 71 | pack_objects_pid = fork(); |
| 72 | if (!pack_objects_pid) { |
| 73 | dup2(pipe_fd[0], 0); |
| 74 | dup2(fd, 1); |
| 75 | close(pipe_fd[0]); |
| 76 | close(pipe_fd[1]); |
| 77 | close(fd); |
| 78 | exec_pack_objects(); |
| 79 | die("pack-objects setup failed"); |
| 80 | } |
| 81 | if (pack_objects_pid < 0) |
| 82 | die("pack-objects fork failed"); |
| 83 | dup2(pipe_fd[1], 1); |
| 84 | close(pipe_fd[0]); |
| 85 | close(pipe_fd[1]); |
| 86 | close(fd); |
| 87 | exec_rev_list(refs); |
| 88 | } |
| 89 | |
| 90 | static int pack_objects(int fd, struct ref *refs) |
| 91 | { |
| 92 | pid_t rev_list_pid; |
| 93 | |
| 94 | rev_list_pid = fork(); |
| 95 | if (!rev_list_pid) { |
| 96 | rev_list(fd, refs); |
| 97 | die("rev-list setup failed"); |
| 98 | } |
| 99 | if (rev_list_pid < 0) |
| 100 | die("rev-list fork failed"); |
| 101 | /* |
| 102 | * We don't wait for the rev-list pipeline in the parent: |
| 103 | * we end up waiting for the other end instead |
| 104 | */ |
Linus Torvalds | 7ec4e60 | 2005-07-03 17:00:01 | [diff] [blame] | 105 | return 0; |
Linus Torvalds | 94fdb7a | 2005-06-30 17:17:39 | [diff] [blame] | 106 | } |
Linus Torvalds | e4b5c7f | 2005-06-30 05:31:41 | [diff] [blame] | 107 | |
Junio C Hamano | 51b0fca | 2005-08-06 06:05:33 | [diff] [blame] | 108 | static void unmark_and_free(struct commit_list *list, unsigned int mark) |
| 109 | { |
| 110 | while (list) { |
| 111 | struct commit_list *temp = list; |
| 112 | temp->item->object.flags &= ~mark; |
| 113 | list = temp->next; |
| 114 | free(temp); |
| 115 | } |
| 116 | } |
| 117 | |
Junio C Hamano | 37fde87 | 2005-08-05 07:47:56 | [diff] [blame] | 118 | static int ref_newer(const unsigned char *new_sha1, |
| 119 | const unsigned char *old_sha1) |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 120 | { |
Junio C Hamano | 37fde87 | 2005-08-05 07:47:56 | [diff] [blame] | 121 | struct object *o; |
| 122 | struct commit *old, *new; |
Junio C Hamano | 51b0fca | 2005-08-06 06:05:33 | [diff] [blame] | 123 | struct commit_list *list, *used; |
| 124 | int found = 0; |
Linus Torvalds | 2a9c3fe | 2005-07-19 11:03:47 | [diff] [blame] | 125 | |
Junio C Hamano | 37fde87 | 2005-08-05 07:47:56 | [diff] [blame] | 126 | /* Both new and old must be commit-ish and new is descendant of |
| 127 | * old. Otherwise we require --force. |
| 128 | */ |
| 129 | o = deref_tag(parse_object(old_sha1)); |
| 130 | if (!o || o->type != commit_type) |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 131 | return 0; |
Junio C Hamano | 37fde87 | 2005-08-05 07:47:56 | [diff] [blame] | 132 | old = (struct commit *) o; |
| 133 | |
| 134 | o = deref_tag(parse_object(new_sha1)); |
| 135 | if (!o || o->type != commit_type) |
Linus Torvalds | 2a9c3fe | 2005-07-19 11:03:47 | [diff] [blame] | 136 | return 0; |
Junio C Hamano | 37fde87 | 2005-08-05 07:47:56 | [diff] [blame] | 137 | new = (struct commit *) o; |
| 138 | |
Linus Torvalds | 2a9c3fe | 2005-07-19 11:03:47 | [diff] [blame] | 139 | if (parse_commit(new) < 0) |
| 140 | return 0; |
Junio C Hamano | 51b0fca | 2005-08-06 06:05:33 | [diff] [blame] | 141 | |
| 142 | used = list = NULL; |
Linus Torvalds | 2a9c3fe | 2005-07-19 11:03:47 | [diff] [blame] | 143 | commit_list_insert(new, &list); |
Linus Torvalds | bdf2514 | 2005-07-27 03:04:22 | [diff] [blame] | 144 | while (list) { |
| 145 | new = pop_most_recent_commit(&list, 1); |
Junio C Hamano | 51b0fca | 2005-08-06 06:05:33 | [diff] [blame] | 146 | commit_list_insert(new, &used); |
| 147 | if (new == old) { |
| 148 | found = 1; |
| 149 | break; |
| 150 | } |
Linus Torvalds | 2a9c3fe | 2005-07-19 11:03:47 | [diff] [blame] | 151 | } |
Junio C Hamano | 51b0fca | 2005-08-06 06:05:33 | [diff] [blame] | 152 | unmark_and_free(list, 1); |
| 153 | unmark_and_free(used, 1); |
| 154 | return found; |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 155 | } |
| 156 | |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 157 | static struct ref *local_refs, **local_tail; |
| 158 | static struct ref *remote_refs, **remote_tail; |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 159 | |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 160 | static int one_local_ref(const char *refname, const unsigned char *sha1) |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 161 | { |
| 162 | struct ref *ref; |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 163 | int len = strlen(refname) + 1; |
| 164 | ref = xcalloc(1, sizeof(*ref) + len); |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 165 | memcpy(ref->new_sha1, sha1, 20); |
| 166 | memcpy(ref->name, refname, len); |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 167 | *local_tail = ref; |
| 168 | local_tail = &ref->next; |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 172 | static void get_local_heads(void) |
Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 173 | { |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 174 | local_tail = &local_refs; |
| 175 | for_each_ref(one_local_ref); |
| 176 | } |
| 177 | |
| 178 | static int send_pack(int in, int out, int nr_refspec, char **refspec) |
| 179 | { |
Linus Torvalds | 7f8e982 | 2005-06-30 05:50:48 | [diff] [blame] | 180 | struct ref *ref; |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 181 | int new_refs; |
Linus Torvalds | 7f8e982 | 2005-06-30 05:50:48 | [diff] [blame] | 182 | |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 183 | /* No funny business with the matcher */ |
| 184 | remote_tail = get_remote_heads(in, &remote_refs, 0, NULL); |
| 185 | get_local_heads(); |
Linus Torvalds | 7f8e982 | 2005-06-30 05:50:48 | [diff] [blame] | 186 | |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 187 | /* match them up */ |
| 188 | if (!remote_tail) |
| 189 | remote_tail = &remote_refs; |
| 190 | if (match_refs(local_refs, remote_refs, &remote_tail, |
| 191 | nr_refspec, refspec, send_all)) |
| 192 | return -1; |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 193 | /* |
| 194 | * Finally, tell the other end! |
| 195 | */ |
| 196 | new_refs = 0; |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 197 | for (ref = remote_refs; ref; ref = ref->next) { |
Linus Torvalds | 7f8e982 | 2005-06-30 05:50:48 | [diff] [blame] | 198 | char old_hex[60], *new_hex; |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 199 | if (!ref->peer_ref) |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 200 | continue; |
Junio C Hamano | 37fde87 | 2005-08-05 07:47:56 | [diff] [blame] | 201 | if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) { |
| 202 | fprintf(stderr, "'%s': up-to-date\n", ref->name); |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | /* This part determines what can overwrite what. |
| 207 | * The rules are: |
| 208 | * |
Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 209 | * (0) you can always use --force or +A:B notation to |
| 210 | * selectively force individual ref pairs. |
Junio C Hamano | 37fde87 | 2005-08-05 07:47:56 | [diff] [blame] | 211 | * |
| 212 | * (1) if the old thing does not exist, it is OK. |
| 213 | * |
| 214 | * (2) if you do not have the old thing, you are not allowed |
| 215 | * to overwrite it; you would not know what you are losing |
| 216 | * otherwise. |
| 217 | * |
| 218 | * (3) if both new and old are commit-ish, and new is a |
| 219 | * descendant of old, it is OK. |
| 220 | */ |
| 221 | |
Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 222 | if (!force_update && |
| 223 | !is_zero_sha1(ref->old_sha1) && |
| 224 | !ref->force) { |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 225 | if (!has_sha1_file(ref->old_sha1)) { |
| 226 | error("remote '%s' object %s does not " |
| 227 | "exist on local", |
| 228 | ref->name, sha1_to_hex(ref->old_sha1)); |
| 229 | continue; |
| 230 | } |
Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 231 | |
Junio C Hamano | 37fde87 | 2005-08-05 07:47:56 | [diff] [blame] | 232 | /* We assume that local is fsck-clean. Otherwise |
Junio C Hamano | ff27adf | 2005-08-24 07:40:14 | [diff] [blame] | 233 | * you _could_ have an old tag which points at |
| 234 | * something you do not have, which may or may not |
Junio C Hamano | 37fde87 | 2005-08-05 07:47:56 | [diff] [blame] | 235 | * be a commit. |
| 236 | */ |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 237 | if (!ref_newer(ref->peer_ref->new_sha1, |
| 238 | ref->old_sha1)) { |
| 239 | error("remote ref '%s' is not a strict " |
| 240 | "subset of local ref '%s'.", ref->name, |
| 241 | ref->peer_ref->name); |
| 242 | continue; |
| 243 | } |
| 244 | } |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 245 | memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20); |
| 246 | if (is_zero_sha1(ref->new_sha1)) { |
| 247 | error("cannot happen anymore"); |
| 248 | continue; |
| 249 | } |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 250 | new_refs++; |
Linus Torvalds | 7f8e982 | 2005-06-30 05:50:48 | [diff] [blame] | 251 | strcpy(old_hex, sha1_to_hex(ref->old_sha1)); |
| 252 | new_hex = sha1_to_hex(ref->new_sha1); |
| 253 | packet_write(out, "%s %s %s", old_hex, new_hex, ref->name); |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 254 | fprintf(stderr, "updating '%s'", ref->name); |
| 255 | if (strcmp(ref->name, ref->peer_ref->name)) |
| 256 | fprintf(stderr, " using '%s'", ref->peer_ref->name); |
| 257 | fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex); |
Linus Torvalds | 7f8e982 | 2005-06-30 05:50:48 | [diff] [blame] | 258 | } |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 259 | |
Linus Torvalds | f3a3214 | 2005-06-30 03:50:15 | [diff] [blame] | 260 | packet_flush(out); |
Linus Torvalds | 584c6cc | 2005-07-08 20:58:40 | [diff] [blame] | 261 | if (new_refs) |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 262 | pack_objects(out, remote_refs); |
Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 263 | close(out); |
| 264 | return 0; |
| 265 | } |
| 266 | |
Junio C Hamano | f88395a | 2005-08-03 23:35:29 | [diff] [blame] | 267 | |
Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 268 | int main(int argc, char **argv) |
| 269 | { |
| 270 | int i, nr_heads = 0; |
| 271 | char *dest = NULL; |
| 272 | char **heads = NULL; |
Linus Torvalds | 7f8e982 | 2005-06-30 05:50:48 | [diff] [blame] | 273 | int fd[2], ret; |
| 274 | pid_t pid; |
Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 275 | |
| 276 | argv++; |
Linus Torvalds | d089391 | 2005-07-16 20:26:33 | [diff] [blame] | 277 | for (i = 1; i < argc; i++, argv++) { |
| 278 | char *arg = *argv; |
Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 279 | |
| 280 | if (*arg == '-') { |
| 281 | if (!strncmp(arg, "--exec=", 7)) { |
| 282 | exec = arg + 7; |
| 283 | continue; |
| 284 | } |
Linus Torvalds | d089391 | 2005-07-16 20:26:33 | [diff] [blame] | 285 | if (!strcmp(arg, "--all")) { |
| 286 | send_all = 1; |
| 287 | continue; |
| 288 | } |
Linus Torvalds | 2a9c3fe | 2005-07-19 11:03:47 | [diff] [blame] | 289 | if (!strcmp(arg, "--force")) { |
| 290 | force_update = 1; |
| 291 | continue; |
| 292 | } |
Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 293 | usage(send_pack_usage); |
| 294 | } |
Linus Torvalds | d089391 | 2005-07-16 20:26:33 | [diff] [blame] | 295 | if (!dest) { |
| 296 | dest = arg; |
| 297 | continue; |
| 298 | } |
Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 299 | heads = argv; |
Linus Torvalds | d089391 | 2005-07-16 20:26:33 | [diff] [blame] | 300 | nr_heads = argc - i; |
Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 301 | break; |
| 302 | } |
| 303 | if (!dest) |
| 304 | usage(send_pack_usage); |
Junio C Hamano | 0bc3cdf | 2005-08-02 19:20:27 | [diff] [blame] | 305 | if (heads && send_all) |
| 306 | usage(send_pack_usage); |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 307 | pid = git_connect(fd, dest, exec); |
Linus Torvalds | 7f8e982 | 2005-06-30 05:50:48 | [diff] [blame] | 308 | if (pid < 0) |
Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 309 | return 1; |
Linus Torvalds | d0efc8a | 2005-06-30 19:28:24 | [diff] [blame] | 310 | ret = send_pack(fd[0], fd[1], nr_heads, heads); |
Linus Torvalds | 7f8e982 | 2005-06-30 05:50:48 | [diff] [blame] | 311 | close(fd[0]); |
| 312 | close(fd[1]); |
Linus Torvalds | f719259 | 2005-07-04 18:57:58 | [diff] [blame] | 313 | finish_connect(pid); |
Linus Torvalds | 7f8e982 | 2005-06-30 05:50:48 | [diff] [blame] | 314 | return ret; |
Linus Torvalds | 6122147 | 2005-06-30 02:09:05 | [diff] [blame] | 315 | } |