Skip to content

Commit e9bb4b8

Browse files
committed
git: incorporate most of the PR feedback
1 parent 659e204 commit e9bb4b8

File tree

6 files changed

+41
-96
lines changed

6 files changed

+41
-96
lines changed

config/config.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ const (
5454
SystemScope
5555
)
5656

57-
// AnonymousRemoteName is the name that is used for anonymous remotes
58-
// (i.e., remotes that are not defined in the git config).
59-
const AnonymousRemoteName = "anonymous"
60-
6157
// Config contains the repository configuration
6258
// https://www.kernel.org/pub/software/scm/git/docs/git-config.html#FILES
6359
type Config struct {
@@ -655,13 +651,6 @@ type RemoteConfig struct {
655651
raw *format.Subsection
656652
}
657653

658-
func NewAnonymousRemoteConfig(url string) *RemoteConfig {
659-
return &RemoteConfig{
660-
Name: AnonymousRemoteName,
661-
URLs: []string{url},
662-
}
663-
}
664-
665654
// Validate validates the fields and sets the default values.
666655
func (c *RemoteConfig) Validate() error {
667656
if c.Name == "" {
@@ -742,13 +731,6 @@ func (c *RemoteConfig) marshal() *format.Subsection {
742731
return c.raw
743732
}
744733

745-
// IsAnonymous returns true if c represents an anonymous remote (i.e.,
746-
// one that was created from an explicit URL rather than from the
747-
// gitconfig).
748-
func (c *RemoteConfig) IsAnonymous() bool {
749-
return c.Name == AnonymousRemoteName
750-
}
751-
752734
func (c *RemoteConfig) IsFirstURLLocal() bool {
753735
return url.IsLocalEndpoint(c.URLs[0])
754736
}

config/config_test.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,23 +288,14 @@ func (s *ConfigSuite) TestValidateInvalidRemoteKey() {
288288
s.ErrorIs(config.Validate(), ErrInvalid)
289289
}
290290

291-
func (s *ConfigSuite) TestAnonymousRemoteConfig() {
291+
func (s *ConfigSuite) TestEphemeralRemoteConfig() {
292292
config := &RemoteConfig{
293-
Name: "anonymous",
294293
URLs: []string{"http://foo/bar"},
295294
}
296295

297296
s.NoError(config.Validate())
298-
s.Equal("anonymous", config.Name)
299-
s.True(config.IsAnonymous())
300-
}
301-
302-
func (s *ConfigSuite) TestNewAnonymousRemoteConfig() {
303-
config := NewAnonymousRemoteConfig("http://foo/bar")
304-
305-
s.NoError(config.Validate())
306-
s.Equal("anonymous", config.Name)
307-
s.True(config.IsAnonymous())
297+
s.Empty(config.Name)
298+
s.Len(config.URLs, 1)
308299
}
309300

310301
func (s *ConfigSuite) TestRemoteConfigValidateMissingURL() {

options.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,12 @@ type ForceWithLease struct {
308308

309309
// Validate validates the fields and sets the default values.
310310
func (o *PushOptions) Validate() error {
311-
if o.RemoteName == "" {
312-
if o.RemoteURL != "" {
313-
o.RemoteName = config.AnonymousRemoteName
314-
} else {
315-
o.RemoteName = DefaultRemoteName
316-
}
311+
if o.RemoteName != "" && o.RemoteURL != "" {
312+
return errors.New("remote name and URL cannot be used together")
313+
}
314+
315+
if o.RemoteName == "" && o.RemoteURL == "" {
316+
o.RemoteName = DefaultRemoteName
317317
}
318318

319319
if len(o.RefSpecs) == 0 {

remote.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ func NewRemote(s storage.Storer, c *config.RemoteConfig) *Remote {
7474
return &Remote{s: s, c: c}
7575
}
7676

77-
// IsAnonymous returns true if the remote is anonymous; i.e., if it
77+
// isEphemeral returns true if this is an ad hoc remote; i.e., if it
7878
// was created directly from a URL and isn't defined in the git
7979
// config.
80-
func (r *Remote) IsAnonymous() bool {
81-
return r.c.IsAnonymous()
80+
func (r *Remote) isEphemeral() bool {
81+
return r.c.Name == ""
8282
}
8383

8484
// Config returns the RemoteConfig object used to instantiate this Remote.
@@ -87,13 +87,19 @@ func (r *Remote) Config() *config.RemoteConfig {
8787
}
8888

8989
func (r *Remote) String() string {
90+
var name string
91+
if r.c.Name != "" {
92+
name = r.c.Name
93+
} else {
94+
name = "<unnamed>"
95+
}
9096
var fetch, push string
9197
if len(r.c.URLs) > 0 {
9298
fetch = r.c.URLs[0]
9399
push = r.c.URLs[len(r.c.URLs)-1]
94100
}
95101

96-
return fmt.Sprintf("%s\t%s (fetch)\n%[1]s\t%[3]s (push)", r.c.Name, fetch, push)
102+
return fmt.Sprintf("%s\t%s (fetch)\n%[1]s\t%[3]s (push)", name, fetch, push)
97103
}
98104

99105
// Push performs a push to the remote. Returns NoErrAlreadyUpToDate if the
@@ -327,9 +333,10 @@ func (r *Remote) addReachableTags(localRefs []*plumbing.Reference, remoteRefs st
327333
func (r *Remote) updateRemoteReferenceStorage(
328334
cmds []*packp.Command,
329335
) error {
330-
if r.IsAnonymous() {
331-
// If the remote is anonymous, then there are no
332-
// remote-tracking references corresponding to it.
336+
if r.isEphemeral() {
337+
// If the remote is ephemeral (i.e., not configured in
338+
// gitconfig), then there are no remote-tracking references
339+
// corresponding to it.
333340
return nil
334341
}
335342

repository.go

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ var (
5858
ErrRepositoryAlreadyExists = errors.New("repository already exists")
5959
ErrRemoteNotFound = errors.New("remote not found")
6060
ErrRemoteExists = errors.New("remote already exists")
61-
ErrAnonymousRemoteName = errors.New("anonymous remote name must be 'anonymous'")
6261
ErrWorktreeNotProvided = errors.New("worktree should be provided")
6362
ErrIsBareRepository = errors.New("worktree not available in a bare repository")
6463
ErrUnableToResolveCommit = errors.New("unable to resolve commit")
@@ -664,24 +663,6 @@ func (r *Repository) CreateRemote(c *config.RemoteConfig) (*Remote, error) {
664663
return remote, r.Storer.SetConfig(cfg)
665664
}
666665

667-
// CreateRemoteAnonymous creates a new anonymous remote. c.Name must
668-
// be config.AnonymousRemoteName. It's used for the equivalent of
669-
//
670-
// git fetch git@github.com:src-d/go-git.git master:master
671-
func (r *Repository) CreateRemoteAnonymous(c *config.RemoteConfig) (*Remote, error) {
672-
if err := c.Validate(); err != nil {
673-
return nil, err
674-
}
675-
676-
if c.Name != config.AnonymousRemoteName {
677-
return nil, ErrAnonymousRemoteName
678-
}
679-
680-
remote := NewRemote(r.Storer, c)
681-
682-
return remote, nil
683-
}
684-
685666
// DeleteRemote delete a remote from the repository and delete the config
686667
func (r *Repository) DeleteRemote(name string) error {
687668
cfg, err := r.Config()
@@ -1247,21 +1228,9 @@ func (r *Repository) PushContext(ctx context.Context, o *PushOptions) error {
12471228
return err
12481229
}
12491230

1250-
var remote *Remote
1251-
if o.RemoteName == config.AnonymousRemoteName {
1252-
var err error
1253-
remote, err = r.CreateRemoteAnonymous(
1254-
config.NewAnonymousRemoteConfig(o.RemoteURL),
1255-
)
1256-
if err != nil {
1257-
return err
1258-
}
1259-
} else {
1260-
var err error
1261-
remote, err = r.Remote(o.RemoteName)
1262-
if err != nil {
1263-
return err
1264-
}
1231+
remote, err := r.Remote(o.RemoteName)
1232+
if err != nil {
1233+
return err
12651234
}
12661235

12671236
return remote.PushContext(ctx, o)

repository_test.go

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -357,34 +357,22 @@ func (s *RepositorySuite) TestCreateRemoteInvalid() {
357357
s.Nil(remote)
358358
}
359359

360-
func (s *RepositorySuite) TestCreateRemoteAnonymous() {
360+
func (s *RepositorySuite) TestCreateRemoteEphemeral() {
361361
r, _ := Init(memory.NewStorage(), nil)
362-
remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{
363-
Name: "anonymous",
362+
remote := NewRemote(r.Storer, &config.RemoteConfig{
364363
URLs: []string{"http://foo/foo.git"},
365364
})
366365

367-
s.NoError(err)
368-
s.Equal("anonymous", remote.Config().Name)
369-
s.True(remote.Config().IsAnonymous())
366+
s.Equal("", remote.Config().Name)
370367
}
371368

372-
func (s *RepositorySuite) TestCreateRemoteAnonymousInvalidName() {
369+
func (s *RepositorySuite) TestCreateRemoteEphemeralInvalidName() {
373370
r, _ := Init(memory.NewStorage(), nil)
374-
remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{
371+
remote := NewRemote(r.Storer, &config.RemoteConfig{
375372
Name: "not_anonymous",
376373
URLs: []string{"http://foo/foo.git"},
377374
})
378375

379-
s.ErrorIs(err, ErrAnonymousRemoteName)
380-
s.Nil(remote)
381-
}
382-
383-
func (s *RepositorySuite) TestCreateRemoteAnonymousInvalid() {
384-
r, _ := Init(memory.NewStorage(), nil)
385-
remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{})
386-
387-
s.ErrorIs(err, config.ErrRemoteConfigEmptyName)
388376
s.Nil(remote)
389377
}
390378

@@ -1717,7 +1705,7 @@ func (s *RepositorySuite) TestPush() {
17171705
})
17181706
}
17191707

1720-
func (s *RepositorySuite) TestPushAnonymous() {
1708+
func (s *RepositorySuite) TestPushEphemeral() {
17211709
url := s.T().TempDir()
17221710

17231711
server, err := PlainInit(url, true)
@@ -1742,12 +1730,20 @@ func (s *RepositorySuite) TestPushAnonymous() {
17421730
})
17431731
s.NoError(err)
17441732

1733+
s.Len(c.Remotes, 1)
1734+
17451735
AssertReferences(s.T(), server, map[string]string{
17461736
"refs/heads/master": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
17471737
"refs/heads/branch": "e8d3ffab552895c19b9fcf7aa264d277cde33881",
17481738
})
17491739

1750-
AssertReferences(s.T(), s.Repository, map[string]string{})
1740+
// Make sure that remote-tracking references were _not_ created.
1741+
refs, err := s.Repository.References()
1742+
s.NoError(err)
1743+
refs.ForEach(func(r *plumbing.Reference) error {
1744+
s.False(strings.HasPrefix(r.Name().String(), "refs/remotes/"))
1745+
return nil
1746+
})
17511747
}
17521748

17531749
func (s *RepositorySuite) TestPushContext() {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy