Skip to contentSkip to conversation

git p4: chdir resolves symlinks only for relative paths

PATCH In review Miklós Fazekas 4 participants 13 messages Jun 15, 2016 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CAAMmcSSvrsZqEVf68Nrqy_ZG6r5ESKhtx7JdQ7vzypkZ3gOFnA@mail.gmail.com

#1 lore raw

PATCH 1/1 git p4: chdir resolves symlinks only for relative paths

[resending as plain text]

If a p4 client is configured to /p/foo which is a symlink to /vol/bar/projects/foo, then resolving symlink, which is done by git-p4's chdir will confuse p4: "Path /vol/bar/projects/foo/... is not under client root /p/foo" While AltRoots in p4 client specification can be used as a workaround on p4 side, git-p4 should not resolve symlinks in client paths.
chdir(dir) uses os.getcwd() after os.chdir(dir) to resolve relative paths, but as a side effect it resolves symlinks too. Now it checks if the dir is relative before resolving.

---
 git-p4.py |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
git-p4.py +4 -1
index 2da5649..5d74649 100755
@@ -64,7 +64,10 @@ def chdir(dir):
     # not using the shell, we have to set it ourselves.  This path could
     # be relative, so go there first, then figure out where we ended up.
     os.chdir(dir)
-    os.environ['PWD'] = os.getcwd()
+    if os.path.isabs(dir):
+        os.environ['PWD'] = dir
+    else:
+        os.environ['PWD'] = os.getcwd()
 
 def die(msg):
     if verbose:
Signature · 1 line
1.7.10.2 (Apple Git-33)
#2 lore raw

[email protected] wrote on Tue, 29 Jan 2013 09:37 +0100:

Quoted 22 lines

If a p4 client is configured to /p/foo which is a symlink to /vol/bar/projects/foo, then resolving symlink, which is done by git-p4's chdir will confuse p4: "Path /vol/bar/projects/foo/... is not under client root /p/foo" While AltRoots in p4 client specification can be used as a workaround on p4 side, git-p4 should not resolve symlinks in client paths.
chdir(dir) uses os.getcwd() after os.chdir(dir) to resolve relative paths, but as a side effect it resolves symlinks too. Now it checks if the dir is relative before resolving.

---
 git-p4.py |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
git-p4.py +0 -0
index 2da5649..5d74649 100755
@@ -64,7 +64,10 @@ def chdir(dir):
     # not using the shell, we have to set it ourselves.  This path could

git-p4.py def chdir(dir): · line 73

Quoted 10 lines
git-p4.py +4 -1
     # be relative, so go there first, then figure out where we ended up.
     os.chdir(dir)
-    os.environ['PWD'] = os.getcwd()
+    if os.path.isabs(dir):
+        os.environ['PWD'] = dir
+    else:
+        os.environ['PWD'] = os.getcwd()
 
 def die(msg):
     if verbose:

Thanks, this is indeed a bug and I have reproduced it with a test case. Your patch works, but I think it would be better to separate the callers of chdir(): those that know they are cd-ing to a path from a p4 client, and everybody else. The former should not use os.getcwd(), as you show.

I'll whip something up soon, unless you beat me to it.

		-- Pete
#3 lore raw

Sorry for the late turnaround here is an improved version. Now chdir has an optional argument client_path, if it's true then we don't do os.getcwd. I think that my first patch is also valid too - when the path is absolute no need for getcwd no matter what is the context, when it's relative we have to use os.getcwd() no matter of the context.

---
If p4 client is configured to /p/foo which is a symlink:
/p/foo -> /vol/barvol/projects/foo. Then resolving the symlink will confuse p4:
"Path /vol/barvol/projects/foo/... is not under client root /p/foo". While AltRoots in p4 client specification can be used as a workaround on p4 side, git-p4 should not resolve symlinks in client paths.
chdir(dir) uses os.getcwd() after os.chdir(dir) to resolve relative paths, but as a sideeffect it resolves symlinks too. Now for client paths we don't call os.getcwd().

