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: Improve Bash variously #1032

Merged
merged 1 commit into from
Feb 21, 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
7 changes: 1 addition & 6 deletions bin/git-browse
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ if [[ $remote == "" ]]; then
exit 1
fi

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

if [[ $? -ne 0 ]]; then
exit $?
fi
remote_url=$(git remote get-url "$remote") || exit $?

if [[ $remote_url = git@* ]]; then
url=$(echo "$remote_url" | sed -E -e 's/:/\//' -e 's/\.git$//' -e 's/.*@(.*)/http:\/\/\1/')
Expand Down
7 changes: 1 addition & 6 deletions bin/git-browse-ci
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ then
exit 1
fi

remote_url=$(git remote get-url "${remote}")

if [[ $? -ne 0 ]]
then
exit $?
fi
remote_url=$(git remote get-url "${remote}") || exit $?

if [[ $remote_url = git@* ]]
then
Expand Down
2 changes: 1 addition & 1 deletion bin/git-brv
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ for b in "${ordered[@]}"; do
"$color_branch_upstream" "-$uwidth" "${upstream[$b]}" "$reset" \
"$color_diff_commit" "-$hwidth" "${hash[$b]}" "$reset" \
"$msg"
done
done
8 changes: 4 additions & 4 deletions bin/git-bulk
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ usage() {
# add another workspace to global git config
function addworkspace {
git config --global bulkworkspaces."$wsname" "$wsdir";
if [ ! -z "$source" ]; then
if [ -n "$source" ]; then
if [ ! -d "$wsdir" ]; then echo 1>&2 "Path of workspace doesn't exist, make it first."; exit 1; fi
regex='http(s)?://|ssh://|(git@)?.*:.*/.*'
if [[ "$source" =~ $regex ]]; then
Expand Down Expand Up @@ -118,7 +118,7 @@ function wsnameToCurrent () {

# helper to check number of arguments.
function allowedargcount () {
if [ $paramcount -ne $1 ] && [ $paramcount -ne $2 ]; then
if [ "$paramcount" -ne "$1" ] && [ "$paramcount" -ne "$2" ]; then
echo 1>&2 "error: wrong number of arguments" && usage;
exit 1;
fi
Expand All @@ -137,11 +137,11 @@ function executBulkOp () {

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

for line in ${allGitFolders[@]}; do
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 leadingpath=${curdir#${actual}}
local leadingpath=${curdir#"${actual}"}
guardedExecution "$@"
eval cd "\"$rwsdir\"" # back to origin location of last find command
done
Expand Down
22 changes: 10 additions & 12 deletions bin/git-changelog
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ commitList() {
# fetch our tags
local _ref _date _tag _tab='%x09'
local _tag_regex='tag: *'
while IFS=$'\t' read _ref _date _tag; do
while IFS=$'\t' read -r _ref _date _tag; do
[[ -z "${_tag}" ]] && continue
# strip out tags form ()
# git v2.2.0+ supports '%D', like '%d' without the " (", ")" wrapping. One day we should use it instead.
Expand Down Expand Up @@ -376,7 +376,7 @@ _exit() {
# so we need to calculate the total column number(COL_NUM) of header first.
# Why don't we just use the last column?
# Because the body of CMD column may contain space and be treated as multiple fields.
pid_list=( $(ps -f |
pid_list=( $(ps -f |
awk -v ppid=$$ 'NR == 1 {
COL_NUM = NF
}
Expand All @@ -391,7 +391,7 @@ _exit() {
local _pid
for _pid in "${pid_list[@]}"; do
echo "killing: ${_pid}"
kill -TERM ${_pid}
kill -TERM "${_pid}"
done

wait; stty sane; exit 1
Expand Down Expand Up @@ -474,10 +474,10 @@ main() {
esac
shift
done

# The default log format unless already set
[[ -z "$CUR_GIT_LOG_FORMAT" ]] && CUR_GIT_LOG_FORMAT="$GIT_LOG_FORMAT"

local _tag="$(_valueForKeyFakeAssocArray "start_tag" "${option[*]}")"
local start_commit="$(_valueForKeyFakeAssocArray "start_commit" "${option[*]}")"

Expand All @@ -487,7 +487,6 @@ main() {
return 1
fi

start_commit="$start_commit"
start_tag="$(git describe --tags --contains "$start_commit" 2>/dev/null || echo 'null')"
if [[ -z "$start_tag" ]]; then
_error "Could find the associative tag for the start-commit!"
Expand Down Expand Up @@ -569,12 +568,11 @@ main() {
else
cp -f "$tmpfile" "$changelog"
rm -f "$tmpfile"
# Use `eval` to handle GIT_EDITOR which contains space and options,
# like ""C:\Program Files (x86)\Notepad++\notepad++.exe" -multiInst ".
# Thanks @JanSchulz to inspire me this solution
[[ -n "$GIT_EDITOR" ]] && eval $GIT_EDITOR "$changelog"
if [[ $? -ne 0 ]]; then
_exit

if [[ -n "$GIT_EDITOR" ]]; then
$GIT_EDITOR "$changelog" || _exit
else
less "$changelog" || _exit
fi
fi

Expand Down
2 changes: 1 addition & 1 deletion bin/git-delete-merged-branches
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

branches=$(git branch --no-color --merged | grep -vE "^(\*|\+)" | grep -v $(git_extra_default_branch) | grep -v svn)
branches=$(git branch --no-color --merged | grep -vE "^(\*|\+)" | grep -v "$(git_extra_default_branch)" | grep -v svn)
if [ -n "$branches" ]
then
echo "$branches" | xargs -n 1 git branch -d
Expand Down
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
2 changes: 1 addition & 1 deletion bin/git-delete-tag
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

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

# Detect the default remote exists or not
default_remote=$(git config git-extras.default-remote)
Expand Down
31 changes: 16 additions & 15 deletions bin/git-effort
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ active_days() {

color_for() {
if [ "$to_tty" = true ]; then
if [ $1 -gt 200 ]; then color="$(tputq setaf 1)$(tputq bold)"
elif [ $1 -gt 150 ]; then color="$(tputq setaf 1)" # red
elif [ $1 -gt 125 ]; then color="$(tputq setaf 2)$(tputq bold)"
elif [ $1 -gt 100 ]; then color="$(tputq setaf 2)" # green
elif [ $1 -gt 75 ]; then color="$(tputq setaf 5)$(tputq bold)"
elif [ $1 -gt 50 ]; then color="$(tputq setaf 5)" # purplish
elif [ $1 -gt 25 ]; then color="$(tputq setaf 3)$(tputq bold)"
elif [ $1 -gt 10 ]; then color="$(tputq setaf 3)" # yellow
if [ "$1" -gt 200 ]; then color="$(tputq setaf 1)$(tputq bold)"
elif [ "$1" -gt 150 ]; then color="$(tputq setaf 1)" # red
elif [ "$1" -gt 125 ]; then color="$(tputq setaf 2)$(tputq bold)"
elif [ "$1" -gt 100 ]; then color="$(tputq setaf 2)" # green
elif [ "$1" -gt 75 ]; then color="$(tputq setaf 5)$(tputq bold)"
elif [ "$1" -gt 50 ]; then color="$(tputq setaf 5)" # purplish
elif [ "$1" -gt 25 ]; then color="$(tputq setaf 3)$(tputq bold)"
elif [ "$1" -gt 10 ]; then color="$(tputq setaf 3)" # yellow
else color="$(tputq sgr0)" # default color
fi
else
Expand All @@ -84,20 +84,21 @@ effort() {
local color reset_color commits len dot f_dot i msg active
reset_color=""
test "$to_tty" = true && reset_color="$(tputq sgr0)"
commit_dates=$(dates "$path")
[ $? -gt 0 ] && exit 255
if ! commit_dates=$(dates "$path"); then
exit 255
fi

# Ensure it's not just an empty line
if [ -z "$(head -c 1 <<<$(echo $commit_dates))" ]
if [ -z "$(head -c 1 <<<"$commit_dates")" ]
then
exit 0
fi

commits=$(wc -l <<<"$(echo "$commit_dates")")
commits=$(wc -l <<<"$commit_dates")
color='90'

# ignore <= --above
test $commits -le $above && exit 0
test "$commits" -le "$above" && exit 0

# commits
color_for $(( commits - above ))
Expand All @@ -109,12 +110,12 @@ effort() {
i=$((i+1))
done

msg=$(printf " ${color}%s %-10d" "$f_dot" $commits)
msg=$(printf " ${color}%s %-10d" "$f_dot" "$commits")

# active days
active=$(active_days "$commit_dates")
color_for $(( active - above ))
msg="$msg $(printf "${color} %d${reset_color}\n" $active)"
msg="$msg $(printf "${color} %d${reset_color}\n" "$active")"
echo "$msg"
}

Expand Down
4 changes: 2 additions & 2 deletions bin/git-feature
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ do
shift # shift -a|-alias
branch_prefix=$1
else
argv+=($1) # treat tail '-a' as <name>
argv+=("$1") # treat tail '-a' as <name>
fi
;;
-r|--remote )
Expand All @@ -37,7 +37,7 @@ do
shift
;;
* )
argv+=($1)
argv+=("$1")
;;
esac
shift
Expand Down
12 changes: 6 additions & 6 deletions bin/git-force-clone
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ main() {

# Delete all remotes
for remote in $(git remote); do
git remote rm ${remote}
git remote rm "${remote}"
done

# Add origin
git remote add origin ${remote_url}
git remote add origin "${remote_url}"
git fetch origin

# Set default branch
if [ -z "${branch:-}" ]; then
branch=$(LC_ALL=C git remote show origin | grep -oP '(?<=HEAD branch: )[^ ]+$')
git remote set-head origin ${branch}
git remote set-head origin "${branch}"
else
git remote set-head origin -a
fi
Expand All @@ -87,8 +87,8 @@ main() {
git reset --hard HEAD

# Get on the desired branch
git checkout ${branch}
git reset --hard origin/${branch}
git checkout "${branch}"
git reset --hard "origin/${branch}"

# Delete all other branches
branches=$(git branch | grep -v \* | xargs)
Expand All @@ -97,7 +97,7 @@ main() {
fi
)
elif [ -n "${branch:-}" ]; then
git clone -b ${branch} "${remote_url}" "${destination_path}"
git clone -b "${branch}" "${remote_url}" "${destination_path}"
else
git clone "${remote_url}" "${destination_path}"
fi
Expand Down
7 changes: 4 additions & 3 deletions bin/git-fork
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ fi
[[ -z "$project" || -z "$owner" ]] && abort "github repo needs to be specified as an argument"

# create fork
curl -qsf \
if ! curl -qsf \
-X POST \
-u "$user:$github_personal_access_token" \
-H "X-GitHub-OTP: $MFA_CODE" \
"https://api.github.com/repos/$owner/$project/forks"

[ $? = 0 ] || abort "fork failed"
then
abort "fork failed"
fi

echo "Add GitHub remote branch via SSH (you will be prompted to verify the server's credentials)? (y/n)"
read -r use_ssh
Expand Down
11 changes: 5 additions & 6 deletions bin/git-ignore-io
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,11 @@ search() {
}

print_last_modified_time() {
gi_list_date=$(stat -c "%y" "$default_path" 2> /dev/null)
if [ "$?" -ne 0 ]; then
gi_list_date=$(stat -f "%t%Sm" "$default_path" 2> /dev/null)
if [ "$?" -ne 0 ]; then
gi_list_date=$(date -r "$default_path" +%s 2> /dev/null)
[ "$?" -ne 0 ] && gi_list_date=0
if ! gi_list_date=$(stat -c "%y" "$default_path" 2> /dev/null); then
if ! gi_list_date=$(stat -f "%t%Sm" "$default_path" 2> /dev/null); then
if ! gi_list_date=$(date -r "$default_path" +%s 2> /dev/null); then
gi_list_date=0
fi
fi
fi
echo "Last update time: $gi_list_date"
Expand Down
12 changes: 6 additions & 6 deletions bin/git-pr
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ pull() {
ref="refs/pull/$id/merge"
fi

git fetch -fu $remote $ref:$branch && \
git checkout $branch && \
git config --local --replace branch.$branch.merge $ref && \
git config --local --replace branch.$branch.remote $remote;
git fetch -fu "$remote" "$ref:$branch" && \
git checkout "$branch" && \
git config --local --replace "branch.$branch.merge" "$ref" && \
git config --local --replace "branch.$branch.remote" "$remote"
}

pull_pr_if_matched() {
Expand Down Expand Up @@ -54,8 +54,8 @@ done
test -z "$1" && echo "pr number required." 1>&2 && exit 1

if test "$1" = "clean"; then
git for-each-ref refs/heads/pr/* --format='%(refname)' | while read ref; do
git branch -D ${ref#refs/heads/}
git for-each-ref refs/heads/pr/* --format='%(refname)' | while read -r ref; do
git branch -D "${ref#refs/heads/}"
done

elif [[ "$1" =~ ^[0-9]+$ ]]; then
Expand Down
2 changes: 1 addition & 1 deletion bin/git-psykorebase
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ fi

if [[ "$CONTINUE" == "yes" ]]; then
TARGET_BRANCH=$(current_branch)
set $(echo "$TARGET_BRANCH" | sed -e "s/-rebased-on-top-of-/\n/g")
set "$(echo "$TARGET_BRANCH" | sed -e "s/-rebased-on-top-of-/\n/g")"
if [[ $# != 2 ]]; then
echo "Couldn't continue rebasing on ${TARGET_BRANCH}"
exit 30 # Impossible to detect PRIMARY_BRANCH AND SECONDARY_BRANCH
Expand Down
Loading