@matheusbsbs / matheus-bagliano / commits / 86551586da
Fork 0

git-fetch: rewrite expand_ref_wildcard in C

This does not seem to make measurable improvement when dealing with 1000 unpacked refs, but we would need something like it if we were to do a full rewrite in C somedaoy. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jan 16, 2007 at 13:43 UTC 86551586da8cba6c06ac04783a656843a4e47f35
2 files changed +91 -45
builtin-fetch--tool.c
+90
@@ -323,6 +323,91 @@ static int parse_reflist(const char *reflist)
323 return 0;
324 }
325
326 +static int expand_refs_wildcard(const char *ls_remote_result, int numrefs,
327 + const char **refs)
328 +{
329 + int i, matchlen, replacelen;
330 + int found_one = 0;
331 + const char *remote = *refs++;
332 + numrefs--;
333 +
334 + if (numrefs == 0) {
335 + fprintf(stderr, "Nothing specified for fetching with remote.%s.fetch\n",
336 + remote);
337 + printf("empty\n");
338 + }
339 +
340 + for (i = 0; i < numrefs; i++) {
341 + const char *ref = refs[i];
342 + const char *lref = ref;
343 + const char *colon;
344 + const char *tail;
345 + const char *ls;
346 + const char *next;
347 +
348 + if (*lref == '+')
349 + lref++;
350 + colon = strchr(lref, ':');
351 + tail = lref + strlen(lref);
352 + if (!(colon &&
353 + 2 < colon - lref &&
354 + colon[-1] == '*' &&
355 + colon[-2] == '/' &&
356 + 2 < tail - (colon + 1) &&
357 + tail[-1] == '*' &&
358 + tail[-2] == '/')) {
359 + /* not a glob */
360 + if (!found_one++)
361 + printf("explicit\n");
362 + printf("%s\n", ref);
363 + continue;
364 + }
365 +
366 + /* glob */
367 + if (!found_one++)
368 + printf("glob\n");
369 +
370 + /* lref to colon-2 is remote hierarchy name;
371 + * colon+1 to tail-2 is local.
372 + */
373 + matchlen = (colon-1) - lref;
374 + replacelen = (tail-1) - (colon+1);
375 + for (ls = ls_remote_result; ls; ls = next) {
376 + const char *eol;
377 + unsigned char sha1[20];
378 + int namelen;
379 +
380 + while (*ls && isspace(*ls))
381 + ls++;
382 + next = strchr(ls, '\n');
383 + eol = !next ? (ls + strlen(ls)) : next;
384 + if (!memcmp("^{}", eol-3, 3))
385 + continue;
386 + if (get_sha1_hex(ls, sha1))
387 + continue;
388 + ls += 40;
389 + while (ls < eol && isspace(*ls))
390 + ls++;
391 + /* ls to next (or eol) is the name.
392 + * is it identical to lref to colon-2?
393 + */
394 + if ((eol - ls) <= matchlen ||
395 + strncmp(ls, lref, matchlen))
396 + continue;
397 +
398 + /* Yes, it is a match */
399 + namelen = eol - ls;
400 + if (lref != ref)
401 + putchar('+');
402 + printf("%.*s:%.*s%.*s\n",
403 + namelen, ls,
404 + replacelen, colon + 1,
405 + namelen - matchlen, ls + matchlen);
406 + }
407 + }
408 + return 0;
409 +}
410 +
411 int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
412 {
413 int verbose = 0;
@@ -380,6 +465,11 @@ int cmd_fetch__tool(int argc, const char **argv, const char *prefix)
465 return error("parse-reflist takes 1 arg");
466 return parse_reflist(argv[2]);
467 }
468 + if (!strcmp("expand-refs-wildcard", argv[1])) {
469 + if (argc < 4)
470 + return error("expand-refs-wildcard takes at least 2 args");
471 + return expand_refs_wildcard(argv[2], argc - 3, argv + 3);
472 + }
473
474 return error("Unknown subcommand: %s", argv[1]);
475 }
git-parse-remote.sh
+1 -45
@@ -81,51 +81,7 @@ get_remote_default_refs_for_push () {
81 # is to help prevent randomly "globbed" ref from being chosen as
82 # a merge candidate
83 expand_refs_wildcard () {
84 - remote="$1"
85 - shift
86 - first_one=yes
87 - if test "$#" = 0
88 - then
89 - echo empty
90 - echo >&2 "Nothing specified for fetching with remote.$remote.fetch"
91 - fi
92 - for ref
93 - do
94 - lref=${ref#'+'}
95 - # a non glob pattern is given back as-is.
96 - expr "z$lref" : 'zrefs/.*/\*:refs/.*/\*$' >/dev/null || {
97 - if test -n "$first_one"
98 - then
99 - echo "explicit"
100 - first_one=
101 - fi
102 - echo "$ref"
103 - continue
104 - }
105 -
106 - # glob
107 - if test -n "$first_one"
108 - then
109 - echo "glob"
110 - first_one=
111 - fi
112 - from=`expr "z$lref" : 'z\(refs/.*/\)\*:refs/.*/\*$'`
113 - to=`expr "z$lref" : 'zrefs/.*/\*:\(refs/.*/\)\*$'`
114 - local_force=
115 - test "z$lref" = "z$ref" || local_force='+'
116 - echo "$ls_remote_result" |
117 - sed -e '/\^{}$/d' |
118 - (
119 - IFS=' '
120 - while read sha1 name
121 - do
122 - # ignore the ones that do not start with $from
123 - mapped=${name#"$from"}
124 - test "z$name" = "z$mapped" && continue
125 - echo "${local_force}${name}:${to}${mapped}"
126 - done
127 - )
128 - done
84 + git fetch--tool expand-refs-wildcard "$ls_remote_result" "$@"
85 }
86
87 # Subroutine to canonicalize remote:local notation.