2016-11-04 03:46:01 +05:30
|
|
|
// Copyright 2015 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"container/list"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// getRefCommitID returns the last commit ID string of given reference (branch or tag).
|
|
|
|
func (repo *Repository) getRefCommitID(name string) (string, error) {
|
|
|
|
stdout, err := NewCommand("show-ref", "--verify", name).RunInDir(repo.Path)
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "not a valid ref") {
|
|
|
|
return "", ErrNotExist{name, ""}
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return strings.Split(stdout, " ")[0], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBranchCommitID returns last commit ID string of given branch.
|
|
|
|
func (repo *Repository) GetBranchCommitID(name string) (string, error) {
|
2016-12-22 15:00:52 +05:30
|
|
|
return repo.getRefCommitID(BranchPrefix + name)
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetTagCommitID returns last commit ID string of given tag.
|
|
|
|
func (repo *Repository) GetTagCommitID(name string) (string, error) {
|
2016-12-22 15:00:52 +05:30
|
|
|
return repo.getRefCommitID(TagPrefix + name)
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// parseCommitData parses commit information from the (uncompressed) raw
|
|
|
|
// data from the commit object.
|
|
|
|
// \n\n separate headers from message
|
|
|
|
func parseCommitData(data []byte) (*Commit, error) {
|
|
|
|
commit := new(Commit)
|
2016-12-22 15:00:52 +05:30
|
|
|
commit.parents = make([]SHA1, 0, 1)
|
2016-11-04 03:46:01 +05:30
|
|
|
// we now have the contents of the commit object. Let's investigate...
|
|
|
|
nextline := 0
|
|
|
|
l:
|
|
|
|
for {
|
|
|
|
eol := bytes.IndexByte(data[nextline:], '\n')
|
|
|
|
switch {
|
|
|
|
case eol > 0:
|
|
|
|
line := data[nextline : nextline+eol]
|
|
|
|
spacepos := bytes.IndexByte(line, ' ')
|
|
|
|
reftype := line[:spacepos]
|
|
|
|
switch string(reftype) {
|
|
|
|
case "tree", "object":
|
|
|
|
id, err := NewIDFromString(string(line[spacepos+1:]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commit.Tree.ID = id
|
|
|
|
case "parent":
|
|
|
|
// A commit can have one or more parents
|
|
|
|
oid, err := NewIDFromString(string(line[spacepos+1:]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commit.parents = append(commit.parents, oid)
|
|
|
|
case "author", "tagger":
|
|
|
|
sig, err := newSignatureFromCommitline(line[spacepos+1:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commit.Author = sig
|
|
|
|
case "committer":
|
|
|
|
sig, err := newSignatureFromCommitline(line[spacepos+1:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commit.Committer = sig
|
2017-03-22 16:13:54 +05:30
|
|
|
case "gpgsig":
|
|
|
|
sig, err := newGPGSignatureFromCommitline(data, nextline+spacepos+1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commit.Signature = sig
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
nextline += eol + 1
|
|
|
|
case eol == 0:
|
|
|
|
commit.CommitMessage = string(data[nextline+1:])
|
|
|
|
break l
|
|
|
|
default:
|
|
|
|
break l
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return commit, nil
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
func (repo *Repository) getCommit(id SHA1) (*Commit, error) {
|
2016-11-04 03:46:01 +05:30
|
|
|
c, ok := repo.commitCache.Get(id.String())
|
|
|
|
if ok {
|
|
|
|
log("Hit cache: %s", id)
|
|
|
|
return c.(*Commit), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := NewCommand("cat-file", "-p", id.String()).RunInDirBytes(repo.Path)
|
|
|
|
if err != nil {
|
|
|
|
if strings.Contains(err.Error(), "fatal: Not a valid object name") {
|
|
|
|
return nil, ErrNotExist{id.String(), ""}
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
commit, err := parseCommitData(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
commit.repo = repo
|
|
|
|
commit.ID = id
|
|
|
|
|
|
|
|
repo.commitCache.Set(id.String(), commit)
|
|
|
|
return commit, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommit returns commit object of by ID string.
|
|
|
|
func (repo *Repository) GetCommit(commitID string) (*Commit, error) {
|
|
|
|
if len(commitID) != 40 {
|
|
|
|
var err error
|
|
|
|
commitID, err = NewCommand("rev-parse", commitID).RunInDir(repo.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
id, err := NewIDFromString(commitID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo.getCommit(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBranchCommit returns the last commit of given branch.
|
|
|
|
func (repo *Repository) GetBranchCommit(name string) (*Commit, error) {
|
|
|
|
commitID, err := repo.GetBranchCommitID(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return repo.GetCommit(commitID)
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
// GetTagCommit get the commit of the specific tag via name
|
2016-11-04 03:46:01 +05:30
|
|
|
func (repo *Repository) GetTagCommit(name string) (*Commit, error) {
|
|
|
|
commitID, err := repo.GetTagCommitID(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return repo.GetCommit(commitID)
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
func (repo *Repository) getCommitByPathWithID(id SHA1, relpath string) (*Commit, error) {
|
2016-11-04 03:46:01 +05:30
|
|
|
// File name starts with ':' must be escaped.
|
|
|
|
if relpath[0] == ':' {
|
|
|
|
relpath = `\` + relpath
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
stdout, err := NewCommand("log", "-1", prettyLogFormat, id.String(), "--", relpath).RunInDir(repo.Path)
|
2016-11-04 03:46:01 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err = NewIDFromString(stdout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo.getCommit(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCommitByPath returns the last commit of relative path.
|
|
|
|
func (repo *Repository) GetCommitByPath(relpath string) (*Commit, error) {
|
2016-12-22 15:00:52 +05:30
|
|
|
stdout, err := NewCommand("log", "-1", prettyLogFormat, "--", relpath).RunInDirBytes(repo.Path)
|
2016-11-04 03:46:01 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
commits, err := repo.parsePrettyFormatLogToList(stdout)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return commits.Front().Value.(*Commit), nil
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
// CommitsRangeSize the default commits range size
|
2016-11-04 03:46:01 +05:30
|
|
|
var CommitsRangeSize = 50
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
func (repo *Repository) commitsByRange(id SHA1, page int) (*list.List, error) {
|
2016-11-04 03:46:01 +05:30
|
|
|
stdout, err := NewCommand("log", id.String(), "--skip="+strconv.Itoa((page-1)*CommitsRangeSize),
|
2016-12-22 15:00:52 +05:30
|
|
|
"--max-count="+strconv.Itoa(CommitsRangeSize), prettyLogFormat).RunInDirBytes(repo.Path)
|
2016-11-04 03:46:01 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return repo.parsePrettyFormatLogToList(stdout)
|
|
|
|
}
|
|
|
|
|
2017-02-05 20:13:28 +05:30
|
|
|
func (repo *Repository) searchCommits(id SHA1, keyword string, all bool) (*list.List, error) {
|
|
|
|
cmd := NewCommand("log", id.String(), "-100", "-i", "--grep="+keyword, prettyLogFormat)
|
|
|
|
if all {
|
|
|
|
cmd.AddArguments("--all")
|
|
|
|
}
|
|
|
|
stdout, err := cmd.RunInDirBytes(repo.Path)
|
2016-11-04 03:46:01 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return repo.parsePrettyFormatLogToList(stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *Repository) getFilesChanged(id1 string, id2 string) ([]string, error) {
|
|
|
|
stdout, err := NewCommand("diff", "--name-only", id1, id2).RunInDirBytes(repo.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return strings.Split(string(stdout), "\n"), nil
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
// FileCommitsCount return the number of files at a revison
|
2016-11-04 03:46:01 +05:30
|
|
|
func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
|
|
|
|
return commitsCount(repo.Path, revision, file)
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
// CommitsByFileAndRange return the commits accroding revison file and the page
|
2016-11-04 03:46:01 +05:30
|
|
|
func (repo *Repository) CommitsByFileAndRange(revision, file string, page int) (*list.List, error) {
|
2017-04-08 07:53:39 +05:30
|
|
|
stdout, err := NewCommand("log", revision, "--follow", "--skip="+strconv.Itoa((page-1)*50),
|
2016-12-22 15:00:52 +05:30
|
|
|
"--max-count="+strconv.Itoa(CommitsRangeSize), prettyLogFormat, "--", file).RunInDirBytes(repo.Path)
|
2016-11-04 03:46:01 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return repo.parsePrettyFormatLogToList(stdout)
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
// FilesCountBetween return the number of files changed between two commits
|
2016-11-04 03:46:01 +05:30
|
|
|
func (repo *Repository) FilesCountBetween(startCommitID, endCommitID string) (int, error) {
|
|
|
|
stdout, err := NewCommand("diff", "--name-only", startCommitID+"..."+endCommitID).RunInDir(repo.Path)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return len(strings.Split(stdout, "\n")) - 1, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CommitsBetween returns a list that contains commits between [last, before).
|
|
|
|
func (repo *Repository) CommitsBetween(last *Commit, before *Commit) (*list.List, error) {
|
2017-10-23 19:06:14 +05:30
|
|
|
stdout, err := NewCommand("rev-list", before.ID.String()+"..."+last.ID.String()).RunInDirBytes(repo.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
2017-10-23 19:06:14 +05:30
|
|
|
return repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
// CommitsBetweenIDs return commits between twoe commits
|
2016-11-04 03:46:01 +05:30
|
|
|
func (repo *Repository) CommitsBetweenIDs(last, before string) (*list.List, error) {
|
|
|
|
lastCommit, err := repo.GetCommit(last)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
beforeCommit, err := repo.GetCommit(before)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return repo.CommitsBetween(lastCommit, beforeCommit)
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
// CommitsCountBetween return numbers of commits between two commits
|
2016-11-04 03:46:01 +05:30
|
|
|
func (repo *Repository) CommitsCountBetween(start, end string) (int64, error) {
|
|
|
|
return commitsCount(repo.Path, start+"..."+end, "")
|
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
// commitsBefore the limit is depth, not total number of returned commits.
|
2017-12-11 07:53:34 +05:30
|
|
|
func (repo *Repository) commitsBefore(id SHA1, limit int) (*list.List, error) {
|
|
|
|
cmd := NewCommand("log")
|
|
|
|
if limit > 0 {
|
|
|
|
cmd.AddArguments("-"+ strconv.Itoa(limit), prettyLogFormat, id.String())
|
|
|
|
} else {
|
|
|
|
cmd.AddArguments(prettyLogFormat, id.String())
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
2017-12-11 07:53:34 +05:30
|
|
|
stdout, err := cmd.RunInDirBytes(repo.Path)
|
2016-11-04 03:46:01 +05:30
|
|
|
if err != nil {
|
2017-12-11 07:53:34 +05:30
|
|
|
return nil, err
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
2017-12-11 07:53:34 +05:30
|
|
|
formattedLog, err := repo.parsePrettyFormatLogToList(bytes.TrimSpace(stdout))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
2017-12-11 07:53:34 +05:30
|
|
|
commits := list.New()
|
|
|
|
for logEntry := formattedLog.Front(); logEntry != nil; logEntry = logEntry.Next() {
|
|
|
|
commit := logEntry.Value.(*Commit)
|
|
|
|
branches, err := repo.getBranches(commit, 2)
|
2016-11-04 03:46:01 +05:30
|
|
|
if err != nil {
|
2017-12-11 07:53:34 +05:30
|
|
|
return nil, err
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
2017-12-11 07:53:34 +05:30
|
|
|
|
|
|
|
if len(branches) > 1 {
|
|
|
|
break
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
2017-12-11 07:53:34 +05:30
|
|
|
|
|
|
|
commits.PushBack(commit)
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
2017-12-11 07:53:34 +05:30
|
|
|
return commits, nil
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
func (repo *Repository) getCommitsBefore(id SHA1) (*list.List, error) {
|
2017-12-11 07:53:34 +05:30
|
|
|
return repo.commitsBefore(id, 0)
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|
|
|
|
|
2016-12-22 15:00:52 +05:30
|
|
|
func (repo *Repository) getCommitsBeforeLimit(id SHA1, num int) (*list.List, error) {
|
2017-12-11 07:53:34 +05:30
|
|
|
return repo.commitsBefore(id, num)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *Repository) getBranches(commit *Commit, limit int) ([]string, error) {
|
|
|
|
stdout, err := NewCommand("for-each-ref", "--count="+ strconv.Itoa(limit), "--format=%(refname)", "--contains", commit.ID.String(), BranchPrefix).RunInDir(repo.Path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
refs := strings.Split(stdout, "\n")
|
|
|
|
branches := make([]string, len(refs)-1)
|
|
|
|
for i, ref := range refs[:len(refs)-1] {
|
|
|
|
branches[i] = strings.TrimPrefix(ref, BranchPrefix)
|
|
|
|
}
|
|
|
|
return branches, nil
|
2016-11-04 03:46:01 +05:30
|
|
|
}
|