Skip to content

Commit

Permalink
Merge pull request #105 from tegonal/ask-to-re-initialise-if-remote-b…
Browse files Browse the repository at this point in the history
…roken

move asking to re-initialise git if remote borken to own function
  • Loading branch information
robstoll authored May 4, 2023
2 parents f6e956e + 73071ca commit 61a9292
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
20 changes: 2 additions & 18 deletions src/gt-pull.sh
Original file line number Diff line number Diff line change
Expand Up @@ -210,25 +210,9 @@ function gt_pull() {
# shellcheck disable=SC2064
trap "gt_pull_cleanupRepo '$repo'" EXIT SIGINT

askToDeleteAndReInitialiseGitDirIfRemoteIsBroken "$workingDirAbsolute" "$remote"

cd "$repo"
if ! git remote | grep "$remote" >/dev/null; then
logError "looks like the .git directory of remote \033[0;36m%s\033[0m is broken. There is no remote %s set up in its gitconfig." "$remote" "$remote"
if [[ -f $gitconfig ]]; then
if askYesOrNo "Shall I delete the repo and re-initialise it based on %s" "$gitconfig"; then
# cd only necessary because we did a cd $repo beforehand, could be removed if we don't do it
cd "$workingDir"
deleteDirChmod777 "$repo"
reInitialiseGitDir "$workingDir" "$remote"
# cd only necessary because we did a cd $repo and then cd $workingDir beforehand, could be removed if we don't do it
cd "$repo"
else
exit 1
fi
else
logInfo >&2 "%s does not exist, cannot ask to re-initialise the repo, must abort" "$gitconfig"
exit 1
fi
fi
local tags
tags=$(git tag) || die "The following command failed (see above): git tag"
if grep "$tagToPull" <<<"$tags" >/dev/null; then
Expand Down
32 changes: 30 additions & 2 deletions src/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,32 @@ function reInitialiseGitDir() {
cp "$gitconfig" "$repo/.git/config" || die "could not copy %s to %s" "$gitconfig" "$repo/.git/config"
}

function askToDeleteAndReInitialiseGitDirIfRemoteIsBroken() {
local workingDirAbsolute remote
# shellcheck disable=SC2034 # is passed to parseFnArgs by name
local -ra params=(workingDirAbsolute remote)
parseFnArgs params "$@"

local repo gitconfig
source "$dir_of_gt/paths.source.sh" || die "could not source paths.source.sh"

if ! git --git-dir="$repo/.git" remote | grep "$remote" >/dev/null; then
logError "looks like the .git directory of remote \033[0;36m%s\033[0m is broken. There is no remote %s set up in its gitconfig. Following the remotes:" "$remote" "$remote"
git --git-dir="$repo/.git" remote
if [[ -f $gitconfig ]]; then
if askYesOrNo "Shall I delete the repo and re-initialise it based on %s" "$gitconfig"; then
deleteDirChmod777 "$repo"
reInitialiseGitDir "$workingDirAbsolute" "$remote"
else
exit 1
fi
else
logInfo >&2 "%s does not exist, cannot ask to re-initialise the repo, must abort" "$gitconfig"
exit 1
fi
fi
}

function reInitialiseGitDirIfDotGitNotPresent() {
local workingDirAbsolute remote
# shellcheck disable=SC2034 # is passed to parseFnArgs by name
Expand All @@ -145,6 +171,8 @@ function reInitialiseGitDirIfDotGitNotPresent() {
if ! [[ -d "$repo/.git" ]]; then
logInfo "repo directory (or its .git directory) does not exist for remote \033[0;36m%s\033[0m. We are going to re-initialise it based on the stored gitconfig" "$remote"
reInitialiseGitDir "$workingDirAbsolute" "$remote"
else
askToDeleteAndReInitialiseGitDirIfRemoteIsBroken "$workingDirAbsolute" "$remote"
fi
}

Expand Down Expand Up @@ -195,7 +223,7 @@ function validateGpgKeysAndImport() {

local -r sigExtension="sig"

# shellcheck disable=SC2317 # called by name
# shellcheck disable=SC2317 # called by name
function validateGpgKeysAndImport_do() {
findAscInDir "$sourceDir" -print0 >&3
echo ""
Expand Down Expand Up @@ -258,7 +286,7 @@ function importRemotesPulledPublicKeys() {
local gpgDir publicKeysDir repo
source "$dir_of_gt/paths.source.sh" || die "could not source paths.source.sh"

# shellcheck disable=SC2317 # called by name
# shellcheck disable=SC2317 # called by name
function importRemotesPublicKeys_importKeyCallback() {
local -r publicKey=$1
local -r sig=$2
Expand Down

0 comments on commit 61a9292

Please sign in to comment.