Skip to content

Commit 39259a9

Browse files
committed
Improve editor lookup algorithm
1 parent 8a509d9 commit 39259a9

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Output format of any non-plumbing command can change in non-backward-compatible
6060
1. Verify that a build started for [Snap](https://build.snapcraft.io/user/VirtusLab/git-machete).
6161
If not, check build.snapcraft.io webhook on Github (under Settings > Webhooks) -
6262
sometimes the build system responds with 500 status for no clear reason, in such case `Redeliver` the call.
63-
Once ready, install the `latest/edge` revision locally (`sudo snap install --edge git-machete`)
63+
Once ready, install the `latest/edge` revision locally (`sudo snap install --edge --classic git-machete`)
6464
and verify that it works correctly, esp. wrt. push/pull via ssh/https and editor (`git machete edit` and interactive rebases).
6565

6666
1. Run `./tag-release.sh` script to create an annotated tag for the release.
@@ -90,7 +90,7 @@ Output format of any non-plumbing command can change in non-backward-compatible
9090
1. Verify that a build started on [git-machete PPA](https://launchpad.net/~virtuslab/+archive/ubuntu/git-machete/+packages).
9191

9292
Once the new version package is published and the old one is removed (typically takes around 20-30 min),
93-
follow the instructions from ci/deb-ppa-test-install/README.md.
93+
follow the instructions from [ci/deb-ppa-test-install/README.md](https://github.com/VirtusLab/git-machete/tree/master/ci/deb-ppa-test-install).
9494
Inspect the output of `docker-compose` and verify that the latest version gets correctly installed on Ubuntu (esp. see the output of `git machete --version`).
9595

9696
1. Perform a release from `latest/edge` to `latest/stable` for each architecture from [Snapcraft web dashboard](https://snapcraft.io/git-machete/releases) or via CLI.

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- changed: confinement of snaps from `strict` to `classic`
77
- changed: Docker images moved from under `virtuslab` organization to `gitmachete`
88
- changed: `apt-ppa` in all contexts to `deb-ppa`
9+
- improved: determining the default editor (also including `git config core.editor`, `$GIT_EDITOR`, `editor` and `$VISUAL`)
910

1011
## New in git-machete 2.13.5
1112

git_machete/cmd.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -451,43 +451,52 @@ def is_executable(path):
451451
return os.access(path, os.X_OK)
452452

453453

454-
# Copied from distutils.spawn to avoid dependency on distutils
455-
def find_executable(executable, path=None):
456-
"""Tries to find 'executable' in the directories listed in 'path'.
457-
458-
A string listing directories separated by 'os.pathsep'; defaults to
459-
os.environ['PATH']. Returns the complete filename or None if not found.
460-
"""
461-
if path is None:
462-
path = os.environ.get('PATH', os.defpath)
463-
464-
paths = path.split(os.pathsep)
454+
def find_executable(executable):
465455
base, ext = os.path.splitext(executable)
466456

467457
if (sys.platform == 'win32' or os.name == 'os2') and (ext != '.exe'):
468458
executable = executable + '.exe'
469459

470-
if not os.path.isfile(executable):
471-
for p in paths:
472-
f = os.path.join(p, executable)
473-
if os.path.isfile(f):
474-
# the file exists, we have a shot at spawn working
475-
return f
476-
return None
477-
else:
460+
if os.path.isfile(executable):
478461
return executable
479462

463+
path = os.environ.get('PATH', os.defpath)
464+
paths = path.split(os.pathsep)
465+
for p in paths:
466+
f = os.path.join(p, executable)
467+
if os.path.isfile(f) and is_executable(f):
468+
debug("find_executable(%s)" % executable, "found %s at %s" % (executable, f))
469+
return f
470+
return None
471+
472+
473+
def get_default_editor():
474+
# Based on the git's own algorithm for identifying the editor.
475+
# 'editor' (to please Debian-based systems) and 'nano' have been added.
476+
proposed_editor_funs = [
477+
("$GIT_EDITOR", lambda: os.environ.get("GIT_EDITOR")),
478+
("git config core.editor", lambda: get_config_or_none("core.editor")),
479+
("editor", lambda: "editor"),
480+
("$VISUAL", lambda: os.environ.get("VISUAL")),
481+
("$EDITOR", lambda: os.environ.get("EDITOR")),
482+
("nano", lambda: "nano"),
483+
("vi", lambda: "vi")
484+
]
485+
486+
for name, fun in proposed_editor_funs:
487+
editor = fun()
488+
if not editor:
489+
debug("get_default_editor()", "'%s' is undefined" % name)
490+
elif not find_executable(editor):
491+
debug("get_default_editor()", "'%s'%s is not available" % (name, (" (" + editor + ")") if editor != name else ""))
492+
else:
493+
debug("get_default_editor()", "'%s'%s is available" % (name, (" (" + editor + ")") if editor != name else ""))
494+
return editor
495+
raise MacheteException("Cannot determine editor. Set EDITOR environment variable or edit %s directly." % definition_file)
496+
480497

481498
def edit():
482-
editor = os.environ.get("EDITOR")
483-
if editor and find_executable(editor):
484-
return run_cmd(editor, definition_file)
485-
elif find_executable("vim"):
486-
return run_cmd("vim", definition_file)
487-
elif find_executable("nano"):
488-
return run_cmd("nano", definition_file)
489-
else:
490-
raise MacheteException("Cannot determine editor. Set EDITOR environment variable or edit %s directly." % definition_file)
499+
return run_cmd(get_default_editor(), definition_file)
491500

492501

493502
git_version = None

0 commit comments

Comments
 (0)