Skip to content

Commit

Permalink
Add repo-sync-releases admin command
Browse files Browse the repository at this point in the history
Will help recovering corrupted database, see go-gitea#3247
  • Loading branch information
strk committed Dec 31, 2017
1 parent ae9cc8f commit ddc51c1
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ package cmd
import (
"fmt"

"code.gitea.io/git"
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"

"github.com/urfave/cli"
Expand All @@ -24,6 +26,7 @@ to make automatic initialization process more smoothly`,
Subcommands: []cli.Command{
subcmdCreateUser,
subcmdChangePassword,
subcmdRepoSyncReleases,
},
}

Expand Down Expand Up @@ -76,6 +79,12 @@ to make automatic initialization process more smoothly`,
},
},
}

subcmdRepoSyncReleases = cli.Command{
Name: "repo-sync-releases",
Usage: "Synchronize repository releases with tags",
Action: runRepoSyncReleases,
}
)

func runChangePassword(c *cli.Context) error {
Expand Down Expand Up @@ -145,3 +154,41 @@ func runCreateUser(c *cli.Context) error {
fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
return nil
}

func runRepoSyncReleases(c *cli.Context) error {

setting.NewContext()
models.LoadConfigs()

setting.NewXORMLogService(false)
if err := models.SetEngine(); err != nil {
return fmt.Errorf("models.SetEngine: %v", err)
}

repos, count, err := models.SearchRepositoryByName(&models.SearchRepoOptions{})
if err != nil {
log.Fatal(4, "SearchRepositoryByName: %v", err)
return nil
}
log.Trace("Found %d repos", count)
for _, repo := range repos {
log.Trace("Synchronizing repo %s with path %s", repo.FullName(), repo.RepoPath())
gitRepo, err := git.OpenRepository(repo.RepoPath())
if err != nil {
log.Warn("OpenRepository: %v", err)
continue
}

oldnum := repo.NumReleases
log.Trace(" currentgNumReleases is %d, running SyncReleasesWithTags", oldnum)

if err = models.SyncReleasesWithTags(repo, gitRepo); err != nil {
log.Warn("SyncReleasesWithTags: %v", err)
}

log.Trace("Repo %s releases synchronized to tags: from %d to %d",
repo.FullName(), oldnum, repo.NumReleases)
}

return nil
}

0 comments on commit ddc51c1

Please sign in to comment.