-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pipeline: add Pipeline.Branch to define where to find CommitsHead
Signed-off-by: Robert Lin <[email protected]>
- Loading branch information
Showing
4 changed files
with
143 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"time" | ||
|
||
"gopkg.in/src-d/go-billy.v4/memfs" | ||
"gopkg.in/src-d/go-git.v4" | ||
"gopkg.in/src-d/go-git.v4/plumbing" | ||
"gopkg.in/src-d/go-git.v4/plumbing/object" | ||
|
@@ -75,3 +78,59 @@ func init() { | |
panic(err) | ||
} | ||
} | ||
|
||
// InMemRepositoryOptions declares config for NewInMemRepository | ||
type InMemRepositoryOptions struct { | ||
CreateBranch string | ||
} | ||
|
||
// InMemRepositoryOutput provides output from options provided in InMemRepositoryOptions | ||
type InMemRepositoryOutput struct { | ||
CreatedBranchHash plumbing.Hash | ||
} | ||
|
||
// NewInMemRepository initializes a new in-memory repository | ||
func NewInMemRepository(opts *InMemRepositoryOptions) (*git.Repository, InMemRepositoryOutput) { | ||
var out InMemRepositoryOutput | ||
|
||
repo, err := git.Clone(memory.NewStorage(), memfs.New(), &git.CloneOptions{ | ||
URL: "https://github.com/src-d/hercules", | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if opts != nil && opts.CreateBranch != "" { | ||
t, err := repo.Worktree() | ||
if err != nil { | ||
panic(err) | ||
} | ||
if _, err := t.Add("."); err != nil { | ||
panic(err) | ||
} | ||
if err := t.Checkout(&git.CheckoutOptions{ | ||
Branch: plumbing.NewBranchReferenceName(opts.CreateBranch), | ||
Create: true, | ||
}); err != nil { | ||
panic(err) | ||
} | ||
out.CreatedBranchHash, err = t.Commit( | ||
fmt.Sprintf("test commit on %s", opts.CreateBranch), | ||
&git.CommitOptions{ | ||
Author: &object.Signature{Name: "bobheadxi", Email: "[email protected]", When: time.Now()}, | ||
}, | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// check out master again | ||
if err := t.Checkout(&git.CheckoutOptions{ | ||
Branch: plumbing.NewBranchReferenceName("master"), | ||
}); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
return repo, out | ||
} |