6 static const char fetch_pack_usage
[] = "git-fetch-pack [host:]directory [heads]* < mycommitlist";
7 static const char *exec
= "git-upload-pack";
9 static int get_ack(int fd
, unsigned char *result_sha1
)
11 static char line
[1000];
12 int len
= packet_read_line(fd
, line
, sizeof(line
));
15 die("git-fetch-pack: expected ACK/NAK, got EOF");
16 if (line
[len
-1] == '\n')
18 if (!strcmp(line
, "NAK"))
20 if (!strncmp(line
, "ACK ", 3)) {
21 if (!get_sha1_hex(line
+4, result_sha1
))
24 die("git-fetch_pack: expected ACK/NAK, got '%s'", line
);
27 static int find_common(int fd
[2], unsigned char *result_sha1
, unsigned char *remote
)
29 static char line
[1000];
30 int count
= 0, flushes
= 0, retval
;
33 revs
= popen("git-rev-list $(git-rev-parse --all)", "r");
35 die("unable to run 'git-rev-list'");
36 packet_write(fd
[1], "want %s\n", sha1_to_hex(remote
));
40 while (fgets(line
, sizeof(line
), revs
) != NULL
) {
41 unsigned char sha1
[20];
42 if (get_sha1_hex(line
, sha1
))
43 die("git-fetch-pack: expected object name, got crud");
44 packet_write(fd
[1], "have %s\n", sha1_to_hex(sha1
));
45 if (!(31 & ++count
)) {
50 * We keep one window "ahead" of the other side, and
51 * will wait for an ACK only on the next one
55 if (get_ack(fd
[0], result_sha1
)) {
64 packet_write(fd
[1], "done\n");
67 if (get_ack(fd
[0], result_sha1
))
73 static int get_old_sha1(const char *refname
, unsigned char *sha1
)
75 static char pathname
[PATH_MAX
];
79 git_dir
= gitenv(GIT_DIR_ENVIRONMENT
) ? : DEFAULT_GIT_DIR_ENVIRONMENT
;
80 snprintf(pathname
, sizeof(pathname
), "%s/%s", git_dir
, refname
);
81 fd
= open(pathname
, O_RDONLY
);
85 if (read(fd
, buffer
, sizeof(buffer
)) >= 40)
86 ret
= get_sha1_hex(buffer
, sha1
);
92 static int check_ref(const char *refname
, const unsigned char *sha1
)
94 unsigned char mysha1
[20];
97 if (get_old_sha1(refname
, mysha1
) < 0)
98 memset(mysha1
, 0, 20);
100 if (!memcmp(sha1
, mysha1
, 20)) {
101 fprintf(stderr
, "%s: unchanged\n", refname
);
105 memcpy(oldhex
, sha1_to_hex(mysha1
), 41);
106 fprintf(stderr
, "%s: %s (%s)\n", refname
, sha1_to_hex(sha1
), oldhex
);
110 static int get_remote_heads(int fd
, int nr_match
, char **match
, unsigned char *result
)
115 static char line
[1000];
116 unsigned char sha1
[20];
120 len
= packet_read_line(fd
, line
, sizeof(line
));
123 if (line
[len
-1] == '\n')
125 if (len
< 42 || get_sha1_hex(line
, sha1
))
126 die("git-fetch-pack: protocol error - expected ref descriptor, got '%sä'", line
);
128 if (nr_match
&& !path_match(refname
, nr_match
, match
))
130 if (check_ref(refname
, sha1
)) {
132 memcpy(result
, sha1
, 20);
138 static int fetch_pack(int fd
[2], int nr_match
, char **match
)
140 unsigned char sha1
[20], remote
[20];
144 heads
= get_remote_heads(fd
[0], nr_match
, match
, remote
);
147 die(heads
? "multiple remote heads" : "no matching remote head");
149 if (find_common(fd
, sha1
, remote
) < 0)
150 die("git-fetch-pack: no common commits");
153 die("git-fetch-pack: unable to fork off git-unpack-objects");
158 execlp("git-unpack-objects", "git-unpack-objects", NULL
);
159 die("git-unpack-objects exec failed");
163 while (waitpid(pid
, &status
, 0) < 0) {
165 die("waiting for git-unpack-objects: %s", strerror(errno
));
167 if (WIFEXITED(status
)) {
168 int code
= WEXITSTATUS(status
);
170 die("git-unpack-objects died with error code %d", code
);
171 puts(sha1_to_hex(remote
));
174 if (WIFSIGNALED(status
)) {
175 int sig
= WTERMSIG(status
);
176 die("git-unpack-objects died of signal %d", sig
);
178 die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status
);
181 int main(int argc
, char **argv
)
183 int i
, ret
, nr_heads
;
184 char *dest
= NULL
, **heads
;
190 for (i
= 1; i
< argc
; i
++) {
194 /* Arguments go here */
195 usage(fetch_pack_usage
);
198 heads
= argv
+ i
+ 1;
199 nr_heads
= argc
- i
- 1;
203 usage(fetch_pack_usage
);
204 pid
= git_connect(fd
, dest
, exec
);
207 ret
= fetch_pack(fd
, nr_heads
, heads
);