Class: Dependabot::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/git.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Git



7
8
9
# File 'lib/dependabot/git.rb', line 7

def initialize(path)
  @repo = Rugged::Repository.discover(path)
end

Instance Attribute Details

#repoObject (readonly)

Returns the value of attribute repo.



5
6
7
# File 'lib/dependabot/git.rb', line 5

def repo
  @repo
end

Class Method Details

.for(dependency) ⇒ Object



11
12
13
# File 'lib/dependabot/git.rb', line 11

def self.for(dependency)
  new(dependency.path.parent)
end

Instance Method Details

#checkout(branch:) ⇒ Object



15
16
17
18
# File 'lib/dependabot/git.rb', line 15

def checkout(branch:)
  repo.create_branch(branch, repo.head.name) unless repo.branches[branch]
  repo.checkout(branch)
end

#commit(message:, all: false) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/dependabot/git.rb', line 32

def commit(message:, all: false)
  repo.status { |path, status| stage(path) if status.include?(:worktree_modified) } if all

  Rugged::Commit.create(repo, {
    message: message,
    parents: repo.empty? ? [] : [repo.head.target].compact,
    tree: repo.index.write_tree(repo),
    update_ref: "HEAD",
    author: { email: "dependabot[bot]@users.noreply.github.com", name: "dependabot[bot]" },
    committer: { email: "dependabot[bot]@users.noreply.github.com", name: "dependabot[bot]" },
  })
end

#patchObject



28
29
30
# File 'lib/dependabot/git.rb', line 28

def patch
  repo.index.diff.patch
end

#push(remote: "origin", branch: "HEAD") ⇒ Object



20
21
22
23
24
25
26
# File 'lib/dependabot/git.rb', line 20

def push(remote: "origin", branch: "HEAD")
  repo.push(remote, ["refs/heads/#{branch}"], credentials: credentials_for(remote))
rescue StandardError
  Dir.chdir(File.dirname(repo.path)) do
    system("git push #{remote} #{branch}", exception: true)
  end
end