]> git.ipfire.org Git - thirdparty/git.git/blame - send-pack.c
receive-pack hooks updates.
[thirdparty/git.git] / send-pack.c
CommitLineData
61221472 1#include "cache.h"
2a9c3fe8 2#include "commit.h"
584c6cc9 3#include "refs.h"
f3a3214e 4#include "pkt-line.h"
61221472 5
2a245013 6static const char send_pack_usage[] =
0bc3cdfc
JH
7"git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
8" --all and explicit <head> specification are mutually exclusive.";
61221472 9static const char *exec = "git-receive-pack";
d089391c 10static int send_all = 0;
2a9c3fe8 11static int force_update = 0;
61221472 12
584c6cc9
LT
13static int is_zero_sha1(const unsigned char *sha1)
14{
15 int i;
16
17 for (i = 0; i < 20; i++) {
18 if (*sha1++)
19 return 0;
20 }
21 return 1;
22}
23
94fdb7aa
LT
24static void exec_pack_objects(void)
25{
26 static char *args[] = {
27 "git-pack-objects",
28 "--stdout",
29 NULL
30 };
31 execvp("git-pack-objects", args);
32 die("git-pack-objects exec failed (%s)", strerror(errno));
33}
34
35static void exec_rev_list(struct ref *refs)
36{
37 static char *args[1000];
38 int i = 0;
39
40 args[i++] = "git-rev-list"; /* 0 */
41 args[i++] = "--objects"; /* 1 */
42 while (refs) {
43 char *buf = malloc(100);
44 if (i > 900)
45 die("git-rev-list environment overflow");
584c6cc9
LT
46 if (!is_zero_sha1(refs->old_sha1)) {
47 args[i++] = buf;
48 snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
49 buf += 50;
50 }
51 if (!is_zero_sha1(refs->new_sha1)) {
52 args[i++] = buf;
53 snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
54 }
94fdb7aa
LT
55 refs = refs->next;
56 }
57 args[i] = NULL;
58 execvp("git-rev-list", args);
59 die("git-rev-list exec failed (%s)", strerror(errno));
60}
61
62static void rev_list(int fd, struct ref *refs)
63{
64 int pipe_fd[2];
65 pid_t pack_objects_pid;
66
67 if (pipe(pipe_fd) < 0)
68 die("rev-list setup: pipe failed");
69 pack_objects_pid = fork();
70 if (!pack_objects_pid) {
71 dup2(pipe_fd[0], 0);
72 dup2(fd, 1);
73 close(pipe_fd[0]);
74 close(pipe_fd[1]);
75 close(fd);
76 exec_pack_objects();
77 die("pack-objects setup failed");
78 }
79 if (pack_objects_pid < 0)
80 die("pack-objects fork failed");
81 dup2(pipe_fd[1], 1);
82 close(pipe_fd[0]);
83 close(pipe_fd[1]);
84 close(fd);
85 exec_rev_list(refs);
86}
87
88static int pack_objects(int fd, struct ref *refs)
89{
90 pid_t rev_list_pid;
91
92 rev_list_pid = fork();
93 if (!rev_list_pid) {
94 rev_list(fd, refs);
95 die("rev-list setup failed");
96 }
97 if (rev_list_pid < 0)
98 die("rev-list fork failed");
99 /*
100 * We don't wait for the rev-list pipeline in the parent:
101 * we end up waiting for the other end instead
102 */
7ec4e608 103 return 0;
94fdb7aa 104}
e4b5c7ff
LT
105
106static int read_ref(const char *ref, unsigned char *sha1)
107{
108 int fd, ret;
e4b5c7ff 109 char buffer[60];
e4b5c7ff 110
723c31fe 111 fd = open(git_path("%s", ref), O_RDONLY);
e4b5c7ff
LT
112 if (fd < 0)
113 return -1;
114 ret = -1;
115 if (read(fd, buffer, sizeof(buffer)) >= 40)
116 ret = get_sha1_hex(buffer, sha1);
117 close(fd);
118 return ret;
119}
120
584c6cc9
LT
121static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
122{
2a9c3fe8
LT
123 struct commit *new, *old;
124 struct commit_list *list;
125
126 if (force_update)
127 return 1;
128 old = lookup_commit_reference(old_sha1);
129 if (!old)
584c6cc9 130 return 0;
2a9c3fe8
LT
131 new = lookup_commit_reference(new_sha1);
132 if (!new)
133 return 0;
134 if (parse_commit(new) < 0)
135 return 0;
136 list = NULL;
137 commit_list_insert(new, &list);
bdf25142
LT
138 while (list) {
139 new = pop_most_recent_commit(&list, 1);
2a9c3fe8
LT
140 if (new == old)
141 return 1;
142 }
143 return 0;
584c6cc9
LT
144}
145
146static int local_ref_nr_match;
147static char **local_ref_match;
d089391c
LT
148static struct ref *local_ref_list;
149static struct ref **local_last_ref;
584c6cc9
LT
150
151static int try_to_match(const char *refname, const unsigned char *sha1)
152{
153 struct ref *ref;
154 int len;
155
d089391c
LT
156 if (!path_match(refname, local_ref_nr_match, local_ref_match)) {
157 if (!send_all)
158 return 0;
159
160 /* If we have it listed already, skip it */
161 for (ref = local_ref_list ; ref ; ref = ref->next) {
162 if (!strcmp(ref->name, refname))
163 return 0;
164 }
165 }
584c6cc9
LT
166
167 len = strlen(refname)+1;
168 ref = xmalloc(sizeof(*ref) + len);
169 memset(ref->old_sha1, 0, 20);
170 memcpy(ref->new_sha1, sha1, 20);
171 memcpy(ref->name, refname, len);
172 ref->next = NULL;
d089391c
LT
173 *local_last_ref = ref;
174 local_last_ref = &ref->next;
584c6cc9
LT
175 return 0;
176}
177
d0efc8a7 178static int send_pack(int in, int out, int nr_match, char **match)
61221472 179{
d1c133f5 180 struct ref *ref_list, **last_ref;
7f8e9828 181 struct ref *ref;
584c6cc9 182 int new_refs;
7f8e9828 183
d1c133f5
LT
184 /* First we get all heads, whether matching or not.. */
185 last_ref = get_remote_heads(in, &ref_list, 0, NULL);
584c6cc9
LT
186
187 /*
188 * Go through the refs, see if we want to update
189 * any of them..
190 */
191 for (ref = ref_list; ref; ref = ref->next) {
192 unsigned char new_sha1[20];
193 char *name = ref->name;
194
d0efc8a7
LT
195 if (nr_match && !path_match(name, nr_match, match))
196 continue;
584c6cc9 197
7f8e9828 198 if (read_ref(name, new_sha1) < 0)
584c6cc9
LT
199 continue;
200
584c6cc9 201 if (!memcmp(ref->old_sha1, new_sha1, 20)) {
e4b5c7ff 202 fprintf(stderr, "'%s' unchanged\n", name);
7f8e9828 203 continue;
e4b5c7ff 204 }
584c6cc9
LT
205
206 if (!ref_newer(new_sha1, ref->old_sha1)) {
2a9c3fe8 207 error("remote '%s' isn't a strict parent of local", name);
584c6cc9
LT
208 continue;
209 }
210
211 /* Ok, mark it for update */
7f8e9828 212 memcpy(ref->new_sha1, new_sha1, 20);
7f8e9828
LT
213 }
214
584c6cc9
LT
215 /*
216 * See if we have any refs that the other end didn't have
217 */
0bc3cdfc 218 if (nr_match || send_all) {
584c6cc9
LT
219 local_ref_nr_match = nr_match;
220 local_ref_match = match;
d089391c
LT
221 local_ref_list = ref_list;
222 local_last_ref = last_ref;
584c6cc9
LT
223 for_each_ref(try_to_match);
224 }
225
226 /*
227 * Finally, tell the other end!
228 */
229 new_refs = 0;
7f8e9828
LT
230 for (ref = ref_list; ref; ref = ref->next) {
231 char old_hex[60], *new_hex;
584c6cc9
LT
232 if (is_zero_sha1(ref->new_sha1))
233 continue;
234 new_refs++;
7f8e9828
LT
235 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
236 new_hex = sha1_to_hex(ref->new_sha1);
237 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
238 fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
61221472 239 }
7f8e9828 240
f3a3214e 241 packet_flush(out);
584c6cc9 242 if (new_refs)
94fdb7aa 243 pack_objects(out, ref_list);
61221472
LT
244 close(out);
245 return 0;
246}
247
61221472
LT
248int main(int argc, char **argv)
249{
250 int i, nr_heads = 0;
251 char *dest = NULL;
252 char **heads = NULL;
7f8e9828
LT
253 int fd[2], ret;
254 pid_t pid;
61221472
LT
255
256 argv++;
d089391c
LT
257 for (i = 1; i < argc; i++, argv++) {
258 char *arg = *argv;
61221472
LT
259
260 if (*arg == '-') {
261 if (!strncmp(arg, "--exec=", 7)) {
262 exec = arg + 7;
263 continue;
264 }
d089391c
LT
265 if (!strcmp(arg, "--all")) {
266 send_all = 1;
267 continue;
268 }
2a9c3fe8
LT
269 if (!strcmp(arg, "--force")) {
270 force_update = 1;
271 continue;
272 }
61221472
LT
273 usage(send_pack_usage);
274 }
d089391c
LT
275 if (!dest) {
276 dest = arg;
277 continue;
278 }
61221472 279 heads = argv;
d089391c 280 nr_heads = argc - i;
61221472
LT
281 break;
282 }
283 if (!dest)
284 usage(send_pack_usage);
0bc3cdfc
JH
285 if (heads && send_all)
286 usage(send_pack_usage);
f7192598 287 pid = git_connect(fd, dest, exec);
7f8e9828 288 if (pid < 0)
61221472 289 return 1;
d0efc8a7 290 ret = send_pack(fd[0], fd[1], nr_heads, heads);
7f8e9828
LT
291 close(fd[0]);
292 close(fd[1]);
f7192598 293 finish_connect(pid);
7f8e9828 294 return ret;
61221472 295}