'ArcanistLinterTestCase' => 'lint/linter/base/test',
'ArcanistListWorkflow' => 'workflow/list',
'ArcanistMarkCommittedWorkflow' => 'workflow/mark-committed',
+ 'ArcanistMercurialAPI' => 'repository/api/mercurial',
'ArcanistNoEffectException' => 'exception/usage/noeffect',
'ArcanistNoEngineException' => 'exception/usage/noengine',
'ArcanistNoLintLinter' => 'lint/linter/nolint',
'ArcanistLinterTestCase' => 'ArcanistPhutilTestCase',
'ArcanistListWorkflow' => 'ArcanistBaseWorkflow',
'ArcanistMarkCommittedWorkflow' => 'ArcanistBaseWorkflow',
+ 'ArcanistMercurialAPI' => 'ArcanistRepositoryAPI',
'ArcanistNoEffectException' => 'ArcanistUsageException',
'ArcanistNoEngineException' => 'ArcanistUsageException',
'ArcanistNoLintLinter' => 'ArcanistLinter',
protected $text;
protected $line;
protected $isGit;
+ protected $isMercurial;
protected $detectBinaryFiles = false;
protected $changes = array();
'(?P<binary>Binary) files '.
'(?P<old>.+)\s+\d{4}-\d{2}-\d{2} and '.
'(?P<new>.+)\s+\d{4}-\d{2}-\d{2} differ.*',
+
+ // This is a normal Mercurial text change, probably from "hg diff".
+ '(?P<type>diff -r) (?P<hgrev>[a-f0-9]+) (?P<cur>.+)',
);
$ok = false;
$line = $this->nextLine();
$this->parseChangeset($change);
break;
+ case 'diff -r':
+ $this->setIsMercurial(true);
+ $this->parseIndexHunk($change);
+ break;
default:
$this->didFailParse("Unknown diff type.");
}
return $this->isGit;
}
+ public function setIsMercurial($is_mercurial) {
+ $this->isMercurial = $is_mercurial;
+ return $this;
+ }
+
+ public function getIsMercurial() {
+ return $this->isMercurial;
+ }
+
protected function parseIndexHunk(ArcanistDiffChange $change) {
$is_git = $this->getIsGit();
+ $is_mercurial = $this->getIsMercurial();
+ $is_svn = (!$is_git && !$is_mercurial);
$line = $this->getLine();
if ($is_git) {
}
$line = $this->getLine();
- $ok = preg_match('/^=+$/', $line) ||
- ($is_git && preg_match('/^index .*$/', $line));
- if (!$ok) {
- if ($is_git) {
- $this->didFailParse(
- "Expected 'index af23f...a98bc' header line.");
+
+ if ($is_svn) {
+ $ok = preg_match('/^=+$/', $line);
+ if (!$ok) {
+ $this->didFailParse("Expected '=======================' divider line.");
} else {
- $this->didFailParse(
- "Expected '==========================' divider line.");
+ // Adding an empty file in SVN can produce an empty line here.
+ $line = $this->nextNonemptyLine();
+ }
+ } else if ($is_git) {
+ $ok = preg_match('/^index .*$/', $line);
+ if (!$ok) {
+ // TODO: "hg diff -g" diffs ("mercurial git-style diffs") do not include
+ // this line, so we can't parse them if we fail on it. Maybe introduce
+ // a flag saying "parse this diff using relaxed git-style diff rules"?
+
+ // $this->didFailParse("Expected 'index af23f...a98bc' header line.");
+ } else {
+ $line = $this->nextLine();
}
}
- // Adding an empty file in SVN can produce an empty line here.
- $line = $this->nextNonemptyLine();
// If there are files with only whitespace changes and -b or -w are
// supplied as command-line flags to `diff', svn and git both produce
protected function parseHunkTarget() {
$line = $this->getLine();
$matches = null;
+
+ $remainder = '(?:\s*\(.*\))?';
+ if ($this->getIsMercurial()) {
+ // Something like "Fri Aug 26 01:20:50 2005 -0700", don't bother trying
+ // to parse it.
+ $remainder = '\t.*';
+ }
+
$ok = preg_match(
- '@^[-+]{3} (?:[ab]/)?(?P<path>.*?)(?:\s*\(.*\))?$@',
+ '@^[-+]{3} (?:[ab]/)?(?P<path>.*?)'.$remainder.'$@',
$line,
$matches);
if (!$ok) {
$this->didFailParse(
"Expected hunk target '+++ path/to/file.ext (revision N)'.");
}
+
$this->nextLine();
return $matches['path'];
}
"any parent directory. Create an '.arcconfig' file to configure arc.");
}
- if (@file_exists($root.'/.svn')) {
- phutil_require_module('arcanist', 'repository/api/subversion');
- return new ArcanistSubversionAPI($root);
+ if (Filesystem::pathExists($root.'/.svn')) {
+ return newv('ArcanistSubversionAPI', array($root));
}
+ if (Filesystem::pathExists($root.'/.hg')) {
+ // TODO: Stabilize and remove.
+ file_put_contents(
+ 'php://stderr',
+ phutil_console_format(
+ "**WARNING:** Mercurial support is largely imaginary right now.\n"));
+
+ return newv('ArcanistMercurialAPI', array($root));
+ }
+
+
$git_root = self::discoverGitBaseDirectory($root);
if ($git_root) {
if (!Filesystem::pathsAreEquivalent($root, $git_root)) {
"'.arcconfig' file is located at '{$root}', but working copy root ".
"is '{$git_root}'. Move '.arcconfig' file to the working copy root.");
}
- phutil_require_module('arcanist', 'repository/api/git');
- return new ArcanistGitAPI($root);
+
+ return newv('ArcanistGitAPI', array($root));
}
throw new ArcanistUsageException(
"The current working directory is not part of a working copy for a ".
- "supported version control system (svn or git).");
+ "supported version control system (svn, git or mercurial).");
}
- protected function __construct($path) {
+ public function __construct($path) {
$this->path = $path;
}
phutil_require_module('arcanist', 'exception/usage');
+phutil_require_module('phutil', 'console');
phutil_require_module('phutil', 'filesystem');
phutil_require_module('phutil', 'future/exec');
+phutil_require_module('phutil', 'utils');
phutil_require_source('ArcanistRepositoryAPI.php');
}
public function getRawDiffText($path) {
- $relative_commit = $this->getRelativeCommit();
$options = $this->getDiffFullOptions();
list($stdout) = execx(
"(cd %s; git diff {$options} %s -- %s)",
--- /dev/null
+<?php
+
+/*
+ * Copyright 2011 Facebook, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * Interfaces with the Mercurial working copies.
+ *
+ * @group workingcopy
+ */
+class ArcanistMercurialAPI extends ArcanistRepositoryAPI {
+
+ private $status;
+ private $base;
+
+ public function getSourceControlSystemName() {
+ return 'hg';
+ }
+
+ public function getSourceControlBaseRevision() {
+ list($stdout) = execx(
+ '(cd %s && hg id -ir %s)',
+ $this->getPath(),
+ $this->getRelativeCommit());
+ return $stdout;
+ }
+
+ public function getSourceControlPath() {
+ return '/';
+ }
+
+ public function getBranchName() {
+ // TODO: I have nearly no idea how hg local branches work.
+ list($stdout) = execx(
+ '(cd %s && hg branch)',
+ $this->getPath());
+ return $stdout;
+ }
+
+ public function getRelativeCommit() {
+ // TODO: This is hardcoded.
+ return 'tip~1';
+ }
+
+ public function getBlame($path) {
+ list($stdout) = execx(
+ '(cd %s && hg blame -u -v -c --rev %s -- %s)',
+ $this->getPath(),
+ $this->getRelativeCommit(),
+ $path);
+
+ $blame = array();
+ foreach (explode("\n", trim($stdout)) as $line) {
+ if (!strlen($line)) {
+ continue;
+ }
+
+ $matches = null;
+ $ok = preg_match('^/\s*([^:]+?) [a-f0-9]{12}: (.*)$/', $line, $matches);
+
+ if (!$ok) {
+ throw new Exception("Unable to parse Mercurial blame line: {$line}");
+ }
+
+ $revision = $matches[2];
+ $author = trim($matches[1]);
+ $blame[] = array($author, $revision);
+ }
+
+ return $blame;
+ }
+
+ public function getWorkingCopyStatus() {
+
+ // TODO: This is critical and not yet implemented.
+
+ return array();
+ }
+
+ private function getDiffOptions() {
+ $options = array(
+ '-g',
+ '-U'.$this->getDiffLinesOfContext(),
+ );
+ return implode(' ', $options);
+ }
+
+ public function getRawDiffText($path) {
+ $options = $this->getDiffOptions();
+
+ list($stdout) = execx(
+ '(cd %s && hg diff %C --rev %s --rev tip -- %s)',
+ $this->getPath(),
+ $options,
+ $this->getRelativeCommit(),
+ $path);
+
+ return $stdout;
+ }
+
+ public function getFullMercurialDiff() {
+ $options = $this->getDiffOptions();
+
+ list($stdout) = execx(
+ '(cd %s && hg diff %C --rev %s --rev tip --)',
+ $this->getPath(),
+ $options,
+ $this->getRelativeCommit());
+
+ return $stdout;
+ }
+
+ public function getOriginalFileData($path) {
+ return $this->getFileDataAtRevision($path, $this->getRelativeCommit());
+ }
+
+ public function getCurrentFileData($path) {
+ return $this->getFileDataAtRevision($path, 'tip');
+ }
+
+ private function getFileDataAtRevision($path, $revision) {
+ list($stdout) = execx(
+ '(cd %s && hg cat --rev %s -- %s)',
+ $this->getPath(),
+ $path);
+ return $stdout;
+ }
+
+}
--- /dev/null
+<?php
+/**
+ * This file is automatically generated. Lint this module to rebuild it.
+ * @generated
+ */
+
+
+
+phutil_require_module('arcanist', 'repository/api/base');
+
+phutil_require_module('phutil', 'future/exec');
+
+
+phutil_require_source('ArcanistMercurialAPI.php');
if ($info['uuid']) {
$repo_uuid = $info['uuid'];
}
- } else {
+ } else if ($repository_api instanceof ArcanistSubversionAPI) {
$repo_uuid = $repository_api->getRepositorySVNUUID();
+ } else if ($repository_api instanceof ArcanistMercurialAPI) {
+ // TODO: Provide this information.
+ } else {
+ throw new Exception("Unsupported repository API!");
}
$working_copy = $this->getWorkingCopy();
}
protected function shouldOnlyCreateDiff() {
+
$repository_api = $this->getRepositoryAPI();
if ($repository_api instanceof ArcanistSubversionAPI) {
return true;
}
+
+ if ($repository_api instanceof ArcanistMercurialAPI) {
+ // TODO: This is unlikely to be correct since it excludes using local
+ // branching in Mercurial.
+ return true;
+ }
+
return $this->getArgument('preview') ||
$this->getArgument('only');
}
}
}
- } else {
+ } else if ($repository_api instanceof ArcanistGitAPI) {
$this->parseGitRelativeCommit(
$repository_api,
$this->getArgument('paths', array()));
$paths = $repository_api->getWorkingCopyStatus();
+ } else if ($repository_api instanceof ArcanistMercurialAPI) {
+ // TODO: Unify this and the previous block.
+
+ // TODO: Parse the relative commit.
+
+ $paths = $repository_api->getWorkingCopyStatus();
+ } else {
+ throw new Exception("Unknown VCS!");
}
foreach ($paths as $path => $mask) {
}
$changes = $parser->parseDiff($diff);
+ } else if ($repository_api instanceof ArcanistMercurialAPI) {
+ $diff = $repository_api->getFullMercurialDiff();
+ $changes = $parser->parseDiff($diff);
} else {
throw new Exception("Repository API is not supported.");
}