Class: Grit::Repo

Inherits:
Object
  • Object
show all
Defined in:
lib/grit/lib/grit/repo.rb

Constant Summary collapse

DAEMON_EXPORT_FILE =
'git-daemon-export-ok'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Repo

Create a new Repo instance

+path+ is the path to either the root git directory or the bare git repo
+options+ :is_bare force to load a bare repo

Examples

g = Repo.new("/Users/tom/dev/grit")
g = Repo.new("/Users/tom/public/grit.git")

Returns Grit::Repo



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/grit/lib/grit/repo.rb', line 23

def initialize(path, options = {})
  epath = File.expand_path(path)
  
  if File.exist?(File.join(epath, '.git'))
    self.working_dir = epath
    self.path = File.join(epath, '.git')
    @bare = false
  elsif File.exist?(epath) && (epath =~ /\.git$/ || options[:is_bare])
    self.path = epath
    @bare = true
  elsif File.exist?(epath)
    raise InvalidGitRepositoryError.new(epath)
  else
    raise NoSuchPathError.new(epath)
  end
  
  self.git = Git.new(self.path)
end

Instance Attribute Details

#bareObject (readonly)

Returns the value of attribute bare.



9
10
11
# File 'lib/grit/lib/grit/repo.rb', line 9

def bare
  @bare
end

#gitObject

The git command line interface object



12
13
14
# File 'lib/grit/lib/grit/repo.rb', line 12

def git
  @git
end

#pathObject

The path of the git repo as a String



7
8
9
# File 'lib/grit/lib/grit/repo.rb', line 7

def path
  @path
end

#working_dirObject

Returns the value of attribute working_dir.



8
9
10
# File 'lib/grit/lib/grit/repo.rb', line 8

def working_dir
  @working_dir
end

Class Method Details

.init(path) ⇒ Object

Does nothing yet…



43
44
45
46
47
48
# File 'lib/grit/lib/grit/repo.rb', line 43

def self.init(path)
  # !! TODO !!
  # create directory
  # generate initial git directory
  # create new Grit::Repo on that dir, return it
end

.init_bare(path, git_options = {}, repo_options = {}) ⇒ Object

Initialize a bare git repository at the given path

+path+ is the full path to the repo (traditionally ends with /<name>.git)
+options+ is any additional options to the git init command

Examples

Grit::Repo.init_bare('/var/git/myrepo.git')

Returns Grit::Repo (the newly created repo)



293
294
295
296
297
# File 'lib/grit/lib/grit/repo.rb', line 293

def self.init_bare(path, git_options = {}, repo_options = {})
  git = Git.new(path)
  git.init(git_options)
  self.new(path, repo_options)
end

Instance Method Details

#add(*files) ⇒ Object

Adds files to the index



112
113
114
# File 'lib/grit/lib/grit/repo.rb', line 112

def add(*files)
  self.git.add({}, *files.flatten)
end

#add_allObject

Adds all files to the index



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

def add_all
  self.git.add({:all => true})
end

#add_updatedObject

Adds changed, tracked files to the index



117
118
119
# File 'lib/grit/lib/grit/repo.rb', line 117

def add_updated
  self.git.add({:update => true})
end

#alternatesObject

The list of alternates for this repo

Returns Array (pathnames of alternates)



383
384
385
386
387
388
389
390
391
# File 'lib/grit/lib/grit/repo.rb', line 383

def alternates
  alternates_path = File.join(self.path, *%w{objects info alternates})
  
  if File.exist?(alternates_path)
    File.read(alternates_path).strip.split("\n")
  else
    []
  end
end

#alternates=(alts) ⇒ Object

Sets the alternates

+alts+ is the Array of String paths representing the alternates

Returns nothing



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/grit/lib/grit/repo.rb', line 397

def alternates=(alts)
  alts.each do |alt|
    unless File.exist?(alt)
      raise "Could not set alternates. Alternate path #{alt} must exist"
    end
  end
  
  if alts.empty?
    File.open(File.join(self.path, *%w{objects info alternates}), 'w') do |f|
      f.write ''
    end
  else
    File.open(File.join(self.path, *%w{objects info alternates}), 'w') do |f|
      f.write alts.join("\n")
    end
  end
end

