1#include "cache.h"2#include "refs.h"3#include "pkt-line.h"45static const char send_pack_usage[] =6"git-send-pack [--exec=git-receive-pack] [host:]directory [heads]*";7static const char *exec = "git-receive-pack";8static int send_all = 0;910static int is_zero_sha1(const unsigned char *sha1)11{12int i;1314for (i = 0; i < 20; i++) {15if (*sha1++)16return 0;17}18return 1;19}2021static void exec_pack_objects(void)22{23static char *args[] = {24"git-pack-objects",25"--stdout",26NULL27};28execvp("git-pack-objects", args);29die("git-pack-objects exec failed (%s)", strerror(errno));30}3132static void exec_rev_list(struct ref *refs)33{34static char *args[1000];35int i = 0;3637args[i++] = "git-rev-list"; /* 0 */38args[i++] = "--objects"; /* 1 */39while (refs) {40char *buf = malloc(100);41if (i > 900)42die("git-rev-list environment overflow");43if (!is_zero_sha1(refs->old_sha1)) {44args[i++] = buf;45snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));46buf += 50;47}48if (!is_zero_sha1(refs->new_sha1)) {49args[i++] = buf;50snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));51}52refs = refs->next;53}54args[i] = NULL;55execvp("git-rev-list", args);56die("git-rev-list exec failed (%s)", strerror(errno));57}5859static void rev_list(int fd, struct ref *refs)60{61int pipe_fd[2];62pid_t pack_objects_pid;6364if (pipe(pipe_fd) < 0)65die("rev-list setup: pipe failed");66pack_objects_pid = fork();67if (!pack_objects_pid) {68dup2(pipe_fd[0], 0);69dup2(fd, 1);70close(pipe_fd[0]);71close(pipe_fd[1]);72close(fd);73exec_pack_objects();74die("pack-objects setup failed");75}76if (pack_objects_pid < 0)77die("pack-objects fork failed");78dup2(pipe_fd[1], 1);79close(pipe_fd[0]);80close(pipe_fd[1]);81close(fd);82exec_rev_list(refs);83}8485static int pack_objects(int fd, struct ref *refs)86{87pid_t rev_list_pid;8889rev_list_pid = fork();90if (!rev_list_pid) {91rev_list(fd, refs);92die("rev-list setup failed");93}94if (rev_list_pid < 0)95die("rev-list fork failed");96/*97* We don't wait for the rev-list pipeline in the parent:98* we end up waiting for the other end instead99*/100return 0;101}102103static int read_ref(const char *ref, unsigned char *sha1)104{105int fd, ret;106char buffer[60];107108fd = open(git_path("%s", ref), O_RDONLY);109if (fd < 0)110return -1;111ret = -1;112if (read(fd, buffer, sizeof(buffer)) >= 40)113ret = get_sha1_hex(buffer, sha1);114close(fd);115return ret;116}117118static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)119{120if (!has_sha1_file(old_sha1))121return 0;122/*123* FIXME! It is not correct to say that the new one is newer124* just because we don't have the old one!125*126* We should really see if we can reach the old_sha1 commit127* from the new_sha1 one.128*/129return 1;130}131132static int local_ref_nr_match;133static char **local_ref_match;134static struct ref *local_ref_list;135static struct ref **local_last_ref;136137static int try_to_match(const char *refname, const unsigned char *sha1)138{139struct ref *ref;140int len;141142if (!path_match(refname, local_ref_nr_match, local_ref_match)) {143if (!send_all)144return 0;145146/* If we have it listed already, skip it */147for (ref = local_ref_list ; ref ; ref = ref->next) {148if (!strcmp(ref->name, refname))149return 0;150}151}152153len = strlen(refname)+1;154ref = xmalloc(sizeof(*ref) + len);155memset(ref->old_sha1, 0, 20);156memcpy(ref->new_sha1, sha1, 20);157memcpy(ref->name, refname, len);158ref->next = NULL;159*local_last_ref = ref;160local_last_ref = &ref->next;161return 0;162}163164static int send_pack(int in, int out, int nr_match, char **match)165{166struct ref *ref_list, **last_ref;167struct ref *ref;168int new_refs;169170/* First we get all heads, whether matching or not.. */171last_ref = get_remote_heads(in, &ref_list, 0, NULL);172173/*174* Go through the refs, see if we want to update175* any of them..176*/177for (ref = ref_list; ref; ref = ref->next) {178unsigned char new_sha1[20];179char *name = ref->name;180181if (nr_match && !path_match(name, nr_match, match))182continue;183184if (read_ref(name, new_sha1) < 0)185continue;186187if (!memcmp(ref->old_sha1, new_sha1, 20)) {188fprintf(stderr, "'%s' unchanged\n", name);189continue;190}191192if (!ref_newer(new_sha1, ref->old_sha1)) {193error("remote '%s' points to object I don't have", name);194continue;195}196197/* Ok, mark it for update */198memcpy(ref->new_sha1, new_sha1, 20);199}200201/*202* See if we have any refs that the other end didn't have203*/204if (nr_match) {205local_ref_nr_match = nr_match;206local_ref_match = match;207local_ref_list = ref_list;208local_last_ref = last_ref;209for_each_ref(try_to_match);210}211212/*213* Finally, tell the other end!214*/215new_refs = 0;216for (ref = ref_list; ref; ref = ref->next) {217char old_hex[60], *new_hex;218if (is_zero_sha1(ref->new_sha1))219continue;220new_refs++;221strcpy(old_hex, sha1_to_hex(ref->old_sha1));222new_hex = sha1_to_hex(ref->new_sha1);223packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);224fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);225}226227packet_flush(out);228if (new_refs)229pack_objects(out, ref_list);230close(out);231return 0;232}233234int main(int argc, char **argv)235{236int i, nr_heads = 0;237char *dest = NULL;238char **heads = NULL;239int fd[2], ret;240pid_t pid;241242argv++;243for (i = 1; i < argc; i++, argv++) {244char *arg = *argv;245246if (*arg == '-') {247if (!strncmp(arg, "--exec=", 7)) {248exec = arg + 7;249continue;250}251if (!strcmp(arg, "--all")) {252send_all = 1;253continue;254}255usage(send_pack_usage);256}257if (!dest) {258dest = arg;259continue;260}261heads = argv;262nr_heads = argc - i;263break;264}265if (!dest)266usage(send_pack_usage);267pid = git_connect(fd, dest, exec);268if (pid < 0)269return 1;270ret = send_pack(fd[0], fd[1], nr_heads, heads);271close(fd[0]);272close(fd[1]);273finish_connect(pid);274return ret;275}