[PATCH] Check the validity of git repository.

Abhinav Jain jainab.2009 at gmail.com
Fri Feb 23 04:02:05 UTC 2018


Sir,

I have made the changes as per your suggestions. 
Please review the changes and guide me, whether I have done it correcctly or not
and also if something more is to be added to the code.

Thanks and Regards
Abhinav Jain


Earlier the function valid was checking the validity of the git repository,
the program was working in. For doing this the function was first checking
whether path exists or not and after that it was checking whether the
directory is a git repository or not and to do so, it
was relying on git status. Since the git status looks for the .git file in
the current directory and if not found it will look up in the tree and if finds
a .git file there it will return valid, which voids the purpose of use.
Now to solve this problem, a function _valid_repo_path is created to check
for the .git file and every function which looks for the validity of
git repository will call this function to check. Closes #2522
---
 source-builder/sb/git.py | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/source-builder/sb/git.py b/source-builder/sb/git.py
index be23396..0d139f9 100644
--- a/source-builder/sb/git.py
+++ b/source-builder/sb/git.py
@@ -38,9 +38,12 @@ class repo:
         if ec:
             raise error.general('git command failed (%s): %d' % (self.git, ec))
 
+    def _valid_repo_path(self):
+    	return path.exists(path.join(self.path, '.git'))
+
     def _run(self, args, check = False):
         e = execute.capture_execution()
-        if path.exists(self.path):
+        if _valid_repo_path():
             cwd = self.path
         else:
             cwd = None
@@ -122,7 +125,7 @@ class repo:
 
     def status(self, submodules_always_clean = False):
         _status = {}
-        if path.exists(self.path):
+        if _valid_repo_path():
             if submodules_always_clean:
                 submodules = self.submodules()
             else:
@@ -166,7 +169,7 @@ class repo:
         return not (len(_status) == 1 and 'branch' in _status)
 
     def valid(self):
-        if path.exists(self.path):
+        if _valid_repo_path():
             ec, output = self._run(['status'])
             return ec == 0
         return False
@@ -233,4 +236,4 @@ if __name__ == '__main__':
     print('g.dirty():', g.dirty())
     print('g.remotes():', g.remotes())
     print('g.email():', g.email())
     print('g.head():', g.head())
\ No newline at end of file
-- 
1.9.1



More information about the devel mailing list