Update the documentation for git-tag-script to reflect current behavior.
[git.git] / send-pack.c
blob2a7ab2d2b9428adcbe740390e78f164b462bb717
1 #include "cache.h"
2 #include "commit.h"
3 #include "refs.h"
4 #include "pkt-line.h"
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;
12 static int is_zero_sha1(const unsigned char *sha1)
14 int i;
16 for (i = 0; i < 20; i++) {
17 if (*sha1++)
18 return 0;
20 return 1;
23 static void exec_pack_objects(void)
25 static char *args[] = {
26 "git-pack-objects",
27 "--stdout",
28 NULL
30 execvp("git-pack-objects", args);
31 die("git-pack-objects exec failed (%s)", strerror(errno));
34 static void exec_rev_list(struct ref *refs)
36 static char *args[1000];
37 int i = 0;
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;
50 if (!is_zero_sha1(refs->new_sha1)) {
51 args[i++] = buf;
52 snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
54 refs = refs->next;
56 args[i] = NULL;
57 execvp("git-rev-list", args);
58 die("git-rev-list exec failed (%s)", strerror(errno));
61 static void rev_list(int fd, struct ref *refs)
63 int pipe_fd[2];
64 pid_t pack_objects_pid;
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");
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);
87 static int pack_objects(int fd, struct ref *refs)
89 pid_t rev_list_pid;
91 rev_list_pid = fork();
92 if (!rev_list_pid) {
93 rev_list(fd, refs);
94 die("rev-list setup failed");
96 if (rev_list_pid < 0)
97 die("rev-list fork failed");
99 * We don't wait for the rev-list pipeline in the parent:
100 * we end up waiting for the other end instead
102 return 0;
105 static int read_ref(const char *ref, unsigned char *sha1)
107 int fd, ret;
108 char buffer[60];
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;
120 static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
122 struct commit *new, *old;
123 struct commit_list *list;
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 ((new = pop_most_recent_commit(&list, 1)) != NULL) {
138 if (new == old)
139 return 1;
141 return 0;
144 static int local_ref_nr_match;
145 static char **local_ref_match;
146 static struct ref *local_ref_list;
147 static struct ref **local_last_ref;
149 static int try_to_match(const char *refname, const unsigned char *sha1)
151 struct ref *ref;
152 int len;
154 if (!path_match(refname, local_ref_nr_match, local_ref_match)) {
155 if (!send_all)
156 return 0;
158 /* If we have it listed already, skip it */
159 for (ref = local_ref_list ; ref ; ref = ref->next) {
160 if (!strcmp(ref->name, refname))
161 return 0;
165 len = strlen(refname)+1;
166 ref = xmalloc(sizeof(*ref) + len);
167 memset(ref->old_sha1, 0, 20);
168 memcpy(ref->new_sha1, sha1, 20);
169 memcpy(ref->name, refname, len);
170 ref->next = NULL;
171 *local_last_ref = ref;
172 local_last_ref = &ref->next;
173 return 0;
176 static int send_pack(int in, int out, int nr_match, char **match)
178 struct ref *ref_list, **last_ref;
179 struct ref *ref;
180 int new_refs;
182 /* First we get all heads, whether matching or not.. */
183 last_ref = get_remote_heads(in, &ref_list, 0, NULL);
186 * Go through the refs, see if we want to update
187 * any of them..
189 for (ref = ref_list; ref; ref = ref->next) {
190 unsigned char new_sha1[20];
191 char *name = ref->name;
193 if (nr_match && !path_match(name, nr_match, match))
194 continue;
196 if (read_ref(name, new_sha1) < 0)
197 continue;
199 if (!memcmp(ref->old_sha1, new_sha1, 20)) {
200 fprintf(stderr, "'%s' unchanged\n", name);
201 continue;
204 if (!ref_newer(new_sha1, ref->old_sha1)) {
205 error("remote '%s' isn't a strict parent of local", name);
206 continue;
209 /* Ok, mark it for update */
210 memcpy(ref->new_sha1, new_sha1, 20);
214 * See if we have any refs that the other end didn't have
216 if (nr_match) {
217 local_ref_nr_match = nr_match;
218 local_ref_match = match;
219 local_ref_list = ref_list;
220 local_last_ref = last_ref;
221 for_each_ref(try_to_match);
225 * Finally, tell the other end!
227 new_refs = 0;
228 for (ref = ref_list; ref; ref = ref->next) {
229 char old_hex[60], *new_hex;
230 if (is_zero_sha1(ref->new_sha1))
231 continue;
232 new_refs++;
233 strcpy(old_hex, sha1_to_hex(ref->old_sha1));
234 new_hex = sha1_to_hex(ref->new_sha1);
235 packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
236 fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
239 packet_flush(out);
240 if (new_refs)
241 pack_objects(out, ref_list);
242 close(out);
243 return 0;
246 int main(int argc, char **argv)
248 int i, nr_heads = 0;
249 char *dest = NULL;
250 char **heads = NULL;
251 int fd[2], ret;
252 pid_t pid;
254 argv++;
255 for (i = 1; i < argc; i++, argv++) {
256 char *arg = *argv;
258 if (*arg == '-') {
259 if (!strncmp(arg, "--exec=", 7)) {
260 exec = arg + 7;
261 continue;
263 if (!strcmp(arg, "--all")) {
264 send_all = 1;
265 continue;
267 if (!strcmp(arg, "--force")) {
268 force_update = 1;
269 continue;
271 usage(send_pack_usage);
273 if (!dest) {
274 dest = arg;
275 continue;
277 heads = argv;
278 nr_heads = argc - i;
279 break;
281 if (!dest)
282 usage(send_pack_usage);
283 pid = git_connect(fd, dest, exec);
284 if (pid < 0)
285 return 1;
286 ret = send_pack(fd[0], fd[1], nr_heads, heads);
287 close(fd[0]);
288 close(fd[1]);
289 finish_connect(pid);
290 return ret;