Move Git hook logic to Opengist (#213)

This commit is contained in:
Thomas Miceli
2024-01-23 20:24:01 +01:00
parent dfe70dc4cf
commit 7a75c5ecfa
16 changed files with 407 additions and 193 deletions

View File

@ -134,7 +134,7 @@ func gitHttp(ctx echo.Context) error {
gist.Uuid = strings.Replace(uuidGist.String(), "-", "", -1)
gist.Title = "gist:" + gist.Uuid
if err = gist.InitRepositoryViaInit(ctx); err != nil {
if err = gist.InitRepository(); err != nil {
return errorRes(500, "Cannot init repository in the file system", err)
}
@ -193,6 +193,7 @@ func pack(ctx echo.Context, serviceType string) error {
}
repositoryPath := getData(ctx, "repositoryPath").(string)
gist := getData(ctx, "gist").(*db.Gist)
var stderr bytes.Buffer
cmd := exec.Command("git", serviceType, "--stateless-rpc", repositoryPath)
@ -200,13 +201,15 @@ func pack(ctx echo.Context, serviceType string) error {
cmd.Stdin = reqBody
cmd.Stdout = ctx.Response().Writer
cmd.Stderr = &stderr
cmd.Env = os.Environ()
cmd.Env = append(cmd.Env, "OPENGIST_REPOSITORY_URL_INTERNAL="+git.RepositoryUrl(ctx, gist.User.Username, gist.Identifier()))
if err = cmd.Run(); err != nil {
return errorRes(500, "Cannot run git "+serviceType+" ; "+stderr.String(), err)
}
// updatedAt is updated only if serviceType is receive-pack
if serviceType == "receive-pack" {
gist := getData(ctx, "gist").(*db.Gist)
if hasNoCommits, err := git.HasNoCommits(gist.User.Username, gist.Uuid); err != nil {
return err

View File

@ -163,8 +163,8 @@ func usernameProcess(ctx echo.Context) error {
return redirect(ctx, "/settings")
}
sourceDir := filepath.Join(config.C.OpengistHome, git.ReposDirectory, strings.ToLower(user.Username))
destinationDir := filepath.Join(config.C.OpengistHome, git.ReposDirectory, strings.ToLower(dto.Username))
sourceDir := filepath.Join(config.GetHomeDir(), git.ReposDirectory, strings.ToLower(user.Username))
destinationDir := filepath.Join(config.GetHomeDir(), git.ReposDirectory, strings.ToLower(dto.Username))
if _, err := os.Stat(sourceDir); !os.IsNotExist(err) {
err := os.Rename(sourceDir, destinationDir)

View File

@ -125,6 +125,8 @@ func structToURLValues(s interface{}) url.Values {
}
func setup(t *testing.T) {
_ = os.Setenv("OPENGIST_SKIP_GIT_HOOKS", "1")
err := config.InitConfig("")
require.NoError(t, err, "Could not init config")
@ -159,7 +161,10 @@ func teardown(t *testing.T, s *testServer) {
err := db.Close()
require.NoError(t, err, "Could not close database")
err = os.RemoveAll(path.Join(config.C.OpengistHome, "tests"))
err = os.RemoveAll(path.Join(config.GetHomeDir(), "tests"))
require.NoError(t, err, "Could not remove repos directory")
err = os.RemoveAll(path.Join(config.GetHomeDir(), "tmp", "repos"))
require.NoError(t, err, "Could not remove repos directory")
// err = os.RemoveAll(path.Join(config.C.OpengistHome, "testsindex"))