git-projects 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4504bc3fe630d0ea505b28aa524773333b5be322
4
+ data.tar.gz: adc75f8bea5c45630d2874000b2dcd9cd3def3a1
5
+ SHA512:
6
+ metadata.gz: 6bf2122f31976a419836eab66db89dc5faefabe44a3293f75bb3bfabdbc94f0ca00787ccf597e1e1192dab337f232e6780c9e6a07d176ea2c451f0b6f64d6d10
7
+ data.tar.gz: e2e8df57c74dfd36c9d327f37d2e18f088ba9fcc036ee4b58a6aad3e65255241600aa7de44c8dd0e52663063f0277d900e9fc8116ba6ca94de14c17ab24bdc90
data/bin/git-projects ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ruby
2
+ require 'git-projects'
@@ -0,0 +1,3 @@
1
+ module GitProjects
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env ruby
2
+ require 'gli'
3
+ require 'json'
4
+ require_relative 'helpers/project'
5
+ require_relative 'helpers/git_project'
6
+
7
+ include GLI::App
8
+
9
+ program_desc 'Easily manage Git projects'
10
+
11
+ pre do |global_options,command,options,args|
12
+ if ENV['GIT_PROJECTS']
13
+ p "Checking repositories. If things go wrong, update #{ENV['GIT_PROJECTS']}"
14
+ else
15
+ raise "Please add the path your git projects config. \n export GIT_PROJECTS=/path/to/git_projects.yml"
16
+ end
17
+ end
18
+
19
+ desc 'Create a config'
20
+ skips_pre
21
+ command :config do |c|
22
+ c.action do |global_options,options,args|
23
+ config_path = "#{args.first}/git-projects.yml"
24
+ GitProject.create_config(args)
25
+ if File.open(config_path)
26
+ p "successfully created git-projects.yml"
27
+ end
28
+ end
29
+ end
30
+
31
+ desc 'Clone all projects'
32
+ command :clone do |c|
33
+ c.action do
34
+ if GitProject.new(ENV['GIT_PROJECTS']).clone_all
35
+ p 'successfully cloned repositories'
36
+ end
37
+ end
38
+ end
39
+
40
+ desc 'Fetch changed from all remotes'
41
+ command :fetch do |c|
42
+ c.action do
43
+ if GitProject.new(ENV['GIT_PROJECTS']).fetch_all
44
+ p 'successfully fetched changes'
45
+ end
46
+ end
47
+ end
48
+
49
+ exit run(ARGV)
@@ -0,0 +1,143 @@
1
+ require_relative 'project'
2
+ require 'git'
3
+ require 'yaml'
4
+
5
+ class GitProject
6
+
7
+ attr :project
8
+
9
+ def initialize(config)
10
+ @project = Project.new(config)
11
+ end
12
+
13
+ class << self
14
+ # Create YAML file
15
+ def create_yaml_file(config_file, projects)
16
+ File.open(config_file, "w") do |f|
17
+ f.write projects.to_yaml.gsub(/- /,'').gsub(/ /,' ').gsub(/---/,'')
18
+ end
19
+ end
20
+
21
+ # Create a hash for remotes
22
+ def add_remotes_to_hash(g, p, dir)
23
+ remotes_h = {}
24
+ r = {}
25
+ remotes_h.tap do |remotes|
26
+ g.remotes.each do |remote|
27
+ r[remote.name] = remote.url
28
+ end
29
+ r['all'] = true
30
+ r['root_dir'] = dir
31
+ remotes.merge!(r)
32
+ end
33
+ end
34
+
35
+ # Create a configuration file based on a root path
36
+ def create_config(dir)
37
+ dir = dir.is_a?(Array) ? dir.first : dir
38
+ config_file = "#{dir}/git-projects.yml"
39
+ unless File.exists?(config_file)
40
+ projects = []
41
+ Dir.entries(dir)[2..-1].each do |project|
42
+ g = Git.open("#{dir}/#{project}")
43
+ p = {}
44
+ p[project] = add_remotes_to_hash(g, p, dir)
45
+ projects << p
46
+ create_yaml_file(config_file, projects)
47
+ end
48
+ else
49
+ raise "The config file, #{config_file} exists"
50
+ end
51
+ end
52
+
53
+ def set_root_path(path)
54
+ @project.set_root_path(path)
55
+ end
56
+
57
+ # Create dir unless it exists
58
+ def create_directory(path)
59
+ unless File.directory?(path)
60
+ `mkdir -p #{path}`
61
+ printf("Creating directory: %s\n", path)
62
+ end
63
+ end
64
+
65
+ # Create root_dir
66
+ def create_root_dir(path)
67
+ GitProject.create_directory(path) unless File.directory?(path)
68
+ end
69
+
70
+ # Clone unless dir exists
71
+ def clone(url, name, path)
72
+ r = "#{path}/#{name}"
73
+ if Git.open(r)
74
+ p "Already cloned #{url}"
75
+ else
76
+ Git.clone(url, name, path: path) || Git.init(r)
77
+ g = Git.open(r)
78
+ p "Cloning #{url} as #{name} into #{path}"
79
+ end
80
+ g
81
+ end
82
+
83
+ # Add remote
84
+ def add_remote(g, v)
85
+ g.add_remote('all', v['origin'])
86
+ v.each do |name, remote|
87
+ unless ['root_dir', 'origin', 'all'].include?(name)
88
+ g.add_remote(name, remote) unless g.remotes.map(&:name).include?(name)
89
+
90
+ # add to all remote
91
+ # useful when you want to do git push all --all
92
+ `git remote set-url --add all #{remote}`
93
+ end
94
+ end
95
+ end
96
+
97
+ def fetch(g, url)
98
+ g.remotes.each do |r|
99
+ unless r.name=='all'
100
+ r.fetch
101
+ p "Fetching updates from #{r.name} : #{r.url}"
102
+ end
103
+ end
104
+ end
105
+ end
106
+
107
+ # 1. Clone all repositories based on the origin key
108
+ # 2. Add all other remotes unless it is origin
109
+ def clone_all
110
+ @project.all.each do |k,v|
111
+ begin
112
+ p "root_dir isn't defined for #{k}" unless v['root_dir']
113
+ p "The dir #{v['root_dir']} does not exist" unless File.directory?(v['root_dir'])
114
+ GitProject.create_root_dir(v['root_dir'])
115
+ g = GitProject.clone(v.values[0], k, v['root_dir'])
116
+ GitProject.add_remote(g,v) if g
117
+ rescue => e
118
+ g = Git.init("#{v['root_dir']}/#{k}")
119
+ if g
120
+ GitProject.add_remote(g,v)
121
+ GitProject.fetch(g, v)
122
+ end
123
+ p "Please check paths and permissions for #{k}. Error: #{e}"
124
+ p "Failed to clone #{v.values[0]}. Initialized & fetch update from remotes instead."
125
+ end
126
+ end
127
+ end
128
+
129
+ # Fetch all updates for remotes for all projects
130
+ def fetch_all
131
+ p "Found #{@project.all.size} projects"
132
+ @project.all.each do |k,v|
133
+ p "Fetching changes for #{k}"
134
+ GitProject.create_root_dir(v['root_dir'])
135
+ working_dir = "#{v['root_dir']}/#{k}"
136
+ g = Git.open(working_dir) || Git.init(working_dir)
137
+ GitProject.fetch(g, v)
138
+ GitProject.add_remote(g,v)
139
+ end
140
+ return true
141
+ end
142
+
143
+ end
@@ -0,0 +1,26 @@
1
+ require 'yaml'
2
+
3
+ class Project
4
+
5
+ attr :projects
6
+
7
+ def initialize(config)
8
+ @projects = YAML.load_file(config)
9
+ end
10
+
11
+ def all
12
+ @projects
13
+ end
14
+
15
+ def first
16
+ all.first
17
+ end
18
+
19
+ def set_root_path(path)
20
+ @projects.tap do |project|
21
+ project.each do |k,v|
22
+ v['root_dir'] = path
23
+ end
24
+ end
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: git-projects
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Katherine Pe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: git
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.6
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.6
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: aruba
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: gli
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 2.9.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 2.9.0
69
+ description:
70
+ email: k@kat.pe
71
+ executables:
72
+ - git-projects
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - bin/git-projects
77
+ - lib/git-projects.rb
78
+ - lib/git-projects/version.rb
79
+ - lib/helpers/git_project.rb
80
+ - lib/helpers/project.rb
81
+ homepage: https://github.com/katgironpe/git-projects
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.2.1
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Easily manage Git projects
106
+ test_files: []