hg-port 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/MIT-LICENSE.txt +21 -0
  2. data/bin/hg-port +207 -0
  3. metadata +48 -0
data/MIT-LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ Copyright (c) 2013 Richard Cook - http://rcook.org/
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
data/bin/hg-port ADDED
@@ -0,0 +1,207 @@
1
+ #!/usr/bin/env ruby
2
+ require 'pathname'
3
+ require 'tempfile'
4
+
5
+ THIS_PATH = Pathname.new(__FILE__).realpath
6
+ THIS_DIR = THIS_PATH.dirname
7
+ THIS_BASE_NAME = THIS_PATH.basename
8
+
9
+ module Helper
10
+ def self.fail(message, lines)
11
+ display_output(lines)
12
+ raise RuntimeError.new(message)
13
+ end
14
+
15
+ def self.display_output(lines)
16
+ lines.each do |line|
17
+ puts "> #{line}"
18
+ end
19
+ end
20
+
21
+ def self.display_usage(versions)
22
+ $stderr.puts <<-EOS
23
+ Usage:
24
+ #{THIS_BASE_NAME} [<arg0> ... <argN-1>]
25
+
26
+ Switches:
27
+ --merge-version <version-branch>
28
+
29
+ Available versions:
30
+ #{versions.collect { |version| " #{version}" }.join("\n")}
31
+
32
+ EOS
33
+ end
34
+ end
35
+
36
+ module HGHelper
37
+ def self.run_hg(dir, command, fail_on_error = true)
38
+ temp_file = Tempfile.new('hg-port-run-hg')
39
+ status = nil
40
+ lines = nil
41
+ begin
42
+ temp_file.close
43
+ status = system("(cd #{dir} && #{command}) > #{temp_file.path} 2>&1")
44
+ lines = File.readlines(temp_file.path).collect(&:chomp)
45
+ if fail_on_error && !status
46
+ Helper.fail("Command \"#{command}\" failed in directory \"#{dir}\"", lines)
47
+ end
48
+ ensure
49
+ temp_file.unlink
50
+ end
51
+ [status, lines]
52
+ end
53
+
54
+ def self.get_branches(dir)
55
+ status, lines = run_hg(dir, 'hg branches')
56
+ lines.collect { |line| line.split.first }
57
+ end
58
+
59
+ def self.current_branch(dir)
60
+ status, lines = run_hg(dir, 'hg branch')
61
+ lines.first
62
+ end
63
+ end
64
+
65
+
66
+ class Version
67
+ include Comparable
68
+
69
+ attr_reader :major, :minor
70
+
71
+ def self.parse(str)
72
+ return nil if str !~ /^v([0-9]+)\.([0-9]+)$/
73
+ new(Integer($1), Integer($2))
74
+ end
75
+
76
+ def <=>(other)
77
+ return 1 if other.nil?
78
+
79
+ delta = major - other.major
80
+ return 1 if delta > 0
81
+ return -1 if delta < 0
82
+
83
+ delta = minor - other.minor
84
+ return 1 if delta > 0
85
+ return -1 if delta < 0
86
+ 0
87
+ end
88
+
89
+ def to_s
90
+ "v#{major}.#{minor}"
91
+ end
92
+
93
+ private
94
+
95
+ def initialize(major, minor)
96
+ @major = major
97
+ @minor = minor
98
+ end
99
+ end
100
+
101
+ class Options
102
+ attr_reader :merge_version
103
+
104
+ def self.parse(args, versions)
105
+ merge_version = nil
106
+ while arg = args.shift
107
+ case arg
108
+ when '--merge-version' then
109
+ temp = args.shift or raise RuntimeError.new('Value required for --merge-version')
110
+ merge_version = Version.parse(temp) or raise RuntimeError.new("Invalid value \"#{temp}\" for --merge-version")
111
+ else
112
+ raise RuntimeError.new("Unsupported switch \"#{arg}\"")
113
+ end
114
+ end
115
+
116
+ if !merge_version || !versions.include?(merge_version)
117
+ Helper.display_usage(versions)
118
+ return nil
119
+ end
120
+
121
+ new(merge_version)
122
+ end
123
+
124
+ private
125
+
126
+ def initialize(merge_version)
127
+ @merge_version = merge_version
128
+ end
129
+ end
130
+
131
+ def get_versions(branches)
132
+ branches.collect { |branch| Version.parse(branch)}.compact.sort
133
+ end
134
+
135
+ def forward_port(dir, merge_version, version)
136
+ # Move to the target branch.
137
+ HGHelper.run_hg(dir, "hg update #{version}")
138
+
139
+ # Build commit message from preview
140
+ status, lines = HGHelper.run_hg(dir, "hg merge --preview #{merge_version}")
141
+ puts "LINES: #{lines.inspect}"
142
+ commit_message = <<-EOS
143
+ Automatic forward port from #{merge_version} to #{version}
144
+
145
+ ----------
146
+
147
+ Changes merged by this commit:
148
+ #{lines.collect { |line| " #{line}" }.join("\n")}
149
+ EOS
150
+
151
+ temp_file = Tempfile.new('hg-port-commit-message')
152
+ begin
153
+ temp_file.write commit_message
154
+ temp_file.close
155
+
156
+ # Perform the merge for real
157
+ status, lines = HGHelper.run_hg(dir, "hg merge #{merge_version}", false)
158
+ if status
159
+ status, lines = HGHelper.run_hg(dir, "hg commit --logfile #{temp_file.path} && hg tip --template '{node}'")
160
+ revision_id = lines.last
161
+ puts "Changes automatically ported from #{merge_version} to #{version} (#{revision_id[0...12]})"
162
+ true
163
+ elsif lines.include?('abort: merging with a working directory ancestor has no effect')
164
+ puts "No changes required porting from #{merge_version} to #{version}"
165
+ true
166
+ else
167
+ Helper.display_output(lines)
168
+
169
+ puts <<-EOS
170
+ Automatic forward port abandoned due to merge conflict.
171
+ Please resolve conflict manually and commit your changes.
172
+ Suggested commit command:
173
+
174
+ hg commit -m "Manual forward port form #{merge_version} to #{version}"
175
+
176
+ Resume the automatic forward port with the following command:
177
+
178
+ #{THIS_BASE_NAME} --merge-version #{version}
179
+
180
+ EOS
181
+ false
182
+ end
183
+ ensure
184
+ temp_file.unlink
185
+ end
186
+
187
+ end
188
+
189
+ dir = '.'
190
+
191
+ versions = get_versions(HGHelper.get_branches(dir))
192
+ options = Options.parse(ARGV.clone, versions)
193
+ exit 1 if options.nil?
194
+
195
+ unless options.merge_version.to_s == HGHelper.current_branch(dir)
196
+ raise RuntimeError.new("Check out merge version \"#{options.merge_version}\" before attempting a forward port")
197
+ end
198
+
199
+ merge_and_later_versions = versions.select { |version| version >= options.merge_version }
200
+ merge_and_later_versions.each_with_index do |version, index|
201
+ next if index == 0
202
+ unless forward_port(dir, merge_and_later_versions[index - 1], version)
203
+ exit 1
204
+ end
205
+ end
206
+ puts 'Done'
207
+
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hg-port
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Richard Cook
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Script to automate forward porting in Mercurial
15
+ email: rcook@rcook.org
16
+ executables:
17
+ - hg-port
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - MIT-LICENSE.txt
22
+ - bin/hg-port
23
+ homepage: https://bitbucket.org/rcook/hg-port
24
+ licenses:
25
+ - MIT
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.23
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: hg-port
48
+ test_files: []