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 (nr && !path_match(path, nr, match))
91 unsigned char old_sha1[20];
92 unsigned char new_sha1[20];
96 struct command *commands = NULL;
99 * This gets called after(if) we've successfully
100 * unpacked the data payload.
102 static void execute_commands(void)
104 struct command *cmd = commands;
107 char old_hex[60], *new_hex;
108 strcpy(old_hex, sha1_to_hex(cmd->old_sha1));
109 new_hex = sha1_to_hex(cmd->new_sha1);
110 fprintf(stderr, "%s: %s -> %s\n", cmd->ref_name, old_hex, new_hex);
115 static void read_head_info(void)
117 struct command **p = &commands;
119 static char line[1000];
120 unsigned char old_sha1[20], new_sha1[20];
124 len = packet_read_line(0, line, sizeof(line));
127 if (line[len-1] == '\n')
132 get_sha1_hex(line, old_sha1) ||
133 get_sha1_hex(line + 41, new_sha1))
134 die("protocol error: expected old/new/ref, got '%s'", line);
135 cmd = xmalloc(sizeof(struct command) + len - 80);
136 memcpy(cmd->old_sha1, old_sha1, 20);
137 memcpy(cmd->new_sha1, new_sha1, 20);
138 memcpy(cmd->ref_name, line + 82, len - 81);
145 static void unpack(void)
150 die("unpack fork failed");
152 setenv("GIT_DIR", ".", 1);
153 execlp(unpacker, unpacker, NULL);
154 die("unpack execute failed");
159 int retval = waitpid(pid, &status, 0);
164 die("waitpid failed (%s)", strerror(retval));
167 die("waitpid is confused");
168 if (WIFSIGNALED(status))
169 die("%s died of signal %d", unpacker, WTERMSIG(status));
170 if (!WIFEXITED(status))
171 die("%s died out of really strange complications", unpacker);
172 code = WEXITSTATUS(status);
174 die("%s exited with error code %d", unpacker, code);
179 int main(int argc, char **argv)
182 const char *dir = NULL;
186 for (i = 1; i < argc; i++) {
187 const char *arg = *argv++;
190 if (!strncmp(arg, "--unpack=", 9)) {
194 /* Do flag handling here */
195 usage(receive_pack_usage);
199 nr_heads = argc - i - 1;
203 usage(receive_pack_usage);
205 /* chdir to the directory. If that fails, try appending ".git" */
206 if (chdir(dir) < 0) {
207 static char path[PATH_MAX];
208 snprintf(path, sizeof(path), "%s.git", dir);
210 die("unable to cd to %s", dir);
213 /* If we have a ".git" directory, chdir to it */
216 if (access("objects", X_OK) < 0 || access("refs/heads", X_OK) < 0)
217 die("%s doesn't appear to be a git directory", dir);
218 write_head_info("refs/", nr_heads, heads);