[PATCH] list shortlog items in commit order
[git/git.git] / send-pack.c
CommitLineData
61221472 1#include "cache.h"
2a9c3fe8 2#include "commit.h"
584c6cc9 3#include "refs.h"
f3a3214e 4#include "pkt-line.h"
61221472 5
2a245013
JH
6static const char send_pack_usage[] =
7"git-send-pack [--exec=git-receive-pack] [host:]directory [heads]*";
61221472 8static const char *exec = "git-receive-pack";
d089391c 9static int send_all = 0;
2a9c3fe8 10static int force_update = 0;
61221472 11
584c6cc9
LT
12static 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
94fdb7aa
LT
23static 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
34static 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");
584c6cc9
LT
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 }
94fdb7aa
LT
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
61static 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
87static 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 */
7ec4e608 102 return 0;
94fdb7aa 103}
e4b5c7ff
LT
104
105static int read_ref(const char *ref, unsigned char *sha1)
106{
107 int fd, ret;
e4b5c7ff 108 char buffer[60];
e4b5c7ff 109
723c31fe 110 fd = open(git_path("%s", ref), O_RDONLY);
e4b5c7ff
LT
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
584c6cc9
LT
120static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
121{
2a9c3fe8
LT
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)
584c6cc9 129 return 0;
2a9c3fe8
LT
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);
bdf25142
LT
137 while (list) {
138 new = pop_most_recent_commit(&list, 1);
2a9c3fe8
LT
139 if (new == old)
140 return 1;
141 }
142 return 0;
584c6cc9
LT
143}
144
145static int local_ref_nr_match;
146static char **local_ref_match;
d089391c
LT
147static struct ref *local_ref_list;
148static struct ref **local_last_ref;
584c6cc9
LT
149
150static int try_to_match(const char *refname, const unsigned char *sha1)
151{
152 struct ref *ref;
153 int len;
154
d089391c
LT
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 }
584c6cc9
LT
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;
d089391c
LT
172 *local_last_ref = ref;
173 local_last_ref = &ref->next;
584c6cc9
LT
174 return 0;
175}
176
d0efc8a7 177static int send_pack(int in, int out, int nr_match, char **match)
61221472 178{
d1c133f5 179 struct ref *ref_list, **last_ref;
7f8e9828 180 struct ref *ref;
584c6cc9 181 int new_refs;
7f8e9828 182
d1c133f5
LT
183 /* First we get all heads, whether matching or not.. */
184 last_ref = get_remote_heads(in, &ref_list, 0, NULL);
584c6cc9
LT
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
d0efc8a7
LT
194 if (nr_match && !path_match(name, nr_match, match))
195 continue;
584c6cc9 196
7f8e9828 197 if (read_ref(name, new_sha1) < 0)
584c6cc9
LT
198 continue;
199
584c6cc9 200 if (!memcmp(ref->old_sha1, new_sha1, 20)) {
e4b5c7ff 201 fprintf(stderr, "'%s' unchanged\n", name);
7f8e9828 202 continue;
e4b5c7ff 203 }
584c6cc9
LT
204
205 if (!ref_newer(new_sha1, ref->old_sha1)) {
2a9c3fe8 206 error("remote '%s' isn't a strict parent of local", name);
584c6cc9
LT
207 continue;
208 }
209
210 /* Ok, mark it for update */
7f8e9828 211 memcpy(ref->new_sha1, new_sha1, 20);
7f8e9828
LT
212 }
213
584c6cc9
LT
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;
d089391c
LT
220 local_ref_list = ref_list;
221 local_last_ref = last_ref;
584c6cc9
LT
222 for_each_ref(try_to_match);
223 }
224
225 /*
226 * Finally, tell the other end!
227 */
228 new_refs = 0;
7f8e9828
LT
229 for (ref = ref_list; ref; ref = ref->next) {
230 char old_hex[60], *new_hex;
584c6cc9
LT
231 if (is_zero_sha1(ref->new_sha1))
232 continue;
233 new_refs++;
7f8e9828
LT
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);
61221472 238 }
7f8e9828 239
f3a3214e 240 packet_flush(out);
584c6cc9 241 if (new_refs)
94fdb7aa 242 pack_objects(out, ref_list);
61221472
LT
243 close(out);
244 return 0;
245}
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);
f7192598 284 pid = git_connect(fd, dest, exec);
7f8e9828 285 if (pid < 0)
61221472 286 return 1;
d0efc8a7 287 ret = send_pack(fd[0], fd[1], nr_heads, heads);
7f8e9828
LT
288 close(fd[0]);
289 close(fd[1]);
f7192598 290 finish_connect(pid);
7f8e9828 291 return ret;
61221472 292}