---
 git-p4.py |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
git-p4.py +8 -3
index 0682e61..2bd8cc2 100755
@@ -68,12 +68,17 @@ def p4_build_cmd(cmd):
         real_cmd += cmd
     return real_cmd
 
-def chdir(dir):
+def chdir(dir,client_path=False):
     # P4 uses the PWD environment variable rather than getcwd(). Since we're
     # not using the shell, we have to set it ourselves.  This path could
     # be relative, so go there first, then figure out where we ended up.
+    # os.getcwd() will resolve symlinks, so we should avoid it for
+    # client_paths.
     os.chdir(dir)
-    os.environ['PWD'] = os.getcwd()
+    if client_path:
+        os.environ['PWD'] = dir
+    else:
+               os.environ['PWD'] = os.getcwd()
 
 def die(msg):
     if verbose:
@@ -1554,7 +1559,7 @@ class P4Submit(Command, P4UserMap):
             new_client_dir = True
             os.makedirs(self.clientPath)
 
-        chdir(self.clientPath)
+        chdir(self.clientPath,client_path=True)
         if self.dry_run:
             print "Would synchronize p4 checkout in %s" % self.clientPath
         else:
Signature · 1 line
1.7.10.2 (Apple Git-33)
Quoted 43 lines · Pete Wyckoff

[email protected] wrote on Tue, 29 Jan 2013 09:37 +0100:

Quoted 22 lines

If a p4 client is configured to /p/foo which is a symlink to /vol/bar/projects/foo, then resolving symlink, which is done by git-p4's chdir will confuse p4: "Path /vol/bar/projects/foo/... is not under client root /p/foo" While AltRoots in p4 client specification can be used as a workaround on p4 side, git-p4 should not resolve symlinks in client paths.
chdir(dir) uses os.getcwd() after os.chdir(dir) to resolve relative paths, but as a side effect it resolves symlinks too. Now it checks if the dir is relative before resolving.

---
 git-p4.py |    5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
git-p4.py +0 -0
index 2da5649..5d74649 100755
@@ -64,7 +64,10 @@ def chdir(dir):
     # not using the shell, we have to set it ourselves.  This path could

git-p4.py def chdir(dir): · line 73

Quoted 10 lines
git-p4.py +4 -1
     # be relative, so go there first, then figure out where we ended up.
     os.chdir(dir)
-    os.environ['PWD'] = os.getcwd()
+    if os.path.isabs(dir):
+        os.environ['PWD'] = dir
+    else:
+        os.environ['PWD'] = os.getcwd()
 
 def die(msg):
     if verbose:

Thanks, this is indeed a bug and I have reproduced it with a test case. Your patch works, but I think it would be better to separate the callers of chdir(): those that know they are cd-ing to a path from a p4 client, and everybody else. The former should not use os.getcwd(), as you show.

I'll whip something up soon, unless you beat me to it.

                -- Pete
#4 lore raw
Quoted 29 lines · Miklós Fazekas

Sorry for the late turnaround here is an improved version. Now chdir has an optional argument client_path, if it's true then we don't do os.getcwd. I think that my first patch is also valid too - when the path is absolute no need for getcwd no matter what is the context, when it's relative we have to use os.getcwd() no matter of the context.

---
If p4 client is configured to /p/foo which is a symlink:
/p/foo -> /vol/barvol/projects/foo. Then resolving the symlink will confuse p4:
"Path /vol/barvol/projects/foo/... is not under client root /p/foo". While AltRoots in p4 client specification can be used as a workaround on p4 side, git-p4 should not resolve symlinks in client paths.
chdir(dir) uses os.getcwd() after os.chdir(dir) to resolve relative paths, but as a sideeffect it resolves symlinks too. Now for client paths we don't call os.getcwd().

---
 git-p4.py |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
git-p4.py +0 -0
index 0682e61..2bd8cc2 100755

