Repository: go-gitea/gitea
File: modules/git/attribute/checker.go (Go)

Code before:
		cmd.AddArguments("--all")
	}

	// there is treeish, read from bare repo or temp index created by "read-tree"
	if treeish != "" {
		if git.DefaultFeatures().SupportCheckAttrOnBare {
			cmd.AddArguments("--source")
			cmd.AddDynamicArguments(treeish)
		} else {
			indexFilename, worktree, deleteTemporaryFile, err := gitRepo.ReadTreeToTemporaryIndex(treeish)
			if err != nil {
				return nil, nil, nil, err
			}

			cmd.AddArguments("--cached")
			envs = append(envs,
				"GIT_INDEX_FILE="+indexFilename,
				"GIT_WORK_TREE="+worktree,
			)
			cancel = deleteTemporaryFile
		}
	} else if cached {
		cmd.AddArguments("--cached")
	} // else: no treeish, assume it is a not a bare repo, read from working directory

	cmd.AddDynamicArguments(attributes...)
	if len(filenames) > 0 {
		cmd.AddDashesAndList(filenames...)
	}
	return cmd, envs, cancel, nil
}

type CheckAttributeOpts struct {
	Filenames  []string
	Attributes []string
	Cached     bool
}

// CheckAttributes return the attributes of the given filenames and attributes in the given treeish.
// If treeish is empty, then it will use current working directory, otherwise it will use the provided treeish on the bare repo
func CheckAttributes(ctx context.Context, gitRepo *git.Repository, treeish string, opts CheckAttributeOpts) (map[string]*Attributes, error) {
	cmd, envs, cancel, err := checkAttrCommand(gitRepo, treeish, opts.Filenames, opts.Attributes, opts.Cached)
	if err != nil {
		return nil, err
	}
	defer cancel()

	stdOut := new(bytes.Buffer)
	stdErr := new(bytes.Buffer)

	if err := cmd.Run(ctx, &git.RunOpts{

Review comment (performance):
Please add some comments about why `cached` is used and since there is a regression, maybe we need some tests to make sure it won't break again.

Code after:
		cmd.AddArguments("--all")
	}

	// there is treeish, read from bare repo or temp index created by "read-tree"
	if treeish != "" {
		if git.DefaultFeatures().SupportCheckAttrOnBare {
			cmd.AddArguments("--source")
			cmd.AddDynamicArguments(treeish)
		} else {
			indexFilename, worktree, deleteTemporaryFile, err := gitRepo.ReadTreeToTemporaryIndex(treeish)
			if err != nil {
				return nil, nil, nil, err
			}

			cmd.AddArguments("--cached")
			envs = append(envs,
				"GIT_INDEX_FILE="+indexFilename,
				"GIT_WORK_TREE="+worktree,
			)
			cancel = deleteTemporaryFile
		}
	} else {
		// Read from existing index, in cases where the repo is bare and has an index,
		// or the work tree contains unstaged changes that shouldn't affect the attribute check.
		// It is caller's responsibility to add changed ".gitattributes" into the index if they want to respect the new changes.
		cmd.AddArguments("--cached")
	}

	cmd.AddDynamicArguments(attributes...)
	if len(filenames) > 0 {
		cmd.AddDashesAndList(filenames...)
	}
	return cmd, envs, cancel, nil
}

type CheckAttributeOpts struct {
	Filenames  []string
	Attributes []string
}

// CheckAttributes return the attributes of the given filenames and attributes in the given treeish.
// If treeish is empty, then it will use current working directory, otherwise it will use the provided treeish on the bare repo
func CheckAttributes(ctx context.Context, gitRepo *git.Repository, treeish string, opts CheckAttributeOpts) (map[string]*Attributes, error) {
	cmd, envs, cancel, err := checkAttrCommand(gitRepo, treeish, opts.Filenames, opts.Attributes)
	if err != nil {
		return nil, err
	}
	defer cancel()

	stdOut := new(bytes.Buffer)
	stdErr := new(bytes.Buffer)