forked from couchbase/manifest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-manifest.rb
executable file
·176 lines (154 loc) · 4.99 KB
/
fetch-manifest.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/usr/bin/env ruby
require 'rexml/document'
require 'rubygems'
require 'rake'
if ARGV.size < 1
puts "I need at least 1 arg: repo/manifest.xml"
puts "Ex: fetch-manifest.rb repo/manifest/default.xml [optional/override.xml] [optional/changes/out.txt]"
exit 1
end
path = ARGV[0] # Required. Example: some/repo/manifest/default.xml
more = ARGV[1] # Optional. Example: some/override.xml
emit = ARGV[2] # Optional. Example: output/changes-since-last-fetch.txt
oxml = ARGV[3] # Optional. Example: output/build-manifest.xml -- usable as input to repo tool.
volt = ARGV[4] # Optional. voltron revision, to be in emitted manifest
root = REXML::Document.new(File.new(path)).root
default = root.get_elements("//default")[0]
remotes_arr = []
remotes = {}
root.each_element("//remote") do |remote|
remotes_arr << remote.attributes['name']
remotes[remote.attributes['name']] = remote
end
projects_arr = []
projects = {}
root.each_element("//project") do |project|
projects_arr << project.attributes['name']
projects[project.attributes['name']] = project
end
# An override.xml file can be useful to specify different remote
# servers, such as to a closer, local git mirror.
#
if more
more_root = REXML::Document.new(File.new(more)).root
more_root.each_element("//remote") do |remote|
unless remotes[remote.attributes['name']]
remotes_arr << remote.attributes['name']
end
remotes[remote.attributes['name']] = remote
end
more_root.each_element("//project") do |project|
unless projects[project.attributes['name']]
projects_arr << project.attributes['name']
end
projects[project.attributes['name']] = project
end
end
changes = {}
projects_arr.each do |name|
project = projects[name]
path = project.attributes['path'] || project.attributes['name']
remote = remotes[project.attributes['remote'] || default.attributes['remote']]
fetch = remote.attributes['fetch']
revision = project.attributes['revision'] || default.attributes['revision']
print "#{name} #{revision}...\n"
unless File.directory?("#{path}/.git")
sh %{git clone #{fetch}#{name} #{path}} do |ok, res|
exit(false) unless ok
end
else
Dir.chdir(path) do
sh %{git remote update} do |ok, res|
exit(false) unless ok
end
end
end
Dir.chdir(path) do
curr = `git rev-parse HEAD`.chomp
sh %{git fetch --tags} do |ok, res|
exit(false) unless ok
end
if revision.include?(' ')
# Handle when the revision attribute is a full command, like...
#
# <project name="ep-engine"
# path="ep-engine"
# revision="git fetch ssh://[email protected]:29418/ep-engine refs/changes/79/5979/1 && git format-patch -1 --stdout FETCH_HEAD"/>
#
# This is useful for gerrit-based test builds.
#
sh revision do |ok, res|
exit(false) unless ok
end
else
sh %{git reset --hard origin/#{revision} || git reset --hard #{revision}} do |ok, res|
exit(false) unless ok
end
end
changes[name] =
"#{name} #{revision}...\n" +
`git log --pretty=format:'%h %an, %s' --abbrev-commit #{curr}..HEAD --`
end
project.each_element("copyfile") do |copyfile|
src = copyfile.attributes['src']
dest = copyfile.attributes['dest']
cp "#{path}/#{src}", dest
end
end
if emit
result = []
changes.keys.sort.each do |name|
result << "=================="
result << changes[name]
result << ""
end
result = result.join("\n") + "\n"
puts result
File.open(emit, 'w') {|o| o.write(result)} unless emit == '--'
end
if oxml
# Optionally emit a manifest.xml that can be used as input to
# the repo / fetch-manifest.rb tool.
#
File.open(oxml, 'w') do |o|
o.write "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
o.write "<manifest>\n"
remotes_arr.each do |name|
remote = remotes[name]
o.write " <remote name=\"#{name}\" fetch=\"#{remote.attributes['fetch']}\"/>\n"
end
o.write "\n"
o.write " <default remote=\"#{default.attributes['remote']}\" revision=\"#{default.attributes['revision']}\"/>\n"
o.write "\n"
projects_arr.each do |name|
project = projects[name]
remote = project.attributes['remote']
path = project.attributes['path'] || project.attributes['name']
Dir.chdir(path) do
curr = `git rev-parse HEAD`.chomp
o.write " <project name=\"#{name}\" path=\"#{path}\" revision=\"#{curr}\""
if remote
o.write "\n"
o.write " remote=\"#{remote}\""
end
has_body = false
project.each_element do |child|
o.write(">\n") unless has_body
o.write(" ")
o.write(child)
o.write("\n")
has_body = true
end
if has_body
o.write " </project>\n"
else
o.write "/>\n"
end
end
end
if volt
o.write " <!--\n <project name=\"voltron\" path=\"voltron\" revision=\"#{volt}\" />\n -->"
end
o.write "</manifest>\n"
end
end