5 static const char receive_pack_usage[] = "git-receive-pack [--unpack=executable] <git-dir> [heads]";
7 static const char *unpacker = "git-unpack-objects";
9 static int path_match(const char *path, int nr, char **match)
12 int pathlen = strlen(path);
14 for (i = 0; i < nr; i++) {
18 if (!len || len > pathlen)
20 if (memcmp(path + pathlen - len, s, len))
22 if (pathlen > len && path[pathlen - len - 1] != '/')
30 static void show_ref(const char *path, unsigned char *sha1)
32 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
35 static int read_ref(const char *path, unsigned char *sha1)
38 int fd = open(path, O_RDONLY);
42 if (read(fd, buffer, sizeof(buffer)) >= 40)
43 ret = get_sha1_hex(buffer, sha1);
49 static void write_head_info(const char *base, int nr, char **match)
51 DIR *dir = opendir(base);
55 int baselen = strlen(base);
56 char *path = xmalloc(baselen + 257);
57 memcpy(path, base, baselen);
59 while ((de = readdir(dir)) != NULL) {
64 if (de->d_name[0] == '.')
66 namelen = strlen(de->d_name);
69 memcpy(path + baselen, de->d_name, namelen+1);
70 if (lstat(path, &st) < 0)
72 if (S_ISDIR(st.st_mode)) {
73 path[baselen + namelen] = '/';
74 path[baselen + namelen + 1] = 0;
75 write_head_info(path, nr, match);
78 if (read_ref(path, sha1) < 0)
80 if (!has_sha1_file(sha1))
82 if (nr && !path_match(path, nr, match))
93 unsigned char old_sha1[20];
94 unsigned char new_sha1[20];
98 struct command *commands = NULL;
100 static int is_all_zeroes(const char *hex)
103 for (i = 0; i < 40; i++)
109 static int verify_old_ref(const char *name, char *hex_contents)
114 if (is_all_zeroes(hex_contents))
116 fd = open(name, O_RDONLY);
119 ret = read(fd, buffer, 40);
123 if (memcmp(buffer, hex_contents, 40))
128 static void update(const char *name, unsigned char *old_sha1, unsigned char *new_sha1)
130 char new_hex[60], *old_hex, *lock_name;
131 int newfd, namelen, written;
133 namelen = strlen(name);
134 lock_name = xmalloc(namelen + 10);
135 memcpy(lock_name, name, namelen);
136 memcpy(lock_name + namelen, ".lock", 6);
138 strcpy(new_hex, sha1_to_hex(new_sha1));
139 old_hex = sha1_to_hex(old_sha1);
140 if (!has_sha1_file(new_sha1))
141 die("unpack should have generated %s, but I can't find it!", new_hex);
143 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0644);
145 die("unable to create %s (%s)", lock_name, strerror(errno));
147 /* Write the ref with an ending '\n' */
150 written = write(newfd, new_hex, 41);
151 /* Remove the '\n' again */
157 die("unable to write %s", lock_name);
159 if (verify_old_ref(name, old_hex) < 0) {
161 die("%s changed during push", name);
163 if (rename(lock_name, name) < 0) {
165 die("unable to replace %s", name);
167 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
172 * This gets called after(if) we've successfully
173 * unpacked the data payload.
175 static void execute_commands(void)
177 struct command *cmd = commands;
180 update(cmd->ref_name, cmd->old_sha1, cmd->new_sha1);
185 static void read_head_info(void)
187 struct command **p = &commands;
189 static char line[1000];
190 unsigned char old_sha1[20], new_sha1[20];
194 len = packet_read_line(0, line, sizeof(line));
197 if (line[len-1] == '\n')
202 get_sha1_hex(line, old_sha1) ||
203 get_sha1_hex(line + 41, new_sha1))
204 die("protocol error: expected old/new/ref, got '%s'", line);
205 cmd = xmalloc(sizeof(struct command) + len - 80);
206 memcpy(cmd->old_sha1, old_sha1, 20);
207 memcpy(cmd->new_sha1, new_sha1, 20);
208 memcpy(cmd->ref_name, line + 82, len - 81);
215 static void unpack(void)
220 die("unpack fork failed");
222 execlp(unpacker, unpacker, NULL);
223 die("unpack execute failed");
228 int retval = waitpid(pid, &status, 0);
233 die("waitpid failed (%s)", strerror(retval));
236 die("waitpid is confused");
237 if (WIFSIGNALED(status))
238 die("%s died of signal %d", unpacker, WTERMSIG(status));
239 if (!WIFEXITED(status))
240 die("%s died out of really strange complications", unpacker);
241 code = WEXITSTATUS(status);
243 die("%s exited with error code %d", unpacker, code);
248 int main(int argc, char **argv)
251 const char *dir = NULL;
255 for (i = 1; i < argc; i++) {
256 const char *arg = *argv++;
259 if (!strncmp(arg, "--unpack=", 9)) {
263 /* Do flag handling here */
264 usage(receive_pack_usage);
268 nr_heads = argc - i - 1;
272 usage(receive_pack_usage);
274 /* chdir to the directory. If that fails, try appending ".git" */
275 if (chdir(dir) < 0) {
276 static char path[PATH_MAX];
277 snprintf(path, sizeof(path), "%s.git", dir);
279 die("unable to cd to %s", dir);
282 /* If we have a ".git" directory, chdir to it */
284 setenv("GIT_DIR", ".", 1);
286 if (access("objects", X_OK) < 0 || access("refs/heads", X_OK) < 0)
287 die("%s doesn't appear to be a git directory", dir);
288 write_head_info("refs/", nr_heads, heads);