diff options
author | Tyler Davis <tyler@gluecode.net> | 2024-11-29 10:55:17 -0800 |
---|---|---|
committer | Tyler Davis <tyler@gluecode.net> | 2024-11-29 10:55:17 -0800 |
commit | 66b9b34cb79730a2d0cbf6d154807542499a04dd (patch) | |
tree | 7aa1d869d677164762e16f2804c8f279d9a34cc3 | |
parent | 2b0abcf1e5e6a9d59d0e1dd938ee9b273daa7062 (diff) | |
download | dotfiles-66b9b34cb79730a2d0cbf6d154807542499a04dd.tar.gz dotfiles-66b9b34cb79730a2d0cbf6d154807542499a04dd.zip |
git: add fetchgit single invocation opt
-rwxr-xr-x | .local/bin/fetchgit | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/.local/bin/fetchgit b/.local/bin/fetchgit index a8be80f..f33a408 100755 --- a/.local/bin/fetchgit +++ b/.local/bin/fetchgit @@ -78,4 +78,55 @@ if [ "$#" -gt 0 ]; then git pull -q --ff-only --autostash origin $CURRENT_BRANCH done +else + # Current directory invocation + if [ $verbose -gt 0 ]; then + echo $PWD + fi + if [ ! -d .git ]; then # If $PWD/.git doesn't exist + # then are we in a ".git" dir? jump up one dir + if [ ${PWD##*/} = ".git" ]; then + cd .. + else #Finally, are we at least in a work tree? + if [ ! $(git rev-parse --is-inside-work-tree) ]; then + echo "Not in a git directory" + exit + fi + fi + fi + + rems=$(eval "git remote") # list all remotes + len_rems=${#rems} + if [ $len_rems -lt 1 ]; then + # If the list of remotes is zero, we don't do any remote ops + exit 0 + fi + + CURRENT_BRANCH=$(git symbolic-ref --short HEAD) + # There should always be an origin remote if there is at least one remote + DEFAULT_BRANCH=$(git remote show origin | sed -n '/HEAD branch/s/.*: //p') + + git fetch --all --prune --quiet + FETCH_FAILED=$? + + git switch --quiet $DEFAULT_BRANCH + + if [ $FETCH_FAILED -ne 0 ]; then + # We should almost always be able to connect to origin + git pull -q --ff-only --autostash origin $DEFAULT_BRANCH + else + if git config remote.upstream.url >/dev/null; then + git pull -q --ff-only --autostash upstream $DEFAULT_BRANCH + git push -q origin $DEFAULT_BRANCH + elif git config remote.upstream.tydavis >/dev/null; then + git pull -q --ff-only --autostash tydavis $DEFAULT_BRANCH + git push -q origin $DEFAULT_BRANCH + else + git pull -q --ff-only --autostash origin $DEFAULT_BRANCH + fi + fi + + git switch --quiet $CURRENT_BRANCH + # Update current branch if out of date + git pull -q --ff-only --autostash origin $CURRENT_BRANCH fi |