Class: MultiRepo::InitCommand

Inherits:
Command
  • Object
show all
Defined in:
lib/multirepo/commands/init-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, #validate!

Constructor Details

#initialize(argv) ⇒ InitCommand

Returns a new instance of InitCommand.



17
18
19
20
# File 'lib/multirepo/commands/init-command.rb', line 17

def initialize(argv)
  @only_extras = argv.flag?("extras")
  super
end

Class Method Details

.optionsObject



13
14
15
# File 'lib/multirepo/commands/init-command.rb', line 13

def self.options
  [['[--extras]', 'Keep the current .multirepo config file as-is and initialize everything else.']].concat(super)
end

Instance Method Details

#add_sibling_repos_stepObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/multirepo/commands/init-command.rb', line 50

def add_sibling_repos_step
  sibling_repos = Utils.sibling_repos
  return false unless sibling_repos.any?

  Console.log_substep("Creating new multirepo config...")
  
  valid_repos = find_valid_repos(sibling_repos)
  entries = create_entries(valid_repos)
  
  fail MultiRepoException, "No sibling repositories were added as dependencies; init aborted" unless entries.any?
  
  ConfigFile.new(".").save_entries(entries)
  return true
end

#check_repo_existsObject



116
117
118
119
# File 'lib/multirepo/commands/init-command.rb', line 116

def check_repo_exists
  fail MultiRepoException, "There is no folder at path '#{@repo.path}'" unless Dir.exist?(@repo.path)
  fail MultiRepoException, "'#{@repo.path}' is not a repository" unless @repo.exists?
end

#create_entries(repos) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/multirepo/commands/init-command.rb', line 97

def create_entries(repos)
  entries = []
  repos.each do |repo|
    origin_url = repo.remote('origin').url
    current_branch_name = repo.current_branch.name
    
    next unless Console.ask("Do you want to add '#{repo.path}' as a dependency?\n  [origin: #{origin_url || 'NONE'}, branch: #{current_branch_name}]")
    
    unless origin_url
      Console.log_warning("Repo 'origin' remote url is not set; skipping")
      next
    end
    
    entries.push(ConfigEntry.new(repo))
    Console.log_substep("Added the repository '#{repo.path}' to the .multirepo file")
  end
  return entries
end

#find_valid_repos(repos) ⇒ Object



90
91
92
93
94
95
# File 'lib/multirepo/commands/init-command.rb', line 90

def find_valid_repos(repos)
  repos.select do |repo|
    next true if repo.head_born?
    Console.log_warning("Ignoring repo '#{repo.path}' because its HEAD is unborn. You must perform at least one commit.")
  end
end

#full_initialize_stepObject



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/multirepo/commands/init-command.rb', line 37

def full_initialize_step
  if ConfigFile.new(".").exists?
    reinitialize = Console.ask(".multirepo file already exists. Reinitialize?")
    fail MultiRepoException, "Initialization aborted" unless reinitialize
  end
  
  unless add_sibling_repos_step
    fail MultiRepoException, "There are no sibling repositories to track as dependencies. Initialization aborted."
  end

  initialize_extras_step
end

#initialize_extras_stepObject



65
66
67
68
69
# File 'lib/multirepo/commands/init-command.rb', line 65

def initialize_extras_step
  install_hooks_step
  update_gitattributes_step
  update_gitconfig_step
end

#install_hooks_stepObject



71
72
73
74
# File 'lib/multirepo/commands/init-command.rb', line 71

def install_hooks_step
  install_hooks(".")
  Console.log_substep("Installed git hooks")
end

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/multirepo/commands/init-command.rb', line 22

def run
  ensure_in_work_tree
  
  if @only_extras
    ensure_multirepo_enabled
    Console.log_step("Initializing extras...")
    initialize_extras_step
  else
    Console.log_step("Initializing multirepo...")
    full_initialize_step
  end
  
  Console.log_step("Done!")
end

#update_gitattributes_stepObject



76
77
78
79
80
81
82
83
# File 'lib/multirepo/commands/init-command.rb', line 76

def update_gitattributes_step
  TrackingFiles.new(".").files.each do |f|
    filename = f.filename
    regex_escaped_filename = Regexp.quote(filename)
    Utils.append_if_missing("./.gitattributes", /^#{regex_escaped_filename} .*/, "#{filename} merge=ours")
  end
  Console.log_substep("Updated .gitattributes file")
end

#update_gitconfig_stepObject



85
86
87
88
# File 'lib/multirepo/commands/init-command.rb', line 85

def update_gitconfig_step
  update_gitconfig(".")
  Console.log_substep("Updated .git/config file")
end