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
, *ent
;
136 old_commit
= (struct commit
*)parse_object(old_sha1
);
137 new_commit
= (struct commit
*)parse_object(new_sha1
);
138 bases
= get_merge_bases(old_commit
, new_commit
, 1);
139 for (ent
= bases
; ent
; ent
= ent
->next
)
140 if (!hashcmp(old_sha1
, ent
->item
->object
.sha1
))
142 free_commit_list(bases
);
144 return error("denying non-fast forward;"
145 " you should pull first");
147 safe_create_leading_directories(lock_name
);
149 newfd
= open(lock_name
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
151 cmd
->error_string
= "can't lock";
152 return error("unable to create %s (%s)",
153 lock_name
, strerror(errno
));
156 /* Write the ref with an ending '\n' */
159 written
= write(newfd
, new_hex
, 41);
160 /* Remove the '\n' again */
166 cmd
->error_string
= "can't write";
167 return error("unable to write %s", lock_name
);
169 if (verify_old_ref(name
, old_hex
) < 0) {
171 cmd
->error_string
= "raced";
172 return error("%s changed during push", name
);
174 if (run_update_hook(name
, old_hex
, new_hex
)) {
176 cmd
->error_string
= "hook declined";
177 return error("hook declined to update %s", name
);
179 else if (rename(lock_name
, name
) < 0) {
181 cmd
->error_string
= "can't rename";
182 return error("unable to replace %s", name
);
185 fprintf(stderr
, "%s: %s -> %s\n", name
, old_hex
, new_hex
);
190 static char update_post_hook
[] = "hooks/post-update";
192 static void run_update_post_hook(struct command
*cmd
)
194 struct command
*cmd_p
;
198 if (access(update_post_hook
, X_OK
) < 0)
200 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
201 if (cmd_p
->error_string
)
205 argv
= xmalloc(sizeof(*argv
) * (1 + argc
));
206 argv
[0] = update_post_hook
;
208 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
210 if (cmd_p
->error_string
)
212 p
= xmalloc(strlen(cmd_p
->ref_name
) + 1);
213 strcpy(p
, cmd_p
->ref_name
);
218 run_command_v_opt(argc
, argv
, RUN_COMMAND_NO_STDIO
);
222 * This gets called after(if) we've successfully
223 * unpacked the data payload.
225 static void execute_commands(void)
227 struct command
*cmd
= commands
;
233 run_update_post_hook(commands
);
236 static void read_head_info(void)
238 struct command
**p
= &commands
;
240 static char line
[1000];
241 unsigned char old_sha1
[20], new_sha1
[20];
246 len
= packet_read_line(0, line
, sizeof(line
));
249 if (line
[len
-1] == '\n')
254 get_sha1_hex(line
, old_sha1
) ||
255 get_sha1_hex(line
+ 41, new_sha1
))
256 die("protocol error: expected old/new/ref, got '%s'",
260 reflen
= strlen(refname
);
261 if (reflen
+ 82 < len
) {
262 if (strstr(refname
+ reflen
+ 1, "report-status"))
265 cmd
= xmalloc(sizeof(struct command
) + len
- 80);
266 hashcpy(cmd
->old_sha1
, old_sha1
);
267 hashcpy(cmd
->new_sha1
, new_sha1
);
268 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
269 cmd
->error_string
= "n/a (unpacker error)";
276 static const char *unpack(int *error_code
)
278 int code
= run_command_v_opt(1, unpacker
, RUN_GIT_CMD
);
284 case -ERR_RUN_COMMAND_FORK
:
285 return "unpack fork failed";
286 case -ERR_RUN_COMMAND_EXEC
:
287 return "unpack execute failed";
288 case -ERR_RUN_COMMAND_WAITPID
:
289 return "waitpid failed";
290 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
291 return "waitpid is confused";
292 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
293 return "unpacker died of signal";
294 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
295 return "unpacker died strangely";
298 return "unpacker exited with error code";
302 static void report(const char *unpack_status
)
305 packet_write(1, "unpack %s\n",
306 unpack_status
? unpack_status
: "ok");
307 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
308 if (!cmd
->error_string
)
309 packet_write(1, "ok %s\n",
312 packet_write(1, "ng %s %s\n",
313 cmd
->ref_name
, cmd
->error_string
);
318 int main(int argc
, char **argv
)
324 for (i
= 1; i
< argc
; i
++) {
328 /* Do flag handling here */
329 usage(receive_pack_usage
);
332 usage(receive_pack_usage
);
336 usage(receive_pack_usage
);
338 if(!enter_repo(dir
, 0))
339 die("'%s': unable to chdir or not a git archive", dir
);
349 const char *unpack_status
= unpack(&code
);
353 report(unpack_status
);