diff --git a/.config/git/config b/.config/git/config index fcab2cc..be54857 100644 --- a/.config/git/config +++ b/.config/git/config @@ -22,7 +22,7 @@ cmd = nvim -f -c \"Gdiffsplit!\" \"$MERGED\" prompt = false [difftool "nvim"] - cmd = "nvim -d \"$LOCAL\" \"$REMOTE\"" + cmd = "nvim -d \"$LOCAL\" \"$REMOTE\"" # Automatically push to branch with matching name [push] @@ -66,17 +66,18 @@ rev = rev-parse --short head = rev-parse --short HEAD root = rev-parse --show-toplevel + dir = rev-parse --git-dir curbranch = symbolic-ref --short HEAD - + f = fetch fa = fetch --all l = !git pull --rebase --autostash || (>&2 echo "Error - aborting rebase!" && git rebase --abort) - lg = log --pretty=tformat:'%C(auto)%h -%d %s %Cgreen(%cd) %Cblue<%an>%Creset' --date=human --graph - lo = log --pretty=tformat:'%C(auto)%h -%d %s %Cgreen(%cd) %Cblue<%an>%Creset' --date=human --no-merges + lg = log --pretty=tformat:'%C(auto)%h%d %s %Cgreen(%cd) %Cblue<%an>%Creset' --date=human --graph + lo = log --pretty=tformat:'%C(auto)%h%d %s %Cgreen(%cd) %Cblue<%an>%Creset' --date=human --no-merges ln = !git --no-pager lo -5 - my = lo --author erus - standup = lo --since yesterday --author erus --all + my = lo --author [Jj]anek + standup = lo --since yesterday --author [Jj]anek --all co-authors = !git log | grep -i Co-Authored | awk '!a[$0]++' when = git for-each-ref --sort=committerdate --format='%(refname:short) * %(authorname) * %(committerdate:relative)' refs/remotes/ # List all branches with their last updates @@ -98,6 +99,7 @@ fs = !git fetch && git st lu = !git pull upstream $(git curbranch) luu = !git pull upstream $(git curbranch) && git push --no-verify + cap = git commit --all --amend --no-edit && git push --force-with-lease rh = reset HEAD~ rs = reset --keep @@ -111,7 +113,7 @@ sm = submodule update --init --recursive # yadm - add-git = !yadm add $XDG_CONFIG_HOME/git/config $CONFIG_SHELLS/git + add-git = !yadm add $XDG_CONFIG_HOME/git/config $CONFIG_SHELLS/git $HOME/.local/bin/git-* add-vim = !yadm add $XDG_CONFIG_HOME/nvim/init* add-shell = !yadm add $CONFIG_SHELLS add-bin = !yadm add -u $HOME/.local/bin @@ -121,4 +123,3 @@ smudge = git-lfs smudge -- %f process = git-lfs filter-process required = true - diff --git a/.config/shell/git b/.config/shell/git index 406b12a..cf5f3a9 100644 --- a/.config/shell/git +++ b/.config/shell/git @@ -6,6 +6,7 @@ alias magit='nvim -c MagitOnly' # if in home or under XDG_CONFIG_HOME and not within a git directory, replace git by yadm git() { case "$1" in + reset) test "$2" = "--hard" && return 1;; config) ;; clone) ;; *) case "$PWD" in @@ -24,42 +25,7 @@ git() { command git "$@"; } -# Log local and upstream commits side by side - useful to check when the history has been manipulated -glno() { - 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 -} - # BRANCHES -# Cleans up remote branches and removes branches where the remote-tracking branches got removed. -alias gitrmgonebranches='git fetch -p && for branch in `git branch -vv | grep ": gone]" | cut -d" " -f3`; do git branch -D "$branch"; done' -# Recreates the current or given branch with the state from master or another given branch. -gitrebranch() { - branch=${1:-$(git curbranch)} - test "$(git curbranch)" = "$branch" && git checkout ${2:-master} - git branch -D $branch - git checkout -b $branch -} -# Removes the given branch locally and remotely. -# With no argument it switches to the default branch and deletes the current branch. -gitrmbranch() { - branch=${1:-$(git curbranch)} - git push -d $(git rev-parse --abbrev-ref $branch@{push} | sed 's/\// /' || echo origin $branch) - test $1 || git checkout main || git checkout master || git checkout $(cat .git/refs/remotes/origin/HEAD | cut -d'/' -f4) - git branch -D $branch -} - -# 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}") } - # Remove list of tags local & remote gitrmtag() { declare -a refs diff --git a/.local/bin/git-lno b/.local/bin/git-lno new file mode 100755 index 0000000..bc41f23 --- /dev/null +++ b/.local/bin/git-lno @@ -0,0 +1,12 @@ +#!/bin/zsh +# Log local and upstream commits side by side +# Useful when the history has been manipulated +loc="$(git ln --color=always "$@")" +upstream="$(git ln --color=always @{push} "$@")" +a=$(echo $loc | wc -l) +b=$(echo $upstream | wc -l) +halfcols="$(expr $(tput cols) / 2)" +for i in $(seq 1 $(test $a -le $b && echo "$a" || echo "$b")); do + printf "%-${halfcols}s $(test $halfcols -lt 100 && echo '\\n')%s\n" "$(echo $loc | head -n $i | tail -1)" "$(echo $upstream | head -n $i | tail -1)" + # use columns instead? - need to interleave! +done diff --git a/.local/bin/git-rebranch b/.local/bin/git-rebranch new file mode 100755 index 0000000..8ef3527 --- /dev/null +++ b/.local/bin/git-rebranch @@ -0,0 +1,6 @@ +#!/bin/sh +# Recreates the current or given branch with the state from master or another given branch. +branch=${1:-$(git curbranch)} +test "$(git curbranch)" = "$branch" && git checkout ${2:-master} +git branch -D $branch +git checkout -b $branch diff --git a/.local/bin/git-rmbranch b/.local/bin/git-rmbranch new file mode 100755 index 0000000..fe4bbca --- /dev/null +++ b/.local/bin/git-rmbranch @@ -0,0 +1,7 @@ +#!/bin/sh +# Removes the given branch locally and remotely. +# With no argument it switches to the default branch and deletes the current branch. +branch=${1:-$(git curbranch)} +git push -d $(git rev-parse --abbrev-ref $branch@{push} | sed 's/\// /' || echo origin $branch) +test $1 || git checkout main || git checkout master || git checkout $(cat .git/refs/remotes/origin/HEAD | cut -d'/' -f4) +git branch -D $branch diff --git a/.local/bin/git-rmgonebranches b/.local/bin/git-rmgonebranches new file mode 100755 index 0000000..f7a1349 --- /dev/null +++ b/.local/bin/git-rmgonebranches @@ -0,0 +1,6 @@ +#!/bin/sh +# Cleans up remote branches and removes branches where the remote-tracking branches got removed. +git fetch -p && + for branch in `git branch -vv | grep ": gone]" | cut -d" " -f3` + do git branch -D "$branch" + done diff --git a/.local/bin/git-snap b/.local/bin/git-snap new file mode 100755 index 0000000..6efbb59 --- /dev/null +++ b/.local/bin/git-snap @@ -0,0 +1,3 @@ +#!/bin/sh +# Save HEAD commit hash to "snap" file in git dir +echo Snapped $(git rev-parse HEAD | tee "$(git rev-parse --git-dir)/${1:-snap}") diff --git a/.local/bin/git-snap-restore b/.local/bin/git-snap-restore new file mode 100755 index 0000000..099a37c --- /dev/null +++ b/.local/bin/git-snap-restore @@ -0,0 +1,3 @@ +#!/bin/sh +# Hard reset HEAD to commit in "snap"-file +git reset --keep $(cat "$(git rev-parse --git-dir)/${1:-snap}")