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

Use git command instead of the ini package to remove the origin remote #25066

Merged
merged 7 commits into from
Jun 5, 2023
Merged
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
23 changes: 17 additions & 6 deletions modules/repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"net/http"
"os"
"path"
"strings"
"time"
Expand All @@ -27,7 +28,7 @@ import (
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"gopkg.in/ini.v1" //nolint:depguard
config "github.com/go-git/go-git/v5/config"
lunny marked this conversation as resolved.
Show resolved Hide resolved
)

/*
Expand Down Expand Up @@ -241,15 +242,25 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
// cleanUpMigrateGitConfig removes mirror info which prevents "push --all".
// This also removes possible user credentials.
func cleanUpMigrateGitConfig(configPath string) error {
cfg, err := ini.Load(configPath) // FIXME: the ini package doesn't really work with git config files
f, err := os.Open(configPath)
if err != nil {
return fmt.Errorf("open config file: %w", err)
}
cfg.DeleteSection("remote \"origin\"")
if err = cfg.SaveToIndent(configPath, "\t"); err != nil {
return fmt.Errorf("save config file: %w", err)
cfg, err := config.ReadConfig(f)
if err != nil {
f.Close()
return fmt.Errorf("read config: %w", err)
}
return nil
delete(cfg.Remotes, "origin")
bs, err := cfg.Marshal()
if err != nil {
f.Close()
return fmt.Errorf("marshal config file: %w", err)
}

stat, _ := f.Stat()
f.Close()
return os.WriteFile(configPath, bs, stat.Mode())
}

// CleanUpMigrateInfo finishes migrating repository and/or wiki with things that don't need to be done for mirrors.
Expand Down
42 changes: 42 additions & 0 deletions modules/repository/repo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package repository

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func Test_cleanUpMigrateGitConfig(t *testing.T) {
f, err := os.CreateTemp(os.TempDir(), "cleanUpMigrateGitConfig")
assert.NoError(t, err)

_, err = f.Write([]byte(`[core]
repositoryformatversion = 0
filemode = true
bare = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://oauth2:xxxxxxxxxxxxx@github.com/lunny/tango.git
`))
assert.NoError(t, err)

p := f.Name()
f.Close()

assert.NoError(t, cleanUpMigrateGitConfig(p))

bs, err := os.ReadFile(p)
assert.NoError(t, err)
assert.EqualValues(t, `[core]
repositoryformatversion = 0
filemode = true
bare = true
ignorecase = true
precomposeunicode = true
`, string(bs))
}