config/git: move branching aliases to bins and update logging

- disabled git reset --hard
- improved lno to conditionally use newlines
- get git-dir appropriately
This commit is contained in:
xerus2000 2020-11-14 02:05:51 +01:00
parent 698f2b3be3
commit e2faef48d6
8 changed files with 47 additions and 43 deletions

View File

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

View File

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

12
.local/bin/git-lno Executable file
View File

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

6
.local/bin/git-rebranch Executable file
View File

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

7
.local/bin/git-rmbranch Executable file
View File

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

6
.local/bin/git-rmgonebranches Executable file
View File

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

3
.local/bin/git-snap Executable file
View File

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

3
.local/bin/git-snap-restore Executable file
View File

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