show-branch: optionally use unique prefix as name.
[git/jnareb-git/bp-gitweb.git] / upload-pack.c
blob83f5a35d2686401e9a70d7aafae07c88c7b6e936
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 #define MAX_HAS (16)
8 #define MAX_NEEDS (256)
9 static int nr_has = 0, nr_needs = 0;
10 static unsigned char has_sha1[MAX_HAS][20];
11 static unsigned char needs_sha1[MAX_NEEDS][20];
13 static int strip(char *line, int len)
15 if (len && line[len-1] == '\n')
16 line[--len] = 0;
17 return len;
20 static void create_pack_file(void)
22 int fd[2];
23 pid_t pid;
25 if (pipe(fd) < 0)
26 die("git-upload-pack: unable to create pipe");
27 pid = fork();
28 if (pid < 0)
29 die("git-upload-pack: unable to fork git-rev-list");
31 if (!pid) {
32 int i;
33 int args;
34 char **argv;
35 char *buf;
36 char **p;
38 if (MAX_NEEDS <= nr_needs)
39 args = nr_has + 10;
40 else
41 args = nr_has + nr_needs + 5;
42 argv = xmalloc(args * sizeof(char *));
43 buf = xmalloc(args * 45);
44 p = argv;
46 dup2(fd[1], 1);
47 close(0);
48 close(fd[0]);
49 close(fd[1]);
50 *p++ = "git-rev-list";
51 *p++ = "--objects";
52 if (MAX_NEEDS <= nr_needs)
53 *p++ = "--all";
54 else {
55 for (i = 0; i < nr_needs; i++) {
56 *p++ = buf;
57 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
58 buf += 41;
61 for (i = 0; i < nr_has; i++) {
62 *p++ = buf;
63 *buf++ = '^';
64 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
65 buf += 41;
67 *p++ = NULL;
68 execvp("git-rev-list", argv);
69 die("git-upload-pack: unable to exec git-rev-list");
71 dup2(fd[0], 0);
72 close(fd[0]);
73 close(fd[1]);
74 execlp("git-pack-objects", "git-pack-objects", "--stdout", NULL);
75 die("git-upload-pack: unable to exec git-pack-objects");
78 static int got_sha1(char *hex, unsigned char *sha1)
80 int nr;
81 if (get_sha1_hex(hex, sha1))
82 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
83 if (!has_sha1_file(sha1))
84 return 0;
85 nr = nr_has;
86 if (nr < MAX_HAS) {
87 memcpy(has_sha1[nr], sha1, 20);
88 nr_has = nr+1;
90 return 1;
93 static int get_common_commits(void)
95 static char line[1000];
96 unsigned char sha1[20];
97 int len;
99 for(;;) {
100 len = packet_read_line(0, line, sizeof(line));
102 if (!len) {
103 packet_write(1, "NAK\n");
104 continue;
106 len = strip(line, len);
107 if (!strncmp(line, "have ", 5)) {
108 if (got_sha1(line+5, sha1)) {
109 packet_write(1, "ACK %s\n", sha1_to_hex(sha1));
110 break;
112 continue;
114 if (!strcmp(line, "done")) {
115 packet_write(1, "NAK\n");
116 return -1;
118 die("git-upload-pack: expected SHA1 list, got '%s'", line);
121 for (;;) {
122 len = packet_read_line(0, line, sizeof(line));
123 if (!len)
124 continue;
125 len = strip(line, len);
126 if (!strncmp(line, "have ", 5)) {
127 got_sha1(line+5, sha1);
128 continue;
130 if (!strcmp(line, "done"))
131 break;
132 die("git-upload-pack: expected SHA1 list, got '%s'", line);
134 return 0;
137 static int receive_needs(void)
139 static char line[1000];
140 int len, needs;
142 needs = 0;
143 for (;;) {
144 unsigned char dummy[20], *sha1_buf;
145 len = packet_read_line(0, line, sizeof(line));
146 if (!len)
147 return needs;
149 sha1_buf = dummy;
150 if (needs == MAX_NEEDS) {
151 fprintf(stderr,
152 "warning: supporting only a max of %d requests. "
153 "sending everything instead.\n",
154 MAX_NEEDS);
156 else if (needs < MAX_NEEDS)
157 sha1_buf = needs_sha1[needs];
159 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
160 die("git-upload-pack: protocol error, "
161 "expected to get sha, not '%s'", line);
162 needs++;
166 static int send_ref(const char *refname, const unsigned char *sha1)
168 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
169 return 0;
172 static int upload_pack(void)
174 head_ref(send_ref);
175 for_each_ref(send_ref);
176 packet_flush(1);
177 nr_needs = receive_needs();
178 if (!nr_needs)
179 return 0;
180 get_common_commits();
181 create_pack_file();
182 return 0;
185 int main(int argc, char **argv)
187 const char *dir;
188 if (argc != 2)
189 usage(upload_pack_usage);
190 dir = argv[1];
192 /* chdir to the directory. If that fails, try appending ".git" */
193 if (chdir(dir) < 0) {
194 if (chdir(mkpath("%s.git", dir)) < 0)
195 die("git-upload-pack unable to chdir to %s", dir);
197 chdir(".git");
198 if (access("objects", X_OK) || access("refs", X_OK))
199 die("git-upload-pack: %s doesn't seem to be a git archive", dir);
200 putenv("GIT_DIR=.");
201 upload_pack();
202 return 0;