Class: MultiRepo::Command

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Command

Returns a new instance of Command.



24
25
26
27
28
29
30
# File 'lib/multirepo/commands/command.rb', line 24

def initialize(argv)
  @argv = argv
  Config.instance.verbose |= argv.flag?("verbose") ? true : false
  Config.instance.extra_output ||= argv.option("extra-output")
  Config.instance.git_executable ||= argv.option("git-exe", "git")
  super
end

Class Method Details

.report_error(exception) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/multirepo/commands/command.rb', line 16

def self.report_error(exception)
  if exception.instance_of?(MultiRepoException)
    Console.log_error(exception.message)
    exit 1
  end
  fail exception
end

Instance Method Details

#ensure_in_work_treeObject



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

def ensure_in_work_tree
  repo = Repo.new(".")
  fail MultiRepoException, "Not a git repository" unless repo.exists?
  fail MultiRepoException, "HEAD is unborn (you must perform at least one commit)" unless repo.head_born?
end

#ensure_multirepo_enabledObject



76
77
78
# File 'lib/multirepo/commands/command.rb', line 76

def ensure_multirepo_enabled
  fail MultiRepoException, "multirepo is not initialized in this repository." unless Utils.multirepo_enabled?(".")
end

#ensure_multirepo_trackedObject



80
81
82
83
84
85
# File 'lib/multirepo/commands/command.rb', line 80

def ensure_multirepo_tracked
  fail MultiRepoException, "Revision is not tracked by multirepo." unless Utils.multirepo_tracked?(".")
  
  lock_file_valid = LockFile.new(".").validate!
  fail MultiRepoException, "Revision is multirepo-enabled but contains a corrupted lock file!" unless lock_file_valid
end

#install_hooks(path) ⇒ Object



44
45
46
47
48
# File 'lib/multirepo/commands/command.rb', line 44

def install_hooks(path)
  actual_path = path || "."
  Utils.install_hook("pre-commit", actual_path)
  Utils.install_hook("post-commit", actual_path)
end

#multirepo_enabled_dependenciesObject



66
67
68
# File 'lib/multirepo/commands/command.rb', line 66

def multirepo_enabled_dependencies
  ConfigFile.new(".").load_entries.select { |e| Utils.multirepo_enabled?(e.repo.path) }
end

#runObject



32
33
34
# File 'lib/multirepo/commands/command.rb', line 32

def run
  help!
end

#uninstall_hooksObject



50
51
52
53
# File 'lib/multirepo/commands/command.rb', line 50

def uninstall_hooks
  FileUtils.rm_f(".git/hooks/pre-commit")
  FileUtils.rm_f(".git/hooks/post-commit")
end

#update_gitconfig(path) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/multirepo/commands/command.rb', line 55

def update_gitconfig(path)
  actual_path = path || "."
  resource_file = Utils.path_for_resource(".gitconfig")
  target_file = File.join(actual_path, '.git/config')
  
  template = File.read(resource_file)
  first_template_line = template.lines.first
  
  Utils.append_if_missing(target_file, Regexp.new(Regexp.quote(first_template_line)), template)
end

#validate!Object



36
37
38
39
40
41
42
# File 'lib/multirepo/commands/command.rb', line 36

def validate!
  super
  path = Config.instance.git_executable
  is_git_exe = path =~ /.*(git)|(git.exe)$/
  file_exists = path == "git" || File.exist?(path)
  help! "Invalid git executable '#{path}'" unless is_git_exe && file_exists
end