'help' =>
"Apply changes from a git patchfile or unified patchfile.",
),
+ 'update' => array(
+ 'supports' => array(
+ 'git', 'svn', 'hg'
+ ),
+ 'help' =>
+ "Update the local working copy before applying the patch.",
+ 'conflicts' => array(
+ 'nobranch' => true,
+ ),
+ ),
'nocommit' => array(
'supports' => array(
'git'
"Normally under git if the patch is successful the changes are ".
"committed to the working copy. This flag prevents the commit.",
),
+ 'nobranch' => array(
+ 'supports' => array(
+ 'git'
+ ),
+ 'help' =>
+ "Normally under git a new branch is created and then the patch ".
+ "is applied and committed in the branch. This flag skips the ".
+ "branch creation step and applies and commits the patch to the ".
+ "current branch.",
+ 'conflicts' => array(
+ 'update' => true,
+ ),
+ ),
'force' => array(
'help' =>
"Do not run any sanity checks.",
return true;
}
+ private function shouldBranch() {
+ // git only for now
+ $repository_api = $this->getRepositoryAPI();
+ if (!($repository_api instanceof ArcanistGitAPI)) {
+ return false;
+ }
+
+ $no_branch = $this->getArgument('nobranch', false);
+ if ($no_branch) {
+ return false;
+ }
+
+ return true;
+ }
+
+ private function getBranchName(ArcanistBundle $bundle) {
+ $branch_name = null;
+ $repository_api = $this->getRepositoryAPI();
+ $revision_id = $bundle->getRevisionID();
+ if ($revision_id) {
+ $base_name = "D{$revision_id}";
+ } else {
+ $base_name = "Arcanist-created-branch";
+ }
+
+ $suffixes = array(null, '-1', '-2', '-3');
+ foreach ($suffixes as $suffix) {
+ $proposed_name = $base_name.$suffix;
+
+ list($err) = exec_manual(
+ '(cd %s; git rev-parse --verify %s)',
+ $repository_api->getPath(),
+ $proposed_name
+ );
+
+ // no error means git rev-parse found a branch
+ if (!$err) {
+ echo phutil_console_format(
+ "Branch name {$proposed_name} alread exists; trying a new name.\n"
+ );
+ continue;
+ } else {
+ $branch_name = $proposed_name;
+ break;
+ }
+ }
+
+ if (!$branch_name) {
+ throw new Exception(
+ "Arc was unable to automagically make a name for this patch. ".
+ "Please clean up your working copy and try again."
+ );
+ }
+
+ return $branch_name;
+ }
+
+ private function createBranch(ArcanistBundle $bundle) {
+ $branch_name = $this->getBranchName($bundle);
+ $repository_api = $this->getRepositoryAPI();
+ $base_revision = $bundle->getBaseRevision();
+
+ if ($base_revision) {
+ execx(
+ '(cd %s; git checkout -b %s %s)',
+ $repository_api->getPath(),
+ $branch_name,
+ $base_revision);
+ } else {
+ execx(
+ '(cd %s; git checkout -b %s)',
+ $repository_api->getPath(),
+ $branch_name);
+ }
+
+ echo phutil_console_format(
+ "Created and checked out branch {$branch_name}.\n"
+ );
+ }
+
+ private function shouldUpdateWorkingCopy() {
+ return $this->getArgument('update', false);
+ }
+
+ private function updateWorkingCopy() {
+ $repository_api = $this->getRepositoryAPI();
+ if ($repository_api instanceof ArcanistSubversionAPI) {
+ execx(
+ '(cd %s; svn up)',
+ $repository_api->getPath());
+ $message = "Updated to HEAD. ";
+ } else if ($repository_api instanceof ArcanistGitAPI) {
+ execx(
+ '(cd %s; git pull)',
+ $repository_api->getPath());
+ $message = "Updated to HEAD. ";
+ } else if ($repository_api instanceof ArcanistMercurialAPI) {
+ execx(
+ '(cd %s; hg up)',
+ $repository_api->getPath());
+ $message = "Updated to tip. ";
+ } else {
+ throw new Exception('Unknown version control system.');
+ }
+
+ echo phutil_console_format($message."\n");
+ }
+
public function run() {
$source = $this->getSource();
}
}
+ // we should update the working copy before we do ANYTHING else
+ if ($this->shouldUpdateWorkingCopy()) {
+ $this->updateWorkingCopy();
+ }
+
+ if ($this->shouldBranch()) {
+ $this->createBranch($bundle);
+ }
+
$force = $this->getArgument('force', false);
if ($force) {
// force means don't do any sanity checks about the patch
if ($patch_err == 0) {
echo phutil_console_format(
- "<bg:green>** OKAY **</bg> Successfully applied patch to the ".
- "working copy.\n");
+ "<bg:green>** OKAY **</bg> Successfully applied patch ".
+ "to the working copy.\n");
} else {
echo phutil_console_format(
"\n\n<bg:yellow>** WARNING **</bg> Some hunks could not be applied ".