Make "git resolve" take the merge message in $3
[git.git] / fetch-pack.c
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include <sys/wait.h>
5
6 static const char fetch_pack_usage[] = "git-fetch-pack [host:]directory [heads]* < mycommitlist";
7 static const char *exec = "git-upload-pack";
8
9 static int find_common(int fd[2], unsigned char *result_sha1, unsigned char *remote)
10 {
11         static char line[1000];
12         int count = 0, flushes = 0, retval;
13         FILE *revs;
14
15         revs = popen("git-rev-list $(git-rev-parse --all)", "r");
16         if (!revs)
17                 die("unable to run 'git-rev-list'");
18         packet_write(fd[1], "want %s\n", sha1_to_hex(remote));
19         packet_flush(fd[1]);
20         flushes = 1;
21         retval = -1;
22         while (fgets(line, sizeof(line), revs) != NULL) {
23                 unsigned char sha1[20];
24                 if (get_sha1_hex(line, sha1))
25                         die("git-fetch-pack: expected object name, got crud");
26                 packet_write(fd[1], "have %s\n", sha1_to_hex(sha1));
27                 if (!(31 & ++count)) {
28                         packet_flush(fd[1]);
29                         flushes++;
30
31                         /*
32                          * We keep one window "ahead" of the other side, and
33                          * will wait for an ACK only on the next one
34                          */
35                         if (count == 32)
36                                 continue;
37                         if (get_ack(fd[0], result_sha1)) {
38                                 flushes = 0;
39                                 retval = 0;
40                                 break;
41                         }
42                         flushes--;
43                 }
44         }
45         pclose(revs);
46         packet_write(fd[1], "done\n");
47         while (flushes) {
48                 flushes--;
49                 if (get_ack(fd[0], result_sha1))
50                         return 0;
51         }
52         return retval;
53 }
54
55 static int get_remote_heads(int fd, int nr_match, char **match, unsigned char *result)
56 {
57         int count = 0;
58
59         for (;;) {
60                 static char line[1000];
61                 unsigned char sha1[20];
62                 char *refname;
63                 int len;
64
65                 len = packet_read_line(fd, line, sizeof(line));
66                 if (!len)
67                         break;
68                 if (line[len-1] == '\n')
69                         line[--len] = 0;
70                 if (len < 42 || get_sha1_hex(line, sha1))
71                         die("git-fetch-pack: protocol error - expected ref descriptor, got '%s¤'", line);
72                 refname = line+41;
73                 if (nr_match && !path_match(refname, nr_match, match))
74                         continue;
75                 count++;
76                 memcpy(result, sha1, 20);
77         }
78         return count;
79 }
80
81 static int fetch_pack(int fd[2], int nr_match, char **match)
82 {
83         unsigned char sha1[20], remote[20];
84         int heads, status;
85         pid_t pid;
86
87         heads = get_remote_heads(fd[0], nr_match, match, remote);
88         if (heads != 1) {
89                 packet_flush(fd[1]);
90                 die(heads ? "multiple remote heads" : "no matching remote head");
91         }
92         if (find_common(fd, sha1, remote) < 0)
93                 die("git-fetch-pack: no common commits");
94         pid = fork();
95         if (pid < 0)
96                 die("git-fetch-pack: unable to fork off git-unpack-objects");
97         if (!pid) {
98                 close(fd[1]);
99                 dup2(fd[0], 0);
100                 close(fd[0]);
101                 execlp("git-unpack-objects", "git-unpack-objects", NULL);
102                 die("git-unpack-objects exec failed");
103         }
104         close(fd[0]);
105         close(fd[1]);
106         while (waitpid(pid, &status, 0) < 0) {
107                 if (errno != EINTR)
108                         die("waiting for git-unpack-objects: %s", strerror(errno));
109         }
110         if (WIFEXITED(status)) {
111                 int code = WEXITSTATUS(status);
112                 if (code)
113                         die("git-unpack-objects died with error code %d", code);
114                 puts(sha1_to_hex(remote));
115                 return 0;
116         }
117         if (WIFSIGNALED(status)) {
118                 int sig = WTERMSIG(status);
119                 die("git-unpack-objects died of signal %d", sig);
120         }
121         die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
122 }
123
124 int main(int argc, char **argv)
125 {
126         int i, ret, nr_heads;
127         char *dest = NULL, **heads;
128         int fd[2];
129         pid_t pid;
130
131         nr_heads = 0;
132         heads = NULL;
133         for (i = 1; i < argc; i++) {
134                 char *arg = argv[i];
135
136                 if (*arg == '-') {
137                         /* Arguments go here */
138                         usage(fetch_pack_usage);
139                 }
140                 dest = arg;
141                 heads = argv + i + 1;
142                 nr_heads = argc - i - 1;
143                 break;
144         }
145         if (!dest)
146                 usage(fetch_pack_usage);
147         pid = git_connect(fd, dest, exec);
148         if (pid < 0)
149                 return 1;
150         ret = fetch_pack(fd, nr_heads, heads);
151         close(fd[0]);
152         close(fd[1]);
153         finish_connect(pid);
154         return ret;
155 }