Fix git-fetch while on detached HEAD not to give needlessly alarming errors
When we are on a detached HEAD, there is no current branch. There is no reason to leak the error messages to the end user since this is a situation we expect to see. This adds -q option to git-symbolic-ref to exit without issuing an error message if the given name is not a symbolic ref. By the way, with or without this patch, there currently is no good way to tell failure modes between "git symbolic-ref HAED" and "git symbolic-ref HEAD". Both says "is not a symbolic ref". We may want to do something about it. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jan 15, 2007 at 13:56 UTC
a0f4280f9e5decd384fe5cc9e30414b901aeb478
3 files changed
+40
-8
Documentation/git-symbolic-ref.txt
+9
-1
@@ -7,7 +7,7 @@ git-symbolic-ref - read and modify symbolic refs
7
8
SYNOPSIS
9
--------
10
-'git-symbolic-ref' <name> [<ref>]
10
+'git-symbolic-ref' [-q] <name> [<ref>]
11
12
DESCRIPTION
13
-----------
@@ -23,6 +23,14 @@ A symbolic ref is a regular file that stores a string that
23
begins with `ref: refs/`. For example, your `.git/HEAD` is
24
a regular file whose contents is `ref: refs/heads/master`.
25
26
+OPTIONS
27
+-------
28
+
29
+-q::
30
+ Do not issue an error message if the <name> is not a
31
+ symbolic ref but a detached HEAD; instead exit with
32
+ non-zero status silently.
33
+
34
NOTES
35
-----
36
In the past, `.git/HEAD` was a symbolic link pointing at
builtin-symbolic-ref.c
+29
-5
@@ -3,9 +3,9 @@
3
#include "refs.h"
4
5
static const char git_symbolic_ref_usage[] =
6
-"git-symbolic-ref name [ref]";
6
+"git-symbolic-ref [-q] name [ref]";
7
8
-static void check_symref(const char *HEAD)
8
+static void check_symref(const char *HEAD, int quiet)
9
{
10
unsigned char sha1[20];
11
int flag;
@@ -13,17 +13,41 @@ static void check_symref(const char *HEAD)
13
14
if (!refs_heads_master)
15
die("No such ref: %s", HEAD);
16
- else if (!(flag & REF_ISSYMREF))
17
- die("ref %s is not a symbolic ref", HEAD);
16
+ else if (!(flag & REF_ISSYMREF)) {
17
+ if (!quiet)
18
+ die("ref %s is not a symbolic ref", HEAD);
19
+ else
20
+ exit(1);
21
+ }
22
puts(refs_heads_master);
23
}
24
25
int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
26
{
27
+ int quiet = 0;
28
+
29
git_config(git_default_config);
30
+
31
+ while (1 < argc) {
32
+ const char *arg = argv[1];
33
+ if (arg[0] != '-')
34
+ break;
35
+ else if (!strcmp("-q", arg))
36
+ quiet = 1;
37
+ else if (!strcmp("--", arg)) {
38
+ argc--;
39
+ argv++;
40
+ break;
41
+ }
42
+ else
43
+ die("unknown option %s", arg);
44
+ argc--;
45
+ argv++;
46
+ }
47
+
48
switch (argc) {
49
case 2:
26
- check_symref(argv[1]);
50
+ check_symref(argv[1], quiet);
51
break;
52
case 3:
53
create_symref(argv[1], argv[2]);
git-parse-remote.sh
+2
-2
@@ -49,7 +49,7 @@ get_remote_url () {
49
}
50
51
get_default_remote () {
52
- curr_branch=$(git-symbolic-ref HEAD | sed -e 's|^refs/heads/||')
52
+ curr_branch=$(git-symbolic-ref -q HEAD | sed -e 's|^refs/heads/||')
53
origin=$(git-repo-config --get "branch.$curr_branch.remote")
54
echo ${origin:-origin}
55
}
@@ -137,7 +137,7 @@ canon_refs_list_for_fetch () {
137
shift
138
if test "$remote" = "$(get_default_remote)"
139
then
140
- curr_branch=$(git-symbolic-ref HEAD | \
140
+ curr_branch=$(git-symbolic-ref -q HEAD | \
141
sed -e 's|^refs/heads/||')
142
merge_branches=$(git-repo-config \
143
--get-all "branch.${curr_branch}.merge")