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