Skip to content

Commit

Permalink
Moved duplicate code to get GIT_EDITOR, in commit.py and tag_create.p…
Browse files Browse the repository at this point in the history
…y, to git.py
  • Loading branch information
pchakraborty committed Apr 18, 2024
1 parent 2bf3212 commit 2b01e93
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 42 deletions.
21 changes: 2 additions & 19 deletions src/mepo/command/commit.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ..state import MepoState
from ..utilities import verify
from ..git import GitRepository
from ..git import get_editor as get_git_editor

from .stage import stage_files

Expand All @@ -16,7 +17,7 @@ def run(args):

# Pop up an editor if a message is not provided
if not args.message:
EDITOR = git_var('GIT_EDITOR')
EDITOR = get_git_editor()
initial_message = b"" # set up the file

# Use delete=False to keep the file around as we send the file name to git commit -F
Expand All @@ -42,21 +43,3 @@ def run(args):
if not args.message:
tf.close()
os.unlink(tf.name)

def git_var(what):
'''
return GIT_EDITOR or GIT_PAGER, for instance
Found at https://stackoverflow.com/a/44174750/1876449
'''
proc = subprocess.Popen(['git', 'var', what], shell=False,
stdout=subprocess.PIPE)
output = proc.stdout.read()
status = proc.wait()
if status != 0:
raise Exception("git_var failed with [%]" % what)
output = output.rstrip(b'\n')
output = output.decode('utf8', errors='ignore') # or similar for py3k
return output

20 changes: 2 additions & 18 deletions src/mepo/command/tag_create.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ..state import MepoState
from ..utilities import verify
from ..git import GitRepository
from ..git import get_editor as get_git_editor

# Popping up an EDITOR is based on https://stackoverflow.com/a/39989442
import os, tempfile, subprocess
Expand All @@ -21,7 +22,7 @@ def run(args):
if create_annotated_tag:
# Pop up an editor if a message is not provided
if not args.message:
EDITOR = git_var('GIT_EDITOR')
EDITOR = get_git_editor()
initial_message = b"" # set up the file

# Use delete=False to keep the file around as we send the file name to git commit -F
Expand All @@ -42,23 +43,6 @@ def run(args):
tf.close()
os.unlink(tf.name)

def git_var(what):
'''
return GIT_EDITOR or GIT_PAGER, for instance
Found at https://stackoverflow.com/a/44174750/1876449
'''
proc = subprocess.Popen(['git', 'var', what], shell=False,
stdout=subprocess.PIPE)
output = proc.stdout.read()
status = proc.wait()
if status != 0:
raise Exception("git_var failed with [%]" % what)
output = output.rstrip(b'\n')
output = output.decode('utf8', errors='ignore') # or similar for py3k
return output

def _get_comps_to_list(specified_comps, allcomps):
comps_to_list = allcomps
if specified_comps:
Expand Down
17 changes: 12 additions & 5 deletions src/mepo/git.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import shutil
import subprocess
import shlex
import subprocess as sp

from urllib.parse import urljoin

Expand All @@ -10,7 +10,14 @@
from .utilities import colors
from .utilities.exceptions import RepoAlreadyClonedError

class GitRepository(object):
def get_editor():
"""
Return GIT_EDITOR
"""
result = sp.run('git var GIT_EDITOR'.split(), stdout=sp.PIPE, stderr=sp.PIPE, check=True)
return result.stdout.rstrip().decode('utf-8') # byte to utf-8

class GitRepository:
"""
Class to consolidate git commands
"""
Expand Down Expand Up @@ -54,7 +61,7 @@ def clone(self, version, recurse, type, comp_name, partial=None):
cmd1 += '--quiet {} {}'.format(self.__remote, self.__full_local_path)
try:
shellcmd.run(shlex.split(cmd1))
except subprocess.CalledProcessError:
except sp.CalledProcessError:
raise RepoAlreadyClonedError(f'Error! Repo [{comp_name}] already cloned')

cmd2 = 'git -C {} checkout {}'.format(self.__full_local_path, version)
Expand All @@ -72,8 +79,8 @@ def checkout(self, version, detach=False):
cmd += '--quiet {}'.format(version)
shellcmd.run(shlex.split(cmd))
if detach:
cmd2 = self.__git + ' checkout --detach'
shellcmd.run(shlex.split(cmd2))
cmd2 = self.__git + ' checkout --detach'
shellcmd.run(shlex.split(cmd2))

def sparsify(self, sparse_config):
dst = os.path.join(self.__full_local_path, '.git', 'info', 'sparse-checkout')
Expand Down

0 comments on commit 2b01e93

Please sign in to comment.