#archive_tar(treeish = 'master', prefix = nil) ⇒ Object

Archive the given treeish

+treeish+ is the treeish name/id (default 'master')
+prefix+ is the optional prefix

Examples

repo.archive_tar
# => <String containing tar archive>

repo.archive_tar('a87ff14')
# => <String containing tar archive for commit a87ff14>

repo.archive_tar('master', 'myproject/')
# => <String containing tar archive and prefixed with 'myproject/'>

Returns String (containing tar archive)



326
327
328
329
330
# File 'lib/grit/lib/grit/repo.rb', line 326

def archive_tar(treeish = 'master', prefix = nil)
  options = {}
  options[:prefix] = prefix if prefix
  self.git.archive(options, treeish)
end

#archive_tar_gz(treeish = 'master', prefix = nil) ⇒ Object

Archive and gzip the given treeish

+treeish+ is the treeish name/id (default 'master')
+prefix+ is the optional prefix

Examples

repo.archive_tar_gz
# => <String containing tar.gz archive>

repo.archive_tar_gz('a87ff14')
# => <String containing tar.gz archive for commit a87ff14>

repo.archive_tar_gz('master', 'myproject/')
# => <String containing tar.gz archive and prefixed with 'myproject/'>

Returns String (containing tar.gz archive)



347
348
349
350
351
# File 'lib/grit/lib/grit/repo.rb', line 347

def archive_tar_gz(treeish = 'master', prefix = nil)
  options = {}
  options[:prefix] = prefix if prefix
  self.git.archive(options, treeish, "| gzip")
end

#archive_to_file(treeish = 'master', prefix = nil, filename = 'archive.tar.gz') ⇒ Object

run archive directly to a file



354
355
356
357
358
# File 'lib/grit/lib/grit/repo.rb', line 354

def archive_to_file(treeish = 'master', prefix = nil, filename = 'archive.tar.gz')
  options = {}
  options[:prefix] = prefix if prefix
  self.git.archive(options, treeish, "| gzip > #{filename}")
end

#blame(file, commit = nil) ⇒ Object



57
58
59
# File 'lib/grit/lib/grit/repo.rb', line 57

def blame(file, commit = nil)
  Blame.new(self, file, commit)
end

#blame_tree(commit, path = nil) ⇒ Object



139
140
141
142
143
144
145
146
147
# File 'lib/grit/lib/grit/repo.rb', line 139

def blame_tree(commit, path = nil)
  commit_array = self.git.blame_tree(commit, path)
  
  final_array = {}
  commit_array.each do |file, sha|
    final_array[file] = commit(sha)
  end
  final_array
end

#blob(id) ⇒ Object

The Blob object for the given id

+id+ is the SHA1 id of the blob

Returns Grit::Blob (unbaked)



254
255
256
# File 'lib/grit/lib/grit/repo.rb', line 254

def blob(id)
  Blob.create(self, :id => id)
end

#checkout(branch) ⇒ Object

Check out a branch

Returns true/false if success, error message



91
92
93
94
# File 'lib/grit/lib/grit/repo.rb', line 91

def checkout(branch)
  self.git.checkout({}, branch)
  [self.git.last_status.success?, self.git.last_err]
end

#commit(id) ⇒ Object

The Commit object for the specified id

+id+ is the SHA1 identifier of the commit

Returns Grit::Commit (baked)



232
233
234
235
236
# File 'lib/grit/lib/grit/repo.rb', line 232

def commit(id)
  options = {:max_count => 1}
  
  Commit.find_all(self, id, options).first
end

#commit_all(message) ⇒ Object

Commits all tracked and modified files

Returns true/false if commit worked



107
108
109
# File 'lib/grit/lib/grit/repo.rb', line 107

def commit_all(message)
  self.git.commit({}, '-a', '-m', message)
end

#commit_count(start = 'master') ⇒ Object

The number of commits reachable by the given branch/commit

+start+ is the branch/commit name (default 'master')

Returns Integer



224
225
226
# File 'lib/grit/lib/grit/repo.rb', line 224

def commit_count(start = 'master')
  Commit.count(self, start)
end

#commit_diff(commit) ⇒ Object

The commit diff for the given commit

+commit+ is the commit name/id

Returns Grit::Diff[]



