Skip to content

Support pushing to an anonymous remote #1512

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

Open
wants to merge 5 commits into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
git: incorporate most of the PR feedback
  • Loading branch information
mhagger committed Apr 21, 2025
commit e9bb4b83d76106c46031319d0f0c3615e277daf6
18 changes: 0 additions & 18 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ const (
SystemScope
)

// AnonymousRemoteName is the name that is used for anonymous remotes
// (i.e., remotes that are not defined in the git config).
const AnonymousRemoteName = "anonymous"

// Config contains the repository configuration
// https://www.kernel.org/pub/software/scm/git/docs/git-config.html#FILES
type Config struct {
Expand Down Expand Up @@ -655,13 +651,6 @@ type RemoteConfig struct {
raw *format.Subsection
}

func NewAnonymousRemoteConfig(url string) *RemoteConfig {
return &RemoteConfig{
Name: AnonymousRemoteName,
URLs: []string{url},
}
}

// Validate validates the fields and sets the default values.
func (c *RemoteConfig) Validate() error {
if c.Name == "" {
Expand Down Expand Up @@ -742,13 +731,6 @@ func (c *RemoteConfig) marshal() *format.Subsection {
return c.raw
}

// IsAnonymous returns true if c represents an anonymous remote (i.e.,
// one that was created from an explicit URL rather than from the
// gitconfig).
func (c *RemoteConfig) IsAnonymous() bool {
return c.Name == AnonymousRemoteName
}

func (c *RemoteConfig) IsFirstURLLocal() bool {
return url.IsLocalEndpoint(c.URLs[0])
}
Expand Down
15 changes: 3 additions & 12 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,23 +288,14 @@ func (s *ConfigSuite) TestValidateInvalidRemoteKey() {
s.ErrorIs(config.Validate(), ErrInvalid)
}

func (s *ConfigSuite) TestAnonymousRemoteConfig() {
func (s *ConfigSuite) TestEphemeralRemoteConfig() {
config := &RemoteConfig{
Name: "anonymous",
URLs: []string{"http://foo/bar"},
}

s.NoError(config.Validate())
Copy link
Member

Choose a reason for hiding this comment

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

The Validate logic will have to be slightly amended for this to work.

s.Equal("anonymous", config.Name)
s.True(config.IsAnonymous())
}

func (s *ConfigSuite) TestNewAnonymousRemoteConfig() {
config := NewAnonymousRemoteConfig("http://foo/bar")

s.NoError(config.Validate())
s.Equal("anonymous", config.Name)
s.True(config.IsAnonymous())
s.Empty(config.Name)
s.Len(config.URLs, 1)
}

func (s *ConfigSuite) TestRemoteConfigValidateMissingURL() {
Expand Down
12 changes: 6 additions & 6 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,12 +308,12 @@ type ForceWithLease struct {

// Validate validates the fields and sets the default values.
func (o *PushOptions) Validate() error {
if o.RemoteName == "" {
if o.RemoteURL != "" {
o.RemoteName = config.AnonymousRemoteName
} else {
o.RemoteName = DefaultRemoteName
}
if o.RemoteName != "" && o.RemoteURL != "" {
return errors.New("remote name and URL cannot be used together")
}

if o.RemoteName == "" && o.RemoteURL == "" {
o.RemoteName = DefaultRemoteName
}

if len(o.RefSpecs) == 0 {
Expand Down
21 changes: 14 additions & 7 deletions remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ func NewRemote(s storage.Storer, c *config.RemoteConfig) *Remote {
return &Remote{s: s, c: c}
}

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

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

func (r *Remote) String() string {
var name string
if r.c.Name != "" {
name = r.c.Name
} else {
name = "<unnamed>"
}
var fetch, push string
if len(r.c.URLs) > 0 {
fetch = r.c.URLs[0]
push = r.c.URLs[len(r.c.URLs)-1]
}

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

// Push performs a push to the remote. Returns NoErrAlreadyUpToDate if the
Expand Down Expand Up @@ -327,9 +333,10 @@ func (r *Remote) addReachableTags(localRefs []*plumbing.Reference, remoteRefs st
func (r *Remote) updateRemoteReferenceStorage(
cmds []*packp.Command,
) error {
if r.IsAnonymous() {
// If the remote is anonymous, then there are no
// remote-tracking references corresponding to it.
if r.isEphemeral() {
// If the remote is ephemeral (i.e., not configured in
// gitconfig), then there are no remote-tracking references
// corresponding to it.
return nil
}

Expand Down
37 changes: 3 additions & 34 deletions repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ var (
ErrRepositoryAlreadyExists = errors.New("repository already exists")
ErrRemoteNotFound = errors.New("remote not found")
ErrRemoteExists = errors.New("remote already exists")
ErrAnonymousRemoteName = errors.New("anonymous remote name must be 'anonymous'")
ErrWorktreeNotProvided = errors.New("worktree should be provided")
ErrIsBareRepository = errors.New("worktree not available in a bare repository")
ErrUnableToResolveCommit = errors.New("unable to resolve commit")
Expand Down Expand Up @@ -664,24 +663,6 @@ func (r *Repository) CreateRemote(c *config.RemoteConfig) (*Remote, error) {
return remote, r.Storer.SetConfig(cfg)
}

// CreateRemoteAnonymous creates a new anonymous remote. c.Name must
// be config.AnonymousRemoteName. It's used for the equivalent of
//
// git fetch git@github.com:src-d/go-git.git master:master
func (r *Repository) CreateRemoteAnonymous(c *config.RemoteConfig) (*Remote, error) {
if err := c.Validate(); err != nil {
return nil, err
}

if c.Name != config.AnonymousRemoteName {
return nil, ErrAnonymousRemoteName
}

remote := NewRemote(r.Storer, c)

return remote, nil
}

// DeleteRemote delete a remote from the repository and delete the config
func (r *Repository) DeleteRemote(name string) error {
cfg, err := r.Config()
Expand Down Expand Up @@ -1247,21 +1228,9 @@ func (r *Repository) PushContext(ctx context.Context, o *PushOptions) error {
return err
}

var remote *Remote
if o.RemoteName == config.AnonymousRemoteName {
var err error
remote, err = r.CreateRemoteAnonymous(
config.NewAnonymousRemoteConfig(o.RemoteURL),
)
if err != nil {
return err
}
} else {
var err error
remote, err = r.Remote(o.RemoteName)
if err != nil {
return err
}
remote, err := r.Remote(o.RemoteName)
if err != nil {
return err
}

return remote.PushContext(ctx, o)
Expand Down
34 changes: 15 additions & 19 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,34 +357,22 @@ func (s *RepositorySuite) TestCreateRemoteInvalid() {
s.Nil(remote)
}

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

s.NoError(err)
s.Equal("anonymous", remote.Config().Name)
s.True(remote.Config().IsAnonymous())
s.Equal("", remote.Config().Name)
}

func (s *RepositorySuite) TestCreateRemoteAnonymousInvalidName() {
func (s *RepositorySuite) TestCreateRemoteEphemeralInvalidName() {
r, _ := Init(memory.NewStorage(), nil)
remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{
remote := NewRemote(r.Storer, &config.RemoteConfig{
Name: "not_anonymous",
URLs: []string{"http://foo/foo.git"},
})

s.ErrorIs(err, ErrAnonymousRemoteName)
s.Nil(remote)
}

func (s *RepositorySuite) TestCreateRemoteAnonymousInvalid() {
r, _ := Init(memory.NewStorage(), nil)
remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{})

s.ErrorIs(err, config.ErrRemoteConfigEmptyName)
s.Nil(remote)
}

Expand Down Expand Up @@ -1717,7 +1705,7 @@ func (s *RepositorySuite) TestPush() {
})
}

func (s *RepositorySuite) TestPushAnonymous() {
func (s *RepositorySuite) TestPushEphemeral() {
url := s.T().TempDir()

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

Copy link
Member

Choose a reason for hiding this comment

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

Let's confirm no new remote was added.

Suggested change
s.Len(c.Remotes, 1)

s.Len(c.Remotes, 1)

AssertReferences(s.T(), server, map[string]string{
"refs/heads/master": "6ecf0ef2c2dffb796033e5a02219af86ec6584e5",
"refs/heads/branch": "e8d3ffab552895c19b9fcf7aa264d277cde33881",
})

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

func (s *RepositorySuite) TestPushContext() {
Expand Down
Loading
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