[v5,10/15] git-p4: fix assumed path separators to be more Windows friendly
diff mbox series

Message ID b962cce8cd455d4cc561b14b4f498d5553067985.1575740863.git.gitgitgadget@gmail.com
State New
Headers show
Series
  • git-p4.py: Cast byte strings to unicode strings in python3
Related show

Commit Message

Heba Waly via GitGitGadget Dec. 7, 2019, 5:47 p.m. UTC
From: Ben Keene <seraphire@gmail.com>

When a computer is configured to use Git for windows and Python for
windows, and not a Unix subsystem like cygwin or WSL, the directory
separator changes and causes git-p4 to fail to properly determine paths.

Fix 3 path separator errors:

1. getUserCacheFilename() - should not use string concatenation. Change
   this code to use os.path.join to build an OS tolerant path.

2. defaultDestiantion used the OS.path.split to split depot paths.  This
   is incorrect on windows. Change the code to split on a forward
   slash(/) instead since depot paths use this character regardless  of
   the operating system.

3. The call to isValidGitDir() in the main code also used a literal
   forward slash. Change the code to use os.path.join to correctly
   format the path for the operating system.

These three changes allow the suggested windows configuration to
properly locate files while retaining the existing behavior on
non-windows operating systems.

Signed-off-by: Ben Keene <seraphire@gmail.com>
---
 git-p4.py | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

Patch
diff mbox series

diff --git a/git-p4.py b/git-p4.py
index fc6c9406c2..1838045078 100755
--- a/git-p4.py
+++ b/git-p4.py
@@ -1459,8 +1459,10 @@  def p4UserIsMe(self, p4User):
             return True
 
     def getUserCacheFilename(self):
+        """ Returns the filename of the username cache
+        """
         home = os.environ.get("HOME", os.environ.get("USERPROFILE"))
-        return home + "/.gitp4-usercache.txt"
+        return os.path.join(home, ".gitp4-usercache.txt")
 
     def getUserMapFromPerforceServer(self):
         if self.userMapFromPerforceServer:
@@ -3978,13 +3980,16 @@  def __init__(self):
         self.cloneBare = False
 
     def defaultDestination(self, args):
+        """ Returns the last path component as the default git
+            repository directory name
+        """
         ## TODO: use common prefix of args?
         depotPath = args[0]
         depotDir = re.sub("(@[^@]*)$", "", depotPath)
         depotDir = re.sub("(#[^#]*)$", "", depotDir)
         depotDir = re.sub(r"\.\.\.$", "", depotDir)
         depotDir = re.sub(r"/$", "", depotDir)
-        return os.path.split(depotDir)[1]
+        return depotDir.split('/')[-1]
 
     def run(self, args):
         if len(args) < 1:
@@ -4257,8 +4262,8 @@  def main():
                         chdir(cdup);
 
         if not isValidGitDir(cmd.gitdir):
-            if isValidGitDir(cmd.gitdir + "/.git"):
-                cmd.gitdir += "/.git"
+            if isValidGitDir(os.path.join(cmd.gitdir, ".git")):
+                cmd.gitdir = os.path.join(cmd.gitdir, ".git")
             else:
                 die("fatal: cannot locate git repository at %s" % cmd.gitdir)