Skip to content

chore: Various Bash improvements #1029

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

Merged
merged 1 commit into from
Feb 3, 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
4 changes: 2 additions & 2 deletions bin/git-archive-file
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ BRANCH=$(git rev-parse --abbrev-ref HEAD 2> /dev/null)

# get name of the most top folder of current directory, used for the
# output filename
ARCHIVE_NAME=$(basename "$(pwd)")
ARCHIVE_NAME=$(basename "$PWD")

if [[ $BRANCH = tags* ]]; then
BRANCH=$(git describe)
Expand All @@ -28,7 +28,7 @@ fi
FILENAME=${FILENAME//\//-}
FILENAME=${FILENAME//\\/-}
# combine path and filename
OUTPUT=$(pwd)/$FILENAME
OUTPUT=$PWD/$FILENAME

# building archive
git archive --format zip --output "$OUTPUT" "$BRANCH"
Expand Down
4 changes: 2 additions & 2 deletions bin/git-bulk
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,15 @@ function executBulkOp () {
parseWsName "$workspacespec"
if [[ -n $wsname ]] && [[ $rwsname != "$wsname" ]]; then continue; fi
eval cd "\"$rwsdir\""
local actual=$(pwd)
local actual=$PWD
[ "${quiet?}" != "true" ] && echo 1>&2 "Executing bulk operation in workspace ${inverse}$actual${reset}"

allGitFolders=( $(eval find -L . -name ".git") )

for line in ${allGitFolders[@]}; do
local gitrepodir=${line::${#line}-5} # cut the .git part of find results to have the root git directory of that repository
eval cd "\"$gitrepodir\"" # into git repo location
local curdir=$(pwd)
local curdir=$PWD
local leadingpath=${curdir#${actual}}
guardedExecution "$@"
eval cd "\"$rwsdir\"" # back to origin location of last find command
Expand Down
4 changes: 2 additions & 2 deletions bin/git-clear-soft
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

echo -n "Sure? - This command may delete files that cannot be recovered. Files and directories in .gitignore will be preserved [y/N]: "
read ans
if [ "$ans" == "y" ]
read -r ans
if [ "$ans" == "y" ]
then git clean -d -f && git reset --hard
fi
2 changes: 1 addition & 1 deletion bin/git-delete-submodule
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test ! -f '.gitmodules' && abort 2 '.gitmodules file not found'

NAME="${1%/}"
test -z "$(git config --file='.gitmodules' "submodule.$NAME.url")" \
&& abort 3 'Submodule not found'
&& abort 3 'Submodule not found'

# 1. Handle the .git directory
# 1.a. Delete the relevant section from .git/config
Expand Down
8 changes: 4 additions & 4 deletions bin/git-feature
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@ else

if [[ -n $remote ]] && [[ -z $start_point ]]
then
git create-branch -r $remote $branch
git create-branch -r "$remote" "$branch"
fi

if [[ -z $remote ]] && [[ -z $start_point ]]
then
git create-branch $branch
git create-branch "$branch"
fi

if [[ -n $remote ]] && [[ -n $start_point ]]
then
git create-branch -r $remote --from $start_point $branch
git create-branch -r "$remote" --from "$start_point" "$branch"
fi

if [[ -z $remote ]] && [[ -n $start_point ]]
then
git create-branch --from $start_point $branch
git create-branch --from "$start_point" "$branch"
fi
fi
10 changes: 5 additions & 5 deletions bin/git-fresh-branch
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

branch=$1

test -z $branch && echo "branch required." 1>&2 && exit 1
test -z "$branch" && echo "branch required." 1>&2 && exit 1

changes=`git status --porcelain`
changes=$(git status --porcelain)

clean()
{
git symbolic-ref HEAD refs/heads/$branch
git symbolic-ref HEAD "refs/heads/$branch"
rm .git/index
git clean -fdx
}

if [ ! -z "$changes" ]; then
read -p "All untracked changes will be lost. Continue [y/N]? " res
if [ -n "$changes" ]; then
read -rp "All untracked changes will be lost. Continue [y/N]? " res
case $res in
[Yy]* ) ;;
* ) exit 0;;
Expand Down
10 changes: 5 additions & 5 deletions bin/git-graft
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
src=$1
dst=$2

test -z $src && echo "source branch required." 1>&2 && exit 1
test -z $dst && echo "destination branch required." 1>&2 && exit 1
test -z "$src" && echo "source branch required." 1>&2 && exit 1
test -z "$dst" && echo "destination branch required." 1>&2 && exit 1

git checkout $dst \
&& git merge --no-ff $src \
&& git branch -d $src
git checkout "$dst" \
&& git merge --no-ff "$src" \
&& git branch -d "$src"
12 changes: 6 additions & 6 deletions bin/git-guilt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

for param in $*
for param in "$@"
do
case $param in
-h)
Expand Down Expand Up @@ -44,11 +44,11 @@ for file in $(git diff --name-only "$@")
do
test -n "$DEBUG" && echo "git blame $file"
# $1 - since $2 - until
git blame $NOT_WHITESPACE --line-porcelain "$1" -- "$file" 2> /dev/null |
LC_ALL=C sed -n "$PATTERN" | sort | uniq -c | LC_ALL=C sed 's/^\(.\)/- \1/' >> $MERGED_LOG
git blame $NOT_WHITESPACE --line-porcelain "$1" -- "$file" 2> /dev/null |
LC_ALL=C sed -n "$PATTERN" | sort | uniq -c | LC_ALL=C sed 's/^\(.\)/- \1/' >> "$MERGED_LOG"
# if $2 not given, use current commit as "until"
git blame $NOT_WHITESPACE --line-porcelain "${2-@}" -- "$file" 2> /dev/null |
LC_ALL=C sed -n "$PATTERN" | sort | uniq -c | LC_ALL=C sed 's/^\(.\)/+ \1/' >> $MERGED_LOG
LC_ALL=C sed -n "$PATTERN" | sort | uniq -c | LC_ALL=C sed 's/^\(.\)/+ \1/' >> "$MERGED_LOG"
done

DEBUG="$DEBUG" awk '
Expand All @@ -71,8 +71,8 @@ END {
printf("%d %s\n", contributors[people], people)
}
}
}' $MERGED_LOG | sort -nr | # only gawk supports built-in sort function
while read line
}' $MERGED_LOG | sort -nr | # only gawk supports built-in sort function
while read -r line
do
people=${line#* }
num=${line%% *}
Expand Down
2 changes: 1 addition & 1 deletion bin/git-ignore-io
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ print_in_alphabetical_order() {
for ignorable in $(echo "$gi_list" | sort);
do
first_character=${ignorable:0:1}
if [[ $first_character = $previous_first_character ]]; then
if [[ $first_character = "$previous_first_character" ]]; then
printf " %s" "$ignorable"
elif [[ $first = true ]]; then
previous_first_character=$first_character
Expand Down
4 changes: 2 additions & 2 deletions bin/git-info
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ echon "${COLOR_TITLE}## Local Branches:${NORMAL}"
echon "$(local_branches)"

SUBMODULES_LOG=$(submodules)
if [ ! -z "$SUBMODULES_LOG" ]; then
if [ -n "$SUBMODULES_LOG" ]; then
echon "${COLOR_TITLE}## Submodule(s):${NORMAL}"
echon "$SUBMODULES_LOG"
fi

echon "${COLOR_TITLE}## Most Recent Commit:${NORMAL}"
echon "$(most_recent_commit)"

if [ ! -z "$HIDE_CONFIG" ]; then
if [ -n "$HIDE_CONFIG" ]; then
echon "${COLOR_TITLE}## Configuration (.git/config):${NORMAL}"
echon "$(get_config)"
fi
2 changes: 1 addition & 1 deletion bin/git-local-commits
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash

git log @{upstream}..@ $*
git log "@{upstream}..@" "$@"
16 changes: 8 additions & 8 deletions bin/git-magic
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ while getopts "m:eapfh" arg; do
FORCE='-f'
;;
h)
echo $USAGE
echo "$USAGE"
exit 0
;;
?)
Expand All @@ -46,27 +46,27 @@ done
shift $((OPTIND-1))

