clone-pack.con commit GIT v0.99.8d (a2239b7)
   1#include "cache.h"
   2#include "refs.h"
   3#include "pkt-line.h"
   4#include <sys/wait.h>
   5
   6static int quiet;
   7static const char clone_pack_usage[] = "git-clone-pack [-q] [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
   8static const char *exec = "git-upload-pack";
   9
  10static void clone_handshake(int fd[2], struct ref *ref)
  11{
  12        unsigned char sha1[20];
  13
  14        while (ref) {
  15                packet_write(fd[1], "want %s\n", sha1_to_hex(ref->old_sha1));
  16                ref = ref->next;
  17        }
  18        packet_flush(fd[1]);
  19
  20        /* We don't have nuttin' */
  21        packet_write(fd[1], "done\n");
  22        if (get_ack(fd[0], sha1))
  23                error("Huh! git-clone-pack got positive ack for %s", sha1_to_hex(sha1));
  24}
  25
  26static int is_master(struct ref *ref)
  27{
  28        return !strcmp(ref->name, "refs/heads/master");
  29}
  30
  31static void write_one_ref(struct ref *ref)
  32{
  33        char *path = git_path("%s", ref->name);
  34        int fd;
  35        char *hex;
  36
  37        if (!strncmp(ref->name, "refs/", 5) &&
  38            check_ref_format(ref->name + 5)) {
  39                error("refusing to create funny ref '%s' locally", ref->name);
  40                return;
  41        }
  42
  43        if (safe_create_leading_directories(path))
  44                die("unable to create leading directory for %s", ref->name);
  45        fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);
  46        if (fd < 0)
  47                die("unable to create ref %s", ref->name);
  48        hex = sha1_to_hex(ref->old_sha1);
  49        hex[40] = '\n';
  50        if (write(fd, hex, 41) != 41)
  51                die("unable to write ref %s", ref->name);
  52        close(fd);
  53}
  54
  55static void write_refs(struct ref *ref)
  56{
  57        struct ref *head = NULL, *head_ptr, *master_ref;
  58        char *head_path;
  59
  60        /* Upload-pack must report HEAD first */
  61        if (!strcmp(ref->name, "HEAD")) {
  62                head = ref;
  63                ref = ref->next;
  64        }
  65        head_ptr = NULL;
  66        master_ref = NULL;
  67        while (ref) {
  68                if (is_master(ref))
  69                        master_ref = ref;
  70                if (head &&
  71                    !memcmp(ref->old_sha1, head->old_sha1, 20) &&
  72                    !strncmp(ref->name, "refs/heads/",11) &&
  73                    (!head_ptr || ref == master_ref))
  74                        head_ptr = ref;
  75
  76                write_one_ref(ref);
  77                ref = ref->next;
  78        }
  79        if (!head) {
  80                fprintf(stderr, "No HEAD in remote.\n");
  81                return;
  82        }
  83
  84        head_path = strdup(git_path("HEAD"));
  85        if (!head_ptr) {
  86                /*
  87                 * If we had a master ref, and it wasn't HEAD, we need to undo the
  88                 * symlink, and write a standalone HEAD. Give a warning, because that's
  89                 * really really wrong.
  90                 */
  91                if (master_ref) {
  92                        error("HEAD doesn't point to any refs! Making standalone HEAD");
  93                        unlink(head_path);
  94                }
  95                write_one_ref(head);
  96                free(head_path);
  97                return;
  98        }
  99
 100        /* We reset to the master branch if it's available */
 101        if (master_ref)
 102                return;
 103
 104        fprintf(stderr, "Setting HEAD to %s\n", head_ptr->name);
 105
 106        /*
 107         * Uhhuh. Other end didn't have master. We start HEAD off with
 108         * the first branch with the same value.
 109         */
 110        if (create_symref(head_path, head_ptr->name) < 0)
 111                die("unable to link HEAD to %s", head_ptr->name);
 112        free(head_path);
 113}
 114
 115static int clone_pack(int fd[2], int nr_match, char **match)
 116{
 117        struct ref *refs;
 118        int status;
 119        pid_t pid;
 120
 121        get_remote_heads(fd[0], &refs, nr_match, match, 1);
 122        if (!refs) {
 123                packet_flush(fd[1]);
 124                die("no matching remote head");
 125        }
 126        clone_handshake(fd, refs);
 127        pid = fork();
 128        if (pid < 0)
 129                die("git-clone-pack: unable to fork off git-unpack-objects");
 130        if (!pid) {
 131                dup2(fd[0], 0);
 132                close(fd[0]);
 133                close(fd[1]);
 134                execlp("git-unpack-objects", "git-unpack-objects",
 135                        quiet ? "-q" : NULL, NULL);
 136                die("git-unpack-objects exec failed");
 137        }
 138        close(fd[0]);
 139        close(fd[1]);
 140        while (waitpid(pid, &status, 0) < 0) {
 141                if (errno != EINTR)
 142                        die("waiting for git-unpack-objects: %s", strerror(errno));
 143        }
 144        if (WIFEXITED(status)) {
 145                int code = WEXITSTATUS(status);
 146                if (code)
 147                        die("git-unpack-objects died with error code %d", code);
 148                write_refs(refs);
 149                return 0;
 150        }
 151        if (WIFSIGNALED(status)) {
 152                int sig = WTERMSIG(status);
 153                die("git-unpack-objects died of signal %d", sig);
 154        }
 155        die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
 156}
 157
 158int main(int argc, char **argv)
 159{
 160        int i, ret, nr_heads;
 161        char *dest = NULL, **heads;
 162        int fd[2];
 163        pid_t pid;
 164
 165        nr_heads = 0;
 166        heads = NULL;
 167        for (i = 1; i < argc; i++) {
 168                char *arg = argv[i];
 169
 170                if (*arg == '-') {
 171                        if (!strcmp("-q", arg)) {
 172                                quiet = 1;
 173                                continue;
 174                        }
 175                        if (!strncmp("--exec=", arg, 7)) {
 176                                exec = arg + 7;
 177                                continue;
 178                        }
 179                        usage(clone_pack_usage);
 180                }
 181                dest = arg;
 182                heads = argv + i + 1;
 183                nr_heads = argc - i - 1;
 184                break;
 185        }
 186        if (!dest)
 187                usage(clone_pack_usage);
 188        pid = git_connect(fd, dest, exec);
 189        if (pid < 0)
 190                return 1;
 191        ret = clone_pack(fd, nr_heads, heads);
 192        close(fd[0]);
 193        close(fd[1]);
 194        finish_connect(pid);
 195        return ret;
 196}