Class: MultiRepo::BranchCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/multirepo/commands/branch-command.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

#ensure_in_work_tree, #ensure_multirepo_enabled, #ensure_multirepo_tracked, #install_hooks, #multirepo_enabled_dependencies, report_error, #uninstall_hooks, #update_gitconfig

Constructor Details

#initialize(argv) ⇒ BranchCommand

Returns a new instance of BranchCommand.



20
21
22
23
24
25
# File 'lib/multirepo/commands/branch-command.rb', line 20

def initialize(argv)
  @branch_name = argv.shift_argument
  @force = argv.flag?("force")
  @push = argv.flag?("push", false)
  super
end

Class Method Details

.optionsObject



12
13
14
15
16
17
18
# File 'lib/multirepo/commands/branch-command.rb', line 12

def self.options
  [
    ['<branch name>', 'The name of the branch to create and checkout.'],
    ['[--force]', 'Force creating the branch even if there are uncommmitted changes.'],
    ['[--push]', 'Push the branch on creation.']
  ].concat(super)
end

Instance Method Details

#perform_branch(repo) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/multirepo/commands/branch-command.rb', line 63

def perform_branch(repo)
  Console.log_substep("Branching '#{repo.path}' ...")
  Console.log_info("Creating and checking out branch #{@branch_name} ...")
  
  branch = repo.branch(@branch_name)
  branch.create unless branch.exists?
  branch.checkout
  
  if Utils.multirepo_enabled?(repo.path)
    Console.log_info("Updating and committing tracking files")
    tracking_files = TrackingFiles.new(repo.path)
    tracking_files.update
    tracking_files.commit("[multirepo] Post-branch tracking files update")
  end
  
  return unless @push
  
  if @force
    Console.log_warning("Skipping #{@branch_name} branch push because we're force-branching")
  else
    Console.log_info("Pushing #{@branch_name} to origin/#{@branch_name}")
    repo.branch(@branch_name).push
  end
end

#runObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/multirepo/commands/branch-command.rb', line 33

def run
  ensure_in_work_tree
  ensure_multirepo_enabled
  
  Console.log_step("Branching...")
  
  main_repo = Repo.new(".")
  
  unless @force
    # Ensure the main repo is clean
    fail MultiRepoException, "Main repo is not clean; multi branch aborted" unless main_repo.clean?
    
    # Ensure dependencies are clean
    config_entries = ConfigFile.new(".").load_entries
    unless Utils.dependencies_clean?(config_entries)
      fail MultiRepoException, "Dependencies are not clean; multi branch aborted"
    end
  end

  # Branch dependencies
  Performer.depth_ordered_dependencies.each do |dependency|
    perform_branch(dependency.config_entry.repo)
  end
  
  # Branch the main repo
  perform_branch(main_repo)
  
  Console.log_step("Done!")
end

#validate!Object



27
28
29
30
31
# File 'lib/multirepo/commands/branch-command.rb', line 27

def validate!
  super
  help! "You must specify a branch name" unless @branch_name
  help! "Please provide a valid branch name" unless Git.valid_branch_name?(@branch_name)
end