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