send-pack.con commit Make sure leading directories exist when pushing refs. (29f3b3d)
   1#include "cache.h"
   2#include "commit.h"
   3#include "refs.h"
   4#include "pkt-line.h"
   5
   6static const char send_pack_usage[] =
   7"git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
   8"  --all and explicit <head> specification are mutually exclusive.";
   9static const char *exec = "git-receive-pack";
  10static int send_all = 0;
  11static int force_update = 0;
  12
  13static int is_zero_sha1(const unsigned char *sha1)
  14{
  15        int i;
  16
  17        for (i = 0; i < 20; i++) {
  18                if (*sha1++)
  19                        return 0;
  20        }
  21        return 1;
  22}
  23
  24static void exec_pack_objects(void)
  25{
  26        static char *args[] = {
  27                "git-pack-objects",
  28                "--stdout",
  29                NULL
  30        };
  31        execvp("git-pack-objects", args);
  32        die("git-pack-objects exec failed (%s)", strerror(errno));
  33}
  34
  35static void exec_rev_list(struct ref *refs)
  36{
  37        static char *args[1000];
  38        int i = 0;
  39
  40        args[i++] = "git-rev-list";     /* 0 */
  41        args[i++] = "--objects";        /* 1 */
  42        while (refs) {
  43                char *buf = malloc(100);
  44                if (i > 900)
  45                        die("git-rev-list environment overflow");
  46                if (!is_zero_sha1(refs->old_sha1)) {
  47                        args[i++] = buf;
  48                        snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
  49                        buf += 50;
  50                }
  51                if (!is_zero_sha1(refs->new_sha1)) {
  52                        args[i++] = buf;
  53                        snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
  54                }
  55                refs = refs->next;
  56        }
  57        args[i] = NULL;
  58        execvp("git-rev-list", args);
  59        die("git-rev-list exec failed (%s)", strerror(errno));
  60}
  61
  62static void rev_list(int fd, struct ref *refs)
  63{
  64        int pipe_fd[2];
  65        pid_t pack_objects_pid;
  66
  67        if (pipe(pipe_fd) < 0)
  68                die("rev-list setup: pipe failed");
  69        pack_objects_pid = fork();
  70        if (!pack_objects_pid) {
  71                dup2(pipe_fd[0], 0);
  72                dup2(fd, 1);
  73                close(pipe_fd[0]);
  74                close(pipe_fd[1]);
  75                close(fd);
  76                exec_pack_objects();
  77                die("pack-objects setup failed");
  78        }
  79        if (pack_objects_pid < 0)
  80                die("pack-objects fork failed");
  81        dup2(pipe_fd[1], 1);
  82        close(pipe_fd[0]);
  83        close(pipe_fd[1]);
  84        close(fd);
  85        exec_rev_list(refs);
  86}
  87
  88static int pack_objects(int fd, struct ref *refs)
  89{
  90        pid_t rev_list_pid;
  91
  92        rev_list_pid = fork();
  93        if (!rev_list_pid) {
  94                rev_list(fd, refs);
  95                die("rev-list setup failed");
  96        }
  97        if (rev_list_pid < 0)
  98                die("rev-list fork failed");
  99        /*
 100         * We don't wait for the rev-list pipeline in the parent:
 101         * we end up waiting for the other end instead
 102         */
 103        return 0;
 104}
 105
 106static int read_ref(const char *ref, unsigned char *sha1)
 107{
 108        int fd, ret;
 109        char buffer[60];
 110
 111        fd = open(git_path("%s", ref), O_RDONLY);
 112        if (fd < 0)
 113                return -1;
 114        ret = -1;
 115        if (read(fd, buffer, sizeof(buffer)) >= 40)
 116                ret = get_sha1_hex(buffer, sha1);
 117        close(fd);
 118        return ret;
 119}
 120
 121static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
 122{
 123        struct commit *new, *old;
 124        struct commit_list *list;
 125
 126        if (force_update)
 127                return 1;
 128        old = lookup_commit_reference(old_sha1);
 129        if (!old)
 130                return 0;
 131        new = lookup_commit_reference(new_sha1);
 132        if (!new)
 133                return 0;
 134        if (parse_commit(new) < 0)
 135                return 0;
 136        list = NULL;
 137        commit_list_insert(new, &list);
 138        while (list) {
 139                new = pop_most_recent_commit(&list, 1);
 140                if (new == old)
 141                        return 1;
 142        }
 143        return 0;
 144}
 145
 146static int local_ref_nr_match;
 147static char **local_ref_match;
 148static struct ref *local_ref_list;
 149static struct ref **local_last_ref;
 150
 151static int try_to_match(const char *refname, const unsigned char *sha1)
 152{
 153        struct ref *ref;
 154        int len;
 155
 156        if (!path_match(refname, local_ref_nr_match, local_ref_match)) {
 157                if (!send_all)
 158                        return 0;
 159
 160                /* If we have it listed already, skip it */
 161                for (ref = local_ref_list ; ref ; ref = ref->next) {
 162                        if (!strcmp(ref->name, refname))
 163                                return 0;
 164                }
 165        }
 166
 167        len = strlen(refname)+1;
 168        ref = xmalloc(sizeof(*ref) + len);
 169        memset(ref->old_sha1, 0, 20);
 170        memcpy(ref->new_sha1, sha1, 20);
 171        memcpy(ref->name, refname, len);
 172        ref->next = NULL;
 173        *local_last_ref = ref;
 174        local_last_ref = &ref->next;
 175        return 0;
 176}
 177
 178static int send_pack(int in, int out, int nr_match, char **match)
 179{
 180        struct ref *ref_list, **last_ref;
 181        struct ref *ref;
 182        int new_refs;
 183
 184        /* First we get all heads, whether matching or not.. */
 185        last_ref = get_remote_heads(in, &ref_list, 0, NULL);
 186
 187        /*
 188         * Go through the refs, see if we want to update
 189         * any of them..
 190         */
 191        for (ref = ref_list; ref; ref = ref->next) {
 192                unsigned char new_sha1[20];
 193                char *name = ref->name;
 194
 195                if (nr_match && !path_match(name, nr_match, match))
 196                        continue;
 197
 198                if (read_ref(name, new_sha1) < 0)
 199                        continue;
 200
 201                if (!memcmp(ref->old_sha1, new_sha1, 20)) {
 202                        fprintf(stderr, "'%s' unchanged\n", name);
 203                        continue;
 204                }
 205
 206                if (!ref_newer(new_sha1, ref->old_sha1)) {
 207                        error("remote '%s' isn't a strict parent of local", name);
 208                        continue;
 209                }
 210
 211                /* Ok, mark it for update */
 212                memcpy(ref->new_sha1, new_sha1, 20);
 213        }
 214
 215        /*
 216         * See if we have any refs that the other end didn't have
 217         */
 218        if (nr_match || send_all) {
 219                local_ref_nr_match = nr_match;
 220                local_ref_match = match;
 221                local_ref_list = ref_list;
 222                local_last_ref = last_ref;
 223                for_each_ref(try_to_match);
 224        }
 225
 226        /*
 227         * Finally, tell the other end!
 228         */
 229        new_refs = 0;
 230        for (ref = ref_list; ref; ref = ref->next) {
 231                char old_hex[60], *new_hex;
 232                if (is_zero_sha1(ref->new_sha1))
 233                        continue;
 234                new_refs++;
 235                strcpy(old_hex, sha1_to_hex(ref->old_sha1));
 236                new_hex = sha1_to_hex(ref->new_sha1);
 237                packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
 238                fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
 239        }
 240        
 241        packet_flush(out);
 242        if (new_refs)
 243                pack_objects(out, ref_list);
 244        close(out);
 245        return 0;
 246}
 247
 248int main(int argc, char **argv)
 249{
 250        int i, nr_heads = 0;
 251        char *dest = NULL;
 252        char **heads = NULL;
 253        int fd[2], ret;
 254        pid_t pid;
 255
 256        argv++;
 257        for (i = 1; i < argc; i++, argv++) {
 258                char *arg = *argv;
 259
 260                if (*arg == '-') {
 261                        if (!strncmp(arg, "--exec=", 7)) {
 262                                exec = arg + 7;
 263                                continue;
 264                        }
 265                        if (!strcmp(arg, "--all")) {
 266                                send_all = 1;
 267                                continue;
 268                        }
 269                        if (!strcmp(arg, "--force")) {
 270                                force_update = 1;
 271                                continue;
 272                        }
 273                        usage(send_pack_usage);
 274                }
 275                if (!dest) {
 276                        dest = arg;
 277                        continue;
 278                }
 279                heads = argv;
 280                nr_heads = argc - i;
 281                break;
 282        }
 283        if (!dest)
 284                usage(send_pack_usage);
 285        if (heads && send_all)
 286                usage(send_pack_usage);
 287        pid = git_connect(fd, dest, exec);
 288        if (pid < 0)
 289                return 1;
 290        ret = send_pack(fd[0], fd[1], nr_heads, heads);
 291        close(fd[0]);
 292        close(fd[1]);
 293        finish_connect(pid);
 294        return ret;
 295}