4 #include "run-command.h"
7 static const char receive_pack_usage
[] = "git-receive-pack <git-dir>";
9 static const char *unpacker
[] = { "unpack-objects", NULL
};
11 static int report_status
= 0;
13 static char capabilities
[] = "report-status";
14 static int capabilities_sent
= 0;
16 static int show_ref(const char *path
, const unsigned char *sha1
)
18 if (capabilities_sent
)
19 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
21 packet_write(1, "%s %s%c%s\n",
22 sha1_to_hex(sha1
), path
, 0, capabilities
);
23 capabilities_sent
= 1;
27 static void write_head_info(void)
29 for_each_ref(show_ref
);
30 if (!capabilities_sent
)
31 show_ref("capabilities^{}", null_sha1
);
37 const char *error_string
;
38 unsigned char old_sha1
[20];
39 unsigned char new_sha1
[20];
40 char ref_name
[FLEX_ARRAY
]; /* more */
43 static struct command
*commands
= NULL
;
45 static int is_all_zeroes(const char *hex
)
48 for (i
= 0; i
< 40; i
++)
54 static int verify_old_ref(const char *name
, char *hex_contents
)
59 if (is_all_zeroes(hex_contents
))
61 fd
= open(name
, O_RDONLY
);
64 ret
= read(fd
, buffer
, 40);
68 if (memcmp(buffer
, hex_contents
, 40))
73 static char update_hook
[] = "hooks/update";
75 static int run_update_hook(const char *refname
,
76 char *old_hex
, char *new_hex
)
80 if (access(update_hook
, X_OK
) < 0)
82 code
= run_command(update_hook
, refname
, old_hex
, new_hex
, NULL
);
86 case -ERR_RUN_COMMAND_FORK
:
87 return error("hook fork failed");
88 case -ERR_RUN_COMMAND_EXEC
:
89 return error("hook execute failed");
90 case -ERR_RUN_COMMAND_WAITPID
:
91 return error("waitpid failed");
92 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
93 return error("waitpid is confused");
94 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
95 return error("%s died of signal", update_hook
);
96 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
97 return error("%s died strangely", update_hook
);
99 error("%s exited with error code %d", update_hook
, -code
);
104 static int update(struct command
*cmd
)
106 const char *name
= cmd
->ref_name
;
107 unsigned char *old_sha1
= cmd
->old_sha1
;
108 unsigned char *new_sha1
= cmd
->new_sha1
;
109 char new_hex
[60], *old_hex
, *lock_name
;
110 int newfd
, namelen
, written
;
112 cmd
->error_string
= NULL
;
113 if (!strncmp(name
, "refs/", 5) && check_ref_format(name
+ 5)) {
114 cmd
->error_string
= "funny refname";
115 return error("refusing to create funny ref '%s' locally",
119 namelen
= strlen(name
);
120 lock_name
= xmalloc(namelen
+ 10);
121 memcpy(lock_name
, name
, namelen
);
122 memcpy(lock_name
+ namelen
, ".lock", 6);
124 strcpy(new_hex
, sha1_to_hex(new_sha1
));
125 old_hex
= sha1_to_hex(old_sha1
);
126 if (!has_sha1_file(new_sha1
)) {
127 cmd
->error_string
= "bad pack";
128 return error("unpack should have generated %s, "
129 "but I can't find it!", new_hex
);
131 safe_create_leading_directories(lock_name
);
133 newfd
= open(lock_name
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
135 cmd
->error_string
= "can't lock";
136 return error("unable to create %s (%s)",
137 lock_name
, strerror(errno
));
140 /* Write the ref with an ending '\n' */
143 written
= write(newfd
, new_hex
, 41);
144 /* Remove the '\n' again */
150 cmd
->error_string
= "can't write";
151 return error("unable to write %s", lock_name
);
153 if (verify_old_ref(name
, old_hex
) < 0) {
155 cmd
->error_string
= "raced";
156 return error("%s changed during push", name
);
158 if (run_update_hook(name
, old_hex
, new_hex
)) {
160 cmd
->error_string
= "hook declined";
161 return error("hook declined to update %s", name
);
163 else if (rename(lock_name
, name
) < 0) {
165 cmd
->error_string
= "can't rename";
166 return error("unable to replace %s", name
);
169 fprintf(stderr
, "%s: %s -> %s\n", name
, old_hex
, new_hex
);
174 static char update_post_hook
[] = "hooks/post-update";
176 static void run_update_post_hook(struct command
*cmd
)
178 struct command
*cmd_p
;
182 if (access(update_post_hook
, X_OK
) < 0)
184 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
185 if (cmd_p
->error_string
)
189 argv
= xmalloc(sizeof(*argv
) * (1 + argc
));
190 argv
[0] = update_post_hook
;
192 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
194 if (cmd_p
->error_string
)
196 p
= xmalloc(strlen(cmd_p
->ref_name
) + 1);
197 strcpy(p
, cmd_p
->ref_name
);
202 run_command_v_opt(argc
, argv
, RUN_COMMAND_NO_STDIO
);
206 * This gets called after(if) we've successfully
207 * unpacked the data payload.
209 static void execute_commands(void)
211 struct command
*cmd
= commands
;
217 run_update_post_hook(commands
);
220 static void read_head_info(void)
222 struct command
**p
= &commands
;
224 static char line
[1000];
225 unsigned char old_sha1
[20], new_sha1
[20];
230 len
= packet_read_line(0, line
, sizeof(line
));
233 if (line
[len
-1] == '\n')
238 get_sha1_hex(line
, old_sha1
) ||
239 get_sha1_hex(line
+ 41, new_sha1
))
240 die("protocol error: expected old/new/ref, got '%s'",
244 reflen
= strlen(refname
);
245 if (reflen
+ 82 < len
) {
246 if (strstr(refname
+ reflen
+ 1, "report-status"))
249 cmd
= xmalloc(sizeof(struct command
) + len
- 80);
250 memcpy(cmd
->old_sha1
, old_sha1
, 20);
251 memcpy(cmd
->new_sha1
, new_sha1
, 20);
252 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
253 cmd
->error_string
= "n/a (unpacker error)";
260 static const char *unpack(int *error_code
)
262 int code
= run_command_v_opt(1, unpacker
, RUN_GIT_CMD
);
268 case -ERR_RUN_COMMAND_FORK
:
269 return "unpack fork failed";
270 case -ERR_RUN_COMMAND_EXEC
:
271 return "unpack execute failed";
272 case -ERR_RUN_COMMAND_WAITPID
:
273 return "waitpid failed";
274 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
275 return "waitpid is confused";
276 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
277 return "unpacker died of signal";
278 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
279 return "unpacker died strangely";
282 return "unpacker exited with error code";
286 static void report(const char *unpack_status
)
289 packet_write(1, "unpack %s\n",
290 unpack_status
? unpack_status
: "ok");
291 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
292 if (!cmd
->error_string
)
293 packet_write(1, "ok %s\n",
296 packet_write(1, "ng %s %s\n",
297 cmd
->ref_name
, cmd
->error_string
);
302 int main(int argc
, char **argv
)
308 for (i
= 1; i
< argc
; i
++) {
312 /* Do flag handling here */
313 usage(receive_pack_usage
);
316 usage(receive_pack_usage
);
320 usage(receive_pack_usage
);
322 if(!enter_repo(dir
, 0))
323 die("'%s': unable to chdir or not a git archive", dir
);
333 const char *unpack_status
= unpack(&code
);
337 report(unpack_status
);