diff options
author | Jacob Vosmaer (GitLab) <jacob@gitlab.com> | 2018-04-13 14:17:52 +0300 |
---|---|---|
committer | Jacob Vosmaer (GitLab) <jacob@gitlab.com> | 2018-04-13 14:17:52 +0300 |
commit | aac12afbcb1426cbbc1d150486ed3a03328dbc88 (patch) | |
tree | 06c611ea39ec16e3fb11a0b6955b68dd959df700 | |
parent | 093fce4668874ef7fa07ded0761f33c3b13ef3c8 (diff) | |
parent | a67f5e3debd9cfef3275e53277982e8f57fdc874 (diff) |
Merge branch 'languages-json-env' into 'master'
Add config option to point to languages.json
Closes #1119
See merge request gitlab-org/gitaly!652
-rw-r--r-- | CHANGELOG.md | 5 | ||||
-rw-r--r-- | doc/configuration/README.md | 1 | ||||
-rw-r--r-- | internal/config/ruby.go | 1 | ||||
-rw-r--r-- | internal/linguist/linguist.go | 31 | ||||
-rw-r--r-- | internal/linguist/linguist_test.go | 32 | ||||
-rw-r--r-- | internal/linguist/testdata/fake-languages.json | 3 |
6 files changed, 64 insertions, 9 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index ebfa3c2bc..85c5e49a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Gitaly changelog +UNRELEASED + +- Add config option to point to languages.json + https://gitlab.com/gitlab-org/gitaly/merge_requests/652 + v0.96.1 - Vendor gitlab_git at 7e3bb679a92156304 diff --git a/doc/configuration/README.md b/doc/configuration/README.md index f9dce0b5d..e7778f6ba 100644 --- a/doc/configuration/README.md +++ b/doc/configuration/README.md @@ -111,6 +111,7 @@ max\_rss limit. |graceful_restart_timeout|string|no|Grace period to allow a gitaly-ruby process to finish ongoing requests. Default 10 minutes ("10m").| |restart_delay|string|no|Time memory must be high before a restart is triggered, in seconds. Default 5 minutes ("5m").| |num_workers|integer|no|Number of gitaly-ruby worker processes. Default 2, minimum 2.| +|linguist_languages_path|string|no|Override for dynamic languages.json discovery. Default: "" (use dynamic discovery).| ### Logging diff --git a/internal/config/ruby.go b/internal/config/ruby.go index 462dd93a6..9ec0fd3b0 100644 --- a/internal/config/ruby.go +++ b/internal/config/ruby.go @@ -14,6 +14,7 @@ type Ruby struct { RestartDelay time.Duration RestartDelayToml duration `toml:"restart_delay"` NumWorkers int `toml:"num_workers"` + LinguistLanguagesPath string `toml:"linguist_languages_path"` } // This type is a trick to let our TOML library parse durations from strings. diff --git a/internal/linguist/linguist.go b/internal/linguist/linguist.go index f2158a566..9c7feeb2f 100644 --- a/internal/linguist/linguist.go +++ b/internal/linguist/linguist.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "encoding/json" "fmt" + "io" "io/ioutil" "os" "os/exec" @@ -53,14 +54,31 @@ func Color(language string) string { // LoadColors loads the name->color map from the Linguist gem. func LoadColors() error { - linguistPathSymlink, err := ioutil.TempFile("", "gitaly-linguist-path") + jsonReader, err := openLanguagesJSON() if err != nil { return err } + defer jsonReader.Close() + + return json.NewDecoder(jsonReader).Decode(&colorMap) +} + +func openLanguagesJSON() (io.ReadCloser, error) { + if jsonPath := config.Config.Ruby.LinguistLanguagesPath; jsonPath != "" { + // This is a fallback for environments where dynamic discovery of the + // linguist path via Bundler is not working for some reason, for example + // https://gitlab.com/gitlab-org/gitaly/issues/1119. + return os.Open(jsonPath) + } + + linguistPathSymlink, err := ioutil.TempFile("", "gitaly-linguist-path") + if err != nil { + return nil, err + } defer os.Remove(linguistPathSymlink.Name()) if err := linguistPathSymlink.Close(); err != nil { - return err + return nil, err } // We use a symlink because we cannot trust Bundler to not print garbage @@ -73,13 +91,8 @@ func LoadColors() error { if exitError, ok := err.(*exec.ExitError); ok { err = fmt.Errorf("%v; stderr: %q", exitError, exitError.Stderr) } - return err - } - - languageJSON, err := ioutil.ReadFile(path.Join(linguistPathSymlink.Name(), "lib/linguist/languages.json")) - if err != nil { - return err + return nil, err } - return json.Unmarshal(languageJSON, &colorMap) + return os.Open(path.Join(linguistPathSymlink.Name(), "lib/linguist/languages.json")) } diff --git a/internal/linguist/linguist_test.go b/internal/linguist/linguist_test.go new file mode 100644 index 000000000..aac91d866 --- /dev/null +++ b/internal/linguist/linguist_test.go @@ -0,0 +1,32 @@ +package linguist + +import ( + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "gitlab.com/gitlab-org/gitaly/internal/config" + "gitlab.com/gitlab-org/gitaly/internal/testhelper" +) + +func TestLoadLanguages(t *testing.T) { + testhelper.ConfigureRuby() + + colorMap = make(map[string]Language) + require.NoError(t, LoadColors(), "load colors") + + require.Equal(t, "#701516", Color("Ruby"), "color value for 'Ruby'") +} + +func TestLoadLanguagesCustomPath(t *testing.T) { + jsonPath, err := filepath.Abs("testdata/fake-languages.json") + require.NoError(t, err) + + testhelper.ConfigureRuby() + config.Config.Ruby.LinguistLanguagesPath = jsonPath + + colorMap = make(map[string]Language) + require.NoError(t, LoadColors(), "load colors") + + require.Equal(t, "foo color", Color("FooBar")) +} diff --git a/internal/linguist/testdata/fake-languages.json b/internal/linguist/testdata/fake-languages.json new file mode 100644 index 000000000..83f7687f8 --- /dev/null +++ b/internal/linguist/testdata/fake-languages.json @@ -0,0 +1,3 @@ +{ +"FooBar": { "color": "foo color" } +} |