Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Fix more Bash inconsistencies #1021

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions bin/git-archive-file
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ ARCHIVE_NAME=$(basename "$(pwd)")

if [[ $BRANCH = tags* ]]; then
BRANCH=$(git describe)
echo Building for tag \"$BRANCH\"
echo Building for tag "\"$BRANCH\""
FILENAME=$ARCHIVE_NAME.$BRANCH.zip
else
echo Building archive on branch \"$BRANCH\"
echo Building archive on branch "\"$BRANCH\""
# get a version string, so archives will not be overwritten when creating
# many of them
VERSION=$(git describe --always --long)
# if not on master append branch name into the filename
if [ "$BRANCH" = $(git_extra_default_branch) ]; then
if [ "$BRANCH" = "$(git_extra_default_branch)" ]; then
FILENAME=$ARCHIVE_NAME.$VERSION.zip
else
FILENAME=$ARCHIVE_NAME.$VERSION.$BRANCH.zip
Expand All @@ -31,7 +31,7 @@ FILENAME=${FILENAME//\\/-}
OUTPUT=$(pwd)/$FILENAME

# building archive
git archive --format zip --output "$OUTPUT" $BRANCH
git archive --format zip --output "$OUTPUT" "$BRANCH"

# also display size of the resulting file
echo Saved to \""$FILENAME"\" \("$(du -h "$OUTPUT" | cut -f1)"\)
2 changes: 1 addition & 1 deletion bin/git-browse
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ if [[ $remote == "" ]]; then
fi

# get remote url
remote_url=$(git remote get-url $remote)
remote_url=$(git remote get-url "$remote")

if [[ $? -ne 0 ]]; then
exit $?
Expand Down
2 changes: 1 addition & 1 deletion bin/git-browse-ci
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ else
remote=$1
fi

if [[ $remote == "" ]]
if [[ -z $remote ]]
then
echo "Remote not found"
exit 1
Expand Down
10 changes: 5 additions & 5 deletions bin/git-bulk
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ function checkGitCommand () {

# check if workspace name is registered
function checkWSName () {
while read workspace; do
while read -r workspace; do
parseWsName "$workspace"
if [[ $rwsname == "$wsname" ]]; then return; fi
done <<< "$(echo "$(listall)")"
done <<< "$(listall)"
# when here the ws name was not found
usage && echo 1>&2 "error: unknown workspace name: $wsname" && exit 1
}
Expand All @@ -106,14 +106,14 @@ function parseWsName () {

# detects the wsname of the current directory
function wsnameToCurrent () {
while read workspace; do
while read -r workspace; do
if [ -z "$workspace" ]; then continue; fi
parseWsName "$workspace"
if echo "$PWD" | grep -o -q "$rwsdir"; then wsname="$rwsname" && return; fi
done <<< "$(echo "$(listall)")"
done <<< "$(listall)"
# when here then not in workspace dir
echo 1>&2 "error: you are not in a workspace directory. your registered workspaces are:" && \
wslist="$(echo "$(listall)")" && echo 1>&2 "${wslist:-'<no workspaces defined yet>'}" && exit 1
wslist="$(listall)" && echo 1>&2 "${wslist:-'<no workspaces defined yet>'}" && exit 1
}

# helper to check number of arguments.
Expand Down
18 changes: 9 additions & 9 deletions bin/git-create-branch
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ test -z $BRANCH && echo "branch argument required." 1>&2 && exit 1
if [[ -n $REMOTE ]]
then
stderr=$(git_extra_mktemp)
git ls-remote --exit-code $REMOTE 1>/dev/null 2>"$stderr"
git ls-remote --exit-code "$REMOTE" 1>/dev/null 2>"$stderr"
REMOTE_EXIT=$?
REMOTE_ERROR=$(<"$stderr")
rm -f "$stderr"
if [ $REMOTE_EXIT -eq 0 ]
then
if [[ -n $START_POINT ]]; then
git fetch $REMOTE
git checkout --track -b $BRANCH $START_POINT
git push $REMOTE HEAD:refs/heads/$BRANCH
git fetch "$REMOTE"
git checkout --track -b "$BRANCH" "$START_POINT"
git push "$REMOTE" "HEAD:refs/heads/$BRANCH"
else
git push $REMOTE HEAD:refs/heads/$BRANCH
git fetch $REMOTE
git checkout --track -b $BRANCH $REMOTE/$BRANCH
git push "$REMOTE" "HEAD:refs/heads/$BRANCH"
git fetch "$REMOTE"
git checkout --track -b "$BRANCH" "$REMOTE/$BRANCH"
fi
exit $?
else
Expand All @@ -70,7 +70,7 @@ fi

if [[ -n $START_POINT ]]
then
git checkout -b $BRANCH "$START_POINT"
git checkout -b "$BRANCH" "$START_POINT"
else
git checkout -b $BRANCH
git checkout -b "$BRANCH"
fi
14 changes: 7 additions & 7 deletions bin/git-delete-branch
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
set -e

# Assert there is at least one branch provided
test -z $1 && echo "branch required." 1>&2 && exit 1
test -z "$1" && echo "branch required." 1>&2 && exit 1

for branch in "$@"
do
remote=$(git config branch.$branch.remote || echo "origin")
ref=$(git config branch.$branch.merge || echo "refs/heads/$branch")
remote=$(git config "branch.$branch.remote" || echo "origin")
ref=$(git config "branch.$branch.merge" || echo "refs/heads/$branch")

git branch -D $branch || true
git branch -D "$branch" || true
# Avoid deleting local upstream
[ "$remote" == "." ] && continue
git branch -d -r $remote/$branch || continue
git push $remote :$ref
[ "$remote" = "." ] && continue
git branch -d -r "$remote/$branch" || continue
git push "$remote" ":$ref"
done
2 changes: 1 addition & 1 deletion bin/git-delete-squashed-branches
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fi
git for-each-ref refs/heads/ "--format=%(refname:short)" | while read -r branch; do
mergeBase=$(git merge-base "$targetBranch" "$branch")

if [[ $(git cherry $targetBranch $(git commit-tree $(git rev-parse $branch\^{tree}) -p $mergeBase -m _)) == "-"* ]]; then
if [[ $(git cherry "$targetBranch" $(git commit-tree $(git rev-parse $branch\^{tree}) -p $mergeBase -m _)) == "-"* ]]; then
git branch -D "$branch"
fi
done
4 changes: 2 additions & 2 deletions bin/git-delete-tag
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ do
done

# Delete all the tags
git tag -d $local_tags
git push $origin $origin_refs
git tag -d "$local_tags"
git push "$origin" "$origin_refs"
2 changes: 1 addition & 1 deletion bin/git-delta
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ else
fi
fi

git diff --name-only --diff-filter=$filter $branch
git diff --name-only --diff-filter="$filter" "$branch"
4 changes: 2 additions & 2 deletions bin/git-gh-pages
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ echo 'setting up gh-pages'
echo '-------------------'

echo 'Tell me your github account username: '
read username
read -r username

echo 'Now, tell me your repository name: '
read repository
read -r repository

git stash \
&& git checkout -b 'gh-pages' \
Expand Down
6 changes: 3 additions & 3 deletions bin/git-merge-repo
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ branch=$2
prefix=$3
flat=0

if test $prefix = "."; then
if test "$prefix" = "."; then
prefix=$(mktemp -u 'git-merge-repo.XXXXXXX')
flat=1
fi
Expand All @@ -16,10 +16,10 @@ message=$(git log -1 --pretty=%B)

if test $flat -eq 1; then
git stash -u
mv -i $prefix/* ./
mv -i "$prefix"/* ./
git undo
git add .
git commit -am "$message"
git stash apply
rm -drf $prefix
rm -drf "$prefix"
fi
12 changes: 6 additions & 6 deletions bin/git-mr
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ if [ -z "${1-}" ] ; then
fi

if test "$1" = "clean"; then
git for-each-ref refs/heads/mr/* --format='%(refname)' | while read ref; do
git branch -D ${ref#refs/heads/}
git for-each-ref refs/heads/mr/* --format='%(refname)' | while read -r ref; do
git branch -D "${ref#refs/heads/}"
done
exit 0
elif [[ $1 =~ ^(https?://[^/]+/(.+))/merge_requests/([0-9]+).*$ ]]; then
Expand All @@ -22,7 +22,7 @@ fi

branch=mr/$id
remote_ref=refs/merge-requests/$id/head
git fetch -fu $remote $remote_ref:$branch
git checkout $branch
git config --local --replace branch.$branch.merge $remote_ref
git config --local --replace branch.$branch.remote $remote
git fetch -fu "$remote" "$remote_ref:$branch"
git checkout "$branch"
git config --local --replace "branch.$branch.merge" "$remote_ref"
git config --local --replace "branch.$branch.remote" "$remote"
2 changes: 1 addition & 1 deletion bin/git-psykorebase
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function usage()
echo " -c|--continue: Continue after the user updates conflicts."
}

while [[ $# > 0 ]]
while [[ $# -gt 0 ]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed? > is be perfectly and expressive find within [[ ... ]], isn't it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

> and < perform lexicographical comparison rather than a numerical comparison in this context, so it was changed to -gt

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Thx for the explanation. With this new knowledge I now have to have a look at some of my own scripts as well :)

do
key="$1"

Expand Down
8 changes: 4 additions & 4 deletions bin/git-release
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ set -e
hook() {
local hook=.git/hooks/$1.sh
# compat without extname
if test ! -f $hook; then
if test ! -f "$hook"; then
hook=.git/hooks/$1
fi

if test -f $hook; then
if test -f "$hook"; then
echo "... $1"
shift
if test -x $hook; then
if test -x "$hook"; then
$hook "$@"
else
. $hook "$@"
. "$hook" "$@"
fi
fi
}
Expand Down
12 changes: 6 additions & 6 deletions bin/git-rename-tag
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
old=$1
new=$2

test -z $old && echo "old tag name required." 1>&2 && exit 1
test -z $new && echo "new tag name required." 1>&2 && exit 1
test -z "$old" && echo "old tag name required." 1>&2 && exit 1
test -z "$new" && echo "new tag name required." 1>&2 && exit 1

git tag $new $old
git tag -d $old
git push origin $new
git push origin :refs/tags/$old
git tag "$new" "$old"
git tag -d "$old"
git push origin "$new"
git push origin ":refs/tags/$old"
2 changes: 1 addition & 1 deletion bin/git-repl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ echo "type 'ls' to ls files below current directory, '!command' to execute any c

while true; do
# Current branch
cur=`git symbolic-ref HEAD 2> /dev/null | cut -d/ -f3-`
cur=$(git symbolic-ref HEAD 2> /dev/null | cut -d/ -f3-)

# Prompt
if test -n "$cur"; then
Expand Down
8 changes: 4 additions & 4 deletions bin/git-reset-file
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ file="$1"
commit="$2"

if [[ -f $file ]]; then
git rm --cached -q -f -- $file
git rm --cached -q -f -- "$file"
if [[ -z $commit ]]; then
git checkout HEAD -- $file
git checkout HEAD -- "$file"
echo "Reset '$1' to HEAD"
else
git checkout $commit -- $file
git checkout "$commit" -- "$file"
echo "Reset '$1' to $commit"
fi
else
echo "File '$1' not found in `pwd`"
echo "File '$1' not found in $(pwd)"
fi
6 changes: 3 additions & 3 deletions bin/git-scp
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,16 @@ function _error()
[ $# -eq 0 ] && _usage && exit 0

echo
echo ERROR: "$@"
echo "ERROR: $*"
echo
exit 1
}

### OPTIONS ###
case $(basename $0) in
case $(basename "$0") in
git-scp)
case $1 in
''|-h|'?'|help|--help) shift; _test_git_scp; _usage $@;;
''|-h|'?'|help|--help) shift; _test_git_scp; _usage "$@";;
*) scp_and_stage $@;;
esac
;;
Expand Down
3 changes: 1 addition & 2 deletions bin/git-stamp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ EOF

error() {
if [[ -n "$1" ]]; then
local msg=$( echo "error: $1" )
echo -e "${msg}" >&2
echo "error: $1" >&2
fi
usage
exit 1
Expand Down
2 changes: 1 addition & 1 deletion bin/git-touch
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ USAGE(){

test $# -lt 1 && USAGE

for filename in $@; do
for filename in "$@"; do
touch "$filename" \
&& git add "$filename"
done