Skip to content

Commit 0888f00

Browse files
richmahnunknwon
authored andcommitted
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
1 parent 731b9be commit 0888f00

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

commit.go

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ func (c *Commit) SearchCommits(keyword string) (*list.List, error) {
180180
return c.repo.searchCommits(c.ID, keyword)
181181
}
182182

183+
func (c *Commit) GetFilesChangedSinceCommit(pastCommit string) ([]string, error) {
184+
return c.repo.getFilesChanged(pastCommit, c.ID.String())
185+
}
186+
183187
func (c *Commit) GetSubModules() (*objectCache, error) {
184188
if c.submoduleCache != nil {
185189
return c.submoduleCache, nil

repo.go

+44
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type CloneRepoOptions struct {
7676
Bare bool
7777
Quiet bool
7878
Timeout time.Duration
79+
Branch string
7980
}
8081

8182
// Clone clones original repository to target path.
@@ -95,6 +96,9 @@ func Clone(from, to string, opts CloneRepoOptions) (err error) {
9596
if opts.Quiet {
9697
cmd.AddArguments("--quiet")
9798
}
99+
if len(opts.Branch) > 0 {
100+
cmd.AddArguments("-b", opts.Branch)
101+
}
98102
cmd.AddArguments(from, to)
99103

100104
if opts.Timeout <= 0 {
@@ -107,6 +111,8 @@ func Clone(from, to string, opts CloneRepoOptions) (err error) {
107111

108112
type PullRemoteOptions struct {
109113
All bool
114+
Remote string
115+
Branch string
110116
Timeout time.Duration
111117
}
112118

@@ -115,6 +121,9 @@ func Pull(repoPath string, opts PullRemoteOptions) error {
115121
cmd := NewCommand("pull")
116122
if opts.All {
117123
cmd.AddArguments("--all")
124+
} else {
125+
cmd.AddArguments(opts.Remote)
126+
cmd.AddArguments(opts.Branch)
118127
}
119128

120129
if opts.Timeout <= 0 {
@@ -131,6 +140,33 @@ func Push(repoPath, remote, branch string) error {
131140
return err
132141
}
133142

143+
type CheckoutOptions struct {
144+
Branch string
145+
OldBranch string
146+
Timeout time.Duration
147+
}
148+
149+
// Checkout checkouts a branch
150+
func Checkout(repoPath string, opts CheckoutOptions) error {
151+
cmd := NewCommand("checkout")
152+
if len(opts.OldBranch) > 0 {
153+
cmd.AddArguments("-b")
154+
}
155+
156+
if opts.Timeout <= 0 {
157+
opts.Timeout = -1
158+
}
159+
160+
cmd.AddArguments(opts.Branch)
161+
162+
if len(opts.OldBranch) > 0 {
163+
cmd.AddArguments(opts.OldBranch)
164+
}
165+
166+
_, err := cmd.RunInDirTimeout(opts.Timeout, repoPath)
167+
return err
168+
}
169+
134170
// ResetHEAD resets HEAD to given revision or head of branch.
135171
func ResetHEAD(repoPath string, hard bool, revision string) error {
136172
cmd := NewCommand("reset")
@@ -140,3 +176,11 @@ func ResetHEAD(repoPath string, hard bool, revision string) error {
140176
_, err := cmd.AddArguments(revision).RunInDir(repoPath)
141177
return err
142178
}
179+
180+
// MoveFile moves a file to another file or directory
181+
func MoveFile(repoPath, oldTreeName, newTreeName string) error {
182+
cmd := NewCommand("mv")
183+
cmd.AddArguments(oldTreeName, newTreeName)
184+
_, err := cmd.RunInDir(repoPath)
185+
return err
186+
}

repo_commit.go

+8
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,14 @@ func (repo *Repository) searchCommits(id sha1, keyword string) (*list.List, erro
187187
return repo.parsePrettyFormatLogToList(stdout)
188188
}
189189

190+
func (repo *Repository) getFilesChanged(id1 string, id2 string) ([]string, error) {
191+
stdout, err := NewCommand("diff", "--name-only", id1, id2).RunInDirBytes(repo.Path)
192+
if err != nil {
193+
return nil, err
194+
}
195+
return strings.Split(string(stdout), "\n"), nil
196+
}
197+
190198
func (repo *Repository) FileCommitsCount(revision, file string) (int64, error) {
191199
return commitsCount(repo.Path, revision, file)
192200
}

0 commit comments

Comments
 (0)