git-p4.py def p4_build_cmd(cmd): · line 71

Quoted 5 lines
git-p4.py +1 -1
@@ -68,12 +68,17 @@ def p4_build_cmd(cmd):
         real_cmd += cmd
     return real_cmd
 
-def chdir(dir):
+def chdir(dir,client_path=False):

Style (space after comma):

def chdir(dir, client_path=False):

git-p4.py def p4_build_cmd(cmd): · line 81

Quoted 11 lines
git-p4.py +6 -1
     # P4 uses the PWD environment variable rather than getcwd(). Since we're
     # not using the shell, we have to set it ourselves.  This path could
     # be relative, so go there first, then figure out where we ended up.
+    # os.getcwd() will resolve symlinks, so we should avoid it for
+    # client_paths.
     os.chdir(dir)
-    os.environ['PWD'] = os.getcwd()
+    if client_path:
+        os.environ['PWD'] = dir
+    else:
+               os.environ['PWD'] = os.getcwd()

Indentation seems to have gone a bit wrong here...

git-p4.py class P4Submit(Command, P4UserMap): · line 1562

Quoted 9 lines
git-p4.py +1 -1
 def die(msg):
     if verbose:
@@ -1554,7 +1559,7 @@ class P4Submit(Command, P4UserMap):
             new_client_dir = True
             os.makedirs(self.clientPath)
 
-        chdir(self.clientPath)
+        chdir(self.clientPath,client_path=True)

Again, there should be a space after the comma here.

Quoted 5 lines
git-p4.py +0 -0
         if self.dry_run:
             print "Would synchronize p4 checkout in %s" % self.clientPath
         else:
Signature · 1 line
1.7.10.2 (Apple Git-33)
#5 lore raw

cover fix git-p4 client root symlink problems

Miklós pointed out in

    http://thread.gmane.org/gmane.comp.version-control.git/214915

that when the p4 client root included a symlink, bad things happen. It is fixable, but inconvenient, to use an absolute path in one's p4 client. It's not too hard to be smarter about this in git-p4.

Thanks to Miklós for the patch, and to John for the style suggestions. I wrote a couple of tests to make sure this part doesn't break again.

This is maybe a bug introduced by bf1d68f (git-p4: use absolute directory for PWD env var, 2011-12-09), but that's so long ago that I don't think this is a candidate for maint.

		-- Pete

Miklós Fazekas (1):
git p4: avoid expanding client paths in chdir

Pete Wyckoff (2):
git p4 test: make sure P4CONFIG relative path works git p4 test: should honor symlink in p4 client root

 git-p4.py               | 29 ++++++++++++++++++++++-------
 t/t9808-git-p4-chdir.sh | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 7 deletions(-)
Signature · 1 line
1.8.2.rc2.64.g8335025
#6 lore raw

PATCH 1/3 git p4 test: make sure P4CONFIG relative path works

This adds a test for the fix in bf1d68f (git-p4: use absolute directory for PWD env var, 2011-12-09). It is necessary to set PWD to an absolute path so that p4 can find files referenced by non-absolute paths, like the value of the P4CONFIG environment variable.

P4 does not open files directly; it builds a path by prepending the contents of the PWD environment variable.

---
 t/t9808-git-p4-chdir.sh | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
t/t9808-git-p4-chdir.sh +14 -0
index dc92e60..55c5e36 100755
@@ -42,6 +42,20 @@ test_expect_success 'P4CONFIG and relative dir clone' '
 	)
 '
 
+# Common setup using .p4config to set P4CLIENT and P4PORT breaks
+# if clone destination is relative.  Make sure that chdir() expands
+# the relative path in --dest to absolute.
+test_expect_success 'p4 client root would be relative due to clone --dest' '
+	test_when_finished cleanup_git &&
+	(
+		echo P4PORT=$P4PORT >git/.p4config &&
+		P4CONFIG=.p4config &&
+		export P4CONFIG &&
+		unset P4PORT &&
+		git p4 clone --dest="git" //depot
+	)
+'
+
 test_expect_success 'kill p4d' '
 	kill_p4d
 '
