-
Notifications
You must be signed in to change notification settings - Fork 0
/
mgit.rb
110 lines (95 loc) · 2.53 KB
/
mgit.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require 'yaml'
require 'logger'
require 'commander/import'
require 'git'
require 'fileutils'
# MGit - multiple git repositories manager
class MGit
def initialize
@config_dir = ENV[MGIT_CONFIGURATION_ENV]
unless configuration_defined?
throw RuntimeError.new("Please define configuration directory in \
#{MGIT_CONFIGURATION_ENV} environment variable")
end
@config_file = @config_dir + '/mgit.yml'
@config_local_file = @config_dir + '/mgit.local.yml'
@config = read_config(@config_file)
@local_config = read_config(@config_local_file)
end
def dump_config
puts 'Dumping configuration:'
puts
puts "\tProject directory: #{@local_config['project_directory']}"
puts
puts "\tListing repositories in the project:"
@config['repos'].each do |repo|
dump_repo_config(repo)
end
end
def status
@config['repos'].each do |repo|
repo_status(@local_config['project_directory'], repo)
end
end
def clone
puts "Cloning..."
FileUtils::mkdir_p @local_config['project_directory']
Dir.chdir @local_config['project_directory']
@config['repos'].each do |repo|
puts "Cloning #{repo['upstream']} to #{repo.keys.first}"
g = Git.clone(repo['upstream'], repo.keys.first)
g.branch(repo['ref']).checkout
end
end
private
MGIT_CONFIGURATION_ENV = 'MGIT_CONFIGURATION'.freeze
def configuration_defined?
if @config_dir.nil? || @config_dir.empty?
false
else
true
end
end
def read_config(config_file)
YAML.load_file(config_file)
end
def dump_repo_config(repo)
puts "\t\t#{repo.keys.first}"
puts "\t\tUpstream url: #{repo['upstream']}"
puts "\t\tDevelopment url: #{repo['dev']}"
puts "\t\tGit reference: #{repo['ref']}"
puts
end
def repo_status(project_dir, repo)
working_dir = project_dir + repo.keys.first
puts "Opening #{working_dir}"
g = Git.open(working_dir, :log => Logger.new(STDOUT))
end
end
program :name, 'mgit'
program :version, '0.0.1'
program :description, 'Multiple git repositories manager.'
command :dump_config do |c|
c.syntax = 'mgit dump_config'
c.description = 'Displays mgit configuration'
c.action do ||
mgit = MGit.new
mgit.dump_config
end
end
command :clone do |c|
c.syntax = 'mgit clone'
c.description = 'Clone all the repositories'
c.action do ||
mgit = MGit.new
mgit.clone
end
end
command :status do |c|
c.syntax = 'mgit status'
c.description = 'Status of every repository'
c.action do ||
mgit = MGit.new
mgit.status
end
end