forked from src-d/go-git
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request src-d#803 from TheHipbot/branch-tracking-on-clone
config: adds branches to config for tracking branches against remotes…
- Loading branch information
Showing
6 changed files
with
507 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
|
||
"gopkg.in/src-d/go-git.v4/plumbing" | ||
format "gopkg.in/src-d/go-git.v4/plumbing/format/config" | ||
) | ||
|
||
var ( | ||
errBranchEmptyName = errors.New("branch config: empty name") | ||
errBranchInvalidMerge = errors.New("branch config: invalid merge") | ||
) | ||
|
||
// Branch contains information on the | ||
// local branches and which remote to track | ||
type Branch struct { | ||
// Name of branch | ||
Name string | ||
// Remote name of remote to track | ||
Remote string | ||
// Merge is the local refspec for the branch | ||
Merge plumbing.ReferenceName | ||
|
||
raw *format.Subsection | ||
} | ||
|
||
// Validate validates fields of branch | ||
func (b *Branch) Validate() error { | ||
if b.Name == "" { | ||
return errBranchEmptyName | ||
} | ||
|
||
if b.Merge != "" && !b.Merge.IsBranch() { | ||
return errBranchInvalidMerge | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (b *Branch) marshal() *format.Subsection { | ||
if b.raw == nil { | ||
b.raw = &format.Subsection{} | ||
} | ||
|
||
b.raw.Name = b.Name | ||
|
||
if b.Remote == "" { | ||
b.raw.RemoveOption(remoteSection) | ||
} else { | ||
b.raw.SetOption(remoteSection, b.Remote) | ||
} | ||
|
||
if b.Merge == "" { | ||
b.raw.RemoveOption(mergeKey) | ||
} else { | ||
b.raw.SetOption(mergeKey, string(b.Merge)) | ||
} | ||
|
||
return b.raw | ||
} | ||
|
||
func (b *Branch) unmarshal(s *format.Subsection) error { | ||
b.raw = s | ||
|
||
b.Name = b.raw.Name | ||
b.Remote = b.raw.Options.Get(remoteSection) | ||
b.Merge = plumbing.ReferenceName(b.raw.Options.Get(mergeKey)) | ||
|
||
return b.Validate() | ||
} |
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package config | ||
|
||
import ( | ||
. "gopkg.in/check.v1" | ||
"gopkg.in/src-d/go-git.v4/plumbing" | ||
) | ||
|
||
type BranchSuite struct{} | ||
|
||
var _ = Suite(&BranchSuite{}) | ||
|
||
func (b *BranchSuite) TestValidateName(c *C) { | ||
goodBranch := Branch{ | ||
Name: "master", | ||
Remote: "some_remote", | ||
Merge: "refs/heads/master", | ||
} | ||
badBranch := Branch{ | ||
Remote: "some_remote", | ||
Merge: "refs/heads/master", | ||
} | ||
c.Assert(goodBranch.Validate(), IsNil) | ||
c.Assert(badBranch.Validate(), NotNil) | ||
} | ||
|
||
func (b *BranchSuite) TestValidateMerge(c *C) { | ||
goodBranch := Branch{ | ||
Name: "master", | ||
Remote: "some_remote", | ||
Merge: "refs/heads/master", | ||
} | ||
badBranch := Branch{ | ||
Name: "master", | ||
Remote: "some_remote", | ||
Merge: "blah", | ||
} | ||
c.Assert(goodBranch.Validate(), IsNil) | ||
c.Assert(badBranch.Validate(), NotNil) | ||
} | ||
|
||
func (b *BranchSuite) TestMarshall(c *C) { | ||
expected := []byte(`[core] | ||
bare = false | ||
[branch "branch-tracking-on-clone"] | ||
remote = fork | ||
merge = refs/heads/branch-tracking-on-clone | ||
`) | ||
|
||
cfg := NewConfig() | ||
cfg.Branches["branch-tracking-on-clone"] = &Branch{ | ||
Name: "branch-tracking-on-clone", | ||
Remote: "fork", | ||
Merge: plumbing.ReferenceName("refs/heads/branch-tracking-on-clone"), | ||
} | ||
|
||
actual, err := cfg.Marshal() | ||
c.Assert(err, IsNil) | ||
c.Assert(string(actual), Equals, string(expected)) | ||
} | ||
|
||
func (b *BranchSuite) TestUnmarshall(c *C) { | ||
input := []byte(`[core] | ||
bare = false | ||
[branch "branch-tracking-on-clone"] | ||
remote = fork | ||
merge = refs/heads/branch-tracking-on-clone | ||
`) | ||
|
||
cfg := NewConfig() | ||
err := cfg.Unmarshal(input) | ||
c.Assert(err, IsNil) | ||
branch := cfg.Branches["branch-tracking-on-clone"] | ||
c.Assert(branch.Name, Equals, "branch-tracking-on-clone") | ||
c.Assert(branch.Remote, Equals, "fork") | ||
c.Assert(branch.Merge, Equals, plumbing.ReferenceName("refs/heads/branch-tracking-on-clone")) | ||
} |
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
Oops, something went wrong.