Signature · 1 line
1.8.2.rc2.64.g8335025
#7 lore raw

PATCH 2/3 git p4 test: should honor symlink in p4 client root

In reply to Pete Wyckoff

This test fails when the p4 client root includes
a symlink. It complains:

Path /vol/bar/projects/foo/... is not under client root /p/foo

and dumps a traceback.

---
 t/t9808-git-p4-chdir.sh | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
t/t9808-git-p4-chdir.sh +27 -0
index 55c5e36..af8bd8a 100755
@@ -56,6 +56,33 @@ test_expect_success 'p4 client root would be relative due to clone --dest' '
 	)
 '
 
+# When the p4 client Root is a symlink, make sure chdir() does not use
+# getcwd() to convert it to a physical path.
+test_expect_failure 'p4 client root symlink should stay symbolic' '
+	physical="$TRASH_DIRECTORY/physical" &&
+	symbolic="$TRASH_DIRECTORY/symbolic" &&
+	test_when_finished "rm -rf \"$physical\"" &&
+	test_when_finished "rm \"$symbolic\"" &&
+	mkdir -p "$physical" &&
+	ln -s "$physical" "$symbolic" &&
+	test_when_finished cleanup_git &&
+	(
+		P4CLIENT=client-sym &&
+		p4 client -i <<-EOF &&
+		Client: $P4CLIENT
+		Description: $P4CLIENT
+		Root: $symbolic
+		LineEnd: unix
+		View: //depot/... //$P4CLIENT/...
+		EOF
+		git p4 clone --dest="$git" //depot &&
+		cd "$git" &&
+		test_commit file2 &&
+		git config git-p4.skipSubmitEdit true &&
+		git p4 submit
+	)
+'
+
 test_expect_success 'kill p4d' '
 	kill_p4d
 '
Signature · 1 line
1.8.2.rc2.64.g8335025
#8 lore raw

Re: [PATCH 2/3] git p4 test: should honor symlink in p4 client root

Am 3/8/2013 0:19, schrieb Pete Wyckoff:

t/t9808-git-p4-chdir.sh test_expect_success 'p4 client root would be relative due to clone --dest' ' · line 67

Quoted 9 lines
t/t9808-git-p4-chdir.sh +9 -0
+# When the p4 client Root is a symlink, make sure chdir() does not use
+# getcwd() to convert it to a physical path.
+test_expect_failure 'p4 client root symlink should stay symbolic' '
+	physical="$TRASH_DIRECTORY/physical" &&
+	symbolic="$TRASH_DIRECTORY/symbolic" &&
+	test_when_finished "rm -rf \"$physical\"" &&
+	test_when_finished "rm \"$symbolic\"" &&
+	mkdir -p "$physical" &&
+	ln -s "$physical" "$symbolic" &&

This test needs a SYMLINKS prerequisite to future-proof it, in case the Windows port gains p4 support some time.

Quoted 7 lines
t/t9808-git-p4-chdir.sh +7 -0
+	test_when_finished cleanup_git &&
+	(
+		P4CLIENT=client-sym &&
+		p4 client -i <<-EOF &&
+		Client: $P4CLIENT
+		Description: $P4CLIENT
+		Root: $symbolic

t/t9808-git-p4-chdir.sh test_expect_success 'p4 client root would be relative due to clone --dest' ' · line 84

Quoted 10 lines
t/t9808-git-p4-chdir.sh +10 -0
+		LineEnd: unix
+		View: //depot/... //$P4CLIENT/...
+		EOF
+		git p4 clone --dest="$git" //depot &&
+		cd "$git" &&
+		test_commit file2 &&
+		git config git-p4.skipSubmitEdit true &&
+		git p4 submit
+	)
+'

-- Hannes

