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