git-fetch: Avoid reading packed refs over and over again
When checking which tags to fetch, the old code used to call git-show-ref --verify for each remote tag. Since reading even packed refs is not a cheap operation when there are a lot of local refs, the code became quite slow. This fixes it by teaching git-show-ref to filter out existing refs using a new mode of operation of git-show-ref. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Dec 17, 2006 at 17:57 UTC
ed9f7c954c26ec6d517bdca3d8e4b895278d1b2b
2 files changed
+62
-10
builtin-show-ref.c
+59
-1
@@ -2,8 +2,9 @@
2
#include "refs.h"
3
#include "object.h"
4
#include "tag.h"
5
+#include "path-list.h"
6
6
-static const char show_ref_usage[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<length>]] [--abbrev[=<length>]] [--tags] [--heads] [--] [pattern*]";
7
+static const char show_ref_usage[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<length>]] [--abbrev[=<length>]] [--tags] [--heads] [--] [pattern*] | --filter-invalid < ref-list";
8
9
static int deref_tags = 0, show_head = 0, tags_only = 0, heads_only = 0,
10
found_match = 0, verify = 0, quiet = 0, hash_only = 0, abbrev = 0;
@@ -86,6 +87,59 @@ match:
87
return 0;
88
}
89
90
+static int add_existing(const char *refname, const unsigned char *sha1, int flag, void *cbdata)
91
+{
92
+ struct path_list *list = (struct path_list *)cbdata;
93
+ path_list_insert(refname, list);
94
+ return 0;
95
+}
96
+
97
+/*
98
+ * read "^(?:<anything>\s)?<refname>(?:\^\{\})?$" from the standard input,
99
+ * and
100
+ * (1) strip "^{}" at the end of line if any;
101
+ * (2) ignore if match is provided and does not head-match refname;
102
+ * (3) warn if refname is not a well-formed refname and skip;
103
+ * (4) ignore if refname is a ref that exists in the local repository;
104
+ * (5) otherwise output the line.
105
+ */
106
+static int exclude_existing(const char *match)
107
+{
108
+ static struct path_list existing_refs = { NULL, 0, 0, 0 };
109
+ char buf[1024];
110
+ int matchlen = match ? strlen(match) : 0;
111
+
112
+ for_each_ref(add_existing, &existing_refs);
113
+ while (fgets(buf, sizeof(buf), stdin)) {
114
+ int len = strlen(buf);
115
+ char *ref;
116
+ if (len > 0 && buf[len - 1] == '\n')
117
+ buf[--len] = '\0';
118
+ if (!strcmp(buf + len - 3, "^{}")) {
119
+ len -= 3;
120
+ buf[len] = '\0';
121
+ }
122
+ for (ref = buf + len; buf < ref; ref--)
123
+ if (isspace(ref[-1]))
124
+ break;
125
+ if (match) {
126
+ int reflen = buf + len - ref;
127
+ if (reflen < matchlen)
128
+ continue;
129
+ if (strncmp(ref, match, matchlen))
130
+ continue;
131
+ }
132
+ if (check_ref_format(ref)) {
133
+ fprintf(stderr, "warning: ref '%s' ignored\n", ref);
134
+ continue;
135
+ }
136
+ if (!path_list_has_path(&existing_refs, ref)) {
137
+ printf("%s\n", buf);
138
+ }
139
+ }
140
+ return 0;
141
+}
142
+
143
int cmd_show_ref(int argc, const char **argv, const char *prefix)
144
{
145
int i;
@@ -153,6 +207,10 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
207
heads_only = 1;
208
continue;
209
}
210
+ if (!strcmp(arg, "--exclude-existing"))
211
+ return exclude_existing(NULL);
212
+ if (!strncmp(arg, "--exclude-existing=", 19))
213
+ return exclude_existing(arg + 19);
214
usage(show_ref_usage);
215
}
216
if (show_head)
git-fetch.sh
+3
-9
@@ -242,7 +242,7 @@ esac
242
reflist=$(get_remote_refs_for_fetch "$@")
243
if test "$tags"
244
then
245
- taglist=`IFS=" " &&
245
+ taglist=`IFS=' ' &&
246
echo "$ls_remote_result" |
247
while read sha1 name
248
do
@@ -438,17 +438,11 @@ case "$no_tags$tags" in
438
*:refs/*)
439
# effective only when we are following remote branch
440
# using local tracking branch.
441
- taglist=$(IFS=" " &&
441
+ taglist=$(IFS=' ' &&
442
echo "$ls_remote_result" |
443
- sed -n -e 's|^\('"$_x40"'\) \(refs/tags/.*\)^{}$|\1 \2|p' \
444
- -e 's|^\('"$_x40"'\) \(refs/tags/.*\)$|\1 \2|p' |
443
+ git-show-ref --exclude-existing=refs/tags/ |
444
while read sha1 name
445
do
447
- git-show-ref --verify --quiet -- "$name" && continue
448
- git-check-ref-format "$name" || {
449
- echo >&2 "warning: tag ${name} ignored"
450
- continue
451
- }
446
git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
447
echo >&2 "Auto-following $name"
448
echo ".${name}:${name}"