Module: Git
- Defined in:
- lib/git/branch/stray.rb,
lib/git/branch/stray/gemspec.rb,
lib/git/branch/stray/version.rb
Defined Under Namespace
Modules: Branch
Class Method Summary collapse
- .branch_name_from(symbolic_ref) ⇒ Object
-
.delete_stray_branches ⇒ Object
rubocop:disable Layout/LineLength.
- .head_refs_for(remote) ⇒ Object
-
.list_stray_branches ⇒ Object
rubocop:enable Layout/LineLength.
- .local_head_refs ⇒ Object
- .merge_ref_for(branch) ⇒ Object
- .remote_for(branch) ⇒ Object
- .stray_branches ⇒ Object
- .upstream_reference_for(branch) ⇒ Object
Class Method Details
.branch_name_from(symbolic_ref) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/git/branch/stray.rb', line 60 def branch_name_from(symbolic_ref) # extract the branch name from a given symbolic ref # local: e.g. "refs/heads/master" # remote: e.g. "refs/remotes/origin/master" symbolic_ref.sub(%r{\Arefs/(heads|remotes/[^/]+)/}, '') end |
.delete_stray_branches ⇒ Object
rubocop:disable Layout/LineLength
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/git/branch/stray.rb', line 85 def delete_stray_branches stray_branches.('Delete stray branch "%s"', inspector: :yellow) do |stray| system("git branch -d #{stray}") next if $CHILD_STATUS.success? Array(stray).('Delete unmerged branch "%s"', inspector: :red) do |unmerged| system("git branch -D #{unmerged}") end end end |
.head_refs_for(remote) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/git/branch/stray.rb', line 34 def head_refs_for(remote) # alternative implementation that requires # access to the remote tracking repository # # git("ls-remote --quiet --refs --heads #{remote}") # .map { |sha_and_ref| sha_and_ref.split(/\s+/).last } # # but we assume that this utility will mainly be # used as part of a "git workflow" that includes # regular pruning of remote tracking references, # which means we don't have to go over the wire! git("for-each-ref --format='%(refname)' refs/remotes/#{remote}") end |
.list_stray_branches ⇒ Object
rubocop:enable Layout/LineLength
97 98 99 100 101 |
# File 'lib/git/branch/stray.rb', line 97 def list_stray_branches stray_branches.each do |stray| system("git --no-pager branch -vv --list #{stray}") end end |
.local_head_refs ⇒ Object
30 31 32 |
# File 'lib/git/branch/stray.rb', line 30 def local_head_refs git("for-each-ref --format='%(refname)' refs/heads") end |
.merge_ref_for(branch) ⇒ Object
56 57 58 |
# File 'lib/git/branch/stray.rb', line 56 def merge_ref_for(branch) git("config --local --get branch.#{branch}.merge").first end |
.remote_for(branch) ⇒ Object
52 53 54 |
# File 'lib/git/branch/stray.rb', line 52 def remote_for(branch) git("config --local --get branch.#{branch}.remote").first end |
.stray_branches ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/git/branch/stray.rb', line 67 def stray_branches to_branch_name = method(:branch_name_from) local_branch_names = local_head_refs.map(&to_branch_name) remote_branch_names = Hash.new { |branch_names, remote| branch_names[remote] = head_refs_for(remote).map(&to_branch_name) } local_branch_names.find_all { |local_branch| (remote = remote_for(local_branch)) && (merge_ref = merge_ref_for(local_branch)) && (upstream_branch = to_branch_name[merge_ref]) && !remote_branch_names[remote].include?(upstream_branch) } end |
.upstream_reference_for(branch) ⇒ Object
48 49 50 |
# File 'lib/git/branch/stray.rb', line 48 def upstream_reference_for(branch) git("rev-parse --verify --abbrev-ref #{branch}@{upstream}").first end |