1#include "cache.h"
2#include "refs.h"
3#include "pkt-line.h"
45
static const char send_pack_usage[] =
6"git-send-pack [--exec=git-receive-pack] [host:]directory [heads]*";
7static const char *exec = "git-receive-pack";
89
struct ref {
10struct ref *next;
11unsigned char old_sha1[20];
12unsigned char new_sha1[20];
13char name[0];
14};
1516
static int is_zero_sha1(const unsigned char *sha1)
17{
18int i;
1920
for (i = 0; i < 20; i++) {
21if (*sha1++)
22return 0;
23}
24return 1;
25}
2627
static void exec_pack_objects(void)
28{
29static char *args[] = {
30"git-pack-objects",
31"--stdout",
32NULL
33};
34execvp("git-pack-objects", args);
35die("git-pack-objects exec failed (%s)", strerror(errno));
36}
3738
static void exec_rev_list(struct ref *refs)
39{
40static char *args[1000];
41int i = 0;
4243
args[i++] = "git-rev-list"; /* 0 */
44args[i++] = "--objects"; /* 1 */
45while (refs) {
46char *buf = malloc(100);
47if (i > 900)
48die("git-rev-list environment overflow");
49if (!is_zero_sha1(refs->old_sha1)) {
50args[i++] = buf;
51snprintf(buf, 50, "^%s", sha1_to_hex(refs->old_sha1));
52buf += 50;
53}
54if (!is_zero_sha1(refs->new_sha1)) {
55args[i++] = buf;
56snprintf(buf, 50, "%s", sha1_to_hex(refs->new_sha1));
57}
58refs = refs->next;
59}
60args[i] = NULL;
61execvp("git-rev-list", args);
62die("git-rev-list exec failed (%s)", strerror(errno));
63}
6465
static void rev_list(int fd, struct ref *refs)
66{
67int pipe_fd[2];
68pid_t pack_objects_pid;
6970
if (pipe(pipe_fd) < 0)
71die("rev-list setup: pipe failed");
72pack_objects_pid = fork();
73if (!pack_objects_pid) {
74dup2(pipe_fd[0], 0);
75dup2(fd, 1);
76close(pipe_fd[0]);
77close(pipe_fd[1]);
78close(fd);
79exec_pack_objects();
80die("pack-objects setup failed");
81}
82if (pack_objects_pid < 0)
83die("pack-objects fork failed");
84dup2(pipe_fd[1], 1);
85close(pipe_fd[0]);
86close(pipe_fd[1]);
87close(fd);
88exec_rev_list(refs);
89}
9091
static int pack_objects(int fd, struct ref *refs)
92{
93pid_t rev_list_pid;
9495
rev_list_pid = fork();
96if (!rev_list_pid) {
97rev_list(fd, refs);
98die("rev-list setup failed");
99}
100if (rev_list_pid < 0)
101die("rev-list fork failed");
102/*
103* We don't wait for the rev-list pipeline in the parent:
104* we end up waiting for the other end instead
105*/
106return 0;
107}
108109
static int read_ref(const char *ref, unsigned char *sha1)
110{
111int fd, ret;
112char buffer[60];
113114
fd = open(git_path("%s", ref), O_RDONLY);
115if (fd < 0)
116return -1;
117ret = -1;
118if (read(fd, buffer, sizeof(buffer)) >= 40)
119ret = get_sha1_hex(buffer, sha1);
120close(fd);
121return ret;
122}
123124
static int ref_newer(const unsigned char *new_sha1, const unsigned char *old_sha1)
125{
126if (!has_sha1_file(old_sha1))
127return 0;
128/*
129* FIXME! It is not correct to say that the new one is newer
130* just because we don't have the old one!
131*
132* We should really see if we can reach the old_sha1 commit
133* from the new_sha1 one.
134*/
135return 1;
136}
137138
static int local_ref_nr_match;
139static char **local_ref_match;
140static struct ref **local_ref_list;
141142
static int try_to_match(const char *refname, const unsigned char *sha1)
143{
144struct ref *ref;
145int len;
146147
if (!path_match(refname, local_ref_nr_match, local_ref_match))
148return 0;
149150
len = strlen(refname)+1;
151ref = xmalloc(sizeof(*ref) + len);
152memset(ref->old_sha1, 0, 20);
153memcpy(ref->new_sha1, sha1, 20);
154memcpy(ref->name, refname, len);
155ref->next = NULL;
156*local_ref_list = ref;
157local_ref_list = &ref->next;
158return 0;
159}
160161
static int send_pack(int in, int out, int nr_match, char **match)
162{
163struct ref *ref_list = NULL, **last_ref = &ref_list;
164struct ref *ref;
165int new_refs;
166167
/*
168* Read all the refs from the other end
169*/
170for (;;) {
171unsigned char old_sha1[20];
172static char buffer[1000];
173char *name;
174int len;
175176
len = packet_read_line(in, buffer, sizeof(buffer));
177if (!len)
178break;
179if (buffer[len-1] == '\n')
180buffer[--len] = 0;
181182
if (len < 42 || get_sha1_hex(buffer, old_sha1) || buffer[40] != ' ')
183die("protocol error: expected sha/ref, got '%s'", buffer);
184name = buffer + 41;
185ref = xmalloc(sizeof(*ref) + len - 40);
186memcpy(ref->old_sha1, old_sha1, 20);
187memset(ref->new_sha1, 0, 20);
188memcpy(ref->name, buffer + 41, len - 40);
189ref->next = NULL;
190*last_ref = ref;
191last_ref = &ref->next;
192}
193194
/*
195* Go through the refs, see if we want to update
196* any of them..
197*/
198for (ref = ref_list; ref; ref = ref->next) {
199unsigned char new_sha1[20];
200char *name = ref->name;
201202
if (nr_match && !path_match(name, nr_match, match))
203continue;
204205
if (read_ref(name, new_sha1) < 0)
206continue;
207208
if (!memcmp(ref->old_sha1, new_sha1, 20)) {
209fprintf(stderr, "'%s' unchanged\n", name);
210continue;
211}
212213
if (!ref_newer(new_sha1, ref->old_sha1)) {
214error("remote '%s' points to object I don't have", name);
215continue;
216}
217218
/* Ok, mark it for update */
219memcpy(ref->new_sha1, new_sha1, 20);
220}
221222
/*
223* See if we have any refs that the other end didn't have
224*/
225if (nr_match) {
226local_ref_nr_match = nr_match;
227local_ref_match = match;
228local_ref_list = last_ref;
229for_each_ref(try_to_match);
230}
231232
/*
233* Finally, tell the other end!
234*/
235new_refs = 0;
236for (ref = ref_list; ref; ref = ref->next) {
237char old_hex[60], *new_hex;
238if (is_zero_sha1(ref->new_sha1))
239continue;
240new_refs++;
241strcpy(old_hex, sha1_to_hex(ref->old_sha1));
242new_hex = sha1_to_hex(ref->new_sha1);
243packet_write(out, "%s %s %s", old_hex, new_hex, ref->name);
244fprintf(stderr, "'%s': updating from %s to %s\n", ref->name, old_hex, new_hex);
245}
246247
packet_flush(out);
248if (new_refs)
249pack_objects(out, ref_list);
250close(out);
251return 0;
252}
253254
int main(int argc, char **argv)
255{
256int i, nr_heads = 0;
257char *dest = NULL;
258char **heads = NULL;
259int fd[2], ret;
260pid_t pid;
261262
argv++;
263for (i = 1; i < argc; i++) {
264char *arg = *argv++;
265266
if (*arg == '-') {
267if (!strncmp(arg, "--exec=", 7)) {
268exec = arg + 7;
269continue;
270}
271usage(send_pack_usage);
272}
273dest = arg;
274heads = argv;
275nr_heads = argc - i -1;
276break;
277}
278if (!dest)
279usage(send_pack_usage);
280pid = git_connect(fd, dest, exec);
281if (pid < 0)
282return 1;
283ret = send_pack(fd[0], fd[1], nr_heads, heads);
284close(fd[0]);
285close(fd[1]);
286finish_connect(pid);
287return ret;
288}