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