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