Clean generated files a bit more, to cope with Debian build droppings.
[git/jrn.git] / send-pack.c
blob0ab135c9881c4d9fa6b82ea80f5e62d2fbf17d28
1 #include "cache.h"
2 #include "commit.h"
3 #include "tag.h"
4 #include "refs.h"
5 #include "pkt-line.h"
7 static const char send_pack_usage[] =
8 "git-send-pack [--all] [--exec=git-receive-pack] <remote> [<head>...]\n"
9 " --all and explicit <head> specification are mutually exclusive.";
10 static const char *exec = "git-receive-pack";
11 static int send_all = 0;
12 static int force_update = 0;
14 static int is_zero_sha1(const unsigned char *sha1)
16 int i;
18 for (i = 0; i < 20; i++) {
19 if (*sha1++)
20 return 0;
22 return 1;
25 static void exec_pack_objects(void)
27 static char *args[] = {
28 "git-pack-objects",
29 "--stdout",
30 NULL
32 execvp("git-pack-objects", args);
33 die("git-pack-objects exec failed (%s)", strerror(errno));
36 static void exec_rev_list(struct ref *refs)
38 static char *args[1000];
39 int i = 0;
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");
47 if (!is_zero_sha1(refs->old_sha1) &&
48 has_sha1_file(refs->old_sha1)) {
49 args[i++] = buf;
50 snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
51 buf += 50;
53 if (!is_zero_sha1(refs->new_sha1)) {
54 args[i++] = buf;
55 snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
57 refs = refs->next;
59 args[i] = NULL;
60 execvp("git-rev-list", args);
61 die("git-rev-list exec failed (%s)", strerror(errno));
64 static void rev_list(int fd, struct ref *refs)
66 int pipe_fd[2];
67 pid_t pack_objects_pid;
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");
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);
90 static int pack_objects(int fd, struct ref *refs)
92 pid_t rev_list_pid;
94 rev_list_pid = fork();
95 if (!rev_list_pid) {
96 rev_list(fd, refs);
97 die("rev-list setup failed");
99 if (rev_list_pid < 0)
100 die("rev-list fork failed");
102 * We don't wait for the rev-list pipeline in the parent:
103 * we end up waiting for the other end instead
105 return 0;
108 static void unmark_and_free(struct commit_list *list, unsigned int mark)
110 while (list) {
111 struct commit_list *temp = list;
112 temp->item->object.flags &= ~mark;
113 list = temp->next;
114 free(temp);
118 static int ref_newer(const unsigned char *new_sha1,
119 const unsigned char *old_sha1)
121 struct object *o;
122 struct commit *old, *new;
123 struct commit_list *list, *used;
124 int found = 0;
126 /* Both new and old must be commit-ish and new is descendant of
127 * old. Otherwise we require --force.
129 o = deref_tag(parse_object(old_sha1));
130 if (!o || o->type != commit_type)
131 return 0;
132 old = (struct commit *) o;
134 o = deref_tag(parse_object(new_sha1));
135 if (!o || o->type != commit_type)
136 return 0;
137 new = (struct commit *) o;
139 if (parse_commit(new) < 0)
140 return 0;
142 used = list = NULL;
143 commit_list_insert(new, &list);
144 while (list) {
145 new = pop_most_recent_commit(&list, 1);
146 commit_list_insert(new, &used);
147 if (new == old) {
148 found = 1;
149 break;
152 unmark_and_free(list, 1);
153 unmark_and_free(used, 1);
154 return found;
157 static struct ref *local_refs, **local_tail;
158 static struct ref *remote_refs, **remote_tail;
160 static int one_local_ref(const char *refname, const unsigned char *sha1)
162 struct ref *ref;
163 int len = strlen(refname) + 1;
164 ref = xcalloc(1, sizeof(*ref) + len);
165 memcpy(ref->new_sha1, sha1, 20);
166 memcpy(ref->name, refname, len);
167 *local_tail = ref;
168 local_tail = &ref->next;
169 return 0;
172 static void get_local_heads(void)
174 local_tail = &local_refs;
175 for_each_ref(one_local_ref);
178 static int send_pack(int in, int out, int nr_refspec, char **refspec)
180 struct ref *ref;
181 int new_refs;
183 /* No funny business with the matcher */
184 remote_tail = get_remote_heads(in, &remote_refs, 0, NULL);
185 get_local_heads();
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;
194 * Finally, tell the other end!
196 new_refs = 0;
197 for (ref = remote_refs; ref; ref = ref->next) {
198 char old_hex[60], *new_hex;
199 if (!ref->peer_ref)
200 continue;
201 if (!memcmp(ref->old_sha1, ref->peer_ref->new_sha1, 20)) {
202 fprintf(stderr, "'%s': up-to-date\n", ref->name);
203 continue;
206 /* This part determines what can overwrite what.
207 * The rules are:
209 * (0) you can always use --force.
211 * (1) if the old thing does not exist, it is OK.
213 * (2) if you do not have the old thing, you are not allowed
214 * to overwrite it; you would not know what you are losing
215 * otherwise.
217 * (3) if both new and old are commit-ish, and new is a
218 * descendant of old, it is OK.
221 if (!force_update && !is_zero_sha1(ref->old_sha1)) {
222 if (!has_sha1_file(ref->old_sha1)) {
223 error("remote '%s' object %s does not "
224 "exist on local",
225 ref->name, sha1_to_hex(ref->old_sha1));
226 continue;
228 /* We assume that local is fsck-clean. Otherwise
229 * you _could_ have a old tag which points at
230 * something you do not have which may or may not
231 * be a commit.
233 if (!ref_newer(ref->peer_ref->new_sha1,
234 ref->old_sha1)) {
235 error("remote ref '%s' is not a strict "
236 "subset of local ref '%s'.", ref->name,
237 ref->peer_ref->name);
238 continue;
241 memcpy(ref->new_sha1, ref->peer_ref->new_sha1, 20);
242 if (is_zero_sha1(ref->new_sha1)) {
243 error("cannot happen anymore");
244 continue;
246 new_refs++;
247 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
248 new_hex = sha1_to_hex(ref->new_sha1);
249 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
250 fprintf(stderr, "updating '%s'", ref->name);
251 if (strcmp(ref->name, ref->peer_ref->name))
252 fprintf(stderr, " using '%s'", ref->peer_ref->name);
253 fprintf(stderr, "\n from %s\n to %s\n", old_hex, new_hex);
256 packet_flush(out);
257 if (new_refs)
258 pack_objects(out, remote_refs);
259 close(out);
260 return 0;
264 int main(int argc, char **argv)
266 int i, nr_heads = 0;
267 char *dest = NULL;
268 char **heads = NULL;
269 int fd[2], ret;
270 pid_t pid;
272 argv++;
273 for (i = 1; i < argc; i++, argv++) {
274 char *arg = *argv;
276 if (*arg == '-') {
277 if (!strncmp(arg, "--exec=", 7)) {
278 exec = arg + 7;
279 continue;
281 if (!strcmp(arg, "--all")) {
282 send_all = 1;
283 continue;
285 if (!strcmp(arg, "--force")) {
286 force_update = 1;
287 continue;
289 usage(send_pack_usage);
291 if (!dest) {
292 dest = arg;
293 continue;
295 heads = argv;
296 nr_heads = argc - i;
297 break;
299 if (!dest)
300 usage(send_pack_usage);
301 if (heads && send_all)
302 usage(send_pack_usage);
303 pid = git_connect(fd, dest, exec);
304 if (pid < 0)
305 return 1;
306 ret = send_pack(fd[0], fd[1], nr_heads, heads);
307 close(fd[0]);
308 close(fd[1]);
309 finish_connect(pid);
310 return ret;