281
282
283
# File 'lib/grit/lib/grit/repo.rb', line 281

def commit_diff(commit)
  Commit.diff(self, commit)
end

#commit_index(message) ⇒ Object

Commits current index

Returns true/false if commit worked



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

def commit_index(message)
  self.git.commit({}, '-m', message)
end

#commit_stats(start = 'master', max_count = 10, skip = 0) ⇒ Object



177
178
179
180
181
182
# File 'lib/grit/lib/grit/repo.rb', line 177

def commit_stats(start = 'master', max_count = 10, skip = 0)
  options = {:max_count => max_count,
             :skip => skip}
  
  CommitStats.find_all(self, start, options)
end

#commits(start = 'master', max_count = 10, skip = 0) ⇒ Object

An array of Commit objects representing the history of a given ref/commit

+start+ is the branch/commit name (default 'master')
+max_count+ is the maximum number of commits to return (default 10, use +false+ for all)
+skip+ is the number of commits to skip (default 0)

Returns Grit::Commit[] (baked)



190
191
192
193
194
195
# File 'lib/grit/lib/grit/repo.rb', line 190

def commits(start = 'master', max_count = 10, skip = 0)
  options = {:max_count => max_count,
             :skip => skip}
  
  Commit.find_all(self, start, options)
end

#commits_between(from, to) ⇒ Object

The Commits objects that are reachable via to but not via from Commits are returned in chronological order.

+from+ is the branch/commit name of the younger item
+to+ is the branch/commit name of the older item

Returns Grit::Commit[] (baked)



203
204
205
# File 'lib/grit/lib/grit/repo.rb', line 203

def commits_between(from, to)
  Commit.find_all(self, "#{from}..#{to}").reverse
end

#commits_since(start = 'master', since = '1970-01-01', extra_options = {}) ⇒ Object

The Commits objects that are newer than the specified date. Commits are returned in chronological order.

+start+ is the branch/commit name (default 'master')
+since+ is a string represeting a date/time
+extra_options+ is a hash of extra options

Returns Grit::Commit[] (baked)



214
215
216
217
218
# File 'lib/grit/lib/grit/repo.rb', line 214

def commits_since(start = 'master', since = '1970-01-01', extra_options = {})
  options = {:since => since}.merge(extra_options)
  
  Commit.find_all(self, start, options)
end

#configObject



415
416
417
# File 'lib/grit/lib/grit/repo.rb', line 415

def config
  @config ||= Config.new(self)
end

#descriptionObject

The project’s description. Taken verbatim from GIT_REPO/description

Returns String



53
54
55
# File 'lib/grit/lib/grit/repo.rb', line 53

def description
  File.open(File.join(self.path, 'description')).read.chomp
end

#diff(a, b, *paths) ⇒ Object

The diff from commit a to commit b, optionally restricted to the given file(s)

+a+ is the base commit
+b+ is the other commit
+paths+ is an optional list of file paths on which to restrict the diff


273
274
275
# File 'lib/grit/lib/grit/repo.rb', line 273

def diff(a, b, *paths)
  self.git.diff({}, a, b, '--', *paths)
end

#disable_daemon_serveObject

Disable git-daemon serving of this repository by ensuring there is no git-daemon-export-ok file in its git directory

Returns nothing



372
373
374
# File 'lib/grit/lib/grit/repo.rb', line 372

def disable_daemon_serve
  FileUtils.rm_f(File.join(self.path, DAEMON_EXPORT_FILE))
end

#enable_daemon_serveObject

Enable git-daemon serving of this repository by writing the git-daemon-export-ok file to its git directory

Returns nothing



364
365
366
# File 'lib/grit/lib/grit/repo.rb', line 364

def enable_daemon_serve
  FileUtils.touch(File.join(self.path, DAEMON_EXPORT_FILE))
end

#fork_bare(path, options = {}) ⇒ Object

Fork a bare git repository from this repo

+path+ is the full path of the new repo (traditionally ends with /<name>.git)
+options+ is any additional options to the git clone command (:bare and :shared are true by default)

Returns Grit::Repo (the newly forked repo)



304
305
306
307
308
309
# File 'lib/grit/lib/grit/repo.rb', line 304

