Skip to content

Commit

Permalink
support main branch by checking for default branch
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Jul 2, 2024
1 parent 1d69626 commit b2e50b7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
27 changes: 23 additions & 4 deletions lib/git/whence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module Git::Whence
module CLI
SQUASH_REGEX = /\(#(\d+)\)$/
DEFAULT_BRANCHES = ["main", "master"]

class << self
def run(argv)
Expand Down Expand Up @@ -66,8 +67,8 @@ def origin
def find_merge(commit)
merge_commit, merge = (
find_merge_simple(commit, "HEAD") ||
find_merge_simple(commit, "master") ||
find_merge_fuzzy(commit, "master")
find_merge_simple(commit, default_branch) ||
find_merge_fuzzy(commit, default_branch)
)

if merge && merge_include_commit?(merge, merge_commit)
Expand All @@ -83,11 +84,29 @@ def merge_include_commit?(merge, commit)
end

def find_merge_fuzzy(commit, branch)
if similar = find_similar(commit, branch)
if (similar = find_similar(commit, branch))
find_merge_simple(similar, branch)
end
end

def default_branch
@default_branch ||= remote_default_branch || local_default_branch
end

def remote_default_branch
remotes = sh("git remote").split("\n")
return nil if remotes.empty?
preferred = (remotes.include?("origin") ? "origin" : remotes.first)
folder = ".git/refs/remotes/#{preferred}"
(Dir["#{folder}/*"].map { |f| f.sub("#{folder}/", "") } & DEFAULT_BRANCHES).sort.first
end

# guess default branch by last changed commonly used default branch or current branch
def local_default_branch
branches = sh("git branch --sort=-committerdate").split("\n").map { |br| br.split(" ").last }
(branches & DEFAULT_BRANCHES).first || sh("git symbolic-ref HEAD").strip.sub("refs/heads/", "")
end

def find_squash_merge(commit)
commit if sh("git show -s --format='%s' #{commit}") =~ SQUASH_REGEX
end
Expand All @@ -98,7 +117,7 @@ def find_similar(commit, branch)
time = time.to_i
same = sh("git log #{branch} --pretty=format:'%H %an %s' --before #{time + month} --after #{time - month}")
found = same.split("\n").map { |x| x.split(" ", 2) }.detect { |_, message| message == search }
found && found.first
found&.first
end

def find_merge_simple(commit, branch)
Expand Down
9 changes: 8 additions & 1 deletion test/git/whence_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@
whence(commit).must_equal "#{merge[0...7]} Merge branch 'foobar'\n"
end

it "finds on non-default branch" do
init_git
sh("git co -b main")
merge, commit = add_merge
whence(commit).must_equal "#{merge[0...7]} Merge branch 'foobar'\n"
end

it "fails with a mainline commit" do
init_git
3.times { |i| sh("echo #{i} > xxx && git commit -am 'xxx#{i}'") }
Expand Down Expand Up @@ -172,7 +179,7 @@ def init_git

# return merge commit and merged commit
def add_merge(options={})
if message = options[:message]
if (message = options[:message])
message = "-m '#{message}'"
end
branch = options[:branch] || "foobar"
Expand Down

0 comments on commit b2e50b7

Please sign in to comment.