Run git gc for repositories (#90)

This commit is contained in:
Thomas Miceli
2023-09-04 11:11:54 +02:00
committed by GitHub
parent a7b346d8df
commit ffafde2b3e
4 changed files with 68 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package git
import (
"fmt"
"github.com/rs/zerolog/log"
"github.com/thomiceli/opengist/internal/config"
"os"
"os/exec"
@ -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()