right_scraper 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -47,52 +47,71 @@ module RightScale
47
47
  msg = @incremental ? 'Pulling ' : 'Cloning '
48
48
  msg += "git repository '#{@repo.display_name}'"
49
49
  @callback.call(msg, is_step=true) if @callback
50
- ssh_cmd = ssh_command
51
- res = ''
52
- is_tag = nil
50
+ ssh_cmd = ssh_command
51
+ res = ''
52
+ is_tag = nil
53
53
  is_branch = nil
54
+ update_failed = false
54
55
 
55
56
  if @incremental
56
57
  Dir.chdir(@current_repo_dir) do
57
- is_tag, is_branch, res = git_fetch(ssh_cmd)
58
- if !is_tag && !is_branch
59
- @callback.call('Nothing to update: repo tag refers to neither a branch nor a tag', is_step=false)
60
- return true
61
- end
62
- if is_tag && is_branch
63
- @errors << 'Repository tag ambiguous: could be git tag or git branch'
64
- else
65
- tag = @repo.tag.nil? || @repo.tag.empty? ? 'master' : @repo.tag
66
- res += `git checkout #{tag} 2>&1`
67
- if $? != 0
68
- @callback.call("Failed to update repo: #{res}, falling back to cloning", is_step=false) if @callback
69
- FileUtils.rm_rf(@current_repo_dir)
70
- @incremental = false
58
+ analysis = git_fetch_and_analyze(ssh_cmd)
59
+ if analysis[:success]
60
+ is_tag = analysis[:tag]
61
+ is_branch = analysis[:branch]
62
+ if !is_tag && !is_branch
63
+ @callback.call('Nothing to update: repo tag refers to neither a branch nor a tag', is_step=false)
64
+ return true
65
+ end
66
+ if is_tag && is_branch
67
+ @errors << 'Repository tag ambiguous: could be git tag or git branch'
68
+ else
69
+ tag = @repo.tag.nil? || @repo.tag.empty? ? 'master' : @repo.tag
70
+ res = `git checkout #{tag} 2>&1`
71
+ if $? != 0
72
+ @callback.call("Failed to checkout #{tag}: #{res}, falling back to cloning", is_step=false) if @callback
73
+ update_failed = true
74
+ end
71
75
  end
76
+ else
77
+ @callback.call("Failed to update repo: #{analysis[:output]}, falling back to cloning", is_step=false) if @callback
78
+ update_failed = true
72
79
  end
73
80
  end
74
81
  end
82
+ if update_failed
83
+ FileUtils.rm_rf(@current_repo_dir)
84
+ @incremental = false
85
+ end
75
86
  if !@incremental && succeeded?
76
87
  res += `#{ssh_cmd} git clone --quiet --depth 1 "#{@repo.url}" "#{@current_repo_dir}" 2>&1`
77
- @errors << res if $? != 0
88
+ @errors << "Failed to clone repo: #{res}" if $? != 0
78
89
  if !@repo.tag.nil? && !@repo.tag.empty? && @repo.tag != 'master' && succeeded?
79
90
  Dir.chdir(@current_repo_dir) do
80
91
  if is_tag.nil?
81
- is_tag, is_branch, out = git_fetch(ssh_cmd)
82
- res += out
92
+ analysis = git_fetch_and_analyze(ssh_cmd)
93
+ if analysis[:success]
94
+ is_tag = analysis[:tag]
95
+ is_branch = analysis[:branch]
96
+ res += analysis[:output]
97
+ else
98
+ @errors << "Failed to analyze repo: #{res}"
99
+ end
83
100
  end
84
- if is_tag && is_branch
85
- @errors << 'Repository tag ambiguous: could be git tag or git branch'
86
- elsif is_branch
87
- res += `git branch #{@repo.tag} origin/#{@repo.tag} 2>&1`
88
- @errors << res if $? != 0
89
- elsif !is_tag # Not a branch nor a tag, SHA ref? fetch everything so we have all SHAs
90
- res += `#{ssh_cmd} git fetch origin master --depth #{2**31 - 1} 2>&1`
91
- @errors << res if $? != 0
92
- end
93
- if succeeded?
94
- res += `git checkout #{@repo.tag} 2>&1`
95
- @errors << res if $? != 0
101
+ if succeded?
102
+ if is_tag && is_branch
103
+ @errors << 'Repository tag ambiguous: could be git tag or git branch'
104
+ elsif is_branch
105
+ res += `git branch #{@repo.tag} origin/#{@repo.tag} 2>&1`
106
+ @errors << res if $? != 0
107
+ elsif !is_tag # Not a branch nor a tag, SHA ref? fetch everything so we have all SHAs
108
+ res += `#{ssh_cmd} git fetch origin master --depth #{2**31 - 1} 2>&1`
109
+ @errors << res if $? != 0
110
+ end
111
+ if succeeded?
112
+ res += `git checkout #{@repo.tag} 2>&1`
113
+ @errors << res if $? != 0
114
+ end
96
115
  end
97
116
  end
98
117
  end
@@ -189,16 +208,20 @@ module RightScale
189
208
  # ssh_cmd(String):: SSH command to be used with git if any
190
209
  #
191
210
  # === Return
192
- # res(Array)::
193
- # - res[0] is true if git repo has a tag with a name corresponding to the repository tag
194
- # - res[1] is true if git repo has a branch with a name corresponding to the repository tag
195
- # - res[2] contains the git output
196
- def git_fetch(ssh_cmd)
197
- return [ false, true, "" ] if @repo.tag.nil? || @repo.tag.empty? || @repo.tag == 'master'
211
+ # res(Hash)::
212
+ # - resp[:success]:: true if fetch was successful, false otherwise
213
+ # - res[:output] contains the git output
214
+ # - res[:tag]:: is true if git repo has a tag with a name corresponding to the repository tag
215
+ # - res[:branch] is true if git repo has a branch with a name corresponding to the repository tag
216
+ def git_fetch_and_analyze(ssh_cmd)
198
217
  output = `#{ssh_cmd} git fetch --tags --depth 1 2>&1`
199
- is_tag = `git tag`.split("\n").include?(@repo.tag)
200
- is_branch = `git branch -r`.split("\n").map { |t| t.strip }.include?("origin/#{@repo.tag}")
201
- res = [ is_tag, is_branch, output ]
218
+ success = [0, 1].include? $?.exitstatus # git fetch returns 1 when there is nothing to fetch
219
+ is_tag = is_branch = false
220
+ if success
221
+ is_tag = `git tag`.split("\n").include?(@repo.tag)
222
+ is_branch = `git branch -r`.split("\n").map { |t| t.strip }.include?("origin/#{@repo.tag}")
223
+ end
224
+ { :success => success, :output => output, :tag => is_tag, :branch => is_branch }
202
225
  end
203
226
 
204
227
  end
@@ -23,7 +23,7 @@ require 'rubygems'
23
23
 
24
24
  spec = Gem::Specification.new do |spec|
25
25
  spec.name = 'right_scraper'
26
- spec.version = '1.0.2'
26
+ spec.version = '1.0.3'
27
27
  spec.authors = ['Raphael Simon']
28
28
  spec.email = 'raphael@rightscale.com'
29
29
  spec.homepage = 'https://github.com/rightscale/right_scraper'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: right_scraper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Raphael Simon
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-02 00:00:00 -08:00
12
+ date: 2010-02-04 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15