Class: CapitaGit::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/capita_git/cli.rb

Instance Method Summary collapse

Constructor Details

#initializeCLI


25
26
27
28
29
30
31
# File 'lib/capita_git/cli.rb', line 25

def initialize(*)
  super
  the_shell = (options["no-color"] ? Thor::Shell::Basic.new : shell)
  CapitaGit.ui = CapitaGit::UI::Shell.new(the_shell)
  CapitaGit.ui.debug! if options["verbose"]
  Gem::DefaultUserInteraction.ui = CapitaGit::UI::RGProxy.new(CapitaGit.ui)
end

Instance Method Details

#backport(fix_branch = nil) ⇒ Object


155
156
157
158
159
160
161
# File 'lib/capita_git/cli.rb', line 155

def backport(fix_branch=nil)
  repo = CapitaGit::Repository.open(Dir.pwd)
  fix_branch = fix_branch.nil? ? repo.current_branch : fix_branch

  log :confirm, "--> Backporting changes from '#{fix_branch}' into 'master'"
  repo.merge_fixbranch(fix_branch)
end

#checkObject


76
77
78
79
80
81
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
116
117
118
119
# File 'lib/capita_git/cli.rb', line 76

def check
  opts = options.dup
  log :debug, "[DEBUG] Starting action 'check'"

  repo = CapitaGit::Repository.open(Dir.pwd, CapitaGit.ui)
  repo.git_remote = opts[:remote] if opts[:remote]
  log :debug, "[DEBUG] Repository remote set to '#{repo.git_remote}'"

  log :info, "-- Starting to check repository '#{repo.name}'"
  log :confirm, "-> Active user   : '#{repo.user_name} <#{repo.user_email}>'"
  log :confirm, "-> User shortcut : '#{repo.user_shortcut}'"
  log :info, "\n"

  log :info, "-- Checking for latest changes on \'#{repo.remote}\'"
  repo.fetch_remote_changes
  latest_major_release_tag = repo.latest_major_release_tag
  latest_minor_release_tag = repo.latest_minor_release_tag
  log :confirm, "-> Latest major release tag is: #{latest_major_release_tag || '---'}"
  log :confirm, "-> Latest minor release tag is: #{latest_minor_release_tag || '---'}"
  log :info, "\n"

  if latest_major_release_tag.nil?
    log :warn, 'No major release tag found, exiting!'
    exit 0
  end

  log :info, '-- Checking for presence of major release fixbranch'
  local_fixbranch = repo.local_fixbranch_for_version?(latest_major_release_tag)
  remote_fixbranch = repo.remote_fixbranch_for_version?(latest_major_release_tag)
  log :confirm, "-> Local  : #{local_fixbranch.nil? ? '---' : local_fixbranch.full }"
  log :confirm, "-> Remote : #{remote_fixbranch.nil? ? '---' : remote_fixbranch.full }"

  if not repo.has_remote_fixbranch_for_version?(latest_major_release_tag)
    log :info, "--> Creating remote fixbranch #{latest_major_release_tag}-fix"
    repo.create_remote_fixbranch_for_version(latest_major_release_tag)
  end

  if not repo.has_local_fixbranch_for_version?(latest_major_release_tag)
    log :info, "--> Creating tracking local fixbranch #{latest_major_release_tag}-fix from remote fixbranch"
    repo.create_local_fixbranch_for_version(latest_major_release_tag)
  end

  log :info, "\n-- Done!\n"
end

#close(feature_branch = nil) ⇒ Object


145
146
147
148
149
150
151
# File 'lib/capita_git/cli.rb', line 145

def close(feature_branch=nil)
  repo = CapitaGit::Repository.open(Dir.pwd)
  feature_branch = feature_branch.nil? ? repo.current_branch : feature_branch

  log :confirm, "--> Closing feature branch '#{feature_branch}' onto '#{repo.source_branch(feature_branch)}'"
  repo.close_local_branch(feature_branch)
end

#create(name, source_branch = nil) ⇒ Object


123
124
125
126
127
128
129
130
# File 'lib/capita_git/cli.rb', line 123

def create(name, source_branch=nil)
  repo = CapitaGit::Repository.open(Dir.pwd)
  source_branch = source_branch.nil? ? repo.current_branch.to_s : source_branch
  new_branch = "#{repo.user_shortcut}_#{source_branch}_#{name}"
  log :confirm, "--> Creating and switching to feature branch '#{new_branch}'"
  repo.create_local_branch(new_branch, source_branch)
  repo.checkout_local_branch(new_branch)
end

#help(cli = nil) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/capita_git/cli.rb', line 38

def help(cli = nil)
  case cli
    when "gemfile" then
      command = "gemfile.5"
    when nil then
      command = "gitc"
    else
      command = "gitc-#{cli}"
  end

  manpages = %w(
          gitc
          gitc-check
          gitc-create
          gitc-update
          gitc-close
          gitc-backport
          gitc-install_autocomplete)

  if manpages.include?(command)
    root = File.expand_path("../man", __FILE__)

    if have_groff? && root !~ %r{^file:/.+!/META-INF/jruby.home/.+}
      groff = "groff -Wall -mtty-char -mandoc -Tascii"
      pager = ENV['MANPAGER'] || ENV['PAGER'] || 'more'

      Kernel.exec "#{groff} #{root}/#{command} | #{pager}"
    else
      puts File.read("#{root}/#{command}.txt")
    end
  else
    super
  end
end

#install_autocompleteObject


165
166
167
168
169
# File 'lib/capita_git/cli.rb', line 165

def install_autocomplete
  root = File.expand_path("../../", __FILE__)
  system "cp #{root}/bash_completion.txt ~/.gitc_completion"
  system "if grep -q 'gitc_completion' ~/.bashrc; then echo 'Done'; else echo -e '\n. .gitc_completion' >> ~/.bashrc; . ~/.gitc_completion; fi"
end

#publish(branch_name = nil) ⇒ Object


173
174
175
176
177
178
179
# File 'lib/capita_git/cli.rb', line 173

def publish(branch_name=nil)
  repo = CapitaGit::Repository.open(Dir.pwd)
  branch_name = branch_name.nil? ? repo.current_branch : branch_name

  log :confirm, "--> Publishing '#{branch_name}' to 'origin"
  repo.publish_branch(branch_name)
end

#runner(command) ⇒ Object


183
184
185
186
# File 'lib/capita_git/cli.rb', line 183

def runner(command)
  repo = CapitaGit::Repository.open(Dir.pwd)
  puts repo.send("#{command}")
end

#update(feature_branch = nil) ⇒ Object


134
135
136
137
138
139
140
141
# File 'lib/capita_git/cli.rb', line 134

def update(feature_branch=nil)
  repo = CapitaGit::Repository.open(Dir.pwd)
  feature_branch = feature_branch.nil? ? repo.current_branch : feature_branch
  raise "Source branch '#{feature_branch}' is not a feature branch, can't update!" unless repo.is_local_feature_branch?(feature_branch)

  log :confirm, "--> Updating feature branch '#{feature_branch}' from '#{repo.source_branch(feature_branch)}'"
  repo.rebase_local_branch(feature_branch)
end