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

Run git gc for repositories #90

Merged
merged 2 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 42 additions & 0 deletions internal/git/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package git

import (
"fmt"
"github.com/rs/zerolog/log"
"github.com/thomiceli/opengist/internal/config"
"os"
"os/exec"
Expand Down Expand Up @@ -263,6 +264,47 @@ func RPC(user string, gist string, service string) ([]byte, error) {
return stdout, err
}

func GcRepos() error {
subdirs, err := os.ReadDir(filepath.Join(config.GetHomeDir(), "repos"))
if err != nil {
return err
}

for _, subdir := range subdirs {
if !subdir.IsDir() {
continue
}

subRoot := filepath.Join(config.GetHomeDir(), "repos", subdir.Name())

gitRepos, err := os.ReadDir(subRoot)
if err != nil {
log.Warn().Err(err).Msg("Cannot read directory")
continue
}

for _, repo := range gitRepos {
if !repo.IsDir() {
continue
}

repoPath := filepath.Join(subRoot, repo.Name())

log.Info().Msg("Running git gc for repository " + repoPath)

cmd := exec.Command("git", "gc")
cmd.Dir = repoPath
err = cmd.Run()
if err != nil {
log.Warn().Err(err).Msg("Cannot run git gc for repository " + repoPath)
continue
}
}
}

return err
}

func GetGitVersion() (string, error) {
cmd := exec.Command("git", "--version")
stdout, err := cmd.Output()
Expand Down
19 changes: 19 additions & 0 deletions internal/web/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
var (
syncReposFromFS = false
syncReposFromDB = false
gitGcRepos = false
)

func adminIndex(ctx echo.Context) error {
Expand Down Expand Up @@ -51,6 +52,7 @@ func adminIndex(ctx echo.Context) error {

setData(ctx, "syncReposFromFS", syncReposFromFS)
setData(ctx, "syncReposFromDB", syncReposFromDB)
setData(ctx, "gitGcRepos", gitGcRepos)
return html(ctx, "admin_index.html")
}

Expand Down Expand Up @@ -185,6 +187,23 @@ func adminSyncReposFromDB(ctx echo.Context) error {
return redirect(ctx, "/admin-panel")
}

func adminGcRepos(ctx echo.Context) error {
addFlash(ctx, "Garbage collecting repositories...", "success")
go func() {
if gitGcRepos {
return
}
gitGcRepos = true
if err := git.GcRepos(); err != nil {
log.Error().Err(err).Msg("Error garbage collecting repositories")
gitGcRepos = false
return
}
gitGcRepos = false
}()
return redirect(ctx, "/admin-panel")
}

func adminConfig(ctx echo.Context) error {
setData(ctx, "title", "Configuration")
setData(ctx, "htmlTitle", "Configuration - Admin panel")
Expand Down
1 change: 1 addition & 0 deletions internal/web/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func Start() {
g2.POST("/gists/:gist/delete", adminGistDelete)
g2.POST("/sync-fs", adminSyncReposFromFS)
g2.POST("/sync-db", adminSyncReposFromDB)
g2.POST("/gc-repos", adminGcRepos)
g2.GET("/configuration", adminConfig)
g2.PUT("/set-config", adminSetConfig)
}
Expand Down
6 changes: 6 additions & 0 deletions templates/pages/admin_index.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
Synchronize gists from database
</button>
</form>
<form action="/admin-panel/gc-repos" method="POST">
{{ .csrfHtml }}
<button type="submit" {{ if .gitGcRepos }}disabled="disabled"{{ end }} class="whitespace-nowrap text-slate-700 dark:text-slate-300{{ if .gitGcRepos }} text-slate-500 cursor-not-allowed {{ end }}rounded border border-gray-300 dark:border-gray-600 bg-gray-50 dark:bg-gray-800 px-2.5 py-2 text-xs font-medium text-gray-700 dark:text-white shadow-sm hover:bg-gray-100 dark:hover:bg-gray-700 hover:border-gray-500 hover:text-slate-700 dark:hover:text-slate-300 focus:outline-none focus:ring-1 focus:border-primary-500 focus:ring-primary-500 leading-3">
Garbage collect git repositories
</button>
</form>
</div>
</div>
</div>
Expand Down