Skip to content

Commit 9ac3e71

Browse files
authored
Merge pull request #4684 from jedwards4b/refine_git_interface
refine the git interface to handle no git and old git
2 parents 2776043 + 9a57386 commit 9ac3e71

File tree

3 files changed

+57
-38
lines changed

3 files changed

+57
-38
lines changed

CIME/case/case_setup.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -527,37 +527,27 @@ def case_setup(self, clean=False, test_mode=False, reset=False, keep=None):
527527

528528

529529
def _create_case_repo(self, caseroot):
530-
version = run_cmd_no_fail("git --version")
531-
result = re.findall(r"([0-9]+)\.([0-9]+)\.?[0-9]*", version)
532-
major = int(result[0][0])
533-
minor = int(result[0][1])
534-
535-
# gitinterface needs git version 2.28 or newer
536-
if major > 2 or (major == 2 and minor >= 28):
537-
self._gitinterface = GitInterface(
538-
caseroot, logger, branch=self.get_value("CASE")
530+
531+
self._gitinterface = GitInterface(caseroot, logger, branch=self.get_value("CASE"))
532+
if self._gitinterface and not os.path.exists(os.path.join(caseroot, ".gitignore")):
533+
safe_copy(
534+
os.path.join(
535+
self.get_value("CIMEROOT"),
536+
"CIME",
537+
"data",
538+
"templates",
539+
"gitignore.template",
540+
),
541+
os.path.join(caseroot, ".gitignore"),
542+
)
543+
append_case_status(
544+
"", "", "local git repository created", gitinterface=self._gitinterface
539545
)
540-
if not os.path.exists(os.path.join(caseroot, ".gitignore")):
541-
safe_copy(
542-
os.path.join(
543-
self.get_value("CIMEROOT"),
544-
"CIME",
545-
"data",
546-
"templates",
547-
"gitignore.template",
548-
),
549-
os.path.join(caseroot, ".gitignore"),
550-
)
551-
append_case_status(
552-
"", "", "local git repository created", gitinterface=self._gitinterface
553-
)
554546
# add all files in caseroot to local repository
555547
self._gitinterface._git_command("add", "*")
556-
else:
557-
logger.warning("git interface requires git version 2.28 or newer")
558-
548+
elif not self._gitinterface:
559549
append_case_status(
560550
"",
561551
"",
562-
f"local git version too old for cime git interface {major}.{minor}",
552+
"Local git version too old for cime git interface, version 2.28 or newer required.",
563553
)

CIME/gitinterface.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
1-
import sys
1+
import sys, shutil, re
22
from CIME.utils import run_cmd_no_fail
33
from pathlib import Path
44

55

66
class GitInterface:
77
def __init__(self, repo_path, logger, branch=None):
8+
major = 0
9+
minor = 0
10+
self.logger = logger
11+
self._defined = False
12+
if shutil.which("git"):
13+
version = run_cmd_no_fail("git --version")
14+
result = re.findall(r"([0-9]+)\.([0-9]+)\.?[0-9]*", version)
15+
major = int(result[0][0])
16+
minor = int(result[0][1])
17+
if major < 2 or (major == 2 and minor < 28):
18+
logger.warning(
19+
"Git not found or git version too old for cesm git interface {} {}".format(
20+
major, minor
21+
)
22+
)
23+
return
24+
825
logger.debug("Initialize GitInterface for {}".format(repo_path))
26+
self._defined = True
927
if isinstance(repo_path, str):
1028
self.repo_path = Path(repo_path).resolve()
1129
elif isinstance(repo_path, Path):
1230
self.repo_path = repo_path.resolve()
1331
else:
1432
raise TypeError("repo_path must be a str or Path object")
15-
self.logger = logger
1633
try:
1734
import git
1835

@@ -28,9 +45,11 @@ def __init__(self, repo_path, logger, branch=None):
2845
if not (self.repo_path / ".git").exists():
2946
self._init_git_repo(branch=branch)
3047
msg = "Using shell interface to git"
31-
self.logger.debug(msg)
48+
logger.debug(msg)
3249

3350
def _git_command(self, operation, *args):
51+
if not self._defined:
52+
return
3453
self.logger.debug(operation)
3554
if self._use_module and operation != "submodule":
3655
try:
@@ -41,6 +60,8 @@ def _git_command(self, operation, *args):
4160
return ["git", "-C", str(self.repo_path), operation] + list(args)
4261

4362
def _init_git_repo(self, branch=None):
63+
if not self._defined:
64+
return
4465
if self._use_module:
4566
self.repo = self.git.Repo.init(str(self.repo_path))
4667
if branch:
@@ -53,6 +74,8 @@ def _init_git_repo(self, branch=None):
5374

5475
# pylint: disable=unused-argument
5576
def git_operation(self, operation, *args, **kwargs):
77+
if not self._defined:
78+
return
5679
command = self._git_command(operation, *args)
5780
if isinstance(command, list):
5881
try:
@@ -63,6 +86,8 @@ def git_operation(self, operation, *args, **kwargs):
6386
return command
6487

6588
def config_get_value(self, section, name):
89+
if not self._defined:
90+
return
6691
if self._use_module:
6792
config = self.repo.config_reader()
6893
try:
@@ -83,6 +108,8 @@ def config_get_value(self, section, name):
83108
return output.strip()
84109

85110
def config_set_value(self, section, name, value):
111+
if not self._defined:
112+
return
86113
if self._use_module:
87114
with self.repo.config_writer() as writer:
88115
writer.set_value(section, name, value)

CIME/status.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ def append_case_status(phase, status, msg=None, caseroot=".", gitinterface=None)
3939
if gitinterface:
4040
filelist = gitinterface.git_operation(
4141
"ls-files", "--deleted", "--exclude-standard"
42-
).splitlines()
42+
)
4343
# First delete files that have been removed
44-
for f in filelist:
45-
logger.debug("removing file {}".format(f))
46-
gitinterface.git_operation("rm", f)
44+
if filelist:
45+
for f in filelist.splitlines():
46+
logger.debug("removing file {}".format(f))
47+
gitinterface.git_operation("rm", f)
4748
filelist = gitinterface.git_operation(
4849
"ls-files", "--others", "--modified", "--exclude-standard"
49-
).splitlines()
50+
)
5051
# Files that should not be added should have been excluded by the .gitignore file
51-
for f in filelist:
52-
logger.debug("adding file {}".format(f))
53-
gitinterface.git_operation("add", f)
52+
if filelist:
53+
for f in filelist.splitlines():
54+
logger.debug("adding file {}".format(f))
55+
gitinterface.git_operation("add", f)
5456
msg = msg if msg else " no message provided"
5557
gitinterface.git_operation("commit", "-m", '"' + msg + '"')
5658
remote = gitinterface.git_operation("remote")

0 commit comments

Comments
 (0)