Class: BetweenMeals::Repo::Git

Inherits:
BetweenMeals::Repo show all
Defined in:
lib/between_meals/repo/git.rb,
lib/between_meals/repo/git/cmd.rb

Defined Under Namespace

Classes: Cmd

Instance Attribute Summary

Attributes inherited from BetweenMeals::Repo

#bin, #repo_path

Instance Method Summary collapse

Methods inherited from BetweenMeals::Repo

#create, get, #head, #head_msg, #head_msg=, #initialize, #latest_revision

Constructor Details

This class inherits a constructor from BetweenMeals::Repo

Instance Method Details

#changes(start_ref, end_ref) ⇒ Object

Return files changed between two revisions



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/between_meals/repo/git.rb', line 89

def changes(start_ref, end_ref)
  valid_ref?(start_ref)
  valid_ref?(end_ref) if end_ref
  stdout = @cmd.diff(start_ref, end_ref).stdout
  begin
    parse_status(stdout).compact
  rescue StandardError => e
    # We've seen some weird non-reproducible failures here
    @logger.error(
      'Something went wrong. Please report this output.',
    )
    @logger.error(e)
    stdout.lines.each do |line|
      @logger.error(line.strip)
    end
    exit(1)
  end
end

#checkout(url) ⇒ Object



83
84
85
86
# File 'lib/between_meals/repo/git.rb', line 83

def checkout(url)
  @cmd.clone(url, @repo_path)
  @repo = Rugged::Repository.new(File.expand_path(@repo_path))
end

#emailObject



135
136
137
# File 'lib/between_meals/repo/git.rb', line 135

def email
  @cmd.config('user.email').stdout.strip
end

#exists?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/between_meals/repo/git.rb', line 52

def exists?
  !@repo.nil?
end

#filesObject

Return all files



113
114
115
# File 'lib/between_meals/repo/git.rb', line 113

def files
  @repo.index.map { |x| { :path => x[:path], :status => :created } }
end

#head_parentsObject



77
78
79
80
81
# File 'lib/between_meals/repo/git.rb', line 77

def head_parents
  @repo.head.target.parents.map do |x|
    { :rev => x.tree.oid, :time => x.time }
  end
end

#head_revObject



56
57
58
# File 'lib/between_meals/repo/git.rb', line 56

def head_rev
  @repo.head.target.oid
end

#last_authorObject



73
74
75
# File 'lib/between_meals/repo/git.rb', line 73

def last_author
  @repo.head.target.to_hash[:author]
end

#last_msgObject



60
61
62
# File 'lib/between_meals/repo/git.rb', line 60

def last_msg
  @repo.head.target.message
end

#last_msg=(msg) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/between_meals/repo/git.rb', line 64

def last_msg=(msg)
  @repo.head.target.amend(
    {
      :message => msg,
      :update_ref => 'HEAD',
    },
  )
end

#nameObject



131
132
133
# File 'lib/between_meals/repo/git.rb', line 131

def name
  @cmd.config('user.name').stdout.strip
end

#repo_objectObject

Allow people to get access to the underlying Rugged object for their hooks



48
49
50
# File 'lib/between_meals/repo/git.rb', line 48

def repo_object
  @repo
end

#setupObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/between_meals/repo/git.rb', line 28

def setup
  if File.exist?(File.expand_path(@repo_path))
    begin
      @repo = Rugged::Repository.new(File.expand_path(@repo_path))
    rescue StandardError
      @repo = nil
    end
  else
    @repo = nil
  end
  @bin = 'git'
  @cmd = BetweenMeals::Repo::Git::Cmd.new(
    :bin => @bin,
    :cwd => @repo_path,
    :logger => @logger,
  )
end

#statusObject



127
128
129
# File 'lib/between_meals/repo/git.rb', line 127

def status
  @cmd.status.stdout.strip
end

#updateObject



108
109
110
# File 'lib/between_meals/repo/git.rb', line 108

def update
  @cmd.pull.stdout
end

#upstream?(rev, master = 'upstream/master') ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
# File 'lib/between_meals/repo/git.rb', line 117

def upstream?(rev, master = 'upstream/master')
  if @cmd.merge_base(rev, master).stdout.strip == rev
    return true
  end

  return false
rescue StandardError
  return false
end

#valid_ref?(ref) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
142
143
# File 'lib/between_meals/repo/git.rb', line 139

def valid_ref?(ref)
  unless @repo.exists?(ref)
    fail Changeset::ReferenceError
  end
end