Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git: remote, fix default remote name #1009

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions options.go
Expand Up @@ -280,9 +280,9 @@ type ForceWithLease struct {
}

// Validate validates the fields and sets the default values.
func (o *PushOptions) Validate() error {
func (o *PushOptions) Validate(defaultRemoteName string) error {
if o.RemoteName == "" {
o.RemoteName = DefaultRemoteName
o.RemoteName = defaultRemoteName
}

if len(o.RefSpecs) == 0 {
Expand Down
14 changes: 14 additions & 0 deletions options_test.go
Expand Up @@ -90,6 +90,20 @@ func (s *OptionsSuite) TestCreateTagOptionsLoadGlobal(c *C) {
c.Assert(o.Tagger.Email, Equals, "foo@foo.com")
}

func (s *OptionsSuite) TestPushOptionsValidateDefaults(c *C) {
const defaultRemoteName = "foo"

o := &PushOptions{}

err := o.Validate(defaultRemoteName)
c.Assert(err, IsNil)

c.Assert(o.RemoteName, Equals, defaultRemoteName)
c.Assert(o.RefSpecs, DeepEquals, []config.RefSpec{
config.RefSpec(config.DefaultPushRefSpec),
})
}

func (s *OptionsSuite) writeGlobalConfig(c *C, cfg *config.Config) func() {
fs, clean := s.TemporalFilesystem()

Expand Down
26 changes: 13 additions & 13 deletions remote.go
Expand Up @@ -101,7 +101,7 @@ func (r *Remote) Push(o *PushOptions) error {
// operation is complete, an error is returned. The context only affects the
// transport operations.
func (r *Remote) PushContext(ctx context.Context, o *PushOptions) (err error) {
if err := o.Validate(); err != nil {
if err := o.Validate(r.c.Name); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of changing PushOptions.Validate why didn't you go for the simpler solution of doing:

Suggested change
if err := o.Validate(r.c.Name); err != nil {
// If the PushOptions don't have a remote name set - just default them to this remote for ease of use
if o.RemoteName == "" {
o.RemoteName = r.c.Name
}
if err := o.Validate(); err != nil {

Then this PR would reduce considerably in size.


One issue with both of these approaches is that the PushOptions are passed in as a pointer so any changes here permanently change the object meaning that if the PushOptions are reused then the result will be different depending on who used them first.

This isn't ideal and perhaps indicates that the Valdate option shouldn't really be changing the struct or it should be returning a duplicate with the default values set, but given that it does it might be that this PR may be inadvisable.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got you point. I will change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option: don't use RemoteName from PushOptions at all. Or create RemotePushOption struct without RemoteName field.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can anyone help me with failed tests?

return err
}

Expand Down Expand Up @@ -334,7 +334,6 @@ func (r *Remote) newReferenceUpdateRequest(
}

if err := r.addReferencesToUpdate(o.RefSpecs, localRefs, remoteRefs, req, o.Prune, o.ForceWithLease); err != nil {

return nil, err
}

Expand All @@ -350,7 +349,6 @@ func (r *Remote) newReferenceUpdateRequest(
func (r *Remote) updateRemoteReferenceStorage(
req *packp.ReferenceUpdateRequest,
) error {

for _, spec := range r.c.Fetch {
for _, c := range req.Commands {
if !spec.Match(c.Name) {
Expand Down Expand Up @@ -548,8 +546,8 @@ func newClient(url string, insecure bool, cabundle []byte, proxyOpts transport.P
}

func (r *Remote) fetchPack(ctx context.Context, o *FetchOptions, s transport.UploadPackSession,
req *packp.UploadPackRequest) (err error) {

req *packp.UploadPackRequest,
) (err error) {
reader, err := s.UploadPack(ctx, req)
if err != nil {
if errors.Is(err, transport.ErrEmptyUploadPackRequest) {
Expand Down Expand Up @@ -647,7 +645,8 @@ func (r *Remote) deleteReferences(rs config.RefSpec,
remoteRefs storer.ReferenceStorer,
refsDict map[string]*plumbing.Reference,
req *packp.ReferenceUpdateRequest,
prune bool) error {
prune bool,
) error {
iter, err := remoteRefs.IterReferences()
if err != nil {
return err
Expand Down Expand Up @@ -683,8 +682,8 @@ func (r *Remote) deleteReferences(rs config.RefSpec,

func (r *Remote) addCommit(rs config.RefSpec,
remoteRefs storer.ReferenceStorer, localCommit plumbing.Hash,
req *packp.ReferenceUpdateRequest) error {

req *packp.ReferenceUpdateRequest,
) error {
if rs.IsWildcard() {
return errors.New("can't use wildcard together with hash refspecs")
}
Expand Down Expand Up @@ -720,8 +719,8 @@ func (r *Remote) addCommit(rs config.RefSpec,

func (r *Remote) addReferenceIfRefSpecMatches(rs config.RefSpec,
remoteRefs storer.ReferenceStorer, localRef *plumbing.Reference,
req *packp.ReferenceUpdateRequest, forceWithLease *ForceWithLease) error {

req *packp.ReferenceUpdateRequest, forceWithLease *ForceWithLease,
) error {
if localRef.Type() != plumbing.HashReference {
return nil
}
Expand Down Expand Up @@ -816,7 +815,8 @@ func (r *Remote) references() ([]*plumbing.Reference, error) {
}

func getRemoteRefsFromStorer(remoteRefStorer storer.ReferenceStorer) (
map[plumbing.Hash]bool, error) {
map[plumbing.Hash]bool, error,
) {
remoteRefs := map[plumbing.Hash]bool{}
iter, err := remoteRefStorer.IterReferences()
if err != nil {
Expand Down Expand Up @@ -1116,8 +1116,8 @@ func isFastForward(s storer.EncodedObjectStorer, old, new plumbing.Hash, earlies
}

func (r *Remote) newUploadPackRequest(o *FetchOptions,
ar *packp.AdvRefs) (*packp.UploadPackRequest, error) {

ar *packp.AdvRefs,
) (*packp.UploadPackRequest, error) {
req := packp.NewUploadPackRequestFromCapabilities(ar.Capabilities)

if o.Depth != 0 {
Expand Down
12 changes: 12 additions & 0 deletions remote_test.go
Expand Up @@ -1215,6 +1215,18 @@ func (s *RemoteSuite) TestPushWrongRemoteName(c *C) {
c.Assert(err, ErrorMatches, ".*remote names don't match.*")
}

func (s *RemoteSuite) TestPushNonDefaultRemoteName(c *C) {
const remoteName = "foo"

r := NewRemote(nil, &config.RemoteConfig{
Name: remoteName,
URLs: []string{"some-url"},
})

err := r.Push(&PushOptions{})
c.Assert(err, ErrorMatches, "repository not found")
}

func (s *RemoteSuite) TestGetHaves(c *C) {
f := fixtures.Basic().One()
sto := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault())
Expand Down
2 changes: 1 addition & 1 deletion repository.go
Expand Up @@ -1221,7 +1221,7 @@ func (r *Repository) Push(o *PushOptions) error {
// operation is complete, an error is returned. The context only affects the
// transport operations.
func (r *Repository) PushContext(ctx context.Context, o *PushOptions) error {
if err := o.Validate(); err != nil {
if err := o.Validate(DefaultRemoteName); err != nil {
return err
}

Expand Down