Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Mar 5, 2024
1 parent ade6241 commit 1bb9cb7
Show file tree
Hide file tree
Showing 15 changed files with 180 additions and 81 deletions.
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,8 @@ var migrations = []Migration{
NewMigration("Use Slug instead of ID for Badges", v1_22.UseSlugInsteadOfIDForBadges),
// v288 -> v289
NewMigration("Add user_blocking table", v1_22.AddUserBlockingTable),
// v289 -> v290
NewMigration("Add default_wiki_branch to repository table", v1_22.AddDefaultWikiBranch),
}

// GetCurrentDBVersion returns the current db version
Expand Down
18 changes: 18 additions & 0 deletions models/migrations/v1_22/v289.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package v1_22

Check failure on line 4 in models/migrations/v1_22/v289.go

View workflow job for this annotation

GitHub Actions / lint-backend

var-naming: don't use an underscore in package name (revive)

Check failure on line 4 in models/migrations/v1_22/v289.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

var-naming: don't use an underscore in package name (revive)

Check failure on line 4 in models/migrations/v1_22/v289.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

var-naming: don't use an underscore in package name (revive)

import "xorm.io/xorm"

func AddDefaultWikiBranch(x *xorm.Engine) error {
type Repository struct {
ID int64
DefaultWikiBranch string
}
if err := x.Sync(&Repository{}); err != nil {
return err
}
_, err := x.Exec("UPDATE `repository` SET default_wiki_branch = 'master' WHERE (default_wiki_branch IS NULL) OR (default_wiki_branch = '')")
return err
}
1 change: 1 addition & 0 deletions models/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type Repository struct {
OriginalServiceType api.GitServiceType `xorm:"index"`
OriginalURL string `xorm:"VARCHAR(2048)"`
DefaultBranch string
DefaultWikiBranch string

NumWatches int
NumStars int
Expand Down
3 changes: 1 addition & 2 deletions modules/git/repo_base_gogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package git

import (
"context"
"errors"
"path/filepath"

gitealog "code.gitea.io/gitea/modules/log"
Expand Down Expand Up @@ -52,7 +51,7 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
if err != nil {
return nil, err
} else if !isDir(repoPath) {
return nil, errors.New("no such file or directory")
return nil, util.NewNotExistErrorf("no such file or directory")

Check failure on line 54 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-sqlite

undefined: util

Check failure on line 54 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: util

Check failure on line 54 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: util

Check failure on line 54 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: util

Check failure on line 54 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: util

Check failure on line 54 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: util

Check failure on line 54 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: util

Check failure on line 54 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: util

Check failure on line 54 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: util

Check failure on line 54 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: util

Check failure on line 54 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / test-pgsql

undefined: util

Check failure on line 54 in modules/git/repo_base_gogit.go

View workflow job for this annotation

GitHub Actions / backend

undefined: util
}

fs := osfs.New(repoPath)
Expand Down
4 changes: 2 additions & 2 deletions modules/git/repo_base_nogogit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ package git
import (
"bufio"
"context"
"errors"
"path/filepath"

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
)

func init() {
Expand Down Expand Up @@ -54,7 +54,7 @@ func OpenRepository(ctx context.Context, repoPath string) (*Repository, error) {
if err != nil {
return nil, err
} else if !isDir(repoPath) {
return nil, errors.New("no such file or directory")
return nil, util.NewNotExistErrorf("no such file or directory")
}

// Now because of some insanity with git cat-file not immediately failing if not run in a valid git directory we need to run git rev-parse first!
Expand Down
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2092,6 +2092,8 @@ settings.branches.add_new_rule = Add New Rule
settings.advanced_settings = Advanced Settings
settings.wiki_desc = Enable Repository Wiki
settings.use_internal_wiki = Use Built-In Wiki
settings.default_wiki_branch_name = Default Wiki Branch Name
settings.failed_to_change_default_wiki_branch = Failed to change the default wiki branch.
settings.use_external_wiki = Use External Wiki
settings.external_wiki_url = External Wiki URL
settings.external_wiki_url_error = The external wiki URL is not a valid URL.
Expand Down
7 changes: 7 additions & 0 deletions routers/web/repo/setting/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@ func SettingsPost(ctx *context.Context) {
}
}

if form.ChangeDefaultWikiBranch != "" {
if err := wiki_service.ChangeDefaultWikiBranch(ctx, repo, form.ChangeDefaultWikiBranch); err != nil {
log.Error("ChangeDefaultWikiBranch failed, err: %v", err)
ctx.Flash.Warning(ctx.Tr("repo.settings.failed_to_change_default_wiki_branch"))
}
}

if form.EnableIssues && form.EnableExternalTracker && !unit_model.TypeExternalTracker.UnitGlobalDisabled() {
if !validation.IsValidExternalURL(form.ExternalTrackerURL) {
ctx.Flash.Error(ctx.Tr("repo.settings.external_tracker_url_error"))
Expand Down
51 changes: 30 additions & 21 deletions routers/web/repo/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,31 @@ func findEntryForFile(commit *git.Commit, target string) (*git.TreeEntry, error)
}

func findWikiRepoCommit(ctx *context.Context) (*git.Repository, *git.Commit, error) {
wikiRepo, err := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
wikiGitRepo, err := gitrepo.OpenWikiRepository(ctx, ctx.Repo.Repository)
if err != nil {
ctx.ServerError("OpenRepository", err)
return nil, nil, err
}

commit, err := wikiRepo.GetBranchCommit(wiki_service.DefaultBranch)
commit, err := wikiGitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultWikiBranch)
if git.IsErrNotExist(err) {
// if the default branch recorded in database is out of sync, then re-sync it
if gitRepoDefaultBranch, err := wikiGitRepo.GetDefaultBranch(); err != nil {
return wikiGitRepo, nil, err
} else {

Check failure on line 107 in routers/web/repo/wiki.go

View workflow job for this annotation

GitHub Actions / lint-backend

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)

Check failure on line 107 in routers/web/repo/wiki.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)

Check failure on line 107 in routers/web/repo/wiki.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)
err := repo_model.UpdateRepositoryCols(ctx, &repo_model.Repository{ID: ctx.Repo.Repository.ID, DefaultWikiBranch: gitRepoDefaultBranch}, "default_wiki_branch")
if err != nil {
return wikiGitRepo, nil, err
}
ctx.Repo.Repository.DefaultWikiBranch = gitRepoDefaultBranch
}
// retry to get the commit from the correct default branch
commit, err = wikiGitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultWikiBranch)
}
if err != nil {
return wikiRepo, nil, err
return wikiGitRepo, nil, err
}
return wikiRepo, commit, nil
return wikiGitRepo, commit, nil
}