#9 lore raw

v2 cover fix git-p4 client root symlink problems

Update from v1:

* add SYMLINKS prerequisite to the new symlink test

Thanks Hannes.

Miklós pointed out in

    http://thread.gmane.org/gmane.comp.version-control.git/214915

that when the p4 client root included a symlink, bad things happen. It is fixable, but inconvenient, to use an absolute path in one's p4 client. It's not too hard to be smarter about this in git-p4.

Thanks to Miklós for the patch, and to John for the style suggestions. I wrote a couple of tests to make sure this part doesn't break again.

This is maybe a bug introduced by bf1d68f (git-p4: use absolute directory for PWD env var, 2011-12-09), but that's so long ago that I don't think this is a candidate for maint.

		-- Pete

Miklós Fazekas (1):
git p4: avoid expanding client paths in chdir

Pete Wyckoff (2):
git p4 test: make sure P4CONFIG relative path works git p4 test: should honor symlink in p4 client root

 git-p4.py               | 29 ++++++++++++++++++++++-------
 t/t9808-git-p4-chdir.sh | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 63 insertions(+), 7 deletions(-)
Signature · 1 line
1.8.2.rc2.65.g92f3e2d
#10 lore raw

PATCH v2 1/3 git p4 test: make sure P4CONFIG relative path works

This adds a test for the fix in bf1d68f (git-p4: use absolute directory for PWD env var, 2011-12-09). It is necessary to set PWD to an absolute path so that p4 can find files referenced by non-absolute paths, like the value of the P4CONFIG environment variable.

P4 does not open files directly; it builds a path by prepending the contents of the PWD environment variable.

---
 t/t9808-git-p4-chdir.sh | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
t/t9808-git-p4-chdir.sh +14 -0
index dc92e60..55c5e36 100755
@@ -42,6 +42,20 @@ test_expect_success 'P4CONFIG and relative dir clone' '
 	)
 '
 
+# Common setup using .p4config to set P4CLIENT and P4PORT breaks
+# if clone destination is relative.  Make sure that chdir() expands
+# the relative path in --dest to absolute.
+test_expect_success 'p4 client root would be relative due to clone --dest' '
+	test_when_finished cleanup_git &&
+	(
+		echo P4PORT=$P4PORT >git/.p4config &&
+		P4CONFIG=.p4config &&
+		export P4CONFIG &&
+		unset P4PORT &&
+		git p4 clone --dest="git" //depot
+	)
+'
+
 test_expect_success 'kill p4d' '
 	kill_p4d
 '
Signature · 1 line
1.8.2.rc2.65.g92f3e2d
#11 lore raw

PATCH v2 2/3 git p4 test: should honor symlink in p4 client root

In reply to Pete Wyckoff

This test fails when the p4 client root includes
a symlink. It complains:

Path /vol/bar/projects/foo/... is not under client root /p/foo

and dumps a traceback.

---
 t/t9808-git-p4-chdir.sh | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
t/t9808-git-p4-chdir.sh +27 -0
index 55c5e36..4773296 100755
@@ -56,6 +56,33 @@ test_expect_success 'p4 client root would be relative due to clone --dest' '
 	)
 '
 
+# When the p4 client Root is a symlink, make sure chdir() does not use
+# getcwd() to convert it to a physical path.
+test_expect_failure SYMLINKS 'p4 client root symlink should stay symbolic' '
+	physical="$TRASH_DIRECTORY/physical" &&
+	symbolic="$TRASH_DIRECTORY/symbolic" &&
+	test_when_finished "rm -rf \"$physical\"" &&
+	test_when_finished "rm \"$symbolic\"" &&
+	mkdir -p "$physical" &&
+	ln -s "$physical" "$symbolic" &&
+	test_when_finished cleanup_git &&
+	(
+		P4CLIENT=client-sym &&
+		p4 client -i <<-EOF &&
+		Client: $P4CLIENT
+		Description: $P4CLIENT
+		Root: $symbolic
+		LineEnd: unix
+		View: //depot/... //$P4CLIENT/...
+		EOF
+		git p4 clone --dest="$git" //depot &&
+		cd "$git" &&
+		test_commit file2 &&
+		git config git-p4.skipSubmitEdit true &&
+		git p4 submit
+	)
+'
+
 test_expect_success 'kill p4d' '
 	kill_p4d
 '
