Class: Piston::Git::Commit

Inherits:
Revision show all
Defined in:
lib/piston/git/commit.rb

Defined Under Namespace

Classes: Gone, InvalidCommit

Instance Attribute Summary collapse

Attributes inherited from Revision

#dir, #recalled_values, #repository, #revision

Instance Method Summary collapse

Methods inherited from Revision

#initialize, logger, #logger, #to_s, #url

Constructor Details

This class inherits a constructor from Piston::Revision

Instance Attribute Details

#sha1Object (readonly)

Returns the value of attribute sha1.



12
13
14
# File 'lib/piston/git/commit.rb', line 12

def sha1
  @sha1
end

Instance Method Details

#branch_nameObject



39
40
41
# File 'lib/piston/git/commit.rb', line 39

def branch_name
  "my-#{commit}"
end

#checkout_to(dir) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/piston/git/commit.rb', line 43

def checkout_to(dir)
  super
  git(:clone, repository.url, @dir)
  Dir.chdir(@dir) do
    logger.debug {"in dir #{@dir}"}
    git(:checkout, "-b", branch_name, commit)
    response = git(:log, "-n", "1")
    @sha1 = $1 if response =~ /commit\s+([a-f\d]{40})/i
  end

  def remember_values
    { Piston::Git::COMMIT => @sha1, Piston::Git::BRANCH => commit }
  end

  def each
    raise ArgumentError, "Never cloned + checked out" if @dir.nil?
    @dir.find do |path|
      Find.prune if path.to_s =~ %r{/[.]git}
      next if @dir == path
      next if File.directory?(path)
      yield path.relative_path_from(@dir)
    end
  end

  def copy_to(relpath, abspath)
    Pathname.new(abspath).dirname.mkpath
    FileUtils.cp(@dir + relpath, abspath)
  end
end

#clientObject



14
15
16
# File 'lib/piston/git/commit.rb', line 14

def client
  @client ||= Piston::Git::Client.instance
end

#copy_to(relpath, abspath) ⇒ Object



67
68
69
70
# File 'lib/piston/git/commit.rb', line 67

def copy_to(relpath, abspath)
  Pathname.new(abspath).dirname.mkpath
  FileUtils.cp(@dir + relpath, abspath)
end

#eachObject

Raises:

  • (ArgumentError)


57
58
59
60
61
62
63
64
65
# File 'lib/piston/git/commit.rb', line 57

def each
  raise ArgumentError, "Never cloned + checked out" if @dir.nil?
  @dir.find do |path|
    Find.prune if path.to_s =~ %r{/[.]git}
    next if @dir == path
    next if File.directory?(path)
    yield path.relative_path_from(@dir)
  end
end

#git(*args) ⇒ Object



18
19
20
# File 'lib/piston/git/commit.rb', line 18

def git(*args)
  client.git(*args)
end

#nameObject



35
36
37
# File 'lib/piston/git/commit.rb', line 35

def name
  commit[0,7]
end

#recalled_commit_idObject



22
23
24
# File 'lib/piston/git/commit.rb', line 22

def recalled_commit_id
  recalled_values[Piston::Git::COMMIT]
end

#remember_valuesObject



53
54
55
# File 'lib/piston/git/commit.rb', line 53

def remember_values
  { Piston::Git::COMMIT => @sha1, Piston::Git::BRANCH => commit }
end

#validate!Object



26
27
28
29
30
31
32
33
# File 'lib/piston/git/commit.rb', line 26

def validate!
  begin
    data = git("ls-remote", @repository.url)
    self
  rescue Piston::Git::Client::CommandError
    raise Piston::Git::Commit::Gone, "Repository at #{@repository.url} does not exist anymore"
  end
end