Page MenuHomeSealhub

No OneTemporary

diff --git a/src/parser/ArcanistBundle.php b/src/parser/ArcanistBundle.php
index c0000c9f..6e46b8e8 100644
--- a/src/parser/ArcanistBundle.php
+++ b/src/parser/ArcanistBundle.php
@@ -1,839 +1,875 @@
<?php
/**
* Converts changesets between different formats.
*
* @group diff
*/
final class ArcanistBundle {
private $changes;
private $conduit;
private $blobs = array();
private $diskPath;
private $projectID;
private $baseRevision;
private $revisionID;
private $encoding;
private $loadFileDataCallback;
- private $author;
+ private $authorName;
+ private $authorEmail;
- public function setAuthor($author) {
- $this->author = $author;
+ public function setAuthorEmail($author_email) {
+ $this->authorEmail = $author_email;
return $this;
}
- public function getAuthor() {
- return $this->author;
+
+ public function getAuthorEmail() {
+ return $this->authorEmail;
+ }
+
+ public function setAuthorName($author_name) {
+ $this->authorName = $author_name;
+ return $this;
+ }
+
+ public function getAuthorName() {
+ return $this->authorName;
+ }
+
+ public function getFullAuthor() {
+ $author_name = $this->getAuthorName();
+ if ($author_name === null) {
+ return null;
+ }
+
+ $author_email = $this->getAuthorEmail();
+ if ($author_email === null) {
+ return null;
+ }
+
+ $full_author = sprintf('%s <%s>', $author_name, $author_email);
+
+ // Because git is very picky about the author being in a valid format,
+ // verify that we can parse it.
+ $address = new PhutilEmailAddress($full_author);
+ if (!$address->getDisplayName() || !$address->getAddress()) {
+ return null;
+ }
+
+ return $full_author;
}
public function setConduit(ConduitClient $conduit) {
$this->conduit = $conduit;
return $this;
}
public function setProjectID($project_id) {
$this->projectID = $project_id;
return $this;
}
public function getProjectID() {
return $this->projectID;
}
public function setBaseRevision($base_revision) {
$this->baseRevision = $base_revision;
return $this;
}
public function setEncoding($encoding) {
$this->encoding = $encoding;
return $this;
}
public function getEncoding() {
return $this->encoding;
}
public function getBaseRevision() {
return $this->baseRevision;
}
public function setRevisionID($revision_id) {
$this->revisionID = $revision_id;
return $this;
}
public function getRevisionID() {
return $this->revisionID;
}
public static function newFromChanges(array $changes) {
$obj = new ArcanistBundle();
$obj->changes = $changes;
return $obj;
}
private function getEOL($patch_type) {
// NOTE: Git always generates "\n" line endings, even under Windows, and
// can not parse certain patches with "\r\n" line endings. SVN generates
// patches with "\n" line endings on Mac or Linux and "\r\n" line endings
// on Windows. (This EOL style is used only for patch metadata lines, not
// for the actual patch content.)
// (On Windows, Mercurial generates \n newlines for `--git` diffs, as it
// must, but also \n newlines for unified diffs. We never need to deal with
// these as we use Git format for Mercurial, so this case is currently
// ignored.)
switch ($patch_type) {
case 'git':
return "\n";
case 'unified':
return phutil_is_windows() ? "\r\n" : "\n";
default:
throw new Exception(
"Unknown patch type '{$patch_type}'!");
}
}
public static function newFromArcBundle($path) {
$path = Filesystem::resolvePath($path);
$future = new ExecFuture(
'tar tfO %s',
$path);
list($stdout, $file_list) = $future->resolvex();
$file_list = explode("\n", trim($file_list));
if (in_array('meta.json', $file_list)) {
$future = new ExecFuture(
'tar xfO %s meta.json',
$path);
$meta_info = $future->resolveJSON();
$version = idx($meta_info, 'version', 0);
$project_name = idx($meta_info, 'projectName');
$base_revision = idx($meta_info, 'baseRevision');
$revision_id = idx($meta_info, 'revisionID');
$encoding = idx($meta_info, 'encoding');
- $author = idx($meta_info, 'author');
- // this arc bundle was probably made before we started storing meta info
+ $author_name = idx($meta_info, 'authorName');
+ $author_email = idx($meta_info, 'authorEmail');
} else {
+ // this arc bundle was probably made before we started storing meta info
$version = 0;
$project_name = null;
$base_revision = null;
$revision_id = null;
$encoding = null;
$author = null;
}
$future = new ExecFuture(
'tar xfO %s changes.json',
$path);
$changes = $future->resolveJSON();
foreach ($changes as $change_key => $change) {
foreach ($change['hunks'] as $key => $hunk) {
list($hunk_data) = execx('tar xfO %s hunks/%s', $path, $hunk['corpus']);
$changes[$change_key]['hunks'][$key]['corpus'] = $hunk_data;
}
}
foreach ($changes as $change_key => $change) {
$changes[$change_key] = ArcanistDiffChange::newFromDictionary($change);
}
$obj = new ArcanistBundle();
$obj->changes = $changes;
$obj->diskPath = $path;
$obj->setProjectID($project_name);
$obj->setBaseRevision($base_revision);
$obj->setRevisionID($revision_id);
$obj->setEncoding($encoding);
return $obj;
}
public static function newFromDiff($data) {
$obj = new ArcanistBundle();
$parser = new ArcanistDiffParser();
$obj->changes = $parser->parseDiff($data);
return $obj;
}
private function __construct() {
}
public function writeToDisk($path) {
$changes = $this->getChanges();
$change_list = array();
foreach ($changes as $change) {
$change_list[] = $change->toDictionary();
}
$hunks = array();
foreach ($change_list as $change_key => $change) {
foreach ($change['hunks'] as $key => $hunk) {
$hunks[] = $hunk['corpus'];
$change_list[$change_key]['hunks'][$key]['corpus'] = count($hunks) - 1;
}
}
$blobs = array();
foreach ($change_list as $change) {
if (!empty($change['metadata']['old:binary-phid'])) {
$blobs[$change['metadata']['old:binary-phid']] = null;
}
if (!empty($change['metadata']['new:binary-phid'])) {
$blobs[$change['metadata']['new:binary-phid']] = null;
}
}
foreach ($blobs as $phid => $null) {
$blobs[$phid] = $this->getBlob($phid);
}
$meta_info = array(
- 'version' => 4,
+ 'version' => 5,
'projectName' => $this->getProjectID(),
'baseRevision' => $this->getBaseRevision(),
'revisionID' => $this->getRevisionID(),
'encoding' => $this->getEncoding(),
- 'author' => $this->getAuthor(),
+ 'authorName' => $this->getAuthorName(),
+ 'authorEmail' => $this->getAuthorEmail(),
);
$dir = Filesystem::createTemporaryDirectory();
Filesystem::createDirectory($dir.'/hunks');
Filesystem::createDirectory($dir.'/blobs');
Filesystem::writeFile($dir.'/changes.json', json_encode($change_list));
Filesystem::writeFile($dir.'/meta.json', json_encode($meta_info));
foreach ($hunks as $key => $hunk) {
Filesystem::writeFile($dir.'/hunks/'.$key, $hunk);
}
foreach ($blobs as $key => $blob) {
Filesystem::writeFile($dir.'/blobs/'.$key, $blob);
}
execx(
'(cd %s; tar -czf %s *)',
$dir,
Filesystem::resolvePath($path));
Filesystem::remove($dir);
}
public function toUnifiedDiff() {
$eol = $this->getEOL('unified');
$result = array();
$changes = $this->getChanges();
foreach ($changes as $change) {
$hunk_changes = $this->buildHunkChanges($change->getHunks(), $eol);
if (!$hunk_changes) {
continue;
}
$old_path = $this->getOldPath($change);
$cur_path = $this->getCurrentPath($change);
$index_path = $cur_path;
if ($index_path === null) {
$index_path = $old_path;
}
$result[] = 'Index: '.$index_path;
$result[] = $eol;
$result[] = str_repeat('=', 67);
$result[] = $eol;
if ($old_path === null) {
$old_path = '/dev/null';
}
if ($cur_path === null) {
$cur_path = '/dev/null';
}
// When the diff is used by `patch`, `patch` ignores what is listed as the
// current path and just makes changes to the file at the old path (unless
// the current path is '/dev/null'.
// If the old path and the current path aren't the same (and neither is
// /dev/null), this indicates the file was moved or copied. By listing
// both paths as the new file, `patch` will apply the diff to the new
// file.
if ($cur_path !== '/dev/null' && $old_path !== '/dev/null') {
$old_path = $cur_path;
}
$result[] = '--- '.$old_path.$eol;
$result[] = '+++ '.$cur_path.$eol;
$result[] = $hunk_changes;
}
if (!$result) {
return '';
}
$diff = implode('', $result);
return $this->convertNonUTF8Diff($diff);
}
public function toGitPatch() {
$eol = $this->getEOL('git');
$result = array();
$changes = $this->getChanges();
$binary_sources = array();
foreach ($changes as $change) {
if (!$this->isGitBinaryChange($change)) {
continue;
}
$type = $change->getType();
if ($type == ArcanistDiffChangeType::TYPE_MOVE_AWAY ||
$type == ArcanistDiffChangeType::TYPE_COPY_AWAY ||
$type == ArcanistDiffChangeType::TYPE_MULTICOPY) {
foreach ($change->getAwayPaths() as $path) {
$binary_sources[$path] = $change;
}
}
}
foreach (array_keys($changes) as $multicopy_key) {
$multicopy_change = $changes[$multicopy_key];
$type = $multicopy_change->getType();
if ($type != ArcanistDiffChangeType::TYPE_MULTICOPY) {
continue;
}
// Decompose MULTICOPY into one MOVE_HERE and several COPY_HERE because
// we need more information than we have in order to build a delete patch
// and represent it as a bunch of COPY_HERE plus a delete. For details,
// see T419.
// Basically, MULTICOPY means there are 2 or more corresponding COPY_HERE
// changes, so find one of them arbitrarily and turn it into a MOVE_HERE.
// TODO: We might be able to do this more cleanly after T230 is resolved.
$decompose_okay = false;
foreach ($changes as $change_key => $change) {
if ($change->getType() != ArcanistDiffChangeType::TYPE_COPY_HERE) {
continue;
}
if ($change->getOldPath() != $multicopy_change->getCurrentPath()) {
continue;
}
$decompose_okay = true;
$change = clone $change;
$change->setType(ArcanistDiffChangeType::TYPE_MOVE_HERE);
$changes[$change_key] = $change;
// The multicopy is now fully represented by MOVE_HERE plus one or more
// COPY_HERE, so throw it away.
unset($changes[$multicopy_key]);
break;
}
if (!$decompose_okay) {
throw new Exception(
"Failed to decompose multicopy changeset in order to generate diff.");
}
}
foreach ($changes as $change) {
$type = $change->getType();
$file_type = $change->getFileType();
if ($file_type == ArcanistDiffChangeType::FILE_DIRECTORY) {
// TODO: We should raise a FYI about this, so the user is aware
// that we omitted it, if the directory is empty or has permissions
// which git can't represent.
// Git doesn't support empty directories, so we simply ignore them. If
// the directory is nonempty, 'git apply' will create it when processing
// the changesets for files inside it.
continue;
}
if ($type == ArcanistDiffChangeType::TYPE_MOVE_AWAY) {
// Git will apply this in the corresponding MOVE_HERE.
continue;
}
$old_mode = idx($change->getOldProperties(), 'unix:filemode', '100644');
$new_mode = idx($change->getNewProperties(), 'unix:filemode', '100644');
$is_binary = $this->isGitBinaryChange($change);
if ($is_binary) {
$old_binary = idx($binary_sources, $this->getCurrentPath($change));
$change_body = $this->buildBinaryChange($change, $old_binary);
} else {
$change_body = $this->buildHunkChanges($change->getHunks(), $eol);
}
if ($type == ArcanistDiffChangeType::TYPE_COPY_AWAY) {
// TODO: This is only relevant when patching old Differential diffs
// which were created prior to arc pruning TYPE_COPY_AWAY for files
// with no modifications.
if (!strlen($change_body) && ($old_mode == $new_mode)) {
continue;
}
}
$old_path = $this->getOldPath($change);
$cur_path = $this->getCurrentPath($change);
if ($old_path === null) {
$old_index = 'a/'.$cur_path;
$old_target = '/dev/null';
} else {
$old_index = 'a/'.$old_path;
$old_target = 'a/'.$old_path;
}
if ($cur_path === null) {
$cur_index = 'b/'.$old_path;
$cur_target = '/dev/null';
} else {
$cur_index = 'b/'.$cur_path;
$cur_target = 'b/'.$cur_path;
}
$result[] = "diff --git {$old_index} {$cur_index}".$eol;
if ($type == ArcanistDiffChangeType::TYPE_ADD) {
$result[] = "new file mode {$new_mode}".$eol;
}
if ($type == ArcanistDiffChangeType::TYPE_COPY_HERE ||
$type == ArcanistDiffChangeType::TYPE_MOVE_HERE ||
$type == ArcanistDiffChangeType::TYPE_COPY_AWAY ||
$type == ArcanistDiffChangeType::TYPE_CHANGE) {
if ($old_mode !== $new_mode) {
$result[] = "old mode {$old_mode}".$eol;
$result[] = "new mode {$new_mode}".$eol;
}
}
if ($type == ArcanistDiffChangeType::TYPE_COPY_HERE) {
$result[] = "copy from {$old_path}".$eol;
$result[] = "copy to {$cur_path}".$eol;
} else if ($type == ArcanistDiffChangeType::TYPE_MOVE_HERE) {
$result[] = "rename from {$old_path}".$eol;
$result[] = "rename to {$cur_path}".$eol;
} else if ($type == ArcanistDiffChangeType::TYPE_DELETE ||
$type == ArcanistDiffChangeType::TYPE_MULTICOPY) {
$old_mode = idx($change->getOldProperties(), 'unix:filemode');
if ($old_mode) {
$result[] = "deleted file mode {$old_mode}".$eol;
}
}
if ($change_body) {
if (!$is_binary) {
$result[] = "--- {$old_target}".$eol;
$result[] = "+++ {$cur_target}".$eol;
}
$result[] = $change_body;
}
}
$diff = implode('', $result).$eol;
return $this->convertNonUTF8Diff($diff);
}
private function isGitBinaryChange(ArcanistDiffChange $change) {
$file_type = $change->getFileType();
return ($file_type == ArcanistDiffChangeType::FILE_BINARY ||
$file_type == ArcanistDiffChangeType::FILE_IMAGE);
}
private function convertNonUTF8Diff($diff) {
if ($this->encoding) {
$diff = phutil_utf8_convert($diff, $this->encoding, 'UTF-8');
}
return $diff;
}
public function getChanges() {
return $this->changes;
}
private function breakHunkIntoSmallHunks(ArcanistDiffHunk $base_hunk) {
$context = 3;
$results = array();
$lines = phutil_split_lines($base_hunk->getCorpus());
$n = count($lines);
$old_offset = $base_hunk->getOldOffset();
$new_offset = $base_hunk->getNewOffset();
$ii = 0;
$jj = 0;
while ($ii < $n) {
// Skip lines until we find the next line with changes. Note: this skips
// both ' ' (no changes) and '\' (no newline at end of file) lines. If we
// don't skip the latter, we may incorrectly generate a terminal hunk
// that has no actual change information when a file doesn't have a
// terminal newline and not changed near the end of the file. 'patch' will
// fail to apply the diff if we generate a hunk that does not actually
// contain changes.
for ($jj = $ii; $jj < $n; ++$jj) {
$char = $lines[$jj][0];
if ($char == '-' || $char == '+') {
break;
}
}
if ($jj >= $n) {
break;
}
$hunk_start = max($jj - $context, 0);
// NOTE: There are two tricky considerations here.
// We can not generate a patch with overlapping hunks, or 'git apply'
// rejects it after 1.7.3.4.
// We can not generate a patch with too much trailing context, or
// 'patch' rejects it.
// So we need to ensure that we generate disjoint hunks, but don't
// generate any hunks with too much context.
$old_lines = 0;
$new_lines = 0;
$hunk_adjust = 0;
$last_change = $jj;
$break_here = null;
for (; $jj < $n; ++$jj) {
if ($lines[$jj][0] == ' ') {
if ($jj - $last_change > $context) {
if ($break_here === null) {
// We haven't seen a change in $context lines, so this is a
// potential place to break the hunk. However, we need to keep
// looking in case there is another change fewer than $context
// lines away, in which case we have to merge the hunks.
$break_here = $jj;
}
}
if ($jj - $last_change > (($context + 1) * 2)) {
// We definitely aren't going to merge this with the next hunk, so
// break out of the loop. We'll end the hunk at $break_here.
break;
}
} else {
$break_here = null;
$last_change = $jj;
if ($lines[$jj][0] == '\\') {
// When we have a "\ No newline at end of file" line, it does not
// contribute to either hunk length.
++$hunk_adjust;
} else if ($lines[$jj][0] == '-') {
++$old_lines;
} else if ($lines[$jj][0] == '+') {
++$new_lines;
}
}
}
if ($break_here !== null) {
$jj = $break_here;
}
$hunk_length = min($jj, $n) - $hunk_start;
$count_length = ($hunk_length - $hunk_adjust);
$hunk = new ArcanistDiffHunk();
$hunk->setOldOffset($old_offset + $hunk_start - $ii);
$hunk->setNewOffset($new_offset + $hunk_start - $ii);
$hunk->setOldLength($count_length - $new_lines);
$hunk->setNewLength($count_length - $old_lines);
$corpus = array_slice($lines, $hunk_start, $hunk_length);
$corpus = implode('', $corpus);
$hunk->setCorpus($corpus);
$results[] = $hunk;
$old_offset += ($jj - $ii) - $new_lines;
$new_offset += ($jj - $ii) - $old_lines;
$ii = $jj;
}
return $results;
}
private function getOldPath(ArcanistDiffChange $change) {
$old_path = $change->getOldPath();
$type = $change->getType();
if (!strlen($old_path) ||
$type == ArcanistDiffChangeType::TYPE_ADD) {
$old_path = null;
}
return $old_path;
}
private function getCurrentPath(ArcanistDiffChange $change) {
$cur_path = $change->getCurrentPath();
$type = $change->getType();
if (!strlen($cur_path) ||
$type == ArcanistDiffChangeType::TYPE_DELETE ||
$type == ArcanistDiffChangeType::TYPE_MULTICOPY) {
$cur_path = null;
}
return $cur_path;
}
private function buildHunkChanges(array $hunks, $eol) {
assert_instances_of($hunks, 'ArcanistDiffHunk');
$result = array();
foreach ($hunks as $hunk) {
$small_hunks = $this->breakHunkIntoSmallHunks($hunk);
foreach ($small_hunks as $small_hunk) {
$o_off = $small_hunk->getOldOffset();
$o_len = $small_hunk->getOldLength();
$n_off = $small_hunk->getNewOffset();
$n_len = $small_hunk->getNewLength();
$corpus = $small_hunk->getCorpus();
// NOTE: If the length is 1 it can be omitted. Since git does this,
// we also do it so that "arc export --git" diffs are as similar to
// real git diffs as possible, which helps debug issues.
if ($o_len == 1) {
$o_head = "{$o_off}";
} else {
$o_head = "{$o_off},{$o_len}";
}
if ($n_len == 1) {
$n_head = "{$n_off}";
} else {
$n_head = "{$n_off},{$n_len}";
}
$result[] = "@@ -{$o_head} +{$n_head} @@".$eol;
$result[] = $corpus;
$last = substr($corpus, -1);
if ($last !== false && $last != "\r" && $last != "\n") {
$result[] = $eol;
}
}
}
return implode('', $result);
}
public function setLoadFileDataCallback($callback) {
$this->loadFileDataCallback = $callback;
return $this;
}
private function getBlob($phid, $name = null) {
if ($this->loadFileDataCallback) {
return call_user_func($this->loadFileDataCallback, $phid);
}
if ($this->diskPath) {
list($blob_data) = execx('tar xfO %s blobs/%s', $this->diskPath, $phid);
return $blob_data;
}
$console = PhutilConsole::getConsole();
if ($this->conduit) {
if ($name) {
$console->writeErr("Downloading binary data for '%s'...\n", $name);
} else {
$console->writeErr("Downloading binary data...\n");
}
$data_base64 = $this->conduit->callMethodSynchronous(
'file.download',
array(
'phid' => $phid,
));
return base64_decode($data_base64);
}
throw new Exception("Nowhere to load blob '{$phid}' from!");
}
private function buildBinaryChange(ArcanistDiffChange $change, $old_binary) {
$eol = $this->getEOL('git');
// In Git, when we write out a binary file move or copy, we need the
// original binary for the source and the current binary for the
// destination.
if ($old_binary) {
if ($old_binary->getOriginalFileData() !== null) {
$old_data = $old_binary->getOriginalFileData();
$old_phid = null;
} else {
$old_data = null;
$old_phid = $old_binary->getMetadata('old:binary-phid');
}
} else {
$old_data = $change->getOriginalFileData();
$old_phid = $change->getMetadata('old:binary-phid');
}
if ($old_data === null && $old_phid) {
$name = basename($change->getOldPath());
$old_data = $this->getBlob($old_phid, $name);
}
$old_length = strlen($old_data);
if ($old_data === null) {
$old_data = '';
$old_sha1 = str_repeat('0', 40);
} else {
$old_sha1 = sha1("blob {$old_length}\0{$old_data}");
}
$new_phid = $change->getMetadata('new:binary-phid');
$new_data = null;
if ($change->getCurrentFileData() !== null) {
$new_data = $change->getCurrentFileData();
} else if ($new_phid) {
$name = basename($change->getCurrentPath());
$new_data = $this->getBlob($new_phid, $name);
}
$new_length = strlen($new_data);
if ($new_data === null) {
$new_data = '';
$new_sha1 = str_repeat('0', 40);
} else {
$new_sha1 = sha1("blob {$new_length}\0{$new_data}");
}
$content = array();
$content[] = "index {$old_sha1}..{$new_sha1}".$eol;
$content[] = "GIT binary patch".$eol;
$content[] = "literal {$new_length}".$eol;
$content[] = $this->emitBinaryDiffBody($new_data).$eol;
$content[] = "literal {$old_length}".$eol;
$content[] = $this->emitBinaryDiffBody($old_data).$eol;
return implode('', $content);
}
private function emitBinaryDiffBody($data) {
$eol = $this->getEOL('git');
if (!function_exists('gzcompress')) {
throw new Exception(
"This patch has binary data. The PHP zlib extension is required to ".
"apply patches with binary data to git. Install the PHP zlib ".
"extension to continue.");
}
// See emit_binary_diff_body() in diff.c for git's implementation.
$buf = '';
$deflated = gzcompress($data);
$lines = str_split($deflated, 52);
foreach ($lines as $line) {
$len = strlen($line);
// The first character encodes the line length.
if ($len <= 26) {
$buf .= chr($len + ord('A') - 1);
} else {
$buf .= chr($len - 26 + ord('a') - 1);
}
$buf .= self::encodeBase85($line);
$buf .= $eol;
}
return $buf;
}
public static function encodeBase85($data) {
// This is implemented awkwardly in order to closely mirror git's
// implementation in base85.c
// It is also implemeted awkwardly to work correctly on 32-bit machines.
// Broadly, this algorithm converts the binary input to printable output
// by transforming each 4 binary bytes of input to 5 printable bytes of
// output, one piece at a time.
//
// To do this, we convert the 4 bytes into a 32-bit integer, then use
// modulus and division by 85 to pick out printable bytes (85^5 is slightly
// larger than 2^32). In C, this algorithm is fairly easy to implement
// because the accumulator can be made unsigned.
//
// In PHP, there are no unsigned integers, so values larger than 2^31 break
// on 32-bit systems under modulus:
//
// $ php -r 'print (1 << 31) % 13;' # On a 32-bit machine.
// -11
//
// However, PHP's float type is an IEEE 754 64-bit double precision float,
// so we can safely store integers up to around 2^53 without loss of
// precision. To work around the lack of an unsigned type, we just use a
// double and perform the modulus with fmod().
//
// (Since PHP overflows integer operations into floats, we don't need much
// additional casting.)
static $map = array(
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
'!', '#', '$', '%', '&', '(', ')', '*', '+', '-',
';', '<', '=', '>', '?', '@', '^', '_', '`', '{',
'|', '}', '~',
);
$buf = '';
$pos = 0;
$bytes = strlen($data);
while ($bytes) {
$accum = 0;
for ($count = 24; $count >= 0; $count -= 8) {
$val = ord($data[$pos++]);
$val = $val * (1 << $count);
$accum = $accum + $val;
if (--$bytes == 0) {
break;
}
}
$slice = '';
for ($count = 4; $count >= 0; $count--) {
$val = (int)fmod($accum, 85.0);
$accum = floor($accum / 85.0);
$slice .= $map[$val];
}
$buf .= strrev($slice);
}
return $buf;
}
}
diff --git a/src/workflow/ArcanistBaseWorkflow.php b/src/workflow/ArcanistBaseWorkflow.php
index 795555d2..72d5659e 100644
--- a/src/workflow/ArcanistBaseWorkflow.php
+++ b/src/workflow/ArcanistBaseWorkflow.php
@@ -1,1564 +1,1565 @@
<?php
/**
* Implements a runnable command, like "arc diff" or "arc help".
*
* = Managing Conduit =
*
* Workflows have the builtin ability to open a Conduit connection to a
* Phabricator installation, so methods can be invoked over the API. Workflows
* may either not need this (e.g., "help"), or may need a Conduit but not
* authentication (e.g., calling only public APIs), or may need a Conduit and
* authentication (e.g., "arc diff").
*
* To specify that you need an //unauthenticated// conduit, override
* @{method:requiresConduit} to return ##true##. To specify that you need an
* //authenticated// conduit, override @{method:requiresAuthentication} to
* return ##true##. You can also manually invoke @{method:establishConduit}
* and/or @{method:authenticateConduit} later in a workflow to upgrade it.
* Once a conduit is open, you can access the client by calling
* @{method:getConduit}, which allows you to invoke methods. You can get
* verified information about the user identity by calling @{method:getUserPHID}
* or @{method:getUserName} after authentication occurs.
*
* = Scratch Files =
*
* Arcanist workflows can read and write 'scratch files', which are temporary
* files stored in the project that persist across commands. They can be useful
* if you want to save some state, or keep a copy of a long message the user
* entered if something goes wrong.
*
*
* @task conduit Conduit
* @task scratch Scratch Files
* @group workflow
* @stable
*/
abstract class ArcanistBaseWorkflow extends Phobject {
const COMMIT_DISABLE = 0;
const COMMIT_ALLOW = 1;
const COMMIT_ENABLE = 2;
const AUTO_COMMIT_TITLE = 'Automatic commit by arc';
private $commitMode = self::COMMIT_DISABLE;
private $shouldAmend;
private $conduit;
private $conduitURI;
private $conduitCredentials;
private $conduitAuthenticated;
private $forcedConduitVersion;
private $conduitTimeout;
private $userPHID;
private $userName;
private $repositoryAPI;
private $workingCopy;
private $arguments;
private $passedArguments;
private $command;
private $projectInfo;
private $arcanistConfiguration;
private $parentWorkflow;
private $workingDirectory;
private $repositoryVersion;
private $changeCache = array();
public function __construct() {
}
abstract public function run();
/**
* Return the command used to invoke this workflow from the command like,
* e.g. "help" for @{class:ArcanistHelpWorkflow}.
*
* @return string The command a user types to invoke this workflow.
*/
abstract public function getWorkflowName();
/**
* Return console formatted string with all command synopses.
*
* @return string 6-space indented list of available command synopses.
*/
abstract public function getCommandSynopses();
/**
* Return console formatted string with command help printed in `arc help`.
*
* @return string 10-space indented help to use the command.
*/
abstract public function getCommandHelp();
/* -( Conduit )------------------------------------------------------------ */
/**
* Set the URI which the workflow will open a conduit connection to when
* @{method:establishConduit} is called. Arcanist makes an effort to set
* this by default for all workflows (by reading ##.arcconfig## and/or the
* value of ##--conduit-uri##) even if they don't need Conduit, so a workflow
* can generally upgrade into a conduit workflow later by just calling
* @{method:establishConduit}.
*
* You generally should not need to call this method unless you are
* specifically overriding the default URI. It is normally sufficient to
* just invoke @{method:establishConduit}.
*
* NOTE: You can not call this after a conduit has been established.
*
* @param string The URI to open a conduit to when @{method:establishConduit}
* is called.
* @return this
* @task conduit
*/
final public function setConduitURI($conduit_uri) {
if ($this->conduit) {
throw new Exception(
"You can not change the Conduit URI after a conduit is already open.");
}
$this->conduitURI = $conduit_uri;
return $this;
}
/**
* Returns the URI the conduit connection within the workflow uses.
*
* @return string
* @task conduit
*/
final public function getConduitURI() {
return $this->conduitURI;
}
/**
* Open a conduit channel to the server which was previously configured by
* calling @{method:setConduitURI}. Arcanist will do this automatically if
* the workflow returns ##true## from @{method:requiresConduit}, or you can
* later upgrade a workflow and build a conduit by invoking it manually.
*
* You must establish a conduit before you can make conduit calls.
*
* NOTE: You must call @{method:setConduitURI} before you can call this
* method.
*
* @return this
* @task conduit
*/
final public function establishConduit() {
if ($this->conduit) {
return $this;
}
if (!$this->conduitURI) {
throw new Exception(
"You must specify a Conduit URI with setConduitURI() before you can ".
"establish a conduit.");
}
$this->conduit = new ConduitClient($this->conduitURI);
if ($this->conduitTimeout) {
$this->conduit->setTimeout($this->conduitTimeout);
}
$user = $this->getConfigFromWhateverSourceAvailiable('http.basicauth.user');
$pass = $this->getConfigFromWhateverSourceAvailiable('http.basicauth.pass');
if ($user !== null && $pass !== null) {
$this->conduit->setBasicAuthCredentials($user, $pass);
}
return $this;
}
final public function getConfigFromWhateverSourceAvailiable($key) {
if ($this->requiresWorkingCopy()) {
$working_copy = $this->getWorkingCopy();
return $working_copy->getConfigFromAnySource($key);
} else {
$global_config = self::readGlobalArcConfig();
$pval = idx($global_config, $key);
if ($pval === null) {
$system_config = self::readSystemArcConfig();
$pval = idx($system_config, $key);
}
return $pval;
}
}
/**
* Set credentials which will be used to authenticate against Conduit. These
* credentials can then be used to establish an authenticated connection to
* conduit by calling @{method:authenticateConduit}. Arcanist sets some
* defaults for all workflows regardless of whether or not they return true
* from @{method:requireAuthentication}, based on the ##~/.arcrc## and
* ##.arcconf## files if they are present. Thus, you can generally upgrade a
* workflow which does not require authentication into an authenticated
* workflow by later invoking @{method:requireAuthentication}. You should not
* normally need to call this method unless you are specifically overriding
* the defaults.
*
* NOTE: You can not call this method after calling
* @{method:authenticateConduit}.
*
* @param dict A credential dictionary, see @{method:authenticateConduit}.
* @return this
* @task conduit
*/
final public function setConduitCredentials(array $credentials) {
if ($this->isConduitAuthenticated()) {
throw new Exception(
"You may not set new credentials after authenticating conduit.");
}
$this->conduitCredentials = $credentials;
return $this;
}
/**
* Force arc to identify with a specific Conduit version during the
* protocol handshake. This is primarily useful for development (especially
* for sending diffs which bump the client Conduit version), since the client
* still actually speaks the builtin version of the protocol.
*
* Controlled by the --conduit-version flag.
*
* @param int Version the client should pretend to be.
* @return this
* @task conduit
*/
public function forceConduitVersion($version) {
$this->forcedConduitVersion = $version;
return $this;
}
/**
* Get the protocol version the client should identify with.
*
* @return int Version the client should claim to be.
* @task conduit
*/
public function getConduitVersion() {
return nonempty($this->forcedConduitVersion, 6);
}
/**
* Override the default timeout for Conduit.
*
* Controlled by the --conduit-timeout flag.
*
* @param float Timeout, in seconds.
* @return this
* @task conduit
*/
public function setConduitTimeout($timeout) {
$this->conduitTimeout = $timeout;
if ($this->conduit) {
$this->conduit->setConduitTimeout($timeout);
}
return $this;
}
/**
* Open and authenticate a conduit connection to a Phabricator server using
* provided credentials. Normally, Arcanist does this for you automatically
* when you return true from @{method:requiresAuthentication}, but you can
* also upgrade an existing workflow to one with an authenticated conduit
* by invoking this method manually.
*
* You must authenticate the conduit before you can make authenticated conduit
* calls (almost all calls require authentication).
*
* This method uses credentials provided via @{method:setConduitCredentials}
* to authenticate to the server:
*
* - ##user## (required) The username to authenticate with.
* - ##certificate## (required) The Conduit certificate to use.
* - ##description## (optional) Description of the invoking command.
*
* Successful authentication allows you to call @{method:getUserPHID} and
* @{method:getUserName}, as well as use the client you access with
* @{method:getConduit} to make authenticated calls.
*
* NOTE: You must call @{method:setConduitURI} and
* @{method:setConduitCredentials} before you invoke this method.
*
* @return this
* @task conduit
*/
final public function authenticateConduit() {
if ($this->isConduitAuthenticated()) {
return $this;
}
$this->establishConduit();
$credentials = $this->conduitCredentials;
try {
if (!$credentials) {
throw new Exception(
"Set conduit credentials with setConduitCredentials() before ".
"authenticating conduit!");
}
if (empty($credentials['user'])) {
throw new ConduitClientException('ERR-INVALID-USER',
'Empty user in credentials.');
}
if (empty($credentials['certificate'])) {
throw new ConduitClientException('ERR-NO-CERTIFICATE',
'Empty certificate in credentials.');
}
$description = idx($credentials, 'description', '');
$user = $credentials['user'];
$certificate = $credentials['certificate'];
$connection = $this->getConduit()->callMethodSynchronous(
'conduit.connect',
array(
'client' => 'arc',
'clientVersion' => $this->getConduitVersion(),
'clientDescription' => php_uname('n').':'.$description,
'user' => $user,
'certificate' => $certificate,
'host' => $this->conduitURI,
));
} catch (ConduitClientException $ex) {
if ($ex->getErrorCode() == 'ERR-NO-CERTIFICATE' ||
$ex->getErrorCode() == 'ERR-INVALID-USER') {
$conduit_uri = $this->conduitURI;
$message =
"\n".
phutil_console_format(
"YOU NEED TO __INSTALL A CERTIFICATE__ TO LOGIN TO PHABRICATOR").
"\n\n".
phutil_console_format(
" To do this, run: **arc install-certificate**").
"\n\n".
"The server '{$conduit_uri}' rejected your request:".
"\n".
$ex->getMessage();
throw new ArcanistUsageException($message);
} else if ($ex->getErrorCode() == 'NEW-ARC-VERSION') {
// Cleverly disguise this as being AWESOME!!!
echo phutil_console_format("**New Version Available!**\n\n");
echo phutil_console_wrap($ex->getMessage());
echo "\n\n";
echo "In most cases, arc can be upgraded automatically.\n";
$ok = phutil_console_confirm(
"Upgrade arc now?",
$default_no = false);
if (!$ok) {
throw $ex;
}
$root = dirname(phutil_get_library_root('arcanist'));
chdir($root);
$err = phutil_passthru('%s upgrade', $root.'/bin/arc');
if (!$err) {
echo "\nTry running your arc command again.\n";
}
exit(1);
} else {
throw $ex;
}
}
$this->userName = $user;
$this->userPHID = $connection['userPHID'];
$this->conduitAuthenticated = true;
return $this;
}
/**
* @return bool True if conduit is authenticated, false otherwise.
* @task conduit
*/
final protected function isConduitAuthenticated() {
return (bool)$this->conduitAuthenticated;
}
/**
* Override this to return true if your workflow requires a conduit channel.
* Arc will build the channel for you before your workflow executes. This
* implies that you only need an unauthenticated channel; if you need
* authentication, override @{method:requiresAuthentication}.
*
* @return bool True if arc should build a conduit channel before running
* the workflow.
* @task conduit
*/
public function requiresConduit() {
return false;
}
/**
* Override this to return true if your workflow requires an authenticated
* conduit channel. This implies that it requires a conduit. Arc will build
* and authenticate the channel for you before the workflow executes.
*
* @return bool True if arc should build an authenticated conduit channel
* before running the workflow.
* @task conduit
*/
public function requiresAuthentication() {
return false;
}
/**
* Returns the PHID for the user once they've authenticated via Conduit.
*
* @return phid Authenticated user PHID.
* @task conduit
*/
final public function getUserPHID() {
if (!$this->userPHID) {
$workflow = get_class($this);
throw new Exception(
"This workflow ('{$workflow}') requires authentication, override ".
"requiresAuthentication() to return true.");
}
return $this->userPHID;
}
/**
* Deprecated. See @{method:getUserPHID}.
*
* @deprecated
*/
final public function getUserGUID() {
phutil_deprecated(
'ArcanistBaseWorkflow::getUserGUID',
'This method has been renamed to getUserPHID().');
return $this->getUserPHID();
}
/**
* Return the username for the user once they've authenticated via Conduit.
*
* @return string Authenticated username.
* @task conduit
*/
final public function getUserName() {
return $this->userName;
}
/**
* Get the established @{class@libphutil:ConduitClient} in order to make
* Conduit method calls. Before the client is available it must be connected,
* either implicitly by making @{method:requireConduit} or
* @{method:requireAuthentication} return true, or explicitly by calling
* @{method:establishConduit} or @{method:authenticateConduit}.
*
* @return @{class@libphutil:ConduitClient} Live conduit client.
* @task conduit
*/
final public function getConduit() {
if (!$this->conduit) {
$workflow = get_class($this);
throw new Exception(
"This workflow ('{$workflow}') requires a Conduit, override ".
"requiresConduit() to return true.");
}
return $this->conduit;
}
public function setArcanistConfiguration(
ArcanistConfiguration $arcanist_configuration) {
$this->arcanistConfiguration = $arcanist_configuration;
return $this;
}
public function getArcanistConfiguration() {
return $this->arcanistConfiguration;
}
public function requiresWorkingCopy() {
return false;
}
public function desiresWorkingCopy() {
return false;
}
public function requiresRepositoryAPI() {
return false;
}
public function desiresRepositoryAPI() {
return false;
}
public function setCommand($command) {
$this->command = $command;
return $this;
}
public function getCommand() {
return $this->command;
}
public function getArguments() {
return array();
}
public function setWorkingDirectory($working_directory) {
$this->workingDirectory = $working_directory;
return $this;
}
public function getWorkingDirectory() {
return $this->workingDirectory;
}
private function setParentWorkflow($parent_workflow) {
$this->parentWorkflow = $parent_workflow;
return $this;
}
protected function getParentWorkflow() {
return $this->parentWorkflow;
}
public function buildChildWorkflow($command, array $argv) {
$arc_config = $this->getArcanistConfiguration();
$workflow = $arc_config->buildWorkflow($command);
$workflow->setParentWorkflow($this);
$workflow->setCommand($command);
if ($this->repositoryAPI) {
$workflow->setRepositoryAPI($this->repositoryAPI);
}
if ($this->userPHID) {
$workflow->userPHID = $this->getUserPHID();
$workflow->userName = $this->getUserName();
}
if ($this->conduit) {
$workflow->conduit = $this->conduit;
}
if ($this->workingCopy) {
$workflow->setWorkingCopy($this->workingCopy);
}
$workflow->setArcanistConfiguration($arc_config);
$workflow->parseArguments(array_values($argv));
return $workflow;
}
public function getArgument($key, $default = null) {
return idx($this->arguments, $key, $default);
}
public function getPassedArguments() {
return $this->passedArguments;
}
final public function getCompleteArgumentSpecification() {
$spec = $this->getArguments();
$arc_config = $this->getArcanistConfiguration();
$command = $this->getCommand();
$spec += $arc_config->getCustomArgumentsForCommand($command);
return $spec;
}
public function parseArguments(array $args) {
$this->passedArguments = $args;
$spec = $this->getCompleteArgumentSpecification();
$dict = array();
$more_key = null;
if (!empty($spec['*'])) {
$more_key = $spec['*'];
unset($spec['*']);
$dict[$more_key] = array();
}
$short_to_long_map = array();
foreach ($spec as $long => $options) {
if (!empty($options['short'])) {
$short_to_long_map[$options['short']] = $long;
}
}
foreach ($spec as $long => $options) {
if (!empty($options['repeat'])) {
$dict[$long] = array();
}
}
$more = array();
for ($ii = 0; $ii < count($args); $ii++) {
$arg = $args[$ii];
$arg_name = null;
$arg_key = null;
if ($arg == '--') {
$more = array_merge(
$more,
array_slice($args, $ii + 1));
break;
} else if (!strncmp($arg, '--', 2)) {
$arg_key = substr($arg, 2);
if (!array_key_exists($arg_key, $spec)) {
throw new ArcanistUsageException(
"Unknown argument '{$arg_key}'. Try 'arc help'.");
}
} else if (!strncmp($arg, '-', 1)) {
$arg_key = substr($arg, 1);
if (empty($short_to_long_map[$arg_key])) {
throw new ArcanistUsageException(
"Unknown argument '{$arg_key}'. Try 'arc help'.");
}
$arg_key = $short_to_long_map[$arg_key];
} else {
$more[] = $arg;
continue;
}
$options = $spec[$arg_key];
if (empty($options['param'])) {
$dict[$arg_key] = true;
} else {
if ($ii == count($args) - 1) {
throw new ArcanistUsageException(
"Option '{$arg}' requires a parameter.");
}
if (!empty($options['repeat'])) {
$dict[$arg_key][] = $args[$ii + 1];
} else {
$dict[$arg_key] = $args[$ii + 1];
}
$ii++;
}
}
if ($more) {
if ($more_key) {
$dict[$more_key] = $more;
} else {
$example = reset($more);
throw new ArcanistUsageException(
"Unrecognized argument '{$example}'. Try 'arc help'.");
}
}
foreach ($dict as $key => $value) {
if (empty($spec[$key]['conflicts'])) {
continue;
}
foreach ($spec[$key]['conflicts'] as $conflict => $more) {
if (isset($dict[$conflict])) {
if ($more) {
$more = ': '.$more;
} else {
$more = '.';
}
// TODO: We'll always display these as long-form, when the user might
// have typed them as short form.
throw new ArcanistUsageException(
"Arguments '--{$key}' and '--{$conflict}' are mutually exclusive".
$more);
}
}
}
$this->arguments = $dict;
$this->didParseArguments();
return $this;
}
protected function didParseArguments() {
// Override this to customize workflow argument behavior.
}
public function getWorkingCopy() {
if (!$this->workingCopy) {
$workflow = get_class($this);
throw new Exception(
"This workflow ('{$workflow}') requires a working copy, override ".
"requiresWorkingCopy() to return true.");
}
return $this->workingCopy;
}
public function setWorkingCopy(
ArcanistWorkingCopyIdentity $working_copy) {
$this->workingCopy = $working_copy;
return $this;
}
public function setRepositoryAPI($api) {
$this->repositoryAPI = $api;
return $this;
}
public function getRepositoryAPI() {
if (!$this->repositoryAPI) {
$workflow = get_class($this);
throw new Exception(
"This workflow ('{$workflow}') requires a Repository API, override ".
"requiresRepositoryAPI() to return true.");
}
return $this->repositoryAPI;
}
protected function shouldRequireCleanUntrackedFiles() {
return empty($this->arguments['allow-untracked']);
}
public function setCommitMode($mode) {
$this->commitMode = $mode;
return $this;
}
public function requireCleanWorkingCopy() {
$api = $this->getRepositoryAPI();
$must_commit = array();
$working_copy_desc = phutil_console_format(
" Working copy: __%s__\n\n",
$api->getPath());
$untracked = $api->getUntrackedChanges();
if ($this->shouldRequireCleanUntrackedFiles()) {
if (!empty($untracked)) {
echo "You have untracked files in this working copy.\n\n".
$working_copy_desc.
" Untracked files in working copy:\n".
" ".implode("\n ", $untracked)."\n\n";
if ($api instanceof ArcanistGitAPI) {
echo phutil_console_wrap(
"Since you don't have '.gitignore' rules for these files and have ".
"not listed them in '.git/info/exclude', you may have forgotten ".
"to 'git add' them to your commit.");
} else if ($api instanceof ArcanistSubversionAPI) {
echo phutil_console_wrap(
"Since you don't have 'svn:ignore' rules for these files, you may ".
"have forgotten to 'svn add' them.");
} else if ($api instanceof ArcanistMercurialAPI) {
echo phutil_console_wrap(
"Since you don't have '.hgignore' rules for these files, you ".
"may have forgotten to 'hg add' them to your commit.");
}
if ($this->askForAdd()) {
$api->addToCommit($untracked);
$must_commit += array_flip($untracked);
} else if ($this->commitMode == self::COMMIT_DISABLE) {
$prompt = "Do you want to continue without adding these files?";
if (!phutil_console_confirm($prompt, $default_no = false)) {
throw new ArcanistUserAbortException();
}
}
}
}
$incomplete = $api->getIncompleteChanges();
if ($incomplete) {
throw new ArcanistUsageException(
"You have incompletely checked out directories in this working copy. ".
"Fix them before proceeding.\n\n".
$working_copy_desc.
" Incomplete directories in working copy:\n".
" ".implode("\n ", $incomplete)."\n\n".
"You can fix these paths by running 'svn update' on them.");
}
$conflicts = $api->getMergeConflicts();
if ($conflicts) {
throw new ArcanistUsageException(
"You have merge conflicts in this working copy. Resolve merge ".
"conflicts before proceeding.\n\n".
$working_copy_desc.
" Conflicts in working copy:\n".
" ".implode("\n ", $conflicts)."\n");
}
$unstaged = $api->getUnstagedChanges();
if ($unstaged) {
echo "You have unstaged changes in this working copy.\n\n".
$working_copy_desc.
" Unstaged changes in working copy:\n".
" ".implode("\n ", $unstaged);
if ($this->askForAdd()) {
$api->addToCommit($unstaged);
$must_commit += array_flip($unstaged);
} else {
throw new ArcanistUsageException(
"Stage and commit (or revert) them before proceeding.");
}
}
$uncommitted = $api->getUncommittedChanges();
foreach ($uncommitted as $key => $path) {
if (array_key_exists($path, $must_commit)) {
unset($uncommitted[$key]);
}
}
if ($uncommitted) {
echo "You have uncommitted changes in this working copy.\n\n".
$working_copy_desc.
" Uncommitted changes in working copy:\n".
" ".implode("\n ", $uncommitted);
if ($this->askForAdd()) {
$must_commit += array_flip($uncommitted);
} else {
throw new ArcanistUncommittedChangesException(
"Commit (or revert) them before proceeding.");
}
}
if ($must_commit) {
if ($this->shouldAmend) {
$commit = head($api->getLocalCommitInformation());
$api->amendCommit($commit['message']);
} else if ($api->supportsLocalCommits()) {
$api->doCommit(self::AUTO_COMMIT_TITLE);
}
}
}
private function shouldAmend() {
$api = $this->getRepositoryAPI();
if ($this->isHistoryImmutable() || !$api->supportsAmend()) {
return false;
}
$commits = $api->getLocalCommitInformation();
if (!$commits) {
return false;
}
$commit = reset($commits);
$message = ArcanistDifferentialCommitMessage::newFromRawCorpus(
$commit['message']);
if ($message->getGitSVNBaseRevision()) {
return false;
}
if ($api->getAuthor() != $commit['author']) {
return false;
}
// TODO: Check commits since tracking branch. If empty then return false.
$repository = $this->loadProjectRepository();
if ($repository) {
$callsign = $repository['callsign'];
$known_commits = $this->getConduit()->callMethodSynchronous(
'diffusion.getcommits',
array('commits' => array('r'.$callsign.$commit['commit'])));
if (ifilter($known_commits, 'error', $negate = true)) {
return false;
}
}
if (!$message->getRevisionID()) {
return true;
}
$in_working_copy = $api->loadWorkingCopyDifferentialRevisions(
$this->getConduit(),
array(
'authors' => array($this->getUserPHID()),
'status' => 'status-open',
));
if ($in_working_copy) {
return true;
}
return false;
}
private function askForAdd() {
if ($this->commitMode == self::COMMIT_DISABLE) {
return false;
} else if ($this->commitMode == self::COMMIT_ENABLE) {
return true;
}
if ($this->shouldAmend === null) {
$this->shouldAmend = $this->shouldAmend();
}
if ($this->shouldAmend) {
$prompt = "Do you want to amend these files to the commit?";
} else {
$prompt = "Do you want to add these files to the commit?";
}
return phutil_console_confirm($prompt);
}
protected function loadDiffBundleFromConduit(
ConduitClient $conduit,
$diff_id) {
return $this->loadBundleFromConduit(
$conduit,
array(
'diff_id' => $diff_id,
));
}
protected function loadRevisionBundleFromConduit(
ConduitClient $conduit,
$revision_id) {
return $this->loadBundleFromConduit(
$conduit,
array(
'revision_id' => $revision_id,
));
}
private function loadBundleFromConduit(
ConduitClient $conduit,
$params) {
$future = $conduit->callMethod('differential.getdiff', $params);
$diff = $future->resolve();
$changes = array();
foreach ($diff['changes'] as $changedict) {
$changes[] = ArcanistDiffChange::newFromDictionary($changedict);
}
$bundle = ArcanistBundle::newFromChanges($changes);
$bundle->setConduit($conduit);
// since the conduit method has changes, assume that these fields
// could be unset
$bundle->setProjectID(idx($diff, 'projectName'));
$bundle->setBaseRevision(idx($diff, 'sourceControlBaseRevision'));
$bundle->setRevisionID(idx($diff, 'revisionID'));
- $bundle->setAuthor(idx($diff, 'author'));
+ $bundle->setAuthorName(idx($diff, 'authorName'));
+ $bundle->setAuthorEmail(idx($diff, 'authorEmail'));
return $bundle;
}
/**
* Return a list of lines changed by the current diff, or ##null## if the
* change list is meaningless (for example, because the path is a directory
* or binary file).
*
* @param string Path within the repository.
* @param string Change selection mode (see ArcanistDiffHunk).
* @return list|null List of changed line numbers, or null to indicate that
* the path is not a line-oriented text file.
*/
protected function getChangedLines($path, $mode) {
$repository_api = $this->getRepositoryAPI();
$full_path = $repository_api->getPath($path);
if (is_dir($full_path)) {
return null;
}
if (!file_exists($full_path)) {
return null;
}
$change = $this->getChange($path);
if ($change->getFileType() !== ArcanistDiffChangeType::FILE_TEXT) {
return null;
}
$lines = $change->getChangedLines($mode);
return array_keys($lines);
}
protected function getChange($path) {
$repository_api = $this->getRepositoryAPI();
// TODO: Very gross
$is_git = ($repository_api instanceof ArcanistGitAPI);
$is_hg = ($repository_api instanceof ArcanistMercurialAPI);
$is_svn = ($repository_api instanceof ArcanistSubversionAPI);
if ($is_svn) {
// NOTE: In SVN, we don't currently support a "get all local changes"
// operation, so special case it.
if (empty($this->changeCache[$path])) {
$diff = $repository_api->getRawDiffText($path);
$parser = new ArcanistDiffParser();
$changes = $parser->parseDiff($diff);
if (count($changes) != 1) {
throw new Exception("Expected exactly one change.");
}
$this->changeCache[$path] = reset($changes);
}
} else if ($is_git || $is_hg) {
if (empty($this->changeCache)) {
$changes = $repository_api->getAllLocalChanges();
foreach ($changes as $change) {
$this->changeCache[$change->getCurrentPath()] = $change;
}
}
} else {
throw new Exception("Missing VCS support.");
}
if (empty($this->changeCache[$path])) {
if ($is_git) {
// This can legitimately occur under git if you make a change, "git
// commit" it, and then revert the change in the working copy and run
// "arc lint".
$change = new ArcanistDiffChange();
$change->setCurrentPath($path);
return $change;
} else {
throw new Exception(
"Trying to get change for unchanged path '{$path}'!");
}
}
return $this->changeCache[$path];
}
final public function willRunWorkflow() {
$spec = $this->getCompleteArgumentSpecification();
foreach ($this->arguments as $arg => $value) {
if (empty($spec[$arg])) {
continue;
}
$options = $spec[$arg];
if (!empty($options['supports'])) {
$system_name = $this->getRepositoryAPI()->getSourceControlSystemName();
if (!in_array($system_name, $options['supports'])) {
$extended_info = null;
if (!empty($options['nosupport'][$system_name])) {
$extended_info = ' '.$options['nosupport'][$system_name];
}
throw new ArcanistUsageException(
"Option '--{$arg}' is not supported under {$system_name}.".
$extended_info);
}
}
}
}
protected function normalizeRevisionID($revision_id) {
return preg_replace('/^D/i', '', $revision_id);
}
protected function shouldShellComplete() {
return true;
}
protected function getShellCompletions(array $argv) {
return array();
}
protected function getSupportedRevisionControlSystems() {
return array('any');
}
protected function getPassthruArgumentsAsMap($command) {
$map = array();
foreach ($this->getCompleteArgumentSpecification() as $key => $spec) {
if (!empty($spec['passthru'][$command])) {
if (isset($this->arguments[$key])) {
$map[$key] = $this->arguments[$key];
}
}
}
return $map;
}
protected function getPassthruArgumentsAsArgv($command) {
$spec = $this->getCompleteArgumentSpecification();
$map = $this->getPassthruArgumentsAsMap($command);
$argv = array();
foreach ($map as $key => $value) {
$argv[] = '--'.$key;
if (!empty($spec[$key]['param'])) {
$argv[] = $value;
}
}
return $argv;
}
public static function getSystemArcConfigLocation() {
if (phutil_is_windows()) {
// this is a horrible place to put this, but there doesn't seem to be a
// non-horrible place on Windows
return Filesystem::resolvePath(
'Phabricator/Arcanist/config',
getenv('PROGRAMFILES'));
} else {
return '/etc/arcconfig';
}
}
public static function readSystemArcConfig() {
$system_config = array();
$system_config_path = self::getSystemArcConfigLocation();
if (Filesystem::pathExists($system_config_path)) {
$file = Filesystem::readFile($system_config_path);
if ($file) {
$system_config = json_decode($file, true);
}
}
return $system_config;
}
public static function getUserConfigurationFileLocation() {
if (phutil_is_windows()) {
return getenv('APPDATA').'/.arcrc';
} else {
return getenv('HOME').'/.arcrc';
}
}
public static function readUserConfigurationFile() {
$user_config = array();
$user_config_path = self::getUserConfigurationFileLocation();
if (Filesystem::pathExists($user_config_path)) {
if (!phutil_is_windows()) {
$mode = fileperms($user_config_path);
if (!$mode) {
throw new Exception("Unable to get perms of '{$user_config_path}'!");
}
if ($mode & 0177) {
// Mode should allow only owner access.
$prompt = "File permissions on your ~/.arcrc are too open. ".
"Fix them by chmod'ing to 600?";
if (!phutil_console_confirm($prompt, $default_no = false)) {
throw new ArcanistUsageException("Set ~/.arcrc to file mode 600.");
}
execx('chmod 600 %s', $user_config_path);
// Drop the stat cache so we don't read the old permissions if
// we end up here again. If we don't do this, we may prompt the user
// to fix permissions multiple times.
clearstatcache();
}
}
$user_config_data = Filesystem::readFile($user_config_path);
$user_config = json_decode($user_config_data, true);
if (!is_array($user_config)) {
throw new ArcanistUsageException(
"Your '~/.arcrc' file is not a valid JSON file.");
}
}
return $user_config;
}
public static function writeUserConfigurationFile($config) {
$json_encoder = new PhutilJSON();
$json = $json_encoder->encodeFormatted($config);
$path = self::getUserConfigurationFileLocation();
Filesystem::writeFile($path, $json);
if (!phutil_is_windows()) {
execx('chmod 600 %s', $path);
}
}
public static function readGlobalArcConfig() {
return idx(self::readUserConfigurationFile(), 'config', array());
}
public static function writeGlobalArcConfig(array $options) {
$config = self::readUserConfigurationFile();
$config['config'] = $options;
self::writeUserConfigurationFile($config);
}
public function readLocalArcConfig() {
$local = array();
$file = $this->readScratchFile('config');
if ($file) {
$local = json_decode($file, true);
}
return $local;
}
public function writeLocalArcConfig(array $config) {
$json_encoder = new PhutilJSON();
$json = $json_encoder->encodeFormatted($config);
$this->writeScratchFile('config', $json);
return $this;
}
/**
* Write a message to stderr so that '--json' flags or stdout which is meant
* to be piped somewhere aren't disrupted.
*
* @param string Message to write to stderr.
* @return void
*/
protected function writeStatusMessage($msg) {
fwrite(STDERR, $msg);
}
protected function isHistoryImmutable() {
$repository_api = $this->getRepositoryAPI();
$working_copy = $this->getWorkingCopy();
$config = $working_copy->getConfigFromAnySource('history.immutable');
if ($config !== null) {
return $config;
}
return $repository_api->isHistoryDefaultImmutable();
}
/**
* Workflows like 'lint' and 'unit' operate on a list of working copy paths.
* The user can either specify the paths explicitly ("a.js b.php"), or by
* specfifying a revision ("--rev a3f10f1f") to select all paths modified
* since that revision, or by omitting both and letting arc choose the
* default relative revision.
*
* This method takes the user's selections and returns the paths that the
* workflow should act upon.
*
* @param list List of explicitly provided paths.
* @param string|null Revision name, if provided.
* @param mask Mask of ArcanistRepositoryAPI flags to exclude.
* Defaults to ArcanistRepositoryAPI::FLAG_UNTRACKED.
* @return list List of paths the workflow should act on.
*/
protected function selectPathsForWorkflow(
array $paths,
$rev,
$omit_mask = null) {
if ($omit_mask === null) {
$omit_mask = ArcanistRepositoryAPI::FLAG_UNTRACKED;
}
if ($paths) {
$working_copy = $this->getWorkingCopy();
foreach ($paths as $key => $path) {
$full_path = Filesystem::resolvePath($path);
if (!Filesystem::pathExists($full_path)) {
throw new ArcanistUsageException("Path '{$path}' does not exist!");
}
$relative_path = Filesystem::readablePath(
$full_path,
$working_copy->getProjectRoot());
$paths[$key] = $relative_path;
}
} else {
$repository_api = $this->getRepositoryAPI();
if ($rev) {
$this->parseBaseCommitArgument(array($rev));
}
$paths = $repository_api->getWorkingCopyStatus();
foreach ($paths as $path => $flags) {
if ($flags & $omit_mask) {
unset($paths[$path]);
}
}
$paths = array_keys($paths);
}
return array_values($paths);
}
protected function renderRevisionList(array $revisions) {
$list = array();
foreach ($revisions as $revision) {
$list[] = ' - D'.$revision['id'].': '.$revision['title']."\n";
}
return implode('', $list);
}
/* -( Scratch Files )------------------------------------------------------ */
/**
* Try to read a scratch file, if it exists and is readable.
*
* @param string Scratch file name.
* @return mixed String for file contents, or false for failure.
* @task scratch
*/
protected function readScratchFile($path) {
if (!$this->repositoryAPI) {
return false;
}
return $this->getRepositoryAPI()->readScratchFile($path);
}
/**
* Try to read a scratch JSON file, if it exists and is readable.
*
* @param string Scratch file name.
* @return array Empty array for failure.
* @task scratch
*/
protected function readScratchJSONFile($path) {
$file = $this->readScratchFile($path);
if (!$file) {
return array();
}
return json_decode($file, true);
}
/**
* Try to write a scratch file, if there's somewhere to put it and we can
* write there.
*
* @param string Scratch file name to write.
* @param string Data to write.
* @return bool True on success, false on failure.
* @task scratch
*/
protected function writeScratchFile($path, $data) {
if (!$this->repositoryAPI) {
return false;
}
return $this->getRepositoryAPI()->writeScratchFile($path, $data);
}
/**
* Try to write a scratch JSON file, if there's somewhere to put it and we can
* write there.
*
* @param string Scratch file name to write.
* @param array Data to write.
* @return bool True on success, false on failure.
* @task scratch
*/
protected function writeScratchJSONFile($path, array $data) {
return $this->writeScratchFile($path, json_encode($data));
}
/**
* Try to remove a scratch file.
*
* @param string Scratch file name to remove.
* @return bool True if the file was removed successfully.
* @task scratch
*/
protected function removeScratchFile($path) {
if (!$this->repositoryAPI) {
return false;
}
return $this->getRepositoryAPI()->removeScratchFile($path);
}
/**
* Get a human-readable description of the scratch file location.
*
* @param string Scratch file name.
* @return mixed String, or false on failure.
* @task scratch
*/
protected function getReadableScratchFilePath($path) {
if (!$this->repositoryAPI) {
return false;
}
return $this->getRepositoryAPI()->getReadableScratchFilePath($path);
}
/**
* Get the path to a scratch file, if possible.
*
* @param string Scratch file name.
* @return mixed File path, or false on failure.
* @task scratch
*/
protected function getScratchFilePath($path) {
if (!$this->repositoryAPI) {
return false;
}
return $this->getRepositoryAPI()->getScratchFilePath($path);
}
protected function getRepositoryEncoding() {
$default = 'UTF-8';
return nonempty(idx($this->getProjectInfo(), 'encoding'), $default);
}
protected function getProjectInfo() {
if ($this->projectInfo === null) {
$project_id = $this->getWorkingCopy()->getProjectID();
if (!$project_id) {
$this->projectInfo = array();
} else {
try {
$this->projectInfo = $this->getConduit()->callMethodSynchronous(
'arcanist.projectinfo',
array(
'name' => $project_id,
));
} catch (ConduitClientException $ex) {
if ($ex->getErrorCode() != 'ERR-BAD-ARCANIST-PROJECT') {
throw $ex;
}
// TODO: Implement a proper query method that doesn't throw on
// project not found. We just swallow this because some pathways,
// like Git with uncommitted changes in a repository with a new
// project ID, may attempt to access project information before
// the project is created. See T2153.
return array();
}
}
}
return $this->projectInfo;
}
protected function loadProjectRepository() {
$project = $this->getProjectInfo();
if (isset($project['repository'])) {
return $project['repository'];
}
// NOTE: The rest of the code is here for backwards compatibility.
$repository_phid = idx($project, 'repositoryPHID');
if (!$repository_phid) {
return array();
}
$repositories = $this->getConduit()->callMethodSynchronous(
'repository.query',
array());
$repositories = ipull($repositories, null, 'phid');
return idx($repositories, $repository_phid, array());
}
protected function newInteractiveEditor($text) {
$editor = new PhutilInteractiveEditor($text);
$preferred = $this->getWorkingCopy()->getConfigFromAnySource('editor');
if ($preferred) {
$editor->setPreferredEditor($preferred);
}
return $editor;
}
protected function newDiffParser() {
$parser = new ArcanistDiffParser();
if ($this->repositoryAPI) {
$parser->setRepositoryAPI($this->getRepositoryAPI());
}
$parser->setWriteDiffOnFailure(true);
return $parser;
}
protected function resolveCall(ConduitFuture $method, $timeout = null) {
try {
return $method->resolve($timeout);
} catch (ConduitClientException $ex) {
if ($ex->getErrorCode() == 'ERR-CONDUIT-CALL') {
echo phutil_console_wrap(
"This feature requires a newer version of Phabricator. Please ".
"update it using these instructions: ".
"http://www.phabricator.com/docs/phabricator/article/".
"Installation_Guide.html#updating-phabricator\n\n");
}
throw $ex;
}
}
protected function dispatchEvent($type, array $data) {
$data += array(
'workflow' => $this,
);
$event = new PhutilEvent($type, $data);
PhutilEventEngine::dispatchEvent($event);
return $event;
}
public function parseBaseCommitArgument(array $argv) {
if (!count($argv)) {
return;
}
$api = $this->getRepositoryAPI();
if (!$api->supportsCommitRanges()) {
throw new ArcanistUsageException(
"This version control system does not support commit ranges.");
}
if (count($argv) > 1) {
throw new ArcanistUsageException(
"Specify exactly one base commit. The end of the commit range is ".
"always the working copy state.");
}
$api->setBaseCommit(head($argv));
return $this;
}
protected function getRepositoryVersion() {
if (!$this->repositoryVersion) {
$api = $this->getRepositoryAPI();
$commit = $api->getSourceControlBaseRevision();
$versions = array('' => $commit);
foreach ($api->getChangedFiles($commit) as $path => $mask) {
$versions[$path] = (Filesystem::pathExists($path)
? md5_file($path)
: '');
}
$this->repositoryVersion = md5(json_encode($versions));
}
return $this->repositoryVersion;
}
}
diff --git a/src/workflow/ArcanistPatchWorkflow.php b/src/workflow/ArcanistPatchWorkflow.php
index 2d9c44fd..e9fa45a9 100644
--- a/src/workflow/ArcanistPatchWorkflow.php
+++ b/src/workflow/ArcanistPatchWorkflow.php
@@ -1,1002 +1,996 @@
<?php
/**
* Applies changes from Differential or a file to the working copy.
*
* @group workflow
*/
final class ArcanistPatchWorkflow extends ArcanistBaseWorkflow {
const SOURCE_BUNDLE = 'bundle';
const SOURCE_PATCH = 'patch';
const SOURCE_REVISION = 'revision';
const SOURCE_DIFF = 'diff';
private $source;
private $sourceParam;
public function getWorkflowName() {
return 'patch';
}
public function getCommandSynopses() {
return phutil_console_format(<<<EOTEXT
**patch** __D12345__
**patch** __--revision__ __revision_id__
**patch** __--diff__ __diff_id__
**patch** __--patch__ __file__
**patch** __--arcbundle__ __bundlefile__
EOTEXT
);
}
public function getCommandHelp() {
return phutil_console_format(<<<EOTEXT
Supports: git, svn, hg
Apply the changes in a Differential revision, patchfile, or arc
bundle to the working copy.
EOTEXT
);
}
public function getArguments() {
return array(
'revision' => array(
'param' => 'revision_id',
'paramtype' => 'complete',
'help' =>
"Apply changes from a Differential revision, using the most recent ".
"diff that has been attached to it. You can run 'arc patch D12345' ".
"as a shorthand.",
),
'diff' => array(
'param' => 'diff_id',
'help' =>
"Apply changes from a Differential diff. Normally you want to use ".
"--revision to get the most recent changes, but you can ".
"specifically apply an out-of-date diff or a diff which was never ".
"attached to a revision by using this flag.",
),
'arcbundle' => array(
'param' => 'bundlefile',
'paramtype' => 'file',
'help' =>
"Apply changes from an arc bundle generated with 'arc export'.",
),
'patch' => array(
'param' => 'patchfile',
'paramtype' => 'file',
'help' =>
"Apply changes from a git patchfile or unified patchfile.",
),
'encoding' => array(
'param' => 'encoding',
'help' =>
"Attempt to convert non UTF-8 patch into specified encoding.",
),
'update' => array(
'supports' => array(
'git', 'svn', 'hg'
),
'help' =>
"Update the local working copy before applying the patch.",
'conflicts' => array(
'nobranch' => true,
'bookmark' => true,
),
),
'nocommit' => array(
'supports' => array(
'git', 'hg'
),
'help' =>
"Normally under git/hg, 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 new branch. This flag ".
"cherry-picks the resultant commit onto the original branch and ".
"deletes the temporary branch.",
'conflicts' => array(
'update' => true,
),
),
'bookmark' => array(
'supports' => array(
'hg'
),
'help' =>
"Normally under hg, a new bookmark is not created and the patch ".
"is applied and committed in the current bookmark. With this flag, ".
"a new bookmark is created and the patch is applied and committed ".
"in the new bookmark.",
'conflicts' => array(
'update' => true,
),
),
'force' => array(
'help' =>
"Do not run any sanity checks.",
),
'*' => 'name',
);
}
protected function didParseArguments() {
$source = null;
$requested = 0;
if ($this->getArgument('revision')) {
$source = self::SOURCE_REVISION;
$requested++;
}
if ($this->getArgument('diff')) {
$source = self::SOURCE_DIFF;
$requested++;
}
if ($this->getArgument('arcbundle')) {
$source = self::SOURCE_BUNDLE;
$requested++;
}
if ($this->getArgument('patch')) {
$source = self::SOURCE_PATCH;
$requested++;
}
$use_revision_id = null;
if ($this->getArgument('name')) {
$namev = $this->getArgument('name');
if (count($namev) > 1) {
throw new ArcanistUsageException("Specify at most one revision name.");
}
$source = self::SOURCE_REVISION;
$requested++;
$use_revision_id = $this->normalizeRevisionID(head($namev));
}
if ($requested === 0) {
throw new ArcanistUsageException(
"Specify one of 'D12345', '--revision <revision_id>' (to select the ".
"current changes attached to a Differential revision), ".
"'--diff <diff_id>' (to select a specific, out-of-date diff or a ".
"diff which is not attached to a revision), '--arcbundle <file>' ".
"or '--patch <file>' to choose a patch source.");
} else if ($requested > 1) {
throw new ArcanistUsageException(
"Options 'D12345', '--revision', '--diff', '--arcbundle' and ".
"'--patch' are not compatible. Choose exactly one patch source.");
}
$this->source = $source;
$this->sourceParam = nonempty(
$use_revision_id,
$this->getArgument($source));
}
public function requiresConduit() {
return ($this->getSource() != self::SOURCE_PATCH);
}
public function requiresRepositoryAPI() {
return true;
}
public function requiresWorkingCopy() {
return true;
}
private function getSource() {
return $this->source;
}
private function getSourceParam() {
return $this->sourceParam;
}
private function shouldCommit() {
$no_commit = $this->getArgument('nocommit', false);
if ($no_commit) {
return false;
}
return true;
}
private function canBranch() {
// git only for now
$repository_api = $this->getRepositoryAPI();
if (!($repository_api instanceof ArcanistGitAPI)) {
return false;
}
return true;
}
private function shouldBranch() {
$no_branch = $this->getArgument('nobranch', false);
if ($no_branch) {
return false;
}
return true;
}
private function shouldBookmark() {
// specific to hg
$repository_api = $this->getRepositoryAPI();
if (!($repository_api instanceof ArcanistMercurialAPI)) {
return false;
}
$bookmark = $this->getArgument('bookmark', false);
if ($bookmark) {
return true;
}
return false;
}
private function getBranchName(ArcanistBundle $bundle) {
$branch_name = null;
$repository_api = $this->getRepositoryAPI();
$revision_id = $bundle->getRevisionID();
$base_name = "arcpatch";
if ($revision_id) {
$base_name .= "-D{$revision_id}";
}
$suffixes = array(null, '-1', '-2', '-3');
foreach ($suffixes as $suffix) {
$proposed_name = $base_name.$suffix;
list($err) = $repository_api->execManualLocal(
'rev-parse --verify %s',
$proposed_name);
// no error means git rev-parse found a branch
if (!$err) {
echo phutil_console_format(
"Branch name {$proposed_name} already 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 getBookmarkName(ArcanistBundle $bundle) {
$bookmark_name = null;
$repository_api = $this->getRepositoryAPI();
$revision_id = $bundle->getRevisionID();
$base_name = "arcpatch";
if ($revision_id) {
$base_name .= "-D{$revision_id}";
}
$suffixes = array(null, '-1', '-2', '-3');
foreach ($suffixes as $suffix) {
$proposed_name = $base_name.$suffix;
list($err) = $repository_api->execManualLocal(
'log -r %s',
$proposed_name);
// no error means hg log found a bookmark
if (!$err) {
echo phutil_console_format(
"Bookmark name {$proposed_name} already exists; trying a new name.\n"
);
continue;
} else {
$bookmark_name = $proposed_name;
break;
}
}
if (!$bookmark_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 $bookmark_name;
}
private function hasBaseRevision(ArcanistBundle $bundle) {
$base_revision = $bundle->getBaseRevision();
$repository_api = $this->getRepositoryAPI();
// verify the base revision is valid
// in a working copy that uses the git-svn bridge, the base revision might
// be a svn uri instead of a git ref
// NOTE: Use 'cat-file', not 'rev-parse --verify', because 'rev-parse'
// always "verifies" any properly-formatted commit even if it does not
// exist.
list($err) = $repository_api->execManualLocal(
'cat-file -t %s',
$base_revision);
return !$err;
}
private function createBranch(ArcanistBundle $bundle, $has_base_revision) {
$branch_name = $this->getBranchName($bundle);
$base_revision = $bundle->getBaseRevision();
$repository_api = $this->getRepositoryAPI();
if ($base_revision && $has_base_revision) {
$repository_api->execxLocal(
'checkout -b %s %s',
$branch_name,
$base_revision);
} else {
$repository_api->execxLocal(
'checkout -b %s',
$branch_name);
}
echo phutil_console_format(
"Created and checked out branch %s.\n",
$branch_name);
return $branch_name;
}
private function createBookmark(ArcanistBundle $bundle) {
$bookmark_name = $this->getBookmarkName($bundle);
$repository_api = $this->getRepositoryAPI();
$repository_api->execxLocal(
'bookmark %s',
$bookmark_name);
echo phutil_console_format(
"Created and applied bookmark %s.\n",
$bookmark_name);
}
private function shouldUpdateWorkingCopy() {
return $this->getArgument('update', false);
}
private function updateWorkingCopy() {
echo "Updating working copy...\n";
$this->getRepositoryAPI()->updateWorkingCopy();
echo "Done.\n";
}
public function run() {
$source = $this->getSource();
$param = $this->getSourceParam();
try {
switch ($source) {
case self::SOURCE_PATCH:
if ($param == '-') {
$patch = @file_get_contents('php://stdin');
if (!strlen($patch)) {
throw new ArcanistUsageException(
"Failed to read patch from stdin!");
}
} else {
$patch = Filesystem::readFile($param);
}
$bundle = ArcanistBundle::newFromDiff($patch);
break;
case self::SOURCE_BUNDLE:
$path = $this->getArgument('arcbundle');
$bundle = ArcanistBundle::newFromArcBundle($path);
break;
case self::SOURCE_REVISION:
$bundle = $this->loadRevisionBundleFromConduit(
$this->getConduit(),
$param);
break;
case self::SOURCE_DIFF:
$bundle = $this->loadDiffBundleFromConduit(
$this->getConduit(),
$param);
break;
}
} catch (ConduitClientException $ex) {
if ($ex->getErrorCode() == 'ERR-INVALID-SESSION') {
// Phabricator is not configured to allow anonymous access to
// Differential.
$this->authenticateConduit();
return $this->run();
} else {
throw $ex;
}
}
$try_encoding = nonempty($this->getArgument('encoding'), null);
if (!$try_encoding) {
if ($this->requiresConduit()) {
try {
$try_encoding = $this->getRepositoryEncoding();
} catch (ConduitClientException $e) {
$try_encoding = null;
}
}
}
if ($try_encoding) {
$bundle->setEncoding($try_encoding);
}
$force = $this->getArgument('force', false);
if ($force) {
// force means don't do any sanity checks about the patch
} else {
$this->sanityCheck($bundle);
}
// we should update the working copy before we do ANYTHING else
if ($this->shouldUpdateWorkingCopy()) {
$this->updateWorkingCopy();
}
$repository_api = $this->getRepositoryAPI();
$has_base_revision = $this->hasBaseRevision($bundle);
if ($this->shouldCommit() &&
$this->canBranch() &&
($this->shouldBranch() || $has_base_revision)) {
$original_branch = $repository_api->getBranchName();
// If we weren't on a branch, then record the ref we'll return to
// instead.
if ($original_branch === null) {
$original_branch = $repository_api->getCanonicalRevisionName('HEAD');
}
$new_branch = $this->createBranch($bundle, $has_base_revision);
}
if ($this->shouldBookmark()) {
$this->createBookmark($bundle);
}
if ($repository_api instanceof ArcanistSubversionAPI) {
$patch_err = 0;
$copies = array();
$deletes = array();
$patches = array();
$propset = array();
$adds = array();
$symlinks = array();
$changes = $bundle->getChanges();
foreach ($changes as $change) {
$type = $change->getType();
$should_patch = true;
$filetype = $change->getFileType();
switch ($filetype) {
case ArcanistDiffChangeType::FILE_SYMLINK:
$should_patch = false;
$symlinks[] = $change;
break;
}
switch ($type) {
case ArcanistDiffChangeType::TYPE_MOVE_AWAY:
case ArcanistDiffChangeType::TYPE_MULTICOPY:
case ArcanistDiffChangeType::TYPE_DELETE:
$path = $change->getCurrentPath();
$fpath = $repository_api->getPath($path);
if (!@file_exists($fpath)) {
$ok = phutil_console_confirm(
"Patch deletes file '{$path}', but the file does not exist in ".
"the working copy. Continue anyway?");
if (!$ok) {
throw new ArcanistUserAbortException();
}
} else {
$deletes[] = $change->getCurrentPath();
}
$should_patch = false;
break;
case ArcanistDiffChangeType::TYPE_COPY_HERE:
case ArcanistDiffChangeType::TYPE_MOVE_HERE:
$path = $change->getOldPath();
$fpath = $repository_api->getPath($path);
if (!@file_exists($fpath)) {
$cpath = $change->getCurrentPath();
if ($type == ArcanistDiffChangeType::TYPE_COPY_HERE) {
$verbs = 'copies';
} else {
$verbs = 'moves';
}
$ok = phutil_console_confirm(
"Patch {$verbs} '{$path}' to '{$cpath}', but source path ".
"does not exist in the working copy. Continue anyway?");
if (!$ok) {
throw new ArcanistUserAbortException();
}
} else {
$copies[] = array(
$change->getOldPath(),
$change->getCurrentPath());
}
break;
case ArcanistDiffChangeType::TYPE_ADD:
$adds[] = $change->getCurrentPath();
break;
}
if ($should_patch) {
$cbundle = ArcanistBundle::newFromChanges(array($change));
$patches[$change->getCurrentPath()] = $cbundle->toUnifiedDiff();
$prop_old = $change->getOldProperties();
$prop_new = $change->getNewProperties();
$props = $prop_old + $prop_new;
foreach ($props as $key => $ignored) {
if (idx($prop_old, $key) !== idx($prop_new, $key)) {
$propset[$change->getCurrentPath()][$key] = idx($prop_new, $key);
}
}
}
}
// Before we start doing anything, create all the directories we're going
// to add files to if they don't already exist.
foreach ($copies as $copy) {
list($src, $dst) = $copy;
$this->createParentDirectoryOf($dst);
}
foreach ($patches as $path => $patch) {
$this->createParentDirectoryOf($path);
}
foreach ($adds as $add) {
$this->createParentDirectoryOf($add);
}
// TODO: The SVN patch workflow likely does not work on windows because
// of the (cd ...) stuff.
foreach ($copies as $copy) {
list($src, $dst) = $copy;
passthru(
csprintf(
'(cd %s; svn cp %s %s)',
$repository_api->getPath(),
$src,
$dst));
}
foreach ($deletes as $delete) {
passthru(
csprintf(
'(cd %s; svn rm %s)',
$repository_api->getPath(),
$delete));
}
foreach ($symlinks as $symlink) {
$link_target = $symlink->getSymlinkTarget();
$link_path = $symlink->getCurrentPath();
switch ($symlink->getType()) {
case ArcanistDiffChangeType::TYPE_ADD:
case ArcanistDiffChangeType::TYPE_CHANGE:
case ArcanistDiffChangeType::TYPE_MOVE_HERE:
case ArcanistDiffChangeType::TYPE_COPY_HERE:
execx(
'(cd %s && ln -sf %s %s)',
$repository_api->getPath(),
$link_target,
$link_path);
break;
}
}
foreach ($patches as $path => $patch) {
$err = null;
if ($patch) {
$tmp = new TempFile();
Filesystem::writeFile($tmp, $patch);
passthru(
csprintf(
'(cd %s; patch -p0 < %s)',
$repository_api->getPath(),
$tmp),
$err);
} else {
passthru(
csprintf(
'(cd %s; touch %s)',
$repository_api->getPath(),
$path),
$err);
}
if ($err) {
$patch_err = max($patch_err, $err);
}
}
foreach ($adds as $add) {
passthru(
csprintf(
'(cd %s; svn add %s)',
$repository_api->getPath(),
$add));
}
foreach ($propset as $path => $changes) {
foreach ($changes as $prop => $value) {
if ($prop == 'unix:filemode') {
// Setting this property also changes the file mode.
$prop = 'svn:executable';
$value = (octdec($value) & 0111 ? 'on' : null);
}
if ($value === null) {
passthru(
csprintf(
'(cd %s; svn propdel %s %s)',
$repository_api->getPath(),
$prop,
$path));
} else {
passthru(
csprintf(
'(cd %s; svn propset %s %s %s)',
$repository_api->getPath(),
$prop,
$value,
$path));
}
}
}
if ($patch_err == 0) {
echo phutil_console_format(
"<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 ".
"cleanly by the unix 'patch' utility. Your working copy may be ".
"different from the revision's base, or you may be in the wrong ".
"subdirectory. You can export the raw patch file using ".
"'arc export --unified', and then try to apply it by fiddling with ".
"options to 'patch' (particularly, -p), or manually. The output ".
"above, from 'patch', may be helpful in figuring out what went ".
"wrong.\n");
}
return $patch_err;
} else if ($repository_api instanceof ArcanistGitAPI) {
$future = $repository_api->execFutureLocal(
'apply --index --reject');
$future->write($bundle->toGitPatch());
try {
$future->resolvex();
} catch (CommandException $ex) {
echo phutil_console_format(
"\n<bg:red>** Patch Failed! **</bg>\n");
$stderr = $ex->getStdErr();
if (preg_match('/already exists in working directory/', $stderr)) {
echo phutil_console_wrap(
phutil_console_format(
"\n<bg:yellow>** WARNING **</bg> This patch may have failed ".
"because it attempts to change the case of a filename (for ".
"instance, from 'example.c' to 'Example.c'). Git cannot apply ".
"patches like this on case-insensitive filesystems. You must ".
"apply this patch manually.\n"));
}
throw $ex;
}
if ($this->shouldCommit()) {
- // git is really, really finicky about the author information so we
- // make sure it is in the
- // "George Washington <gwashington@example.com> format or don't use it.
- $author_cmd = '';
- $author = $bundle->getAuthor();
- if ($author) {
- $address = new PhutilEmailAddress($author);
- if ($address->getDisplayName() && $address->getAddress()) {
- $author = sprintf('%s <%s>',
- $address->getDisplayName(),
- $address->getAddress());
- $author_cmd = csprintf('--author=%s ', $author);
- }
+ if ($bundle->getFullAuthor()) {
+ $author_cmd = csprintf('--author=%s', $bundle->getFullAuthor());
+ } else {
+ $author_cmd = '';
}
+
$commit_message = $this->getCommitMessage($bundle);
$future = $repository_api->execFutureLocal(
- 'commit -a %C-F -',
+ 'commit -a %C -F -',
$author_cmd);
$future->write($commit_message);
$future->resolvex();
$verb = 'committed';
} else {
$verb = 'applied';
}
if ($this->shouldCommit() && $this->canBranch() &&
!$this->shouldBranch() && $has_base_revision) {
$repository_api->execxLocal('checkout %s', $original_branch);
$ex = null;
try {
$repository_api->execxLocal('cherry-pick %s', $new_branch);
} catch (Exception $ex) {}
$repository_api->execxLocal('branch -D %s', $new_branch);
if ($ex) {
echo phutil_console_format(
"\n<bg:red>** Cherry Pick Failed!**</bg>\n");
throw $ex;
}
}
echo phutil_console_format(
"<bg:green>** OKAY **</bg> Successfully {$verb} patch.\n");
} else if ($repository_api instanceof ArcanistMercurialAPI) {
$future = $repository_api->execFutureLocal(
'import --no-commit -');
$future->write($bundle->toGitPatch());
try {
$future->resolvex();
} catch (CommandException $ex) {
echo phutil_console_format(
"\n<bg:red>** Patch Failed! **</bg>\n");
$stderr = $ex->getStdErr();
if (preg_match('/case-folding collision/', $stderr)) {
echo phutil_console_wrap(
phutil_console_format(
"\n<bg:yellow>** WARNING **</bg> This patch may have failed ".
"because it attempts to change the case of a filename (for ".
"instance, from 'example.c' to 'Example.c'). Mercurial cannot ".
"apply patches like this on case-insensitive filesystems. You ".
"must apply this patch manually.\n"));
}
throw $ex;
}
if ($this->shouldCommit()) {
- $author_cmd = '';
- $author = $bundle->getAuthor();
- if ($author) {
- $author_cmd = sprintf('-u %s ', $author);
+ $author = coalesce($bundle->getFullAuthor(), $bundle->getAuthorName());
+ if ($author !== null) {
+ $author_cmd = csprintf('-u %s', $author);
+ } else {
+ $author_cmd = '';
}
+
$commit_message = $this->getCommitMessage($bundle);
$future = $repository_api->execFutureLocal(
- 'commit -A %C-l -',
+ 'commit -A %C -l -',
$author_cmd);
$future->write($commit_message);
$future->resolvex();
$verb = 'committed';
} else {
$verb = 'applied';
}
echo phutil_console_format(
"<bg:green>** OKAY **</bg> Successfully {$verb} patch.\n");
} else {
throw new Exception('Unknown version control system.');
}
return 0;
}
private function getCommitMessage(ArcanistBundle $bundle) {
$revision_id = $bundle->getRevisionID();
$commit_message = null;
$prompt_message = null;
// if we have a revision id the commit message is in differential
// TODO: See T848 for the authenticated stuff.
if ($revision_id && $this->isConduitAuthenticated()) {
$conduit = $this->getConduit();
$commit_message = $conduit->callMethodSynchronous(
'differential.getcommitmessage',
array(
'revision_id' => $revision_id,
));
$prompt_message = " Note arcanist failed to load the commit message ".
"from differential for revision D{$revision_id}.";
}
// no revision id or failed to fetch commit message so get it from the
// user on the command line
if (!$commit_message) {
$template =
"\n\n".
"# Enter a commit message for this patch. If you just want to apply ".
"the patch to the working copy without committing, re-run arc patch ".
"with the --nocommit flag.".
$prompt_message.
"\n";
$commit_message = $this->newInteractiveEditor($template)
->setName('arcanist-patch-commit-message')
->editInteractively();
$commit_message = ArcanistCommentRemover::removeComments($commit_message);
if (!strlen(trim($commit_message))) {
throw new ArcanistUserAbortException();
}
}
return $commit_message;
}
public function getShellCompletions(array $argv) {
// TODO: Pull open diffs from 'arc list'?
return array('ARGUMENT');
}
/**
* Do the best we can to prevent PEBKAC and id10t issues.
*/
private function sanityCheck(ArcanistBundle $bundle) {
$repository_api = $this->getRepositoryAPI();
// Require clean working copy
$this->requireCleanWorkingCopy();
// Check to see if the bundle's project id matches the working copy
// project id
$bundle_project_id = $bundle->getProjectID();
$working_copy_project_id = $this->getWorkingCopy()->getProjectID();
if (empty($bundle_project_id)) {
// this means $source is SOURCE_PATCH || SOURCE_BUNDLE w/ $version = 0
// they don't come with a project id so just do nothing
} else if ($bundle_project_id != $working_copy_project_id) {
if ($working_copy_project_id) {
$issue =
"This patch is for the '{$bundle_project_id}' project, but the ".
"working copy belongs to the '{$working_copy_project_id}' project.";
} else {
$issue =
"This patch is for the '{$bundle_project_id}' project, but the ".
"working copy does not have an '.arcconfig' file to identify which ".
"project it belongs to.";
}
$ok = phutil_console_confirm(
"{$issue} Still try to apply the patch?",
$default_no = false
);
if (!$ok) {
throw new ArcanistUserAbortException();
}
}
// Check to see if the bundle's base revision matches the working copy
// base revision
if ($repository_api->supportsLocalCommits()) {
$bundle_base_rev = $bundle->getBaseRevision();
if (empty($bundle_base_rev)) {
// this means $source is SOURCE_PATCH || SOURCE_BUNDLE w/ $version < 2
// they don't have a base rev so just do nothing
$commit_exists = true;
} else {
$commit_exists =
$repository_api->hasLocalCommit($bundle_base_rev);
}
if (!$commit_exists) {
// we have a problem...! lots of work because we need to ask
// differential for revision information for these base revisions
// to improve our error message.
$bundle_base_rev_str = null;
$source_base_rev = $repository_api->getWorkingCopyRevision();
$source_base_rev_str = null;
if ($repository_api instanceof ArcanistGitAPI) {
$hash_type = ArcanistDifferentialRevisionHash::HASH_GIT_COMMIT;
} else if ($repository_api instanceof ArcanistMercurialAPI) {
$hash_type = ArcanistDifferentialRevisionHash::HASH_MERCURIAL_COMMIT;
} else {
$hash_type = null;
}
if ($hash_type) {
// 2 round trips because even though we could send off one query
// we wouldn't be able to tell which revisions were for which hash
$hash = array($hash_type, $bundle_base_rev);
$bundle_revision = $this->loadRevisionFromHash($hash);
$hash = array($hash_type, $source_base_rev);
$source_revision = $this->loadRevisionFromHash($hash);
if ($bundle_revision) {
$bundle_base_rev_str = $bundle_base_rev .
' \ D' . $bundle_revision['id'];
}
if ($source_revision) {
$source_base_rev_str = $source_base_rev .
' \ D' . $source_revision['id'];
}
}
$bundle_base_rev_str = nonempty($bundle_base_rev_str,
$bundle_base_rev);
$source_base_rev_str = nonempty($source_base_rev_str,
$source_base_rev);
$ok = phutil_console_confirm(
"This diff is against commit {$bundle_base_rev_str}, but the ".
"commit is nowhere in the working copy. Try to apply it against ".
"the current working copy state? ({$source_base_rev_str})",
$default_no = false
);
if (!$ok) {
throw new ArcanistUserAbortException();
}
}
}
// TODO -- more sanity checks here
}
/**
* Create parent directories one at a time, since we need to "svn add" each
* one. (Technically we could "svn add" just the topmost new directory.)
*/
private function createParentDirectoryOf($path) {
$repository_api = $this->getRepositoryAPI();
$dir = dirname($path);
if (Filesystem::pathExists($dir)) {
return;
} else {
// Make sure the parent directory exists before we make this one.
$this->createParentDirectoryOf($dir);
execx(
'(cd %s && mkdir %s)',
$repository_api->getPath(),
$dir);
passthru(
csprintf(
'(cd %s && svn add %s)',
$repository_api->getPath(),
$dir));
}
}
private function loadRevisionFromHash($hash) {
// TODO -- de-hack this as permissions become more clear with things
// like T848 (add scope to OAuth)
if (!$this->isConduitAuthenticated()) {
return null;
}
$conduit = $this->getConduit();
$revisions = $conduit->callMethodSynchronous(
'differential.query',
array(
'commitHashes' => array($hash),
)
);
// grab the latest closed revision only
$found_revision = null;
$revisions = isort($revisions, 'dateModified');
foreach ($revisions as $revision) {
if ($revision['status'] == ArcanistDifferentialRevisionStatus::CLOSED) {
$found_revision = $revision;
}
}
return $found_revision;
}
}

File Metadata

Mime Type
text/x-diff
Expires
Mon, Dec 23, 06:46 (20 h, 16 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
556842
Default Alt Text
(107 KB)

Event Timeline