fetch-pack.con commit Merge branch 'fixes' (4ae22d9)
   1#include "cache.h"
   2#include "refs.h"
   3#include "pkt-line.h"
   4#include "commit.h"
   5#include "tag.h"
   6#include <time.h>
   7#include <sys/wait.h>
   8
   9static int quiet;
  10static int verbose;
  11static const char fetch_pack_usage[] =
  12"git-fetch-pack [-q] [-v] [--exec=upload-pack] [host:]directory <refs>...";
  13static const char *exec = "git-upload-pack";
  14
  15#define COMPLETE        (1U << 0)
  16
  17static int find_common(int fd[2], unsigned char *result_sha1,
  18                       struct ref *refs)
  19{
  20        int fetching;
  21        static char line[1000];
  22        static char rev_command[1024];
  23        int count = 0, flushes = 0, retval, rev_command_len;
  24        FILE *revs;
  25
  26        strcpy(rev_command, "git-rev-list $(git-rev-parse --all)");
  27        rev_command_len = strlen(rev_command);
  28        fetching = 0;
  29        for ( ; refs ; refs = refs->next) {
  30                unsigned char *remote = refs->old_sha1;
  31                struct object *o;
  32
  33                /*
  34                 * If that object is complete (i.e. it is an ancestor of a
  35                 * local ref), we tell them we have it but do not have to
  36                 * tell them about its ancestors, which they already know
  37                 * about.
  38                 *
  39                 * We use lookup_object here because we are only
  40                 * interested in the case we *know* the object is
  41                 * reachable and we have already scanned it.
  42                 */
  43                if (((o = lookup_object(remote)) != NULL) &&
  44                    (o->flags & COMPLETE)) {
  45                        struct commit_list *p;
  46                        struct commit *commit =
  47                                (struct commit *) (o = deref_tag(o));
  48                        if (!o)
  49                                goto repair;
  50                        if (o->type != commit_type)
  51                                continue;
  52                        p = commit->parents;
  53                        while (p &&
  54                               rev_command_len + 44 < sizeof(rev_command)) {
  55                                snprintf(rev_command + rev_command_len, 44,
  56                                         " ^%s",
  57                                         sha1_to_hex(p->item->object.sha1));
  58                                rev_command_len += 43;
  59                                p = p->next;
  60                        }
  61                        continue;
  62                }
  63        repair:
  64                packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
  65                fetching++;
  66        }
  67        packet_flush(fd[1]);
  68        if (!fetching)
  69                return 1;
  70
  71        revs = popen(rev_command, "r");
  72        if (!revs)
  73                die("unable to run 'git-rev-list'");
  74
  75        flushes = 1;
  76        retval = -1;
  77        while (fgets(line, sizeof(line), revs) != NULL) {
  78                unsigned char sha1[20];
  79                if (get_sha1_hex(line, sha1))
  80                        die("git-fetch-pack: expected object name, got crud");
  81                packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
  82                if (verbose)
  83                        fprintf(stderr, "have %s\n", sha1_to_hex(sha1));
  84                if (!(31 & ++count)) {
  85                        packet_flush(fd[1]);
  86                        flushes++;
  87
  88                        /*
  89                         * We keep one window "ahead" of the other side, and
  90                         * will wait for an ACK only on the next one
  91                         */
  92                        if (count == 32)
  93                                continue;
  94                        if (get_ack(fd[0], result_sha1)) {
  95                                flushes = 0;
  96                                retval = 0;
  97                                if (verbose)
  98                                        fprintf(stderr, "got ack\n");
  99                                break;
 100                        }
 101                        flushes--;
 102                }
 103        }
 104        pclose(revs);
 105        packet_write(fd[1], "done\n");
 106        if (verbose)
 107                fprintf(stderr, "done\n");
 108        while (flushes) {
 109                flushes--;
 110                if (get_ack(fd[0], result_sha1)) {
 111                        if (verbose)
 112                                fprintf(stderr, "got ack\n");
 113                        return 0;
 114                }
 115        }
 116        return retval;
 117}
 118
 119static struct commit_list *complete = NULL;
 120
 121static int mark_complete(const char *path, const unsigned char *sha1)
 122{
 123        struct object *o = parse_object(sha1);
 124
 125        while (o && o->type == tag_type) {
 126                struct tag *t = (struct tag *) o;
 127                if (!t->tagged)
 128                        break; /* broken repository */
 129                o->flags |= COMPLETE;
 130                o = parse_object(t->tagged->sha1);
 131        }
 132        if (o && o->type == commit_type) {
 133                struct commit *commit = (struct commit *)o;
 134                commit->object.flags |= COMPLETE;
 135                insert_by_date(commit, &complete);
 136        }
 137        return 0;
 138}
 139
 140static void mark_recent_complete_commits(unsigned long cutoff)
 141{
 142        while (complete && cutoff <= complete->item->date) {
 143                if (verbose)
 144                        fprintf(stderr, "Marking %s as complete\n",
 145                                sha1_to_hex(complete->item->object.sha1));
 146                pop_most_recent_commit(&complete, COMPLETE);
 147        }
 148}
 149
 150static int everything_local(struct ref *refs)
 151{
 152        struct ref *ref;
 153        int retval;
 154        unsigned long cutoff = 0;
 155
 156        track_object_refs = 0;
 157        save_commit_buffer = 0;
 158
 159        for (ref = refs; ref; ref = ref->next) {
 160                struct object *o;
 161
 162                o = parse_object(ref->old_sha1);
 163                if (!o)
 164                        continue;
 165
 166                /* We already have it -- which may mean that we were
 167                 * in sync with the other side at some time after
 168                 * that (it is OK if we guess wrong here).
 169                 */
 170                if (o->type == commit_type) {
 171                        struct commit *commit = (struct commit *)o;
 172                        if (!cutoff || cutoff < commit->date)
 173                                cutoff = commit->date;
 174                }
 175        }
 176
 177        for_each_ref(mark_complete);
 178        if (cutoff)
 179                mark_recent_complete_commits(cutoff);
 180
 181        for (retval = 1; refs ; refs = refs->next) {
 182                const unsigned char *remote = refs->old_sha1;
 183                unsigned char local[20];
 184                struct object *o;
 185
 186                o = parse_object(remote);
 187                if (!o || !(o->flags & COMPLETE)) {
 188                        retval = 0;
 189                        if (!verbose)
 190                                continue;
 191                        fprintf(stderr,
 192                                "want %s (%s)\n", sha1_to_hex(remote),
 193                                refs->name);
 194                        continue;
 195                }
 196
 197                memcpy(refs->new_sha1, local, 20);
 198                if (!verbose)
 199                        continue;
 200                fprintf(stderr,
 201                        "already have %s (%s)\n", sha1_to_hex(remote),
 202                        refs->name);
 203        }
 204        return retval;
 205}
 206
 207static int fetch_pack(int fd[2], int nr_match, char **match)
 208{
 209        struct ref *ref;
 210        unsigned char sha1[20];
 211        int status;
 212        pid_t pid;
 213
 214        get_remote_heads(fd[0], &ref, nr_match, match, 1);
 215        if (!ref) {
 216                packet_flush(fd[1]);
 217                die("no matching remote head");
 218        }
 219        if (everything_local(ref)) {
 220                packet_flush(fd[1]);
 221                goto all_done;
 222        }
 223        if (find_common(fd, sha1, ref) < 0)
 224                fprintf(stderr, "warning: no common commits\n");
 225        pid = fork();
 226        if (pid < 0)
 227                die("git-fetch-pack: unable to fork off git-unpack-objects");
 228        if (!pid) {
 229                dup2(fd[0], 0);
 230                close(fd[0]);
 231                close(fd[1]);
 232                execlp("git-unpack-objects", "git-unpack-objects",
 233                       quiet ? "-q" : NULL, NULL);
 234                die("git-unpack-objects exec failed");
 235        }
 236        close(fd[0]);
 237        close(fd[1]);
 238        while (waitpid(pid, &status, 0) < 0) {
 239                if (errno != EINTR)
 240                        die("waiting for git-unpack-objects: %s", strerror(errno));
 241        }
 242        if (WIFEXITED(status)) {
 243                int code = WEXITSTATUS(status);
 244                if (code)
 245                        die("git-unpack-objects died with error code %d", code);
 246all_done:
 247                while (ref) {
 248                        printf("%s %s\n",
 249                               sha1_to_hex(ref->old_sha1), ref->name);
 250                        ref = ref->next;
 251                }
 252                return 0;
 253        }
 254        if (WIFSIGNALED(status)) {
 255                int sig = WTERMSIG(status);
 256                die("git-unpack-objects died of signal %d", sig);
 257        }
 258        die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
 259}
 260
 261int main(int argc, char **argv)
 262{
 263        int i, ret, nr_heads;
 264        char *dest = NULL, **heads;
 265        int fd[2];
 266        pid_t pid;
 267
 268        nr_heads = 0;
 269        heads = NULL;
 270        for (i = 1; i < argc; i++) {
 271                char *arg = argv[i];
 272
 273                if (*arg == '-') {
 274                        if (!strncmp("--exec=", arg, 7)) {
 275                                exec = arg + 7;
 276                                continue;
 277                        }
 278                        if (!strcmp("-q", arg)) {
 279                                quiet = 1;
 280                                continue;
 281                        }
 282                        if (!strcmp("-v", arg)) {
 283                                verbose = 1;
 284                                continue;
 285                        }
 286                        usage(fetch_pack_usage);
 287                }
 288                dest = arg;
 289                heads = argv + i + 1;
 290                nr_heads = argc - i - 1;
 291                break;
 292        }
 293        if (!dest)
 294                usage(fetch_pack_usage);
 295        pid = git_connect(fd, dest, exec);
 296        if (pid < 0)
 297                return 1;
 298        ret = fetch_pack(fd, nr_heads, heads);
 299        close(fd[0]);
 300        close(fd[1]);
 301        finish_connect(pid);
 302        return ret;
 303}