From 0acb3505ffe71718cb3b6b0e957cc921ea9ce880 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Sun, 27 Sep 2020 23:14:51 +0200 Subject: [PATCH] repo: implement GetCoreEditor for go-git --- repository/gogit.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/repository/gogit.go b/repository/gogit.go index 6d3aede91ea7ffb4b26d49b81693546f97ad3c7e..aac89d4bd626ef1aa52e86543e5ed7d49933366e 100644 --- a/repository/gogit.go +++ b/repository/gogit.go @@ -208,8 +208,30 @@ func (repo *GoGitRepo) GetUserEmail() (string, error) { // GetCoreEditor returns the name of the editor that the user has used to configure git. func (repo *GoGitRepo) GetCoreEditor() (string, error) { + // See https://git-scm.com/docs/git-var + // The order of preference is the $GIT_EDITOR environment variable, then core.editor configuration, then $VISUAL, then $EDITOR, and then the default chosen at compile time, which is usually vi. - panic("implement me") + if val, ok := os.LookupEnv("GIT_EDITOR"); ok { + return val, nil + } + + val, err := repo.AnyConfig().ReadString("core.editor") + if err == nil && val != "" { + return val, nil + } + if err != nil && err != ErrNoConfigEntry { + return "", err + } + + if val, ok := os.LookupEnv("VISUAL"); ok { + return val, nil + } + + if val, ok := os.LookupEnv("EDITOR"); ok { + return val, nil + } + + return "vi", nil } // GetRemotes returns the configured remotes repositories.