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;
15 static int is_zero_sha1(const unsigned char *sha1
)
19 for (i
= 0; i
< 20; i
++) {
26 static void exec_pack_objects(void)
28 static char *args
[] = {
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];
42 args
[i
++] = "git-rev-list"; /* 0 */
43 args
[i
++] = "--objects"; /* 1 */
45 char *buf
= malloc(100);
47 die("git-rev-list environment overflow");
48 if (!is_zero_sha1(refs
->old_sha1
) &&
49 has_sha1_file(refs
->old_sha1
)) {
51 snprintf(buf
, 50, "^%s", sha1_to_hex(refs
->old_sha1
));
54 if (!is_zero_sha1(refs
->new_sha1
)) {
56 snprintf(buf
, 50, "%s", sha1_to_hex(refs
->new_sha1
));
61 execvp("git-rev-list", args
);
62 die("git-rev-list exec failed (%s)", strerror(errno
));
65 static void rev_list(int fd
, struct ref
*refs
)
68 pid_t pack_objects_pid
;
70 if (pipe(pipe_fd
) < 0)
71 die("rev-list setup: pipe failed");
72 pack_objects_pid
= fork();
73 if (!pack_objects_pid
) {
80 die("pack-objects setup failed");
82 if (pack_objects_pid
< 0)
83 die("pack-objects fork failed");
91 static int pack_objects(int fd
, struct ref
*refs
)
95 rev_list_pid
= fork();
98 die("rev-list setup failed");
100 if (rev_list_pid
< 0)
101 die("rev-list fork failed");
103 * We don't wait for the rev-list pipeline in the parent:
104 * we end up waiting for the other end instead
109 static void unmark_and_free(struct commit_list
*list
, unsigned int mark
)
112 struct commit_list
*temp
= list
;
113 temp
->item
->object
.flags
&= ~mark
;
119 static int ref_newer(const unsigned char *new_sha1
,
120 const unsigned char *old_sha1
)
123 struct commit
*old
, *new;
124 struct commit_list
*list
, *used
;
127 /* Both new and old must be commit-ish and new is descendant of
128 * old. Otherwise we require --force.
130 o
= deref_tag(parse_object(old_sha1
), NULL
, 0);
131 if (!o
|| o
->type
!= commit_type
)
133 old
= (struct commit
*) o
;
135 o
= deref_tag(parse_object(new_sha1
), NULL
, 0);
136 if (!o
|| o
->type
!= commit_type
)
138 new = (struct commit
*) o
;
140 if (parse_commit(new) < 0)
144 commit_list_insert(new, &list
);
146 new = pop_most_recent_commit(&list
, 1);
147 commit_list_insert(new, &used
);
153 unmark_and_free(list
, 1);
154 unmark_and_free(used
, 1);
158 static struct ref
*local_refs
, **local_tail
;
159 static struct ref
*remote_refs
, **remote_tail
;
161 static int one_local_ref(const char *refname
, const unsigned char *sha1
)
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
);
169 local_tail
= &ref
->next
;
173 static void get_local_heads(void)
175 local_tail
= &local_refs
;
176 for_each_ref(one_local_ref
);
179 static int send_pack(int in
, int out
, int nr_refspec
, char **refspec
)
185 /* No funny business with the matcher */
186 remote_tail
= get_remote_heads(in
, &remote_refs
, 0, NULL
, 1);
191 remote_tail
= &remote_refs
;
192 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
193 nr_refspec
, refspec
, send_all
))
197 fprintf(stderr
, "No refs in common and none specified; doing nothing.\n");
202 * Finally, tell the other end!
205 for (ref
= remote_refs
; ref
; ref
= ref
->next
) {
206 char old_hex
[60], *new_hex
;
209 if (!memcmp(ref
->old_sha1
, ref
->peer_ref
->new_sha1
, 20)) {
211 fprintf(stderr
, "'%s': up-to-date\n", ref
->name
);
215 /* This part determines what can overwrite what.
218 * (0) you can always use --force or +A:B notation to
219 * selectively force individual ref pairs.
221 * (1) if the old thing does not exist, it is OK.
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
227 * (3) if both new and old are commit-ish, and new is a
228 * descendant of old, it is OK.
232 !is_zero_sha1(ref
->old_sha1
) &&
234 if (!has_sha1_file(ref
->old_sha1
)) {
235 error("remote '%s' object %s does not "
237 ref
->name
, sha1_to_hex(ref
->old_sha1
));
242 /* We assume that local is fsck-clean. Otherwise
243 * you _could_ have an old tag which points at
244 * something you do not have, which may or may not
247 if (!ref_newer(ref
->peer_ref
->new_sha1
,
249 error("remote ref '%s' is not a strict "
250 "subset of local ref '%s'.", ref
->name
,
251 ref
->peer_ref
->name
);
256 memcpy(ref
->new_sha1
, ref
->peer_ref
->new_sha1
, 20);
257 if (is_zero_sha1(ref
->new_sha1
)) {
258 error("cannot happen anymore");
263 strcpy(old_hex
, sha1_to_hex(ref
->old_sha1
));
264 new_hex
= sha1_to_hex(ref
->new_sha1
);
265 packet_write(out
, "%s %s %s", old_hex
, new_hex
, ref
->name
);
266 fprintf(stderr
, "updating '%s'", ref
->name
);
267 if (strcmp(ref
->name
, ref
->peer_ref
->name
))
268 fprintf(stderr
, " using '%s'", ref
->peer_ref
->name
);
269 fprintf(stderr
, "\n from %s\n to %s\n", old_hex
, new_hex
);
274 pack_objects(out
, remote_refs
);
276 fprintf(stderr
, "Everything up-to-date\n");
282 int main(int argc
, char **argv
)
290 setup_git_directory();
292 for (i
= 1; i
< argc
; i
++, argv
++) {
296 if (!strncmp(arg
, "--exec=", 7)) {
300 if (!strcmp(arg
, "--all")) {
304 if (!strcmp(arg
, "--force")) {
308 if (!strcmp(arg
, "--verbose")) {
312 usage(send_pack_usage
);
323 usage(send_pack_usage
);
324 if (heads
&& send_all
)
325 usage(send_pack_usage
);
326 pid
= git_connect(fd
, dest
, exec
);
329 ret
= send_pack(fd
[0], fd
[1], nr_heads
, heads
);