Skip to content

Git designed for AI agents.

gitcmd: read the object format for attr.tree from the repository files

Federico Elia6dac5d99cd43
diff --git a/internal/git/gitcmd/command_factory.go b/internal/git/gitcmd/command_factory.go
index 8ecb60a..946c4f8 100644
--- a/internal/git/gitcmd/command_factory.go
+++ b/internal/git/gitcmd/command_factory.go
@@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
+ "io/fs"
"os"
"path/filepath"
"strings"
@@ -452,7 +453,10 @@ func (cf *ExecCommandFactory) newCommand(ctx context.Context, repo storage.Repos
//
// This can be removed once https://gitlab.com/gitlab-org/git/-/issues/316 is implemented and put in git upstream
- attrTreeConfig := cf.AttrTreeConfig(ctx, repo, sc, opts...)
+ attrTreeConfig, err := cf.AttrTreeConfig(ctx, repo, sc, opts...)
+ if err != nil {
+ return nil, err
+ }
if attrTreeConfig != nil {
opts = append(opts, WithConfig(*attrTreeConfig))
}
@@ -769,13 +773,19 @@ func (cf *ExecCommandFactory) trace2Finalizer(manager *trace2.Manager) func(cont
// AttrTreeConfig adds `attr.tree = HEAD` to the commands in the whiteList have,
// others has `attr.tree = emptyTreeHash`.
-func (cf *ExecCommandFactory) AttrTreeConfig(ctx context.Context, repo storage.Repository, sc Command, opts ...CmdOpt) *ConfigPair {
+//
+// The empty tree depends on the object format of the repository, which is read from the
+// configuration file of its common git directory, as git reads it, without starting a git process.
+// An error means that the object format cannot be determined: the command fails with it, because
+// running it without attr.tree would make git read the attributes from HEAD, the slow path that
+// attr.tree exists to avoid.
+func (cf *ExecCommandFactory) AttrTreeConfig(ctx context.Context, repo storage.Repository, sc Command, opts ...CmdOpt) (*ConfigPair, error) {
repoPath := cf.findRepoPath(ctx, repo, sc, opts...)
if len(repoPath) == 0 {
// Add tree config only when repo exists, because some git command, e.g. diff
// can exec without a repo, If we add an attr.tree when a command is executing
// without a repo, it leads to error, e.g. attempting to get main_ref_store outside of repository.
- return nil
+ return nil, nil
}
whiteList := map[string]struct{}{
@@ -787,48 +797,90 @@ func (cf *ExecCommandFactory) AttrTreeConfig(ctx context.Context, repo storage.R
"archive": {},
"log": {},
"format-patch": {},
- // Below commands do not need attributes. They are here
- // to avoid object hash resolving below as it causes
- // performance degradation with transactions.
+ // Below commands read attributes only on request (cat-file --filters and --textconv),
+ // or not at all. They keep attr.tree = HEAD so that cat-file's filters see the
+ // attributes of HEAD.
"cat-file": {},
"rev-parse": {},
"update-ref": {},
}
if _, ok := whiteList[sc.Name]; ok {
- return &ConfigPair{Key: "attr.tree", Value: "HEAD"}
+ return &ConfigPair{Key: "attr.tree", Value: "HEAD"}, nil
}
- // Execute git -C <repo path> rev-parse --show-object-format before setting
- // attr.tree to an empty tree. We need to decide the object format of the repo. SHA1 and SHA256
- // have different empty tree object ID.
- //
- // Note that we're not using `newCommand()` or `DetectObjectHash()`, but instead hand-craft the command.
- // This is required to avoid recursive git command spawning.
- execEnv := cf.GetExecutionEnvironment(ctx)
- var objectFormat bytes.Buffer
- findFormatCmd, err := command.New(ctx, cf.logger, []string{
- execEnv.BinaryPath,
- "-C", repoPath,
- "rev-parse", "--show-object-format",
- },
- command.WithEnvironment(execEnv.EnvironmentVariables),
- command.WithStdout(&objectFormat),
- )
+ objectHash, err := repositoryObjectHash(repoPath)
if err != nil {
- return nil
+ return nil, fmt.Errorf("detecting the object format for attr.tree: %w", err)
}
- if err := findFormatCmd.Wait(); err != nil {
- return nil
+
+ return &ConfigPair{Key: "attr.tree", Value: objectHash.EmptyTreeOID.String()}, nil
+}
+
+// repositoryObjectHash returns the object hash of the repository that git -C path would use, where
+// path is a git directory (a bare repository) or the root of a worktree, as findRepoPath returns
+// them. Its object format is the extensions.objectFormat of the configuration file of the common
+// git directory. A repository without a configuration file uses SHA-1, as git does.
+func repositoryObjectHash(path string) (git.ObjectHash, error) {
+ commonDir, err := commonGitDir(path)
+ if err != nil {
+ return git.ObjectHash{}, err
+ }
+
+ objectHash, err := DetectObjectHash(context.Background(), commonDir)
+ if errors.Is(err, fs.ErrNotExist) {
+ return git.ObjectHashSHA1, nil
}
- switch strings.TrimSpace(objectFormat.String()) {
- case git.ObjectHashSHA1.Format:
- return &ConfigPair{Key: "attr.tree", Value: git.ObjectHashSHA1.EmptyTreeOID.String()}
- case git.ObjectHashSHA256.Format:
- return &ConfigPair{Key: "attr.tree", Value: git.ObjectHashSHA256.EmptyTreeOID.String()}
+ return objectHash, err
+}
+
+// commonGitDir returns the common git directory of the repository at path. path is the git
+// directory itself unless it holds a .git entry: a .git directory is the git directory of a main
+// worktree, and a .git file ("gitdir: <dir>") names the git directory of a linked worktree. A
+// linked worktree's git directory names its common directory in its commondir file. Relative paths
+// are relative to the directory holding the file, as git resolves them.
+func commonGitDir(path string) (string, error) {
+ gitDir := path
+
+ dotGit := filepath.Join(path, ".git")
+ info, err := os.Stat(dotGit)
+ switch {
+ case errors.Is(err, fs.ErrNotExist):
+ case err != nil:
+ return "", fmt.Errorf("stat .git: %w", err)
+ case info.IsDir():
+ gitDir = dotGit
default:
- return nil
+ content, err := os.ReadFile(dotGit)
+ if err != nil {
+ return "", fmt.Errorf("reading .git file: %w", err)
+ }
+
+ target, ok := strings.CutPrefix(strings.TrimSpace(string(content)), "gitdir:")
+ if !ok {
+ return "", fmt.Errorf("invalid .git file %q", dotGit)
+ }
+
+ gitDir = strings.TrimSpace(target)
+ if !filepath.IsAbs(gitDir) {
+ gitDir = filepath.Join(path, gitDir)
+ }
+ }
+
+ commonDir, err := os.ReadFile(filepath.Join(gitDir, "commondir"))
+ switch {
+ case errors.Is(err, fs.ErrNotExist):
+ return gitDir, nil
+ case err != nil:
+ return "", fmt.Errorf("reading commondir: %w", err)
}
+
+ common := strings.TrimSpace(string(commonDir))
+ if !filepath.IsAbs(common) {
+ common = filepath.Join(gitDir, common)
+ }
+
+ return common, nil
}
// findRepoPath find the repo to execute git rev-parse --show-object-format
diff --git a/internal/git/gitcmd/command_factory_attrtree_test.go b/internal/git/gitcmd/command_factory_attrtree_test.go
new file mode 100644
index 0000000..bc9bfe2
--- /dev/null
+++ b/internal/git/gitcmd/command_factory_attrtree_test.go
@@ -0,0 +1,180 @@
+package gitcmd_test
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/feder-cr/git-ai/internal/git"
+ "github.com/feder-cr/git-ai/internal/git/gitcmd"
+ "github.com/feder-cr/git-ai/internal/git/gittest"
+ "github.com/feder-cr/git-ai/internal/gitaly/config"
+ "github.com/feder-cr/git-ai/internal/gitaly/storage"
+ "github.com/feder-cr/git-ai/internal/log"
+ "github.com/feder-cr/git-ai/internal/testhelper"
+ "github.com/feder-cr/git-ai/internal/testhelper/testcfg"
+ "github.com/stretchr/testify/require"
+)
+
+// bundledGit runs the bundled git with args and returns its trimmed output.
+func bundledGit(t *testing.T, cfg config.Cfg, args ...string) string {
+ t.Helper()
+ return strings.TrimSpace(string(gittest.Exec(t, cfg, args...)))
+}
+
+// TestRepositoryObjectHashMatchesGit checks that the object hash read from the files of a repository
+// is the object format git -C <path> rev-parse --show-object-format reports, for the paths a command
+// can run in: a bare repository, the root of a main worktree, the root of a linked worktree of a
+// non-bare and of a bare repository, and a repository without a configuration file.
+func TestRepositoryObjectHashMatchesGit(t *testing.T) {
+ t.Parallel()
+
+ cfg := testcfg.Build(t)
+
+ for _, format := range []string{"sha1", "sha256"} {
+ t.Run(format, func(t *testing.T) {
+ t.Parallel()
+
+ dir := testhelper.TempDir(t)
+
+ bare := filepath.Join(dir, "bare.git")
+ bundledGit(t, cfg, "init", "--quiet", "--bare", "--object-format="+format, bare)
+
+ main := filepath.Join(dir, "main")
+ bundledGit(t, cfg, "init", "--quiet", "--object-format="+format, main)
+ bundledGit(t, cfg, "-C", main, "commit", "--quiet", "--allow-empty", "-m", "first")
+
+ // A linked worktree: its .git is a file naming its git directory, whose commondir file
+ // names the main git directory, which holds the configuration.
+ linked := filepath.Join(dir, "linked")
+ bundledGit(t, cfg, "-C", main, "worktree", "add", "--quiet", "--detach", linked, "HEAD")
+ require.FileExists(t, filepath.Join(linked, ".git"))
+
+ // The same with a bare repository as the main git directory.
+ bundledGit(t, cfg, "-C", main, "push", "--quiet", bare, "HEAD:refs/heads/main")
+ bareLinked := filepath.Join(dir, "bare-linked")
+ bundledGit(t, cfg, "-C", bare, "worktree", "add", "--quiet", "--detach", bareLinked, "main")
+
+ for name, path := range map[string]string{
+ "bare repository": bare,
+ "main worktree": main,
+ "linked worktree": linked,
+ "worktree of the bare": bareLinked,
+ } {
+ t.Run(name, func(t *testing.T) {
+ want := bundledGit(t, cfg, "-C", path, "rev-parse", "--show-object-format")
+ require.Equal(t, format, want)
+
+ got, err := gitcmd.RepositoryObjectHash(path)
+ require.NoError(t, err)
+ require.Equal(t, want, got.Format)
+
+ common, err := gitcmd.CommonGitDir(path)
+ require.NoError(t, err)
+ wantCommon := bundledGit(t, cfg, "-C", path, "rev-parse", "--path-format=absolute", "--git-common-dir")
+ require.Equal(t, filepath.Clean(wantCommon), filepath.Clean(common))
+ })
+ }
+ })
+ }
+
+ t.Run("repository without a configuration file", func(t *testing.T) {
+ t.Parallel()
+
+ repo := filepath.Join(testhelper.TempDir(t), "repo.git")
+ bundledGit(t, cfg, "init", "--quiet", "--bare", repo)
+ require.NoError(t, os.Remove(filepath.Join(repo, "config")))
+
+ require.Equal(t, git.ObjectHashSHA1.Format, bundledGit(t, cfg, "-C", repo, "rev-parse", "--show-object-format"))
+ got, err := gitcmd.RepositoryObjectHash(repo)
+ require.NoError(t, err)
+ require.Equal(t, git.ObjectHashSHA1.Format, got.Format)
+ require.Equal(t, git.ObjectHashSHA1.EmptyTreeOID, got.EmptyTreeOID)
+ })
+}
+
+func TestRepositoryObjectHashErrors(t *testing.T) {
+ t.Parallel()
+
+ cfg := testcfg.Build(t)
+
+ t.Run("invalid .git file", func(t *testing.T) {
+ t.Parallel()
+
+ worktree := testhelper.TempDir(t)
+ require.NoError(t, os.WriteFile(filepath.Join(worktree, ".git"), []byte("not a gitdir line\n"), 0o600))
+
+ _, err := gitcmd.RepositoryObjectHash(worktree)
+ require.ErrorContains(t, err, "invalid .git file")
+ })
+
+ t.Run("unreadable configuration", func(t *testing.T) {
+ t.Parallel()
+
+ repo := filepath.Join(testhelper.TempDir(t), "repo.git")
+ bundledGit(t, cfg, "init", "--quiet", "--bare", repo)
+ configPath := filepath.Join(repo, "config")
+ require.NoError(t, os.Chmod(configPath, 0o000))
+ t.Cleanup(func() { require.NoError(t, os.Chmod(configPath, 0o600)) })
+
+ _, err := gitcmd.RepositoryObjectHash(repo)
+ require.Error(t, err)
+ })
+
+ t.Run("unknown object format", func(t *testing.T) {
+ t.Parallel()
+
+ repo := filepath.Join(testhelper.TempDir(t), "repo.git")
+ bundledGit(t, cfg, "init", "--quiet", "--bare", repo)
+ require.NoError(t, os.WriteFile(filepath.Join(repo, "config"),
+ []byte("[core]\n\trepositoryformatversion = 1\n[extensions]\n\tobjectformat = sha512\n"), 0o600))
+
+ _, err := gitcmd.RepositoryObjectHash(repo)
+ require.Error(t, err)
+ })
+}
+
+// TestExecCommandFactory_attrTreeErrorFailsTheCommand: a command that needs attr.tree = the empty
+// tree fails when the object format of its repository cannot be read, instead of running without
+// attr.tree.
+func TestExecCommandFactory_attrTreeErrorFailsTheCommand(t *testing.T) {
+ t.Parallel()
+
+ ctx := testhelper.Context(t)
+ cfg := testcfg.Build(t)
+ repo, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{SkipCreationViaService: true})
+
+ configPath := filepath.Join(repoPath, "config")
+ require.NoError(t, os.Chmod(configPath, 0o000))
+ t.Cleanup(func() { require.NoError(t, os.Chmod(configPath, 0o600)) })
+
+ cf := gittest.NewCommandFactory(t, cfg)
+ _, err := cf.New(ctx, repo, gitcmd.Command{Name: "rev-list", Flags: []gitcmd.Option{gitcmd.Flag{Name: "--all"}}})
+ require.ErrorContains(t, err, "detecting the object format for attr.tree")
+}
+
+// TestExecCommandFactory_attrTreeStartsNoProcess: a command outside the attr.tree whitelist runs one
+// process, its own: the object format is read from the repository's files.
+func TestExecCommandFactory_attrTreeStartsNoProcess(t *testing.T) {
+ t.Parallel()
+
+ ctx := log.InitContextCustomFields(testhelper.Context(t))
+ cfg := testcfg.Build(t)
+ repo, repoPath := gittest.CreateRepository(t, ctx, cfg, gittest.CreateRepositoryConfig{SkipCreationViaService: true})
+ gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch(git.DefaultBranch))
+
+ cf := gittest.NewCommandFactory(t, cfg)
+ run := func(repo storage.Repository) {
+ cmd, err := cf.New(ctx, repo, gitcmd.Command{Name: "rev-list", Flags: []gitcmd.Option{gitcmd.Flag{Name: "--all"}}})
+ require.NoError(t, err)
+ require.NoError(t, cmd.Wait())
+ }
+
+ // The first command also detects the version of git once.
+ run(repo)
+ before, _ := log.CustomFieldsFromContext(ctx).Fields()["command.count"].(int)
+ run(repo)
+ after, _ := log.CustomFieldsFromContext(ctx).Fields()["command.count"].(int)
+ require.Equal(t, 1, after-before)
+}
diff --git a/internal/git/gitcmd/command_factory_test.go b/internal/git/gitcmd/command_factory_test.go
index 02abcd2..1762c09 100644
--- a/internal/git/gitcmd/command_factory_test.go
+++ b/internal/git/gitcmd/command_factory_test.go
@@ -376,7 +376,8 @@ func TestExecCommandFactory_addAttrTree(t *testing.T) {
} {
t.Run(tc.desc, func(t *testing.T) {
cmd := gitcmd.Command{Name: tc.cmd}
- actualAttrTreeConfig := cf.AttrTreeConfig(ctx, tc.repo, cmd)
+ actualAttrTreeConfig, err := cf.AttrTreeConfig(ctx, tc.repo, cmd)
+ require.NoError(t, err)
require.Equal(t, tc.expectedAttrTreeConfig, actualAttrTreeConfig)
})
}
diff --git a/internal/git/gitcmd/export_test.go b/internal/git/gitcmd/export_test.go
new file mode 100644
index 0000000..01b798c
--- /dev/null
+++ b/internal/git/gitcmd/export_test.go
@@ -0,0 +1,8 @@
+package gitcmd
+
+// RepositoryObjectHash and CommonGitDir expose the attr.tree object-format detection to the tests of
+// package gitcmd_test, which compare it with what the bundled git reports.
+var (
+ RepositoryObjectHash = repositoryObjectHash
+ CommonGitDir = commonGitDir
+)
diff --git a/internal/gitaly/service/operations/land_batch_pack_test.go b/internal/gitaly/service/operations/land_batch_pack_test.go
index 81d677c..87fcaad 100644
--- a/internal/gitaly/service/operations/land_batch_pack_test.go
+++ b/internal/gitaly/service/operations/land_batch_pack_test.go
@@ -310,10 +310,9 @@ func TestLandBatchStepperLandsWithOneProcessPerMember(t *testing.T) {
}
// A rebase onto the running tip, a request already landed, and a fast-forward of the starting
- // tip each run one short git command: the range listing (git rev-list). The command factory
- // starts a second process for it: for a command outside its attr.tree list it runs git
- // rev-parse --show-object-format first (gitcmd AttrTreeConfig).
- const rangeListingProcesses = 2
+ // tip each run one short git process: the range listing (git rev-list). The command factory
+ // reads the object format for attr.tree from the repository's files, without a process.
+ const rangeListingProcesses = 1
second, processes := landWithProcesses(first.tip, f.diverged2)
require.True(t, second.rewritten)