Fix refname termination.
[git/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
37fde874
JH
108static int ref_newer(const unsigned char *new_sha1,
109 const unsigned char *old_sha1)
584c6cc9 110{
37fde874
JH
111 struct object *o;
112 struct commit *old, *new;
2a9c3fe8
LT
113 struct commit_list *list;
114
37fde874
JH
115 /* Both new and old must be commit-ish and new is descendant of
116 * old. Otherwise we require --force.
117 */
118 o = deref_tag(parse_object(old_sha1));
119 if (!o || o->type != commit_type)
584c6cc9 120 return 0;
37fde874
JH
121 old = (struct commit *) o;
122
123 o = deref_tag(parse_object(new_sha1));
124 if (!o || o->type != commit_type)
2a9c3fe8 125 return 0;
37fde874
JH
126 new = (struct commit *) o;
127
2a9c3fe8
LT
128 if (parse_commit(new) < 0)
129 return 0;
130 list = NULL;
131 commit_list_insert(new, &list);
bdf25142
LT
132 while (list) {
133 new = pop_most_recent_commit(&list, 1);
2a9c3fe8
LT
134 if (new == old)
135 return 1;
136 }
137 return 0;
584c6cc9
LT
138}
139
f88395ac
JH
140static struct ref *local_refs, **local_tail;
141static struct ref *remote_refs, **remote_tail;
584c6cc9 142
f88395ac 143static int one_local_ref(const char *refname, const unsigned char *sha1)
584c6cc9
LT
144{
145 struct ref *ref;
f88395ac
JH
146 int len = strlen(refname) + 1;
147 ref = xcalloc(1, sizeof(*ref) + len);
584c6cc9
LT
148 memcpy(ref->new_sha1, sha1, 20);
149 memcpy(ref->name, refname, len);
f88395ac
JH
150 *local_tail = ref;
151 local_tail = &ref->next;
584c6cc9
LT
152 return 0;
153}
154
f88395ac
JH
155static void get_local_heads(void)
156{
157 local_tail = &local_refs;
158 for_each_ref(one_local_ref);
159}
160
161static int send_pack(int in, int out, int nr_refspec, char **refspec)
61221472 162{
7f8e9828 163 struct ref *ref;
584c6cc9 164 int new_refs;
7f8e9828 165
f88395ac
JH
166 /* No funny business with the matcher */
167 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL);
168 get_local_heads();
584c6cc9 169
f88395ac
JH
170 /* match them up */
171 if (!remote_tail)
172 remote_tail = &remote_refs;
173 if (match_refs(local_refs, remote_refs, &remote_tail,
174 nr_refspec, refspec, send_all))
175 return -1;
584c6cc9 176 /*
f88395ac 177 * Finally, tell the other end!
584c6cc9 178 */
f88395ac
JH
179 new_refs = 0;
180 for (ref = remote_refs; ref; ref = ref->next) {
181 char old_hex[60], *new_hex;
182 if (!ref->peer_ref)
7f8e9828 183 continue;
37fde874
JH
184 if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
185 fprintf(stderr, "'%s': up-to-date\n", ref->name);
186 continue;
187 }
188
189 /* This part determines what can overwrite what.
190 * The rules are:
191 *
192 * (0) you can always use --force.
193 *
194 * (1) if the old thing does not exist, it is OK.
195 *
196 * (2) if you do not have the old thing, you are not allowed
197 * to overwrite it; you would not know what you are losing
198 * otherwise.
199 *
200 * (3) if both new and old are commit-ish, and new is a
201 * descendant of old, it is OK.
202 */
203
204 if (!force_update && !is_zero_sha1(ref->old_sha1)) {
f88395ac
JH
205 if (!has_sha1_file(ref->old_sha1)) {
206 error("remote '%s' object %s does not "
207 "exist on local",
208 ref->name, sha1_to_hex(ref->old_sha1));
209 continue;
210 }
37fde874
JH
211 /* We assume that local is fsck-clean. Otherwise
212 * you _could_ have a old tag which points at
213 * something you do not have which may or may not
214 * be a commit.
215 */
f88395ac
JH
216 if (!ref_newer(ref->peer_ref->new_sha1,
217 ref->old_sha1)) {
218 error("remote ref '%s' is not a strict "
219 "subset of local ref '%s'.", ref->name,
220 ref->peer_ref->name);
221 continue;
222 }
e4b5c7ff 223 }
f88395ac
JH
224 memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
225 if (is_zero_sha1(ref->new_sha1)) {
226 error("cannot happen anymore");
584c6cc9
LT
227 continue;
228 }
584c6cc9 229 new_refs++;
7f8e9828
LT
230 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
231 new_hex = sha1_to_hex(ref->new_sha1);
232 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
f88395ac
JH
233 fprintf(stderr, "updating '%s'", ref->name);
234 if (strcmp(ref->name, ref->peer_ref->name))
235 fprintf(stderr, " using '%s'", ref->peer_ref->name);
236 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
61221472 237 }
f88395ac 238
f3a3214e 239 packet_flush(out);
584c6cc9 240 if (new_refs)
f88395ac 241 pack_objects(out, remote_refs);
61221472
LT
242 close(out);
243 return 0;
244}
245
f88395ac 246
61221472
LT
247int main(int argc, char **argv)
248{
249 int i, nr_heads = 0;
250 char *dest = NULL;
251 char **heads = NULL;
7f8e9828
LT
252 int fd[2], ret;
253 pid_t pid;
61221472
LT
254
255 argv++;
d089391c
LT
256 for (i = 1; i < argc; i++, argv++) {
257 char *arg = *argv;
61221472
LT
258
259 if (*arg == '-') {
260 if (!strncmp(arg, "--exec=", 7)) {
261 exec = arg + 7;
262 continue;
263 }
d089391c
LT
264 if (!strcmp(arg, "--all")) {
265 send_all = 1;
266 continue;
267 }
2a9c3fe8
LT
268 if (!strcmp(arg, "--force")) {
269 force_update = 1;
270 continue;
271 }
61221472
LT
272 usage(send_pack_usage);
273 }
d089391c
LT
274 if (!dest) {
275 dest = arg;
276 continue;
277 }
61221472 278 heads = argv;
d089391c 279 nr_heads = argc - i;
61221472
LT
280 break;
281 }
282 if (!dest)
283 usage(send_pack_usage);
0bc3cdfc
JH
284 if (heads && send_all)
285 usage(send_pack_usage);
f7192598 286 pid = git_connect(fd, dest, exec);
7f8e9828 287 if (pid < 0)
61221472 288 return 1;
d0efc8a7 289 ret = send_pack(fd[0], fd[1], nr_heads, heads);
7f8e9828
LT
290 close(fd[0]);
291 close(fd[1]);
f7192598 292 finish_connect(pid);
7f8e9828 293 return ret;
61221472 294}