obazoud-git-external 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/bin/git-external +152 -71
  2. metadata +10 -8
data/bin/git-external CHANGED
@@ -1,9 +1,58 @@
1
1
  #!/usr/bin/env ruby
2
2
  # encoding: utf-8
3
3
 
4
- $root_dir = `git rev-parse --show-toplevel`.chomp
5
- $externals_file = "#{$root_dir}/.gitexternals"
6
- $ignore_file = "#{$root_dir}/.gitignore"
4
+ require 'rbconfig'
5
+
6
+ module OS
7
+ class << self
8
+ def is?(what)
9
+ what === RbConfig::CONFIG['host_os']
10
+ end
11
+ alias is is?
12
+
13
+ def to_s
14
+ RbConfig::CONFIG['host_os']
15
+ end
16
+ end
17
+
18
+ module_function
19
+
20
+ def linux?
21
+ OS.is? /linux|cygwin/
22
+ end
23
+
24
+ def mac?
25
+ OS.is? /mac|darwin/
26
+ end
27
+
28
+ def bsd?
29
+ OS.is? /bsd/
30
+ end
31
+
32
+ def windows?
33
+ OS.is? /mswin|win|mingw|mingw32/
34
+ end
35
+
36
+ def solaris?
37
+ OS.is? /solaris|sunos/
38
+ end
39
+
40
+ def posix?
41
+ linux? or mac? or bsd? or solaris? # or Process.respond_to?(:fork)
42
+ end
43
+ end
44
+
45
+ if OS.posix?
46
+ $root_dir = `git rev-parse --show-toplevel`.chomp + '/'
47
+ $externals_file = "#{$root_dir}.gitexternals"
48
+ $ignore_file = "#{$root_dir}.gitignore"
49
+ else
50
+ # puts "Not posix os, you must run git-external from top level directory"
51
+ $root_dir = ''
52
+ $externals_file = "#{$root_dir}.gitexternals".gsub(' ', '\\ ')
53
+ $ignore_file = "#{$root_dir}.gitignore".gsub(' ', '\\ ')
54
+ end
55
+
7
56
  $configurations = {}
8
57
 
9
58
  def usage
@@ -15,56 +64,37 @@ def usage
15
64
  puts " or: git external list"
16
65
  end
17
66
 
18
- def load_configuration
19
- if File.file? $externals_file
20
- lines = `git config -l -f #{$externals_file}`.split(/\n/)
21
- $configurations = {}
22
- lines.each do |line|
23
- if line =~ /^external\.([^$]+)\.([^=]+)=(.*)$/
24
- $configurations[$1.chomp] ||= {}
25
- $configurations[$1.chomp][$2.chomp] = $3.chomp
26
- end
67
+ # Load .gitexternals
68
+ def load_configuration(file)
69
+ $configurations = {}
70
+ linesExit, lines = execute('git config', ['--list', '--file', file])
71
+ lines.each do |line|
72
+ if line =~ /^external\.([^$]+)\.([^=]+)=(.*)$/
73
+ $configurations[$1.chomp] ||= {}
74
+ $configurations[$1.chomp][$2.chomp] = $3.chomp
27
75
  end
28
76
  end
29
77
  end
30
78
 
79
+ # Print .gitexternals content
31
80
  def print_configuration
32
81
  $configurations.each do |name, config|
33
- puts name
82
+ puts "* #{name}"
34
83
  config.each do |key, value|
35
84
  puts "\t#{key}: #{value}"
36
85
  end
37
86
  end
38
87
  end
39
88
 
40
- def normalize_url(url)
41
- if url =~ /^\./
42
- origin_url = `git config --get remote.origin.url`.chomp
43
-
44
- unless origin_url =~ /^\w+:\/\//
45
- if origin_url =~ /^([^:\/]+):([^:]+)/
46
- origin_url = "ssh://#{$1}/#{$2}"
47
- end
48
- end
49
-
50
- require 'uri'
51
- uri = URI.parse URI.encode origin_url
52
- uri.path = File.expand_path(url, uri.path)
53
- uri.to_s
54
- else
55
- url
56
- end
57
- end
58
-
59
89
  # Check for uncommitted changes
60
90
  def uncommitted_changes?(path)
61
- diffExit, diff = execute("cd #{path}; git diff-index --quiet HEAD")
62
- return ((diffExit == 1) ? true : false)
91
+ diffExit, diff = execute('git diff-index', ['--quiet', 'HEAD'], path)
92
+ return ((diffExit == 1) ? true : false)
63
93
  end
64
94
 
65
95
  # Check if a branch exists
66
96
  def branch_exists(path, branch)
67
- branches = `cd #{path}; git branch`
97
+ branchesExit, branches = execute('git branch', [], path)
68
98
  regex = Regexp.new('[\\n\\s\\*]+' + Regexp.escape(branch.to_s) + '\\n')
69
99
  result = ((branches =~ regex) ? true : false)
70
100
  return result
