forked from dustin/snippets
/
bundle-replicate
executable file
·45 lines (37 loc) · 1.12 KB
/
bundle-replicate
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
#!/usr/bin/env ruby
def get_stored_state(topdir)
d=Dir.open(topdir)
latest=(d.grep(/.hg$/).sort {|x,y| y <=> x}).first || 'null-null-null.hg'
d.close
File.basename(latest, '.hg').split /-/
end
def first_successful(cmds)
cmds.each do |cmd|
result=`#{cmd}`
return result.split().first if $?.success?
end
raise "Cannot identify tree version."
end
def get_latest_repo_version
first_successful(['hg id -r qparent', 'hg id -r tip'])
end
# main
topdir, devdir=$*
Dir.chdir devdir
ver, min, max = get_stored_state(topdir)
latest_ver=get_latest_repo_version
puts "Latest is #{latest_ver} (we've got #{max})"
if ver != 'null' && max != latest_ver
nextid=Kernel.sprintf "%04d", ver.to_i+1
puts "Need to update to #{nextid} for #{latest_ver}"
outfile=File.join topdir, "#{nextid}-#{max}-#{latest_ver}.hg"
unless system('hg', 'bundle', '-r', latest_ver, '--base', max, outfile)
raise "Crap. Bundle failed."
end
elsif ver == 'null'
puts "Bootstrapping..."
outfile=File.join topdir, "0000-base-#{latest_ver}.hg"
unless system('hg', 'bundle', '-r', latest_ver, '--base', 'null', outfile)
raise "Crap. Bundle failed."
end
end