From 73c0864c8be56b2e2c1016c7b515bfdfba6cc2b0 Mon Sep 17 00:00:00 2001 From: Julian Prein Date: Thu, 1 Dec 2022 01:27:28 +0100 Subject: [PATCH] git: Add https-and-ssh to setup origin Sets up origin so that it fetches over https but pushes over ssh. This way unlocking the ssh key is only needed when really necessary. --- .config/git/config | 2 ++ .config/zsh/autoload/git/git-https-and-ssh | 20 ++++++++++++++++++++ .config/zsh/autoload/git/git-ssh-and-https | 1 + 3 files changed, 23 insertions(+) create mode 100755 .config/zsh/autoload/git/git-https-and-ssh create mode 120000 .config/zsh/autoload/git/git-ssh-and-https diff --git a/.config/git/config b/.config/git/config index fa45012..cfa9f28 100644 --- a/.config/git/config +++ b/.config/git/config @@ -19,12 +19,14 @@ root = rev-parse --show-toplevel signoff = rebase --signoff ss = stash + ssh-and-https = https-and-ssh # Implemented as zsh functions or external programs checkout-worktree = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-checkout-worktree\"" commit-last-msg = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-commit-last-msg\"" filter-repo = !git-filter-repo flog = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/glog\"" + https-and-ssh = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-https-and-ssh\"" make-fork = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-make-fork\"" ssync = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-ssync\"" submodule-rm = "!\"${XDG_CONFIG_HOME:-$HOME/.config}/zsh/autoload/git/git-submodule-rm\"" diff --git a/.config/zsh/autoload/git/git-https-and-ssh b/.config/zsh/autoload/git/git-https-and-ssh new file mode 100755 index 0000000..4556af9 --- /dev/null +++ b/.config/zsh/autoload/git/git-https-and-ssh @@ -0,0 +1,20 @@ +#!/bin/sh +#!/usr/bin/zsh + +# Sets up origin so that it fetches over https but pushes over ssh. This way +# unlocking the ssh key is only needed when really necessary. + +GIT="git" + +origin="$($GIT remote -v | grep -Po '(?<=^origin\t).*(?= \(fetch\)$)')" + +if [ "${origin##https://}" = "$origin" ]; then + https_origin="${origin##*@}" + https_origin="https://${https_origin/://}" + $GIT remote set-url origin "$https_origin" + $GIT remote set-url --push origin "$origin" +else + ssh_origin="${origin##https://}" + ssh_origin="git@${ssh_origin/\//:}" + $GIT remote set-url --push origin "$ssh_origin" +fi diff --git a/.config/zsh/autoload/git/git-ssh-and-https b/.config/zsh/autoload/git/git-ssh-and-https new file mode 120000 index 0000000..6da7605 --- /dev/null +++ b/.config/zsh/autoload/git/git-ssh-and-https @@ -0,0 +1 @@ +git-https-and-ssh \ No newline at end of file