diff --git a/.config/fish/functions/ugc.fish b/.config/fish/functions/ugc.fish new file mode 100644 index 0000000..b39bbf2 --- /dev/null +++ b/.config/fish/functions/ugc.fish @@ -0,0 +1,53 @@ +function ugc --description "Update git fork." + # Declare our arguments + argparse h/help c/current -- $argv + or return + + # Get function name + set current_name $(status current-function) + + set upstream_remote upstream + + # Check if no arguments were provided, or if the first argument is '--help' + if set -ql _flag_help + echo -e "Usage: $current_name" + echo -e " $current_name [-h|--help] - Show this help message" + echo -e " $current_name \t\t - Update master branch from '$upstream_remote' remote" + echo -e " $current_name [-c|--current] - Update current branch from '$upstream_remote' remote" + return 0 + end + + # Check if we are in a git repository + git status >/dev/null + if not test $status -eq 0 + echo "$current_name: not in a git repository" 1>&2 + return 1 + end + + # Check if upstream exists + if not contains upstream (git remote) + echo "$current_name: not '$upstream_remote' remote" 1>&2 + echo "$current_name: use: git remote add $upstream_remote " 1>&2 + return 1 + end + + # Check what is the default branch + set origin_remote origin + set default_branch (git remote show $origin_remote | sed -n '/HEAD/s/.*: //p') + set current_branch (git rev-parse --abbrev-ref HEAD) + + if not set -ql _flag_current + # Change to default branch + git switch $default_branch + end + + # Merge upstream + git fetch $upstream_remote + git pull $upstream_remote $default_branch + git push $origin_remote $default_branch + + if not set -ql _flag_current + # Return to the branch we were before if we changed + git switch $current_branch + end +end diff --git a/Makefile b/Makefile index 09f5795..45c194b 100644 --- a/Makefile +++ b/Makefile @@ -50,6 +50,7 @@ sync-dotconfig: @$(WGET) $(REPO_SRC)/.config/fish/functions/makeaway.fish -O $(HOME)/.config/fish/functions/makeaway.fish @$(WGET) $(REPO_SRC)/.config/fish/functions/ssh-export.fish -O $(HOME)/.config/fish/functions/ssh-export.fish @$(WGET) $(REPO_SRC)/.config/fish/functions/ssh-import.fish -O $(HOME)/.config/fish/functions/ssh-import.fish + @$(WGET) $(REPO_SRC)/.config/fish/functions/ugc.fish -O $(HOME)/.config/fish/functions/ugc.fish @$(WGET) $(REPO_SRC)/.config/fish/conf.d/abbr.fish -O $(HOME)/.config/fish/conf.d/abbr.fish @$(WGET) $(REPO_SRC)/.config/fish/conf.d/alias.fish -O $(HOME)/.config/fish/conf.d/alias.fish