Skip to content

Commit

Permalink
[git_extras] add only needed scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
dev99problems committed Dec 11, 2022
1 parent 7510e13 commit d22a6c4
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 0 deletions.
17 changes: 17 additions & 0 deletions git_extras/git_merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
# https://github.com/tj/git-extras/blob/master/bin/git-show-merged-branches

git_extra_default_branch() {
local extras_default_branch init_default_branch
extras_default_branch=$(git config --get git-extras.default-branch)
init_default_branch=$(git config --get init.defaultBranch)
if [ -n "$extras_default_branch" ]; then
echo "$extras_default_branch"
elif [ -n "$init_default_branch" ]; then
echo "$init_default_branch"
else
echo "main"
fi
}

git branch --merged | grep -v "\*" | grep -v $(git_extra_default_branch) | tr -d ' '
243 changes: 243 additions & 0 deletions git_extras/git_summary
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
#!/usr/bin/env bash
#https://github.com/tj/git-extras/blob/master/bin/git-summary

git_root() {
git rev-parse --show-toplevel
}

# get the relative path of current path according to root of repo
git_root_relative() {
rel=$(git rev-parse --show-prefix)
if [ -z "$rel" ]; then
# git rev-parse --show-prefix will output empty string when we are in the root dir
echo "."
else
echo "$rel"
fi
}

git_root_cmd() {
if test $# -eq 0; then
git_root
else
case "$1" in
-r|--relative)
git_root_relative
;;
*)
git_root
;;
esac
fi
}

cd "$(git_root_cmd)" || { echo "Can't cd to top level directory";exit 1; }

SUMMARY_BY_LINE=
DEDUP_BY_EMAIL=
MERGES_ARG=
for arg in "$@"; do
case "$arg" in
--line)
SUMMARY_BY_LINE=1
;;
--dedup-by-email)
DEDUP_BY_EMAIL=1
;;
--no-merges)
MERGES_ARG="--no-merges"
;;
-*)
>&2 echo "unknown argument $arg found"
exit 1
;;
*)
# set the argument back
set -- "$@" "$arg"
;;
esac

shift
done

if [ -n "$DEDUP_BY_EMAIL" ] && [ -n "$SUMMARY_BY_LINE" ]; then
>&2 echo "--dedup-by-email used with --line is not supported"
exit 1
fi

if [ -n "$MERGES_ARG" ] && [ -n "$SUMMARY_BY_LINE" ]; then
>&2 echo "--no-merges used with --line is not supported"
exit 1
fi


if [ -n "$SUMMARY_BY_LINE" ]; then
paths=( "$@" )
else
commit="HEAD"
[ $# -ne 0 ] && commit=$*
fi
project=${PWD##*/}

#
# get date for the given <commit>
#

date() {
# the $1 can be empty
# shellcheck disable=SC2086
git log $MERGES_ARG --pretty='format: %ai' $1 | cut -d ' ' -f 2
}

#
# get active days for the given <commit>
#

active_days() {
# shellcheck disable=SC2086
date $1 | sort -r | uniq | awk '
{ sum += 1 }
END { print sum }
'
}

#
# get the commit total
#

commit_count() {
# shellcheck disable=SC2086
git log $MERGES_ARG --oneline $commit | wc -l | tr -d ' '
}

#
# total file count
#

file_count() {
git ls-files | wc -l | tr -d ' '
}

#
# remove duplicate authors who belong to the same email address
#

dedup_by_email() {
# in:
# 27 luo zexuan <LuoZexuan@xxx.com>
# 7 罗泽轩 <luozexuan@xxx.com>
# out:
# 34 luo zexuan
LC_ALL=C awk '
{
sum += $1
last_field = tolower($NF)
if (last_field in emails) {
emails[last_field] += $1
} else {
email = last_field
emails[email] = $1
# set commits/email to empty
$1=$NF=""
sub(/^[[:space:]]+/, "", $0)
sub(/[[:space:]]+$/, "", $0)
name = $0
if (name in names) {
# when the same name is associated with existed email,
# merge the previous email into the later one.
emails[email] += emails[names[name]]
emails[names[name]] = 0
}
names[name] = email
}
}
END {
for (name in names) {
email = names[name]
printf "%6d\t%s\n", emails[email], name
}
}' | sort -rn -k 1
}

#
# list authors
#

format_authors() {
# a rare unicode character is used as separator to avoid conflicting with
# author name. However, Linux column utility will escape tab if separator
# specified, so we do unesaping after it.
LC_ALL=C awk '
{ args[NR] = $0; sum += $0 }
END {
for (i = 1; i <= NR; ++i) {
printf "%s♪%2.1f%%\n", args[i], 100 * args[i] / sum
}
}
' | column -t -s♪ | sed "s/\\\x09/\t/g"
}

#
# fetch repository age from oldest commit
#

repository_age() {
git log --reverse --pretty=oneline --format="%ar" | head -n 1 | LC_ALL=C sed 's/ago//'
}

#
# list the last modified author for each line
#
single_file() {
while read -r data
do
if [[ $(file "$data") = *text* ]]; then
git blame --line-porcelain "$data" 2>/dev/null | grep "^author\ " | LC_ALL=C sed -n 's/^author //p';
fi
done
}

#
# list the author for all file
#
lines() {
git ls-files -- "$@" | single_file
}

#
# get the number of the lines
#
line_count() {
lines "$@" | wc -l
}

# summary

echo
echo " project : $project"

if [ -n "$SUMMARY_BY_LINE" ]; then
echo " lines : $(line_count "${paths[@]}")"
echo " authors :"
lines "${paths[@]}" | sort | uniq -c | sort -rn | format_authors
else
echo " repo age : $(repository_age)"
# shellcheck disable=SC2086
echo " active : $(active_days $commit) days"
# shellcheck disable=SC2086
echo " commits : $(commit_count $commit)"

# The file count doesn't support passing a git ref so ignore it if a ref is given
if [ "$commit" == "HEAD" ]; then
echo " files : $(file_count)"
fi
echo " authors : "
if [ -n "$DEDUP_BY_EMAIL" ]; then
# the $commit can be empty
# shellcheck disable=SC2086
git shortlog $MERGES_ARG -n -s -e $commit | dedup_by_email | format_authors
else
# shellcheck disable=SC2086
git shortlog $MERGES_ARG -n -s $commit | format_authors
fi
fi
17 changes: 17 additions & 0 deletions git_extras/git_unmerged
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env bash
# https://github.com/tj/git-extras/blob/master/bin/git-show-unmerged-branches

git_extra_default_branch() {
local extras_default_branch init_default_branch
extras_default_branch=$(git config --get git-extras.default-branch)
init_default_branch=$(git config --get init.defaultBranch)
if [ -n "$extras_default_branch" ]; then
echo "$extras_default_branch"
elif [ -n "$init_default_branch" ]; then
echo "$init_default_branch"
else
echo "main"
fi
}

git branch --no-merged | grep -v "\*" | grep -v $(git_extra_default_branch) | tr -d ' '
5 changes: 5 additions & 0 deletions zsh/.zshrc
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ alias gdu='git add -N . && git diff'
alias gsta='git add . && git stash'
alias gunsta='git unstash && git reset .'

## git-extras
alias gsummary="/Users/$USER/.git_extras/git_summary"
alias gmerged="/Users/$USER/.git_extras/git_merged"
alias gunmerged="/Users/$USER/.git_extras/git_unmerged"

## gh
alias ghh="gh pr create --fill"
alias ghhd="gh pr create --fill -d"
Expand Down

0 comments on commit d22a6c4

Please sign in to comment.