mercurial-wrapper 0.8.4

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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/mercurial.rb +239 -0
  3. metadata +85 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 185e574bb08f9f83c0634b9b2fc9022a051f9cde
4
+ data.tar.gz: 5971916e25606783411b06a47c7722b2ba6fe974
5
+ SHA512:
6
+ metadata.gz: 418b3aa88f2d5e1c1eba66f4d6fc59040dc8379a25df75fe8c9954b46a20c6852646bd6688073d1dbe5ea69cf12c955f92927000a26aa125679a713539d59a53
7
+ data.tar.gz: 9296477ba41b5b07ff1c395ecf5f72652eb62171f0581bb6da56084c64ce13c457b3f2985b2dca3c23080cde3408892fe5d51b8a3afad4cdbd458cf73b1501f1
data/lib/mercurial.rb ADDED
@@ -0,0 +1,239 @@
1
+ require 'logger'
2
+ require 'tempfile'
3
+ require 'childprocess'
4
+
5
+ class MercurialOperationError < StandardError; end
6
+
7
+ class Mercurial
8
+
9
+ def initialize(log=nil)
10
+ @logger = log||Logger.new(STDOUT)
11
+
12
+ @settings = {
13
+ :basedir => '.',
14
+ :repo_name => '',
15
+ :default_branch => 'default',
16
+ :executable => '/usr/local/bin/hg',
17
+ :timeout => 300
18
+ }
19
+ end
20
+
21
+ def root
22
+ repo_name = ''
23
+ repo_name = "#{settings[:repo_name]}" unless settings[:repo_name] == '.'
24
+ "#{settings[:basedir]}/#{repo_name}"
25
+ end
26
+
27
+ def settings
28
+ @settings
29
+ end
30
+
31
+ def serve(repo_dir=root())
32
+ process = ChildProcess.build(hg, 'serve')
33
+ StringIO.open do |io|
34
+ io.sync
35
+ process.cwd = repo_dir
36
+ process.io.stdout = process.io.stderr = io
37
+ process.start
38
+ end
39
+ process
40
+ end
41
+
42
+ def init(basedir = nil)
43
+ cmd = [hg, 'init']
44
+ execute_command cmd, basedir
45
+ end
46
+
47
+ def add_remove(basedir = nil)
48
+ cmd = [hg, 'addremove']
49
+ execute_command cmd, basedir
50
+ end
51
+
52
+ def clone_repo(repo)
53
+ destination = settings[:repo_name]||settings[:basedir]
54
+ cmd = [hg, 'clone', repo, destination]
55
+ execute_command cmd, settings[:basedir]
56
+ end
57
+
58
+ def pull(options=nil)
59
+ cmd = [hg, 'pull']
60
+ cmd += ['-b', options[:branch]] if options and options[:branch]
61
+ execute_command cmd
62
+ end
63
+
64
+ def update_repo(options)
65
+ cmd = [hg, 'update']
66
+ cmd += ['-C', options[:branch]] if options[:branch]
67
+ cmd.push('--check') if options[:force] and !options[:branch]
68
+ execute_command cmd
69
+ end
70
+
71
+ def create_branch(branch, username, comment)
72
+ branch_cmd = [hg, 'branch', branch]
73
+ execute_command branch_cmd
74
+ commit_branch_cmd = [hg, 'ci', '-m', comment, '-u', username]
75
+ execute_command commit_branch_cmd
76
+ end
77
+
78
+ def current_branch
79
+ cmd = [hg, 'branch']
80
+ branch = execute_command cmd
81
+ branch.strip
82
+ end
83
+
84
+ def switch_branch(branch)
85
+ update_to_tip = [hg, 'update','-C', 'tip']
86
+ execute_command update_to_tip
87
+ pull
88
+ update_repo(:force => true)
89
+ update_cmd = [hg, 'update', '-C', branch]
90
+ execute_command update_cmd
91
+ end
92
+
93
+ def add(file_name, basedir=nil)
94
+ cmd = [hg, 'add', file_name]
95
+ execute_command cmd , basedir
96
+ end
97
+
98
+ def remove(file_name, basedir=nil)
99
+ cmd = [hg, 'remove', file_name]
100
+ execute_command cmd, basedir
101
+ end
102
+
103
+ def commit(username, comment, timestamp, basedir=nil)
104
+ cmd = [hg, 'commit', '-u', username, '-d', timestamp, '-m', comment]
105
+ execute_command cmd, basedir
106
+ end
107
+
108
+ def push(options=nil)
109
+ cmd = [hg, 'push']
110
+ cmd += ['-b', options[:branch]] if options and options[:branch]
111
+ execute_command cmd
112
+ end
113
+
114
+ def config( &block)
115
+ block.call @settings
116
+ self
117
+ end
118
+
119
+ def reset_settings
120
+ @settings = {
121
+ :basedir => '.',
122
+ :repo_name => '',
123
+ :default_branch => 'default',
124
+ :executable => '/usr/local/bin/hg',
125
+ :timeout => 300
126
+ }
127
+ end
128
+
129
+ def current_revision(file_name, basedir=nil)
130
+ check_file(file_name)
131
+ cmd = [hg, 'parents', file_name]
132
+ changeset = execute_command(cmd, basedir).split("\n").grep(/changeset/) do |line|
133
+ line.split()[1].strip
134
+ end
135
+ return changeset[0]
136
+ end
137
+
138
+ def previous_revision(file_name, current_rev, basedir=nil)
139
+ begin
140
+ cmd =[hg, 'parents', '-r', current_rev, file_name]
141
+ changeset = execute_command(cmd, basedir).split("\n").grep(/changeset/) do |line|
142
+ line.split()[1].strip
143
+ end
144
+ return changeset[0]
145
+ rescue
146
+ return 0 #we always default to 0 when there is no previous revision
147
+ end
148
+ end
149
+
150
+ def last_diff(file_name, base_rev=nil, basedir=nil)
151
+ current_rev = base_rev
152
+ begin
153
+ current_rev = current_revision(file_name, basedir) if current_rev.nil?
154
+ previous_rev = previous_revision(file_name, current_rev, basedir)
155
+ return diff(previous_rev, current_rev, file_name, basedir)
156
+ rescue
157
+ cmd =[hg, 'diff', '-r', '0', '-r', current_rev, file_name]
158
+ return execute_command cmd , basedir
159
+ end
160
+ end
161
+
162
+ def diff(from_rev, to_rev, file_name, basedir=nil)
163
+ cmd = [hg, 'diff', '-r', from_rev, '-r', to_rev, file_name]
164
+ result = execute_command cmd, basedir
165
+ return result.lines.to_a[1..-1].join if not result.start_with? 'diff'
166
+ return result
167
+ end
168
+
169
+ def apply_patch(file_name, patch_file, basedir=nil)
170
+ cmd = ['patch', file_name, patch_file]
171
+ return execute_command cmd , basedir
172
+ end
173
+
174
+ def update(file_name, branch)
175
+ switch_branch(branch)
176
+ end
177
+
178
+ def check_file(file_name, basedir=nil)
179
+ root_dir = basedir.nil? ? root : basedir
180
+ if not File.exists? File.join(root_dir, file_name) then
181
+ message = "Could not find #{file_name}. Are you sure you have set the correct basedir?"
182
+ raise Errno::ENOENT, message
183
+ end
184
+ end
185
+
186
+ def default_branch
187
+ settings[:default_branch]
188
+ end
189
+
190
+ def get_file_content(file_name, revision, basedir=nil)
191
+ cmd = [hg, 'cat', file_name, '-r', revision]
192
+ execute_command cmd , basedir
193
+ end
194
+
195
+ def merge(branch_name, basedir=nil)
196
+ cmd = [hg, 'branch', branch_name]
197
+ execute_command cmd , basedir
198
+ end
199
+
200
+ def status(basedir=nil)
201
+ cmd = [hg, 'status']
202
+ execute_command cmd , basedir
203
+ end
204
+
205
+ def revert(file_name, options = [], basedir=nil)
206
+ cmd = [hg, 'revert', '-r']
207
+ options.each {|option| cmd.push(option.to_s)}
208
+ cmd.push file_name
209
+ execute_command cmd , basedir
210
+ end
211
+
212
+ def hg
213
+ settings[:executable]
214
+ end
215
+
216
+ def execute_command(cmd, basedir = nil, timeout=nil)
217
+ basedir = basedir.nil? ? root : basedir
218
+ @logger.debug("Executing command #{cmd.join(' ')} | CWD: #{basedir}")
219
+ command_timeout = timeout||settings[:timeout]
220
+ out = Tempfile.new('hg_cmd')
221
+ process = ChildProcess.build(*cmd)
222
+ process.cwd = basedir
223
+ process.io.stdout = process.io.stderr = out
224
+ process.start
225
+ process.poll_for_exit(command_timeout)
226
+ out.rewind
227
+ result = out.read
228
+ raise MercurialOperationError, "Could not successfully execute command '#{cmd}'\n#{result} exit_code = #{process.exit_code}" if process.exit_code != 0
229
+ result
230
+ rescue ChildProcess::TimeoutError
231
+ raise MercurialOperationError, "TIMEOUT[#{command_timeout}]! Could not successfully execute command '#{cmd}'"
232
+ ensure
233
+ out.close if out
234
+ out.unlink if out
235
+ end
236
+
237
+ end
238
+
239
+
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mercurial-wrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.8.4
5
+ platform: ruby
6
+ authors:
7
+ - Fabio Neves
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: childprocess
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.5.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.5.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: A simple wrapper around HG command line tool
56
+ email: infrastructure@datalex.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/mercurial.rb
62
+ homepage: https://github.com/datalex-opensource/mercurial-wrapper
63
+ licenses: []
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.2.2
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Mercurial command line ruby wrapper
85
+ test_files: []