@matheusbsbs / matheus-bagliano / commits / 8e87ca6615
Fork 0

Teach git-local-fetch the --stdin switch

This makes it possible to fetch many commits (refs) at once, greatly speeding up cg-clone. Signed-off-by: Petr Baudis <pasky@suse.cz> Signed-off-by: Junio C Hamano <junkio@cox.net>

Petr Baudis committed Jul 27, 2006 at 23:56 UTC 8e87ca661513d9f4b737295b234e93767cd2ee0c
4 files changed +74 -10
Documentation/git-local-fetch.txt
+6
@@ -29,6 +29,12 @@ OPTIONS
29 Writes the commit-id into the filename under $GIT_DIR/refs/<filename> on
30 the local end after the transfer is complete.
31
32 +--stdin::
33 + Instead of a commit id on the commandline (which is not expected in this
34 + case), 'git-local-fetch' expects lines on stdin in the format
35 +
36 + <commit-id>['\t'<filename-as-in--w>]
37 +
38 Author
39 ------
40 Written by Junio C Hamano <junkio@cox.net>
fetch.c
+40
@@ -7,6 +7,7 @@
7 #include "tag.h"
8 #include "blob.h"
9 #include "refs.h"
10 +#include "strbuf.h"
11
12 int get_tree = 0;
13 int get_history = 0;
@@ -210,6 +211,45 @@ static int mark_complete(const char *path, const unsigned char *sha1)
211 return 0;
212 }
213
214 +int pull_targets_stdin(char ***target, const char ***write_ref)
215 +{
216 + int targets = 0, targets_alloc = 0;
217 + struct strbuf buf;
218 + *target = NULL; *write_ref = NULL;
219 + strbuf_init(&buf);
220 + while (1) {
221 + char *rf_one = NULL;
222 + char *tg_one;
223 +
224 + read_line(&buf, stdin, '\n');
225 + if (buf.eof)
226 + break;
227 + tg_one = buf.buf;
228 + rf_one = strchr(tg_one, '\t');
229 + if (rf_one)
230 + *rf_one++ = 0;
231 +
232 + if (targets >= targets_alloc) {
233 + targets_alloc = targets_alloc ? targets_alloc * 2 : 64;
234 + *target = xrealloc(*target, targets_alloc * sizeof(**target));
235 + *write_ref = xrealloc(*write_ref, targets_alloc * sizeof(**write_ref));
236 + }
237 + (*target)[targets] = strdup(tg_one);
238 + (*write_ref)[targets] = rf_one ? strdup(rf_one) : NULL;
239 + targets++;
240 + }
241 + return targets;
242 +}
243 +
244 +void pull_targets_free(int targets, char **target, const char **write_ref)
245 +{
246 + while (targets--) {
247 + free(target[targets]);
248 + if (write_ref[targets])
249 + free((char *) write_ref[targets]);
250 + }
251 +}
252 +
253 int pull(int targets, char **target, const char **write_ref,
254 const char *write_ref_log_details)
255 {
fetch.h
+6
@@ -40,6 +40,12 @@ extern int get_recover;
40 /* Report what we got under get_verbosely */
41 extern void pull_say(const char *, const char *);
42
43 +/* Load pull targets from stdin */
44 +extern int pull_targets_stdin(char ***target, const char ***write_ref);
45 +
46 +/* Free up loaded targets */
47 +extern void pull_targets_free(int targets, char **target, const char **write_ref);
48 +
49 /* If write_ref is set, the ref filename to write the target value to. */
50 /* If write_ref_log_details is set, additional text will appear in the ref log. */
51 extern int pull(int targets, char **target, const char **write_ref,
local-fetch.c
+22 -10
@@ -8,8 +8,9 @@
8 static int use_link = 0;
9 static int use_symlink = 0;
10 static int use_filecopy = 1;
11 +static int commits_on_stdin = 0;
12
12 -static char *path; /* "Remote" git repository */
13 +static const char *path; /* "Remote" git repository */
14
15 void prefetch(unsigned char *sha1)
16 {
@@ -194,7 +195,7 @@ int fetch_ref(char *ref, unsigned char *sha1)
195 }
196
197 static const char local_pull_usage[] =
197 -"git-local-fetch [-c] [-t] [-a] [-v] [-w filename] [--recover] [-l] [-s] [-n] commit-id path";
198 +"git-local-fetch [-c] [-t] [-a] [-v] [-w filename] [--recover] [-l] [-s] [-n] [--stdin] commit-id path";
199
200 /*
201 * By default we only use file copy.
@@ -202,10 +203,11 @@ static const char local_pull_usage[] =
203 * If -s is specified, then a symlink is attempted.
204 * If -n is _not_ specified, then a regular file-to-file copy is done.
205 */
205 -int main(int argc, char **argv)
206 +int main(int argc, const char **argv)
207 {
207 - const char *write_ref = NULL;
208 - char *commit_id;
208 + int commits;
209 + const char **write_ref = NULL;
210 + char **commit_id;
211 int arg = 1;
212
213 setup_git_directory();
@@ -230,20 +232,30 @@ int main(int argc, char **argv)
232 else if (argv[arg][1] == 'v')
233 get_verbosely = 1;
234 else if (argv[arg][1] == 'w')
233 - write_ref = argv[++arg];
235 + write_ref = &argv[++arg];
236 else if (!strcmp(argv[arg], "--recover"))
237 get_recover = 1;
238 + else if (!strcmp(argv[arg], "--stdin"))
239 + commits_on_stdin = 1;
240 else
241 usage(local_pull_usage);
242 arg++;
243 }
240 - if (argc < arg + 2)
244 + if (argc < arg + 2 - commits_on_stdin)
245 usage(local_pull_usage);
242 - commit_id = argv[arg];
243 - path = argv[arg + 1];
246 + if (commits_on_stdin) {
247 + commits = pull_targets_stdin(&commit_id, &write_ref);
248 + } else {
249 + commit_id = (char **) &argv[arg++];
250 + commits = 1;
251 + }
252 + path = argv[arg];
253
245 - if (pull(1, &commit_id, &write_ref, path))
254 + if (pull(commits, commit_id, write_ref, path))
255 return 1;
256
257 + if (commits_on_stdin)
258 + pull_targets_free(commits, commit_id, write_ref);
259 +
260 return 0;
261 }