// wikiContentsByEntry returns the contents of the wiki page referenced by the
Expand Down Expand Up @@ -316,7 +330,7 @@ func renderViewPage(ctx *context.Context) (*git.Repository, *git.TreeEntry) {
}

// get commit count - wiki revisions
commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename)
commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.DefaultWikiBranch, pageFilename)
ctx.Data["CommitCount"] = commitsCount

return wikiRepo, entry
Expand Down Expand Up @@ -368,7 +382,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
ctx.Data["footerContent"] = ""

// get commit count - wiki revisions
commitsCount, _ := wikiRepo.FileCommitsCount(wiki_service.DefaultBranch, pageFilename)
commitsCount, _ := wikiRepo.FileCommitsCount(ctx.Repo.Repository.DefaultWikiBranch, pageFilename)
ctx.Data["CommitCount"] = commitsCount

// get page
Expand All @@ -380,7 +394,7 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)
// get Commit Count
commitsHistory, err := wikiRepo.CommitsByFileAndRange(
git.CommitsByFileAndRangeOptions{
Revision: wiki_service.DefaultBranch,
Revision: ctx.Repo.Repository.DefaultWikiBranch,
File: pageFilename,
Page: page,
})
Expand All @@ -402,20 +416,17 @@ func renderRevisionPage(ctx *context.Context) (*git.Repository, *git.TreeEntry)

