4 #include "run-command.h"
8 static const char receive_pack_usage
[] = "git-receive-pack <git-dir>";
10 static const char *unpacker
[] = { "unpack-objects", NULL
};
12 static int report_status
;
14 static char capabilities
[] = "report-status";
15 static int capabilities_sent
;
17 static int show_ref(const char *path
, const unsigned char *sha1
)
19 if (capabilities_sent
)
20 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
22 packet_write(1, "%s %s%c%s\n",
23 sha1_to_hex(sha1
), path
, 0, capabilities
);
24 capabilities_sent
= 1;
28 static void write_head_info(void)
30 for_each_ref(show_ref
);
31 if (!capabilities_sent
)
32 show_ref("capabilities^{}", null_sha1
);
38 const char *error_string
;
39 unsigned char old_sha1
[20];
40 unsigned char new_sha1
[20];
41 char ref_name
[FLEX_ARRAY
]; /* more */
44 static struct command
*commands
;
46 static int is_all_zeroes(const char *hex
)
49 for (i
= 0; i
< 40; i
++)
55 static int verify_old_ref(const char *name
, char *hex_contents
)
60 if (is_all_zeroes(hex_contents
))
62 fd
= open(name
, O_RDONLY
);
65 ret
= read(fd
, buffer
, 40);
69 if (memcmp(buffer
, hex_contents
, 40))
74 static char update_hook
[] = "hooks/update";
76 static int run_update_hook(const char *refname
,
77 char *old_hex
, char *new_hex
)
81 if (access(update_hook
, X_OK
) < 0)
83 code
= run_command(update_hook
, refname
, old_hex
, new_hex
, NULL
);
87 case -ERR_RUN_COMMAND_FORK
:
88 return error("hook fork failed");
89 case -ERR_RUN_COMMAND_EXEC
:
90 return error("hook execute failed");
91 case -ERR_RUN_COMMAND_WAITPID
:
92 return error("waitpid failed");
93 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
94 return error("waitpid is confused");
95 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
96 return error("%s died of signal", update_hook
);
97 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
98 return error("%s died strangely", update_hook
);
100 error("%s exited with error code %d", update_hook
, -code
);
105 static int update(struct command
*cmd
)
107 const char *name
= cmd
->ref_name
;
108 unsigned char *old_sha1
= cmd
->old_sha1
;
109 unsigned char *new_sha1
= cmd
->new_sha1
;
110 char new_hex
[60], *old_hex
, *lock_name
;
111 int newfd
, namelen
, written
;
113 cmd
->error_string
= NULL
;
114 if (!strncmp(name
, "refs/", 5) && check_ref_format(name
+ 5)) {
115 cmd
->error_string
= "funny refname";
116 return error("refusing to create funny ref '%s' locally",
120 namelen
= strlen(name
);
121 lock_name
= xmalloc(namelen
+ 10);
122 memcpy(lock_name
, name
, namelen
);
123 memcpy(lock_name
+ namelen
, ".lock", 6);
125 strcpy(new_hex
, sha1_to_hex(new_sha1
));
126 old_hex
= sha1_to_hex(old_sha1
);
127 if (!has_sha1_file(new_sha1
)) {
128 cmd
->error_string
= "bad pack";
129 return error("unpack should have generated %s, "
130 "but I can't find it!", new_hex
);
132 if (deny_non_fast_forwards
&& !is_null_sha1(old_sha1
)) {
133 struct commit
*old_commit
, *new_commit
;
134 struct commit_list
*bases
;
136 old_commit
= (struct commit
*)parse_object(old_sha1
);
137 new_commit
= (struct commit
*)parse_object(new_sha1
);
138 for (bases
= get_merge_bases(old_commit
, new_commit
, 1);
139 bases
; bases
= bases
->next
)
140 if (!hashcmp(old_sha1
, bases
->item
->object
.sha1
))
143 return error("denying non-fast forward;"
144 " you should pull first");
146 safe_create_leading_directories(lock_name
);
148 newfd
= open(lock_name
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
150 cmd
->error_string
= "can't lock";
151 return error("unable to create %s (%s)",
152 lock_name
, strerror(errno
));
155 /* Write the ref with an ending '\n' */
158 written
= write(newfd
, new_hex
, 41);
159 /* Remove the '\n' again */
165 cmd
->error_string
= "can't write";
166 return error("unable to write %s", lock_name
);
168 if (verify_old_ref(name
, old_hex
) < 0) {
170 cmd
->error_string
= "raced";
171 return error("%s changed during push", name
);
173 if (run_update_hook(name
, old_hex
, new_hex
)) {
175 cmd
->error_string
= "hook declined";
176 return error("hook declined to update %s", name
);
178 else if (rename(lock_name
, name
) < 0) {
180 cmd
->error_string
= "can't rename";
181 return error("unable to replace %s", name
);
184 fprintf(stderr
, "%s: %s -> %s\n", name
, old_hex
, new_hex
);
189 static char update_post_hook
[] = "hooks/post-update";
191 static void run_update_post_hook(struct command
*cmd
)
193 struct command
*cmd_p
;
197 if (access(update_post_hook
, X_OK
) < 0)
199 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
200 if (cmd_p
->error_string
)
204 argv
= xmalloc(sizeof(*argv
) * (1 + argc
));
205 argv
[0] = update_post_hook
;
207 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
209 if (cmd_p
->error_string
)
211 p
= xmalloc(strlen(cmd_p
->ref_name
) + 1);
212 strcpy(p
, cmd_p
->ref_name
);
217 run_command_v_opt(argc
, argv
, RUN_COMMAND_NO_STDIO
);
221 * This gets called after(if) we've successfully
222 * unpacked the data payload.
224 static void execute_commands(void)
226 struct command
*cmd
= commands
;
232 run_update_post_hook(commands
);
235 static void read_head_info(void)
237 struct command
**p
= &commands
;
239 static char line
[1000];
240 unsigned char old_sha1
[20], new_sha1
[20];
245 len
= packet_read_line(0, line
, sizeof(line
));
248 if (line
[len
-1] == '\n')
253 get_sha1_hex(line
, old_sha1
) ||
254 get_sha1_hex(line
+ 41, new_sha1
))
255 die("protocol error: expected old/new/ref, got '%s'",
259 reflen
= strlen(refname
);
260 if (reflen
+ 82 < len
) {
261 if (strstr(refname
+ reflen
+ 1, "report-status"))
264 cmd
= xmalloc(sizeof(struct command
) + len
- 80);
265 hashcpy(cmd
->old_sha1
, old_sha1
);
266 hashcpy(cmd
->new_sha1
, new_sha1
);
267 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
268 cmd
->error_string
= "n/a (unpacker error)";
275 static const char *unpack(int *error_code
)
277 int code
= run_command_v_opt(1, unpacker
, RUN_GIT_CMD
);
283 case -ERR_RUN_COMMAND_FORK
:
284 return "unpack fork failed";
285 case -ERR_RUN_COMMAND_EXEC
:
286 return "unpack execute failed";
287 case -ERR_RUN_COMMAND_WAITPID
:
288 return "waitpid failed";
289 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
290 return "waitpid is confused";
291 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
292 return "unpacker died of signal";
293 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
294 return "unpacker died strangely";
297 return "unpacker exited with error code";
301 static void report(const char *unpack_status
)
304 packet_write(1, "unpack %s\n",
305 unpack_status
? unpack_status
: "ok");
306 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
307 if (!cmd
->error_string
)
308 packet_write(1, "ok %s\n",
311 packet_write(1, "ng %s %s\n",
312 cmd
->ref_name
, cmd
->error_string
);
317 int main(int argc
, char **argv
)
323 for (i
= 1; i
< argc
; i
++) {
327 /* Do flag handling here */
328 usage(receive_pack_usage
);
331 usage(receive_pack_usage
);
335 usage(receive_pack_usage
);
337 if(!enter_repo(dir
, 0))
338 die("'%s': unable to chdir or not a git archive", dir
);
348 const char *unpack_status
= unpack(&code
);
352 report(unpack_status
);