def fork_bare(path, options = {})
  default_options = {:bare => true, :shared => true}
  real_options = default_options.merge(options)
  self.git.clone(real_options, self.path, path)
  Repo.new(path)
end

#gc_autoObject



376
377
378
# File 'lib/grit/lib/grit/repo.rb', line 376

def gc_auto
  self.git.gc({:auto => true})
end

#get_head(head_name) ⇒ Object



72
73
74
# File 'lib/grit/lib/grit/repo.rb', line 72

def get_head(head_name)
  heads.find { |h| h.name == head_name }
end

#headObject

Object reprsenting the current repo head.

Returns Grit::Head (baked)



83
84
85
# File 'lib/grit/lib/grit/repo.rb', line 83

def head
  Head.current(self)
end

#headsObject Also known as: branches

An array of Head objects representing the branch heads in this repo

Returns Grit::Head[] (baked)



66
67
68
# File 'lib/grit/lib/grit/repo.rb', line 66

def heads
  Head.find_all(self)
end

#indexObject



419
420
421
# File 'lib/grit/lib/grit/repo.rb', line 419

def index
  Index.new(self)
end

#inspectObject

Pretty object inspection



436
437
438
# File 'lib/grit/lib/grit/repo.rb', line 436

def inspect
  %Q{#<Grit::Repo "#{@path}">}
end

#is_head?(head_name) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/grit/lib/grit/repo.rb', line 76

def is_head?(head_name)
  get_head(head_name)
end

#log(commit = 'master', path = nil, options = {}) ⇒ Object

The commit log for a treeish

Returns Grit::Commit[]



261
262
263
264
265
266
267
# File 'lib/grit/lib/grit/repo.rb', line 261

def log(commit = 'master', path = nil, options = {})
  default_options = {:pretty => "raw"}
  actual_options  = default_options.merge(options)
  arg = path ? [commit, '--', path] : [commit]
  commits = self.git.log(actual_options, *arg)
  Commit.list_from_string(self, commits)
end

#refsObject

An array of Ref objects representing the refs in this repo

Returns Grit::Ref[] (baked)



173
174
175
# File 'lib/grit/lib/grit/repo.rb', line 173

def refs
  [ Head.find_all(self), Tag.find_all(self), Remote.find_all(self) ].flatten
end

#remotesObject

An array of Remote objects representing the remote branches in this repo

Returns Grit::Remote[] (baked)



165
166
167
# File 'lib/grit/lib/grit/repo.rb', line 165

def remotes
  Remote.find_all(self)
end

#remove(*files) ⇒ Object

Remove files from the index delete from working copy, stage the file to be removed on commit



128
129
130
# File 'lib/grit/lib/grit/repo.rb', line 128

def remove(*files)
  self.git.rm({}, *files.flatten)
end

#reset_allObject

Unstage files from the index remove from index, but not from working copy



134
135
136
# File 'lib/grit/lib/grit/repo.rb', line 134

def reset_all
  self.git.reset
end

#statusObject



149
150
151
# File 'lib/grit/lib/grit/repo.rb', line 149

def status
  Status.new(self)
end

#tagsObject

An array of Tag objects that are available in this repo

Returns Grit::Tag[] (baked)



157
158
159
# File 'lib/grit/lib/grit/repo.rb', line 157

def tags
  Tag.find_all(self)
end

#tree(treeish = 'master', paths = []) ⇒ Object

The Tree object for the given treeish reference

+treeish+ is the reference (default 'master')
+paths+ is an optional Array of directory paths to restrict the tree (deafult [])

Examples

repo.tree('master', ['lib/'])

Returns Grit::Tree (baked)



246
247
248
# File 'lib/grit/lib/grit/repo.rb', line 246

def tree(treeish = 'master', paths = [])
  Tree.construct(self, treeish, paths)
end

#update_ref(head, commit_sha) ⇒ Object



423
424
425
426
427
428
429
430
431
432
433
# File 'lib/grit/lib/grit/repo.rb', line 423

def update_ref(head, commit_sha)
  return nil if !commit_sha || (commit_sha.size != 40)
   
  ref_heads = File.join(self.path, 'refs', 'heads')
  FileUtils.mkdir_p(ref_heads)
  File.open(File.join(ref_heads, head), 'w') do |f|
    f.write(commit_sha)
  end
  commit_sha

end