4 #include "run-command.h"
7 static const char receive_pack_usage
[] = "git-receive-pack <git-dir>";
9 static const char unpacker
[] = "git-unpack-objects";
11 static int show_ref(const char *path
, const unsigned char *sha1
)
13 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
17 static void write_head_info(void)
19 for_each_ref(show_ref
);
24 unsigned char old_sha1
[20];
25 unsigned char new_sha1
[20];
29 static struct command
*commands
= NULL
;
31 static int is_all_zeroes(const char *hex
)
34 for (i
= 0; i
< 40; i
++)
40 static int verify_old_ref(const char *name
, char *hex_contents
)
45 if (is_all_zeroes(hex_contents
))
47 fd
= open(name
, O_RDONLY
);
50 ret
= read(fd
, buffer
, 40);
54 if (memcmp(buffer
, hex_contents
, 40))
59 static char update_hook
[] = "hooks/update";
61 static int run_update_hook(const char *refname
,
62 char *old_hex
, char *new_hex
)
66 if (access(update_hook
, X_OK
) < 0)
68 code
= run_command(update_hook
, refname
, old_hex
, new_hex
, NULL
);
72 case -ERR_RUN_COMMAND_FORK
:
73 die("hook fork failed");
74 case -ERR_RUN_COMMAND_EXEC
:
75 die("hook execute failed");
76 case -ERR_RUN_COMMAND_WAITPID
:
77 die("waitpid failed");
78 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
79 die("waitpid is confused");
80 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
81 fprintf(stderr
, "%s died of signal", update_hook
);
83 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
84 die("%s died strangely", update_hook
);
86 error("%s exited with error code %d", update_hook
, -code
);
91 static void update(const char *name
, unsigned char *old_sha1
, unsigned char *new_sha1
)
93 char new_hex
[60], *old_hex
, *lock_name
;
94 int newfd
, namelen
, written
;
96 namelen
= strlen(name
);
97 lock_name
= xmalloc(namelen
+ 10);
98 memcpy(lock_name
, name
, namelen
);
99 memcpy(lock_name
+ namelen
, ".lock", 6);
101 strcpy(new_hex
, sha1_to_hex(new_sha1
));
102 old_hex
= sha1_to_hex(old_sha1
);
103 if (!has_sha1_file(new_sha1
))
104 die("unpack should have generated %s, but I can't find it!", new_hex
);
106 newfd
= open(lock_name
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
108 die("unable to create %s (%s)", lock_name
, strerror(errno
));
110 /* Write the ref with an ending '\n' */
113 written
= write(newfd
, new_hex
, 41);
114 /* Remove the '\n' again */
120 die("unable to write %s", lock_name
);
122 if (verify_old_ref(name
, old_hex
) < 0) {
124 die("%s changed during push", name
);
126 if (run_update_hook(name
, old_hex
, new_hex
)) {
128 fprintf(stderr
, "hook declined to update %s\n", name
);
130 else if (rename(lock_name
, name
) < 0) {
132 die("unable to replace %s", name
);
135 fprintf(stderr
, "%s: %s -> %s\n", name
, old_hex
, new_hex
);
140 * This gets called after(if) we've successfully
141 * unpacked the data payload.
143 static void execute_commands(void)
145 struct command
*cmd
= commands
;
148 update(cmd
->ref_name
, cmd
->old_sha1
, cmd
->new_sha1
);
151 run_update_hook("", NULL
, NULL
);
154 static void read_head_info(void)
156 struct command
**p
= &commands
;
158 static char line
[1000];
159 unsigned char old_sha1
[20], new_sha1
[20];
163 len
= packet_read_line(0, line
, sizeof(line
));
166 if (line
[len
-1] == '\n')
171 get_sha1_hex(line
, old_sha1
) ||
172 get_sha1_hex(line
+ 41, new_sha1
))
173 die("protocol error: expected old/new/ref, got '%s'", line
);
174 cmd
= xmalloc(sizeof(struct command
) + len
- 80);
175 memcpy(cmd
->old_sha1
, old_sha1
, 20);
176 memcpy(cmd
->new_sha1
, new_sha1
, 20);
177 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
184 static void unpack(void)
186 int code
= run_command(unpacker
, NULL
);
190 case -ERR_RUN_COMMAND_FORK
:
191 die("unpack fork failed");
192 case -ERR_RUN_COMMAND_EXEC
:
193 die("unpack execute failed");
194 case -ERR_RUN_COMMAND_WAITPID
:
195 die("waitpid failed");
196 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
197 die("waitpid is confused");
198 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
199 die("%s died of signal", unpacker
);
200 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
201 die("%s died strangely", unpacker
);
203 die("%s exited with error code %d", unpacker
, -code
);
207 int main(int argc
, char **argv
)
210 const char *dir
= NULL
;
213 for (i
= 1; i
< argc
; i
++) {
214 const char *arg
= *argv
++;
217 /* Do flag handling here */
218 usage(receive_pack_usage
);
221 usage(receive_pack_usage
);
225 usage(receive_pack_usage
);
227 /* chdir to the directory. If that fails, try appending ".git" */
228 if (chdir(dir
) < 0) {
229 if (chdir(mkpath("%s.git", dir
)) < 0)
230 die("unable to cd to %s", dir
);
233 /* If we have a ".git" directory, chdir to it */
235 setenv("GIT_DIR", ".", 1);
237 if (access("objects", X_OK
) < 0 || access("refs/heads", X_OK
) < 0)
238 die("%s doesn't appear to be a git directory", dir
);