Class: MGit::AddCommand

Inherits:
Command show all
Defined in:
lib/mgit/commands/add.rb

Instance Method Summary collapse

Methods inherited from Command

#check_arity, execute, instance_each, list, load_commands, register_alias, register_command

Methods included from Output

#perror, #pinfo, #ptable, #pwarn

Instance Method Details

#arityObject



17
18
19
# File 'lib/mgit/commands/add.rb', line 17

def arity
  [1, 2]
end

#descriptionObject



25
26
27
# File 'lib/mgit/commands/add.rb', line 25

def description
  'add a repository to mgit'
end

#execute(args) ⇒ Object

Raises:



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/mgit/commands/add.rb', line 3

def execute(args)
  path = File.expand_path(args[0])
  raise CommandUsageError.new('First argument must be a path to a directory.', self) unless File.directory?(path)
  raise CommandUsageError.new('First argument must be a path to a git repository.', self) unless is_git_dir?(path)
  raise CommandUsageError.new('Sorry, mgit can not handle bare repositories.', self) if is_bare?(path)

  # NOTE: The to_s to the HighLine::String is needed due to a bug in YAML/Psych in Ruby 2.0.0.
  # https://github.com/JEG2/highline/issues/69
  name = (args.size == 2) ? args[1] : (ask('Name of the repository? ') { |q| q.default = File.basename(path) }).to_s
  raise CommandUsageError.new("Repository named #{name} already exists with different path.", self) unless is_new_or_same?(name, path)
  
  Registry.add(name, path)
end

#usageObject



21
22
23
# File 'lib/mgit/commands/add.rb', line 21

def usage
  'add <path_to_git_repository> [name]'
end