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

Returns a new instance of CLI.



11
12
13
14
15
16
17
# File 'lib/capita_git/cli.rb', line 11

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



131
132
133
134
135
136
137
# File 'lib/capita_git/cli.rb', line 131

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/capita_git/cli.rb', line 56

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



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

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



102
103
104
105
106
107
108
109
# File 'lib/capita_git/cli.rb', line 102

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/capita_git/cli.rb', line 24

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)

  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

#runner(command) ⇒ Object



141
142
143
144
# File 'lib/capita_git/cli.rb', line 141

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

#update(feature_branch = nil) ⇒ Object



112
113
114
115
116
117
118
119
# File 'lib/capita_git/cli.rb', line 112

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