Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacob Vosmaer (GitLab) <jacob@gitlab.com>2017-08-31 16:53:27 +0300
committerJacob Vosmaer (GitLab) <jacob@gitlab.com>2017-08-31 16:53:27 +0300
commitee47129ffd082384f465a4a6365d1ffeae7b7c53 (patch)
tree25dbfe3abbb9df5c765b7146efb11d671ffe9001 /internal/linguist/linguist.go
parentaf66130f437865ef5a9a6dd0c1b1233bcaf50540 (diff)
parentc78b640c2b33f047270594d40c38462557429670 (diff)
Merge branch 'git-linguist' into 'master'
Use git-linguist to implement CommitLanguages See merge request !316
Diffstat (limited to 'internal/linguist/linguist.go')
-rw-r--r--internal/linguist/linguist.go74
1 files changed, 74 insertions, 0 deletions
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)
+}