Skip to content

Commit 12f2c9d

Browse files
committed
Split repository metadata gathering into a function
This will prevent the issue of error exiting for certain option flags when outside of a Git repository (like `--help` and `--version`). As part of this, we must also prevent error exits from certain Git commands so we can handle notifying the user more cleanly.
1 parent f771f4d commit 12f2c9d

1 file changed

Lines changed: 50 additions & 44 deletions

File tree

transcrypt

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,7 @@ readonly VERSION='2.0.0'
2121
# the default cipher to utilize
2222
readonly DEFAULT_CIPHER='aes-256-cbc'
2323

24-
# regular expression used to test user input
25-
readonly YES_REGEX='^[Yy]$'
26-
27-
## Repository Metadata
28-
29-
# whether or not transcrypt is already configured
30-
readonly CONFIGURED=$(git config --get --local transcrypt.version 2>/dev/null)
31-
32-
# the current git repository's top-level directory
33-
readonly REPO=$(git rev-parse --show-toplevel 2>/dev/null)
34-
35-
# whether or not a HEAD revision exists
36-
readonly HEAD_EXISTS=$(git rev-parse --verify --quiet HEAD 2>/dev/null)
37-
38-
# https://github.com/RichiH/vcsh
39-
# whether or not the git repository is running under vcsh
40-
readonly IS_VCSH=$(git config --get --local --bool vcsh.vcsh 2>/dev/null)
41-
42-
# whether or not the git repository is bare
43-
readonly IS_BARE=$(git rev-parse --is-bare-repository 2>/dev/null)
44-
45-
## Git Directory Handling
24+
##### FUNCTIONS
4625

4726
# print a canonicalized absolute pathname
4827
realpath() {
@@ -72,21 +51,40 @@ realpath() {
7251
fi
7352
}
7453

75-
# the current git repository's .git directory
76-
RELATIVE_GIT_DIR=$(git rev-parse --git-dir 2>/dev/null)
77-
readonly GIT_DIR=$(realpath "$RELATIVE_GIT_DIR" 2>/dev/null)
54+
# establish repository metadata and directory handling
55+
gather_repo_metadata() {
56+
# whether or not transcrypt is already configured
57+
readonly CONFIGURED=$(git config --get --local transcrypt.version 2>/dev/null)
7858

79-
# the current git repository's gitattributes file
80-
readonly CORE_ATTRIBUTES=$(git config --get --local --path core.attributesFile)
81-
if [[ $CORE_ATTRIBUTES ]]; then
82-
readonly GIT_ATTRIBUTES=$CORE_ATTRIBUTES
83-
elif [[ $IS_BARE == 'true' ]] || [[ $IS_VCSH == 'true' ]]; then
84-
readonly GIT_ATTRIBUTES="${GIT_DIR}/info/attributes"
85-
else
86-
readonly GIT_ATTRIBUTES="${REPO}/.gitattributes"
87-
fi
59+
# the current git repository's top-level directory
60+
readonly REPO=$(git rev-parse --show-toplevel 2>/dev/null)
8861

89-
##### FUNCTIONS
62+
# whether or not a HEAD revision exists
63+
readonly HEAD_EXISTS=$(git rev-parse --verify --quiet HEAD 2>/dev/null)
64+
65+
# https://github.com/RichiH/vcsh
66+
# whether or not the git repository is running under vcsh
67+
readonly IS_VCSH=$(git config --get --local --bool vcsh.vcsh 2>/dev/null)
68+
69+
# whether or not the git repository is bare
70+
readonly IS_BARE=$(git rev-parse --is-bare-repository 2>/dev/null || printf 'false')
71+
72+
# the current git repository's .git directory
73+
local RELATIVE_GIT_DIR
74+
RELATIVE_GIT_DIR=$(git rev-parse --git-dir 2>/dev/null || printf '')
75+
readonly GIT_DIR=$(realpath "$RELATIVE_GIT_DIR" 2>/dev/null)
76+
77+
# the current git repository's gitattributes file
78+
local CORE_ATTRIBUTES
79+
CORE_ATTRIBUTES=$(git config --get --local --path core.attributesFile 2>/dev/null || printf '')
80+
if [[ $CORE_ATTRIBUTES ]]; then
81+
readonly GIT_ATTRIBUTES=$CORE_ATTRIBUTES
82+
elif [[ $IS_BARE == 'true' ]] || [[ $IS_VCSH == 'true' ]]; then
83+
readonly GIT_ATTRIBUTES="${GIT_DIR}/info/attributes"
84+
else
85+
readonly GIT_ATTRIBUTES="${REPO}/.gitattributes"
86+
fi
87+
}
9088

9189
# print a message to stderr
9290
warn() {
@@ -715,15 +713,16 @@ help() {
715713

716714
# reset all variables that might be set
717715
cipher=''
718-
password=''
719-
interactive='true'
720716
display_config=''
721-
rekey=''
722717
flush_creds=''
723-
uninstall=''
724-
show_file=''
725-
gpg_recipient=''
726718
gpg_import_file=''
719+
gpg_recipient=''
720+
interactive='true'
721+
list=''
722+
password=''
723+
rekey=''
724+
show_file=''
725+
uninstall=''
727726

728727
# used to bypass certain safety checks
729728
requires_existing_config=''
@@ -771,8 +770,7 @@ while [[ "${1:-}" != '' ]]; do
771770
requires_clean_repo=''
772771
;;
773772
-l | --list)
774-
list_files
775-
exit 0
773+
list='true'
776774
;;
777775
-s | --show-raw)
778776
show_file=$2
@@ -824,12 +822,20 @@ while [[ "${1:-}" != '' ]]; do
824822
shift
825823
done
826824

825+
gather_repo_metadata
826+
827827
# always run our safety checks
828828
run_safety_checks
829829

830+
# regular expression used to test user input
831+
readonly YES_REGEX='^[Yy]$'
832+
830833
# in order to keep behavior consistent no matter what order the options were
831834
# specified in, we must run these here rather than in the case statement above
832-
if [[ $uninstall ]]; then
835+
if [[ $list ]]; then
836+
list_files
837+
exit 0
838+
elif [[ $uninstall ]]; then
833839
uninstall_transcrypt
834840
exit 0
835841
elif [[ $display_config ]] && [[ $flush_creds ]]; then

0 commit comments

Comments
 (0)