Class: Hglib::Repo

Inherits:
Object
  • Object
show all
Extended by:
Loggability
Includes:
VersionInfo
Defined in:
lib/hglib/repo.rb

Defined Under Namespace

Classes: Bookmark, Id, LogEntry, StatusEntry, Tag

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from VersionInfo

#extension_enabled?, #extension_versions, #version, #versions

Constructor Details

#initialize(path) ⇒ Repo

Create a new Repo object that will operate on the Mercurial repo at the specified path.



29
30
31
32
# File 'lib/hglib/repo.rb', line 29

def initialize( path )
	@path = Pathname( path )
	@server = nil
end

Instance Attribute Details

#pathObject (readonly)

The path to the repository



41
42
43
# File 'lib/hglib/repo.rb', line 41

def path
  @path
end

Instance Method Details

#add(*files, **options) ⇒ Object

Schedule the given files to be version controlled and added to the repository on the next commit. To undo an add before that, see #forget.

If no files are given, add all files to the repository (except files matching “.hgignore”).

Returns true if all files are successfully added.



99
100
101
102
# File 'lib/hglib/repo.rb', line 99

def add( *files, **options )
	self.server.run( :add, *files, **options )
	return true
end

#addremove(*files, **options) ⇒ Object Also known as: add_remove, addr

Add all new files and remove all missing files from the repository.

Unless files are given, new files are ignored if they match any of the patterns in “.hgignore”. As with #add, these changes take effect at the next commit.

Use the :similarity option to detect renamed files. This option takes a percentage between 0 (disabled) and 100 (files must be identical) as its value. With a value greater than 0, this compares every removed file with every added file and records those similar enough as renames. Detecting renamed files this way can be expensive. After using this option, you can call #status with the :copies options to check which files were identified as moved or renamed. If not specified, :similarity defaults to 100 and only renames of identical files are detected.

Returns true if all files are successfully added.



121
122
123
124
# File 'lib/hglib/repo.rb', line 121

def addremove( *files, **options )
	self.server.run( :addremove, *files, **options )
	return true
end

#bookmark(*names, **options) ⇒ Object

Create new bookmarks with the specified names.



185
186
187
188
189
190
# File 'lib/hglib/repo.rb', line 185

def bookmark( *names, **options )
	raise "expected at least one bookmark name" if names.empty?

	self.server.run( :bookmark, *names, **options )
	return true
end

#bookmarksObject

Return a Hglib::Repo::Bookmark object for each bookmark in the repo.



194
195
196
197
198
# File 'lib/hglib/repo.rb', line 194

def bookmarks
	options = { list: true }
	response = self.server.run_with_json_template( :bookmarks, **options )
	return response.map {|bk| Hglib::Repo::Bookmark.new(self, **bk) }
end

#clean?Boolean

Returns true if the repo has no outstanding changes.

Returns:

  • (Boolean)


257
258
259
# File 'lib/hglib/repo.rb', line 257

def clean?
	return !self.dirty?
end

#commit(*files, **options) ⇒ Object

Commit the specified files with the given options.



130
131
132
133
# File 'lib/hglib/repo.rb', line 130

def commit( *files, **options )
	self.server.run( :commit, *files, **options )
	return true
end

#config(untrusted: false) ⇒ Object

Fetch the current global Mercurial config and return it as an Hglib::Config object.



203
204
205
206
207
# File 'lib/hglib/repo.rb', line 203

def config( untrusted: false )
	options = { untrusted: untrusted }
	config = self.server.run_with_json_template( :showconfig, **options )
	return Hglib::Config.new( config )
end

#diff(*files, **options) ⇒ Object

Return a String showing differences between revisions for the specified files in the unified diff format.



87
88
89
# File 'lib/hglib/repo.rb', line 87

def diff( *files, **options )
	return self.server.run( :diff, *files, **options )
end

#dirty?Boolean

Returns true if the repo has outstanding changes.

Returns:

  • (Boolean)


251
252
253
# File 'lib/hglib/repo.rb', line 251

def dirty?
	return self.identify.dirty?
end

#draft?(revset = nil) ⇒ Boolean

Returns true if all of the changesets in the specified revset (or the current changeset if no revset is given) are in the draft phase.

Returns:

  • (Boolean)


271
272
273
# File 'lib/hglib/repo.rb', line 271

def draft?( revset=nil )
	return self.phase( revset ).values.all?( :draft )
end

#identify(source = nil, **options) ⇒ Object Also known as: identity

