1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
package linguist
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"gitlab.com/gitlab-org/gitaly/internal/config"
"gitlab.com/gitlab-org/gitaly/internal/helper"
)
var (
colorMap = make(map[string]Language)
)
// Language is used to parse Linguist's language.json file.
type Language struct {
Color string `json:"color"`
}
// Stats returns the repository's language stats as reported by 'git-linguist'.
func Stats(ctx context.Context, repoPath string, commitID string) (map[string]int, error) {
cmd := exec.Command("bundle", "exec", "bin/ruby-cd", repoPath, "git-linguist", "--commit="+commitID, "stats")
cmd.Dir = config.Config.Ruby.Dir
reader, err := helper.NewCommand(ctx, cmd, nil, nil, nil, os.Environ()...)
if err != nil {
return nil, err
}
defer reader.Close()
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
stats := make(map[string]int)
return stats, json.Unmarshal(data, &stats)
}
// Color returns the color Linguist has assigned to language.
func Color(language string) string {
if color := colorMap[language].Color; color != "" {
return color
}
colorSha := sha256.Sum256([]byte(language))
return fmt.Sprintf("#%x", colorSha[0:3])
}
// LoadColors loads the name->color map from the Linguist gem.
func LoadColors() error {
cmd := exec.Command("bundle", "show", "linguist")
cmd.Dir = config.Config.Ruby.Dir
linguistPath, err := cmd.Output()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
err = fmt.Errorf("%v; stderr: %q", exitError, exitError.Stderr)
}
return err
}
languageJSON, err := ioutil.ReadFile(path.Join(strings.TrimSpace(string(linguistPath)), "lib/linguist/languages.json"))
if err != nil {
return err
}
return json.Unmarshal(languageJSON, &colorMap)
}
|