lib/git-utils/command.rb in git-utils-2.1.0 vs lib/git-utils/command.rb in git-utils-2.2.0
- old
+ new
@@ -24,10 +24,31 @@
# Returns the current Git branch.
def current_branch
@current_branch ||= `git rev-parse --abbrev-ref HEAD`.strip
end
+ # Returns the default branch for the current repository.
+ # Command retrieved from
+ # https://stackoverflow.com/questions/28666357/git-how-to-get-default-branch
+ def default_branch
+ branch_name = `git symbolic-ref --short refs/remotes/origin/HEAD \
+ | sed 's@^origin/@@'`.strip
+ if branch_name.empty?
+ $stderr.puts "Repository configuration error"
+ $stderr.puts "Missing reference to refs/remotes/origin/HEAD"
+ $stderr.puts "Run"
+ $stderr.puts
+ $stderr.puts " git remote set-head origin <default branch>"
+ $stderr.puts
+ $stderr.puts "where <default branch> is the default branch name"
+ $stderr.puts "(typically `main`, `master`, or `trunk`)"
+ $stderr.puts "and then rerun the command"
+ exit(1)
+ end
+ @default_branch ||= branch_name
+ end
+
# Returns the URL for the remote origin.
def origin_url
@origin_url ||= `git config --get remote.origin.url`.strip
end
@@ -93,6 +114,6 @@
end
def deliver?
options.deliver
end
-end
\ No newline at end of file
+end