@@ -77,17 +107,17 @@ def init_external(url, path, branch='master')
77
107
  else
78
108
  FileUtils.makedirs File.dirname(path)
79
109
  url = normalize_url url
80
- system "git clone #{url} #{path}"
110
+ execute('git clone', ["#{url}", path], Dir.getwd, '')
81
111
 
82
112
  # Create a local tracking branch if it doesn't exist already
83
113
  unless branch_exists(path, branch)
84
114
  puts "- Creating local tracking branch: #{branch} -> origin/#{branch}"
85
- system "cd #{path}; git branch checkout --track #{branch} origin/#{branch}"
115
+ execute('git branch', ['checkout', '--track', "#{branch}", "origin/#{branch}"], path)
86
116
  end
87
117
 
88
118
  # Check out the local branch
89
119
  puts "- Checkout local branch: #{branch}"
90
- system "cd #{path}; git checkout #{branch}"
120
+ execute('git checkout', ["#{branch}"], path)
91
121
  end
92
122
  end
93
123
 
@@ -98,20 +128,11 @@ def update_external(url, path, branch='master')
98
128
  if uncommitted_changes?(path)
99
129
  puts "#{path} - uncommitted changes detected, can not update repository"
100
130
  else
101
- `cd #{path}; git pull origin #{branch}`
131
+ execute('git pull', ['origin', "#{branch}"], path, '')
102
132
  end
103
133
  end
104
134
  end
105
135
 
106
- def execute(cmd)
107
- values = []
108
- IO.popen(cmd) do |f|
109
- tmp = f.gets
110
- values.push(tmp.gsub(/\r/, '').gsub(/\n/, '')) unless tmp.nil?
111
- end
112
- return $?.exitstatus, values
113
- end
114
-
115
136
  def command_status
116
137
  ok = 0
117
138
  broken = 0
@@ -120,8 +141,11 @@ def command_status
120
141
  url = config["url"]
121
142
  path = config["path"]
122
143
 
123
- gitBranchExit, gitBranch = execute("cd #{path} && git config \"branch.$(git symbolic-ref HEAD --short).merge\" | sed \"s/refs\\/heads\\///g\"")
124
- gitRemoteExit, gitRemote = execute("cd #{path} && git config \"remote.$(git config \"branch.$(git symbolic-ref HEAD --short).remote\").url\"");
144
+ symbolicExit, symbolic = execute('git symbolic-ref', ['HEAD', '--short'], path)
145
+ gitBranchExit, gitBranch = execute('git config', ["branch.#{symbolic}.merge"], path)
146
+ gitBranch = gitBranch.gsub('refs/heads/', '')
147
+ remoteExit, remote = execute('git config', ["branch.#{symbolic}.remote"], path)
148
+ gitRemoteExit, gitRemote = execute('git config', ["remote.#{remote}.url"], path)
125
149
 
126
150
  if uncommitted_changes?(path)
127
151
  changesString = "(Uncommitted Changes) "
@@ -129,20 +153,20 @@ def command_status
129
153
  changesString = ""
130
154
  end
131
155
 
132
- if gitBranchExit != 0 && gitRemoteExit != 0
133
- puts " ✗ #{path}: exit code #{gitBranchExit}, #{gitRemoteExit}"
156
+ if gitBranchExit != 0 && gitBranchExit != 1 && gitRemoteExit != 0 && gitRemoteExit != 1
157
+ puts " ✗ #{path} -- exit code #{gitBranchExit}, #{gitRemoteExit}"
134
158
  broken += 1
135
159
  else
136
- if branch == gitBranch[0]
137
- if url == gitRemote[0]
160
+ if branch == gitBranch
161
+ if url == gitRemote
138
162
  puts " ✓ #{path} #{changesString}"
139
163
  ok += 1
140
164
  else
141
- puts " ✗ #{path} #{changesString} -- expected url '#{url}' but was '#{gitRemote[0]}'"
165
+ puts " ✗ #{path} #{changesString} -- expected url '#{url}' but was '#{gitRemote}'"
142
166
  broken +=1
143
167
  end
144
168
  else
145
- puts " ✗ #{path} #{changesString} -- expected branch '#{branch}' but was '#{gitBranch[0]}'"
169
+ puts " ✗ #{path} #{changesString} -- expected branch '#{branch}' but was '#{gitBranch}'"
146
170
  broken +=1
147
171
  end
148
172
  end
@@ -152,23 +176,31 @@ end
152
176
 
153
177
  def command_add(url, path, branch='master')
154
178
  command_rm(path)
155
- `git config -f #{$externals_file} --add external.#{path}.path #{path}`
156
- `git config -f #{$externals_file} --add external.#{path}.url #{url}`
157
- `git config -f #{$externals_file} --add external.#{path}.branch #{branch}`
158
- `echo "#{path}" >> #{$ignore_file}`
179
+ execute('git config', ['--file', $externals_file, '--add', "external.#{path}.path", "#{path}"])
180
+ execute('git config', ['--file', $externals_file, '--add', "external.#{path}.url", "#{url}"])
181
+ execute('git config', ['--file', $externals_file, '--add', "external.#{path}.branch", "#{branch}"])
182
+
183
+ File.open("#{$ignore_file}", 'a+') do |f|
184
+ f.puts("#{path}")
185
+ end
186
+
159
187
  end
