]>
Commit | Line | Data |
---|---|---|
1fcc8ea5 LT |
1 | #include "cache.h" |
2 | #include "refs.h" | |
3 | #include "pkt-line.h" | |
1fcc8ea5 | 4 | |
64c381bf | 5 | static const char clone_pack_usage[] = |
e1c7ada6 | 6 | "git-clone-pack [--exec=<git-upload-pack>] [<host>:]<directory> [<heads>]*"; |
1fcc8ea5 LT |
7 | static const char *exec = "git-upload-pack"; |
8 | ||
5ee2ad65 LT |
9 | static int quiet = 0; |
10 | ||
1fcc8ea5 LT |
11 | static void clone_handshake(int fd[2], struct ref *ref) |
12 | { | |
13 | unsigned char sha1[20]; | |
14 | ||
15 | while (ref) { | |
d1c133f5 | 16 | packet_write(fd[1], "want %s\n", sha1_to_hex(ref->old_sha1)); |
1fcc8ea5 LT |
17 | ref = ref->next; |
18 | } | |
19 | packet_flush(fd[1]); | |
20 | ||
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)); | |
25 | } | |
26 | ||
27 | static int is_master(struct ref *ref) | |
28 | { | |
29 | return !strcmp(ref->name, "refs/heads/master"); | |
30 | } | |
31 | ||
32 | static void write_one_ref(struct ref *ref) | |
33 | { | |
4ec99bf0 | 34 | char *path = git_path("%s", ref->name); |
b2cb9425 | 35 | int fd; |
1fcc8ea5 LT |
36 | char *hex; |
37 | ||
d8a1deec JH |
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; | |
42 | } | |
43 | ||
b2cb9425 JH |
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); | |
1fcc8ea5 LT |
47 | if (fd < 0) |
48 | die("unable to create ref %s", ref->name); | |
d1c133f5 | 49 | hex = sha1_to_hex(ref->old_sha1); |
1fcc8ea5 LT |
50 | hex[40] = '\n'; |
51 | if (write(fd, hex, 41) != 41) | |
52 | die("unable to write ref %s", ref->name); | |
53 | close(fd); | |
54 | } | |
55 | ||
56 | static void write_refs(struct ref *ref) | |
57 | { | |
58 | struct ref *head = NULL, *head_ptr, *master_ref; | |
59 | char *head_path; | |
60 | ||
4cfc63db | 61 | /* Upload-pack must report HEAD first */ |
1fcc8ea5 LT |
62 | if (!strcmp(ref->name, "HEAD")) { |
63 | head = ref; | |
64 | ref = ref->next; | |
65 | } | |
66 | head_ptr = NULL; | |
67 | master_ref = NULL; | |
68 | while (ref) { | |
69 | if (is_master(ref)) | |
70 | master_ref = ref; | |
4cfc63db JH |
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; | |
76 | ||
1fcc8ea5 LT |
77 | write_one_ref(ref); |
78 | ref = ref->next; | |
79 | } | |
4cfc63db JH |
80 | if (!head) { |
81 | fprintf(stderr, "No HEAD in remote.\n"); | |
1fcc8ea5 | 82 | return; |
4cfc63db | 83 | } |
1fcc8ea5 | 84 | |
4cfc63db | 85 | head_path = strdup(git_path("HEAD")); |
1fcc8ea5 LT |
86 | if (!head_ptr) { |
87 | /* | |
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. | |
91 | */ | |
92 | if (master_ref) { | |
93 | error("HEAD doesn't point to any refs! Making standalone HEAD"); | |
94 | unlink(head_path); | |
95 | } | |
96 | write_one_ref(head); | |
4cfc63db | 97 | free(head_path); |
1fcc8ea5 LT |
98 | return; |
99 | } | |
100 | ||
101 | /* We reset to the master branch if it's available */ | |
102 | if (master_ref) | |
103 | return; | |
104 | ||
4cfc63db JH |
105 | fprintf(stderr, "Setting HEAD to %s\n", head_ptr->name); |
106 | ||
1fcc8ea5 LT |
107 | /* |
108 | * Uhhuh. Other end didn't have master. We start HEAD off with | |
109 | * the first branch with the same value. | |
110 | */ | |
4cfc63db | 111 | if (create_symref(head_path, head_ptr->name) < 0) |
1fcc8ea5 | 112 | die("unable to link HEAD to %s", head_ptr->name); |
4cfc63db | 113 | free(head_path); |
1fcc8ea5 LT |
114 | } |
115 | ||
11dcec07 JH |
116 | static int clone_pack(int fd[2], int nr_match, char **match) |
117 | { | |
118 | struct ref *refs; | |
119 | int status; | |
120 | ||
1a7141ff | 121 | get_remote_heads(fd[0], &refs, nr_match, match, 1); |
11dcec07 JH |
122 | if (!refs) { |
123 | packet_flush(fd[1]); | |
124 | die("no matching remote head"); | |
125 | } | |
126 | clone_handshake(fd, refs); | |
127 | ||
5ee2ad65 | 128 | status = receive_keep_pack(fd, "git-clone-pack", quiet); |
21fcd1bd JH |
129 | if (!quiet) |
130 | fprintf(stderr, "\n"); | |
11dcec07 | 131 | |
31ec6abf JH |
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; | |
141 | } | |
142 | } | |
11dcec07 JH |
143 | return status; |
144 | } | |
145 | ||
1fcc8ea5 LT |
146 | int main(int argc, char **argv) |
147 | { | |
148 | int i, ret, nr_heads; | |
149 | char *dest = NULL, **heads; | |
150 | int fd[2]; | |
151 | pid_t pid; | |
152 | ||
5a327713 JH |
153 | setup_git_directory(); |
154 | ||
1fcc8ea5 LT |
155 | nr_heads = 0; |
156 | heads = NULL; | |
157 | for (i = 1; i < argc; i++) { | |
158 | char *arg = argv[i]; | |
159 | ||
160 | if (*arg == '-') { | |
5ee2ad65 LT |
161 | if (!strcmp("-q", arg)) { |
162 | quiet = 1; | |
167a4a33 | 163 | continue; |
5ee2ad65 | 164 | } |
6ec311da JH |
165 | if (!strncmp("--exec=", arg, 7)) { |
166 | exec = arg + 7; | |
167 | continue; | |
168 | } | |
1fcc8ea5 LT |
169 | usage(clone_pack_usage); |
170 | } | |
171 | dest = arg; | |
172 | heads = argv + i + 1; | |
173 | nr_heads = argc - i - 1; | |
174 | break; | |
175 | } | |
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; | |
186 | } |