Perform correct quoting of recipient names.
[git/fastimport.git] / send-pack.c
blobd5b51628dfbcc44b22b5069df19eeac67dda1e57
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "refs.h"
5 #include "pkt-line.h"
6 #include "run-command.h"
8 static const char send_pack_usage[] =
9 "git-send-pack [--all] [--force] [--receive-pack=<git-receive-pack>] [--verbose] [--thin] [<host>:]<directory> [<ref>...]\n"
10 " --all and explicit <ref> specification are mutually exclusive.";
11 static const char *receivepack = "git-receive-pack";
12 static int verbose;
13 static int send_all;
14 static int force_update;
15 static int use_thin_pack;
18 * Make a pack stream and spit it out into file descriptor fd
20 static int pack_objects(int fd, struct ref *refs)
23 * The child becomes pack-objects --revs; we feed
24 * the revision parameters to it via its stdin and
25 * let its stdout go back to the other end.
27 const char *args[] = {
28 "pack-objects",
29 "--all-progress",
30 "--revs",
31 "--stdout",
32 NULL,
33 NULL,
35 struct child_process po;
37 if (use_thin_pack)
38 args[4] = "--thin";
39 memset(&po, 0, sizeof(po));
40 po.argv = args;
41 po.in = -1;
42 po.out = fd;
43 po.git_cmd = 1;
44 if (start_command(&po))
45 die("git-pack-objects failed (%s)", strerror(errno));
48 * We feed the pack-objects we just spawned with revision
49 * parameters by writing to the pipe.
51 while (refs) {
52 char buf[42];
54 if (!is_null_sha1(refs->old_sha1) &&
55 has_sha1_file(refs->old_sha1)) {
56 memcpy(buf + 1, sha1_to_hex(refs->old_sha1), 40);
57 buf[0] = '^';
58 buf[41] = '\n';
59 if (!write_or_whine(po.in, buf, 42,
60 "send-pack: send refs"))
61 break;
63 if (!is_null_sha1(refs->new_sha1)) {
64 memcpy(buf, sha1_to_hex(refs->new_sha1), 40);
65 buf[40] = '\n';
66 if (!write_or_whine(po.in, buf, 41,
67 "send-pack: send refs"))
68 break;
70 refs = refs->next;
73 if (finish_command(&po))
74 return error("pack-objects died with strange error");
75 return 0;
78 static void unmark_and_free(struct commit_list *list, unsigned int mark)
80 while (list) {
81 struct commit_list *temp = list;
82 temp->item->object.flags &= ~mark;
83 list = temp->next;
84 free(temp);
88 static int ref_newer(const unsigned char *new_sha1,
89 const unsigned char *old_sha1)
91 struct object *o;
92 struct commit *old, *new;
93 struct commit_list *list, *used;
94 int found = 0;
96 /* Both new and old must be commit-ish and new is descendant of
97 * old. Otherwise we require --force.
99 o = deref_tag(parse_object(old_sha1), NULL, 0);
100 if (!o || o->type != OBJ_COMMIT)
101 return 0;
102 old = (struct commit *) o;
104 o = deref_tag(parse_object(new_sha1), NULL, 0);
105 if (!o || o->type != OBJ_COMMIT)
106 return 0;
107 new = (struct commit *) o;
109 if (parse_commit(new) < 0)
110 return 0;
112 used = list = NULL;
113 commit_list_insert(new, &list);
114 while (list) {
115 new = pop_most_recent_commit(&list, 1);
116 commit_list_insert(new, &used);
117 if (new == old) {
118 found = 1;
119 break;
122 unmark_and_free(list, 1);
123 unmark_and_free(used, 1);
124 return found;
127 static struct ref *local_refs, **local_tail;
128 static struct ref *remote_refs, **remote_tail;
130 static int one_local_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
132 struct ref *ref;
133 int len = strlen(refname) + 1;
134 ref = xcalloc(1, sizeof(*ref) + len);
135 hashcpy(ref->new_sha1, sha1);
136 memcpy(ref->name, refname, len);
137 *local_tail = ref;
138 local_tail = &ref->next;
139 return 0;
142 static void get_local_heads(void)
144 local_tail = &local_refs;
145 for_each_ref(one_local_ref, NULL);
148 static int receive_status(int in)
150 char line[1000];
151 int ret = 0;
152 int len = packet_read_line(in, line, sizeof(line));
153 if (len < 10 || memcmp(line, "unpack ", 7)) {
154 fprintf(stderr, "did not receive status back\n");
155 return -1;
157 if (memcmp(line, "unpack ok\n", 10)) {
158 fputs(line, stderr);
159 ret = -1;
161 while (1) {
162 len = packet_read_line(in, line, sizeof(line));
163 if (!len)
164 break;
165 if (len < 3 ||
166 (memcmp(line, "ok", 2) && memcmp(line, "ng", 2))) {
167 fprintf(stderr, "protocol error: %s\n", line);
168 ret = -1;
169 break;
171 if (!memcmp(line, "ok", 2))
172 continue;
173 fputs(line, stderr);
174 ret = -1;
176 return ret;
179 static int send_pack(int in, int out, int nr_refspec, char **refspec)
181 struct ref *ref;
182 int new_refs;
183 int ret = 0;
184 int ask_for_status_report = 0;
185 int allow_deleting_refs = 0;
186 int expect_status_report = 0;
188 /* No funny business with the matcher */
189 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL, REF_NORMAL);
190 get_local_heads();
192 /* Does the other end support the reporting? */
193 if (server_supports("report-status"))
194 ask_for_status_report = 1;
195 if (server_supports("delete-refs"))
196 allow_deleting_refs = 1;
198 /* match them up */
199 if (!remote_tail)
200 remote_tail = &remote_refs;
201 if (match_refs(local_refs, remote_refs, &remote_tail,
202 nr_refspec, refspec, send_all))
203 return -1;
205 if (!remote_refs) {
206 fprintf(stderr, "No refs in common and none specified; doing nothing.\n");
207 return 0;
211 * Finally, tell the other end!
213 new_refs = 0;
214 for (ref = remote_refs; ref; ref = ref->next) {
215 char old_hex[60], *new_hex;
216 int delete_ref;
218 if (!ref->peer_ref)
219 continue;
221 delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
222 if (delete_ref && !allow_deleting_refs) {
223 error("remote does not support deleting refs");
224 ret = -2;
225 continue;
227 if (!delete_ref &&
228 !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1)) {
229 if (verbose)
230 fprintf(stderr, "'%s': up-to-date\n", ref->name);
231 continue;
234 /* This part determines what can overwrite what.
235 * The rules are:
237 * (0) you can always use --force or +A:B notation to
238 * selectively force individual ref pairs.
240 * (1) if the old thing does not exist, it is OK.
242 * (2) if you do not have the old thing, you are not allowed
243 * to overwrite it; you would not know what you are losing
244 * otherwise.
246 * (3) if both new and old are commit-ish, and new is a
247 * descendant of old, it is OK.
249 * (4) regardless of all of the above, removing :B is
250 * always allowed.
253 if (!force_update &&
254 !delete_ref &&
255 !is_null_sha1(ref->old_sha1) &&
256 !ref->force) {
257 if (!has_sha1_file(ref->old_sha1) ||
258 !ref_newer(ref->peer_ref->new_sha1,
259 ref->old_sha1)) {
260 /* We do not have the remote ref, or
261 * we know that the remote ref is not
262 * an ancestor of what we are trying to
263 * push. Either way this can be losing
264 * commits at the remote end and likely
265 * we were not up to date to begin with.
267 error("remote '%s' is not a strict "
268 "subset of local ref '%s'. "
269 "maybe you are not up-to-date and "
270 "need to pull first?",
271 ref->name,
272 ref->peer_ref->name);
273 ret = -2;
274 continue;
277 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
278 if (!delete_ref)
279 new_refs++;
280 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
281 new_hex = sha1_to_hex(ref->new_sha1);
283 if (ask_for_status_report) {
284 packet_write(out, "%s %s %s%c%s",
285 old_hex, new_hex, ref->name, 0,
286 "report-status");
287 ask_for_status_report = 0;
288 expect_status_report = 1;
290 else
291 packet_write(out, "%s %s %s",
292 old_hex, new_hex, ref->name);
293 if (delete_ref)
294 fprintf(stderr, "deleting '%s'\n", ref->name);
295 else {
296 fprintf(stderr, "updating '%s'", ref->name);
297 if (strcmp(ref->name, ref->peer_ref->name))
298 fprintf(stderr, " using '%s'",
299 ref->peer_ref->name);
300 fprintf(stderr, "\n from %s\n to %s\n",
301 old_hex, new_hex);
305 packet_flush(out);
306 if (new_refs)
307 ret = pack_objects(out, remote_refs);
308 close(out);
310 if (expect_status_report) {
311 if (receive_status(in))
312 ret = -4;
315 if (!new_refs && ret == 0)
316 fprintf(stderr, "Everything up-to-date\n");
317 return ret;
320 static void verify_remote_names(int nr_heads, char **heads)
322 int i;
324 for (i = 0; i < nr_heads; i++) {
325 const char *remote = strchr(heads[i], ':');
327 remote = remote ? (remote + 1) : heads[i];
328 switch (check_ref_format(remote)) {
329 case 0: /* ok */
330 case -2: /* ok but a single level -- that is fine for
331 * a match pattern.
333 continue;
335 die("remote part of refspec is not a valid name in %s",
336 heads[i]);
340 int main(int argc, char **argv)
342 int i, nr_heads = 0;
343 char *dest = NULL;
344 char **heads = NULL;
345 int fd[2], ret;
346 pid_t pid;
348 setup_git_directory();
349 git_config(git_default_config);
351 argv++;
352 for (i = 1; i < argc; i++, argv++) {
353 char *arg = *argv;
355 if (*arg == '-') {
356 if (!prefixcmp(arg, "--receive-pack=")) {
357 receivepack = arg + 15;
358 continue;
360 if (!prefixcmp(arg, "--exec=")) {
361 receivepack = arg + 7;
362 continue;
364 if (!strcmp(arg, "--all")) {
365 send_all = 1;
366 continue;
368 if (!strcmp(arg, "--force")) {
369 force_update = 1;
370 continue;
372 if (!strcmp(arg, "--verbose")) {
373 verbose = 1;
374 continue;
376 if (!strcmp(arg, "--thin")) {
377 use_thin_pack = 1;
378 continue;
380 usage(send_pack_usage);
382 if (!dest) {
383 dest = arg;
384 continue;
386 heads = argv;
387 nr_heads = argc - i;
388 break;
390 if (!dest)
391 usage(send_pack_usage);
392 if (heads && send_all)
393 usage(send_pack_usage);
394 verify_remote_names(nr_heads, heads);
396 pid = git_connect(fd, dest, receivepack);
397 if (pid < 0)
398 return 1;
399 ret = send_pack(fd[0], fd[1], nr_heads, heads);
400 close(fd[0]);
401 close(fd[1]);
402 ret |= finish_connect(pid);
403 return !!ret;