src/eric7/Plugins/VcsPlugins/vcsGit/git.py

branch
eric7
changeset 9616
13aa04c979d7
parent 9576
be9f8e7e42e0
child 9617
6a32a62e55e7
diff -r fbb3616f6bd3 -r 13aa04c979d7 src/eric7/Plugins/VcsPlugins/vcsGit/git.py
--- a/src/eric7/Plugins/VcsPlugins/vcsGit/git.py	Mon Dec 12 19:50:37 2022 +0100
+++ b/src/eric7/Plugins/VcsPlugins/vcsGit/git.py	Tue Dec 13 14:03:13 2022 +0100
@@ -74,7 +74,7 @@
 
         self.commandHistory = []
 
-        self.adminDir = ".git"
+        self.adminDirOrFile = ".git"
 
         self.log = None
         self.logBrowser = None
@@ -341,7 +341,11 @@
         @return flag indicating an execution without errors (boolean)
         """
         status = self.vcsCheckout(vcsDataDict, projectDir)
-        shutil.rmtree(os.path.join(projectDir, self.adminDir), True)
+        adminPath = os.path.join(projectDir, self.adminDirOrFile)
+        if os.path.isdir(adminPath):
+            shutil.rmtree(adminPath, True)
+        else:
+            os.remove(adminPath)
         if os.path.exists(os.path.join(projectDir, Git.IgnoreFileName)):
             os.remove(os.path.join(projectDir, Git.IgnoreFileName))
         return status
@@ -454,11 +458,9 @@
             dname, fname = self.splitPath(name)
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return
 
         if isinstance(name, list):
             args.append("--")
@@ -556,11 +558,9 @@
                 args.append(fname)
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return False
 
         if noDialog:
             self.startSynchronizedProcess(QProcess(), "git", args, repodir)
@@ -597,11 +597,9 @@
                 dname, fname = self.splitPath(name)
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return
 
         if isinstance(name, list):
             self.addArguments(args, name)
@@ -674,11 +672,9 @@
             args.append(name)
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return False
 
         if noDialog:
             res = self.startSynchronizedProcess(QProcess(), "git", args, repodir)
@@ -740,11 +736,9 @@
 
             dname, fname = self.splitPath(name)
             # find the root of the repo
-            repodir = dname
-            while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-                repodir = os.path.dirname(repodir)
-                if os.path.splitdrive(repodir)[1] == os.sep:
-                    return False
+            repodir = self.findRepoRoot(dname)
+            if not repodir:
+                return False
 
             if noDialog:
                 res = self.startSynchronizedProcess(QProcess(), "git", args, repodir)
@@ -859,11 +853,9 @@
             dname, fname = self.splitPath(name)
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return False
 
         args = self.initCommand("reset")
         args.append("HEAD")
@@ -906,11 +898,9 @@
             names = [name]
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return False
 
         project = ericApp().getObject("Project")
         names = [project.getRelativePath(nam) for nam in names]
@@ -957,11 +947,9 @@
         dname, fname = self.splitPath(name)
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return
 
         dlg = GitMergeDialog(
             self.gitGetTagsList(repodir),
@@ -1009,11 +997,9 @@
         dname, fname = self.splitPath(name)
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return False
 
         dlg = GitRevisionSelectionDialog(
             self.gitGetTagsList(repodir),
@@ -1039,18 +1025,16 @@
         name = os.path.normcase(name)
         dname, fname = self.splitPath(name)
 
-        if fname == "." and os.path.isdir(os.path.join(dname, self.adminDir)):
+        if fname == "." and os.path.exists(os.path.join(dname, self.adminDirOrFile)):
             return self.canBeCommitted
 
         if name in self.statusCache:
             return self.statusCache[name]
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return 0
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return 0
 
         args = self.initCommand("status")
         args.append("--porcelain")
@@ -1120,11 +1104,9 @@
 
         if not found:
             # find the root of the repo
-            repodir = dname
-            while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-                repodir = os.path.dirname(repodir)
-                if os.path.splitdrive(repodir)[1] == os.sep:
-                    return names
+            repodir = self.findRepoRoot(dname)
+            if not repodir:
+                return names
 
             args = self.initCommand("status")
             args.append("--porcelain")
@@ -1235,11 +1217,9 @@
             self.addArguments(args, commandList)
 
             # find the root of the repo
-            repodir = name
-            while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-                repodir = os.path.dirname(repodir)
-                if os.path.splitdrive(repodir)[1] == os.sep:
-                    return
+            repodir = self.findRepoRoot(name)
+            if not repodir:
+                return
 
             dia = GitDialog(self.tr("Git Command"), self)
             res = dia.startProcess(args, repodir)
@@ -1593,11 +1573,9 @@
                     return
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return
 
         dlg = GitRevisionsSelectionDialog(
             self.gitGetTagsList(repodir), self.gitGetBranchesList(repodir)
@@ -1620,11 +1598,9 @@
         @return contents of the file (string) and an error message (string)
         """
         # find the root of the repo
