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
Changes from 3 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
22 changes: 11 additions & 11 deletions modules/repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"fmt"
"io"
"net/http"
"path"
"strings"
"time"

Expand All @@ -26,8 +25,6 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

"gopkg.in/ini.v1" //nolint:depguard
)

/*
Expand Down Expand Up @@ -240,16 +237,19 @@ 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
func cleanUpMigrateGitConfig(ctx context.Context, repoPath string) error {
cmd := git.NewCommand(ctx, "remote", "rm", "origin")
// if the origin does not exist
stdout, _, err := cmd.RunStdString(&git.RunOpts{
lunny marked this conversation as resolved.
Show resolved Hide resolved
Dir: repoPath,
})
if err != nil {
return fmt.Errorf("open config file: %w", err)
return err
}
cfg.DeleteSection("remote \"origin\"")
if err = cfg.SaveToIndent(configPath, "\t"); err != nil {
return fmt.Errorf("save config file: %w", err)
if strings.HasPrefix(stdout, "fatal: No such remote") || stdout == "" {
lunny marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
return nil
return errors.New(stdout)
}

// CleanUpMigrateInfo finishes migrating repository and/or wiki with things that don't need to be done for mirrors.
Expand All @@ -270,7 +270,7 @@ func CleanUpMigrateInfo(ctx context.Context, repo *repo_model.Repository) (*repo
}

if repo.HasWiki() {
if err := cleanUpMigrateGitConfig(path.Join(repo.WikiPath(), "config")); err != nil {
if err := cleanUpMigrateGitConfig(ctx, repo.WikiPath()); err != nil {
return repo, fmt.Errorf("cleanUpMigrateGitConfig (wiki): %w", err)
}
}
Expand Down