Class: Stagecoach::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/stagecoach/git.rb

Class Method Summary collapse

Class Method Details

.assign_issue_to_me(issue_number) ⇒ Object



174
175
176
# File 'lib/stagecoach/git.rb', line 174

def assign_issue_to_me(issue_number)
  `ghi assign #{issue_number}`
end

.branch_exist?(branch) ⇒ Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/stagecoach/git.rb', line 166

def branch_exist?(branch)
  branches.find { |e| /#{branch}/ =~ e }
end

.branch_has_commits?(branch) ⇒ Boolean

Returns:

  • (Boolean)


178
179
180
181
182
183
184
185
# File 'lib/stagecoach/git.rb', line 178

def branch_has_commits?(branch)
  log = `git log --branches --not --remotes --simplify-by-decoration --decorate --oneline`
  if log.include? branch
    return true
  else
    return false
  end
end

.branch_merged_to_master?(branch) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/stagecoach/git.rb', line 91

def branch_merged_to_master?(branch)
   list = `git branch --merged`.split("\n").collect(&:strip)
   list << Git.current_branch
   list.include?(branch.strip)
end

.branchesObject



4
5
6
# File 'lib/stagecoach/git.rb', line 4

def branches
  `git branch`.split("\n").collect(&:strip)
end

.change_to_branch(branch) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/stagecoach/git.rb', line 115

def change_to_branch(branch)
  CommandLine.line_break
  puts "Changing to branch '#{branch}'"
  if branch_exist?(branch)
    `git checkout #{branch}`
  else
    print "Branch '#{branch}' does not exist. [C]reate or [Q]uit:  "
    case STDIN.gets.chomp
    when /c/i
      new_branch(branch)
    when /q/i     
      exit
    end
  end
end

.changesObject



75
76
77
# File 'lib/stagecoach/git.rb', line 75

def changes
  `git diff-files --name-status -r --ignore-submodules`
end

.checkout(branch) ⇒ Object



158
159
160
# File 'lib/stagecoach/git.rb', line 158

def checkout(branch) 
  puts `git checkout #{branch}`
end

.correct_branch?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
108
# File 'lib/stagecoach/git.rb', line 98

def correct_branch?
  CommandLine.line_break
  print "You are currently in local branch: #{Git.current_branch.red} \nAre these details correct? ([Y]es or [Q]uit):  "
  case STDIN.gets.chomp
  when /y/i
  when /q/i
    exit
  else
    puts "Please enter Y to continue or Q to quit."
  end
end

.current_branchObject



83
84
85
86
87
88
89
# File 'lib/stagecoach/git.rb', line 83

def current_branch
  branches.each do |b| 
    if b =~ /\*/
      return b[1..-1].strip
    end
  end
end

.delete_branch(branch, list) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/stagecoach/git.rb', line 59

def delete_branch(branch, list)
  puts "Local: " + `git branch -D #{branch}`
  if list.include?(branch)
    puts "Remote: "
    puts `git push origin :#{branch}`
  end
end

.diff(branch1, branch2) ⇒ Object



131
132
133
134
# File 'lib/stagecoach/git.rb', line 131

def diff(branch1, branch2)
  diff = `git diff --name-status #{branch1}..#{branch2}`
  return diff
end

.erase(list) ⇒ Object



53
54
55
56
57
# File 'lib/stagecoach/git.rb', line 53

def erase(list)
  change_to_branch('master')
  branches_on_remote = Git.remote_branches
  list.each {|b| delete_branch(b, branches_on_remote ) }
end

.global_config(header, config) ⇒ Object



67
68
69
# File 'lib/stagecoach/git.rb', line 67

def global_config(header, config)
  `git config --global #{header}.#{config}`
end

.issue(id) ⇒ Object



187
188
189
# File 'lib/stagecoach/git.rb', line 187

def issue(id)
  `ghi list #{id}`
end

.list(local_stagecoach_branches, all_branches_list) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/stagecoach/git.rb', line 8

def list(local_stagecoach_branches, all_branches_list)
  deletable_branches = []
  local_stagecoach_branches.keys.sort.each do |branch_name|
    # branch_attributes = local_stagecoach_branches[branch_name]
    if all_branches_list.include?(branch_name.strip)
      if Git.branch_merged_to_master?(branch_name) 
        puts branch_name  + " *".red
        deletable_branches << branch_name
      else
       puts branch_name
      end
    end 
  end
  CommandLine.line_break
  puts "*".red + " = merged to master, can be deleted by stagecoach -t" if deletable_branches.length > 0
  CommandLine.line_break
  deletable_branches
end

.local_stagecoach_branches(config) ⇒ Object



27
28
29
30
# File 'lib/stagecoach/git.rb', line 27

def local_stagecoach_branches(config)
  local_stagecoach_branches = {}
  config.each { |k,v| local_stagecoach_branches[k] = v unless k =~ /redmine_site|redmine_api_key|redmine_user_id|master|staging/i}
end

.merge(to_branch, from_branch) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/stagecoach/git.rb', line 137

def merge(to_branch, from_branch)
  CommandLine.line_break
  puts "Merging into #{to_branch} (after pulling updates)"
  Git.change_to_branch(to_branch)
  puts `git pull origin #{to_branch}`
  puts `git merge #{from_branch}`
  begin
    raise 'Merge failed' if $?.exitstatus != 0
  rescue 
    puts $!.class.name + ": " + $!.message      # $! refers to the last error object
    puts "Please resolve the merge conflict and deploy again. Exiting..."
  end
end

.new_branch(branch) ⇒ Object



110
111
112
113
# File 'lib/stagecoach/git.rb', line 110

def new_branch(branch)
  CommandLine.line_break
  `git checkout -b #{branch}`
end

.new_issue(title, description) ⇒ Object



170
171
172
# File 'lib/stagecoach/git.rb', line 170

def new_issue(title, description)
  `ghi open -m "#{title}\n#{description}"`
end

.pull(branch) ⇒ Object



162
163
164
# File 'lib/stagecoach/git.rb', line 162

def pull(branch)
  puts `git pull origin #{branch}`
end

.push(branch) ⇒ Object



151
152
153
154
155
# File 'lib/stagecoach/git.rb', line 151

def push(branch)
  CommandLine.line_break
  puts "Pushing your changes to branch '#{branch}'"
  puts `git push origin #{branch}`
end

.remote_branchesObject



49
50
51
# File 'lib/stagecoach/git.rb', line 49

def remote_branches
  (`git ls-remote`.split(" ").each.select { |e| e =~ /refs\/heads/}).collect {|a| a.gsub("refs/heads/", "")}
end

.set_global_config(header, config, value) ⇒ Object



71
72
73
# File 'lib/stagecoach/git.rb', line 71

def set_global_config(header, config, value)
  `git config --global #{header}.#{config} #{value}`
end

.statusObject



79
80
81
# File 'lib/stagecoach/git.rb', line 79

def status
  `git status`
end

.tidy(deletable_branches) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/stagecoach/git.rb', line 32

def tidy(deletable_branches)
  CommandLine.line_break
  if deletable_branches.length > 0
    puts "All branches that have been merged into master will be deleted locally and remotely.".red
    print "Continue? [Y]es or anything else to cancel: "
    case STDIN.gets.chomp
    when /y/i 
      erase(deletable_branches) 
    else
      puts 'No branches deleted.  Exiting...'
    end
  else
    puts 'No branches to delete.  Exiting...'
  end
  exit
end