]> git.ipfire.org Git - thirdparty/git.git/blame - receive-pack.c
git-describe: use find_unique_abbrev()
[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"
575f4974
LT
5#include <sys/wait.h>
6
d0efc8a7 7static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
575f4974 8
b1bf95bb 9static const char unpacker[] = "git-unpack-objects";
575f4974 10
944d8589 11static int show_ref(const char *path, const unsigned char *sha1)
575f4974 12{
f3a3214e 13 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
8a65ff76 14 return 0;
575f4974
LT
15}
16
8a65ff76 17static void write_head_info(void)
575f4974 18{
8a65ff76 19 for_each_ref(show_ref);
575f4974
LT
20}
21
eb1af2df
LT
22struct command {
23 struct command *next;
19614330 24 unsigned char updated;
eb1af2df
LT
25 unsigned char old_sha1[20];
26 unsigned char new_sha1[20];
2c04662d 27 char ref_name[0];
575f4974
LT
28};
29
6da4016a 30static struct command *commands = NULL;
575f4974 31
f65fdf04
LT
32static int is_all_zeroes(const char *hex)
33{
34 int i;
35 for (i = 0; i < 40; i++)
36 if (*hex++ != '0')
37 return 0;
38 return 1;
39}
40
2eca23da
LT
41static int verify_old_ref(const char *name, char *hex_contents)
42{
43 int fd, ret;
44 char buffer[60];
45
f65fdf04
LT
46 if (is_all_zeroes(hex_contents))
47 return 0;
2eca23da
LT
48 fd = open(name, O_RDONLY);
49 if (fd < 0)
50 return -1;
51 ret = read(fd, buffer, 40);
52 close(fd);
53 if (ret != 40)
54 return -1;
55 if (memcmp(buffer, hex_contents, 40))
56 return -1;
57 return 0;
58}
59
b1bf95bb
JW
60static char update_hook[] = "hooks/update";
61
62static int run_update_hook(const char *refname,
63 char *old_hex, char *new_hex)
64{
65 int code;
66
67 if (access(update_hook, X_OK) < 0)
68 return 0;
69 code = run_command(update_hook, refname, old_hex, new_hex, NULL);
70 switch (code) {
71 case 0:
72 return 0;
73 case -ERR_RUN_COMMAND_FORK:
74 die("hook fork failed");
75 case -ERR_RUN_COMMAND_EXEC:
76 die("hook execute failed");
77 case -ERR_RUN_COMMAND_WAITPID:
78 die("waitpid failed");
79 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
80 die("waitpid is confused");
81 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
7246ed43 82 fprintf(stderr, "%s died of signal\n", update_hook);
b1bf95bb
JW
83 return -1;
84 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
85 die("%s died strangely", update_hook);
86 default:
87 error("%s exited with error code %d", update_hook, -code);
88 return -code;
89 }
90}
91
19614330
JH
92static int update(const char *name,
93 unsigned char *old_sha1, unsigned char *new_sha1)
2eca23da
LT
94{
95 char new_hex[60], *old_hex, *lock_name;
96 int newfd, namelen, written;
97
d8a1deec
JH
98 if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5))
99 return error("refusing to create funny ref '%s' locally",
100 name);
101
2eca23da
LT
102 namelen = strlen(name);
103 lock_name = xmalloc(namelen + 10);
104 memcpy(lock_name, name, namelen);
105 memcpy(lock_name + namelen, ".lock", 6);
106
107 strcpy(new_hex, sha1_to_hex(new_sha1));
2eca23da
LT
108 old_hex = sha1_to_hex(old_sha1);
109 if (!has_sha1_file(new_sha1))
19614330
JH
110 return error("unpack should have generated %s, "
111 "but I can't find it!", new_hex);
2eca23da 112
29f3b3de
JH
113 safe_create_leading_directories(lock_name);
114
f312de01 115 newfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);
2eca23da 116 if (newfd < 0)
19614330
JH
117 return error("unable to create %s (%s)",
118 lock_name, strerror(errno));
f65fdf04
LT
119
120 /* Write the ref with an ending '\n' */
121 new_hex[40] = '\n';
122 new_hex[41] = 0;
2eca23da 123 written = write(newfd, new_hex, 41);
f65fdf04
LT
124 /* Remove the '\n' again */
125 new_hex[40] = 0;
126
2eca23da
LT
127 close(newfd);
128 if (written != 41) {
129 unlink(lock_name);
19614330 130 return error("unable to write %s", lock_name);
2eca23da
LT
131 }
132 if (verify_old_ref(name, old_hex) < 0) {
133 unlink(lock_name);
19614330 134 return error("%s changed during push", name);
2eca23da 135 }
b1bf95bb
JW
136 if (run_update_hook(name, old_hex, new_hex)) {
137 unlink(lock_name);
19614330 138 return error("hook declined to update %s\n", name);
b1bf95bb
JW
139 }
140 else if (rename(lock_name, name) < 0) {
2eca23da 141 unlink(lock_name);
19614330 142 return error("unable to replace %s", name);
2eca23da 143 }
19614330 144 else {
b1bf95bb 145 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
19614330
JH
146 return 0;
147 }
2eca23da
LT
148}
149
19614330
JH
150static char update_post_hook[] = "hooks/post-update";
151
152static void run_update_post_hook(struct command *cmd)
153{
154 struct command *cmd_p;
155 int argc;
156 char **argv;
157
158 if (access(update_post_hook, X_OK) < 0)
159 return;
160 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
161 if (!cmd_p->updated)
162 continue;
163 argc++;
164 }
165 argv = xmalloc(sizeof(*argv) * (1 + argc));
166 argv[0] = update_post_hook;
167
168 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
169 if (!cmd_p->updated)
170 continue;
171 argv[argc] = xmalloc(strlen(cmd_p->ref_name) + 1);
172 strcpy(argv[argc], cmd_p->ref_name);
173 argc++;
174 }
175 argv[argc] = NULL;
128aed68 176 run_command_v_opt(argc, argv, RUN_COMMAND_NO_STDIO);
19614330 177}
2eca23da 178
575f4974
LT
179/*
180 * This gets called after(if) we've successfully
181 * unpacked the data payload.
182 */
183static void execute_commands(void)
184{
eb1af2df
LT
185 struct command *cmd = commands;
186
187 while (cmd) {
19614330
JH
188 cmd->updated = !update(cmd->ref_name,
189 cmd->old_sha1, cmd->new_sha1);
eb1af2df 190 cmd = cmd->next;
575f4974 191 }
19614330 192 run_update_post_hook(commands);
575f4974
LT
193}
194
195static void read_head_info(void)
196{
eb1af2df 197 struct command **p = &commands;
575f4974
LT
198 for (;;) {
199 static char line[1000];
eb1af2df
LT
200 unsigned char old_sha1[20], new_sha1[20];
201 struct command *cmd;
202 int len;
203
204 len = packet_read_line(0, line, sizeof(line));
575f4974
LT
205 if (!len)
206 break;
eb1af2df
LT
207 if (line[len-1] == '\n')
208 line[--len] = 0;
209 if (len < 83 ||
210 line[40] != ' ' ||
211 line[81] != ' ' ||
212 get_sha1_hex(line, old_sha1) ||
213 get_sha1_hex(line + 41, new_sha1))
214 die("protocol error: expected old/new/ref, got '%s'", line);
215 cmd = xmalloc(sizeof(struct command) + len - 80);
216 memcpy(cmd->old_sha1, old_sha1, 20);
217 memcpy(cmd->new_sha1, new_sha1, 20);
218 memcpy(cmd->ref_name, line + 82, len - 81);
219 cmd->next = NULL;
220 *p = cmd;
221 p = &cmd->next;
575f4974
LT
222 }
223}
224
225static void unpack(void)
226{
b1bf95bb
JW
227 int code = run_command(unpacker, NULL);
228 switch (code) {
229 case 0:
c742b813 230 return;
b1bf95bb 231 case -ERR_RUN_COMMAND_FORK:
575f4974 232 die("unpack fork failed");
b1bf95bb 233 case -ERR_RUN_COMMAND_EXEC:
575f4974 234 die("unpack execute failed");
b1bf95bb
JW
235 case -ERR_RUN_COMMAND_WAITPID:
236 die("waitpid failed");
237 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
238 die("waitpid is confused");
239 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
240 die("%s died of signal", unpacker);
241 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
242 die("%s died strangely", unpacker);
243 default:
244 die("%s exited with error code %d", unpacker, -code);
575f4974
LT
245 }
246}
247
248int main(int argc, char **argv)
249{
d0efc8a7 250 int i;
8d630132 251 char *dir = NULL;
575f4974
LT
252
253 argv++;
254 for (i = 1; i < argc; i++) {
8d630132 255 char *arg = *argv++;
575f4974
LT
256
257 if (*arg == '-') {
575f4974
LT
258 /* Do flag handling here */
259 usage(receive_pack_usage);
260 }
d0efc8a7
LT
261 if (dir)
262 usage(receive_pack_usage);
575f4974 263 dir = arg;
575f4974
LT
264 }
265 if (!dir)
266 usage(receive_pack_usage);
267
8d630132
AE
268 if(!enter_repo(dir, 0))
269 die("'%s': unable to chdir or not a git archive", dir);
575f4974 270
8a65ff76 271 write_head_info();
575f4974
LT
272
273 /* EOF */
f3a3214e 274 packet_flush(1);
575f4974
LT
275
276 read_head_info();
7f8e9828
LT
277 if (commands) {
278 unpack();
279 execute_commands();
280 }
575f4974
LT
281 return 0;
282}