Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
rev-parse: implement --quiet for the --is-* arguments
Instead of printing true or false, use the exit code to return a value
returning the status.

Signed-off-by: Kevin Daudt <me@ikke.info>
  • Loading branch information
Ikke committed Jul 30, 2015
1 parent f99a38c commit dcec39b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 22 deletions.
12 changes: 9 additions & 3 deletions Documentation/git-rev-parse.txt
Expand Up @@ -111,10 +111,13 @@ can be used.

-q::
--quiet::
Only meaningful in `--verify` mode. Do not output an error
message if the first argument is not a valid object name;
instead exit with non-zero status silently.
In combination with --verify, do not output an error message
if the first argument is not a valid object name; instead
exit with non-zero status silently.
SHA-1s for valid object names are printed to stdout on success.
In combination with any of --is-inside-git-dir,
--is-inside-work-tree and --is-bare-repository, do not output
true or false, but a specific exit code.

--sq::
Usually the output is made one line per flag and
Expand Down Expand Up @@ -222,13 +225,16 @@ print a message to stderr and exit with nonzero status.
--is-inside-git-dir::
When the current working directory is below the repository
directory print "true", otherwise "false".
With --quiet, return 1 instead of printing "true".

--is-inside-work-tree::
When the current working directory is inside the work tree of the
repository print "true", otherwise "false".
With --quiet, return 2 instead of printing "true".

--is-bare-repository::
When the repository is bare print "true", otherwise "false".
with --quiet, return 4 instead of printing "true".

--resolve-git-dir <path>::
Check if <path> is a valid repository or a gitfile that
Expand Down
32 changes: 26 additions & 6 deletions builtin/rev-parse.c
Expand Up @@ -507,6 +507,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
int has_dashdash = 0;
int output_prefix = 0;
int is_result = 0;
int return_code = 0;
unsigned char sha1[20];
unsigned int flags = 0;
const char *name = NULL;
Expand Down Expand Up @@ -777,18 +779,33 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
continue;
}
if (!strcmp(arg, "--is-inside-git-dir")) {
printf("%s\n", is_inside_git_dir() ? "true"
: "false");
is_result = is_inside_git_dir();
if (quiet == 1) {
return_code |= is_result * (1 << 0);
} else {
printf("%s\n", is_result ? "true"
: "false");
}
continue;
}
if (!strcmp(arg, "--is-inside-work-tree")) {
printf("%s\n", is_inside_work_tree() ? "true"
: "false");
is_result = is_inside_work_tree();
if (quiet == 1) {
return_code |= is_result * (1 << 1);
} else {
printf("%s\n", is_result ? "true"
: "false");
}
continue;
}
if (!strcmp(arg, "--is-bare-repository")) {
printf("%s\n", is_bare_repository() ? "true"
: "false");
is_result = is_bare_repository();
if (quiet == 1) {
return_code |= is_result * (1 << 2);
} else {
printf("%s\n", is_result ? "true"
: "false");
}
continue;
}
if (!strcmp(arg, "--shared-index-path")) {
Expand Down Expand Up @@ -848,6 +865,9 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
continue;
verify_filename(prefix, arg, 1);
}
if (quiet && !verify) {
return return_code;
}
if (verify) {
if (revs_count == 1) {
show_rev(type, sha1, name);
Expand Down
31 changes: 18 additions & 13 deletions t/t1500-rev-parse.sh
Expand Up @@ -7,6 +7,11 @@ test_rev_parse() {
name=$1
shift

test_expect_success "$name: quiet" \
"test_expect_code $1 git rev-parse -q --is-inside-git-dir --is-inside-work-tree --is-bare-repository"
shift
[ $# -eq 0 ] && return

test_expect_success "$name: is-bare-repository" \
"test '$1' = \"\$(git rev-parse --is-bare-repository)\""
shift
Expand All @@ -33,28 +38,28 @@ test_rev_parse() {
[ $# -eq 0 ] && return
}

# label is-bare is-inside-git is-inside-work prefix git-dir
# label return_code is-bare is-inside-git is-inside-work prefix git-dir

ROOT=$(pwd)

test_rev_parse toplevel false false true '' .git
test_rev_parse toplevel 2 false false true '' .git

cd .git || exit 1
test_rev_parse .git/ false true false '' .
test_rev_parse .git/ 1 false true false '' .
cd objects || exit 1
test_rev_parse .git/objects/ false true false '' "$ROOT/.git"
test_rev_parse .git/objects/ 1 false true false '' "$ROOT/.git"
cd ../.. || exit 1

mkdir -p sub/dir || exit 1
cd sub/dir || exit 1
test_rev_parse subdirectory false false true sub/dir/ "$ROOT/.git"
test_rev_parse subdirectory 2 false false true sub/dir/ "$ROOT/.git"
cd ../.. || exit 1

git config core.bare true
test_rev_parse 'core.bare = true' true false false
test_rev_parse 'core.bare = true' 4 true false false

git config --unset core.bare
test_rev_parse 'core.bare undefined' false false true
test_rev_parse 'core.bare undefined' 2 false false true

mkdir work || exit 1
cd work || exit 1
Expand All @@ -63,25 +68,25 @@ GIT_CONFIG="$(pwd)"/../.git/config
export GIT_DIR GIT_CONFIG

git config core.bare false
test_rev_parse 'GIT_DIR=../.git, core.bare = false' false false true ''
test_rev_parse 'GIT_DIR=../.git, core.bare = false' 2 false false true ''

git config core.bare true
test_rev_parse 'GIT_DIR=../.git, core.bare = true' true false false ''
test_rev_parse 'GIT_DIR=../.git, core.bare = true' 4 true false false ''

git config --unset core.bare
test_rev_parse 'GIT_DIR=../.git, core.bare undefined' false false true ''
test_rev_parse 'GIT_DIR=../.git, core.bare undefined' 2 false false true ''

mv ../.git ../repo.git || exit 1
GIT_DIR=../repo.git
GIT_CONFIG="$(pwd)"/../repo.git/config

git config core.bare false
test_rev_parse 'GIT_DIR=../repo.git, core.bare = false' false false true ''
test_rev_parse 'GIT_DIR=../repo.git, core.bare = false' 2 false false true ''

git config core.bare true
test_rev_parse 'GIT_DIR=../repo.git, core.bare = true' true false false ''
test_rev_parse 'GIT_DIR=../repo.git, core.bare = true' 4 true false false ''

git config --unset core.bare
test_rev_parse 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
test_rev_parse 'GIT_DIR=../repo.git, core.bare undefined' 2 false false true ''

test_done

0 comments on commit dcec39b

Please sign in to comment.