From 43a397ce9ae082e0c6e9367e31743e1cb4d71c20 Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Sun, 23 Aug 2020 11:02:35 -0500 Subject: Initial support for push options (#12169) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Initial support for push options Signed-off-by: jolheiser * Fix misspelling 🤦 Signed-off-by: jolheiser * Fix formatting after conflict resolution * defer close git repo * According the GitLab documentation, git >= 2.10 Signed-off-by: jolheiser * Words are hard. Thanks @mrsdizzie :sweat_smile: Co-authored-by: mrsdizzie * Only update if there are push options Signed-off-by: jolheiser Co-authored-by: mrsdizzie --- cmd/doctor.go | 31 +++++++++++++++++++++++++++++++ cmd/hook.go | 16 ++++++++++++++++ 2 files changed, 47 insertions(+) (limited to 'cmd') diff --git a/cmd/doctor.go b/cmd/doctor.go index 20c1904afa..2a93db27da 100644 --- a/cmd/doctor.go +++ b/cmd/doctor.go @@ -127,6 +127,12 @@ var checklist = []check{ isDefault: false, f: runDoctorUserStarNum, }, + { + title: "Enable push options", + name: "enable-push-options", + isDefault: false, + f: runDoctorEnablePushOptions, + }, // more checks please append here } @@ -605,3 +611,28 @@ func runDoctorCheckDBConsistency(ctx *cli.Context) ([]string, error) { return results, nil } + +func runDoctorEnablePushOptions(ctx *cli.Context) ([]string, error) { + numRepos := 0 + _, err := iterateRepositories(func(repo *models.Repository) ([]string, error) { + numRepos++ + r, err := git.OpenRepository(repo.RepoPath()) + if err != nil { + return nil, err + } + defer r.Close() + + if ctx.Bool("fix") { + _, err := git.NewCommand("config", "receive.advertisePushOptions", "true").RunInDir(r.Path) + return nil, err + } + + return nil, nil + }) + + var prefix string + if !ctx.Bool("fix") { + prefix = "DRY RUN: " + } + return []string{fmt.Sprintf("%sEnabled push options for %d repositories.", prefix, numRepos)}, err +} diff --git a/cmd/hook.go b/cmd/hook.go index f5658de7cd..863ed832a9 100644 --- a/cmd/hook.go +++ b/cmd/hook.go @@ -178,6 +178,7 @@ Gitea or set your environment appropriately.`, "") GitAlternativeObjectDirectories: os.Getenv(private.GitAlternativeObjectDirectories), GitObjectDirectory: os.Getenv(private.GitObjectDirectory), GitQuarantinePath: os.Getenv(private.GitQuarantinePath), + GitPushOptions: pushOptions(), ProtectedBranchID: prID, IsDeployKey: isDeployKey, } @@ -326,6 +327,7 @@ Gitea or set your environment appropriately.`, "") GitAlternativeObjectDirectories: os.Getenv(private.GitAlternativeObjectDirectories), GitObjectDirectory: os.Getenv(private.GitObjectDirectory), GitQuarantinePath: os.Getenv(private.GitQuarantinePath), + GitPushOptions: pushOptions(), } oldCommitIDs := make([]string, hookBatchSize) newCommitIDs := make([]string, hookBatchSize) @@ -438,3 +440,17 @@ func hookPrintResults(results []private.HookPostReceiveBranchResult) { os.Stderr.Sync() } } + +func pushOptions() map[string]string { + opts := make(map[string]string) + if pushCount, err := strconv.Atoi(os.Getenv(private.GitPushOptionCount)); err == nil { + for idx := 0; idx < pushCount; idx++ { + opt := os.Getenv(fmt.Sprintf("GIT_PUSH_OPTION_%d", idx)) + kv := strings.SplitN(opt, "=", 2) + if len(kv) == 2 { + opts[kv[0]] = kv[1] + } + } + } + return opts +} -- cgit v1.2.3