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