From c78b640c2b33f047270594d40c38462557429670 Mon Sep 17 00:00:00 2001 From: "Jacob Vosmaer (GitLab)" Date: Thu, 31 Aug 2017 13:53:26 +0000 Subject: Use git-linguist to implement CommitLanguages --- internal/linguist/linguist.go | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 internal/linguist/linguist.go (limited to 'internal/linguist/linguist.go') diff --git a/internal/linguist/linguist.go b/internal/linguist/linguist.go new file mode 100644 index 000000000..497f3b9a3 --- /dev/null +++ b/internal/linguist/linguist.go @@ -0,0 +1,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) +} -- cgit v1.2.3