2020-06-03 10:26:15 +00:00
# Tools
2020-10-07 19:46:34 +00:00
alias g="git"
2020-09-04 10:15:05 +00:00
alias bfg="java -jar $HOME/daten/applications/bfg-1.13.0.jar"
2020-06-03 10:26:15 +00:00
alias magit='nvim -c MagitOnly'
2020-03-08 19:18:49 +00:00
2020-06-26 21:25:53 +00:00
# if in home or under XDG_CONFIG_HOME and not within a git directory, replace git by yadm
2020-06-08 09:42:34 +00:00
git() {
2020-10-08 09:11:51 +00:00
case "$1" in
config) ;;
clone) ;;
*) case "$PWD" in
$HOME|$XDG_CONFIG_HOME|$LAST_YADM)
yadm "$@"
return
;;
$XDG_CONFIG_HOME*)
if ! command git rev-parse --show-toplevel &>/dev/null; then
export LAST_YADM="$PWD"
yadm "$@"
return
fi;;
esac;;
esac
command git "$@";
2020-06-08 09:42:34 +00:00
}
2020-11-13 21:40:28 +00:00
# Log local and upstream commits side by side - useful to check when the history has been manipulated
2020-03-08 19:18:49 +00:00
glno() {
2020-10-08 09:11:51 +00:00
loc="$(git l --color=always "$@")"
upstream="$(git l --color=always $(git upstream) "$@")"
a=$(echo $loc | wc -l)
b=$(echo $upstream | wc -l)
halfcols="$(($(tput cols) / 2))"
for i in `seq 1 $([ $a -le $b ] && echo "$a" || echo "$b")`; do
printf "%-${halfcols}s %s\n" "$(echo $loc | head -n $i | tail -1)" "$(echo $upstream | head -n $i | tail -1)"
done
2020-03-08 19:18:49 +00:00
}
2020-11-13 11:49:24 +00:00
# BRANCHES
# Cleans up remote branches and removes branches where the remote-tracking branches got removed.
2020-06-03 10:26:15 +00:00
alias gitrmgonebranches='git fetch -p && for branch in `git branch -vv | grep ": gone]" | cut -d" " -f3`; do git branch -D "$branch"; done'
2020-11-13 11:49:24 +00:00
# Recreates the current or given branch with the state from master or another given branch.
2020-03-08 19:18:49 +00:00
gitrebranch() {
2020-10-08 09:11:51 +00:00
branch=${1:-$(git curbranch)}
test "$(git curbranch)" = "$branch" && git checkout ${2:-master}
git branch -D $branch
git checkout -b $branch
2020-03-08 19:18:49 +00:00
}
2020-11-13 11:49:24 +00:00
# Removes the given branch locally and remotely.
# With no argument it switches to the default branch and deletes the current branch.
2020-03-08 19:18:49 +00:00
gitrmbranch() {
2020-10-08 09:11:51 +00:00
branch=${1:-$(git curbranch)}
2020-11-13 11:49:24 +00:00
git push -d $(git rev-parse --abbrev-ref $branch@{push} | sed 's/\// /' || echo origin $branch)
2020-11-12 08:56:29 +00:00
test $1 || git checkout main || git checkout master || git checkout $(cat .git/refs/remotes/origin/HEAD | cut -d'/' -f4)
2020-10-08 09:11:51 +00:00
git branch -D $branch
2020-03-08 19:18:49 +00:00
}
2020-06-03 10:26:15 +00:00
# Save HEAD commit hash to "snap" file in git dir
gitsnap() { echo Snapped $(git rev-parse HEAD | tee "$(git dir)/${1:-snap}") }
# Hard reset HEAD to commit in "snap"-file
gitsnaprestore() { git reset --hard $(cat "$(git dir)/${1:-snap}") }
2020-03-08 19:18:49 +00:00
2020-06-03 10:26:15 +00:00
# Remove list of tags local & remote
2020-03-08 19:18:49 +00:00
gitrmtag() {
2020-10-08 09:11:51 +00:00
declare -a refs
local index=1
for tag in $@; do refs[index++]=":refs/tags/$tag"; done
git push origin "${refs[@]}" && git tag -d "$@"
2020-03-08 19:18:49 +00:00
}
2020-06-03 10:26:15 +00:00
# Rename a tag
2020-03-08 19:18:49 +00:00
gitretag() {
2020-10-08 09:11:51 +00:00
git push origin refs/tags/${1}:refs/tags/${2} :refs/tags/$1 && git tag -d $1
2020-03-08 19:18:49 +00:00
}
2020-06-03 10:26:15 +00:00
# Repo management {{{
2020-03-08 19:18:49 +00:00
gitinit() {
2020-11-12 08:56:29 +00:00
git init -b main
2020-10-08 09:11:51 +00:00
git add .
git commit -m "First strike"
gitorigin "$@"
git push
2020-03-08 19:18:49 +00:00
}
gitbackup() {
2020-10-08 09:11:51 +00:00
p=$(basename $PWD)
builtin cd ..
git clone --mirror $p
cd $p
2020-03-08 19:18:49 +00:00
}
project() {
2020-10-08 09:11:51 +00:00
cd $projects_dir
if [ -d $2 ]
then cd "$2" && gitorigin "$@"
else gitclone "$@"
fi
2020-03-08 19:18:49 +00:00
}
2020-06-03 10:26:15 +00:00
gitremote() {
2020-10-08 09:11:51 +00:00
case "$1" in
http*) echo "git@$(echo "$1" | cut -d'/' -f3):$(echo "$1" | cut -d'/' -f4)/$(echo "$1" | cut -d'/' -f5)" ;;
git:*) echo "$1" ;;
*) test "$3" = "cau" && 3="CAU-Kiel-Tech-Inf"
test "$3" = "btl" && 3="betweenthelinesev"
2020-10-14 11:36:14 +00:00
echo "git@${1:-github.com}:${3:-xerus2000}/${2:-$(basename $PWD)}.git" ;;
2020-10-08 09:11:51 +00:00
esac
2020-06-03 10:26:15 +00:00
}
gitclone() {
2020-10-08 09:11:51 +00:00
remote=$(gitremote "$@")
echo $remote
git clone $remote ${@:4}
cd "$(test $4 && echo $4 || basename ${remote/.git})"
2020-03-08 19:18:49 +00:00
}
gitfork() {
2020-10-08 09:11:51 +00:00
builtin cd "$projects_dir/_forks"
gitclone hub "$1" "" "$3" --recurse-submodules
2020-10-13 09:16:07 +00:00
test "$2" && git remote add upstream "$(gitremote github "$1" "$2")"
2020-10-08 09:11:51 +00:00
git remote -v
2020-03-08 19:18:49 +00:00
}
2020-06-03 10:26:15 +00:00
# set repo as origin and set all branches upstream to their respective remote branch, if available
2020-03-08 19:18:49 +00:00
gitorigin() {
2020-10-08 09:11:51 +00:00
git remote remove origin 2>/dev/null
git remote add origin $(gitremote "$@")
git remote -v && git fetch || ( last=$? && echo "git fetch failed, aborting\!" && return $last )
2020-03-08 19:18:49 +00:00
2020-10-08 09:11:51 +00:00
git branch | sed 's/ //g' | sed 's/*//' | while read branch
do test $(git branch -a | grep origin/$branch | wc -l) -gt 0 && git branch -u origin/$branch $branch
done
2020-03-08 19:18:49 +00:00
}
2020-06-26 21:25:53 +00:00
# set repo as upstream or add a new remote
2020-03-08 19:18:49 +00:00
gitupstream() {
2020-10-08 09:11:51 +00:00
if test "$2"
then local name="$2"; git remote remove "$2" >/dev/null
else git remote get-url upstream &>/dev/null && local name="$1" || local name="upstream"
fi
local origin="$(git remote get-url origin)"
git remote add -f $name "$(echo $origin | cut -d':' -f1):$1/$(echo $origin | cut -d'/' -f2)"
git remote -v
2020-03-08 19:18:49 +00:00
}
2020-06-03 10:26:15 +00:00
# }}}
2020-03-08 19:18:49 +00:00
2020-06-03 10:26:15 +00:00
# Rewriting history {{{
2020-03-08 19:18:49 +00:00
# gets the AuthorDate of a given committish
git-authordate() {
2020-10-08 09:11:51 +00:00
local date=$(git log --pretty=fuller --date=raw -1 $1 | grep AuthorDate)
echo ${date##*: }
2020-03-08 19:18:49 +00:00
}
# executes a git command (usually commit) with the date of a given committish
git-withdate() {
2020-10-08 09:11:51 +00:00
date=$(git-authordate $1)
GIT_AUTHOR_DATE="$date" GIT_COMMITTER_DATE="$date" git "${@:2}"
2020-03-08 19:18:49 +00:00
}
# takes all changes in the current working tree and amends them to the given commit
gitedit() {
2020-10-08 09:11:51 +00:00
git stash
gitcommits -q $1
git reset --hard $1
git stash pop -q
git-withdate $1 commit --all --amend "${@:2}"
gitcommits
2020-03-08 19:18:49 +00:00
}
# takes two committishs and squashes them with all commits between them into a single commit
# this will rewrite the full history from then on, but should not create any conflicts
gitsquash() {
2020-10-08 09:11:51 +00:00
local -a options
while [ $# -gt 0 ]; do
case $1 in
-i) ignore=true; shift 1;;
-f|--force) force=true; shift 1;;
-*) options+=($1); exit 1;;
*) break;;
esac
done
((#!=2)) && echo "Usage: [options] <startcommit> <endcommit>" && return 1
[[ -n $(git status -s) ]] && [ ! $force ] && echo -e "Tree is dirty, commit or stash your changes first!\nIf you want to execute the command regardless, run again with --force" && return 1
1=$(git rev-parse $1)
2=$(git rev-parse $2)
[ $(git rev-list $1 --count) -lt $(git rev-list $2 --count) ] && t=$1 && 1=$2 && 2=$t
gitcommits -q $1
git reset --hard $1
if [ $(git rev-list $2 --count) = 1 ]; then
git update-ref -d HEAD
git add .
git-withdate $1 commit -c $1
else
git reset -q $2
git add .
git commit --amend
fi
gitcommits
2020-03-08 19:18:49 +00:00
}
# given a committish, this command saves a list of commits between the HEAD and the given committish into the .git directory
# when ran without parameters it applies the saved list of commits onto the current HEAD
gitcommits() {
2020-10-08 09:11:51 +00:00
verbosity=1
while [[ $# -gt 0 ]]; do
case $1 in
-v) verbosity=2; shift 1;;
-q|--quiet) verbosity=0; shift 1;;
--theirs) params=(-X theirs); shift 1;;
*) break;;
esac
done
stashed="$(git rev-parse --show-toplevel)/.git/stashed-commits"
if [ $1 ]; then
if [ $verbosity -eq 0 ]
then git rev-list --reverse HEAD...$1 >$stashed
else git rev-list --reverse HEAD...$1 | tee $stashed
fi
else
local aborted
for commit in $(cat $stashed); do
[ $aborted ] && rest+=($commit) && continue
[ $verbosity -gt 0 ] && git --no-pager log --oneline -1 $commit
git-withdate $commit cherry-pick $commit ${params:0} >/dev/null
last=$?
[ $last -gt 0 ] && aborted=true && typeset -a rest && continue
[ $verbosity -gt 0 ] && echo -e "\e[1A$(git log --color=always --pretty=format:"%C(yellow)$(git rev-parse --short HEAD^^)%C(bold) -> %Creset%C(yellow)%h%Creset %s" -1)"
[ $verbosity -gt 1 ] && git status -s
done
echo $rest >$stashed
[ $aborted ] && echo "A problem was encountered. Fix it and run 'gitcommits' again to apply the remaining ${#rest} commits."
fi
2020-03-08 19:18:49 +00:00
}
2020-06-03 10:26:15 +00:00
# }}}
# Testing
gittestcommit() { touch file$((++i)) && git add . && git commit -m "Create file$i"; }