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

#copy_from, #copy_to, #inspect, #logger, logger, #url

Constructor Details

#initialize(repository, revision, recalled_values = {}) ⇒ Commit

Returns a new instance of Commit.



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

def initialize(repository, revision, recalled_values={})
  super
  @revision = 'master' if @revision.upcase == 'HEAD'
end

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



44
45
46
# File 'lib/piston/git/commit.rb', line 44

def branch_name
  "my-#{commit}"
end

#checkout_to(dir) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/piston/git/commit.rb', line 48

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

#clientObject



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

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

#eachObject

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
98
# File 'lib/piston/git/commit.rb', line 89

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)
    next if @dir + '.piston.yml' == path
    yield path.relative_path_from(@dir)
  end
end

#exclude_for_diffObject



109
110
111
# File 'lib/piston/git/commit.rb', line 109

def exclude_for_diff
  Piston::Git::EXCLUDE
end

#git(*args) ⇒ Object



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

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

#nameObject



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

def name
  commit[0,7]
end

#recalled_commit_idObject



27
28
29
# File 'lib/piston/git/commit.rb', line 27

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

#remember_valuesObject



78
79
80
81
82
83
84
85
86
87
# File 'lib/piston/git/commit.rb', line 78

def remember_values
  # find last commit for +commit+ if it wasn't checked out
  unless @sha1
    out = git('ls-remote', repository.url, commit).match(/\w+/)
    @sha1 = out[0] unless out.nil?
  end
  # if ls-remote returns nothing, +commit+ must be a commit, not a branch
  @sha1 = commit unless @sha1
  { Piston::Git::COMMIT => @sha1, Piston::Git::BRANCH => commit }
end

#remotely_modifiedObject



100
101
102
103
104
105
106
107
# File 'lib/piston/git/commit.rb', line 100

def remotely_modified
  branch = recalled_values[Piston::Git::BRANCH]
  logger.debug {"Get last commit in #{branch} of #{repository.url}"}
  commit = git('ls-remote', repository.url, branch).match(/\w+/)
  # when we update to a commit, instead latest commit of a branch, +branch+ will be a commit, and ls-remote can return nil
  commit = commit[0] unless commit.nil?
  commit != self.commit
end

#resolve!Object



117
118
119
# File 'lib/piston/git/commit.rb', line 117

def resolve!
  # NOP, because @sha1 is what we want
end

#to_sObject



113
114
115
# File 'lib/piston/git/commit.rb', line 113

def to_s
  "commit #{sha1}"
end

#update_to(commit) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/piston/git/commit.rb', line 60

def update_to(commit)
  raise ArgumentError, "Commit #{self.commit} of #{repository.url} was never checked out -- can't update" unless @dir
  
  Dir.chdir(@dir) do
    logger.debug {"Saving old changes before updating"}
    git(:commit, '-a', '-m', 'old changes')
    target = commit
    target = "origin/#{target}" unless target.include?("/") || target =~ /^[a-f\d]+$/i
    logger.debug {"Merging old changes with #{target}"}
    git(:merge, '--squash', target)
    output = git(:status)
    added = output.scan(/new file:\s+(.*)$/).flatten
    deleted = output.scan(/deleted:\s+(.*)$/).flatten
    renamed = output.scan(/renamed:\s+(.+) -> (.+)$/)
    [added, deleted, renamed]
  end
end

#validate!Object



31
32
33
34
35
36
37
38
# File 'lib/piston/git/commit.rb', line 31

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