if [[ $# != 0 ]]; then
echo "Unknown arguments: $@"
echo "Unknown arguments: $*"
echo "${USAGE}"
exit 1
fi

set -- "${ARGS[@]}" # restore positional parameters

if [[ $ALL == true ]]; then

# Check if there is no changes to stage
if [[ -z $(git status --porcelain) ]]; then
echo "No changes to commit"
exit 0
fi

# Get confirmation from user
git status
echo "Everything will be added"
read -p "Press enter to continue"
# Restore staging area so that, for example,
read -rp "Press enter to continue"

# Restore staging area so that, for example,
# add and modify will not be separate entries in status
git restore --staged . || true
git add .
Expand All @@ -79,7 +79,7 @@ if [[ $PUSH == true ]]; then
git push $FORCE
fi

# --no-edit by default. use option -e to override this
# --no-edit by default. use option -e to override this

# Arguments are passed with quoted "$@" to avoid misparsing

Expand Down
16 changes: 8 additions & 8 deletions bin/git-psykorebase
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function usage()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
--no-ff)
FF="--no-ff"
Expand Down Expand Up @@ -61,8 +61,8 @@ if [[ "$CONTINUE" == "yes" ]]; then

echo "Continuing rebasing of $SECONDARY_BRANCH on top of $PRIMARY_BRANCH"
git commit || exit 51
git branch -d ${SECONDARY_BRANCH} || exit 52
git branch -m ${TARGET_BRANCH} ${SECONDARY_BRANCH} || exit 53
git branch -d "${SECONDARY_BRANCH}" || exit 52
git branch -m "${TARGET_BRANCH}" "${SECONDARY_BRANCH}" || exit 53

elif [[ "$PRIMARY_BRANCH" == "" ]]; then
usage
Expand All @@ -71,14 +71,14 @@ else
echo "Rebasing $SECONDARY_BRANCH on top of $PRIMARY_BRANCH"
TARGET_BRANCH="${SECONDARY_BRANCH}-rebased-on-top-of-${PRIMARY_BRANCH}"

git checkout ${PRIMARY_BRANCH} || exit 41
git checkout -b ${TARGET_BRANCH} || exit 42
git merge ${SECONDARY_BRANCH} ${FF} \
git checkout "${PRIMARY_BRANCH}" || exit 41
git checkout -b "${TARGET_BRANCH}" || exit 42
git merge "${SECONDARY_BRANCH}" ${FF} \
-m "Psycho-rebased branch ${SECONDARY_BRANCH} on top of ${PRIMARY_BRANCH}"

if [[ $? == 0 ]]; then
git branch -d ${SECONDARY_BRANCH} || exit 43
git branch -m ${TARGET_BRANCH} ${SECONDARY_BRANCH} || exit 44
git branch -d "${SECONDARY_BRANCH}" || exit 43
git branch -m "${TARGET_BRANCH}" "${SECONDARY_BRANCH}" || exit 44
else
echo "Resolve the conflict and run ``${PROGRAM} --continue``."
exit 1
Expand Down
20 changes: 10 additions & 10 deletions bin/git-rebase-patch
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fi
# Use a temporary index.
index=$(git_extra_mktemp)
cleanup() {
rm $index
rm "$index"
exit 2
}
trap cleanup 2
Expand All @@ -27,16 +27,16 @@ echo "Trying to find a commit the patch applies to..."
rev=$(git rev-parse HEAD)
while [ $? = 0 ]
do
GIT_INDEX_FILE=$index git read-tree $rev
GIT_INDEX_FILE=$index git read-tree "$rev"

# Try to apply the patch.
GIT_INDEX_FILE=$index git apply --cached $1 >/dev/null 2>&1
GIT_INDEX_FILE=$index git apply --cached "$1" >/dev/null 2>&1
patch_failed=$?

# Do it again, but show the error, if the problem is the patch itself.
if [ $patch_failed = 128 ]
then
GIT_INDEX_FILE=$index git apply --index --check $1
GIT_INDEX_FILE=$index git apply --index --check "$1"
exit $patch_failed
fi

Expand All @@ -45,19 +45,19 @@ do
then
# Manufacture a commit.
tree=$(GIT_INDEX_FILE=$index git write-tree)
commit=$(git commit-tree $tree -p $rev -m "$1")
rm $index
commit=$(git commit-tree "$tree" -p "$rev" -m "$1")
rm "$index"

echo "Patch applied to $(git rev-parse --short $rev) as $(git rev-parse --short $commit)"
echo "Patch applied to $(git rev-parse --short "$rev") as $(git rev-parse --short "$commit")"

git cherry-pick $commit
git cherry-pick "$commit"
exit $?
fi

rev=$(git rev-parse --verify -q $rev^)
rev=$(git rev-parse --verify -q "$rev^")
done

# No compatible commit found. Restore.
echo "Failed to find a commit the patch applies to."
rm $index
rm "$index"
exit 1
4 changes: 2 additions & 2 deletions bin/git-rename-branch
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
set -e

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

if [ -z $2 ]; then
if [ -z "$2" ]; then
new_branch="$1"
old_branch="$(git symbolic-ref --short -q HEAD)"
else
Expand Down
8 changes: 4 additions & 4 deletions bin/git-rename-remote
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ set -euo pipefail
old=$1
new=$2

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

if ! git config --get "remote.$old.fetch" > /dev/null; then
echo "remote $old doesn't exist"
exit 1
fi

if git config --get "remote.$new.fetch" > /dev/null; then
git remote remove $new
git remote remove "$new"
fi
git remote rename $old $new
git remote rename "$old $new"
git remote -v
6 changes: 3 additions & 3 deletions bin/git-reset-file
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ commit="$2"

if [[ -f $file ]]; then
git rm --cached -q -f -- "$file"
if [[ -z $commit ]]; then
if [[ -z $commit ]]; then
git checkout HEAD -- "$file"
echo "Reset '$1' to HEAD"
else
git checkout "$commit" -- "$file"
echo "Reset '$1' to $commit"
fi
else
echo "File '$1' not found in $(pwd)"
else
echo "File '$1' not found in $PWD"
fi
Loading