From 0888f003de7c81e6b76e279e0b705e6b1bdc34fa Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Wed, 1 Jun 2016 02:24:11 -0600 Subject: [PATCH] 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 --- commit.go | 4 ++++ repo.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ repo_commit.go | 8 ++++++++ 3 files changed, 56 insertions(+) diff --git a/commit.go b/commit.go index acc12af8aed17..a7a205c3d8fe4 100644 --- a/commit.go +++ b/commit.go @@ -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 diff --git a/repo.go b/repo.go index 8019f7faa209c..4bec04f6d6038 100644 --- a/repo.go +++ b/repo.go @@ -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 +} diff --git a/repo_commit.go b/repo_commit.go index a653faf4a1006..286561bc54d91 100644 --- a/repo_commit.go +++ b/repo_commit.go @@ -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) }