-        repodir = self.splitPath(name)[0]
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(self.splitPath(name)[0])
+        if not repodir:
+            return False
 
         args = self.initCommand("cat-file")
         args.append("blob")
@@ -1682,11 +1658,9 @@
 
         if extended:
             # find the root of the repo
-            repodir = self.splitPath(name)[0]
-            while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-                repodir = os.path.dirname(repodir)
-                if os.path.splitdrive(repodir)[1] == os.sep:
-                    return
+            repodir = self.findRepoRoot(self.splitPath(name)[0])
+            if not repodir:
+                return
 
             dlg = GitRevisionsSelectionDialog(
                 self.gitGetTagsList(repodir), self.gitGetBranchesList(repodir)
@@ -1747,11 +1721,9 @@
         from .GitFetchDialog import GitFetchDialog
 
         # find the root of the repo
-        repodir = self.splitPath(name)[0]
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(self.splitPath(name)[0])
+        if not repodir:
+            return
 
         dlg = GitFetchDialog(self, repodir)
         if dlg.exec() == QDialog.DialogCode.Accepted:
@@ -1797,11 +1769,9 @@
         from .GitPullDialog import GitPullDialog
 
         # find the root of the repo
-        repodir = self.splitPath(name)[0]
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(self.splitPath(name)[0])
+        if not repodir:
+            return False
 
         dlg = GitPullDialog(self, repodir)
         if dlg.exec() == QDialog.DialogCode.Accepted:
@@ -1837,11 +1807,9 @@
         from .GitPushDialog import GitPushDialog
 
         # find the root of the repo
-        repodir = self.splitPath(name)[0]
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(self.splitPath(name)[0])
+        if not repodir:
+            return
 
         dlg = GitPushDialog(self, repodir)
         if dlg.exec() == QDialog.DialogCode.Accepted:
@@ -1874,11 +1842,9 @@
         dname, fname = self.splitPath(name)
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return
 
         editor = sys.argv[0].replace(".py", "_editor.py")
         env = {"GIT_EDITOR": "{0} {1}".format(Globals.getPythonExecutable(), editor)}
@@ -1903,11 +1869,9 @@
         dname, fname = self.splitPath(name)
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return False
 
         args = self.initCommand("merge")
         args.append("--abort")
@@ -1958,11 +1922,9 @@
         from .GitPatchFilesDialog import GitPatchFilesDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         dlg = GitPatchFilesDialog(repodir, self.__patchCheckData)
         if dlg.exec() == QDialog.DialogCode.Accepted:
@@ -2029,11 +1991,9 @@
         dname, fname = self.splitPath(name)
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return False
 
         dlg = GitTagDialog(self.gitGetTagsList(repodir), revision, tagName)
         if dlg.exec() == QDialog.DialogCode.Accepted:
@@ -2231,11 +2191,9 @@
         dname, fname = self.splitPath(name)
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False, False
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return False, False
 
         dlg = GitBranchDialog(
             self.gitGetBranchesList(repodir, allBranches=True),
@@ -2335,11 +2293,9 @@
         dname, fname = self.splitPath(name)
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return
 
         dlg = GitBranchPushDialog(
             self.gitGetBranchesList(repodir),
@@ -2367,11 +2323,9 @@
         dname, fname = self.splitPath(name)
 
         # find the root of the repo
-        repodir = dname
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(dname)
+        if not repodir:
+            return
 
         branchName = self.gitGetCurrentBranch(repodir)
         EricMessageBox.information(
@@ -2393,11 +2347,9 @@
         from .GitBundleDialog import GitBundleDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         dlg = GitBundleDialog(
             self.gitGetTagsList(repodir), self.gitGetBranchesList(repodir)
@@ -2455,11 +2407,9 @@
         @param projectDir name of the project directory (string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         fname = EricFileDialog.getOpenFileName(
             None,
@@ -2486,11 +2436,9 @@
         @param projectDir name of the project directory (string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         fname = EricFileDialog.getOpenFileName(
             None,
@@ -2554,11 +2502,9 @@
         from .GitApplyBundleDataDialog import GitApplyBundleDataDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         fname = EricFileDialog.getOpenFileName(
             None,
@@ -2602,11 +2548,9 @@
         from .GitApplyBundleDataDialog import GitApplyBundleDataDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return False
 
         res = False
         fname = EricFileDialog.getOpenFileName(
@@ -2672,11 +2616,9 @@
             )
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return False
 
         res = False
         rev = ""
@@ -2743,11 +2685,9 @@
         @param projectDir name of the project directory (string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         args = self.initCommand("bisect")
         args.append("log")
@@ -2828,11 +2768,9 @@
         @param projectDir name of the project directory (string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         fname = EricFileDialog.getOpenFileName(
             None,
@@ -2855,11 +2793,9 @@
             or delete (boolean)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return False
 
         res = False
         fname = EricFileDialog.getOpenFileName(
@@ -3030,11 +2966,9 @@
         @param remoteName name of the remote repository (string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         args = self.initCommand("remote")
         args.append("show")
@@ -3068,11 +3002,9 @@
         from .GitAddRemoteDialog import GitAddRemoteDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         dlg = GitAddRemoteDialog()
         if dlg.exec() == QDialog.DialogCode.Accepted:
@@ -3092,11 +3024,9 @@
         @param remoteName name of the remote repository (string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         newName, ok = QInputDialog.getText(
             None,
@@ -3126,11 +3056,9 @@
         from .GitChangeRemoteUrlDialog import GitChangeRemoteUrlDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         if not remoteUrl:
             remoteUrl = self.gitGetRemoteUrl(repodir, remoteName)
@@ -3162,11 +3090,9 @@
         from .GitRemoteCredentialsDialog import GitRemoteCredentialsDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         if not remoteUrl:
             remoteUrl = self.gitGetRemoteUrl(repodir, remoteName)
@@ -3192,11 +3118,9 @@
         @param remoteName name of the remote repository (string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         args = self.initCommand("remote")
         args.append("remove")
@@ -3212,11 +3136,9 @@
         @param remoteName name of the remote repository (string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         args = self.initCommand("remote")
         args.append("prune")
@@ -3236,11 +3158,9 @@
         @param commit commit to start the log at (strings)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         args = self.initCommand("shortlog")
         args.append("-w")
@@ -3283,11 +3203,9 @@
         from .GitCherryPickDialog import GitCherryPickDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return False
 
         res = False
 
@@ -3322,11 +3240,9 @@
         @return flag indicating that the project should be reread (boolean)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return False
 
         editor = sys.argv[0].replace(".py", "_editor.py")
         env = {"GIT_EDITOR": "{0} {1}".format(Globals.getPythonExecutable(), editor)}
@@ -3350,11 +3266,9 @@
         @return flag indicating that the project should be reread (boolean)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return False
 
         args = self.initCommand("cherry-pick")
         args.append("--quit")
@@ -3376,11 +3290,9 @@
         @return flag indicating that the project should be reread (boolean)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return False
 
         args = self.initCommand("cherry-pick")
         args.append("--abort")
@@ -3405,11 +3317,9 @@
         @return list of available stashes (list of string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return []
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return []
 
         args = self.initCommand("stash")
         args.append("list")
@@ -3446,11 +3356,9 @@
         from .GitStashDataDialog import GitStashDataDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return False
 
         res = False
         dlg = GitStashDataDialog()
@@ -3499,11 +3407,9 @@
         from .GitDiffDialog import GitDiffDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         if not stashName:
             availableStashes = self.__gitGetStashesList(repodir)
@@ -3534,11 +3440,9 @@
             or delete (boolean)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return False
 
         if not stashName:
             availableStashes = self.__gitGetStashesList(repodir)
@@ -3576,11 +3480,9 @@
             or delete (boolean)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return False
 
         if not stashName:
             availableStashes = self.__gitGetStashesList(repodir)
@@ -3618,11 +3520,9 @@
             or delete (boolean)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return False
 
         branchName, ok = QInputDialog.getText(
             None,
@@ -3669,11 +3569,9 @@
         @return flag indicating a successful deletion (boolean)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return False
 
         if not stashName:
             availableStashes = self.__gitGetStashesList(repodir)
@@ -3715,11 +3613,9 @@
         @return flag indicating a successful deletion (boolean)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return False
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return False
 
         res = EricMessageBox.yesNo(
             None,
@@ -3743,13 +3639,11 @@
         @param projectDir name of the project directory (string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
-
-        cfgFile = os.path.join(repodir, self.adminDir, "config")
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
+
+        cfgFile = os.path.join(repodir, self.adminDirOrFile, "config")
         if not os.path.exists(cfgFile):
             # create an empty one
             with contextlib.suppress(OSError), open(cfgFile, "w"):
@@ -3785,11 +3679,9 @@
         @param projectDir name of the project directory (string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         args = self.initCommand("config")
         args.append("--list")
@@ -3807,11 +3699,9 @@
         @param projectDir name of the project directory (string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         args = self.initCommand("fsck")
         args.append("--strict")
@@ -3830,11 +3720,9 @@
         @param projectDir name of the project directory (string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         args = self.initCommand("gc")
         args.append("--prune")
@@ -3853,11 +3741,9 @@
         @param projectDir name of the project directory (string)
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         args = self.initCommand("count-objects")
         args.append("-v")
@@ -3977,11 +3863,9 @@
         from .GitArchiveDataDialog import GitArchiveDataDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         dlg = GitArchiveDataDialog(
             self.gitGetTagsList(repodir),
@@ -4019,11 +3903,9 @@
         from .GitSubmoduleAddDialog import GitSubmoduleAddDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         dlg = GitSubmoduleAddDialog(self, repodir)
         if dlg.exec() == QDialog.DialogCode.Accepted:
@@ -4097,11 +3979,9 @@
         from .GitSubmodulesListDialog import GitSubmodulesListDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         submodulesList = self.__gitSubmodulesList(repodir)
         if submodulesList:
@@ -4168,11 +4048,9 @@
         @type str
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         submodulePaths, ok = self.__selectSubmodulePaths(repodir)
         if ok:
@@ -4195,11 +4073,9 @@
         from .GitSubmodulesDeinitDialog import GitSubmodulesDeinitDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         paths = [submodule["path"] for submodule in self.__gitSubmodulesList(repodir)]
 
@@ -4232,11 +4108,9 @@
         @type bool
         """
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         submodulePaths, ok = self.__selectSubmodulePaths(repodir)
         if ok:
@@ -4264,11 +4138,9 @@
         from .GitSubmodulesUpdateOptionsDialog import GitSubmodulesUpdateOptionsDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         paths = [submodule["path"] for submodule in self.__gitSubmodulesList(repodir)]
 
@@ -4304,11 +4176,9 @@
         from .GitSubmodulesSyncDialog import GitSubmodulesSyncDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         paths = [submodule["path"] for submodule in self.__gitSubmodulesList(repodir)]
 
@@ -4351,11 +4221,9 @@
         from .GitSubmodulesSummaryOptionsDialog import GitSubmodulesSummaryOptionsDialog
 
         # find the root of the repo
-        repodir = projectDir
-        while not os.path.isdir(os.path.join(repodir, self.adminDir)):
-            repodir = os.path.dirname(repodir)
-            if os.path.splitdrive(repodir)[1] == os.sep:
-                return
+        repodir = self.findRepoRoot(projectDir)
+        if not repodir:
+            return
 
         paths = [submodule["path"] for submodule in self.__gitSubmodulesList(repodir)]
 
@@ -4429,3 +4297,23 @@
         from .GitStatusMonitorThread import GitStatusMonitorThread
 
         return GitStatusMonitorThread(interval, project, self)
+
+    ###########################################################################
+    ##  Method to find the repository root below
+    ###########################################################################
+
+    def findRepoRoot(self, start):
+        """
+        Public method to find the repository root directory given a start.
+
+        @param start start directory name
+        @type str
+        @return directory name of the repository root or None, if it was not found
+        @rtype str or None
+        """
+        repoRoot = start
+        while not os.path.exists(os.path.join(repoRoot, self.adminDirOrFile)):
+            repoRoot = os.path.dirname(repoRoot)
+            if os.path.splitdrive(repoRoot)[1] == os.sep:
+                repoRoot = None
+        return repoRoot

eric ide

mercurial