1<?php
2
3/**
4 * Show which revision or revisions are in the working copy.
5 */
6final class ArcanistWhichWorkflow extends ArcanistWorkflow {
7
8  public function getWorkflowName() {
9    return 'which';
10  }
11
12  public function getCommandSynopses() {
13    return phutil_console_format(<<<EOTEXT
14      **which** [options] (svn)
15      **which** [options] [__commit__] (hg, git)
16EOTEXT
17      );
18  }
19
20  public function getCommandHelp() {
21    return phutil_console_format(<<<EOTEXT
22          Supports: svn, git, hg
23          Shows which repository the current working copy corresponds to,
24          which commits 'arc diff' will select, and which revision is in
25          the working copy (or which revisions, if more than one matches).
26EOTEXT
27      );
28  }
29
30  public function requiresConduit() {
31    return true;
32  }
33
34  public function requiresRepositoryAPI() {
35    return true;
36  }
37
38  public function requiresAuthentication() {
39    return true;
40  }
41
42  public function getArguments() {
43    return array(
44      'any-status' => array(
45        'help' => pht('Show committed and abandoned revisions.'),
46      ),
47      'base' => array(
48        'param' => 'rules',
49        'help'  => pht('Additional rules for determining base revision.'),
50        'nosupport' => array(
51          'svn' => pht('Subversion does not use base commits.'),
52        ),
53        'supports' => array('git', 'hg'),
54      ),
55      'show-base' => array(
56        'help'  => pht('Print base commit only and exit.'),
57        'nosupport' => array(
58          'svn' => pht('Subversion does not use base commits.'),
59        ),
60        'supports' => array('git', 'hg'),
61      ),
62      'head' => array(
63        'param' => 'commit',
64        'help' => pht('Specify the end of the commit range to select.'),
65        'nosupport' => array(
66          'svn' => pht('Subversion does not support commit ranges.'),
67          'hg' => pht('Mercurial does not support %s yet.', '--head'),
68        ),
69        'supports' => array('git'),
70      ),
71      '*' => 'commit',
72    );
73  }
74
75  public function run() {
76    $console = PhutilConsole::getConsole();
77
78    if (!$this->getArgument('show-base')) {
79      $this->printRepositorySection();
80      $console->writeOut("\n");
81    }
82
83    $repository_api = $this->getRepositoryAPI();
84
85    $arg_commit = $this->getArgument('commit');
86    if (count($arg_commit)) {
87      $this->parseBaseCommitArgument($arg_commit);
88    }
89    $arg = $arg_commit ? ' '.head($arg_commit) : '';
90
91    $repository_api->setBaseCommitArgumentRules(
92      $this->getArgument('base', ''));
93
94    $supports_ranges = $repository_api->supportsCommitRanges();
95
96    $head_commit = $this->getArgument('head');
97    if ($head_commit !== null) {
98      $arg .= csprintf(' --head %R', $head_commit);
99      $repository_api->setHeadCommit($head_commit);
100    }
101
102    if ($supports_ranges) {
103      $relative = $repository_api->getBaseCommit();
104
105      if ($this->getArgument('show-base')) {
106        echo $relative."\n";
107        return 0;
108      }
109
110      $info = $repository_api->getLocalCommitInformation();
111      if ($info) {
112        $commits = array();
113        foreach ($info as $commit) {
114          $hash     = substr($commit['commit'], 0, 16);
115          $summary  = $commit['summary'];
116
117          $commits[] = "    {$hash}  {$summary}";
118        }
119        $commits = implode("\n", $commits);
120      } else {
121        $commits = '    '.pht('(No commits.)');
122      }
123
124      $explanation = $repository_api->getBaseCommitExplanation();
125
126      $relative_summary = $repository_api->getCommitSummary($relative);
127      $relative = substr($relative, 0, 16);
128
129      if ($repository_api instanceof ArcanistGitAPI) {
130        $head = $this->getArgument('head', 'HEAD');
131        $command = csprintf('git diff %R', "{$relative}..{$head}");
132      } else if ($repository_api instanceof ArcanistMercurialAPI) {
133        $command = csprintf(
134          'hg diff --rev %R',
135          hgsprintf('%s', $relative));
136      } else {
137        throw new Exception(pht('Unknown VCS!'));
138      }
139
140      echo phutil_console_wrap(
141        phutil_console_format(
142          "**%s**\n%s\n\n    %s  %s\n\n",
143          pht('COMMIT RANGE'),
144          pht(
145            "If you run '%s', changes between the commit:",
146            "arc diff{$arg}"),
147          $relative,
148          $relative_summary));
149
150      if ($head_commit === null) {
151        $will_be_sent = pht(
152          '...and the current working copy state will be sent to '.
153          'Differential, because %s',
154          $explanation);
155      } else {
156        $will_be_sent = pht(
157          '...and "%s" will be sent to Differential, because %s',
158          $head_commit,
159          $explanation);
160      }
161
162      echo phutil_console_wrap(
163        phutil_console_format(
164          "%s\n\n%s\n\n    $ %s\n\n%s\n\n",
165          $will_be_sent,
166          pht(
167            'You can see the exact changes that will be sent by running '.
168            'this command:'),
169          $command,
170          pht('These commits will be included in the diff:')));
171
172      echo $commits."\n\n\n";
173    }
174
175    $any_status = $this->getArgument('any-status');
176
177    $query = array(
178      'status' => $any_status
179        ? 'status-any'
180        : 'status-open',
181    );
182
183    $revisions = $repository_api->loadWorkingCopyDifferentialRevisions(
184      $this->getConduit(),
185      $query);
186
187    echo phutil_console_wrap(
188      phutil_console_format(
189        "**%s**\n%s\n\n",
190        pht('MATCHING REVISIONS'),
191        pht(
192          'These Differential revisions match the changes in this working '.
193          'copy:')));
194
195    if (empty($revisions)) {
196      echo "    ".pht('(No revisions match.)')."\n";
197      echo "\n";
198      echo phutil_console_wrap(
199        phutil_console_format(
200          pht(
201            "Since there are no revisions in Differential which match this ".
202            "working copy, a new revision will be **created** if you run ".
203            "'%s'.\n\n",
204            "arc diff{$arg}")));
205    } else {
206      $other_author_phids = array();
207      foreach ($revisions as $revision) {
208        if ($revision['authorPHID'] != $this->getUserPHID()) {
209          $other_author_phids[] = $revision['authorPHID'];
210        }
211      }
212
213      $other_authors = array();
214      if ($other_author_phids) {
215        $other_authors = $this->getConduit()->callMethodSynchronous(
216          'user.query',
217          array(
218            'phids' => $other_author_phids,
219          ));
220        $other_authors = ipull($other_authors, 'userName', 'phid');
221      }
222
223      foreach ($revisions as $revision) {
224        $title = $revision['title'];
225        $monogram = 'D'.$revision['id'];
226
227        if ($revision['authorPHID'] != $this->getUserPHID()) {
228          $author = $other_authors[$revision['authorPHID']];
229          echo pht("    %s (%s) %s\n", $monogram, $author, $title);
230        } else {
231          echo pht("    %s %s\n", $monogram, $title);
232        }
233
234        echo '        '.pht('Reason').': '.$revision['why']."\n";
235        echo "\n";
236      }
237      if (count($revisions) == 1) {
238        echo phutil_console_wrap(
239          phutil_console_format(
240            pht(
241              "Since exactly one revision in Differential matches this ".
242              "working copy, it will be **updated** if you run '%s'.",
243              "arc diff{$arg}")));
244      } else {
245        echo phutil_console_wrap(
246          pht(
247            "Since more than one revision in Differential matches this ".
248            "working copy, you will be asked which revision you want to ".
249            "update if you run '%s'.",
250            "arc diff {$arg}"));
251      }
252      echo "\n\n";
253    }
254
255    return 0;
256  }
257
258  private function printRepositorySection() {
259    $console = PhutilConsole::getConsole();
260    $console->writeOut("**%s**\n", pht('REPOSITORY'));
261
262    $repo_name = $this->getRepositoryName();
263
264    $console->writeOut(
265      "%s\n\n",
266      pht(
267        'To identify the repository associated with this working copy, '.
268        'arc followed this process:'));
269
270    foreach ($this->getRepositoryReasons() as $reason) {
271      $reason = phutil_console_wrap($reason, 4);
272      $console->writeOut("%s\n\n", $reason);
273    }
274
275    if ($repo_name) {
276      $console->writeOut(
277        "%s\n",
278        pht('This working copy is associated with the %s repository.',
279        phutil_console_format('**%s**', $repo_name)));
280    } else {
281      $console->writeOut(
282        "%s\n",
283        pht('This working copy is not associated with any repository.'));
284    }
285  }
286
287}
288