Add "--branches", "--tags" and "--remotes" options to git-rev-parse.
"git branch" uses "rev-parse --all" and becomes much too slow when there are many tags (it scans all refs). Use the new "--branches" option of rev-parse to speed things up. Signed-off-by: Sean Estabrooks <seanlkml@sympatico.ca> Signed-off-by: Junio C Hamano <junkio@cox.net>
Sean committed
May 13, 2006 at 21:43 UTC
a62be77f5ebdbbd46d6956dc4d07571835588389
5 files changed
+48
-7
Documentation/git-rev-parse.txt
+9
@@ -67,6 +67,15 @@ OPTIONS
67
--all::
68
Show all refs found in `$GIT_DIR/refs`.
69
70
+--branches::
71
+ Show branch refs found in `$GIT_DIR/refs/heads`.
72
+
73
+--tags::
74
+ Show tag refs found in `$GIT_DIR/refs/tags`.
75
+
76
+--remotes::
77
+ Show tag refs found in `$GIT_DIR/refs/remotes`.
78
+
79
--show-prefix::
80
When the command is invoked from a subdirectory, show the
81
path of the current directory relative to the top-level
git-branch.sh
+1
-2
@@ -82,8 +82,7 @@ done
82
83
case "$#" in
84
0)
85
- git-rev-parse --symbolic --all |
86
- sed -ne 's|^refs/heads/||p' |
85
+ git-rev-parse --symbolic --branches |
86
sort |
87
while read ref
88
do
refs.c
+19
-4
@@ -114,7 +114,7 @@ int read_ref(const char *filename, unsigned char *sha1)
114
return -1;
115
}
116
117
-static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1))
117
+static int do_for_each_ref(const char *base, int (*fn)(const char *path, const unsigned char *sha1), int trim)
118
{
119
int retval = 0;
120
DIR *dir = opendir(git_path("%s", base));
@@ -146,7 +146,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
146
if (stat(git_path("%s", path), &st) < 0)
147
continue;
148
if (S_ISDIR(st.st_mode)) {
149
- retval = do_for_each_ref(path, fn);
149
+ retval = do_for_each_ref(path, fn, trim);
150
if (retval)
151
break;
152
continue;
@@ -160,7 +160,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
160
"commit object!", path);
161
continue;
162
}
163
- retval = fn(path, sha1);
163
+ retval = fn(path + trim, sha1);
164
if (retval)
165
break;
166
}
@@ -180,7 +180,22 @@ int head_ref(int (*fn)(const char *path, const unsigned char *sha1))
180
181
int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1))
182
{
183
- return do_for_each_ref("refs", fn);
183
+ return do_for_each_ref("refs", fn, 0);
184
+}
185
+
186
+int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1))
187
+{
188
+ return do_for_each_ref("refs/tags", fn, 10);
189
+}
190
+
191
+int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1))
192
+{
193
+ return do_for_each_ref("refs/heads", fn, 11);
194
+}
195
+
196
+int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1))
197
+{
198
+ return do_for_each_ref("refs/remotes", fn, 13);
199
}
200
201
static char *ref_file_name(const char *ref)
refs.h
+3
@@ -7,6 +7,9 @@
7
*/
8
extern int head_ref(int (*fn)(const char *path, const unsigned char *sha1));
9
extern int for_each_ref(int (*fn)(const char *path, const unsigned char *sha1));
10
+extern int for_each_tag_ref(int (*fn)(const char *path, const unsigned char *sha1));
11
+extern int for_each_branch_ref(int (*fn)(const char *path, const unsigned char *sha1));
12
+extern int for_each_remote_ref(int (*fn)(const char *path, const unsigned char *sha1));
13
14
/** Reads the refs file specified into sha1 **/
15
extern int get_ref_sha1(const char *ref, unsigned char *sha1);
rev-parse.c
+16
-1
@@ -36,6 +36,7 @@ static int is_rev_argument(const char *arg)
36
"--all",
37
"--bisect",
38
"--dense",
39
+ "--branches",
40
"--header",
41
"--max-age=",
42
"--max-count=",
@@ -45,7 +46,9 @@ static int is_rev_argument(const char *arg)
46
"--objects-edge",
47
"--parents",
48
"--pretty",
49
+ "--remotes",
50
"--sparse",
51
+ "--tags",
52
"--topo-order",
53
"--date-order",
54
"--unpacked",
@@ -165,7 +168,7 @@ int main(int argc, char **argv)
168
int i, as_is = 0, verify = 0;
169
unsigned char sha1[20];
170
const char *prefix = setup_git_directory();
168
-
171
+
172
git_config(git_default_config);
173
174
for (i = 1; i < argc; i++) {
@@ -255,6 +258,18 @@ int main(int argc, char **argv)
258
for_each_ref(show_reference);
259
continue;
260
}
261
+ if (!strcmp(arg, "--branches")) {
262
+ for_each_branch_ref(show_reference);
263
+ continue;
264
+ }
265
+ if (!strcmp(arg, "--tags")) {
266
+ for_each_tag_ref(show_reference);
267
+ continue;
268
+ }
269
+ if (!strcmp(arg, "--remotes")) {
270
+ for_each_remote_ref(show_reference);
271
+ continue;
272
+ }
273
if (!strcmp(arg, "--show-prefix")) {
274
if (prefix)
275
puts(prefix);