git-checkout-index: documentation updates.
[git] / clone-pack.c
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
4 #include <sys/wait.h>
5
6 static int quiet;
7 static const char clone_pack_usage[] = "git-clone-pack [-q] [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
8 static const char *exec = "git-upload-pack";
9
10 static void clone_handshake(int fd[2], struct ref *ref)
11 {
12         unsigned char sha1[20];
13
14         while (ref) {
15                 packet_write(fd[1], "want %s\n", sha1_to_hex(ref->old_sha1));
16                 ref = ref->next;
17         }
18         packet_flush(fd[1]);
19
20         /* We don't have nuttin' */
21         packet_write(fd[1], "done\n");
22         if (get_ack(fd[0], sha1))
23                 error("Huh! git-clone-pack got positive ack for %s", sha1_to_hex(sha1));
24 }
25
26 static int is_master(struct ref *ref)
27 {
28         return !strcmp(ref->name, "refs/heads/master");
29 }
30
31 static void write_one_ref(struct ref *ref)
32 {
33         char *path = git_path("%s", ref->name);
34         int fd;
35         char *hex;
36
37         if (safe_create_leading_directories(path))
38                 die("unable to create leading directory for %s", ref->name);
39         fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);
40         if (fd < 0)
41                 die("unable to create ref %s", ref->name);
42         hex = sha1_to_hex(ref->old_sha1);
43         hex[40] = '\n';
44         if (write(fd, hex, 41) != 41)
45                 die("unable to write ref %s", ref->name);
46         close(fd);
47 }
48
49 static void write_refs(struct ref *ref)
50 {
51         struct ref *head = NULL, *head_ptr, *master_ref;
52         char *head_path;
53
54         /* Upload-pack must report HEAD first */
55         if (!strcmp(ref->name, "HEAD")) {
56                 head = ref;
57                 ref = ref->next;
58         }
59         head_ptr = NULL;
60         master_ref = NULL;
61         while (ref) {
62                 if (is_master(ref))
63                         master_ref = ref;
64                 if (head &&
65                     !memcmp(ref->old_sha1, head->old_sha1, 20) &&
66                     !strncmp(ref->name, "refs/heads/",11) &&
67                     (!head_ptr || ref == master_ref))
68                         head_ptr = ref;
69
70                 write_one_ref(ref);
71                 ref = ref->next;
72         }
73         if (!head) {
74                 fprintf(stderr, "No HEAD in remote.\n");
75                 return;
76         }
77
78         head_path = strdup(git_path("HEAD"));
79         if (!head_ptr) {
80                 /*
81                  * If we had a master ref, and it wasn't HEAD, we need to undo the
82                  * symlink, and write a standalone HEAD. Give a warning, because that's
83                  * really really wrong.
84                  */
85                 if (master_ref) {
86                         error("HEAD doesn't point to any refs! Making standalone HEAD");
87                         unlink(head_path);
88                 }
89                 write_one_ref(head);
90                 free(head_path);
91                 return;
92         }
93
94         /* We reset to the master branch if it's available */
95         if (master_ref)
96                 return;
97
98         fprintf(stderr, "Setting HEAD to %s\n", head_ptr->name);
99
100         /*
101          * Uhhuh. Other end didn't have master. We start HEAD off with
102          * the first branch with the same value.
103          */
104         if (create_symref(head_path, head_ptr->name) < 0)
105                 die("unable to link HEAD to %s", head_ptr->name);
106         free(head_path);
107 }
108
109 static int clone_pack(int fd[2], int nr_match, char **match)
110 {
111         struct ref *refs;
112         int status;
113         pid_t pid;
114
115         get_remote_heads(fd[0], &refs, nr_match, match);
116         if (!refs) {
117                 packet_flush(fd[1]);
118                 die("no matching remote head");
119         }
120         clone_handshake(fd, refs);
121         pid = fork();
122         if (pid < 0)
123                 die("git-clone-pack: unable to fork off git-unpack-objects");
124         if (!pid) {
125                 dup2(fd[0], 0);
126                 close(fd[0]);
127                 close(fd[1]);
128                 execlp("git-unpack-objects", "git-unpack-objects",
129                         quiet ? "-q" : NULL, NULL);
130                 die("git-unpack-objects exec failed");
131         }
132         close(fd[0]);
133         close(fd[1]);
134         while (waitpid(pid, &status, 0) < 0) {
135                 if (errno != EINTR)
136                         die("waiting for git-unpack-objects: %s", strerror(errno));
137         }
138         if (WIFEXITED(status)) {
139                 int code = WEXITSTATUS(status);
140                 if (code)
141                         die("git-unpack-objects died with error code %d", code);
142                 write_refs(refs);
143                 return 0;
144         }
145         if (WIFSIGNALED(status)) {
146                 int sig = WTERMSIG(status);
147                 die("git-unpack-objects died of signal %d", sig);
148         }
149         die("Sherlock Holmes! git-unpack-objects died of unnatural causes %d!", status);
150 }
151
152 int main(int argc, char **argv)
153 {
154         int i, ret, nr_heads;
155         char *dest = NULL, **heads;
156         int fd[2];
157         pid_t pid;
158
159         nr_heads = 0;
160         heads = NULL;
161         for (i = 1; i < argc; i++) {
162                 char *arg = argv[i];
163
164                 if (*arg == '-') {
165                         if (!strcmp("-q", arg)) {
166                                 quiet = 1;
167                                 continue;
168                         }
169                         if (!strncmp("--exec=", arg, 7)) {
170                                 exec = arg + 7;
171                                 continue;
172                         }
173                         usage(clone_pack_usage);
174                 }
175                 dest = arg;
176                 heads = argv + i + 1;
177                 nr_heads = argc - i - 1;
178                 break;
179         }
180         if (!dest)
181                 usage(clone_pack_usage);
182         pid = git_connect(fd, dest, exec);
183         if (pid < 0)
184                 return 1;
185         ret = clone_pack(fd, nr_heads, heads);
186         close(fd[0]);
187         close(fd[1]);
188         finish_connect(pid);
189         return ret;
190 }