Class: GitTools::Branches::Cleaner

Inherits:
Object
  • Object
show all
Defined in:
lib/git_tools/branches/cleaner.rb

Constant Summary collapse

MASTER_BRANCH =
'master'
DEFAULT_REMOTE =
'origin'
@@age_threshold_for_deleting_remote_branches_in_master =
15 * Time::SECONDS_IN_DAY
@@age_threshold_for_deleting_any_unmerged_branches =
180 * Time::SECONDS_IN_DAY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote = nil, master_branch = nil, protected_branches = nil) ⇒ Cleaner

Returns a new instance of Cleaner.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/git_tools/branches/cleaner.rb', line 55

def initialize(remote = nil, master_branch = nil, protected_branches = nil)
  @remote = remote
  @protected_branches = protected_branches || Branches.default_keep_list
  @master_branch = master_branch || get_remote_head || MASTER_BRANCH

  git_remote_prune # Prune before collecting branch names.

  @branches = branches

  if @master_branch.empty?
    raise "Master branch was not set or determined."
  else
    puts "Master branch is #{@master_branch}" if $VERBOSE
  end

  puts "Merged branch threshold is #{@@age_threshold_for_deleting_remote_branches_in_master/Time::SECONDS_IN_DAY} days." if $VERBOSE
  puts "Unmerged branch threshold is #{@@age_threshold_for_deleting_any_unmerged_branches/Time::SECONDS_IN_DAY} days." if $VERBOSE
end

Instance Attribute Details

#master_branchObject (readonly)

Returns the value of attribute master_branch.



27
28
29
# File 'lib/git_tools/branches/cleaner.rb', line 27

def master_branch
  @master_branch
end

#protected_branchesObject (readonly)

Returns the value of attribute protected_branches.



27
28
29
# File 'lib/git_tools/branches/cleaner.rb', line 27

def protected_branches
  @protected_branches
end

#remoteObject (readonly)

Returns the value of attribute remote.



27
28
29
# File 'lib/git_tools/branches/cleaner.rb', line 27

def remote
  @remote
end

Class Method Details

.merged_thresholdObject



37
38
39
# File 'lib/git_tools/branches/cleaner.rb', line 37

def self.merged_threshold
  @@age_threshold_for_deleting_remote_branches_in_master
end

.merged_threshold_in_days=(days) ⇒ Object



41
42
43
# File 'lib/git_tools/branches/cleaner.rb', line 41

def self.merged_threshold_in_days=(days)
  @@age_threshold_for_deleting_remote_branches_in_master = days * Time::SECONDS_IN_DAY
end

.unmerged_thresholdObject



45
46
47
# File 'lib/git_tools/branches/cleaner.rb', line 45

def self.unmerged_threshold
  @@age_threshold_for_deleting_any_unmerged_branches
end

.unmerged_threshold_in_days=(days) ⇒ Object



49
50
51
# File 'lib/git_tools/branches/cleaner.rb', line 49

def self.unmerged_threshold_in_days=(days)
  @@age_threshold_for_deleting_any_unmerged_branches = days * Time::SECONDS_IN_DAY
end

.with_local(master_branch = nil, protected_branches = nil) ⇒ Object



29
30
31
# File 'lib/git_tools/branches/cleaner.rb', line 29

def self.with_local(master_branch = nil, protected_branches = nil)
  self.new(nil, master_branch, protected_branches)
end

.with_origin(protected_branches = nil) ⇒ Object



33
34
35
# File 'lib/git_tools/branches/cleaner.rb', line 33

def self.with_origin(protected_branches = nil)
  self.new(DEFAULT_REMOTE, nil, protected_branches)
end

Instance Method Details

#local?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/git_tools/branches/cleaner.rb', line 74

def local?
  @remote.nil?
end

#remote?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/git_tools/branches/cleaner.rb', line 78

def remote?
  !local?
end

#run!Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/git_tools/branches/cleaner.rb', line 82

def run!
  puts "Skipping prompts" if $VERBOSE && ActionExecutor.skip_prompted
  (@branches - protected_branches - [master_branch] ).each do |branch|
    branch = Branch.new(branch, remote)
    containing_branches = contained_branches(branch.normalized_name) - [branch.name]

    if protected_branch?(branch.name)
      puts "#{label_for_remote} branch [#{branch}] is on keep list ( #{kbs.join(" , ")} )" if $VERBOSE
    elsif in_master_branch?(containing_branches)
      if delete_branch_merged_to_master_without_confirmation?(branch.age)
        message = "Removing #{label_for_remote.downcase} branch [#{branch}] since it is in #{master_branch}. [#{branch.age.relative}]"
        branch.remove!(message)
      else
        message = "#{label_for_remote} branch [#{branch}] is in #{master_branch} and could be deleted [#{branch.age.relative}]"
        branch.confirm_remove(message, "Delete #{branch}?")
      end
    elsif delete_unmerged_branch?(branch.age)
      branch_list = containing_branches.empty? ? '' : "Branch has been merged into:\n  #{containing_branches.join("\n  ")}"
      message = "#{label_for_remote} branch [#{branch}] is not on #{master_branch}, but old [#{branch.age.relative}]. #{branch_list}"
      branch.confirm_remove(message, "Delete old unmerged branch: #{branch} ?")
    else
      puts "Ignoring unmerged #{label_for_remote.downcase} branch [#{branch}]" if $VERBOSE
    end

    if defined?($signal_handler)
      break if $signal_handler.interrupted?
    end
  end

  git_remote_prune
rescue StandardError => e
  puts e.message
  puts e.backtrace if $DEBUG
end