4 #include "run-command.h"
6 static const char receive_pack_usage
[] = "git-receive-pack <git-dir>";
8 static const char *unpacker
[] = { "unpack-objects", NULL
};
10 static int report_status
;
12 static char capabilities
[] = "report-status";
13 static int capabilities_sent
;
15 static int show_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
17 if (capabilities_sent
)
18 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
20 packet_write(1, "%s %s%c%s\n",
21 sha1_to_hex(sha1
), path
, 0, capabilities
);
22 capabilities_sent
= 1;
26 static void write_head_info(void)
28 for_each_ref(show_ref
, NULL
);
29 if (!capabilities_sent
)
30 show_ref("capabilities^{}", null_sha1
, 0, NULL
);
36 const char *error_string
;
37 unsigned char old_sha1
[20];
38 unsigned char new_sha1
[20];
39 char ref_name
[FLEX_ARRAY
]; /* more */
42 static struct command
*commands
;
44 static char update_hook
[] = "hooks/update";
46 static int run_update_hook(const char *refname
,
47 char *old_hex
, char *new_hex
)
51 if (access(update_hook
, X_OK
) < 0)
53 code
= run_command(update_hook
, refname
, old_hex
, new_hex
, NULL
);
57 case -ERR_RUN_COMMAND_FORK
:
58 return error("hook fork failed");
59 case -ERR_RUN_COMMAND_EXEC
:
60 return error("hook execute failed");
61 case -ERR_RUN_COMMAND_WAITPID
:
62 return error("waitpid failed");
63 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
64 return error("waitpid is confused");
65 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
66 return error("%s died of signal", update_hook
);
67 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
68 return error("%s died strangely", update_hook
);
70 error("%s exited with error code %d", update_hook
, -code
);
75 static int update(struct command
*cmd
)
77 const char *name
= cmd
->ref_name
;
78 unsigned char *old_sha1
= cmd
->old_sha1
;
79 unsigned char *new_sha1
= cmd
->new_sha1
;
80 char new_hex
[41], old_hex
[41];
81 struct ref_lock
*lock
;
83 cmd
->error_string
= NULL
;
84 if (!strncmp(name
, "refs/", 5) && check_ref_format(name
+ 5)) {
85 cmd
->error_string
= "funny refname";
86 return error("refusing to create funny ref '%s' locally",
90 strcpy(new_hex
, sha1_to_hex(new_sha1
));
91 strcpy(old_hex
, sha1_to_hex(old_sha1
));
92 if (!has_sha1_file(new_sha1
)) {
93 cmd
->error_string
= "bad pack";
94 return error("unpack should have generated %s, "
95 "but I can't find it!", new_hex
);
97 if (run_update_hook(name
, old_hex
, new_hex
)) {
98 cmd
->error_string
= "hook declined";
99 return error("hook declined to update %s", name
);
102 lock
= lock_any_ref_for_update(name
, old_sha1
);
104 cmd
->error_string
= "failed to lock";
105 return error("failed to lock %s", name
);
107 write_ref_sha1(lock
, new_sha1
, "push");
109 fprintf(stderr
, "%s: %s -> %s\n", name
, old_hex
, new_hex
);
113 static char update_post_hook
[] = "hooks/post-update";
115 static void run_update_post_hook(struct command
*cmd
)
117 struct command
*cmd_p
;
121 if (access(update_post_hook
, X_OK
) < 0)
123 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
124 if (cmd_p
->error_string
)
128 argv
= xmalloc(sizeof(*argv
) * (1 + argc
));
129 argv
[0] = update_post_hook
;
131 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
133 if (cmd_p
->error_string
)
135 p
= xmalloc(strlen(cmd_p
->ref_name
) + 1);
136 strcpy(p
, cmd_p
->ref_name
);
141 run_command_v_opt(argc
, argv
, RUN_COMMAND_NO_STDIO
);
145 * This gets called after(if) we've successfully
146 * unpacked the data payload.
148 static void execute_commands(void)
150 struct command
*cmd
= commands
;
156 run_update_post_hook(commands
);
159 static void read_head_info(void)
161 struct command
**p
= &commands
;
163 static char line
[1000];
164 unsigned char old_sha1
[20], new_sha1
[20];
169 len
= packet_read_line(0, line
, sizeof(line
));
172 if (line
[len
-1] == '\n')
177 get_sha1_hex(line
, old_sha1
) ||
178 get_sha1_hex(line
+ 41, new_sha1
))
179 die("protocol error: expected old/new/ref, got '%s'",
183 reflen
= strlen(refname
);
184 if (reflen
+ 82 < len
) {
185 if (strstr(refname
+ reflen
+ 1, "report-status"))
188 cmd
= xmalloc(sizeof(struct command
) + len
- 80);
189 hashcpy(cmd
->old_sha1
, old_sha1
);
190 hashcpy(cmd
->new_sha1
, new_sha1
);
191 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
192 cmd
->error_string
= "n/a (unpacker error)";
199 static const char *unpack(int *error_code
)
201 int code
= run_command_v_opt(1, unpacker
, RUN_GIT_CMD
);
207 case -ERR_RUN_COMMAND_FORK
:
208 return "unpack fork failed";
209 case -ERR_RUN_COMMAND_EXEC
:
210 return "unpack execute failed";
211 case -ERR_RUN_COMMAND_WAITPID
:
212 return "waitpid failed";
213 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
214 return "waitpid is confused";
215 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
216 return "unpacker died of signal";
217 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
218 return "unpacker died strangely";
221 return "unpacker exited with error code";
225 static void report(const char *unpack_status
)
228 packet_write(1, "unpack %s\n",
229 unpack_status
? unpack_status
: "ok");
230 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
231 if (!cmd
->error_string
)
232 packet_write(1, "ok %s\n",
235 packet_write(1, "ng %s %s\n",
236 cmd
->ref_name
, cmd
->error_string
);
241 int main(int argc
, char **argv
)
247 for (i
= 1; i
< argc
; i
++) {
251 /* Do flag handling here */
252 usage(receive_pack_usage
);
255 usage(receive_pack_usage
);
259 usage(receive_pack_usage
);
261 if (!enter_repo(dir
, 0))
262 die("'%s': unable to chdir or not a git archive", dir
);
264 git_config(git_default_config
);
274 const char *unpack_status
= unpack(&code
);
278 report(unpack_status
);