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 deny_non_fast_forwards
= 0;
13 static int report_status
;
15 static char capabilities
[] = "report-status";
16 static int capabilities_sent
;
18 static int receive_pack_config(const char *var
, const char *value
)
20 git_default_config(var
, value
);
22 if (strcmp(var
, "receive.denynonfastforwards") == 0)
24 deny_non_fast_forwards
= git_config_bool(var
, value
);
31 static int show_ref(const char *path
, const unsigned char *sha1
, int flag
, void *cb_data
)
33 if (capabilities_sent
)
34 packet_write(1, "%s %s\n", sha1_to_hex(sha1
), path
);
36 packet_write(1, "%s %s%c%s\n",
37 sha1_to_hex(sha1
), path
, 0, capabilities
);
38 capabilities_sent
= 1;
42 static void write_head_info(void)
44 for_each_ref(show_ref
, NULL
);
45 if (!capabilities_sent
)
46 show_ref("capabilities^{}", null_sha1
, 0, NULL
);
52 const char *error_string
;
53 unsigned char old_sha1
[20];
54 unsigned char new_sha1
[20];
55 char ref_name
[FLEX_ARRAY
]; /* more */
58 static struct command
*commands
;
60 static char update_hook
[] = "hooks/update";
62 static int run_update_hook(const char *refname
,
63 char *old_hex
, char *new_hex
)
67 if (access(update_hook
, X_OK
) < 0)
69 code
= run_command(update_hook
, refname
, old_hex
, new_hex
, NULL
);
73 case -ERR_RUN_COMMAND_FORK
:
74 return error("hook fork failed");
75 case -ERR_RUN_COMMAND_EXEC
:
76 return error("hook execute failed");
77 case -ERR_RUN_COMMAND_WAITPID
:
78 return error("waitpid failed");
79 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
80 return error("waitpid is confused");
81 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
82 return error("%s died of signal", update_hook
);
83 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
84 return error("%s died strangely", update_hook
);
86 error("%s exited with error code %d", update_hook
, -code
);
91 static int update(struct command
*cmd
)
93 const char *name
= cmd
->ref_name
;
94 unsigned char *old_sha1
= cmd
->old_sha1
;
95 unsigned char *new_sha1
= cmd
->new_sha1
;
96 char new_hex
[41], old_hex
[41];
97 struct ref_lock
*lock
;
99 cmd
->error_string
= NULL
;
100 if (!strncmp(name
, "refs/", 5) && check_ref_format(name
+ 5)) {
101 cmd
->error_string
= "funny refname";
102 return error("refusing to create funny ref '%s' locally",
106 strcpy(new_hex
, sha1_to_hex(new_sha1
));
107 strcpy(old_hex
, sha1_to_hex(old_sha1
));
108 if (!has_sha1_file(new_sha1
)) {
109 cmd
->error_string
= "bad pack";
110 return error("unpack should have generated %s, "
111 "but I can't find it!", new_hex
);
113 if (deny_non_fast_forwards
&& !is_null_sha1(old_sha1
)) {
114 struct commit
*old_commit
, *new_commit
;
115 struct commit_list
*bases
, *ent
;
117 old_commit
= (struct commit
*)parse_object(old_sha1
);
118 new_commit
= (struct commit
*)parse_object(new_sha1
);
119 bases
= get_merge_bases(old_commit
, new_commit
, 1);
120 for (ent
= bases
; ent
; ent
= ent
->next
)
121 if (!hashcmp(old_sha1
, ent
->item
->object
.sha1
))
123 free_commit_list(bases
);
125 return error("denying non-fast forward;"
126 " you should pull first");
128 if (run_update_hook(name
, old_hex
, new_hex
)) {
129 cmd
->error_string
= "hook declined";
130 return error("hook declined to update %s", name
);
133 lock
= lock_any_ref_for_update(name
, old_sha1
);
135 cmd
->error_string
= "failed to lock";
136 return error("failed to lock %s", name
);
138 write_ref_sha1(lock
, new_sha1
, "push");
140 fprintf(stderr
, "%s: %s -> %s\n", name
, old_hex
, new_hex
);
144 static char update_post_hook
[] = "hooks/post-update";
146 static void run_update_post_hook(struct command
*cmd
)
148 struct command
*cmd_p
;
152 if (access(update_post_hook
, X_OK
) < 0)
154 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
155 if (cmd_p
->error_string
)
159 argv
= xmalloc(sizeof(*argv
) * (1 + argc
));
160 argv
[0] = update_post_hook
;
162 for (argc
= 1, cmd_p
= cmd
; cmd_p
; cmd_p
= cmd_p
->next
) {
164 if (cmd_p
->error_string
)
166 p
= xmalloc(strlen(cmd_p
->ref_name
) + 1);
167 strcpy(p
, cmd_p
->ref_name
);
172 run_command_v_opt(argc
, argv
, RUN_COMMAND_NO_STDIO
);
176 * This gets called after(if) we've successfully
177 * unpacked the data payload.
179 static void execute_commands(void)
181 struct command
*cmd
= commands
;
187 run_update_post_hook(commands
);
190 static void read_head_info(void)
192 struct command
**p
= &commands
;
194 static char line
[1000];
195 unsigned char old_sha1
[20], new_sha1
[20];
200 len
= packet_read_line(0, line
, sizeof(line
));
203 if (line
[len
-1] == '\n')
208 get_sha1_hex(line
, old_sha1
) ||
209 get_sha1_hex(line
+ 41, new_sha1
))
210 die("protocol error: expected old/new/ref, got '%s'",
214 reflen
= strlen(refname
);
215 if (reflen
+ 82 < len
) {
216 if (strstr(refname
+ reflen
+ 1, "report-status"))
219 cmd
= xmalloc(sizeof(struct command
) + len
- 80);
220 hashcpy(cmd
->old_sha1
, old_sha1
);
221 hashcpy(cmd
->new_sha1
, new_sha1
);
222 memcpy(cmd
->ref_name
, line
+ 82, len
- 81);
223 cmd
->error_string
= "n/a (unpacker error)";
230 static const char *unpack(int *error_code
)
232 int code
= run_command_v_opt(1, unpacker
, RUN_GIT_CMD
);
238 case -ERR_RUN_COMMAND_FORK
:
239 return "unpack fork failed";
240 case -ERR_RUN_COMMAND_EXEC
:
241 return "unpack execute failed";
242 case -ERR_RUN_COMMAND_WAITPID
:
243 return "waitpid failed";
244 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
245 return "waitpid is confused";
246 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
247 return "unpacker died of signal";
248 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
249 return "unpacker died strangely";
252 return "unpacker exited with error code";
256 static void report(const char *unpack_status
)
259 packet_write(1, "unpack %s\n",
260 unpack_status
? unpack_status
: "ok");
261 for (cmd
= commands
; cmd
; cmd
= cmd
->next
) {
262 if (!cmd
->error_string
)
263 packet_write(1, "ok %s\n",
266 packet_write(1, "ng %s %s\n",
267 cmd
->ref_name
, cmd
->error_string
);
272 int main(int argc
, char **argv
)
278 for (i
= 1; i
< argc
; i
++) {
282 /* Do flag handling here */
283 usage(receive_pack_usage
);
286 usage(receive_pack_usage
);
290 usage(receive_pack_usage
);
292 if (!enter_repo(dir
, 0))
293 die("'%s': unable to chdir or not a git archive", dir
);
296 git_config(receive_pack_config
);
306 const char *unpack_status
= unpack(&code
);
310 report(unpack_status
);