Signature · 1 line
1.8.2.rc2.65.g92f3e2d
#12 lore raw

PATCH v2 3/3 git p4: avoid expanding client paths in chdir

In reply to Pete Wyckoff

From: Miklós Fazekas <[email protected]>

The generic chdir() helper sets the PWD environment variable, as that is what is used by p4 to know its current working directory. Normally the shell would do this, but in git-p4, we must do it by hand.

However, when the path contains a symbolic link, os.getcwd() will return the physical location. If the p4 client specification includes symlinks, setting PWD to the physical location causes p4 to think it is not inside the client workspace. It complains, e.g.

Path /vol/bar/projects/foo/... is not under client root /p/foo

One workaround is to use AltRoots in the p4 client specification, but it is cleaner to handle it directly in git-p4.

Other uses of chdir still require setting PWD to an absolute path so p4 features like P4CONFIG work. See bf1d68f (git-p4: use absolute directory for PWD env var, 2011-12-09).

[ pw: tweak patch and commit message ]

Thanks-to: John Keeping <[email protected]>

---
 git-p4.py               | 29 ++++++++++++++++++++++-------
 t/t9808-git-p4-chdir.sh |  2 +-
 2 files changed, 23 insertions(+), 8 deletions(-)
git-p4.py +22 -7
index 647f110..7288c0b 100755
@@ -79,12 +79,27 @@ def p4_build_cmd(cmd):
         real_cmd += cmd
     return real_cmd
 
-def chdir(dir):
-    # P4 uses the PWD environment variable rather than getcwd(). Since we're
-    # not using the shell, we have to set it ourselves.  This path could
-    # be relative, so go there first, then figure out where we ended up.
-    os.chdir(dir)
-    os.environ['PWD'] = os.getcwd()
+def chdir(path, is_client_path=False):
+    """Do chdir to the given path, and set the PWD environment
+       variable for use by P4.  It does not look at getcwd() output.
+       Since we're not using the shell, it is necessary to set the
+       PWD environment variable explicitly.
+       
+       Normally, expand the path to force it to be absolute.  This
+       addresses the use of relative path names inside P4 settings,
+       e.g. P4CONFIG=.p4config.  P4 does not simply open the filename
+       as given; it looks for .p4config using PWD.
+
+       If is_client_path, the path was handed to us directly by p4,
+       and may be a symbolic link.  Do not call os.getcwd() in this
+       case, because it will cause p4 to think that PWD is not inside
+       the client path.
+       """
+
+    os.chdir(path)
+    if not is_client_path:
+        path = os.getcwd()
+    os.environ['PWD'] = path
 
 def die(msg):
     if verbose:
@@ -1624,7 +1639,7 @@ class P4Submit(Command, P4UserMap):
             new_client_dir = True
             os.makedirs(self.clientPath)
 
-        chdir(self.clientPath)
+        chdir(self.clientPath, is_client_path=True)
         if self.dry_run:
             print "Would synchronize p4 checkout in %s" % self.clientPath
         else:
t/t9808-git-p4-chdir.sh +1 -1
index 4773296..11d2b51 100755
@@ -58,7 +58,7 @@ test_expect_success 'p4 client root would be relative due to clone --dest' '
 
 # When the p4 client Root is a symlink, make sure chdir() does not use
 # getcwd() to convert it to a physical path.
