Skip to content

Commit

Permalink
Ensure that git daemon export ok is created for mirrors
Browse files Browse the repository at this point in the history
There is an issue with go-gitea#16508 where it appears that create repo requires that the
repo does not exist. This causes go-gitea#17241 where an error is reported because of this.

This PR fixes this and also runs update-server-info for mirrors and generated repos.

Fix go-gitea#17241

Signed-off-by: Andrew Thornton <art27@cantab.net>
  • Loading branch information
zeripath committed Oct 6, 2021
1 parent 21a784e commit 8f1e238
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 26 deletions.
52 changes: 26 additions & 26 deletions models/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1146,16 +1146,6 @@ func CreateRepository(ctx context.Context, doer, u *User, repo *Repository, over
return fmt.Errorf("recalculateAccesses: %v", err)
}

if u.Visibility == api.VisibleTypePublic && !repo.IsPrivate {
// Create/Remove git-daemon-export-ok for git-daemon...
daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`)
if f, err := os.Create(daemonExportFile); err != nil {
log.Error("Failed to create %s: %v", daemonExportFile, err)
} else {
f.Close()
}
}

if setting.Service.AutoWatchNewRepos {
if err = watchRepo(db.GetEngine(ctx), doer.ID, repo.ID, true); err != nil {
return fmt.Errorf("watchRepo: %v", err)
Expand All @@ -1169,6 +1159,31 @@ func CreateRepository(ctx context.Context, doer, u *User, repo *Repository, over
return nil
}

// CheckDaemonExportOK creates/removes git-daemon-export-ok for git-daemon...
func (repo *Repository) CheckDaemonExportOK() error {
// Create/Remove git-daemon-export-ok for git-daemon...
daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`)
isExist, err := util.IsExist(daemonExportFile)
isPublic := !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePublic
if err != nil {
log.Error("Unable to check if %s exists. Error: %v", daemonExportFile, err)
return err
}
if !isPublic && isExist {
if err = util.Remove(daemonExportFile); err != nil {
log.Error("Failed to remove %s: %v", daemonExportFile, err)
}
} else if isPublic && !isExist {
if f, err := os.Create(daemonExportFile); err != nil {
log.Error("Failed to create %s: %v", daemonExportFile, err)
} else {
f.Close()
}
}

return nil
}

func countRepositories(userID int64, private bool) int64 {
sess := db.GetEngine(db.DefaultContext).Where("id > 0")

Expand Down Expand Up @@ -1318,24 +1333,9 @@ func updateRepository(e db.Engine, repo *Repository, visibilityChanged bool) (er
}

// Create/Remove git-daemon-export-ok for git-daemon...
daemonExportFile := path.Join(repo.RepoPath(), `git-daemon-export-ok`)
isExist, err := util.IsExist(daemonExportFile)
isPublic := !repo.IsPrivate && repo.Owner.Visibility == api.VisibleTypePublic
if err != nil {
log.Error("Unable to check if %s exists. Error: %v", daemonExportFile, err)
if err := repo.CheckDaemonExportOK(); err != nil {
return err
}
if !isPublic && isExist {
if err = util.Remove(daemonExportFile); err != nil {
log.Error("Failed to remove %s: %v", daemonExportFile, err)
}
} else if isPublic && !isExist {
if f, err := os.Create(daemonExportFile); err != nil {
log.Error("Failed to create %s: %v", daemonExportFile, err)
} else {
f.Close()
}
}

forkRepos, err := getRepositoriesByForkID(e, repo.ID)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions modules/repository/adopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func AdoptRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mode
if err := adoptRepository(ctx, repoPath, doer, repo, opts); err != nil {
return fmt.Errorf("createDelegateHooks: %v", err)
}
if err := repo.CheckDaemonExportOK(); err != nil {
return fmt.Errorf("checkDaemonExportOK: %v", err)
}

// Initialize Issue Labels if selected
if len(opts.IssueLabels) > 0 {
Expand Down
4 changes: 4 additions & 0 deletions modules/repository/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func CreateRepository(doer, u *models.User, opts models.CreateRepoOptions) (*mod
}
}

if err := repo.CheckDaemonExportOK(); err != nil {
return fmt.Errorf("checkDaemonExportOK: %v", err)
}

if stdout, err := git.NewCommand("update-server-info").
SetDescription(fmt.Sprintf("CreateRepository(git update-server-info): %s", repoPath)).
RunInDir(repoPath); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions modules/repository/fork.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func ForkRepository(doer, owner *models.User, opts models.ForkRepoOptions) (_ *m
return fmt.Errorf("git clone: %v", err)
}

if err := repo.CheckDaemonExportOK(); err != nil {
return fmt.Errorf("checkDaemonExportOK: %v", err)
}

if stdout, err := git.NewCommand("update-server-info").
SetDescription(fmt.Sprintf("ForkRepository(git update-server-info): %s", repo.FullName())).
RunInDir(repoPath); err != nil {
Expand Down
11 changes: 11 additions & 0 deletions modules/repository/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,5 +275,16 @@ func GenerateRepository(ctx context.Context, doer, owner *models.User, templateR
return generateRepo, err
}

if err = generateRepo.CheckDaemonExportOK(); err != nil {
return generateRepo, fmt.Errorf("checkDaemonExportOK: %v", err)
}

if stdout, err := git.NewCommand("update-server-info").
SetDescription(fmt.Sprintf("GenerateRepository(git update-server-info): %s", repoPath)).
RunInDir(repoPath); err != nil {
log.Error("GenerateRepository(git update-server-info) in %v: Stdout: %s\nError: %v", generateRepo, stdout, err)
return generateRepo, fmt.Errorf("error in GenerateRepository(git update-server-info): %v", err)
}

return generateRepo, nil
}
11 changes: 11 additions & 0 deletions modules/repository/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ func MigrateRepositoryGitData(ctx context.Context, u *models.User, repo *models.
}
}

if err = repo.CheckDaemonExportOK(); err != nil {
return repo, fmt.Errorf("checkDaemonExportOK: %v", err)
}

if stdout, err := git.NewCommand("update-server-info").
SetDescription(fmt.Sprintf("MigrateRepositoryGitData(git update-server-info): %s", repoPath)).
RunInDir(repoPath); err != nil {
log.Error("MigrateRepositoryGitData(git update-server-info) in %v: Stdout: %s\nError: %v", repo, stdout, err)
return repo, fmt.Errorf("error in MigrateRepositoryGitData(git update-server-info): %v", err)
}

gitRepo, err := git.OpenRepository(repoPath)
if err != nil {
return repo, fmt.Errorf("OpenRepository: %v", err)
Expand Down

0 comments on commit 8f1e238

Please sign in to comment.