1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-12-04 12:42:42 +01:00
phorge-arcanist/src/repository/api/ArcanistRepositoryAPI.php

429 lines
12 KiB
PHP
Raw Normal View History

2011-01-10 00:22:25 +01:00
<?php
/**
* Interfaces with the VCS in the working copy.
*
* @group workingcopy
*/
2011-01-10 00:22:25 +01:00
abstract class ArcanistRepositoryAPI {
const FLAG_MODIFIED = 1;
const FLAG_ADDED = 2;
const FLAG_DELETED = 4;
const FLAG_UNTRACKED = 8;
const FLAG_CONFLICT = 16;
const FLAG_MISSING = 32;
const FLAG_UNSTAGED = 64;
const FLAG_UNCOMMITTED = 128;
const FLAG_EXTERNALS = 256;
// Occurs in SVN when you replace a file with a directory without telling
// SVN about it.
2011-01-10 00:22:25 +01:00
const FLAG_OBSTRUCTED = 512;
// Occurs in SVN when an update was interrupted or failed, e.g. you ^C'd it.
const FLAG_INCOMPLETE = 1024;
2011-01-10 00:22:25 +01:00
protected $path;
protected $diffLinesOfContext = 0x7FFF;
private $baseCommitExplanation = '???';
private $workingCopyIdentity;
Add a DSL for selecting base commits Summary: New optional mode. If you set 'base' in local, project or global config or pass '--base' to 'arc diff' or 'arc which', it switches to DSL mode. In DSL mode, lists of rules from args, local, project and global config are resolved, in that order. Rules can manipulate the rule machine or resolve into actual commits. Provides support for some 'arc' rules (mostly machine manipulation) and 'git' rules (symbolic ref and merge-base). Test Plan: Ran unit tests. Also: ```$ arc which --show-base --base 'arc:prompt' Against which commit? HEAD HEAD $ arc which --show-base --base 'git:HEAD' HEAD $ arc which --show-base --base 'git:fake' Usage Exception: None of the rules in your 'base' configuration matched a valid commit. Adjust rules or specify which commit you want to use explicitly. $ arc which --show-base --base 'git:origin/master' origin/master $ arc which --show-base --base 'git:upstream' Usage Exception: None of the rules in your 'base' configuration matched a valid commit. Adjust rules or specify which commit you want to use explicitly. $ arc which --show-base --base 'literal:derp' derp $ arc which --show-base --base 'arc:halt' Usage Exception: None of the rules in your 'base' configuration matched a valid commit. Adjust rules or specify which commit you want to use explicitly. $ arc set-config --local base git:origin/master Set key 'base' = 'git:origin/master' in local config. $ arc which --show-base origin/master $ arc which --show-base --base 'git:HEAD^' HEAD^ $ arc which --show-base --base 'arc:yield, git:HEAD^' origin/master $ arc which --show-base --base 'arc:global, git:HEAD^' HEAD^ $ arc which --show-base --base 'arc:global, git:merge-base(origin/master)' 3f4f8992fba8d1f142974da36a82bae900e247c0``` Reviewers: dschleimer, vrana Reviewed By: dschleimer CC: aran Maniphest Tasks: T1233 Differential Revision: https://secure.phabricator.com/D2748
2012-06-15 23:01:28 +02:00
private $baseCommitArgumentRules;
2011-01-10 00:22:25 +01:00
abstract public function getSourceControlSystemName();
public function getDiffLinesOfContext() {
return $this->diffLinesOfContext;
}
public function setDiffLinesOfContext($lines) {
$this->diffLinesOfContext = $lines;
return $this;
}
public function getWorkingCopyIdentity() {
return $this->workingCopyIdentity;
}
2011-01-10 00:22:25 +01:00
public static function newAPIFromWorkingCopyIdentity(
ArcanistWorkingCopyIdentity $working_copy) {
$root = $working_copy->getProjectRoot();
if (!$root) {
throw new ArcanistUsageException(
"There is no readable '.arcconfig' file in the working directory or ".
"any parent directory. Create an '.arcconfig' file to configure arc.");
}
// check if we're in an svn working copy
list($err) = exec_manual('svn info');
if (!$err) {
$api = new ArcanistSubversionAPI($root);
$api->workingCopyIdentity = $working_copy;
return $api;
2011-01-10 00:22:25 +01:00
}
if (Filesystem::pathExists($root.'/.hg')) {
$api = new ArcanistMercurialAPI($root);
$api->workingCopyIdentity = $working_copy;
return $api;
}
2011-01-10 00:22:25 +01:00
$git_root = self::discoverGitBaseDirectory($root);
if ($git_root) {
if (!Filesystem::pathsAreEquivalent($root, $git_root)) {
throw new ArcanistUsageException(
"'.arcconfig' file is located at '{$root}', but working copy root ".
"is '{$git_root}'. Move '.arcconfig' file to the working copy root.");
}
$api = new ArcanistGitAPI($root);
$api->workingCopyIdentity = $working_copy;
return $api;
2011-01-10 00:22:25 +01:00
}
throw new ArcanistUsageException(
"The current working directory is not part of a working copy for a ".
"supported version control system (svn, git or mercurial).");
2011-01-10 00:22:25 +01:00
}
public function __construct($path) {
2011-01-10 00:22:25 +01:00
$this->path = $path;
}
public function getPath($to_file = null) {
if ($to_file !== null) {
return $this->path.DIRECTORY_SEPARATOR.
ltrim($to_file, DIRECTORY_SEPARATOR);
2011-01-10 00:22:25 +01:00
} else {
return $this->path.DIRECTORY_SEPARATOR;
2011-01-10 00:22:25 +01:00
}
}
public function getUntrackedChanges() {
return $this->getWorkingCopyFilesWithMask(self::FLAG_UNTRACKED);
}
public function getUnstagedChanges() {
return $this->getWorkingCopyFilesWithMask(self::FLAG_UNSTAGED);
}
public function getUncommittedChanges() {
return $this->getWorkingCopyFilesWithMask(self::FLAG_UNCOMMITTED);
}
public function getMergeConflicts() {
return $this->getWorkingCopyFilesWithMask(self::FLAG_CONFLICT);
}
public function getIncompleteChanges() {
return $this->getWorkingCopyFilesWithMask(self::FLAG_INCOMPLETE);
}
2011-01-10 00:22:25 +01:00
private function getWorkingCopyFilesWithMask($mask) {
$match = array();
foreach ($this->getWorkingCopyStatus() as $file => $flags) {
if ($flags & $mask) {
$match[] = $file;
}
}
return $match;
}
private static function discoverGitBaseDirectory($root) {
try {
// NOTE: This awkward construction is to make sure things work on Windows.
$future = new ExecFuture('git rev-parse --show-cdup');
$future->setCWD($root);
list($stdout) = $future->resolvex();
2011-01-10 00:22:25 +01:00
return Filesystem::resolvePath(rtrim($stdout, "\n"), $root);
} catch (CommandException $ex) {
if (preg_match('/^fatal: Not a git repository/', $ex->getStdErr())) {
return null;
}
throw $ex;
}
}
abstract public function getBlame($path);
abstract public function getWorkingCopyStatus();
abstract public function getRawDiffText($path);
abstract public function getOriginalFileData($path);
abstract public function getCurrentFileData($path);
abstract public function getLocalCommitInformation();
abstract public function getSourceControlBaseRevision();
abstract public function getCanonicalRevisionName($string);
abstract public function getBranchName();
abstract public function getSourceControlPath();
abstract public function isHistoryDefaultImmutable();
abstract public function supportsAmend();
abstract public function supportsRelativeLocalCommits();
abstract public function getWorkingCopyRevision();
abstract public function updateWorkingCopy();
abstract public function getMetadataPath();
abstract public function loadWorkingCopyDifferentialRevisions(
ConduitClient $conduit,
array $query);
/**
* Set the base commit to a reasonable default value so that working copy
* status checks can do something meaningful and won't invoke configured
* 'base' rules.
*
* This is primarily useful for workflows which do not operate on commit
* ranges but need to verify the working copy is not dirty, like "amend",
* "upgrade" and "patch".
*
* @return this
*/
public function setDefaultBaseCommit() {
throw new ArcanistCapabilityNotSupportedException($this);
}
public function amendCommit($message) {
throw new ArcanistCapabilityNotSupportedException($this);
}
public function getAllBranches() {
// TODO: Implement for Mercurial/SVN and make abstract.
return array();
}
public function hasLocalCommit($commit) {
throw new ArcanistCapabilityNotSupportedException($this);
}
2012-07-01 20:06:05 +02:00
public function getCommitMessage($commit) {
throw new ArcanistCapabilityNotSupportedException($this);
}
public function parseRelativeLocalCommit(array $argv) {
throw new ArcanistCapabilityNotSupportedException($this);
}
public function getCommitSummary($commit) {
throw new ArcanistCapabilityNotSupportedException($this);
}
public function getAllLocalChanges() {
throw new ArcanistCapabilityNotSupportedException($this);
}
abstract public function supportsLocalBranchMerge();
public function performLocalBranchMerge($branch, $message) {
throw new ArcanistCapabilityNotSupportedException($this);
}
public function getFinalizedRevisionMessage() {
throw new ArcanistCapabilityNotSupportedException($this);
}
2012-07-01 20:06:05 +02:00
public function execxLocal($pattern /* , ... */) {
$args = func_get_args();
return $this->buildLocalFuture($args)->resolvex();
}
2012-07-01 20:06:05 +02:00
public function execManualLocal($pattern /* , ... */) {
$args = func_get_args();
return $this->buildLocalFuture($args)->resolve();
}
2012-07-01 20:06:05 +02:00
public function execFutureLocal($pattern /* , ... */) {
$args = func_get_args();
return $this->buildLocalFuture($args);
}
abstract protected function buildLocalFuture(array $argv);
/* -( 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
*/
public function readScratchFile($path) {
$full_path = $this->getScratchFilePath($path);
if (!$full_path) {
return false;
}
if (!Filesystem::pathExists($full_path)) {
return false;
}
try {
$result = Filesystem::readFile($full_path);
} catch (FilesystemException $ex) {
return false;
}
return $result;
}
/**
* 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
*/
public function writeScratchFile($path, $data) {
$dir = $this->getScratchFilePath('');
if (!$dir) {
return false;
}
if (!Filesystem::pathExists($dir)) {
try {
Filesystem::createDirectory($dir);
} catch (Exception $ex) {
return false;
}
}
try {
Filesystem::writeFile($this->getScratchFilePath($path), $data);
} catch (FilesystemException $ex) {
return false;
}
return true;
}
/**
* Try to remove a scratch file.
*
* @param string Scratch file name to remove.
* @return bool True if the file was removed successfully.
* @task scratch
*/
public function removeScratchFile($path) {
$full_path = $this->getScratchFilePath($path);
if (!$full_path) {
return false;
}
try {
Filesystem::remove($full_path);
} catch (FilesystemException $ex) {
return false;
}
return true;
}
/**
* Get a human-readable description of the scratch file location.
*
* @param string Scratch file name.
* @return mixed String, or false on failure.
* @task scratch
*/
public function getReadableScratchFilePath($path) {
$full_path = $this->getScratchFilePath($path);
if ($full_path) {
return Filesystem::readablePath(
$full_path,
$this->getPath());
} else {
return false;
}
}
/**
* Get the path to a scratch file, if possible.
*
* @param string Scratch file name.
* @return mixed File path, or false on failure.
* @task scratch
*/
public function getScratchFilePath($path) {
$new_scratch_path = Filesystem::resolvePath(
'arc',
$this->getMetadataPath());
static $checked = false;
if (!$checked) {
$checked = true;
$old_scratch_path = $this->getPath('.arc');
// we only want to do the migration once
// unfortunately, people have checked in .arc directories which
// means that the old one may get recreated after we delete it
if (Filesystem::pathExists($old_scratch_path) &&
!Filesystem::pathExists($new_scratch_path)) {
Filesystem::createDirectory($new_scratch_path);
$existing_files = Filesystem::listDirectory($old_scratch_path, true);
foreach ($existing_files as $file) {
$new_path = Filesystem::resolvePath($file, $new_scratch_path);
$old_path = Filesystem::resolvePath($file, $old_scratch_path);
Filesystem::writeFile(
$new_path,
Filesystem::readFile($old_path));
}
Filesystem::remove($old_scratch_path);
}
}
return Filesystem::resolvePath($path, $new_scratch_path);
}
Add a DSL for selecting base commits Summary: New optional mode. If you set 'base' in local, project or global config or pass '--base' to 'arc diff' or 'arc which', it switches to DSL mode. In DSL mode, lists of rules from args, local, project and global config are resolved, in that order. Rules can manipulate the rule machine or resolve into actual commits. Provides support for some 'arc' rules (mostly machine manipulation) and 'git' rules (symbolic ref and merge-base). Test Plan: Ran unit tests. Also: ```$ arc which --show-base --base 'arc:prompt' Against which commit? HEAD HEAD $ arc which --show-base --base 'git:HEAD' HEAD $ arc which --show-base --base 'git:fake' Usage Exception: None of the rules in your 'base' configuration matched a valid commit. Adjust rules or specify which commit you want to use explicitly. $ arc which --show-base --base 'git:origin/master' origin/master $ arc which --show-base --base 'git:upstream' Usage Exception: None of the rules in your 'base' configuration matched a valid commit. Adjust rules or specify which commit you want to use explicitly. $ arc which --show-base --base 'literal:derp' derp $ arc which --show-base --base 'arc:halt' Usage Exception: None of the rules in your 'base' configuration matched a valid commit. Adjust rules or specify which commit you want to use explicitly. $ arc set-config --local base git:origin/master Set key 'base' = 'git:origin/master' in local config. $ arc which --show-base origin/master $ arc which --show-base --base 'git:HEAD^' HEAD^ $ arc which --show-base --base 'arc:yield, git:HEAD^' origin/master $ arc which --show-base --base 'arc:global, git:HEAD^' HEAD^ $ arc which --show-base --base 'arc:global, git:merge-base(origin/master)' 3f4f8992fba8d1f142974da36a82bae900e247c0``` Reviewers: dschleimer, vrana Reviewed By: dschleimer CC: aran Maniphest Tasks: T1233 Differential Revision: https://secure.phabricator.com/D2748
2012-06-15 23:01:28 +02:00
/* -( Base Commits )------------------------------------------------------- */
public function getBaseCommitExplanation() {
return $this->baseCommitExplanation;
}
public function setBaseCommitExplanation($explanation) {
$this->baseCommitExplanation = $explanation;
return $this;
}
public function resolveBaseCommitRule($rule, $source) {
return null;
}
public function setBaseCommitArgumentRules($base_commit_argument_rules) {
$this->baseCommitArgumentRules = $base_commit_argument_rules;
return $this;
}
public function getBaseCommitArgumentRules() {
return $this->baseCommitArgumentRules;
}
public function resolveBaseCommit() {
$working_copy = $this->getWorkingCopyIdentity();
$global_config = ArcanistBaseWorkflow::readGlobalArcConfig();
$system_config = ArcanistBaseWorkflow::readSystemArcConfig();
Add a DSL for selecting base commits Summary: New optional mode. If you set 'base' in local, project or global config or pass '--base' to 'arc diff' or 'arc which', it switches to DSL mode. In DSL mode, lists of rules from args, local, project and global config are resolved, in that order. Rules can manipulate the rule machine or resolve into actual commits. Provides support for some 'arc' rules (mostly machine manipulation) and 'git' rules (symbolic ref and merge-base). Test Plan: Ran unit tests. Also: ```$ arc which --show-base --base 'arc:prompt' Against which commit? HEAD HEAD $ arc which --show-base --base 'git:HEAD' HEAD $ arc which --show-base --base 'git:fake' Usage Exception: None of the rules in your 'base' configuration matched a valid commit. Adjust rules or specify which commit you want to use explicitly. $ arc which --show-base --base 'git:origin/master' origin/master $ arc which --show-base --base 'git:upstream' Usage Exception: None of the rules in your 'base' configuration matched a valid commit. Adjust rules or specify which commit you want to use explicitly. $ arc which --show-base --base 'literal:derp' derp $ arc which --show-base --base 'arc:halt' Usage Exception: None of the rules in your 'base' configuration matched a valid commit. Adjust rules or specify which commit you want to use explicitly. $ arc set-config --local base git:origin/master Set key 'base' = 'git:origin/master' in local config. $ arc which --show-base origin/master $ arc which --show-base --base 'git:HEAD^' HEAD^ $ arc which --show-base --base 'arc:yield, git:HEAD^' origin/master $ arc which --show-base --base 'arc:global, git:HEAD^' HEAD^ $ arc which --show-base --base 'arc:global, git:merge-base(origin/master)' 3f4f8992fba8d1f142974da36a82bae900e247c0``` Reviewers: dschleimer, vrana Reviewed By: dschleimer CC: aran Maniphest Tasks: T1233 Differential Revision: https://secure.phabricator.com/D2748
2012-06-15 23:01:28 +02:00
$parser = new ArcanistBaseCommitParser($this);
$commit = $parser->resolveBaseCommit(
array(
'args' => $this->getBaseCommitArgumentRules(),
'local' => $working_copy->getLocalConfig('base', ''),
'project' => $working_copy->getConfig('base', ''),
'global' => idx($global_config, 'base', ''),
'system' => idx($system_config, 'base', ''),
Add a DSL for selecting base commits Summary: New optional mode. If you set 'base' in local, project or global config or pass '--base' to 'arc diff' or 'arc which', it switches to DSL mode. In DSL mode, lists of rules from args, local, project and global config are resolved, in that order. Rules can manipulate the rule machine or resolve into actual commits. Provides support for some 'arc' rules (mostly machine manipulation) and 'git' rules (symbolic ref and merge-base). Test Plan: Ran unit tests. Also: ```$ arc which --show-base --base 'arc:prompt' Against which commit? HEAD HEAD $ arc which --show-base --base 'git:HEAD' HEAD $ arc which --show-base --base 'git:fake' Usage Exception: None of the rules in your 'base' configuration matched a valid commit. Adjust rules or specify which commit you want to use explicitly. $ arc which --show-base --base 'git:origin/master' origin/master $ arc which --show-base --base 'git:upstream' Usage Exception: None of the rules in your 'base' configuration matched a valid commit. Adjust rules or specify which commit you want to use explicitly. $ arc which --show-base --base 'literal:derp' derp $ arc which --show-base --base 'arc:halt' Usage Exception: None of the rules in your 'base' configuration matched a valid commit. Adjust rules or specify which commit you want to use explicitly. $ arc set-config --local base git:origin/master Set key 'base' = 'git:origin/master' in local config. $ arc which --show-base origin/master $ arc which --show-base --base 'git:HEAD^' HEAD^ $ arc which --show-base --base 'arc:yield, git:HEAD^' origin/master $ arc which --show-base --base 'arc:global, git:HEAD^' HEAD^ $ arc which --show-base --base 'arc:global, git:merge-base(origin/master)' 3f4f8992fba8d1f142974da36a82bae900e247c0``` Reviewers: dschleimer, vrana Reviewed By: dschleimer CC: aran Maniphest Tasks: T1233 Differential Revision: https://secure.phabricator.com/D2748
2012-06-15 23:01:28 +02:00
));
return $commit;
}
2011-01-10 00:22:25 +01:00
}