diff options
Diffstat (limited to 'modules/git/repo_attribute.go')
-rw-r--r-- | modules/git/repo_attribute.go | 60 |
1 files changed, 43 insertions, 17 deletions
diff --git a/modules/git/repo_attribute.go b/modules/git/repo_attribute.go index 38818788f..596a91e80 100644 --- a/modules/git/repo_attribute.go +++ b/modules/git/repo_attribute.go @@ -30,10 +30,10 @@ type CheckAttributeOpts struct { func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[string]string, error) { env := []string{} - if len(opts.IndexFile) > 0 && CheckGitVersionAtLeast("1.7.8") == nil { + if len(opts.IndexFile) > 0 { env = append(env, "GIT_INDEX_FILE="+opts.IndexFile) } - if len(opts.WorkTree) > 0 && CheckGitVersionAtLeast("1.7.8") == nil { + if len(opts.WorkTree) > 0 { env = append(env, "GIT_WORK_TREE="+opts.WorkTree) } @@ -56,8 +56,7 @@ func (repo *Repository) CheckAttribute(opts CheckAttributeOpts) (map[string]map[ } } - // git check-attr --cached first appears in git 1.7.8 - if opts.CachedOnly && CheckGitVersionAtLeast("1.7.8") == nil { + if opts.CachedOnly { cmdArgs = append(cmdArgs, "--cached") } @@ -125,12 +124,12 @@ type CheckAttributeReader struct { func (c *CheckAttributeReader) Init(ctx context.Context) error { cmdArgs := []string{"check-attr", "--stdin", "-z"} - if len(c.IndexFile) > 0 && CheckGitVersionAtLeast("1.7.8") == nil { + if len(c.IndexFile) > 0 { cmdArgs = append(cmdArgs, "--cached") c.env = append(c.env, "GIT_INDEX_FILE="+c.IndexFile) } - if len(c.WorkTree) > 0 && CheckGitVersionAtLeast("1.7.8") == nil { + if len(c.WorkTree) > 0 { c.env = append(c.env, "GIT_WORK_TREE="+c.WorkTree) } @@ -160,17 +159,10 @@ func (c *CheckAttributeReader) Init(ctx context.Context) error { return err } - if CheckGitVersionAtLeast("1.8.5") == nil { - lw := new(nulSeparatedAttributeWriter) - lw.attributes = make(chan attributeTriple, 5) - lw.closed = make(chan struct{}) - c.stdOut = lw - } else { - lw := new(lineSeparatedAttributeWriter) - lw.attributes = make(chan attributeTriple, 5) - lw.closed = make(chan struct{}) - c.stdOut = lw - } + lw := new(nulSeparatedAttributeWriter) + lw.attributes = make(chan attributeTriple, 5) + lw.closed = make(chan struct{}) + c.stdOut = lw return nil } @@ -400,3 +392,37 @@ func (wr *lineSeparatedAttributeWriter) Close() error { close(wr.closed) return nil } + +// Create a check attribute reader for the current repository and provided commit ID +func (repo *Repository) CheckAttributeReader(commitID string) (*CheckAttributeReader, context.CancelFunc) { + indexFilename, worktree, deleteTemporaryFile, err := repo.ReadTreeToTemporaryIndex(commitID) + if err != nil { + return nil, func() {} + } + + checker := &CheckAttributeReader{ + Attributes: []string{"linguist-vendored", "linguist-generated", "linguist-language", "gitlab-language"}, + Repo: repo, + IndexFile: indexFilename, + WorkTree: worktree, + } + ctx, cancel := context.WithCancel(repo.Ctx) + if err := checker.Init(ctx); err != nil { + log.Error("Unable to open checker for %s. Error: %v", commitID, err) + } else { + go func() { + err := checker.Run() + if err != nil && err != ctx.Err() { + log.Error("Unable to open checker for %s. Error: %v", commitID, err) + } + cancel() + }() + } + deferable := func() { + _ = checker.Close() + cancel() + deleteTemporaryFile() + } + + return checker, deferable +} |