{"thread":{"id":"44696","subject":"[PATCHv3] git-p4: support git worktrees","startedAt":"2016-12-13T22:59:33Z","lastAt":"2016-12-14T00:09:27Z","messageCount":3,"participants":["Luke Diamand","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"307697","messageId":"20161213215128.20288-2-luke@diamand.org","threadId":"44696","inReplyTo":"20161213215128.20288-1-luke@diamand.org","subject":"[PATCHv3] git-p4: support git worktrees","fromName":"Luke Diamand","fromEmail":"luke@diamand.org","sentAt":"2016-12-13T21:51:28Z","receivedAt":"2016-12-13T22:59:33Z","isPatch":false,"sender":{"key":"luke@diamand.org","avatar":"https://avatars.githubusercontent.com/u/5330967?v=4"},"body":"git-p4 would attempt to find the git directory using\nits own specific code, which did not know about git\nworktrees.\n\nRework it to use \"git rev-parse --git-dir\" instead.\n\nAdd test cases for worktree usage and specifying\ngit directory via --git-dir and $GIT_DIR.\n\nSigned-off-by: Luke Diamand <luke@diamand.org>\n---\n git-p4.py                 | 17 +++++++++++++----\n t/t9800-git-p4-basic.sh   | 20 ++++++++++++++++++++\n t/t9806-git-p4-options.sh | 32 ++++++++++++++++++++++++++++++++\n 3 files changed, 65 insertions(+), 4 deletions(-)\n\ndiff --git a/git-p4.py b/git-p4.py\nindex fd5ca52..6a1f65f 100755\n--- a/git-p4.py\n+++ b/git-p4.py\n@@ -85,6 +85,16 @@ def p4_build_cmd(cmd):\n         real_cmd += cmd\n     return real_cmd\n \n+def git_dir(path):\n+    \"\"\" Return TRUE if the given path is a git directory (/path/to/dir/.git).\n+        This won't automatically add \".git\" to a directory.\n+    \"\"\"\n+    d = read_pipe([\"git\", \"--git-dir\", path, \"rev-parse\", \"--git-dir\"], True).strip()\n+    if not d or len(d) == 0:\n+        return None\n+    else:\n+        return d\n+\n def chdir(path, is_client_path=False):\n     \"\"\"Do chdir to the given path, and set the PWD environment\n        variable for use by P4.  It does not look at getcwd() output.\n@@ -563,10 +573,7 @@ def currentGitBranch():\n         return read_pipe([\"git\", \"name-rev\", \"HEAD\"]).split(\" \")[1].strip()\n \n def isValidGitDir(path):\n-    if (os.path.exists(path + \"/HEAD\")\n-        and os.path.exists(path + \"/refs\") and os.path.exists(path + \"/objects\")):\n-        return True;\n-    return False\n+    return git_dir(path) != None\n \n def parseRevision(ref):\n     return read_pipe(\"git rev-parse %s\" % ref).strip()\n@@ -3682,6 +3689,7 @@ def main():\n         if cmd.gitdir == None:\n             cmd.gitdir = os.path.abspath(\".git\")\n             if not isValidGitDir(cmd.gitdir):\n+                # \"rev-parse --git-dir\" without arguments will try $PWD/.git\n                 cmd.gitdir = read_pipe(\"git rev-parse --git-dir\").strip()\n                 if os.path.exists(cmd.gitdir):\n                     cdup = read_pipe(\"git rev-parse --show-cdup\").strip()\n@@ -3694,6 +3702,7 @@ def main():\n             else:\n                 die(\"fatal: cannot locate git repository at %s\" % cmd.gitdir)\n \n+        # so git commands invoked from the P4 workspace will succeed\n         os.environ[\"GIT_DIR\"] = cmd.gitdir\n \n     if not cmd.run(args):\ndiff --git a/t/t9800-git-p4-basic.sh b/t/t9800-git-p4-basic.sh\nindex 0730f18..093e9bd 100755\n--- a/t/t9800-git-p4-basic.sh\n+++ b/t/t9800-git-p4-basic.sh\n@@ -257,6 +257,26 @@ test_expect_success 'submit from detached head' '\n \t)\n '\n \n+test_expect_success 'submit from worktree' '\n+\ttest_when_finished cleanup_git &&\n+\tgit p4 clone --dest=\"$git\" //depot &&\n+\t(\n+\t\tcd \"$git\" &&\n+\t\tgit worktree add ../worktree-test\n+\t) &&\n+\t(\n+\t\tcd \"$git/../worktree-test\" &&\n+\t\ttest_commit \"worktree-commit\" &&\n+\t\tgit config git-p4.skipSubmitEdit true &&\n+\t\tgit p4 submit\n+\t) &&\n+\t(\n+\t\tcd \"$cli\" &&\n+\t\tp4 sync &&\n+\t\ttest_path_is_file worktree-commit.t\n+\t)\n+'\n+\n test_expect_success 'kill p4d' '\n \tkill_p4d\n '\ndiff --git a/t/t9806-git-p4-options.sh b/t/t9806-git-p4-options.sh\nindex 254d428..1ab76c4 100755\n--- a/t/t9806-git-p4-options.sh\n+++ b/t/t9806-git-p4-options.sh\n@@ -269,6 +269,38 @@ test_expect_success 'submit works with two branches' '\n \t)\n '\n \n+test_expect_success 'use --git-dir option and GIT_DIR' '\n+\ttest_when_finished cleanup_git &&\n+\tgit p4 clone //depot --destination=\"$git\" &&\n+\t(\n+\t\tcd \"$git\" &&\n+\t\tgit config git-p4.skipSubmitEdit true &&\n+\t\ttest_commit first-change &&\n+\t\tgit p4 submit --git-dir \"$git\"\n+\t) &&\n+\t(\n+\t\tcd \"$cli\" &&\n+\t\tp4 sync &&\n+\t\ttest_path_is_file first-change.t &&\n+\t\techo \"cli_file\" >cli_file.t &&\n+\t\tp4 add cli_file.t &&\n+\t\tp4 submit -d \"cli change\"\n+\t) &&\n+\t(git --git-dir \"$git\" p4 sync) &&\n+\t(cd \"$git\" && git checkout -q p4/master) &&\n+\ttest_path_is_file \"$git\"/cli_file.t &&\n+\t(\n+\t\tcd \"$cli\" &&\n+\t\techo \"cli_file2\" >cli_file2.t &&\n+\t\tp4 add cli_file2.t  &&\n+\t\tp4 submit -d \"cli change2\"\n+\t) &&\n+\t(GIT_DIR=\"$git\" git p4 sync) &&\n+\t(cd \"$git\" && git checkout -q p4/master) &&\n+\ttest_path_is_file \"$git\"/cli_file2.t\n+'\n+\n+\n test_expect_success 'kill p4d' '\n \tkill_p4d\n '\n-- \n2.8.2.703.g78b384c.dirty\n\n"},{"id":"307722","messageId":"xmqqlgvjz81j.fsf@gitster.mtv.corp.google.com","threadId":"44696","inReplyTo":"20161213215128.20288-2-luke@diamand.org","subject":"Re: [PATCHv3] git-p4: support git worktrees","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-12-14T00:08:40Z","receivedAt":"2016-12-14T00:09:24Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Luke Diamand <luke@diamand.org> writes:\n\n> git-p4 would attempt to find the git directory using\n> its own specific code, which did not know about git\n> worktrees.\n>\n> Rework it to use \"git rev-parse --git-dir\" instead.\n>\n> Add test cases for worktree usage and specifying\n> git directory via --git-dir and $GIT_DIR.\n>\n> Signed-off-by: Luke Diamand <luke@diamand.org>\n> ---\n>  git-p4.py                 | 17 +++++++++++++----\n>  t/t9800-git-p4-basic.sh   | 20 ++++++++++++++++++++\n>  t/t9806-git-p4-options.sh | 32 ++++++++++++++++++++++++++++++++\n>  3 files changed, 65 insertions(+), 4 deletions(-)\n>\n> diff --git a/git-p4.py b/git-p4.py\n> index fd5ca52..6a1f65f 100755\n> --- a/git-p4.py\n> +++ b/git-p4.py\n> @@ -85,6 +85,16 @@ def p4_build_cmd(cmd):\n>          real_cmd += cmd\n>      return real_cmd\n>  \n> +def git_dir(path):\n> +    \"\"\" Return TRUE if the given path is a git directory (/path/to/dir/.git).\n> +        This won't automatically add \".git\" to a directory.\n> +    \"\"\"\n> +    d = read_pipe([\"git\", \"--git-dir\", path, \"rev-parse\", \"--git-dir\"], True).strip()\n> +    if not d or len(d) == 0:\n> +        return None\n> +    else:\n> +        return d\n> +\n>  def chdir(path, is_client_path=False):\n>      \"\"\"Do chdir to the given path, and set the PWD environment\n>         variable for use by P4.  It does not look at getcwd() output.\n> @@ -563,10 +573,7 @@ def currentGitBranch():\n>          return read_pipe([\"git\", \"name-rev\", \"HEAD\"]).split(\" \")[1].strip()\n>  \n>  def isValidGitDir(path):\n> -    if (os.path.exists(path + \"/HEAD\")\n> -        and os.path.exists(path + \"/refs\") and os.path.exists(path + \"/objects\")):\n> -        return True;\n> -    return False\n> +    return git_dir(path) != None\n>  \n>  def parseRevision(ref):\n>      return read_pipe(\"git rev-parse %s\" % ref).strip()\n> @@ -3682,6 +3689,7 @@ def main():\n>          if cmd.gitdir == None:\n>              cmd.gitdir = os.path.abspath(\".git\")\n>              if not isValidGitDir(cmd.gitdir):\n> +                # \"rev-parse --git-dir\" without arguments will try $PWD/.git\n>                  cmd.gitdir = read_pipe(\"git rev-parse --git-dir\").strip()\n>                  if os.path.exists(cmd.gitdir):\n>                      cdup = read_pipe(\"git rev-parse --show-cdup\").strip()\n> @@ -3694,6 +3702,7 @@ def main():\n>              else:\n>                  die(\"fatal: cannot locate git repository at %s\" % cmd.gitdir)\n>  \n> +        # so git commands invoked from the P4 workspace will succeed\n>          os.environ[\"GIT_DIR\"] = cmd.gitdir\n\nThe real fix has become surprisingly short and \"feels right\".\n\nWill queue. Thanks.\n\n"},{"id":"307723","messageId":"20161213215128.20288-1-luke@diamand.org","threadId":"44696","inReplyTo":null,"subject":"[PATCHv3] git-p4 worktree support","fromName":"Luke Diamand","fromEmail":"luke@diamand.org","sentAt":"2016-12-13T21:51:27Z","receivedAt":"2016-12-14T00:09:27Z","isPatch":false,"sender":{"key":"luke@diamand.org","avatar":"https://avatars.githubusercontent.com/u/5330967?v=4"},"body":"Support for git-p4 worktrees.\n\nAdds test cases for using git from a worktree, and\nspecifying the git directory either with the --git-dir\ncommand-line option, or with $ENV{GIT_DIR}.\n\nLuke Diamand (1):\n  git-p4: support git worktrees\n\n git-p4.py                 | 17 +++++++++++++----\n t/t9800-git-p4-basic.sh   | 20 ++++++++++++++++++++\n t/t9806-git-p4-options.sh | 32 ++++++++++++++++++++++++++++++++\n 3 files changed, 65 insertions(+), 4 deletions(-)\n\n-- \n2.8.2.703.g78b384c.dirty\n\n"}]}