module Octokit
class Client
module Commits
# List commits
#
# Optionally pass path => "path/to/file.rb" in options to
# only return commits containing the given file path.
#
# @param repo [String, Hash, Repository] A GitHub repository
# @param sha_or_branch [String] Commit SHA or branch name from which to start the list
# @return [Array] An array of hashes representing commits
# @see http://developer.github.com/v3/repos/commits/
def commits(repo, sha_or_branch="master", options={})
params = { :sha => sha_or_branch, :per_page => 35 }
get("repos/#{Repository.new(repo)}/commits", params.merge(options), 3)
end
alias :list_commits :commits
# Get a single commit
#
# @param repo [String, Hash, Repository] A GitHub repository
# @param sha [String] The SHA of the commit to fetch
# @return [Hashie::Mash] A hash representing the commit
# @see http://developer.github.com/v3/repos/commits/
def commit(repo, sha, options={})
get("repos/#{Repository.new(repo)}/commits/#{sha}", options, 3)
end
# Create a commit
#
# Optionally pass author and committer hashes in options
# if you'd like manual control over those parameters. If absent, details will be
# inferred from the authenticated user. See GitHub's documentation
# for details about how to format committer identities.
#
# @param repo [String, Hash, Repository] A GitHub repository
# @param message [String] The commit message
# @param tree [String] The SHA of the tree object the new commit will point to
# @param parents [String, Array] One SHA (for a normal commit) or an array of SHAs (for a merge) of the new commit's parent commits. If ommitted or empty, a root commit will be created
# @return [Hashie::Mash] A hash representing the new commit
# @see http://developer.github.com/v3/git/commits/
# @example Create a commit
# commit = Octokit.create_commit("octocat/Hello-World", "My commit message", "827efc6d56897b048c772eb4087f854f46256132", "7d1b31e74ee336d15cbd21741bc88a537ed063a0")
# commit.sha # => "7638417db6d59f3c431d3e1f261cc637155684cd"
# commit.tree.sha # => "827efc6d56897b048c772eb4087f854f46256132"
# commit.message # => "My commit message"
# commit.committer # => { "name" => "Wynn Netherland", "email" => "wynn@github.com", ... }
def create_commit(repo, message, tree, parents=nil, options={})
params = { :message => message, :tree => tree }
params[:parents] = [parents].flatten if parents
post("repos/#{Repository.new(repo)}/git/commits", options.merge(params), 3)
end
# List all commit comments
#
# @param repo [String, Hash, Repository] A GitHub repository
# @return [Array] An array of hashes representing comments
# @see http://developer.github.com/v3/repos/comments/
def list_commit_comments(repo, options={})
get("repos/#{Repository.new(repo)}/comments", options, 3)
end
# List comments for a single commit
#
# @param repo [String, Hash, Repository] A GitHub repository
# @param sha [String] The SHA of the commit whose comments will be fetched
# @return [Array] An array of hashes representing comments
# @see http://developer.github.com/v3/repos/comments/
def commit_comments(repo, sha, options={})
get("repos/#{Repository.new(repo)}/commits/#{sha}/comments", options, 3)
end
# Get a single commit comment
#
# @param repo [String, Hash, Repository] A GitHub repository
# @param id [String] The ID of the comment to fetch
# @return [Hashie::Mash] A hash representing the comment
# @see http://developer.github.com/v3/repos/comments/
def commit_comment(repo, id, options={})
get("repos/#{Repository.new(repo)}/comments/#{id}", options, 3)
end
# Compare two commits
#
# @param repo [String, Hash, Repository] A GitHub repository
# @param base [String] The sha of the starting commit
# @param end [String] The sha of the ending commit
# @return [Hashie::Mash] A hash representing the comparison
# @see http://developer.github.com/v3/repos/commits/
def compare(repo, start, endd, options={})
get("repos/#{Repository.new(repo)}/compare/#{start}...#{endd}", options, 3)
end
# Merge a branch or sha
#
# @param repo [String, Hash, Repository] A GitHub repository
# @param base [String] The name of the base branch to merge into
# @param head [String] The branch or SHA1 to merge
# @option options [String] :commit_message The commit message for the merge
# @return [Hashie::Mash] A hash representing the comparison
# @see http://developer.github.com/v3/repos/merging/
def merge(repo, base, head, options={})
params = {
:base => base,
:head => head
}.merge(options)
post("repos/#{Repository.new(repo)}/merges", params, 3)
end
end
end
end