]> git.ipfire.org Git - thirdparty/git.git/blame - send-pack.c
t3300: funny name test
[thirdparty/git.git] / send-pack.c
CommitLineData
61221472 1#include "cache.h"
2a9c3fe8 2#include "commit.h"
37fde874 3#include "tag.h"
584c6cc9 4#include "refs.h"
f3a3214e 5#include "pkt-line.h"
61221472 6
2a245013 7static const char send_pack_usage[] =
0bc3cdfc
JH
8"git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
9" --all and explicit <head> specification are mutually exclusive.";
61221472 10static const char *exec = "git-receive-pack";
d089391c 11static int send_all = 0;
2a9c3fe8 12static int force_update = 0;
61221472 13
584c6cc9
LT
14static int is_zero_sha1(const unsigned char *sha1)
15{
16 int i;
17
18 for (i = 0; i < 20; i++) {
19 if (*sha1++)
20 return 0;
21 }
22 return 1;
23}
24
94fdb7aa
LT
25static void exec_pack_objects(void)
26{
27 static char *args[] = {
28 "git-pack-objects",
29 "--stdout",
30 NULL
31 };
32 execvp("git-pack-objects", args);
33 die("git-pack-objects exec failed (%s)", strerror(errno));
34}
35
36static void exec_rev_list(struct ref *refs)
37{
38 static char *args[1000];
39 int i = 0;
40
41 args[i++] = "git-rev-list"; /* 0 */
42 args[i++] = "--objects"; /* 1 */
43 while (refs) {
44 char *buf = malloc(100);
45 if (i > 900)
46 die("git-rev-list environment overflow");
40b64d47
JH
47 if (!is_zero_sha1(refs->old_sha1) &&
48 has_sha1_file(refs->old_sha1)) {
584c6cc9
LT
49 args[i++] = buf;
50 snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
51 buf += 50;
52 }
53 if (!is_zero_sha1(refs->new_sha1)) {
54 args[i++] = buf;
55 snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
56 }
94fdb7aa
LT
57 refs = refs->next;
58 }
59 args[i] = NULL;
60 execvp("git-rev-list", args);
61 die("git-rev-list exec failed (%s)", strerror(errno));
62}
63
64static void rev_list(int fd, struct ref *refs)
65{
66 int pipe_fd[2];
67 pid_t pack_objects_pid;
68
69 if (pipe(pipe_fd) < 0)
70 die("rev-list setup: pipe failed");
71 pack_objects_pid = fork();
72 if (!pack_objects_pid) {
73 dup2(pipe_fd[0], 0);
74 dup2(fd, 1);
75 close(pipe_fd[0]);
76 close(pipe_fd[1]);
77 close(fd);
78 exec_pack_objects();
79 die("pack-objects setup failed");
80 }
81 if (pack_objects_pid < 0)
82 die("pack-objects fork failed");
83 dup2(pipe_fd[1], 1);
84 close(pipe_fd[0]);
85 close(pipe_fd[1]);
86 close(fd);
87 exec_rev_list(refs);
88}
89
90static int pack_objects(int fd, struct ref *refs)
91{
92 pid_t rev_list_pid;
93
94 rev_list_pid = fork();
95 if (!rev_list_pid) {
96 rev_list(fd, refs);
97 die("rev-list setup failed");
98 }
99 if (rev_list_pid < 0)
100 die("rev-list fork failed");
101 /*
102 * We don't wait for the rev-list pipeline in the parent:
103 * we end up waiting for the other end instead
104 */
7ec4e608 105 return 0;
94fdb7aa 106}
e4b5c7ff 107
51b0fca0
JH
108static void unmark_and_free(struct commit_list *list, unsigned int mark)
109{
110 while (list) {
111 struct commit_list *temp = list;
112 temp->item->object.flags &= ~mark;
113 list = temp->next;
114 free(temp);
115 }
116}
117
37fde874
JH
118static int ref_newer(const unsigned char *new_sha1,
119 const unsigned char *old_sha1)
584c6cc9 120{
37fde874
JH
121 struct object *o;
122 struct commit *old, *new;
51b0fca0
JH
123 struct commit_list *list, *used;
124 int found = 0;
2a9c3fe8 125
37fde874
JH
126 /* Both new and old must be commit-ish and new is descendant of
127 * old. Otherwise we require --force.
128 */
9534f40b 129 o = deref_tag(parse_object(old_sha1), NULL, 0);
37fde874 130 if (!o || o->type != commit_type)
584c6cc9 131 return 0;
37fde874
JH
132 old = (struct commit *) o;
133
9534f40b 134 o = deref_tag(parse_object(new_sha1), NULL, 0);
37fde874 135 if (!o || o->type != commit_type)
2a9c3fe8 136 return 0;
37fde874
JH
137 new = (struct commit *) o;
138
2a9c3fe8
LT
139 if (parse_commit(new) < 0)
140 return 0;
51b0fca0
JH
141
142 used = list = NULL;
2a9c3fe8 143 commit_list_insert(new, &list);
bdf25142
LT
144 while (list) {
145 new = pop_most_recent_commit(&list, 1);
51b0fca0
JH
146 commit_list_insert(new, &used);
147 if (new == old) {
148 found = 1;
149 break;
150 }
2a9c3fe8 151 }
51b0fca0
JH
152 unmark_and_free(list, 1);
153 unmark_and_free(used, 1);
154 return found;
584c6cc9
LT
155}
156
f88395ac
JH
157static struct ref *local_refs, **local_tail;
158static struct ref *remote_refs, **remote_tail;
584c6cc9 159
f88395ac 160static int one_local_ref(const char *refname, const unsigned char *sha1)
584c6cc9
LT
161{
162 struct ref *ref;
f88395ac
JH
163 int len = strlen(refname) + 1;
164 ref = xcalloc(1, sizeof(*ref) + len);
584c6cc9
LT
165 memcpy(ref->new_sha1, sha1, 20);
166 memcpy(ref->name, refname, len);
f88395ac
JH
167 *local_tail = ref;
168 local_tail = &ref->next;
584c6cc9
LT
169 return 0;
170}
171
f88395ac
JH
172static void get_local_heads(void)
173{
174 local_tail = &local_refs;
175 for_each_ref(one_local_ref);
176}
177
178static int send_pack(int in, int out, int nr_refspec, char **refspec)
61221472 179{
7f8e9828 180 struct ref *ref;
584c6cc9 181 int new_refs;
7f8e9828 182
f88395ac 183 /* No funny business with the matcher */
1a7141ff 184 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, 1);
f88395ac 185 get_local_heads();
584c6cc9 186
f88395ac
JH
187 /* match them up */
188 if (!remote_tail)
189 remote_tail = &remote_refs;
190 if (match_refs(local_refs, remote_refs, &remote_tail,
191 nr_refspec, refspec, send_all))
192 return -1;
4c353e89
DB
193
194 if (!remote_refs) {
195 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
196 return 0;
197 }
198
584c6cc9 199 /*
f88395ac 200 * Finally, tell the other end!
584c6cc9 201 */
f88395ac
JH
202 new_refs = 0;
203 for (ref = remote_refs; ref; ref = ref->next) {
204 char old_hex[60], *new_hex;
205 if (!ref->peer_ref)
7f8e9828 206 continue;
37fde874
JH
207 if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
208 fprintf(stderr, "'%s': up-to-date\n", ref->name);
209 continue;
210 }
211
212 /* This part determines what can overwrite what.
213 * The rules are:
214 *
ff27adf3
JH
215 * (0) you can always use --force or +A:B notation to
216 * selectively force individual ref pairs.
37fde874
JH
217 *
218 * (1) if the old thing does not exist, it is OK.
219 *
220 * (2) if you do not have the old thing, you are not allowed
221 * to overwrite it; you would not know what you are losing
222 * otherwise.
223 *
224 * (3) if both new and old are commit-ish, and new is a
225 * descendant of old, it is OK.
226 */
227
ff27adf3
JH
228 if (!force_update &&
229 !is_zero_sha1(ref->old_sha1) &&
230 !ref->force) {
f88395ac
JH
231 if (!has_sha1_file(ref->old_sha1)) {
232 error("remote '%s' object %s does not "
233 "exist on local",
234 ref->name, sha1_to_hex(ref->old_sha1));
235 continue;
236 }
ff27adf3 237
37fde874 238 /* We assume that local is fsck-clean. Otherwise
ff27adf3
JH
239 * you _could_ have an old tag which points at
240 * something you do not have, which may or may not
37fde874
JH
241 * be a commit.
242 */
f88395ac
JH
243 if (!ref_newer(ref->peer_ref->new_sha1,
244 ref->old_sha1)) {
245 error("remote ref '%s' is not a strict "
246 "subset of local ref '%s'.", ref->name,
247 ref->peer_ref->name);
248 continue;
249 }
e4b5c7ff 250 }
f88395ac
JH
251 memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
252 if (is_zero_sha1(ref->new_sha1)) {
253 error("cannot happen anymore");
584c6cc9
LT
254 continue;
255 }
584c6cc9 256 new_refs++;
7f8e9828
LT
257 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
258 new_hex = sha1_to_hex(ref->new_sha1);
259 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
f88395ac
JH
260 fprintf(stderr, "updating '%s'", ref->name);
261 if (strcmp(ref->name, ref->peer_ref->name))
262 fprintf(stderr, " using '%s'", ref->peer_ref->name);
263 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
61221472 264 }
f88395ac 265
f3a3214e 266 packet_flush(out);
584c6cc9 267 if (new_refs)
f88395ac 268 pack_objects(out, remote_refs);
61221472
LT
269 close(out);
270 return 0;
271}
272
f88395ac 273
61221472
LT
274int main(int argc, char **argv)
275{
276 int i, nr_heads = 0;
277 char *dest = NULL;
278 char **heads = NULL;
7f8e9828
LT
279 int fd[2], ret;
280 pid_t pid;
61221472 281
5a327713 282 setup_git_directory();
61221472 283 argv++;
d089391c
LT
284 for (i = 1; i < argc; i++, argv++) {
285 char *arg = *argv;
61221472
LT
286
287 if (*arg == '-') {
288 if (!strncmp(arg, "--exec=", 7)) {
289 exec = arg + 7;
290 continue;
291 }
d089391c
LT
292 if (!strcmp(arg, "--all")) {
293 send_all = 1;
294 continue;
295 }
2a9c3fe8
LT
296 if (!strcmp(arg, "--force")) {
297 force_update = 1;
298 continue;
299 }
61221472
LT
300 usage(send_pack_usage);
301 }
d089391c
LT
302 if (!dest) {
303 dest = arg;
304 continue;
305 }
61221472 306 heads = argv;
d089391c 307 nr_heads = argc - i;
61221472
LT
308 break;
309 }
310 if (!dest)
311 usage(send_pack_usage);
0bc3cdfc
JH
312 if (heads && send_all)
313 usage(send_pack_usage);
f7192598 314 pid = git_connect(fd, dest, exec);
7f8e9828 315 if (pid < 0)
61221472 316 return 1;
d0efc8a7 317 ret = send_pack(fd[0], fd[1], nr_heads, heads);
7f8e9828
LT
318 close(fd[0]);
319 close(fd[1]);
f7192598 320 finish_connect(pid);
7f8e9828 321 return ret;
61221472 322}