func renderEditPage(ctx *context.Context) {
wikiRepo, commit, err := findWikiRepoCommit(ctx)
if err != nil {
defer func() {
if wikiRepo != nil {
wikiRepo.Close()
_ = wikiRepo.Close()
}
}()
if err != nil {
if !git.IsErrNotExist(err) {
ctx.ServerError("GetBranchCommit", err)
}
return
}
defer func() {
if wikiRepo != nil {
wikiRepo.Close()
}
}()

// get requested pagename
pageName := wiki_service.WebPathFromRequest(ctx.PathParamRaw("*"))
Expand Down Expand Up @@ -584,17 +595,15 @@ func WikiPages(ctx *context.Context) {
ctx.Data["CanWriteWiki"] = ctx.Repo.CanWrite(unit.TypeWiki) && !ctx.Repo.Repository.IsArchived

wikiRepo, commit, err := findWikiRepoCommit(ctx)
if err != nil {
if wikiRepo != nil {
wikiRepo.Close()
}
return
}
defer func() {
if wikiRepo != nil {
wikiRepo.Close()
_ = wikiRepo.Close()
}
}()
if err != nil {
ctx.Redirect(ctx.Repo.RepoLink + "/wiki")
return
}

entries, err := commit.ListEntries()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions services/forms/repo_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ type RepoSettingForm struct {
EnableCode bool
EnableWiki bool
EnableExternalWiki bool
ChangeDefaultWikiBranch string
ExternalWikiURL string
EnableIssues bool
EnableExternalTracker bool
Expand Down
2 changes: 2 additions & 0 deletions services/repository/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func initRepository(ctx context.Context, repoPath string, u *user_model.User, re
}

repo.DefaultBranch = setting.Repository.DefaultBranch
repo.DefaultWikiBranch = setting.Repository.DefaultBranch

if len(opts.DefaultBranch) > 0 {
repo.DefaultBranch = opts.DefaultBranch
Expand Down Expand Up @@ -240,6 +241,7 @@ func CreateRepositoryDirectly(ctx context.Context, doer, u *user_model.User, opt
TrustModel: opts.TrustModel,
IsMirror: opts.IsMirror,
DefaultBranch: opts.DefaultBranch,
DefaultWikiBranch: setting.Repository.DefaultBranch,
ObjectFormatName: opts.ObjectFormatName,
}

Expand Down
88 changes: 58 additions & 30 deletions services/repository/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,54 @@ import (
"code.gitea.io/gitea/modules/util"
)

func cloneWiki(ctx context.Context, u *user_model.User, opts migration.MigrateOptions, migrateTimeout time.Duration) (string, error) {
wikiPath := repo_model.WikiPath(u.Name, opts.RepoName)
wikiRemotePath := repo_module.WikiRemoteURL(ctx, opts.CloneAddr)
if wikiRemotePath == "" {
return "", nil
}

if err := util.RemoveAll(wikiPath); err != nil {
return "", fmt.Errorf("failed to remove existingi wiki dir %q, err: %w", wikiPath, err)
}

cleanIncompleteWikiPath := func() {
if err := util.RemoveAll(wikiPath); err != nil {
log.Error("Failed to remove incomplete wiki dir %q, err: %v", wikiPath, err)
}
}
if err := git.Clone(ctx, wikiRemotePath, wikiPath, git.CloneRepoOptions{
Mirror: true,
Quiet: true,
Timeout: migrateTimeout,
SkipTLSVerify: setting.Migrations.SkipTLSVerify,
}); err != nil {
log.Error("Clone wiki failed, err: %v", err)
cleanIncompleteWikiPath()
return "", err
}

if err := git.WriteCommitGraph(ctx, wikiPath); err != nil {
cleanIncompleteWikiPath()
return "", err
}

wikiRepo, err := git.OpenRepository(ctx, wikiPath)
if err != nil {
cleanIncompleteWikiPath()
return "", fmt.Errorf("failed to open wiki repo %q, err: %w", wikiPath, err)
}
defer wikiRepo.Close()

defaultBranch, err := wikiRepo.GetDefaultBranch()
if err != nil {
cleanIncompleteWikiPath()
return "", fmt.Errorf("failed to get wiki repo defaul brach for %q, err: %w", wikiPath, err)
}

return defaultBranch, nil
}

// MigrateRepositoryGitData starts migrating git related data after created migrating repository
func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
repo *repo_model.Repository, opts migration.MigrateOptions,
Expand All @@ -44,59 +92,39 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,

migrateTimeout := time.Duration(setting.Git.Timeout.Migrate) * time.Second

var err error
if err = util.RemoveAll(repoPath); err != nil {
return repo, fmt.Errorf("Failed to remove %s: %w", repoPath, err)
if err := util.RemoveAll(repoPath); err != nil {
return repo, fmt.Errorf("failed to remove existing repo dir %q, err: %w", repoPath, err)
}

if err = git.Clone(ctx, opts.CloneAddr, repoPath, git.CloneRepoOptions{
if err := git.Clone(ctx, opts.CloneAddr, repoPath, git.CloneRepoOptions{
Mirror: true,
Quiet: true,
Timeout: migrateTimeout,
SkipTLSVerify: setting.Migrations.SkipTLSVerify,
}); err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return repo, fmt.Errorf("Clone timed out. Consider increasing [git.timeout] MIGRATE in app.ini. Underlying Error: %w", err)
return repo, fmt.Errorf("clone timed out, consider increasing [git.timeout] MIGRATE in app.ini, underlying err: %w", err)
}
return repo, fmt.Errorf("Clone: %w", err)
return repo, fmt.Errorf("clone error: %w", err)
}

if err := git.WriteCommitGraph(ctx, repoPath); err != nil {
return repo, err
}

if opts.Wiki {
wikiPath := repo_model.WikiPath(u.Name, opts.RepoName)
wikiRemotePath := repo_module.WikiRemoteURL(ctx, opts.CloneAddr)
if len(wikiRemotePath) > 0 {
if err := util.RemoveAll(wikiPath); err != nil {
return repo, fmt.Errorf("Failed to remove %s: %w", wikiPath, err)
}

if err := git.Clone(ctx, wikiRemotePath, wikiPath, git.CloneRepoOptions{
Mirror: true,
Quiet: true,
Timeout: migrateTimeout,
Branch: "master",
SkipTLSVerify: setting.Migrations.SkipTLSVerify,
}); err != nil {
log.Warn("Clone wiki: %v", err)
if err := util.RemoveAll(wikiPath); err != nil {
return repo, fmt.Errorf("Failed to remove %s: %w", wikiPath, err)
}
} else {
if err := git.WriteCommitGraph(ctx, wikiPath); err != nil {
return repo, err
}
}
if defaultWikiBranch, err := cloneWiki(ctx, u, opts, migrateTimeout); err != nil {
return repo, fmt.Errorf("clone wiki error: %w", err)
} else {

Check failure on line 118 in services/repository/migrate.go

View workflow job for this annotation

GitHub Actions / lint-backend

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)

Check failure on line 118 in services/repository/migrate.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)

Check failure on line 118 in services/repository/migrate.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

indent-error-flow: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) (revive)
repo.DefaultWikiBranch = defaultWikiBranch
}
}

if repo.OwnerID == u.ID {
repo.Owner = u
}

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

Expand Down
Loading

0 comments on commit 1bb9cb7

Please sign in to comment.