1
0
mirror of https://github.com/go-gitea/git.git synced 2026-02-05 06:45:03 +01:00

Added functions to handle checking out branches and moving files (#12)

* Adds functions to handle checking out branches and moving files

Adds support for checking out a new branch
Fix for branch pull requests

* Adds methods to get files changes between two commits
This commit is contained in:
Richard Mahn
2016-06-01 02:24:11 -06:00
committed by Unknwon
parent 731b9be71b
commit 0888f003de
3 changed files with 56 additions and 0 deletions

View File

@@ -180,6 +180,10 @@ func (c *Commit) SearchCommits(keyword string) (*list.List, error) {
return c.repo.searchCommits(c.ID, keyword)
}
func (c *Commit) GetFilesChangedSinceCommit(pastCommit string) ([]string, error) {
return c.repo.getFilesChanged(pastCommit, c.ID.String())
}
func (c *Commit) GetSubModules() (*objectCache, error) {
if c.submoduleCache != nil {
return c.submoduleCache, nil

44
repo.go
View File

@@ -76,6 +76,7 @@ type CloneRepoOptions struct {
Bare bool
Quiet bool
Timeout time.Duration
Branch string
}
// Clone clones original repository to target path.
@@ -95,6 +96,9 @@ func Clone(from, to string, opts CloneRepoOptions) (err error) {
if opts.Quiet {
cmd.AddArguments("--quiet")
}
if len(opts.Branch) > 0 {
cmd.AddArguments("-b", opts.Branch)
}
cmd.AddArguments(from, to)
if opts.Timeout <= 0 {
@@ -107,6 +111,8 @@ func Clone(from, to string, opts CloneRepoOptions) (err error) {
type PullRemoteOptions struct {
All bool
Remote string
Branch string
Timeout time.Duration
}
@@ -115,6 +121,9 @@ func Pull(repoPath string, opts PullRemoteOptions) error {
cmd := NewCommand("pull")
if opts.All {
cmd.AddArguments("--all")
} else {
cmd.AddArguments(opts.Remote)
cmd.AddArguments(opts.Branch)
}
if opts.Timeout <= 0 {
@@ -131,6 +140,33 @@ func Push(repoPath, remote, branch string) error {
return err
}
type CheckoutOptions struct {
Branch string
OldBranch string
Timeout time.Duration
}
// Checkout checkouts a branch
func Checkout(repoPath string, opts CheckoutOptions) error {
cmd := NewCommand("checkout")
if len(opts.OldBranch) > 0 {
cmd.AddArguments("-b")
}
if opts.Timeout <= 0 {
opts.Timeout = -1
}
cmd.AddArguments(opts.Branch)
if len(opts.OldBranch) > 0 {
cmd.AddArguments(opts.OldBranch)
}
_, err := cmd.RunInDirTimeout(opts.Timeout, repoPath)
return err
}
// ResetHEAD resets HEAD to given revision or head of branch.
func ResetHEAD(repoPath string, hard bool, revision string) error {
cmd := NewCommand("reset")
@@ -140,3 +176,11 @@ func ResetHEAD(repoPath string, hard bool, revision string) error {
_, err := cmd.AddArguments(revision).RunInDir(repoPath)
return err
}
// MoveFile moves a file to another file or directory
func MoveFile(repoPath, oldTreeName, newTreeName string) error {
cmd := NewCommand("mv")
cmd.AddArguments(oldTreeName, newTreeName)
_, err := cmd.RunInDir(repoPath)
return err
}

View File

@@ -187,6 +187,14 @@ func (repo *Repository) searchCommits(id sha1, keyword string) (*list.List, erro
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
}
func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
return commitsCount(repo.Path, revision, file)
}