1- import sys
1+ import sys , shutil , re
22from CIME .utils import run_cmd_no_fail
33from pathlib import Path
44
55
66class 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 )
0 commit comments