From b3108661bb950424a99baa1c9ec93819feee4ad0 Mon Sep 17 00:00:00 2001 From: durham Date: Tue, 16 Apr 2013 13:32:12 -0700 Subject: [PATCH] Enable lint amending commits in mercurial Summary: arc lint was hardcoded for git for amending commits with lint patches. This enables the same functionality for mercurial. Test Plan: Made some changes that would result in a lint patch. arc diff Verify that the patches it produces were amended into the commit. Verified it still works in git as well. Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Differential Revision: https://secure.phabricator.com/D5716 --- src/repository/api/ArcanistGitAPI.php | 17 ++++++++++------ src/repository/api/ArcanistMercurialAPI.php | 6 +++++- src/repository/api/ArcanistRepositoryAPI.php | 2 +- src/workflow/ArcanistLintWorkflow.php | 21 ++++++++++++-------- 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/repository/api/ArcanistGitAPI.php b/src/repository/api/ArcanistGitAPI.php index bcf0a876..4367a035 100644 --- a/src/repository/api/ArcanistGitAPI.php +++ b/src/repository/api/ArcanistGitAPI.php @@ -514,12 +514,17 @@ final class ArcanistGitAPI extends ArcanistRepositoryAPI { return $this; } - public function amendCommit($message) { - $tmp_file = new TempFile(); - Filesystem::writeFile($tmp_file, $message); - $this->execxLocal( - 'commit --amend --allow-empty -F %s', - $tmp_file); + public function amendCommit($message = null) { + if ($message === null) { + $this->execxLocal('commit --amend --allow-empty -C HEAD'); + } else { + $tmp_file = new TempFile(); + Filesystem::writeFile($tmp_file, $message); + $this->execxLocal( + 'commit --amend --allow-empty -F %s', + $tmp_file); + } + $this->reloadWorkingCopy(); return $this; } diff --git a/src/repository/api/ArcanistMercurialAPI.php b/src/repository/api/ArcanistMercurialAPI.php index 5f9eebb3..da82d3a3 100644 --- a/src/repository/api/ArcanistMercurialAPI.php +++ b/src/repository/api/ArcanistMercurialAPI.php @@ -715,7 +715,11 @@ final class ArcanistMercurialAPI extends ArcanistRepositoryAPI { $this->reloadWorkingCopy(); } - public function amendCommit($message) { + public function amendCommit($message = null) { + if ($message === null) { + $message = $this->getCommitMessage('.'); + } + $tmp_file = new TempFile(); Filesystem::writeFile($tmp_file, $message); $this->execxLocal( diff --git a/src/repository/api/ArcanistRepositoryAPI.php b/src/repository/api/ArcanistRepositoryAPI.php index 5e42918b..b45184bb 100644 --- a/src/repository/api/ArcanistRepositoryAPI.php +++ b/src/repository/api/ArcanistRepositoryAPI.php @@ -370,7 +370,7 @@ abstract class ArcanistRepositoryAPI { throw new ArcanistCapabilityNotSupportedException($this); } - public function amendCommit($message) { + public function amendCommit($message = null) { throw new ArcanistCapabilityNotSupportedException($this); } diff --git a/src/workflow/ArcanistLintWorkflow.php b/src/workflow/ArcanistLintWorkflow.php index 054e409e..bf2c84e2 100644 --- a/src/workflow/ArcanistLintWorkflow.php +++ b/src/workflow/ArcanistLintWorkflow.php @@ -380,6 +380,12 @@ EOTEXT $prompt_autofix_patches = true; } + $repository_api = $this->getRepositoryAPI(); + if ($this->shouldAmendChanges) { + $this->shouldAmendChanges = $repository_api->supportsAmend() && + !$this->isHistoryImmutable(); + } + $wrote_to_disk = false; switch ($this->getArgument('output')) { @@ -461,11 +467,7 @@ EOTEXT } } - $repository_api = $this->getRepositoryAPI(); - if ($wrote_to_disk && - ($repository_api instanceof ArcanistGitAPI) && - $this->shouldAmendChanges) { - + if ($wrote_to_disk && $this->shouldAmendChanges) { if ($this->shouldAmendWithoutPrompt || ($this->shouldAmendAutofixesWithoutPrompt && $all_autofix)) { $console->writeOut( @@ -477,9 +479,12 @@ EOTEXT } if ($amend) { - execx( - '(cd %s; git commit -a --amend -C HEAD)', - $repository_api->getPath()); + if ($repository_api instanceof ArcanistGitAPI) { + // Add the changes to the index before amending + $repository_api->execxLocal('add -A'); + } + + $repository_api->amendCommit(); } else { throw new ArcanistUsageException( "Sort out the lint changes that were applied to the working ".