1#include "cache.h"2#include "refs.h"3#include "pkt-line.h"4#include "tag.h"5#include "object.h"6#include "commit.h"78static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";910#define THEY_HAVE (1U << 0)11#define MAX_HAS 25612#define MAX_NEEDS 25613static int nr_has = 0, nr_needs = 0;14static unsigned char has_sha1[MAX_HAS][20];15static unsigned char needs_sha1[MAX_NEEDS][20];16static unsigned int timeout = 0;1718static void reset_timeout(void)19{20alarm(timeout);21}2223static int strip(char *line, int len)24{25if (len && line[len-1] == '\n')26line[--len] = 0;27return len;28}2930static void create_pack_file(void)31{32int fd[2];33pid_t pid;3435if (pipe(fd) < 0)36die("git-upload-pack: unable to create pipe");37pid = fork();38if (pid < 0)39die("git-upload-pack: unable to fork git-rev-list");4041if (!pid) {42int i;43int args;44char **argv;45char *buf;46char **p;4748if (MAX_NEEDS <= nr_needs)49args = nr_has + 10;50else51args = nr_has + nr_needs + 5;52argv = xmalloc(args * sizeof(char *));53buf = xmalloc(args * 45);54p = argv;5556dup2(fd[1], 1);57close(0);58close(fd[0]);59close(fd[1]);60*p++ = "git-rev-list";61*p++ = "--objects";62if (MAX_NEEDS <= nr_needs)63*p++ = "--all";64else {65for (i = 0; i < nr_needs; i++) {66*p++ = buf;67memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);68buf += 41;69}70}71for (i = 0; i < nr_has; i++) {72*p++ = buf;73*buf++ = '^';74memcpy(buf, sha1_to_hex(has_sha1[i]), 41);75buf += 41;76}77*p++ = NULL;78execvp("git-rev-list", argv);79die("git-upload-pack: unable to exec git-rev-list");80}81dup2(fd[0], 0);82close(fd[0]);83close(fd[1]);84execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);85die("git-upload-pack: unable to exec git-pack-objects");86}8788static int got_sha1(char *hex, unsigned char *sha1)89{90if (get_sha1_hex(hex, sha1))91die("git-upload-pack: expected SHA1 object, got '%s'", hex);92if (!has_sha1_file(sha1))93return 0;94if (nr_has < MAX_HAS) {95struct object *o = lookup_object(sha1);96if (!o || (!o->parsed && !parse_object(sha1)))97die("oops (%s)", sha1_to_hex(sha1));98if (o->type == commit_type) {99struct commit_list *parents;100if (o->flags & THEY_HAVE)101return 0;102o->flags |= THEY_HAVE;103for (parents = ((struct commit*)o)->parents;104parents;105parents = parents->next)106parents->item->object.flags |= THEY_HAVE;107}108memcpy(has_sha1[nr_has++], sha1, 20);109}110return 1;111}112113static int get_common_commits(void)114{115static char line[1000];116unsigned char sha1[20];117int len;118119track_object_refs = 0;120save_commit_buffer = 0;121122for(;;) {123len = packet_read_line(0, line, sizeof(line));124reset_timeout();125126if (!len) {127packet_write(1, "NAK\n");128continue;129}130len = strip(line, len);131if (!strncmp(line, "have ", 5)) {132if (got_sha1(line+5, sha1)) {133packet_write(1, "ACK %s\n", sha1_to_hex(sha1));134break;135}136continue;137}138if (!strcmp(line, "done")) {139packet_write(1, "NAK\n");140return -1;141}142die("git-upload-pack: expected SHA1 list, got '%s'", line);143}144145for (;;) {146len = packet_read_line(0, line, sizeof(line));147reset_timeout();148if (!len)149continue;150len = strip(line, len);151if (!strncmp(line, "have ", 5)) {152got_sha1(line+5, sha1);153continue;154}155if (!strcmp(line, "done"))156break;157die("git-upload-pack: expected SHA1 list, got '%s'", line);158}159return 0;160}161162static int receive_needs(void)163{164static char line[1000];165int len, needs;166167needs = 0;168for (;;) {169unsigned char dummy[20], *sha1_buf;170len = packet_read_line(0, line, sizeof(line));171reset_timeout();172if (!len)173return needs;174175sha1_buf = dummy;176if (needs == MAX_NEEDS) {177fprintf(stderr,178"warning: supporting only a max of %d requests. "179"sending everything instead.\n",180MAX_NEEDS);181}182else if (needs < MAX_NEEDS)183sha1_buf = needs_sha1[needs];184185if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))186die("git-upload-pack: protocol error, "187"expected to get sha, not '%s'", line);188needs++;189}190}191192static int send_ref(const char *refname, const unsigned char *sha1)193{194struct object *o = parse_object(sha1);195196packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);197if (o->type == tag_type) {198o = deref_tag(o);199packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);200}201return 0;202}203204static int upload_pack(void)205{206reset_timeout();207head_ref(send_ref);208for_each_ref(send_ref);209packet_flush(1);210nr_needs = receive_needs();211if (!nr_needs)212return 0;213get_common_commits();214create_pack_file();215return 0;216}217218int main(int argc, char **argv)219{220const char *dir;221int i;222int strict = 0;223224for (i = 1; i < argc; i++) {225char *arg = argv[i];226227if (arg[0] != '-')228break;229if (!strcmp(arg, "--strict")) {230strict = 1;231continue;232}233if (!strncmp(arg, "--timeout=", 10)) {234timeout = atoi(arg+10);235continue;236}237if (!strcmp(arg, "--")) {238i++;239break;240}241}242243if (i != argc-1)244usage(upload_pack_usage);245dir = argv[i];246247/* chdir to the directory. If that fails, try appending ".git" */248if (chdir(dir) < 0) {249if (strict || chdir(mkpath("%s.git", dir)) < 0)250die("git-upload-pack unable to chdir to %s", dir);251}252if (!strict)253chdir(".git");254255if (access("objects", X_OK) || access("refs", X_OK))256die("git-upload-pack: %s doesn't seem to be a git archive", dir);257258putenv("GIT_DIR=.");259upload_pack();260return 0;261}