Make "git tag" more user-friendly
[alt-git.git] / send-pack.c
blob4c6adabdac808aa70f9040ec6733581f2ee00c0d
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
5 static const char send_pack_usage[] = "git-send-pack [--exec=other] destination [heads]*";
6 static const char *exec = "git-receive-pack";
8 struct ref {
9 struct ref *next;
10 unsigned char old_sha1[20];
11 unsigned char new_sha1[20];
12 char name[0];
15 static int is_zero_sha1(const unsigned char *sha1)
17 int i;
19 for (i = 0; i < 20; i++) {
20 if (*sha1++)
21 return 0;
23 return 1;
26 static void exec_pack_objects(void)
28 static char *args[] = {
29 "git-pack-objects",
30 "--stdout",
31 NULL
33 execvp("git-pack-objects", args);
34 die("git-pack-objects exec failed (%s)", strerror(errno));
37 static void exec_rev_list(struct ref *refs)
39 static char *args[1000];
40 int i = 0;
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 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 int read_ref(const char *ref, unsigned char *sha1)
110 int fd, ret;
111 char buffer[60];
113 fd = open(git_path("%s", ref), O_RDONLY);
114 if (fd < 0)
115 return -1;
116 ret = -1;
117 if (read(fd, buffer, sizeof(buffer)) >= 40)
118 ret = get_sha1_hex(buffer, sha1);
119 close(fd);
120 return ret;
123 static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
125 if (!has_sha1_file(old_sha1))
126 return 0;
128 * FIXME! It is not correct to say that the new one is newer
129 * just because we don't have the old one!
131 * We should really see if we can reach the old_sha1 commit
132 * from the new_sha1 one.
134 return 1;
137 static int local_ref_nr_match;
138 static char **local_ref_match;
139 static struct ref **local_ref_list;
141 static int try_to_match(const char *refname, const unsigned char *sha1)
143 struct ref *ref;
144 int len;
146 if (!path_match(refname, local_ref_nr_match, local_ref_match))
147 return 0;
149 len = strlen(refname)+1;
150 ref = xmalloc(sizeof(*ref) + len);
151 memset(ref->old_sha1, 0, 20);
152 memcpy(ref->new_sha1, sha1, 20);
153 memcpy(ref->name, refname, len);
154 ref->next = NULL;
155 *local_ref_list = ref;
156 local_ref_list = &ref->next;
157 return 0;
160 static int send_pack(int in, int out, int nr_match, char **match)
162 struct ref *ref_list = NULL, **last_ref = &ref_list;
163 struct ref *ref;
164 int new_refs;
167 * Read all the refs from the other end
169 for (;;) {
170 unsigned char old_sha1[20];
171 static char buffer[1000];
172 char *name;
173 int len;
175 len = packet_read_line(in, buffer, sizeof(buffer));
176 if (!len)
177 break;
178 if (buffer[len-1] == '\n')
179 buffer[--len] = 0;
181 if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
182 die("protocol error: expected sha/ref, got '%s'", buffer);
183 name = buffer + 41;
184 ref = xmalloc(sizeof(*ref) + len - 40);
185 memcpy(ref->old_sha1, old_sha1, 20);
186 memset(ref->new_sha1, 0, 20);
187 memcpy(ref->name, buffer + 41, len - 40);
188 ref->next = NULL;
189 *last_ref = ref;
190 last_ref = &ref->next;
194 * Go through the refs, see if we want to update
195 * any of them..
197 for (ref = ref_list; ref; ref = ref->next) {
198 unsigned char new_sha1[20];
199 char *name = ref->name;
201 if (nr_match && !path_match(name, nr_match, match))
202 continue;
204 if (read_ref(name, new_sha1) < 0)
205 continue;
207 if (nr_match && !path_match(name, nr_match, match))
208 continue;
210 if (!memcmp(ref->old_sha1, new_sha1, 20)) {
211 fprintf(stderr, "'%s' unchanged\n", name);
212 continue;
215 if (!ref_newer(new_sha1, ref->old_sha1)) {
216 error("remote '%s' points to object I don't have", name);
217 continue;
220 /* Ok, mark it for update */
221 memcpy(ref->new_sha1, new_sha1, 20);
225 * See if we have any refs that the other end didn't have
227 if (nr_match) {
228 local_ref_nr_match = nr_match;
229 local_ref_match = match;
230 local_ref_list = last_ref;
231 for_each_ref(try_to_match);
235 * Finally, tell the other end!
237 new_refs = 0;
238 for (ref = ref_list; ref; ref = ref->next) {
239 char old_hex[60], *new_hex;
240 if (is_zero_sha1(ref->new_sha1))
241 continue;
242 new_refs++;
243 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
244 new_hex = sha1_to_hex(ref->new_sha1);
245 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
246 fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
249 packet_flush(out);
250 if (new_refs)
251 pack_objects(out, ref_list);
252 close(out);
253 return 0;
256 int main(int argc, char **argv)
258 int i, nr_heads = 0;
259 char *dest = NULL;
260 char **heads = NULL;
261 int fd[2], ret;
262 pid_t pid;
264 argv++;
265 for (i = 1; i < argc; i++) {
266 char *arg = *argv++;
268 if (*arg == '-') {
269 if (!strncmp(arg, "--exec=", 7)) {
270 exec = arg + 7;
271 continue;
273 usage(send_pack_usage);
275 dest = arg;
276 heads = argv;
277 nr_heads = argc - i -1;
278 break;
280 if (!dest)
281 usage(send_pack_usage);
282 pid = git_connect(fd, dest, exec);
283 if (pid < 0)
284 return 1;
285 ret = send_pack(fd[0], fd[1], nr_heads, heads);
286 close(fd[0]);
287 close(fd[1]);
288 finish_connect(pid);
289 return ret;