5 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
7 static const char *unpacker = "git-unpack-objects";
9 static void show_ref(const char *path, unsigned char *sha1)
11 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
14 static int read_ref(const char *path, unsigned char *sha1)
17 int fd = open(path, O_RDONLY);
21 if (read(fd, buffer, sizeof(buffer)) >= 40)
22 ret = get_sha1_hex(buffer, sha1);
28 static void write_head_info(const char *base)
30 DIR *dir = opendir(base);
34 int baselen = strlen(base);
35 char *path = xmalloc(baselen + 257);
36 memcpy(path, base, baselen);
38 while ((de = readdir(dir)) != NULL) {
43 if (de->d_name[0] == '.')
45 namelen = strlen(de->d_name);
48 memcpy(path + baselen, de->d_name, namelen+1);
49 if (lstat(path, &st) < 0)
51 if (S_ISDIR(st.st_mode)) {
52 path[baselen + namelen] = '/';
53 path[baselen + namelen + 1] = 0;
54 write_head_info(path);
57 if (read_ref(path, sha1) < 0)
59 if (!has_sha1_file(sha1))
70 unsigned char old_sha1[20];
71 unsigned char new_sha1[20];
75 struct command *commands = NULL;
77 static int is_all_zeroes(const char *hex)
80 for (i = 0; i < 40; i++)
86 static int verify_old_ref(const char *name, char *hex_contents)
91 if (is_all_zeroes(hex_contents))
93 fd = open(name, O_RDONLY);
96 ret = read(fd, buffer, 40);
100 if (memcmp(buffer, hex_contents, 40))
105 static void update(const char *name, unsigned char *old_sha1, unsigned char *new_sha1)
107 char new_hex[60], *old_hex, *lock_name;
108 int newfd, namelen, written;
110 namelen = strlen(name);
111 lock_name = xmalloc(namelen + 10);
112 memcpy(lock_name, name, namelen);
113 memcpy(lock_name + namelen, ".lock", 6);
115 strcpy(new_hex, sha1_to_hex(new_sha1));
116 old_hex = sha1_to_hex(old_sha1);
117 if (!has_sha1_file(new_sha1))
118 die("unpack should have generated %s, but I can't find it!", new_hex);
120 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0644);
122 die("unable to create %s (%s)", lock_name, strerror(errno));
124 /* Write the ref with an ending '\n' */
127 written = write(newfd, new_hex, 41);
128 /* Remove the '\n' again */
134 die("unable to write %s", lock_name);
136 if (verify_old_ref(name, old_hex) < 0) {
138 die("%s changed during push", name);
140 if (rename(lock_name, name) < 0) {
142 die("unable to replace %s", name);
144 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
149 * This gets called after(if) we've successfully
150 * unpacked the data payload.
152 static void execute_commands(void)
154 struct command *cmd = commands;
157 update(cmd->ref_name, cmd->old_sha1, cmd->new_sha1);
162 static void read_head_info(void)
164 struct command **p = &commands;
166 static char line[1000];
167 unsigned char old_sha1[20], new_sha1[20];
171 len = packet_read_line(0, line, sizeof(line));
174 if (line[len-1] == '\n')
179 get_sha1_hex(line, old_sha1) ||
180 get_sha1_hex(line + 41, new_sha1))
181 die("protocol error: expected old/new/ref, got '%s'", line);
182 cmd = xmalloc(sizeof(struct command) + len - 80);
183 memcpy(cmd->old_sha1, old_sha1, 20);
184 memcpy(cmd->new_sha1, new_sha1, 20);
185 memcpy(cmd->ref_name, line + 82, len - 81);
192 static void unpack(void)
197 die("unpack fork failed");
199 execlp(unpacker, unpacker, NULL);
200 die("unpack execute failed");
205 int retval = waitpid(pid, &status, 0);
210 die("waitpid failed (%s)", strerror(retval));
213 die("waitpid is confused");
214 if (WIFSIGNALED(status))
215 die("%s died of signal %d", unpacker, WTERMSIG(status));
216 if (!WIFEXITED(status))
217 die("%s died out of really strange complications", unpacker);
218 code = WEXITSTATUS(status);
220 die("%s exited with error code %d", unpacker, code);
225 int main(int argc, char **argv)
228 const char *dir = NULL;
231 for (i = 1; i < argc; i++) {
232 const char *arg = *argv++;
235 /* Do flag handling here */
236 usage(receive_pack_usage);
239 usage(receive_pack_usage);
243 usage(receive_pack_usage);
245 /* chdir to the directory. If that fails, try appending ".git" */
246 if (chdir(dir) < 0) {
247 static char path[PATH_MAX];
248 snprintf(path, sizeof(path), "%s.git", dir);
250 die("unable to cd to %s", dir);
253 /* If we have a ".git" directory, chdir to it */
255 setenv("GIT_DIR", ".", 1);
257 if (access("objects", X_OK) < 0 || access("refs/heads", X_OK) < 0)
258 die("%s doesn't appear to be a git directory", dir);
259 write_head_info("refs/");