From 4a458a3c005c63b6ee8b8fe8283a3ebbfd90086a Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Fri, 18 Jun 2021 17:51:57 +0200 Subject: [PATCH] config/git: refactor a few repo commands into scripts --- .config/git/config | 5 ++-- .config/shell/git | 48 --------------------------------- .local/bin/scripts/git-backup | 6 +++++ .local/bin/scripts/git-get | 6 +++++ .local/bin/scripts/git-origin | 9 +++++++ .local/bin/scripts/git-repo | 7 +++++ .local/bin/scripts/git-upstream | 12 +++++++++ 7 files changed, 43 insertions(+), 50 deletions(-) create mode 100755 .local/bin/scripts/git-backup create mode 100755 .local/bin/scripts/git-get create mode 100755 .local/bin/scripts/git-origin create mode 100755 .local/bin/scripts/git-repo create mode 100755 .local/bin/scripts/git-upstream diff --git a/.config/git/config b/.config/git/config index 228760d..6dc0e65 100644 --- a/.config/git/config +++ b/.config/git/config @@ -97,7 +97,7 @@ lgo = lg HEAD @{push} lo = log --pretty=tformat:'%C(auto)%h%d %s %Cgreen(%cd) %Cblue<%an>%Creset' --date=human --no-merges lp = log -p --date=local - ln = !git --no-pager lo "-$(a=$(git rev-list --count HEAD...@{push} 2>/dev/null); expr $a + 3 \\& ${a:-0} \\> 4 \\| 7)" --color=always --graph HEAD @{u} 2>/dev/null | head -9 || git --no-pager lo -7 + ln = !git --no-pager lo "-$(a=$(git rev-list --count HEAD...@{push} 2>/dev/null); expr $a + 3 \\& ${a:-0} \\> 3 \\| 6)" --color=always --graph HEAD @{u} 2>/dev/null | head -9 || git --no-pager lo -7 my = lo --author [Jj]anek standup = my --since yesterday --all co-authors = !git log | grep -i Co-Authored | awk '!a[$0]++' @@ -133,6 +133,7 @@ rs = reset --keep ru = reset --keep @{upstream} unstage = restore --staged -- + recover = restore --worktree --staged -- format-head = !git stash && git-clang-format HEAD~ && git commit -a --amend --no-edit && git stash pop @@ -151,7 +152,7 @@ ruu = !git fetch upstream && git reset --keep upstream/${1:-$(git curbranch)} && git push --no-verify --force-with-lease luu = !git pull upstream ${1:-(git curbranch)} && git push --no-verify pf = push --force-with-lease - cap = !git commit --all --amend --no-edit && git push --force-with-lease + cap = !git commit --amend --no-edit && git push --force-with-lease [includeIf "gitdir:yadm/repo.git"] path = "~/.config/yadm/gitconfig" diff --git a/.config/shell/git b/.config/shell/git index 692bbfc..9ee01dd 100644 --- a/.config/shell/git +++ b/.config/shell/git @@ -72,13 +72,6 @@ gitinit() { git push } -gitbackup() { - p=$(basename $PWD) - builtin cd .. - git clone --mirror $p - cd $p -} - project() { cd $projects_dir if [ -d $2 ] @@ -87,23 +80,6 @@ project() { fi } -gitremote() { - 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" - echo "git@${1:-github.com}:${3:-$(git config user.name)}/${2:-$(basename $PWD)}.git" ;; - esac -} - -gitclone() { - remote=$(gitremote "$@") - echo $remote - git clone $remote ${@:4} || return $? - cd "$(test $4 && echo $4 || basename ${remote%.git})" -} - gitfork() { builtin cd "$projects_dir/_forks" gitclone github.com "$1" "" "$3" --recurse-submodules || return $? @@ -111,30 +87,6 @@ gitfork() { git remote -v } -# set repo as origin and set all branches upstream to their respective remote branch, if available -gitorigin() { - 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 ) - - 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 -} - -# set repo as upstream or add a new remote -gitupstream() { - if test "$3" - then - local name="$3" - git remote remove "$3" 2>/dev/null - else - git remote get-url upstream 2>/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/${2:-$(echo $origin | cut -d'/' -f2)}" - git remote -v -} # }}} # Rewriting history {{{ diff --git a/.local/bin/scripts/git-backup b/.local/bin/scripts/git-backup new file mode 100755 index 0000000..28338c1 --- /dev/null +++ b/.local/bin/scripts/git-backup @@ -0,0 +1,6 @@ +#!/bin/sh +# Creates a bare clone parallel to the current repository +p="$(basename "$(git rev-parse --show-toplevel)")" +builtin cd .. +git clone --mirror $p +builtin cd $p diff --git a/.local/bin/scripts/git-get b/.local/bin/scripts/git-get new file mode 100755 index 0000000..50e94a7 --- /dev/null +++ b/.local/bin/scripts/git-get @@ -0,0 +1,6 @@ +#!/bin/sh +# Clones from resolving the arguments and switches into the new directory +remote=$(gitremote "$@") +echo $remote +git clone $remote ${@:4} || return $? +cd "$(test $4 && echo $4 || basename ${remote%.git})" diff --git a/.local/bin/scripts/git-origin b/.local/bin/scripts/git-origin new file mode 100755 index 0000000..3692bec --- /dev/null +++ b/.local/bin/scripts/git-origin @@ -0,0 +1,9 @@ +#!/bin/sh +# set repo as origin and set all branches upstream to a corresponding remote branch, if available +git remote remove origin 2>/dev/null +git remote add origin $(git-repo "$@") +git remote -v && git fetch || ( last=$? && echo "git fetch failed, aborting\!" && return $last ) + +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 diff --git a/.local/bin/scripts/git-repo b/.local/bin/scripts/git-repo new file mode 100755 index 0000000..2aebf63 --- /dev/null +++ b/.local/bin/scripts/git-repo @@ -0,0 +1,7 @@ +#!/bin/sh +# Translates different repo notations into ssh +case "$1" in +http*) echo "git@$(echo "$1" | cut -d'/' -f3):$(echo "$1" | cut -d'/' -f4)/$(echo "$1" | cut -d'/' -f5)" ;; +git:*) echo "$1" ;; +*) echo "git@${1:-github.com}:${3:-$(git config user.name)}/${2:-$(basename $PWD)}.git" ;; +esac diff --git a/.local/bin/scripts/git-upstream b/.local/bin/scripts/git-upstream new file mode 100755 index 0000000..b5375b5 --- /dev/null +++ b/.local/bin/scripts/git-upstream @@ -0,0 +1,12 @@ +#!/bin/sh +# set given repository as upstream or add as a new remote +if test "$3" +then + local name="$3" + git remote remove "$3" 2>/dev/null +else + git remote get-url upstream 2>/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/${2:-$(echo $origin | cut -d'/' -f2)}" +git remote -v