mgit 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,4 @@
1
+ require 'colorize'
1
2
  require 'highline/import'
2
3
 
3
4
  require 'mgit/version'
@@ -2,7 +2,7 @@ module MGit
2
2
  class CLI
3
3
  def start
4
4
  raise NoCommandError if ARGV.size == 0
5
- command = Command.create(ARGV.shift.downcase.to_sym)
5
+ command = Command.create(ARGV.shift)
6
6
  command.execute(ARGV)
7
7
  rescue UsageError => e
8
8
  $stderr.puts e
@@ -4,9 +4,10 @@ module MGit
4
4
  @@aliases = {}
5
5
 
6
6
  def self.create(cmd)
7
- c = @@commands[cmd] || @@aliases[cmd]
8
- if c
9
- c.new
7
+ cmd = cmd.downcase.to_sym
8
+ klass = @@commands[cmd] || @@aliases[cmd]
9
+ if klass
10
+ klass.new
10
11
  else
11
12
  raise UnknownCommandError.new(cmd)
12
13
  end
@@ -23,5 +24,23 @@ module MGit
23
24
  def self.list
24
25
  '[' + @@commands.keys.join(', ') + ']'
25
26
  end
27
+
28
+ def self.instance_each
29
+ @@commands.each do |_, klass|
30
+ yield klass.new
31
+ end
32
+ end
33
+
34
+ def usage
35
+ raise ImplementationError.new("Command #{self.class.name} doesn't implement the usage method.")
36
+ end
37
+
38
+ def help
39
+ raise ImplementationError.new("Command #{self.class.name} doesn't implement the help method.")
40
+ end
41
+
42
+ def description
43
+ raise ImplementationError.new("Command #{self.class.name} doesn't implement the description method.")
44
+ end
26
45
  end
27
46
  end
@@ -16,6 +16,10 @@ module MGit
16
16
  'add <path_to_git_repository> [name]'
17
17
  end
18
18
 
19
+ def description
20
+ 'add a repository to mgit'
21
+ end
22
+
19
23
  register_command :add
20
24
 
21
25
  private
@@ -5,7 +5,7 @@ module MGit
5
5
 
6
6
  Repository.chdir_each do |name, path|
7
7
  `git remote`.split.each do |remote|
8
- puts "Fetching #{remote} in repository #{name}..."
8
+ puts "Fetching #{remote} in repository #{name}...".yellow
9
9
  `git fetch #{remote}`
10
10
  end
11
11
  end
@@ -15,6 +15,10 @@ module MGit
15
15
  'fetch'
16
16
  end
17
17
 
18
+ def description
19
+ 'fetch all remote repositories'
20
+ end
21
+
18
22
  register_command :fetch
19
23
  end
20
24
  end
@@ -0,0 +1,26 @@
1
+ module MGit
2
+ class ForEachCommand < Command
3
+ def execute(args)
4
+ raise TooFewArgumentsError.new(self) if args.size == 0
5
+
6
+ command = args.join(' ')
7
+
8
+ Repository.chdir_each do |name, path|
9
+ puts "Executing command in repository #{name}...".yellow
10
+ if !system(command) && !ask("Executing command '#{command}' in repository '#{name}' failed. Would you like to continue anyway?".red)
11
+ break
12
+ end
13
+ end
14
+ end
15
+
16
+ def usage
17
+ 'foreach <command...>'
18
+ end
19
+
20
+ def description
21
+ 'execute a command for each repository'
22
+ end
23
+
24
+ register_command :foreach
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module MGit
2
+ class GrepCommand < Command
3
+ def execute(args)
4
+ raise TooFewArgumentsError.new(self) if args.size == 0
5
+ raise TooManyArgumentsError.new(self) if args.size > 1
6
+
7
+ ptrn = args[0]
8
+
9
+ Repository.chdir_each do |name, path|
10
+ puts "Looking for pattern '#{ptrn}' in repository #{name}...".yellow
11
+ puts `git grep #{ptrn}`
12
+ puts
13
+ end
14
+ end
15
+
16
+ def usage
17
+ 'grep <pattern>'
18
+ end
19
+
20
+ def description
21
+ 'grep for a pattern in each repository'
22
+ end
23
+
24
+ register_command :grep
25
+ end
26
+ end
@@ -0,0 +1,28 @@
1
+ module MGit
2
+ class HelpCommand < Command
3
+ def execute(args)
4
+ raise TooManyArgumentsError.new(self) if args.size > 1
5
+
6
+ if args.size == 0
7
+ puts "M[eta]Git - manage multiple git repositories at the same time"
8
+ puts
9
+ puts "Usage:"
10
+ Command.instance_each do |cmd|
11
+ puts "mgit #{cmd.usage}\n\t- #{cmd.description}"
12
+ end
13
+ else
14
+ puts Command.create(args[0]).help
15
+ end
16
+ end
17
+
18
+ def usage
19
+ 'help [command]'
20
+ end
21
+
22
+ def description
23
+ 'display help information'
24
+ end
25
+
26
+ register_command :help
27
+ end
28
+ end
@@ -12,6 +12,10 @@ module MGit
12
12
  'list'
13
13
  end
14
14
 
15
+ def description
16
+ 'list all repositories'
17
+ end
18
+
15
19
  register_command :list
16
20
  register_alias :ls
17
21
  end
