chore: Various Bash improvements (#1029)

This commit is contained in:
Edwin Kofler 2023-02-02 17:41:36 -08:00 committed by GitHub
parent 9c3343d1ee
commit 75d0dbff6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
25 changed files with 95 additions and 95 deletions

View file

@ -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)
@ -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"

View file

@ -132,7 +132,7 @@ 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") )
@ -140,7 +140,7 @@ function executBulkOp () {
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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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;;

View file

@ -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"

View file

@ -1,6 +1,6 @@
#!/usr/bin/env bash
for param in $*
for param in "$@"
do
case $param in
-h)
@ -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 '
@ -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%% *}

View file

@ -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

View file

@ -65,7 +65,7 @@ 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
@ -73,7 +73,7 @@ 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

View file

@ -1,3 +1,3 @@
#!/usr/bin/env bash
git log @{upstream}..@ $*
git log "@{upstream}..@" "$@"

View file

@ -33,7 +33,7 @@ while getopts "m:eapfh" arg; do
FORCE='-f'
;;
h)
echo $USAGE
echo "$USAGE"
exit 0
;;
?)
@ -46,7 +46,7 @@ done
shift $((OPTIND-1))
if [[ $# != 0 ]]; then
echo "Unknown arguments: $@"
echo "Unknown arguments: $*"
echo "${USAGE}"
exit 1
fi
@ -54,19 +54,19 @@ 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 .
@ -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

View file

@ -22,7 +22,7 @@ function usage()
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--no-ff)
FF="--no-ff"
@ -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
@ -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

View file

@ -17,7 +17,7 @@ fi
# Use a temporary index.
index=$(git_extra_mktemp)
cleanup() {
rm $index
rm "$index"
exit 2
}
trap cleanup 2
@ -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
@ -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

View file

@ -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

View file

@ -5,8 +5,8 @@ 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"
@ -14,7 +14,7 @@ if ! git config --get "remote.$old.fetch" > /dev/null; then
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

View file

@ -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

View file

@ -64,7 +64,7 @@ function _dos2unix()
function _sanitize()
{
git config --get-all extras.scp.sanitize | while read i
git config --get-all extras.scp.sanitize | while read -r i
do
case $i in
php_lint) php_lint $@;; # git config --global --add extras.scp.sanitize php_lint
@ -76,10 +76,10 @@ function _sanitize()
function scp_and_stage
{
set_remote $1
set_remote "$1"
shift
local refhead="$(git rev-parse --quiet --verify $1)"
local refhead="$(git rev-parse --quiet --verify "$1")"
if [ -n "$refhead" ]
then
shift
@ -92,8 +92,8 @@ function scp_and_stage
list=$(git ls-files "$@")" "$(git ls-files -o "$@")
elif [ -n "$refhead" ]
then
git diff --stat $refhead
list=$(git diff $refhead --name-only)
git diff --stat "$refhead"
list=$(git diff "$refhead" --name-only)
else
git diff
list=$(git diff --name-only)
@ -105,12 +105,12 @@ function scp_and_stage
if [ -n "$list" ]
then
local _TMP=${0///}
echo "$list" > $_TMP &&
echo "$list" > "$_TMP" &&
_sanitize $list &&
_info Pushing to $remote \($(git config remote.$remote.url)\) &&
rsync -rlDv --files-from=$_TMP ./ "$(git config remote.$remote.url)/" &&
_info "Pushing to $remote ($(git config "remote.$remote.url"))" &&
rsync -rlDv --files-from="$_TMP" ./ "$(git config "remote.$remote.url")/" &&
git add --force $list &&
rm $_TMP
rm "$_TMP"
fi
deleted=$(for i in $deleted; do echo $(git config remote.$remote.url | cut -d: -f2)/$i; done)
@ -125,13 +125,13 @@ function scp_and_stage
function reverse_scp()
{
set_remote $1
set_remote "$1"
shift
local _TMP=${0///}
echo $@ > $_TMP &&
rsync -rlDv --files-from=$_TMP "$(git config remote.$remote.url)/" ./ &&
rm $_TMP
echo $@ > "$_TMP" &&
rsync -rlDv --files-from="$_TMP" "$(git config "remote.$remote.url")/" ./ &&
rm "$_TMP"
}
function _info()
@ -151,7 +151,7 @@ function _usage()
"
case $1 in
-v|verbose|--verbose) grep -A100 '^#* OPTIONS #*$' $0;;
-v|verbose|--verbose) grep -A100 '^#* OPTIONS #*$' "$0" ;;
esac
exit
}

View file

@ -1,3 +1,3 @@
#!/usr/bin/env bash
git branch --no-color --merged | grep -v "\*" | grep -v $(git_extra_default_branch) | tr -d ' '
git branch --no-color --merged | grep -v "\*" | grep -v "$(git_extra_default_branch)" | tr -d ' '

View file

@ -1,3 +1,3 @@
#!/usr/bin/env bash
git branch --no-color --no-merged | grep -v "\*" | grep -v $(git_extra_default_branch) | tr -d ' '
git branch --no-color --no-merged | grep -v "\*" | grep -v "$(git_extra_default_branch)" | tr -d ' '

View file

@ -33,7 +33,7 @@ stamp() {
local commit_msg=$( git log -1 --pretty=%B )
local stamp_msg
[[ -n "${MSG}" ]] && stamp_msg="${ID} ${MSG}" || stamp_msg="${ID}"
if ${REPLACE}; then
# remove previous stamps with same ID from the commit message
commit_msg=$(
@ -70,9 +70,9 @@ parse_options() {
;;
esac
done
ID="$1"
MSG="${@:2}"
MSG="${*:2}"
}

View file

@ -269,7 +269,7 @@ if [[ $in_git_repo != 0 ]]; then
if [[ -d ".git" || -f ".git" ]] ; then
if GITOUT=$(git_output); then
## Only output if there is some activities
if [[ ! -z "$GITOUT" ]] ; then
if [[ -n "$GITOUT" ]] ; then
echo "${BOLD}${UNDERLINE}${YELLOW}$(basename "$DIR")${NORMAL}"
echo "$GITOUT"
echo ""
@ -287,7 +287,7 @@ else
fi
if GITOUT=$(git_output); then
if [[ ! -z "$GITOUT" ]] ; then
if [[ -n "$GITOUT" ]] ; then
echo "$GITOUT"
else
if [[ $AUTHOR = '.*' ]] ; then

View file

@ -199,7 +199,7 @@ elif [ -n "$SUMMARY_BY_LINE" ]; then
echo " authors :"
lines "${paths[@]}" | sort | uniq -c | sort -rn | format_authors
elif [ -n "$SUMMARY_ONELINE" ]; then
echo "$project / age: $(repository_age) / last active: $(last_active) / active on $(active_days $commit) days / commits: $(commit_count $commit) / uncommitted: $(uncommitted_changes_count)"
echo "$project / age: $(repository_age) / last active: $(last_active) / active on $(active_days "$commit") days / commits: $(commit_count "$commit") / uncommitted: $(uncommitted_changes_count)"
else
echo
echo " project : $project"

View file

@ -60,7 +60,7 @@ function main()
local remote_branch
if [ -z "${remote}" ]; then
if ! remote_branch="$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null)"; then
if ! remote_branch="$(git rev-parse --abbrev-ref --symbolic-full-name '@{u}' 2>/dev/null)"; then
echo "There is no upstream information of local branch."
exit 1
fi

View file

@ -2,7 +2,7 @@
cstr="commits"
ccnt=$(git rev-list --count HEAD)
if [ $ccnt -eq 1 ]; then cstr="commit"; fi
if [ "$ccnt" -eq 1 ]; then cstr="commit"; fi
parm3=""
function _undo()
@ -11,14 +11,14 @@ function _undo()
undo_cnt=${2:-1}
reset=${3:-""}
if [ $undo_cnt -gt $ccnt ]; then
if [ "$undo_cnt" -gt "$ccnt" ]; then
echo "Only $ccnt $cstr, cannot undo $undo_cnt"
elif [ "$type" = "hard" ] && [ $ccnt -eq $undo_cnt ]; then
elif [ "$type" = "hard" ] && [ "$ccnt" -eq "$undo_cnt" ]; then
echo "Cannot hard undo all commits"
elif [ "$type" = "soft" ] && [ $ccnt -eq 1 ]; then
elif [ "$type" = "soft" ] && [ "$ccnt" -eq 1 ]; then
git update-ref -d HEAD
else
git reset --$type HEAD~$undo_cnt
git reset "--$type" "HEAD~$undo_cnt"
fi
if [ "$reset" != "" ]; then git reset; fi
}
@ -69,4 +69,4 @@ if [[ ! $parm2 =~ ^[1-9][0-9]*$ ]]; then
exit 1
fi
_undo $parm1 $parm2 $parm3
_undo $parm1 $parm2 $parm3