Merge branches 'master' and 'jc/combine' into next
[git/fastimport.git] / clone-pack.c
bloba4370f595f1206840e66d488423f1c7e42029c6d
1 #include "cache.h"
2 #include "refs.h"
3 #include "pkt-line.h"
5 static const char clone_pack_usage[] =
6 "git-clone-pack [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*";
7 static const char *exec = "git-upload-pack";
9 static int quiet = 0;
11 static void clone_handshake(int fd[2], struct ref *ref)
13 unsigned char sha1[20];
15 while (ref) {
16 packet_write(fd[1], "want %s\n", sha1_to_hex(ref->old_sha1));
17 ref = ref->next;
19 packet_flush(fd[1]);
21 /* We don't have nuttin' */
22 packet_write(fd[1], "done\n");
23 if (get_ack(fd[0], sha1))
24 error("Huh! git-clone-pack got positive ack for %s", sha1_to_hex(sha1));
27 static int is_master(struct ref *ref)
29 return !strcmp(ref->name, "refs/heads/master");
32 static void write_one_ref(struct ref *ref)
34 char *path = git_path("%s", ref->name);
35 int fd;
36 char *hex;
38 if (!strncmp(ref->name, "refs/", 5) &&
39 check_ref_format(ref->name + 5)) {
40 error("refusing to create funny ref '%s' locally", ref->name);
41 return;
44 if (safe_create_leading_directories(path))
45 die("unable to create leading directory for %s", ref->name);
46 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);
47 if (fd < 0)
48 die("unable to create ref %s", ref->name);
49 hex = sha1_to_hex(ref->old_sha1);
50 hex[40] = '\n';
51 if (write(fd, hex, 41) != 41)
52 die("unable to write ref %s", ref->name);
53 close(fd);
56 static void write_refs(struct ref *ref)
58 struct ref *head = NULL, *head_ptr, *master_ref;
59 char *head_path;
61 /* Upload-pack must report HEAD first */
62 if (!strcmp(ref->name, "HEAD")) {
63 head = ref;
64 ref = ref->next;
66 head_ptr = NULL;
67 master_ref = NULL;
68 while (ref) {
69 if (is_master(ref))
70 master_ref = ref;
71 if (head &&
72 !memcmp(ref->old_sha1, head->old_sha1, 20) &&
73 !strncmp(ref->name, "refs/heads/",11) &&
74 (!head_ptr || ref == master_ref))
75 head_ptr = ref;
77 write_one_ref(ref);
78 ref = ref->next;
80 if (!head) {
81 fprintf(stderr, "No HEAD in remote.\n");
82 return;
85 head_path = strdup(git_path("HEAD"));
86 if (!head_ptr) {
88 * If we had a master ref, and it wasn't HEAD, we need to undo the
89 * symlink, and write a standalone HEAD. Give a warning, because that's
90 * really really wrong.
92 if (master_ref) {
93 error("HEAD doesn't point to any refs! Making standalone HEAD");
94 unlink(head_path);
96 write_one_ref(head);
97 free(head_path);
98 return;
101 /* We reset to the master branch if it's available */
102 if (master_ref)
103 return;
105 fprintf(stderr, "Setting HEAD to %s\n", head_ptr->name);
108 * Uhhuh. Other end didn't have master. We start HEAD off with
109 * the first branch with the same value.
111 if (create_symref(head_path, head_ptr->name) < 0)
112 die("unable to link HEAD to %s", head_ptr->name);
113 free(head_path);
116 static int clone_pack(int fd[2], int nr_match, char **match)
118 struct ref *refs;
119 int status;
121 get_remote_heads(fd[0], &refs, nr_match, match, 1);
122 if (!refs) {
123 packet_flush(fd[1]);
124 die("no matching remote head");
126 clone_handshake(fd, refs);
128 status = receive_keep_pack(fd, "git-clone-pack", quiet);
129 if (!quiet)
130 fprintf(stderr, "\n");
132 if (!status) {
133 if (nr_match == 0)
134 write_refs(refs);
135 else
136 while (refs) {
137 printf("%s %s\n",
138 sha1_to_hex(refs->old_sha1),
139 refs->name);
140 refs = refs->next;
143 return status;
146 int main(int argc, char **argv)
148 int i, ret, nr_heads;
149 char *dest = NULL, **heads;
150 int fd[2];
151 pid_t pid;
153 setup_git_directory();
155 nr_heads = 0;
156 heads = NULL;
157 for (i = 1; i < argc; i++) {
158 char *arg = argv[i];
160 if (*arg == '-') {
161 if (!strcmp("-q", arg)) {
162 quiet = 1;
163 continue;
165 if (!strncmp("--exec=", arg, 7)) {
166 exec = arg + 7;
167 continue;
169 usage(clone_pack_usage);
171 dest = arg;
172 heads = argv + i + 1;
173 nr_heads = argc - i - 1;
174 break;
176 if (!dest)
177 usage(clone_pack_usage);
178 pid = git_connect(fd, dest, exec);
179 if (pid < 0)
180 return 1;
181 ret = clone_pack(fd, nr_heads, heads);
182 close(fd[0]);
183 close(fd[1]);
184 finish_connect(pid);
185 return ret;