From 8e8d91a1ff608a7c924f498cec72602e461e6f58 Mon Sep 17 00:00:00 2001 From: epriestley Date: Wed, 21 Sep 2011 16:05:43 -0700 Subject: [PATCH] Allow Diffusion to display the initial commit in Git repositories Summary: See T507. Since you can't do "xxxxxxxx^" where "xxxxxxxx" is the first commit in a repository, fall back to diffing against the empty tree if we fail to diff against the parent commit. Test Plan: Looked at the first commit in libphutil on my local. Reviewers: edward, jungejason, nh, tuomaspelkonen, aran Reviewed By: nh CC: aran, edward, epriestley, nh Differential Revision: 953 --- .../query/diff/git/DiffusionGitDiffQuery.php | 36 +++++++++++++++---- .../diffusion/query/diff/git/__init__.php | 1 + .../PhabricatorRepositoryEditController.php | 2 +- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/applications/diffusion/query/diff/git/DiffusionGitDiffQuery.php b/src/applications/diffusion/query/diff/git/DiffusionGitDiffQuery.php index 2ada296005..02ec97d327 100644 --- a/src/applications/diffusion/query/diff/git/DiffusionGitDiffQuery.php +++ b/src/applications/diffusion/query/diff/git/DiffusionGitDiffQuery.php @@ -44,12 +44,36 @@ final class DiffusionGitDiffQuery extends DiffusionDiffQuery { ); $options = implode(' ', $options); - list($raw_diff) = execx( - "(cd %s && git diff {$options} %s^ %s -- %s)", - $repository->getDetail('local-path'), - $effective_commit, - $effective_commit, - $drequest->getPath()); + try { + list($raw_diff) = execx( + "(cd %s && git diff %C %s^ %s -- %s)", + $repository->getDetail('local-path'), + $options, + $effective_commit, + $effective_commit, + $drequest->getPath()); + } catch (CommandException $ex) { + // Check if this is the root commit by seeing if it has parents. + list($parents) = execx( + '(cd %s && git log --format=%s %s --)', + $repository->getDetail('local-path'), + '%P', // "parents" + $effective_commit); + if (!strlen(trim($parents))) { + // No parents means we're looking at the root revision. Diff against + // the empty tree hash instead, since there is no parent so "^" does + // not work. See ArcanistGitAPI for more discussion. + list($raw_diff) = execx( + '(cd %s && git diff %C %s %s -- %s)', + $repository->getDetail('local-path'), + $options, + ArcanistGitAPI::GIT_MAGIC_ROOT_COMMIT, + $effective_commit, + $drequest->getPath()); + } else { + throw $ex; + } + } $parser = new ArcanistDiffParser(); $parser->setDetectBinaryFiles(true); diff --git a/src/applications/diffusion/query/diff/git/__init__.php b/src/applications/diffusion/query/diff/git/__init__.php index 3a9b21da1f..d85a8e45c7 100644 --- a/src/applications/diffusion/query/diff/git/__init__.php +++ b/src/applications/diffusion/query/diff/git/__init__.php @@ -7,6 +7,7 @@ phutil_require_module('arcanist', 'parser/diff'); +phutil_require_module('arcanist', 'repository/api/git'); phutil_require_module('phabricator', 'applications/differential/storage/diff'); phutil_require_module('phabricator', 'applications/diffusion/query/diff/base'); diff --git a/src/applications/repository/controller/edit/PhabricatorRepositoryEditController.php b/src/applications/repository/controller/edit/PhabricatorRepositoryEditController.php index 1b55461aec..ad3e69a448 100644 --- a/src/applications/repository/controller/edit/PhabricatorRepositoryEditController.php +++ b/src/applications/repository/controller/edit/PhabricatorRepositoryEditController.php @@ -405,7 +405,7 @@ class PhabricatorRepositoryEditController if ($is_git) { $instructions = 'Enter the URI to clone this repository from. It should look like '. - 'git@github.com:example/example.git, '. + 'git@github.com:example/example.git or '. 'ssh://user@host.com/git/example.git'; } else if ($is_mercurial) { $instructions =