Skip to content

Commit

Permalink
Added functions to handle checking out branches and moving files (#12)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
richmahn authored and unknwon committed Jun 1, 2016
1 parent 731b9be commit 0888f00
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type CloneRepoOptions struct {
Bare bool
Quiet bool
Timeout time.Duration
Branch string
}

// Clone clones original repository to target path.
Expand All @@ -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 {
Expand All @@ -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
}

Expand All @@ -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 {
Expand All @@ -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")
Expand All @@ -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
}
8 changes: 8 additions & 0 deletions repo_commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 0888f00

Please sign in to comment.