160
188
 
161
189
  def command_rm(path)
162
- if File.file? $externals_file
163
- `git config -f #{$externals_file} --unset external.#{path}.path`
164
- `git config -f #{$externals_file} --unset external.#{path}.url`
165
- `git config -f #{$externals_file} --unset external.#{path}.branch`
166
- `git config -f #{$externals_file} --remove-section external.#{path} > /dev/null 2>&1`
167
- File.delete $externals_file if `wc -l #{$externals_file}`.chomp.to_i == 0
168
- end
190
+ execute('git config', ['--file', $externals_file, '--unset', "external.#{path}.path"])
191
+ execute('git config', ['--file', $externals_file, '--unset', "external.#{path}.url"])
192
+ execute('git config', ['--file', $externals_file, '--unset', "external.#{path}.branch"])
193
+ execute('git config', ['--file', $externals_file, '--remove-section', "external.#{path}"])
194
+
169
195
  if File.file? $ignore_file
170
- # Remove entry from .gitignore file
171
- `perl -pi -e 's/\\Q#{path.gsub(/\//, '\/')}\\E\n//g' #{$ignore_file}`
196
+ ignores = []
197
+ File.open($ignore_file, 'r').each_line do |line|
198
+ ignores.push line.gsub(/\n/, '')
199
+ end
200
+ ignores.delete(path)
201
+ File.open($ignore_file, 'w+') do |file|
202
+ file.puts ignores
203
+ end
172
204
  end
173
205
  end
174
206
 
@@ -188,7 +220,9 @@ end
188
220
  def command_cmd(cmd)
189
221
  $configurations.each do |name, config|
190
222
  path = config['path']
191
- system("echo #{path}; cd #{path}; #{cmd}")
223
+ puts "- Executing '#{cmd}' in #{path}"
224
+ exitCmd, output = execute(cmd, [], path, '')
225
+ puts output
192
226
  end
193
227
  end
194
228
 
@@ -196,7 +230,54 @@ def command_list
196
230
  print_configuration
197
231
  end
198
232
 
199
- load_configuration
233
+ #########
234
+ def normalize_url(url)
235
+ if url =~ /^\./
236
+ origin_url = `git config --get remote.origin.url`.chomp
237
+
238
+ unless origin_url =~ /^\w+:\/\//
239
+ if origin_url =~ /^([^:\/]+):([^:]+)/
240
+ origin_url = "ssh://#{$1}/#{$2}"
241
+ end
242
+ end
243
+
244
+ require 'uri'
245
+ uri = URI.parse URI.encode origin_url
246
+ uri.path = File.expand_path(url, uri.path)
247
+ uri.to_s
248
+ else
249
+ url
250
+ end
251
+ end
252
+
253
+ def execute(cmd, opts=[], path=Dir.getwd, redirect='2>&1')
254
+ opts = [opts].flatten.map {|s| escape(s) }.join(' ')
255
+ cmd = "#{cmd} #{opts} #{redirect}"
256
+
257
+ out = nil
258
+ if path && (Dir.getwd != path)
259
+ Dir.chdir(path) { out = execute_now(cmd) }
260
+ else
261
+ out = execute_now(cmd)
262
+ end
263
+ return $?.exitstatus, out
264
+ end
265
+
266
+ def execute_now(cmd)
267
+ if block_given?
268
+ IO.popen(cmd)
269
+ else
270
+ `#{cmd}`.chomp
271
+ end
272
+ end
273
+
274
+ def escape(s)
275
+ escaped = s.to_s.gsub('\'', '\'\\\'\'')
276
+ %Q{"#{escaped}"}
277
+ end
278
+ #########
279
+
280
+ load_configuration $externals_file
200
281
 
201
282
  command=ARGV[0]
202
283
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: obazoud-git-external
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Daniel Cestari
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2012-10-24 00:00:00 Z
20
+ date: 2012-10-28 00:00:00 Z
21
21
  dependencies: []
22
22
 
23
23
  description: Extension for git which adds a command providing similar functionality to git submodules but without attaching each module to a single version
@@ -42,12 +42,14 @@ require_paths:
42
42
  required_ruby_version: !ruby/object:Gem::Requirement
43
43
  none: false
44
44
  requirements:
45
- - - ">="
45
+ - - ~>
46
46
  - !ruby/object:Gem::Version
47
- hash: 3
47
+ hash: 57
48
48
  segments:
49
- - 0
50
- version: "0"
49
+ - 1
50
+ - 8
51
+ - 7
52
+ version: 1.8.7
51
53
  required_rubygems_version: !ruby/object:Gem::Requirement
52
54
  none: false
53
55
  requirements: