Exemplo n.º 1
0
func (r *Repository) Commits(opt vcs.CommitsOptions) ([]*vcs.Commit, uint, error) {
	r.editLock.RLock()
	defer r.editLock.RUnlock()

	walk, err := r.u.Walk()
	if err != nil {
		return nil, 0, err
	}
	defer walk.Free()

	walk.Sorting(git2go.SortTime)

	oid, err := git2go.NewOid(string(opt.Head))
	if err != nil {
		return nil, 0, err
	}
	if err := walk.Push(oid); err != nil {
		if git2go.IsErrorCode(err, git2go.ErrNotFound) {
			return nil, 0, vcs.ErrCommitNotFound
		}
		return nil, 0, err
	}

	if opt.Base != "" {
		baseOID, err := git2go.NewOid(string(opt.Base))
		if err != nil {
			return nil, 0, err
		}
		if err := walk.Hide(baseOID); err != nil {
			if git2go.IsErrorCode(err, git2go.ErrNotFound) {
				return nil, 0, vcs.ErrCommitNotFound
			}
			return nil, 0, err
		}
	}

	var commits []*vcs.Commit
	total := uint(0)
	err = walk.Iterate(func(c *git2go.Commit) bool {
		if total >= opt.Skip && (opt.N == 0 || uint(len(commits)) < opt.N) {
			commits = append(commits, r.makeCommit(c))
		}
		total++
		// If we want total, keep going until the end.
		if !opt.NoTotal {
			return true
		}
		// Otherwise return once N has been satisfied.
		return (opt.N == 0 || uint(len(commits)) < opt.N)
	})
	if err != nil {
		return nil, 0, err
	}
	if opt.NoTotal {
		total = 0
	}

	return commits, total, nil
}
Exemplo n.º 2
0
Arquivo: repo.go Projeto: sombr/ccat
func (r *Repository) Commits(opt vcs.CommitsOptions) ([]*vcs.Commit, uint, error) {
	r.editLock.RLock()
	defer r.editLock.RUnlock()

	walk, err := r.u.Walk()
	if err != nil {
		return nil, 0, err
	}
	defer walk.Free()

	walk.Sorting(git2go.SortTopological)

	oid, err := git2go.NewOid(string(opt.Head))
	if err != nil {
		return nil, 0, err
	}
	if err := walk.Push(oid); err != nil {
		if git2go.IsErrorCode(err, git2go.ErrNotFound) {
			return nil, 0, vcs.ErrCommitNotFound
		}
		return nil, 0, err
	}

	if opt.Base != "" {
		baseOID, err := git2go.NewOid(string(opt.Base))
		if err != nil {
			return nil, 0, err
		}
		if err := walk.Hide(baseOID); err != nil {
			if git2go.IsErrorCode(err, git2go.ErrNotFound) {
				return nil, 0, vcs.ErrCommitNotFound
			}
			return nil, 0, err
		}
	}

	var commits []*vcs.Commit
	total := uint(0)
	err = walk.Iterate(func(c *git2go.Commit) bool {
		if total >= opt.Skip && (opt.N == 0 || uint(len(commits)) < opt.N) {
			commits = append(commits, r.makeCommit(c))
		}
		total++
		return total < uint(MaxCommits)
	})
	if err != nil {
		return nil, 0, err
	}

	return commits, total, nil
}
Exemplo n.º 3
0
// getCommit finds and returns the raw git2go Commit. The caller is
// responsible for freeing it (c.Free()).
func (r *Repository) getCommit(id vcs.CommitID) (*git2go.Commit, error) {
	oid, err := git2go.NewOid(string(id))
	if err != nil {
		return nil, err
	}

	c, err := r.u.LookupCommit(oid)
	if err != nil {
		if git2go.IsErrorCode(err, git2go.ErrNotFound) {
			return nil, vcs.ErrCommitNotFound
		}
		return nil, err
	}
	return c, nil
}