-test_expect_failure SYMLINKS 'p4 client root symlink should stay symbolic' '
+test_expect_success SYMLINKS 'p4 client root symlink should stay symbolic' '
 	physical="$TRASH_DIRECTORY/physical" &&
 	symbolic="$TRASH_DIRECTORY/symbolic" &&
 	test_when_finished "rm -rf \"$physical\"" &&
Signature · 1 line
1.8.2.rc2.65.g92f3e2d
#13 lore raw

PATCH 3/3 git p4: avoid expanding client paths in chdir

In reply to Pete Wyckoff

From: Miklós Fazekas <[email protected]>

The generic chdir() helper sets the PWD environment variable, as that is what is used by p4 to know its current working directory. Normally the shell would do this, but in git-p4, we must do it by hand.

However, when the path contains a symbolic link, os.getcwd() will return the physical location. If the p4 client specification includes symlinks, setting PWD to the physical location causes p4 to think it is not inside the client workspace. It complains, e.g.

Path /vol/bar/projects/foo/... is not under client root /p/foo

One workaround is to use AltRoots in the p4 client specification, but it is cleaner to handle it directly in git-p4.

Other uses of chdir still require setting PWD to an absolute path so p4 features like P4CONFIG work. See bf1d68f (git-p4: use absolute directory for PWD env var, 2011-12-09).

[ pw: tweak patch and commit message ]

Thanks-to: John Keeping <[email protected]>

---
 git-p4.py               | 29 ++++++++++++++++++++++-------
 t/t9808-git-p4-chdir.sh |  2 +-
 2 files changed, 23 insertions(+), 8 deletions(-)
git-p4.py +22 -7
index 647f110..7288c0b 100755
@@ -79,12 +79,27 @@ def p4_build_cmd(cmd):
         real_cmd += cmd
     return real_cmd
 
-def chdir(dir):
-    # P4 uses the PWD environment variable rather than getcwd(). Since we're
-    # not using the shell, we have to set it ourselves.  This path could
-    # be relative, so go there first, then figure out where we ended up.
-    os.chdir(dir)
-    os.environ['PWD'] = os.getcwd()
+def chdir(path, is_client_path=False):
+    """Do chdir to the given path, and set the PWD environment
+       variable for use by P4.  It does not look at getcwd() output.
+       Since we're not using the shell, it is necessary to set the
+       PWD environment variable explicitly.
+       
+       Normally, expand the path to force it to be absolute.  This
+       addresses the use of relative path names inside P4 settings,
+       e.g. P4CONFIG=.p4config.  P4 does not simply open the filename
+       as given; it looks for .p4config using PWD.
+
+       If is_client_path, the path was handed to us directly by p4,
+       and may be a symbolic link.  Do not call os.getcwd() in this
+       case, because it will cause p4 to think that PWD is not inside
+       the client path.
+       """
+
+    os.chdir(path)
+    if not is_client_path:
+        path = os.getcwd()
+    os.environ['PWD'] = path
 
 def die(msg):
     if verbose:
@@ -1624,7 +1639,7 @@ class P4Submit(Command, P4UserMap):
             new_client_dir = True
             os.makedirs(self.clientPath)
 
-        chdir(self.clientPath)
+        chdir(self.clientPath, is_client_path=True)
         if self.dry_run:
             print "Would synchronize p4 checkout in %s" % self.clientPath
         else:
t/t9808-git-p4-chdir.sh +1 -1
index af8bd8a..09b2cc4 100755
@@ -58,7 +58,7 @@ test_expect_success 'p4 client root would be relative due to clone --dest' '
 
 # When the p4 client Root is a symlink, make sure chdir() does not use
 # getcwd() to convert it to a physical path.
-test_expect_failure 'p4 client root symlink should stay symbolic' '
+test_expect_success 'p4 client root symlink should stay symbolic' '
 	physical="$TRASH_DIRECTORY/physical" &&
 	symbolic="$TRASH_DIRECTORY/symbolic" &&
 	test_when_finished "rm -rf \"$physical\"" &&
Signature · 1 line
1.8.2.rc2.64.g8335025