29cf73681f3caf0b91b0d7d11cafbfcd6c4bf8d0
   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;
  13static int send_all;
  14static int force_update;
  15static int use_thin_pack;
  16
  17static void exec_pack_objects(void)
  18{
  19        static const char *args[] = {
  20                "pack-objects",
  21                "--all-progress",
  22                "--stdout",
  23                NULL
  24        };
  25        execv_git_cmd(args);
  26        die("git-pack-objects exec failed (%s)", strerror(errno));
  27}
  28
  29static void exec_rev_list(struct ref *refs)
  30{
  31        static const char *args[4];
  32        int i = 0;
  33
  34        args[i++] = "rev-list"; /* 0 */
  35        if (use_thin_pack)      /* 1 */
  36                args[i++] = "--objects-edge";
  37        else
  38                args[i++] = "--objects";
  39
  40        args[i++] = "--stdin";
  41
  42        args[i] = NULL;
  43        execv_git_cmd(args);
  44        die("git-rev-list exec failed (%s)", strerror(errno));
  45}
  46
  47/*
  48 * Run "rev-list --stdin | pack-objects" pipe.
  49 */
  50static void rev_list(struct ref *refs)
  51{
  52        int pipe_fd[2];
  53        pid_t pack_objects_pid;
  54
  55        if (pipe(pipe_fd) < 0)
  56                die("rev-list setup: pipe failed");
  57        pack_objects_pid = fork();
  58        if (!pack_objects_pid) {
  59                /* The child becomes pack-objects; reads from pipe
  60                 * and writes to the original fd
  61                 */
  62                dup2(pipe_fd[0], 0);
  63                close(pipe_fd[0]);
  64                close(pipe_fd[1]);
  65                exec_pack_objects();
  66                die("pack-objects setup failed");
  67        }
  68        if (pack_objects_pid < 0)
  69                die("pack-objects fork failed");
  70
  71        /* We become rev-list --stdin; output goes to pipe. */
  72        dup2(pipe_fd[1], 1);
  73        close(pipe_fd[0]);
  74        close(pipe_fd[1]);
  75        exec_rev_list(refs);
  76}
  77
  78/*
  79 * Create "rev-list --stdin | pack-objects" pipe and feed
  80 * the refs into the pipeline.
  81 */
  82static void rev_list_generate(int fd, struct ref *refs)
  83{
  84        int pipe_fd[2];
  85        pid_t rev_list_generate_pid;
  86
  87        if (pipe(pipe_fd) < 0)
  88                die("rev-list-generate setup: pipe failed");
  89        rev_list_generate_pid = fork();
  90        if (!rev_list_generate_pid) {
  91                /* The child becomes the "rev-list | pack-objects"
  92                 * pipeline.  It takes input from us, and its output
  93                 * goes to fd.
  94                 */
  95                dup2(pipe_fd[0], 0);
  96                dup2(fd, 1);
  97                close(pipe_fd[0]);
  98                close(pipe_fd[1]);
  99                close(fd);
 100                rev_list(refs);
 101                die("rev-list setup failed");
 102        }
 103        if (rev_list_generate_pid < 0)
 104                die("rev-list-generate fork failed");
 105
 106        /* We feed the rev parameters to them.  We do not write into
 107         * fd nor read from the pipe.
 108         */
 109        close(pipe_fd[0]);
 110        close(fd);
 111        while (refs) {
 112                char buf[42];
 113
 114                if (!is_null_sha1(refs->old_sha1) &&
 115                    has_sha1_file(refs->old_sha1)) {
 116                        memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
 117                        buf[0] = '^';
 118                        buf[41] = '\n';
 119                        write(pipe_fd[1], buf, 42);
 120                }
 121                if (!is_null_sha1(refs->new_sha1)) {
 122                        memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
 123                        buf[40] = '\n';
 124                        write(pipe_fd[1], buf, 41);
 125                }
 126                refs = refs->next;
 127        }
 128        close(pipe_fd[1]);
 129        // waitpid(rev_list_generate_pid);
 130        exit(0);
 131}
 132
 133/*
 134 * Make a pack stream and spit it out into file descriptor fd
 135 */
 136static void pack_objects(int fd, struct ref *refs)
 137{
 138        pid_t rev_list_pid;
 139
 140        rev_list_pid = fork();
 141        if (!rev_list_pid) {
 142                rev_list_generate(fd, refs);
 143                die("rev-list setup failed");
 144        }
 145        if (rev_list_pid < 0)
 146                die("rev-list fork failed");
 147        /*
 148         * We don't wait for the rev-list pipeline in the parent:
 149         * we end up waiting for the other end instead
 150         */
 151}
 152
 153static void unmark_and_free(struct commit_list *list, unsigned int mark)
 154{
 155        while (list) {
 156                struct commit_list *temp = list;
 157                temp->item->object.flags &= ~mark;
 158                list = temp->next;
 159                free(temp);
 160        }
 161}
 162
 163static int ref_newer(const unsigned char *new_sha1,
 164                     const unsigned char *old_sha1)
 165{
 166        struct object *o;
 167        struct commit *old, *new;
 168        struct commit_list *list, *used;
 169        int found = 0;
 170
 171        /* Both new and old must be commit-ish and new is descendant of
 172         * old.  Otherwise we require --force.
 173         */
 174        o = deref_tag(parse_object(old_sha1), NULL, 0);
 175        if (!o || o->type != OBJ_COMMIT)
 176                return 0;
 177        old = (struct commit *) o;
 178
 179        o = deref_tag(parse_object(new_sha1), NULL, 0);
 180        if (!o || o->type != OBJ_COMMIT)
 181                return 0;
 182        new = (struct commit *) o;
 183
 184        if (parse_commit(new) < 0)
 185                return 0;
 186
 187        used = list = NULL;
 188        commit_list_insert(new, &list);
 189        while (list) {
 190                new = pop_most_recent_commit(&list, 1);
 191                commit_list_insert(new, &used);
 192                if (new == old) {
 193                        found = 1;
 194                        break;
 195                }
 196        }
 197        unmark_and_free(list, 1);
 198        unmark_and_free(used, 1);
 199        return found;
 200}
 201
 202static struct ref *local_refs, **local_tail;
 203static struct ref *remote_refs, **remote_tail;
 204
 205static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
 206{
 207        struct ref *ref;
 208        int len = strlen(refname) + 1;
 209        ref = xcalloc(1, sizeof(*ref) + len);
 210        hashcpy(ref->new_sha1, sha1);
 211        memcpy(ref->name, refname, len);
 212        *local_tail = ref;
 213        local_tail = &ref->next;
 214        return 0;
 215}
 216
 217static void get_local_heads(void)
 218{
 219        local_tail = &local_refs;
 220        for_each_ref(one_local_ref, NULL);
 221}
 222
 223static int receive_status(int in)
 224{
 225        char line[1000];
 226        int ret = 0;
 227        int len = packet_read_line(in, line, sizeof(line));
 228        if (len < 10 || memcmp(line, "unpack ", 7)) {
 229                fprintf(stderr, "did not receive status back\n");
 230                return -1;
 231        }
 232        if (memcmp(line, "unpack ok\n", 10)) {
 233                fputs(line, stderr);
 234                ret = -1;
 235        }
 236        while (1) {
 237                len = packet_read_line(in, line, sizeof(line));
 238                if (!len)
 239                        break;
 240                if (len < 3 ||
 241                    (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
 242                        fprintf(stderr, "protocol error: %s\n", line);
 243                        ret = -1;
 244                        break;
 245                }
 246                if (!memcmp(line, "ok", 2))
 247                        continue;
 248                fputs(line, stderr);
 249                ret = -1;
 250        }
 251        return ret;
 252}
 253
 254static int send_pack(int in, int out, int nr_refspec, char **refspec)
 255{
 256        struct ref *ref;
 257        int new_refs;
 258        int ret = 0;
 259        int ask_for_status_report = 0;
 260        int allow_deleting_refs = 0;
 261        int expect_status_report = 0;
 262
 263        /* No funny business with the matcher */
 264        remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
 265        get_local_heads();
 266
 267        /* Does the other end support the reporting? */
 268        if (server_supports("report-status"))
 269                ask_for_status_report = 1;
 270        if (server_supports("delete-refs"))
 271                allow_deleting_refs = 1;
 272
 273        /* match them up */
 274        if (!remote_tail)
 275                remote_tail = &remote_refs;
 276        if (match_refs(local_refs, remote_refs, &remote_tail,
 277                       nr_refspec, refspec, send_all))
 278                return -1;
 279
 280        if (!remote_refs) {
 281                fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
 282                return 0;
 283        }
 284
 285        /*
 286         * Finally, tell the other end!
 287         */
 288        new_refs = 0;
 289        for (ref = remote_refs; ref; ref = ref->next) {
 290                char old_hex[60], *new_hex;
 291                int delete_ref;
 292
 293                if (!ref->peer_ref)
 294                        continue;
 295
 296                delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
 297                if (delete_ref && !allow_deleting_refs) {
 298                        error("remote does not support deleting refs");
 299                        ret = -2;
 300                        continue;
 301                }
 302                if (!delete_ref &&
 303                    !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
 304                        if (verbose)
 305                                fprintf(stderr, "'%s': up-to-date\n", ref->name);
 306                        continue;
 307                }
 308
 309                /* This part determines what can overwrite what.
 310                 * The rules are:
 311                 *
 312                 * (0) you can always use --force or +A:B notation to
 313                 *     selectively force individual ref pairs.
 314                 *
 315                 * (1) if the old thing does not exist, it is OK.
 316                 *
 317                 * (2) if you do not have the old thing, you are not allowed
 318                 *     to overwrite it; you would not know what you are losing
 319                 *     otherwise.
 320                 *
 321                 * (3) if both new and old are commit-ish, and new is a
 322                 *     descendant of old, it is OK.
 323                 *
 324                 * (4) regardless of all of the above, removing :B is
 325                 *     always allowed.
 326                 */
 327
 328                if (!force_update &&
 329                    !delete_ref &&
 330                    !is_null_sha1(ref->old_sha1) &&
 331                    !ref->force) {
 332                        if (!has_sha1_file(ref->old_sha1) ||
 333                            !ref_newer(ref->peer_ref->new_sha1,
 334                                       ref->old_sha1)) {
 335                                /* We do not have the remote ref, or
 336                                 * we know that the remote ref is not
 337                                 * an ancestor of what we are trying to
 338                                 * push.  Either way this can be losing
 339                                 * commits at the remote end and likely
 340                                 * we were not up to date to begin with.
 341                                 */
 342                                error("remote '%s' is not a strict "
 343                                      "subset of local ref '%s'. "
 344                                      "maybe you are not up-to-date and "
 345                                      "need to pull first?",
 346                                      ref->name,
 347                                      ref->peer_ref->name);
 348                                ret = -2;
 349                                continue;
 350                        }
 351                }
 352                hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
 353                if (!delete_ref)
 354                        new_refs++;
 355                strcpy(old_hex, sha1_to_hex(ref->old_sha1));
 356                new_hex = sha1_to_hex(ref->new_sha1);
 357
 358                if (ask_for_status_report) {
 359                        packet_write(out, "%s %s %s%c%s",
 360                                     old_hex, new_hex, ref->name, 0,
 361                                     "report-status");
 362                        ask_for_status_report = 0;
 363                        expect_status_report = 1;
 364                }
 365                else
 366                        packet_write(out, "%s %s %s",
 367                                     old_hex, new_hex, ref->name);
 368                if (delete_ref)
 369                        fprintf(stderr, "deleting '%s'\n", ref->name);
 370                else {
 371                        fprintf(stderr, "updating '%s'", ref->name);
 372                        if (strcmp(ref->name, ref->peer_ref->name))
 373                                fprintf(stderr, " using '%s'",
 374                                        ref->peer_ref->name);
 375                        fprintf(stderr, "\n  from %s\n  to   %s\n",
 376                                old_hex, new_hex);
 377                }
 378        }
 379
 380        packet_flush(out);
 381        if (new_refs)
 382                pack_objects(out, remote_refs);
 383        close(out);
 384
 385        if (expect_status_report) {
 386                if (receive_status(in))
 387                        ret = -4;
 388        }
 389
 390        if (!new_refs && ret == 0)
 391                fprintf(stderr, "Everything up-to-date\n");
 392        return ret;
 393}
 394
 395static void verify_remote_names(int nr_heads, char **heads)
 396{
 397        int i;
 398
 399        for (i = 0; i < nr_heads; i++) {
 400                const char *remote = strchr(heads[i], ':');
 401
 402                remote = remote ? (remote + 1) : heads[i];
 403                switch (check_ref_format(remote)) {
 404                case 0: /* ok */
 405                case -2: /* ok but a single level -- that is fine for
 406                          * a match pattern.
 407                          */
 408                        continue;
 409                }
 410                die("remote part of refspec is not a valid name in %s",
 411                    heads[i]);
 412        }
 413}
 414
 415int main(int argc, char **argv)
 416{
 417        int i, nr_heads = 0;
 418        char *dest = NULL;
 419        char **heads = NULL;
 420        int fd[2], ret;
 421        pid_t pid;
 422
 423        setup_git_directory();
 424        git_config(git_default_config);
 425
 426        argv++;
 427        for (i = 1; i < argc; i++, argv++) {
 428                char *arg = *argv;
 429
 430                if (*arg == '-') {
 431                        if (!strncmp(arg, "--exec=", 7)) {
 432                                exec = arg + 7;
 433                                continue;
 434                        }
 435                        if (!strcmp(arg, "--all")) {
 436                                send_all = 1;
 437                                continue;
 438                        }
 439                        if (!strcmp(arg, "--force")) {
 440                                force_update = 1;
 441                                continue;
 442                        }
 443                        if (!strcmp(arg, "--verbose")) {
 444                                verbose = 1;
 445                                continue;
 446                        }
 447                        if (!strcmp(arg, "--thin")) {
 448                                use_thin_pack = 1;
 449                                continue;
 450                        }
 451                        usage(send_pack_usage);
 452                }
 453                if (!dest) {
 454                        dest = arg;
 455                        continue;
 456                }
 457                heads = argv;
 458                nr_heads = argc - i;
 459                break;
 460        }
 461        if (!dest)
 462                usage(send_pack_usage);
 463        if (heads && send_all)
 464                usage(send_pack_usage);
 465        verify_remote_names(nr_heads, heads);
 466
 467        pid = git_connect(fd, dest, exec);
 468        if (pid < 0)
 469                return 1;
 470        ret = send_pack(fd[0], fd[1], nr_heads, heads);
 471        close(fd[0]);
 472        close(fd[1]);
 473        ret |= finish_connect(pid);
 474        return !!ret;
 475}