mirror of
https://github.com/git/git.git
synced 2025-12-12 20:36:24 +01:00
Merge branch 'ms/refs-exists'
"git refs exists" that works like "git show-ref --exists" has been added. * ms/refs-exists: t: add test for git refs exists subcommand t1422: refactor tests to be shareable t1403: split 'show-ref --exists' tests into a separate file builtin/refs: add 'exists' subcommand
This commit is contained in:
@@ -18,6 +18,7 @@ git refs list [--count=<count>] [--shell|--perl|--python|--tcl]
|
||||
[--contains[=<object>]] [--no-contains[=<object>]]
|
||||
[(--exclude=<pattern>)...] [--start-after=<marker>]
|
||||
[ --stdin | (<pattern>...)]
|
||||
git refs exists <ref>
|
||||
|
||||
DESCRIPTION
|
||||
-----------
|
||||
@@ -38,6 +39,12 @@ list::
|
||||
formatting, and sorting. This subcommand is an alias for
|
||||
linkgit:git-for-each-ref[1] and offers identical functionality.
|
||||
|
||||
exists::
|
||||
Check whether the given reference exists. Returns an exit code of 0 if
|
||||
it does, 2 if it is missing, and 1 in case looking up the reference
|
||||
failed with an error other than the reference being missing. This does
|
||||
not verify whether the reference resolves to an actual object.
|
||||
|
||||
OPTIONS
|
||||
-------
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "strbuf.h"
|
||||
#include "worktree.h"
|
||||
#include "for-each-ref.h"
|
||||
#include "refs/refs-internal.h"
|
||||
|
||||
#define REFS_MIGRATE_USAGE \
|
||||
N_("git refs migrate --ref-format=<format> [--no-reflog] [--dry-run]")
|
||||
@@ -14,6 +15,9 @@
|
||||
#define REFS_VERIFY_USAGE \
|
||||
N_("git refs verify [--strict] [--verbose]")
|
||||
|
||||
#define REFS_EXISTS_USAGE \
|
||||
N_("git refs exists <ref>")
|
||||
|
||||
static int cmd_refs_migrate(int argc, const char **argv, const char *prefix,
|
||||
struct repository *repo UNUSED)
|
||||
{
|
||||
@@ -113,6 +117,48 @@ static int cmd_refs_list(int argc, const char **argv, const char *prefix,
|
||||
return for_each_ref_core(argc, argv, prefix, repo, refs_list_usage);
|
||||
}
|
||||
|
||||
static int cmd_refs_exists(int argc, const char **argv, const char *prefix,
|
||||
struct repository *repo UNUSED)
|
||||
{
|
||||
struct strbuf unused_referent = STRBUF_INIT;
|
||||
struct object_id unused_oid;
|
||||
unsigned int unused_type;
|
||||
int failure_errno = 0;
|
||||
const char *ref;
|
||||
int ret = 0;
|
||||
const char * const exists_usage[] = {
|
||||
REFS_EXISTS_USAGE,
|
||||
NULL,
|
||||
};
|
||||
struct option options[] = {
|
||||
OPT_END(),
|
||||
};
|
||||
|
||||
argc = parse_options(argc, argv, prefix, options, exists_usage, 0);
|
||||
if (argc != 1)
|
||||
die(_("'git refs exists' requires a reference"));
|
||||
|
||||
ref = *argv++;
|
||||
if (refs_read_raw_ref(get_main_ref_store(the_repository), ref,
|
||||
&unused_oid, &unused_referent, &unused_type,
|
||||
&failure_errno)) {
|
||||
if (failure_errno == ENOENT || failure_errno == EISDIR) {
|
||||
error(_("reference does not exist"));
|
||||
ret = 2;
|
||||
} else {
|
||||
errno = failure_errno;
|
||||
error_errno(_("failed to look up reference"));
|
||||
ret = 1;
|
||||
}
|
||||
|
||||
goto out;
|
||||
}
|
||||
|
||||
out:
|
||||
strbuf_release(&unused_referent);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int cmd_refs(int argc,
|
||||
const char **argv,
|
||||
const char *prefix,
|
||||
@@ -122,6 +168,7 @@ int cmd_refs(int argc,
|
||||
REFS_MIGRATE_USAGE,
|
||||
REFS_VERIFY_USAGE,
|
||||
"git refs list " COMMON_USAGE_FOR_EACH_REF,
|
||||
REFS_EXISTS_USAGE,
|
||||
NULL,
|
||||
};
|
||||
parse_opt_subcommand_fn *fn = NULL;
|
||||
@@ -129,6 +176,7 @@ int cmd_refs(int argc,
|
||||
OPT_SUBCOMMAND("migrate", &fn, cmd_refs_migrate),
|
||||
OPT_SUBCOMMAND("verify", &fn, cmd_refs_verify),
|
||||
OPT_SUBCOMMAND("list", &fn, cmd_refs_list),
|
||||
OPT_SUBCOMMAND("exists", &fn, cmd_refs_exists),
|
||||
OPT_END(),
|
||||
};
|
||||
|
||||
|
||||
@@ -206,11 +206,13 @@ integration_tests = [
|
||||
't1419-exclude-refs.sh',
|
||||
't1420-lost-found.sh',
|
||||
't1421-reflog-write.sh',
|
||||
't1422-show-ref-exists.sh',
|
||||
't1430-bad-ref-name.sh',
|
||||
't1450-fsck.sh',
|
||||
't1451-fsck-buffer.sh',
|
||||
't1460-refs-migrate.sh',
|
||||
't1461-refs-list.sh',
|
||||
't1462-refs-exists.sh',
|
||||
't1500-rev-parse.sh',
|
||||
't1501-work-tree.sh',
|
||||
't1502-rev-parse-parseopt.sh',
|
||||
@@ -1221,4 +1223,4 @@ if perl.found() and time.found()
|
||||
timeout: 0,
|
||||
)
|
||||
endforeach
|
||||
endif
|
||||
endif
|
||||
|
||||
77
t/show-ref-exists-tests.sh
Normal file
77
t/show-ref-exists-tests.sh
Normal file
@@ -0,0 +1,77 @@
|
||||
git_show_ref_exists=${git_show_ref_exists:-git show-ref --exists}
|
||||
|
||||
test_expect_success setup '
|
||||
test_commit --annotate A &&
|
||||
git checkout -b side &&
|
||||
test_commit --annotate B &&
|
||||
git checkout main &&
|
||||
test_commit C &&
|
||||
git branch B A^0
|
||||
'
|
||||
|
||||
test_expect_success '--exists with existing reference' '
|
||||
${git_show_ref_exists} refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
|
||||
'
|
||||
|
||||
test_expect_success '--exists with missing reference' '
|
||||
test_expect_code 2 ${git_show_ref_exists} refs/heads/does-not-exist
|
||||
'
|
||||
|
||||
test_expect_success '--exists does not use DWIM' '
|
||||
test_expect_code 2 ${git_show_ref_exists} $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 2>err &&
|
||||
grep "reference does not exist" err
|
||||
'
|
||||
|
||||
test_expect_success '--exists with HEAD' '
|
||||
${git_show_ref_exists} HEAD
|
||||
'
|
||||
|
||||
test_expect_success '--exists with bad reference name' '
|
||||
test_when_finished "git update-ref -d refs/heads/bad...name" &&
|
||||
new_oid=$(git rev-parse HEAD) &&
|
||||
test-tool ref-store main update-ref msg refs/heads/bad...name $new_oid $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
|
||||
${git_show_ref_exists} refs/heads/bad...name
|
||||
'
|
||||
|
||||
test_expect_success '--exists with arbitrary symref' '
|
||||
test_when_finished "git symbolic-ref -d refs/symref" &&
|
||||
git symbolic-ref refs/symref refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
|
||||
${git_show_ref_exists} refs/symref
|
||||
'
|
||||
|
||||
test_expect_success '--exists with dangling symref' '
|
||||
test_when_finished "git symbolic-ref -d refs/heads/dangling" &&
|
||||
git symbolic-ref refs/heads/dangling refs/heads/does-not-exist &&
|
||||
${git_show_ref_exists} refs/heads/dangling
|
||||
'
|
||||
|
||||
test_expect_success '--exists with nonexistent object ID' '
|
||||
test-tool ref-store main update-ref msg refs/heads/missing-oid $(test_oid 001) $ZERO_OID REF_SKIP_OID_VERIFICATION &&
|
||||
${git_show_ref_exists} refs/heads/missing-oid
|
||||
'
|
||||
|
||||
test_expect_success '--exists with non-commit object' '
|
||||
tree_oid=$(git rev-parse HEAD^{tree}) &&
|
||||
test-tool ref-store main update-ref msg refs/heads/tree ${tree_oid} $ZERO_OID REF_SKIP_OID_VERIFICATION &&
|
||||
${git_show_ref_exists} refs/heads/tree
|
||||
'
|
||||
|
||||
test_expect_success '--exists with directory fails with generic error' '
|
||||
cat >expect <<-EOF &&
|
||||
error: reference does not exist
|
||||
EOF
|
||||
test_expect_code 2 ${git_show_ref_exists} refs/heads 2>err &&
|
||||
test_cmp expect err
|
||||
'
|
||||
|
||||
test_expect_success '--exists with non-existent special ref' '
|
||||
test_expect_code 2 ${git_show_ref_exists} FETCH_HEAD
|
||||
'
|
||||
|
||||
test_expect_success '--exists with existing special ref' '
|
||||
test_when_finished "rm .git/FETCH_HEAD" &&
|
||||
git rev-parse HEAD >.git/FETCH_HEAD &&
|
||||
${git_show_ref_exists} FETCH_HEAD
|
||||
'
|
||||
|
||||
test_done
|
||||
@@ -228,69 +228,4 @@ test_expect_success 'show-ref sub-modes are mutually exclusive' '
|
||||
grep "cannot be used together" err
|
||||
'
|
||||
|
||||
test_expect_success '--exists with existing reference' '
|
||||
git show-ref --exists refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
|
||||
'
|
||||
|
||||
test_expect_success '--exists with missing reference' '
|
||||
test_expect_code 2 git show-ref --exists refs/heads/does-not-exist
|
||||
'
|
||||
|
||||
test_expect_success '--exists does not use DWIM' '
|
||||
test_expect_code 2 git show-ref --exists $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 2>err &&
|
||||
grep "reference does not exist" err
|
||||
'
|
||||
|
||||
test_expect_success '--exists with HEAD' '
|
||||
git show-ref --exists HEAD
|
||||
'
|
||||
|
||||
test_expect_success '--exists with bad reference name' '
|
||||
test_when_finished "git update-ref -d refs/heads/bad...name" &&
|
||||
new_oid=$(git rev-parse HEAD) &&
|
||||
test-tool ref-store main update-ref msg refs/heads/bad...name $new_oid $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
|
||||
git show-ref --exists refs/heads/bad...name
|
||||
'
|
||||
|
||||
test_expect_success '--exists with arbitrary symref' '
|
||||
test_when_finished "git symbolic-ref -d refs/symref" &&
|
||||
git symbolic-ref refs/symref refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
|
||||
git show-ref --exists refs/symref
|
||||
'
|
||||
|
||||
test_expect_success '--exists with dangling symref' '
|
||||
test_when_finished "git symbolic-ref -d refs/heads/dangling" &&
|
||||
git symbolic-ref refs/heads/dangling refs/heads/does-not-exist &&
|
||||
git show-ref --exists refs/heads/dangling
|
||||
'
|
||||
|
||||
test_expect_success '--exists with nonexistent object ID' '
|
||||
test-tool ref-store main update-ref msg refs/heads/missing-oid $(test_oid 001) $ZERO_OID REF_SKIP_OID_VERIFICATION &&
|
||||
git show-ref --exists refs/heads/missing-oid
|
||||
'
|
||||
|
||||
test_expect_success '--exists with non-commit object' '
|
||||
tree_oid=$(git rev-parse HEAD^{tree}) &&
|
||||
test-tool ref-store main update-ref msg refs/heads/tree ${tree_oid} $ZERO_OID REF_SKIP_OID_VERIFICATION &&
|
||||
git show-ref --exists refs/heads/tree
|
||||
'
|
||||
|
||||
test_expect_success '--exists with directory fails with generic error' '
|
||||
cat >expect <<-EOF &&
|
||||
error: reference does not exist
|
||||
EOF
|
||||
test_expect_code 2 git show-ref --exists refs/heads 2>err &&
|
||||
test_cmp expect err
|
||||
'
|
||||
|
||||
test_expect_success '--exists with non-existent special ref' '
|
||||
test_expect_code 2 git show-ref --exists FETCH_HEAD
|
||||
'
|
||||
|
||||
test_expect_success '--exists with existing special ref' '
|
||||
test_when_finished "rm .git/FETCH_HEAD" &&
|
||||
git rev-parse HEAD >.git/FETCH_HEAD &&
|
||||
git show-ref --exists FETCH_HEAD
|
||||
'
|
||||
|
||||
test_done
|
||||
|
||||
9
t/t1422-show-ref-exists.sh
Executable file
9
t/t1422-show-ref-exists.sh
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
|
||||
test_description='show-ref --exists'
|
||||
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
|
||||
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
|
||||
|
||||
. ./test-lib.sh
|
||||
|
||||
. "$TEST_DIRECTORY"/show-ref-exists-tests.sh
|
||||
10
t/t1462-refs-exists.sh
Executable file
10
t/t1462-refs-exists.sh
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/bin/sh
|
||||
|
||||
test_description='refs exists'
|
||||
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
|
||||
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
|
||||
|
||||
. ./test-lib.sh
|
||||
|
||||
git_show_ref_exists='git refs exists'
|
||||
. "$TEST_DIRECTORY"/show-ref-exists-tests.sh
|
||||
Reference in New Issue
Block a user