[PATCH] document git-rev-list better
[git.git] / send-pack.c
1 #include "cache.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5
6 static const char send_pack_usage[] =
7 "git-send-pack [--exec=git-receive-pack] [host:]directory [heads]*";
8 static const char *exec = "git-receive-pack";
9 static int send_all = 0;
10 static int force_update = 0;
11
12 static int is_zero_sha1(const unsigned char *sha1)
13 {
14         int i;
15
16         for (i = 0; i < 20; i++) {
17                 if (*sha1++)
18                         return 0;
19         }
20         return 1;
21 }
22
23 static void exec_pack_objects(void)
24 {
25         static char *args[] = {
26                 "git-pack-objects",
27                 "--stdout",
28                 NULL
29         };
30         execvp("git-pack-objects", args);
31         die("git-pack-objects exec failed (%s)", strerror(errno));
32 }
33
34 static void exec_rev_list(struct ref *refs)
35 {
36         static char *args[1000];
37         int i = 0;
38
39         args[i++] = "git-rev-list";     /* 0 */
40         args[i++] = "--objects";        /* 1 */
41         while (refs) {
42                 char *buf = malloc(100);
43                 if (i > 900)
44                         die("git-rev-list environment overflow");
45                 if (!is_zero_sha1(refs->old_sha1)) {
46                         args[i++] = buf;
47                         snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
48                         buf += 50;
49                 }
50                 if (!is_zero_sha1(refs->new_sha1)) {
51                         args[i++] = buf;
52                         snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
53                 }
54                 refs = refs->next;
55         }
56         args[i] = NULL;
57         execvp("git-rev-list", args);
58         die("git-rev-list exec failed (%s)", strerror(errno));
59 }
60
61 static void rev_list(int fd, struct ref *refs)
62 {
63         int pipe_fd[2];
64         pid_t pack_objects_pid;
65
66         if (pipe(pipe_fd) < 0)
67                 die("rev-list setup: pipe failed");
68         pack_objects_pid = fork();
69         if (!pack_objects_pid) {
70                 dup2(pipe_fd[0], 0);
71                 dup2(fd, 1);
72                 close(pipe_fd[0]);
73                 close(pipe_fd[1]);
74                 close(fd);
75                 exec_pack_objects();
76                 die("pack-objects setup failed");
77         }
78         if (pack_objects_pid < 0)
79                 die("pack-objects fork failed");
80         dup2(pipe_fd[1], 1);
81         close(pipe_fd[0]);
82         close(pipe_fd[1]);
83         close(fd);
84         exec_rev_list(refs);
85 }
86
87 static int pack_objects(int fd, struct ref *refs)
88 {
89         pid_t rev_list_pid;
90
91         rev_list_pid = fork();
92         if (!rev_list_pid) {
93                 rev_list(fd, refs);
94                 die("rev-list setup failed");
95         }
96         if (rev_list_pid < 0)
97                 die("rev-list fork failed");
98         /*
99          * We don't wait for the rev-list pipeline in the parent:
100          * we end up waiting for the other end instead
101          */
102         return 0;
103 }
104
105 static int read_ref(const char *ref, unsigned char *sha1)
106 {
107         int fd, ret;
108         char buffer[60];
109
110         fd = open(git_path("%s", ref), O_RDONLY);
111         if (fd < 0)
112                 return -1;
113         ret = -1;
114         if (read(fd, buffer, sizeof(buffer)) >= 40)
115                 ret = get_sha1_hex(buffer, sha1);
116         close(fd);
117         return ret;
118 }
119
120 static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
121 {
122         struct commit *new, *old;
123         struct commit_list *list;
124
125         if (force_update)
126                 return 1;
127         old = lookup_commit_reference(old_sha1);
128         if (!old)
129                 return 0;
130         new = lookup_commit_reference(new_sha1);
131         if (!new)
132                 return 0;
133         if (parse_commit(new) < 0)
134                 return 0;
135         list = NULL;
136         commit_list_insert(new, &list);
137         while (list) {
138                 new = pop_most_recent_commit(&list, 1);
139                 if (new == old)
140                         return 1;
141         }
142         return 0;
143 }
144
145 static int local_ref_nr_match;
146 static char **local_ref_match;
147 static struct ref *local_ref_list;
148 static struct ref **local_last_ref;
149
150 static int try_to_match(const char *refname, const unsigned char *sha1)
151 {
152         struct ref *ref;
153         int len;
154
155         if (!path_match(refname, local_ref_nr_match, local_ref_match)) {
156                 if (!send_all)
157                         return 0;
158
159                 /* If we have it listed already, skip it */
160                 for (ref = local_ref_list ; ref ; ref = ref->next) {
161                         if (!strcmp(ref->name, refname))
162                                 return 0;
163                 }
164         }
165
166         len = strlen(refname)+1;
167         ref = xmalloc(sizeof(*ref) + len);
168         memset(ref->old_sha1, 0, 20);
169         memcpy(ref->new_sha1, sha1, 20);
170         memcpy(ref->name, refname, len);
171         ref->next = NULL;
172         *local_last_ref = ref;
173         local_last_ref = &ref->next;
174         return 0;
175 }
176
177 static int send_pack(int in, int out, int nr_match, char **match)
178 {
179         struct ref *ref_list, **last_ref;
180         struct ref *ref;
181         int new_refs;
182
183         /* First we get all heads, whether matching or not.. */
184         last_ref = get_remote_heads(in, &ref_list, 0, NULL);
185
186         /*
187          * Go through the refs, see if we want to update
188          * any of them..
189          */
190         for (ref = ref_list; ref; ref = ref->next) {
191                 unsigned char new_sha1[20];
192                 char *name = ref->name;
193
194                 if (nr_match && !path_match(name, nr_match, match))
195                         continue;
196
197                 if (read_ref(name, new_sha1) < 0)
198                         continue;
199
200                 if (!memcmp(ref->old_sha1, new_sha1, 20)) {
201                         fprintf(stderr, "'%s' unchanged\n", name);
202                         continue;
203                 }
204
205                 if (!ref_newer(new_sha1, ref->old_sha1)) {
206                         error("remote '%s' isn't a strict parent of local", name);
207                         continue;
208                 }
209
210                 /* Ok, mark it for update */
211                 memcpy(ref->new_sha1, new_sha1, 20);
212         }
213
214         /*
215          * See if we have any refs that the other end didn't have
216          */
217         if (nr_match) {
218                 local_ref_nr_match = nr_match;
219                 local_ref_match = match;
220                 local_ref_list = ref_list;
221                 local_last_ref = last_ref;
222                 for_each_ref(try_to_match);
223         }
224
225         /*
226          * Finally, tell the other end!
227          */
228         new_refs = 0;
229         for (ref = ref_list; ref; ref = ref->next) {
230                 char old_hex[60], *new_hex;
231                 if (is_zero_sha1(ref->new_sha1))
232                         continue;
233                 new_refs++;
234                 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
235                 new_hex = sha1_to_hex(ref->new_sha1);
236                 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
237                 fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
238         }
239         
240         packet_flush(out);
241         if (new_refs)
242                 pack_objects(out, ref_list);
243         close(out);
244         return 0;
245 }
246
247 int main(int argc, char **argv)
248 {
249         int i, nr_heads = 0;
250         char *dest = NULL;
251         char **heads = NULL;
252         int fd[2], ret;
253         pid_t pid;
254
255         argv++;
256         for (i = 1; i < argc; i++, argv++) {
257                 char *arg = *argv;
258
259                 if (*arg == '-') {
260                         if (!strncmp(arg, "--exec=", 7)) {
261                                 exec = arg + 7;
262                                 continue;
263                         }
264                         if (!strcmp(arg, "--all")) {
265                                 send_all = 1;
266                                 continue;
267                         }
268                         if (!strcmp(arg, "--force")) {
269                                 force_update = 1;
270                                 continue;
271                         }
272                         usage(send_pack_usage);
273                 }
274                 if (!dest) {
275                         dest = arg;
276                         continue;
277                 }
278                 heads = argv;
279                 nr_heads = argc - i;
280                 break;
281         }
282         if (!dest)
283                 usage(send_pack_usage);
284         pid = git_connect(fd, dest, exec);
285         if (pid < 0)
286                 return 1;
287         ret = send_pack(fd[0], fd[1], nr_heads, heads);
288         close(fd[0]);
289         close(fd[1]);
290         finish_connect(pid);
291         return ret;
292 }