mirror of
https://github.com/hanzoai/forge.git
synced 2026-10-30 15:00:39 +00:00
Storage buckets, indexer names, the bleve analyzer, the lock prefix, SSH host keys, the markdown front-matter key, the container data paths, the RPM repo id and the API security scheme all read as this product now, and app.example.ini matches the code again. The upstream maintainer list and the changelog generator config went with the release history they belonged to, and the citation comments state the fact rather than link the issue. Three inert rules turn out to have been inert because they named a module path this tree does not use: gci grouped no first-party imports under prefix(), and depguard let models/migrations import models and modules/structs. They now name github.com/hanzoai/git. The config example also documented .gitea/workflows while the code has read .hanzo/workflows for some time. The authorized_keys marker follows the delegate-hook precedent: the new name is written and the old one is still recognised, because a marker we do not know is copied through as a hand-added key and leaves a revoked key authorized. The stored user_setting key keeps its value; only a migration may change that. Co-authored-by: Hanzo Dev <[email protected]>
193 lines
6.4 KiB
Go
193 lines
6.4 KiB
Go
// Copyright 2026 Hanzo AI, Inc. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/hanzoai/git/modules/git/gitcmd"
|
|
"github.com/hanzoai/git/modules/setting"
|
|
)
|
|
|
|
// syncGitConfig only modifies gitconfig, won't change global variables (otherwise there will be data-race problem)
|
|
func syncGitConfig(ctx context.Context) (err error) {
|
|
if err = os.MkdirAll(gitcmd.HomeDir(), os.ModePerm); err != nil {
|
|
return fmt.Errorf("unable to prepare git home directory %s, err: %w", gitcmd.HomeDir(), err)
|
|
}
|
|
|
|
// Git requires setting user.name and user.email in order to commit changes - old comment: "if they're not set just add some defaults"
|
|
// TODO: need to confirm whether users really need to change these values manually. It seems that these values are dummy only and not really used.
|
|
// If these values are not really used, then they can be set (overwritten) directly without considering about existence.
|
|
for configKey, defaultValue := range map[string]string{
|
|
"user.name": "Hanzo Git",
|
|
"user.email": "[email protected]",
|
|
} {
|
|
if err := configSetNonExist(ctx, configKey, defaultValue); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Set git some configurations - these must be set to these values for the forge to work correctly
|
|
if err := configSet(ctx, "core.quotePath", "false"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := configSet(ctx, "receive.advertisePushOptions", "true"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if DefaultFeatures().CheckVersionAtLeast("2.18") {
|
|
if err := configSet(ctx, "core.commitGraph", "true"); err != nil {
|
|
return err
|
|
}
|
|
if err := configSet(ctx, "gc.writeCommitGraph", "true"); err != nil {
|
|
return err
|
|
}
|
|
if err := configSet(ctx, "fetch.writeCommitGraph", "true"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if DefaultFeatures().SupportProcReceive {
|
|
// set support for AGit flow
|
|
if err := configAddNonExist(ctx, "receive.procReceiveRefs", "refs/for"); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if err := configUnsetAll(ctx, "receive.procReceiveRefs", "refs/for"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// Due to CVE-2022-24765, git now denies access to git directories which are not owned by current user.
|
|
// However, some docker users and samba users find it difficult to configure their systems correctly,
|
|
// so that Hanzo Git's git repositories are owned by the Hanzo Git user.
|
|
// (Possibly Windows Service users - but ownership in this case should really be set correctly on the filesystem.)
|
|
// See issue: a repository-level setting must not leak into the global config
|
|
// As Hanzo Git now always use its internal git config file, and access to the git repositories is managed through Hanzo Git,
|
|
// it is now safe to set "safe.directory=*" for internal usage only.
|
|
// Although this setting is only supported by some new git versions, it is also tolerated by earlier versions
|
|
if err := configAddNonExist(ctx, "safe.directory", "*"); err != nil {
|
|
return err
|
|
}
|
|
|
|
if runtime.GOOS == "windows" {
|
|
if err := configSet(ctx, "core.longpaths", "true"); err != nil {
|
|
return err
|
|
}
|
|
if setting.Git.DisableCoreProtectNTFS {
|
|
err = configSet(ctx, "core.protectNTFS", "false")
|
|
} else {
|
|
err = configUnsetAll(ctx, "core.protectNTFS", "false")
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
// By default partial clones are disabled, enable them from git v2.22
|
|
if !setting.Git.DisablePartialClone && DefaultFeatures().CheckVersionAtLeast("2.22") {
|
|
if err = configSet(ctx, "uploadpack.allowfilter", "true"); err != nil {
|
|
return err
|
|
}
|
|
err = configSet(ctx, "uploadpack.allowAnySHA1InWant", "true")
|
|
} else {
|
|
if err = configUnsetAll(ctx, "uploadpack.allowfilter", "true"); err != nil {
|
|
return err
|
|
}
|
|
err = configUnsetAll(ctx, "uploadpack.allowAnySHA1InWant", "true")
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Apply user's git config options last so they take precedence over builtin defaults
|
|
for k, v := range setting.GitConfig.Options {
|
|
if err = configSet(ctx, strings.ToLower(k), v); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func configSet(ctx context.Context, key, value string) error {
|
|
stdout, _, err := gitcmd.NewCommand("config", "--global", "--get").
|
|
AddDynamicArguments(key).
|
|
RunStdString(ctx)
|
|
if err != nil && !gitcmd.IsErrorExitCode(err, 1) {
|
|
return fmt.Errorf("failed to get git config %s, err: %w", key, err)
|
|
}
|
|
|
|
currValue := strings.TrimSpace(stdout)
|
|
if currValue == value {
|
|
return nil
|
|
}
|
|
|
|
if _, _, err = gitcmd.NewCommand("config", "--global").
|
|
AddDynamicArguments(key, value).
|
|
RunStdString(ctx); err != nil {
|
|
return fmt.Errorf("failed to set git global config %s, err: %w", key, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func configSetNonExist(ctx context.Context, key, value string) error {
|
|
_, _, err := gitcmd.NewCommand("config", "--global", "--get").AddDynamicArguments(key).RunStdString(ctx)
|
|
if err == nil {
|
|
// already exist
|
|
return nil
|
|
}
|
|
if gitcmd.IsErrorExitCode(err, 1) {
|
|
// not exist, set new config
|
|
_, _, err = gitcmd.NewCommand("config", "--global").AddDynamicArguments(key, value).RunStdString(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to set git global config %s, err: %w", key, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("failed to get git config %s, err: %w", key, err)
|
|
}
|
|
|
|
func configAddNonExist(ctx context.Context, key, value string) error {
|
|
_, _, err := gitcmd.NewCommand("config", "--global", "--get").AddDynamicArguments(key, regexp.QuoteMeta(value)).RunStdString(ctx)
|
|
if err == nil {
|
|
// already exist
|
|
return nil
|
|
}
|
|
if gitcmd.IsErrorExitCode(err, 1) {
|
|
// not exist, add new config
|
|
_, _, err = gitcmd.NewCommand("config", "--global", "--add").AddDynamicArguments(key, value).RunStdString(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to add git global config %s, err: %w", key, err)
|
|
}
|
|
return nil
|
|
}
|
|
return fmt.Errorf("failed to get git config %s, err: %w", key, err)
|
|
}
|
|
|
|
func configUnsetAll(ctx context.Context, key, value string) error {
|
|
_, _, err := gitcmd.NewCommand("config", "--global", "--get").AddDynamicArguments(key).RunStdString(ctx)
|
|
if err == nil {
|
|
// exist, need to remove
|
|
_, _, err = gitcmd.NewCommand("config", "--global", "--unset-all").AddDynamicArguments(key, regexp.QuoteMeta(value)).RunStdString(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to unset git global config %s, err: %w", key, err)
|
|
}
|
|
return nil
|
|
}
|
|
if gitcmd.IsErrorExitCode(err, 1) {
|
|
// not exist
|
|
return nil
|
|
}
|
|
return fmt.Errorf("failed to get git config %s, err: %w", key, err)
|
|
}
|