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 all commits
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
10 changes: 10 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,16 @@ func (s *ConfigSuite) TestValidateInvalidRemoteKey() {
s.ErrorIs(config.Validate(), ErrInvalid)
}

func (s *ConfigSuite) TestEphemeralRemoteConfig() {
config := &RemoteConfig{
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.Empty(config.Name)
s.Len(config.URLs, 1)
}

func (s *ConfigSuite) TestRemoteConfigValidateMissingURL() {
config := &RemoteConfig{Name: "foo"}
s.ErrorIs(config.Validate(), ErrRemoteConfigEmptyURL)
Expand Down
6 changes: 5 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,11 @@ type ForceWithLease struct {

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

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

Expand Down
25 changes: 24 additions & 1 deletion remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,32 @@ func NewRemote(s storage.Storer, c *config.RemoteConfig) *Remote {
return &Remote{s: s, c: c}
}

// 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) isEphemeral() bool {
return r.c.Name == ""
}

// Config returns the RemoteConfig object used to instantiate this Remote.
func (r *Remote) Config() *config.RemoteConfig {
return r.c
}

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 @@ -314,9 +327,19 @@ func (r *Remote) addReachableTags(localRefs []*plumbing.Reference, remoteRefs st
return nil
}

// updateRemoteReferenceStorage updates the remote references
// corresponding to the references that were just pushed to the
// remote, if needed.
func (r *Remote) updateRemoteReferenceStorage(
cmds []*packp.Command,
) error {
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
}

for _, spec := range r.c.Fetch {
for _, c := range cmds {
if !spec.Match(c.Name) {
Expand Down
17 changes: 0 additions & 17 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,22 +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 "anonymous".
// It's used like '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 != "anonymous" {
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
62 changes: 46 additions & 16 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,33 +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.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 @@ -1716,6 +1705,47 @@ func (s *RepositorySuite) TestPush() {
})
}

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

server, err := PlainInit(url, true)
s.NoError(err)

// Make sure that there is an "origin" remote. It will _not_ be
// used, but we want to make sure that its remote-tracking
// branches are not incorrectly updated by the push.
c, err := s.Repository.Config()
s.NoError(err)
if _, ok := c.Remotes["origin"]; !ok {
// Create an "origin" remote.
_, err = s.Repository.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{filepath.Join(url, "origin-not-used")},
})
s.NoError(err)
}

err = s.Repository.Push(&PushOptions{
RemoteURL: url,
})
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",
})

// 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() {
url, err := os.MkdirTemp("", "")
s.NoError(err)
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