Commit first cut at "git-fetch-pack"
[git/debian.git] / upload-pack.c
blob18b8e1a6cac99633dddcd5628735a54e1047a712
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
5 static const char upload_pack_usage[] = "git-upload-pack <dir>";
7 static int got_sha1(char *hex, unsigned char *sha1)
9 if (get_sha1_hex(hex, sha1))
10 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
11 return has_sha1_file(sha1);
14 static int get_common_commits(void)
16 static char line[1000];
17 unsigned char sha1[20];
18 int len;
20 for(;;) {
21 len = packet_read_line(0, line, sizeof(line));
23 if (!len) {
24 packet_write(1, "NAK\n");
25 continue;
27 if (line[len-1] == '\n')
28 line[--len] = 0;
29 if (!strncmp(line, "have ", 5)) {
30 if (got_sha1(line+5, sha1)) {
31 packet_write(1, "ACK %s\n", sha1_to_hex(sha1));
32 break;
34 continue;
36 if (!strcmp(line, "done")) {
37 packet_write(1, "NAK\n");
38 return -1;
40 die("git-upload-pack: expected SHA1 list, got '%s'", line);
43 for (;;) {
44 len = packet_read_line(0, line, sizeof(line));
45 if (!len)
46 break;
47 if (!strncmp(line, "have ", 5)) {
48 got_sha1(line+5, sha1);
49 continue;
51 if (!strcmp(line, "done"))
52 break;
53 die("git-upload-pack: expected SHA1 list, got '%s'", line);
55 return 0;
58 static int send_ref(const char *refname, const unsigned char *sha1)
60 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
61 return 0;
64 static int upload_pack(void)
66 for_each_ref(send_ref);
67 packet_flush(1);
68 get_common_commits();
69 return 0;
72 int main(int argc, char **argv)
74 const char *dir;
75 if (argc != 2)
76 usage(upload_pack_usage);
77 dir = argv[1];
78 if (chdir(dir))
79 die("git-upload-pack unable to chdir to %s", dir);
80 chdir(".git");
81 if (access("objects", X_OK) || access("refs", X_OK))
82 die("git-upload-pack: %s doesn't seem to be a git archive", dir);
83 setenv("GIT_DIR", ".", 1);
84 upload_pack();
85 return 0;