Return a Hglib::Repo::Id that identifies the repository state at the specified revision, or the current revision if unspecified. A revision of ‘.` identifies the working directory parent without uncommitted changes.



64
65
66
67
68
69
# File 'lib/hglib/repo.rb', line 64

def identify( source=nil, **options )
	response = self.server.run_with_json_template( :identify, source, **options )

	data = response.first
	return Hglib::Repo::Id.new( **data )
end

#log(*files, **options) ⇒ Object

Return an Array of Hglib::Repo::LogEntry objects that describes the revision history of the specified files or the entire project.



76
77
78
79
80
81
82
# File 'lib/hglib/repo.rb', line 76

def log( *files, **options )
	options[:graph] = false

	entries = self.server.run_with_json_template( :log, *files, **options )

	return entries.map {|entry| Hglib::Repo::LogEntry.new(entry) }
end

#pathsObject

Fetch a Hash of aliases for remote repositories.



211
212
213
214
215
216
# File 'lib/hglib/repo.rb', line 211

def paths
	response = self.server.run_with_json_template( :paths )
	return response.each_with_object({}) do |entry, hash|
		hash[ entry[:name].to_sym ] = URI( entry[:url] )
	end
end

#phase(revset = nil, **options) ⇒ Object

Set or show the current phase name for a revset.

With no revset, operates on the current changeset.

You can set the phase of the specified revisions by passing one of the following options:

  • p: true / public: true

  • d: true / draft: true

  • s: true / secret: true

Returns a Hash of <local revision number> => <phase as a Symbol>. Setting the phase returns an empty Hash on success, and raises if there was a problem setting the phase.



233
234
235
236
237
238
239
240
241
242
243
# File 'lib/hglib/repo.rb', line 233

def phase( revset=nil, **options )
	response = self.server.run( :phase, revset, **options )

	return {} if response.empty?

	return response.lines.each_with_object({}) do |line, hash|
		m = line.match( /^(?<revnum>\d+): (?<phase>\w+)/ ) or
			raise "Couldn't parse phase response %p" % [ line ]
		hash[ m[:revnum].to_i ] = m[:phase].to_sym
	end
end

#public?(revset = nil) ⇒ Boolean

Returns true if all of the changesets in the specified revset (or the current changeset if no revset is given) are in the public phase.

Returns:

  • (Boolean)


264
265
266
# File 'lib/hglib/repo.rb', line 264

def public?( revset=nil )
	return self.phase( revset ).values.all?( :public )
end

#pull(source = nil, **options) ⇒ Object

Pull changes from the specified source (which defaults to the default path) into the local repository.



138
139
140
141
# File 'lib/hglib/repo.rb', line 138

def pull( source=nil, **options )
	self.server.run( :pull, source, **options )
	return true
end

#pull_update(source = nil, **options) ⇒ Object

Pull changes from the specified source into the local repository and update to the new branch head if new descendents were pulled.



146
147
148
149
# File 'lib/hglib/repo.rb', line 146

def pull_update( source=nil, **options )
	options[:update] = true
	return self.pull( source, **options )
end

#push(destination = nil, **options) ⇒ Object

Push changes to the specified destination.



160
161
162
163
# File 'lib/hglib/repo.rb', line 160

def push( destination=nil, **options )
	self.server.run( :push, destination, **options )
	return true
end

#secret?(revset = nil) ⇒ Boolean

Returns true if all of the changesets in the specified revset (or the current changeset if no revset is given) are in the secret phase.

Returns:

  • (Boolean)


278
279
280
# File 'lib/hglib/repo.rb', line 278

def secret?( revset=nil )
	return self.phase( revset ).values.all?( :secret )
end

#serverObject

Return the Hglib::Server started for this Repo, creating it if necessary.



45
46
47
# File 'lib/hglib/repo.rb', line 45

def server
	return @server ||= self.create_server
end

#status(*args, **options) ⇒ Object Also known as: stat

Return a Hash of the status of the files in the repo, keyed by Pathname of the file. An empty Hash is returned if there are no files with one of the requested statuses.



53
54
55
56
57
# File 'lib/hglib/repo.rb', line 53

def status( *args, **options )
	response = self.server.run_with_json_template( :status, *args, **options )

	return response.map {|entry| Hglib::Repo::StatusEntry.new(entry) }
end

#tag(*names, **options) ⇒ Object

Name a revision using names.



167
168
169
170
171
172
173
174
# File 'lib/hglib/repo.rb', line 167

def tag( *names, **options )
	raise "expected at least one tag name" if names.empty?

	response = self.server.run( :tag, *names, **options )
	self.logger.debug "Got TAGS response: %p" % [ response ]

	return true
end

#tagsObject

Return a Hglib::Repo::Tag object for each tag in the repo.



178
179
180
181
# File 'lib/hglib/repo.rb', line 178

def tags
	response = self.server.run_with_json_template( :tags )
	return response.flatten.map {|tag| Hglib::Repo::Tag.new(self, **tag) }
end

#update(rev = nil, **options) ⇒ Object

Update the working directory or switch revisions.



153
154
155
156
# File 'lib/hglib/repo.rb', line 153

def update( rev=nil, **options )
	self.server.run( :update, rev, **options )
	return true
end