]> git.ipfire.org Git - thirdparty/git.git/blame - receive-pack.c
Add pack-refs and show-ref test cases.
[thirdparty/git.git] / receive-pack.c
CommitLineData
575f4974 1#include "cache.h"
8a65ff76 2#include "refs.h"
f3a3214e 3#include "pkt-line.h"
b1bf95bb 4#include "run-command.h"
11031d7e
JS
5#include "commit.h"
6#include "object.h"
575f4974 7
d0efc8a7 8static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
575f4974 9
9201c707 10static const char *unpacker[] = { "unpack-objects", NULL };
575f4974 11
96f1e58f 12static int report_status;
cfee10a7
JH
13
14static char capabilities[] = "report-status";
96f1e58f 15static int capabilities_sent;
cfee10a7 16
8da19775 17static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
575f4974 18{
cfee10a7
JH
19 if (capabilities_sent)
20 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
21 else
22 packet_write(1, "%s %s%c%s\n",
23 sha1_to_hex(sha1), path, 0, capabilities);
24 capabilities_sent = 1;
8a65ff76 25 return 0;
575f4974
LT
26}
27
8a65ff76 28static void write_head_info(void)
575f4974 29{
cb5d709f 30 for_each_ref(show_ref, NULL);
cfee10a7 31 if (!capabilities_sent)
8da19775 32 show_ref("capabilities^{}", null_sha1, 0, NULL);
cfee10a7 33
575f4974
LT
34}
35
eb1af2df
LT
36struct command {
37 struct command *next;
cfee10a7 38 const char *error_string;
eb1af2df
LT
39 unsigned char old_sha1[20];
40 unsigned char new_sha1[20];
8f1d2e6f 41 char ref_name[FLEX_ARRAY]; /* more */
575f4974
LT
42};
43
96f1e58f 44static struct command *commands;
575f4974 45
b1bf95bb
JW
46static char update_hook[] = "hooks/update";
47
48static int run_update_hook(const char *refname,
49 char *old_hex, char *new_hex)
50{
51 int code;
52
53 if (access(update_hook, X_OK) < 0)
54 return 0;
55 code = run_command(update_hook, refname, old_hex, new_hex, NULL);
56 switch (code) {
57 case 0:
58 return 0;
59 case -ERR_RUN_COMMAND_FORK:
cfee10a7 60 return error("hook fork failed");
b1bf95bb 61 case -ERR_RUN_COMMAND_EXEC:
cfee10a7 62 return error("hook execute failed");
b1bf95bb 63 case -ERR_RUN_COMMAND_WAITPID:
cfee10a7 64 return error("waitpid failed");
b1bf95bb 65 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
cfee10a7 66 return error("waitpid is confused");
b1bf95bb 67 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
bd2afde8 68 return error("%s died of signal", update_hook);
b1bf95bb 69 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
cfee10a7 70 return error("%s died strangely", update_hook);
b1bf95bb
JW
71 default:
72 error("%s exited with error code %d", update_hook, -code);
73 return -code;
74 }
75}
76
cfee10a7 77static int update(struct command *cmd)
2eca23da 78{
cfee10a7
JH
79 const char *name = cmd->ref_name;
80 unsigned char *old_sha1 = cmd->old_sha1;
81 unsigned char *new_sha1 = cmd->new_sha1;
3159c8dc
JH
82 char new_hex[41], old_hex[41];
83 struct ref_lock *lock;
2eca23da 84
cfee10a7
JH
85 cmd->error_string = NULL;
86 if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5)) {
87 cmd->error_string = "funny refname";
d8a1deec
JH
88 return error("refusing to create funny ref '%s' locally",
89 name);
cfee10a7 90 }
d8a1deec 91
2eca23da 92 strcpy(new_hex, sha1_to_hex(new_sha1));
3159c8dc 93 strcpy(old_hex, sha1_to_hex(old_sha1));
cfee10a7
JH
94 if (!has_sha1_file(new_sha1)) {
95 cmd->error_string = "bad pack";
19614330
JH
96 return error("unpack should have generated %s, "
97 "but I can't find it!", new_hex);
cfee10a7 98 }
11031d7e
JS
99 if (deny_non_fast_forwards && !is_null_sha1(old_sha1)) {
100 struct commit *old_commit, *new_commit;
9edd7e46 101 struct commit_list *bases, *ent;
11031d7e
JS
102
103 old_commit = (struct commit *)parse_object(old_sha1);
104 new_commit = (struct commit *)parse_object(new_sha1);
9edd7e46
JS
105 bases = get_merge_bases(old_commit, new_commit, 1);
106 for (ent = bases; ent; ent = ent->next)
107 if (!hashcmp(old_sha1, ent->item->object.sha1))
11031d7e 108 break;
9edd7e46
JS
109 free_commit_list(bases);
110 if (!ent)
11031d7e 111 return error("denying non-fast forward;"
9edd7e46 112 " you should pull first");
11031d7e 113 }
b1bf95bb 114 if (run_update_hook(name, old_hex, new_hex)) {
cfee10a7 115 cmd->error_string = "hook declined";
bd2afde8 116 return error("hook declined to update %s", name);
b1bf95bb 117 }
3159c8dc
JH
118
119 lock = lock_any_ref_for_update(name, old_sha1);
120 if (!lock) {
121 cmd->error_string = "failed to lock";
122 return error("failed to lock %s", name);
19614330 123 }
3159c8dc
JH
124 write_ref_sha1(lock, new_sha1, "push");
125
126 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
127 return 0;
2eca23da
LT
128}
129
19614330
JH
130static char update_post_hook[] = "hooks/post-update";
131
132static void run_update_post_hook(struct command *cmd)
133{
134 struct command *cmd_p;
135 int argc;
9201c707 136 const char **argv;
19614330
JH
137
138 if (access(update_post_hook, X_OK) < 0)
139 return;
140 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
cfee10a7 141 if (cmd_p->error_string)
19614330
JH
142 continue;
143 argc++;
144 }
145 argv = xmalloc(sizeof(*argv) * (1 + argc));
146 argv[0] = update_post_hook;
147
148 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
9201c707 149 char *p;
cfee10a7 150 if (cmd_p->error_string)
19614330 151 continue;
9201c707
JH
152 p = xmalloc(strlen(cmd_p->ref_name) + 1);
153 strcpy(p, cmd_p->ref_name);
154 argv[argc] = p;
19614330
JH
155 argc++;
156 }
157 argv[argc] = NULL;
128aed68 158 run_command_v_opt(argc, argv, RUN_COMMAND_NO_STDIO);
19614330 159}
2eca23da 160
575f4974
LT
161/*
162 * This gets called after(if) we've successfully
163 * unpacked the data payload.
164 */
165static void execute_commands(void)
166{
eb1af2df
LT
167 struct command *cmd = commands;
168
169 while (cmd) {
cfee10a7 170 update(cmd);
eb1af2df 171 cmd = cmd->next;
575f4974 172 }
19614330 173 run_update_post_hook(commands);
575f4974
LT
174}
175
176static void read_head_info(void)
177{
eb1af2df 178 struct command **p = &commands;
575f4974
LT
179 for (;;) {
180 static char line[1000];
eb1af2df
LT
181 unsigned char old_sha1[20], new_sha1[20];
182 struct command *cmd;
cfee10a7
JH
183 char *refname;
184 int len, reflen;
eb1af2df
LT
185
186 len = packet_read_line(0, line, sizeof(line));
575f4974
LT
187 if (!len)
188 break;
eb1af2df
LT
189 if (line[len-1] == '\n')
190 line[--len] = 0;
191 if (len < 83 ||
192 line[40] != ' ' ||
193 line[81] != ' ' ||
194 get_sha1_hex(line, old_sha1) ||
195 get_sha1_hex(line + 41, new_sha1))
cfee10a7
JH
196 die("protocol error: expected old/new/ref, got '%s'",
197 line);
198
199 refname = line + 82;
200 reflen = strlen(refname);
201 if (reflen + 82 < len) {
202 if (strstr(refname + reflen + 1, "report-status"))
203 report_status = 1;
204 }
eb1af2df 205 cmd = xmalloc(sizeof(struct command) + len - 80);
e702496e
SP
206 hashcpy(cmd->old_sha1, old_sha1);
207 hashcpy(cmd->new_sha1, new_sha1);
eb1af2df 208 memcpy(cmd->ref_name, line + 82, len - 81);
cfee10a7 209 cmd->error_string = "n/a (unpacker error)";
eb1af2df
LT
210 cmd->next = NULL;
211 *p = cmd;
212 p = &cmd->next;
575f4974
LT
213 }
214}
215
cfee10a7 216static const char *unpack(int *error_code)
575f4974 217{
77cb17e9 218 int code = run_command_v_opt(1, unpacker, RUN_GIT_CMD);
cfee10a7
JH
219
220 *error_code = 0;
b1bf95bb
JW
221 switch (code) {
222 case 0:
cfee10a7 223 return NULL;
b1bf95bb 224 case -ERR_RUN_COMMAND_FORK:
cfee10a7 225 return "unpack fork failed";
b1bf95bb 226 case -ERR_RUN_COMMAND_EXEC:
cfee10a7 227 return "unpack execute failed";
b1bf95bb 228 case -ERR_RUN_COMMAND_WAITPID:
cfee10a7 229 return "waitpid failed";
b1bf95bb 230 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
cfee10a7 231 return "waitpid is confused";
b1bf95bb 232 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
cfee10a7 233 return "unpacker died of signal";
b1bf95bb 234 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
cfee10a7 235 return "unpacker died strangely";
b1bf95bb 236 default:
cfee10a7
JH
237 *error_code = -code;
238 return "unpacker exited with error code";
239 }
240}
241
242static void report(const char *unpack_status)
243{
244 struct command *cmd;
245 packet_write(1, "unpack %s\n",
246 unpack_status ? unpack_status : "ok");
247 for (cmd = commands; cmd; cmd = cmd->next) {
248 if (!cmd->error_string)
249 packet_write(1, "ok %s\n",
250 cmd->ref_name);
251 else
252 packet_write(1, "ng %s %s\n",
253 cmd->ref_name, cmd->error_string);
575f4974 254 }
cfee10a7 255 packet_flush(1);
575f4974
LT
256}
257
258int main(int argc, char **argv)
259{
d0efc8a7 260 int i;
8d630132 261 char *dir = NULL;
575f4974
LT
262
263 argv++;
264 for (i = 1; i < argc; i++) {
8d630132 265 char *arg = *argv++;
575f4974
LT
266
267 if (*arg == '-') {
575f4974
LT
268 /* Do flag handling here */
269 usage(receive_pack_usage);
270 }
d0efc8a7
LT
271 if (dir)
272 usage(receive_pack_usage);
575f4974 273 dir = arg;
575f4974
LT
274 }
275 if (!dir)
276 usage(receive_pack_usage);
277
3159c8dc 278 if (!enter_repo(dir, 0))
8d630132 279 die("'%s': unable to chdir or not a git archive", dir);
575f4974 280
94d8213f 281 setup_ident();
3159c8dc
JH
282 git_config(git_default_config);
283
8a65ff76 284 write_head_info();
575f4974
LT
285
286 /* EOF */
f3a3214e 287 packet_flush(1);
575f4974
LT
288
289 read_head_info();
7f8e9828 290 if (commands) {
cfee10a7
JH
291 int code;
292 const char *unpack_status = unpack(&code);
293 if (!unpack_status)
294 execute_commands();
295 if (report_status)
296 report(unpack_status);
7f8e9828 297 }
575f4974
LT
298 return 0;
299}