class Dependabot::Git

Attributes

repo[R]

Public Class Methods

for(dependency) click to toggle source
# File lib/dependabot/git.rb, line 11
def self.for(dependency)
  new(dependency.path.parent)
end
new(path) click to toggle source
# File lib/dependabot/git.rb, line 7
def initialize(path)
  @repo = Rugged::Repository.discover(path)
end

Public Instance Methods

checkout(branch:) click to toggle source
# 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) click to toggle source
# 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
patch() click to toggle source
# File lib/dependabot/git.rb, line 28
def patch
  repo.index.diff.patch
end
push(remote: "origin", branch: "HEAD") click to toggle source
# 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

Private Instance Methods

credentials_for(remote) click to toggle source
# File lib/dependabot/git.rb, line 51
def credentials_for(remote)
  Dependabot.logger.debug(repo.remotes[remote].url)
  if ssh?(repo.remotes[remote].url)
    Rugged::Credentials::SshKeyFromAgent.new(username: "git")
  else
    Rugged::Credentials::UserPassword.new(username: "x-access-token", password: Dependabot.github.token)
  end
end
ssh?(url) click to toggle source
# File lib/dependabot/git.rb, line 60
def ssh?(url)
  url.include?("git@github.com:")
end
stage(path) click to toggle source
# File lib/dependabot/git.rb, line 47
def stage(path)
  repo.index.add(path)
end