Skip to content

Commit

Permalink
Add support for non-master default branch names (including main)
Browse files Browse the repository at this point in the history
This change is backward-compatible by using `git` at the command line
to determine the default branch name for the current repo.
  • Loading branch information
mhartl committed Apr 6, 2021
1 parent 9a69e2f commit 10b0f3f
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
git-utils (2.1.0)
git-utils (2.2.0)

GEM
remote: https://rubygems.org/
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ See below for more details on the commands defined by `git-utils`. To learn more

* `git amend`: alias for `git commit --amend`
* `git bump`: makes a commit with the message `"Bump version number"`
* `git cleanup`: deletes every branch already merged into current branch (apart from `master`, `staging`, `development`, and any branches listed in `~/.git-cleanup-preserved`). Pass the `-r` option to delete remote merged branches.
* `git merge-into-branch [branch]`: merges current branch into given branch (defaults to `master`)
* `git cleanup`: deletes every branch already merged into current branch (apart from `master`, `main`, `staging`, `development`, and any branches listed in `~/.git-cleanup-preserved`). Pass the `-r` option to delete remote merged branches.
* `git merge-into-branch [branch]`: merges current branch into given branch (defaults to repo's default branch)
* `git minor`: makes a commit with the message `"Make minor changes"`
* `git open`: opens the remote page for the repo (macOS & Linux)
* `git polish`: makes a commit with the message `"Polish"`
* `git pull-request`: pushes the branch and opens the remote page for issuing a new a pull request (macOS-only)
* `git push-branch`: pushes the current branch up to origin
* `git delete-remote-branch <branch>`: deletes the remote branch if it is safe to do so
* `git switch <pattern>`: switches to the first branch matching the given pattern
* `git sync [branch]`: syncs the given branch with the remote branch (defaults to master)
* `git sync-fork`: syncs the `master` branch of a fork with the original upstream `master` (assumes upstream configuration as in “[Configuring a remote for a fork](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/configuring-a-remote-for-a-fork)”)
* `git sync [branch]`: syncs the given branch with the remote branch (defaults to repo's default branch)
* `git sync-fork`: syncs the default branch of a fork with the original upstream default (assumes upstream configuration as in “[Configuring a remote for a fork](https://help.github.com/en/github/collaborating-with-issues-and-pull-requests/configuring-a-remote-for-a-fork)”)
* `git typo`: makes a commit with the message `"Fix typo"`
* `git undo`: undoes the last commit
* `git graph`: displays full repository history in graphical format; alias for `git log --graph --oneline --decorate --all --full-history --author-date-order --no-notes`
Expand All @@ -41,9 +41,9 @@ Some of these commands deserve further explanation.

### git merge-into-branch

`git merge-into-branch [target]` merges the current branch into the target branch (defaults to `master`). On a branch called `add-markdown-support`, `git merge-into-branch` is equivalent to the following:
`git merge-into-branch [target]` merges the current branch into the target branch (defaults to repo's default branch). On a branch called `add-markdown-support` in a repo with default branch `main`, `git merge-into-branch` is equivalent to the following:

$ git checkout master
$ git checkout main
$ git merge --no-ff --log add-markdown-support

Note that this effectively changes the default merge behavior from fast-forward to no-fast-forward, which makes it possible to use `git log` to see which of the commit objects together have implemented a feature on a particular branch. As noted in [A successful Git branching model](http://nvie.com/posts/a-successful-git-branching-model/):
Expand All @@ -66,16 +66,16 @@ These options can be overriden (and thus restored to their defaults) by passing

### git sync

`git sync [branch]` syncs the given local branch with the remote branch (defaults to master). On a branch called `add-markdown-support`, `git sync` is equivalent to the following:
`git sync [branch]` syncs the given local branch with the remote branch (defaults to repo's default branch). On a branch called `add-markdown-support` in a repo with default branch `master`, `git sync` is equivalent to the following:

$ git checkout master
$ git pull
$ git checkout add-markdown-support

The main purpose of `git sync` is to prepare the current branch for merging with `master`:
The main purpose of `git sync` is to prepare the current branch for merging with the default branch:

$ git sync
$ git merge master
$ git merge master # or `main`, etc.

(This is essentially equivalent to

Expand Down
8 changes: 4 additions & 4 deletions bin/git-cleanup
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
require 'optparse'

# Deletes (almost) every branch already merged into current branch.
# Exceptions are `master`, `staging`, and `development`, and the current
# branch, which are preserved. We also support custom configuration via the
# `~/.git-cleanup-preserved` file.
# Exceptions are `master`, `main`, `staging`, and `development`,
# and the current branch, which are preserved.
# We also support custom configuration via the `~/.git-cleanup-preserved` file.

options = {}
OptionParser.new do |opts|
Expand All @@ -15,7 +15,7 @@ OptionParser.new do |opts|
end
end.parse!

preserved = "master|staging|development"
preserved = "master|main|staging|development"
preserved_file = File.join(Dir.home, '.git-cleanup-preserved')
if File.exist?(preserved_file)
additional_preserved = File.read(preserved_file).strip.split("\n")
Expand Down
8 changes: 7 additions & 1 deletion lib/git-utils/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ def current_branch
@current_branch ||= `git rev-parse --abbrev-ref HEAD`.strip
end

# Returns the default branch for the current repository.
def default_branch
@default_branch ||= `git symbolic-ref refs/remotes/origin/HEAD \
| sed 's@^refs/remotes/origin/@@'`.strip
end

# Returns the URL for the remote origin.
def origin_url
@origin_url ||= `git config --get remote.origin.url`.strip
Expand Down Expand Up @@ -95,4 +101,4 @@ def finish?
def deliver?
options.deliver
end
end
end
4 changes: 2 additions & 2 deletions lib/git-utils/merge_branch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def cmd

# Returns the name of the branch to be merged into.
# If there is anything left in the known options after parsing,
# that's the merge branch. Otherwise, it's master.
# that's the merge branch. Otherwise, it's the default branch.
def target_branch
self.known_options.first || 'master'
self.known_options.first || default_branch
end
end
4 changes: 2 additions & 2 deletions lib/git-utils/sync.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ def parser

# Returns a command appropriate for executing at the command line.
def cmd
branch = self.known_options.first || 'master'
branch = self.known_options.first || default_branch
c = ["git checkout #{branch}"]
c << "git pull"
c << "git checkout #{current_branch}"
c.join("\n")
end
end
end
6 changes: 3 additions & 3 deletions lib/git-utils/sync_fork.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SyncFork < Command

def parser
OptionParser.new do |opts|
opts.banner = "Usage: git sync-fork"
opts.banner = "Usage: git sync-fork [default]"
opts.on_tail("-h", "--help", "this usage guide") do
puts opts.to_s; exit 0
end
Expand All @@ -13,9 +13,9 @@ def parser

# Returns a command appropriate for executing at the command line.
def cmd
c = ["git checkout master"]
c = ["git checkout #{default_branch}"]
c << "git fetch upstream"
c << "git merge upstream/master"
c << "git merge upstream/#{default_branch}"
c.join("\n")
end
end
2 changes: 1 addition & 1 deletion lib/git-utils/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Git
module Utils
VERSION = "2.1.0"
VERSION = "2.2.0"
end
end
17 changes: 16 additions & 1 deletion spec/commands/merge_branch_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe MergeBranch do

let(:command) { MergeBranch.new }
before { command.stub(:current_branch).and_return('tau-manifesto') }
before { command.stub(:current_branch).and_return('tau-manifesto') }
subject { command }

its(:cmd) { should match /git merge/ }
Expand All @@ -19,6 +19,21 @@
its(:cmd) { should match /git checkout master/ }
end

describe "default branch" do
let(:command) { MergeBranch.new }

describe "for current real repo" do
subject { command.default_branch }
it { should match 'master' }
end

describe "for repo with different default" do
before { command.stub(:default_branch).and_return('main') }
subject { command.default_branch }
it { should match 'main' }
end
end

describe "with a custom development branch" do
let(:command) { MergeBranch.new(['development']) }
its(:cmd) { should match /git checkout development/ }
Expand Down

0 comments on commit 10b0f3f

Please sign in to comment.