@@ -0,0 +1,31 @@
1
+ module MGit
2
+ class RemoveCommand < Command
3
+ def execute(args)
4
+ raise TooFewArgumentsError.new(self) if args.size == 0
5
+ raise TooManyArgumentsError.new(self) if args.size > 1
6
+
7
+ ptrn = args[0]
8
+
9
+ repo = Repository.find do |name, path|
10
+ name == ptrn || path == File.expand_path(ptrn)
11
+ end
12
+
13
+ raise CommandUsageError.new("Couldn't find repository matching '#{ptrn}'.", self) unless repo
14
+
15
+ name = repo[0]
16
+ Repository.remove(name)
17
+ puts "Removed repository #{name}.".yellow
18
+ end
19
+
20
+ def usage
21
+ 'remove <name/path>'
22
+ end
23
+
24
+ def description
25
+ 'remove a repository'
26
+ end
27
+
28
+ register_command :remove
29
+ register_alias :rm
30
+ end
31
+ end
@@ -16,6 +16,10 @@ module MGit
16
16
  'status'
17
17
  end
18
18
 
19
+ def description
20
+ 'display status for each repositories'
21
+ end
22
+
19
23
  register_command :status
20
24
  register_alias :st
21
25
 
@@ -27,19 +31,19 @@ module MGit
27
31
  status.each do |s|
28
32
  case s.split[0]
29
33
  when 'A'
30
- flags << 'Index'
34
+ flags << 'Index'.red
31
35
  when 'M'
32
- flags << 'Dirty'
36
+ flags << 'Dirty'.red
33
37
  when '??'
34
- flags << 'Untracked'
38
+ flags << 'Untracked'.yellow
35
39
  when '##'
36
- if(m = /## (\w+)\.\.\.([\w,\/]+) \[(\w+) (\d+)\]/.match(s))
37
- flags << "#{m[3].capitalize} #{m[2]} by #{m[4]}"
40
+ if(m = /## ([\w,\/]+)\.\.\.([\w,\/]+) \[(\w+) (\d+)\]/.match(s))
41
+ flags << "#{m[3].capitalize} of #{m[2]} by #{m[4]}".blue
38
42
  end
39
43
  end
40
44
  end
41
45
 
42
- flags
46
+ flags.empty? ? ['Clean'.green] : flags
43
47
  end
44
48
  end
45
49
  end
@@ -0,0 +1,18 @@
1
+ module MGit
2
+ class VersionCommand < Command
3
+ def execute(args)
4
+ raise TooManyArgumentsError.new(self) if args.size != 0
5
+ puts "mgit version #{MGit::VERSION}"
6
+ end
7
+
8
+ def usage
9
+ 'version'
10
+ end
11
+
12
+ def description
13
+ 'display mgit version'
14
+ end
15
+
16
+ register_command :version
17
+ end
18
+ end
@@ -1,4 +1,6 @@
1
1
  module MGit
2
+ class ImplementationError < StandardError; end
3
+
2
4
  class UsageError < StandardError
3
5
  def initialize(error, usage)
4
6
  @error = error
@@ -18,10 +18,20 @@ module MGit
18
18
  end
19
19
  end
20
20
 
21
+ def self.find(&block)
22
+ self.all.find(&block)
23
+ end
24
+
21
25
  def self.add(name, path)
22
26
  repos = self.all
23
27
  repos[name] = path
24
- File.open(self.repofile, 'w') { |fd| fd.write repos.to_yaml }
28
+ self.save! repos
29
+ end
30
+
31
+ def self.remove(name)
32
+ repos = self.all
33
+ repos.delete name
34
+ self.save! repos
25
35
  end
26
36
 
27
37
  private
@@ -29,5 +39,9 @@ module MGit
29
39
  def self.repofile
30
40
  File.join(Dir.home, '.config/mgit.yml')
31
41
  end
42
+
43
+ def self.save!(repos)
44
+ File.open(self.repofile, 'w') { |fd| fd.write repos.to_yaml }
45
+ end
32
46
  end
33
47
  end
@@ -1,3 +1,3 @@
1
1
  module MGit
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mgit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -11,6 +11,22 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2013-10-21 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: colorize
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: highline
16
32
  requirement: !ruby/object:Gem::Requirement
@@ -34,16 +50,21 @@ executables:
34
50
  extensions: []
35
51
  extra_rdoc_files: []
36
52
  files:
37
- - lib/mgit.rb
38
- - lib/mgit/repository.rb
39
- - lib/mgit/version.rb
40
53
  - lib/mgit/exceptions.rb
54
+ - lib/mgit/cli.rb
55
+ - lib/mgit/command.rb
56
+ - lib/mgit/commands/add.rb
41
57
  - lib/mgit/commands/list.rb
58
+ - lib/mgit/commands/help.rb
59
+ - lib/mgit/commands/foreach.rb
60
+ - lib/mgit/commands/remove.rb
42
61
  - lib/mgit/commands/fetch.rb
62
+ - lib/mgit/commands/grep.rb
63
+ - lib/mgit/commands/version.rb
43
64
  - lib/mgit/commands/status.rb
44
- - lib/mgit/commands/add.rb
45
- - lib/mgit/command.rb
46
- - lib/mgit/cli.rb
65
+ - lib/mgit/repository.rb
66
+ - lib/mgit/version.rb
67
+ - lib/mgit.rb
47
68
  - bin/mgit
48
69
  homepage: http://github.com/flavoursys/mgit
49
70
  licenses: