From 0f797c6ffeb255b1e8f9ef9eb4c1fd905e311370 Mon Sep 17 00:00:00 2001
From: Oliver Davies <oliver@oliverdavies.uk>
Date: Sun, 19 Jul 2020 21:58:21 +0100
Subject: [PATCH] Prefer "main" to "master"

See
https://github.com/gabebw/dotfiles/commit/1e7fa50da004e10436d1b2145054cd08172afd05.

> This commit introduces `git-master-to-main-wrapper`, which seamlessly
prefers `main` to `master` but works fine with repos that do use
a `master` branch.
---
 bin/git-master-to-main-wrapper | 19 +++++++++++++++++++
 bin/main-or-master-branch      | 10 ++++++++++
 tag-git/gitconfig              |  6 +++---
 tag-zsh/zsh/configs/git.zsh    |  9 ++++++++-
 4 files changed, 40 insertions(+), 4 deletions(-)
 create mode 100755 bin/git-master-to-main-wrapper
 create mode 100755 bin/main-or-master-branch

diff --git a/bin/git-master-to-main-wrapper b/bin/git-master-to-main-wrapper
new file mode 100755
index 0000000..bfef84b
--- /dev/null
+++ b/bin/git-master-to-main-wrapper
@@ -0,0 +1,19 @@
+#!/usr/bin/env zsh
+
+# Usage: instead of
+#
+#     git rebase -i master
+#
+# run this:
+#
+#     git master-to-main-wrapper rebase -i %BRANCH%
+#
+# It will replace the literal string `%BRANCH%` with "main" (preferred) or
+# "master" depending on what the current repository uses.
+
+command=$*
+branchname=$(main-or-master-branch)
+replaced_commands=$(echo $command | sed "s/%BRANCH%/$branchname/g")
+# sh_glob ignores special meaning of parentheses so that fancy logs like this
+# work: `git master-to-main-wrapper log --format='%w(78)%s%n%+b'`
+zsh -c "setopt sh_glob; git ${replaced_commands}"
\ No newline at end of file
diff --git a/bin/main-or-master-branch b/bin/main-or-master-branch
new file mode 100755
index 0000000..ed9ffbd
--- /dev/null
+++ b/bin/main-or-master-branch
@@ -0,0 +1,10 @@
+#!/bin/zsh
+
+# Check if we should use the `main` or `master` branch for this repo.
+# Prefer `main` to `master`.
+
+if git show-ref --quiet origin/main || git rev-parse main &>/dev/null; then
+  echo main
+else
+  echo master
+fi
\ No newline at end of file
diff --git a/tag-git/gitconfig b/tag-git/gitconfig
index eac9cbf..91743bc 100644
--- a/tag-git/gitconfig
+++ b/tag-git/gitconfig
@@ -15,7 +15,7 @@
 	dup = !git checkout develop && git fetch origin && echo && git sl develop..origin/develop && echo && git pull --quiet && git checkout -
 	fixup = commit --fixup
 	issues = !hub browse -- issues
-	mup = !git checkout master && git fetch origin && echo && git sl master..origin/master && echo && git pull --quiet && git checkout -
+	mup = !git master-to-main-wrapper checkout %BRANCH% && git fetch origin && echo && git sl %BRANCH%..origin/%BRANCH% && echo && git pull --quiet && git checkout -
 	nah = !git reset --hard && git clean -fd
 	no-ff = merge --no-ff
 	pl = pull
@@ -26,10 +26,10 @@
 	rdup = !git dup && git rebase develop
 	remotes = remote -v
 	repush = !git pull --rebase && git push
-	rmup = !git mup && git rebase master
+	rmup = !git mup && git master-to-main-wrapper rebase %BRANCH%
 	ri = rebase --interactive
 	rid = !git rebase -i $(git merge-base develop HEAD)
-	rim = !git rebase -i $(git merge-base master HEAD)
+	rim = !git rebase -i $(git master-to-main-wrapper merge-base %BRANCH% HEAD)
 	riu = !git rebase -i $(git rev-parse --abbrev-ref --symbolic-full-name @{u})
 	sl = log --oneline --decorate -20
 	sla = log --oneline --decorate --graph --all -20
diff --git a/tag-zsh/zsh/configs/git.zsh b/tag-zsh/zsh/configs/git.zsh
index 762f05b..e789c91 100644
--- a/tag-zsh/zsh/configs/git.zsh
+++ b/tag-zsh/zsh/configs/git.zsh
@@ -1,7 +1,14 @@
 # No arguments: `git status`
 # With arguments: acts like `git`
 g() {
-  if [[ $# > 0 ]]; then
+  if [[ $1 == init ]];
+  then
+    # Prefer "main" to "master"
+    git "$@"
+    git checkout -b main
+    git branch -d master
+  elif [[ $# > 0 ]];
+  then
     git $@
   else
     git status --short --branch