6 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
8 static const char *unpacker = "git-unpack-objects";
10 static int show_ref(const char *path, const unsigned char *sha1)
12 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
16 static void write_head_info(void)
18 for_each_ref(show_ref);
23 unsigned char old_sha1[20];
24 unsigned char new_sha1[20];
28 static struct command *commands = NULL;
30 static int is_all_zeroes(const char *hex)
33 for (i = 0; i < 40; i++)
39 static int verify_old_ref(const char *name, char *hex_contents)
44 if (is_all_zeroes(hex_contents))
46 fd = open(name, O_RDONLY);
49 ret = read(fd, buffer, 40);
53 if (memcmp(buffer, hex_contents, 40))
58 static void update(const char *name, unsigned char *old_sha1, unsigned char *new_sha1)
60 char new_hex[60], *old_hex, *lock_name;
61 int newfd, namelen, written;
63 namelen = strlen(name);
64 lock_name = xmalloc(namelen + 10);
65 memcpy(lock_name, name, namelen);
66 memcpy(lock_name + namelen, ".lock", 6);
68 strcpy(new_hex, sha1_to_hex(new_sha1));
69 old_hex = sha1_to_hex(old_sha1);
70 if (!has_sha1_file(new_sha1))
71 die("unpack should have generated %s, but I can't find it!", new_hex);
73 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
75 die("unable to create %s (%s)", lock_name, strerror(errno));
77 /* Write the ref with an ending '\n' */
80 written = write(newfd, new_hex, 41);
81 /* Remove the '\n' again */
87 die("unable to write %s", lock_name);
89 if (verify_old_ref(name, old_hex) < 0) {
91 die("%s changed during push", name);
93 if (rename(lock_name, name) < 0) {
95 die("unable to replace %s", name);
97 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
102 * This gets called after(if) we've successfully
103 * unpacked the data payload.
105 static void execute_commands(void)
107 struct command *cmd = commands;
110 update(cmd->ref_name, cmd->old_sha1, cmd->new_sha1);
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 execlp(unpacker, unpacker, NULL);
153 die("unpack execute failed");
158 int retval = waitpid(pid, &status, 0);
163 die("waitpid failed (%s)", strerror(retval));
166 die("waitpid is confused");
167 if (WIFSIGNALED(status))
168 die("%s died of signal %d", unpacker, WTERMSIG(status));
169 if (!WIFEXITED(status))
170 die("%s died out of really strange complications", unpacker);
171 code = WEXITSTATUS(status);
173 die("%s exited with error code %d", unpacker, code);
178 int main(int argc, char **argv)
181 const char *dir = NULL;
184 for (i = 1; i < argc; i++) {
185 const char *arg = *argv++;
188 /* Do flag handling here */
189 usage(receive_pack_usage);
192 usage(receive_pack_usage);
196 usage(receive_pack_usage);
198 /* chdir to the directory. If that fails, try appending ".git" */
199 if (chdir(dir) < 0) {
200 static char path[PATH_MAX];
201 snprintf(path, sizeof(path), "%s.git", dir);
203 die("unable to cd to %s", dir);
206 /* If we have a ".git" directory, chdir to it */
208 setenv("GIT_DIR", ".", 1);
210 if (access("objects", X_OK) < 0 || access("refs/heads", X_OK) < 0)
211 die("%s doesn't appear to be a git directory", dir);