Completed
Push — master ( 6f65d4...f31c7e )
by Alexander
02:58
created

MergeCommand::getRevisionTitle()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 11
cp 0
rs 9.584
c 0
b 0
f 0
cc 4
nc 3
nop 1
crap 20
1
<?php
2
/**
3
 * This file is part of the SVN-Buddy library.
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 *
7
 * @copyright Alexander Obuhovich <[email protected]>
8
 * @link      https://github.com/console-helpers/svn-buddy
9
 */
10
11
namespace ConsoleHelpers\SVNBuddy\Command;
12
13
14
use ConsoleHelpers\SVNBuddy\Config\AbstractConfigSetting;
15
use ConsoleHelpers\SVNBuddy\Config\ArrayConfigSetting;
16
use ConsoleHelpers\SVNBuddy\Config\ChoiceConfigSetting;
17
use ConsoleHelpers\SVNBuddy\Config\StringConfigSetting;
18
use ConsoleHelpers\ConsoleKit\Exception\CommandException;
19
use ConsoleHelpers\SVNBuddy\Helper\OutputHelper;
20
use ConsoleHelpers\SVNBuddy\MergeSourceDetector\AbstractMergeSourceDetector;
21
use ConsoleHelpers\SVNBuddy\Repository\Connector\UrlResolver;
22
use ConsoleHelpers\SVNBuddy\Repository\Parser\RevisionListParser;
23
use ConsoleHelpers\SVNBuddy\Repository\WorkingCopyConflictTracker;
24
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
25
use Symfony\Component\Console\Helper\Table;
26
use Symfony\Component\Console\Input\InputArgument;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
31
class MergeCommand extends AbstractCommand implements IAggregatorAwareCommand, IConfigAwareCommand
32
{
33
34
	const SETTING_MERGE_SOURCE_URL = 'merge.source-url';
35
36
	const SETTING_MERGE_RECENT_CONFLICTS = 'merge.recent-conflicts';
37
38
	const SETTING_MERGE_AUTO_COMMIT = 'merge.auto-commit';
39
40
	const REVISION_ALL = 'all';
41
42
	/**
43
	 * Merge source detector.
44
	 *
45
	 * @var AbstractMergeSourceDetector
46
	 */
47
	private $_mergeSourceDetector;
48
49
	/**
50
	 * Revision list parser.
51
	 *
52
	 * @var RevisionListParser
53
	 */
54
	private $_revisionListParser;
55
56
	/**
57
	 * Unmerged revisions.
58
	 *
59
	 * @var array
60
	 */
61
	private $_unmergedRevisions = array();
62
63
	/**
64
	 * Url resolver.
65
	 *
66
	 * @var UrlResolver
67
	 */
68
	private $_urlResolver;
69
70
	/**
71
	 * Working copy conflict tracker.
72
	 *
73
	 * @var WorkingCopyConflictTracker
74
	 */
75
	private $_workingCopyConflictTracker;
76
77
	/**
78
	 * Prepare dependencies.
79
	 *
80
	 * @return void
81
	 */
82
	protected function prepareDependencies()
83
	{
84
		parent::prepareDependencies();
85
86
		$container = $this->getContainer();
87
88
		$this->_mergeSourceDetector = $container['merge_source_detector'];
89
		$this->_revisionListParser = $container['revision_list_parser'];
90
		$this->_urlResolver = $container['repository_url_resolver'];
91
		$this->_workingCopyConflictTracker = $container['working_copy_conflict_tracker'];
92
	}
93
94
	/**
95
	 * {@inheritdoc}
96
	 */
97
	protected function configure()
98
	{
99
		$this
100
			->setName('merge')
101
			->setDescription('Merge changes from another project or ref within same project into a working copy')
102
			->addArgument(
103
				'path',
104
				InputArgument::OPTIONAL,
105
				'Working copy path',
106
				'.'
107
			)
108
			->addOption(
109
				'source-url',
110
				null,
111
				InputOption::VALUE_REQUIRED,
112
				'Merge source url (absolute or relative) or ref name, e.g. <comment>branches/branch-name</comment>'
113
			)
114
			->addOption(
115
				'revisions',
116
				'r',
117
				InputOption::VALUE_REQUIRED,
118
				'List of revision(-s) and/or revision range(-s) to merge, e.g. <comment>53324</comment>, <comment>1224-4433</comment> or <comment>all</comment>'
119
			)
120
			->addOption(
121
				'bugs',
122
				'b',
123
				InputOption::VALUE_REQUIRED,
124
				'List of bug(-s) to merge, e.g. <comment>JRA-1234</comment>, <comment>43644</comment>'
125
			)
126
			->addOption(
127
				'with-full-message',
128
				'f',
129
				InputOption::VALUE_NONE,
130
				'Shows non-truncated commit messages'
131
			)
132
			->addOption(
133
				'with-details',
134
				'd',
135
				InputOption::VALUE_NONE,
136
				'Shows detailed revision information, e.g. paths affected'
137
			)
138
			->addOption(
139
				'with-summary',
140
				's',
141
				InputOption::VALUE_NONE,
142
				'Shows number of added/changed/removed paths in the revision'
143
			)
144
			->addOption(
145
				'update-revision',
146
				null,
147
				InputOption::VALUE_REQUIRED,
148
				'Update working copy to given revision before performing a merge'
149
			)
150
			->addOption(
151
				'auto-commit',
152
				null,
153
				InputOption::VALUE_REQUIRED,
154
				'Automatically perform commit on successful merge, e.g. <comment>yes</comment> or <comment>no</comment>'
155
			)
156
			->addOption(
157
				'record-only',
158
				null,
159
				InputOption::VALUE_NONE,
160
				'Mark revisions as merged without actually merging them'
161
			);
162
163
		parent::configure();
164
	}
165
166
	/**
167
	 * Return possible values for the named option
168
	 *
169
	 * @param string            $optionName Option name.
170
	 * @param CompletionContext $context    Completion context.
171
	 *
172
	 * @return array
173
	 */
174
	public function completeOptionValues($optionName, CompletionContext $context)
175
	{
176
		$ret = parent::completeOptionValues($optionName, $context);
177
178
		if ( $optionName === 'revisions' ) {
179
			return array('all');
180
		}
181
182
		if ( $optionName === 'source-url' ) {
183
			return $this->getAllRefs();
184
		}
185
186
		if ( $optionName === 'auto-commit' ) {
187
			return array('yes', 'no');
188
		}
189
190
		return $ret;
191
	}
192
193
	/**
194
	 * {@inheritdoc}
195
	 *
196
	 * @throws \RuntimeException When both "--bugs" and "--revisions" options were specified.
197
	 * @throws CommandException When everything is merged.
198
	 * @throws CommandException When manually specified revisions are already merged.
199
	 */
200
	protected function execute(InputInterface $input, OutputInterface $output)
201
	{
202
		$bugs = $this->getList($this->io->getOption('bugs'));
203
		$revisions = $this->getList($this->io->getOption('revisions'));
204
205
		if ( $bugs && $revisions ) {
206
			throw new \RuntimeException('The "--bugs" and "--revisions" options are mutually exclusive.');
207
		}
208
209
		$wc_path = $this->getWorkingCopyPath();
210
211
		$this->ensureLatestWorkingCopy($wc_path);
212
213
		$source_url = $this->getSourceUrl($wc_path);
214
		$this->printSourceAndTarget($source_url, $wc_path);
215
		$this->_unmergedRevisions = $this->getUnmergedRevisions($source_url, $wc_path);
216
217
		if ( ($bugs || $revisions) && !$this->_unmergedRevisions ) {
218
			throw new CommandException('Nothing to merge.');
219
		}
220
221
		$this->ensureWorkingCopyWithoutConflicts($source_url, $wc_path);
222
223
		if ( $this->shouldMergeAll($revisions) ) {
224
			$revisions = $this->_unmergedRevisions;
225
		}
226
		else {
227
			if ( $revisions ) {
228
				$revisions = $this->getDirectRevisions($revisions, $source_url);
229
			}
230
			elseif ( $bugs ) {
231
				$revisions = $this->getRevisionLog($source_url)->find('bugs', $bugs);
232
233
				if ( !$revisions ) {
234
					throw new CommandException('Specified bugs aren\'t mentioned in any of revisions');
235
				}
236
			}
237
238
			if ( $revisions ) {
239
				$revisions = array_intersect($revisions, $this->_unmergedRevisions);
240
241
				if ( !$revisions ) {
242
					throw new CommandException('Requested revisions are already merged');
243
				}
244
			}
245
		}
246
247
		if ( $revisions ) {
248
			$this->performMerge($source_url, $wc_path, $revisions);
249
		}
250
		elseif ( $this->_unmergedRevisions ) {
251
			$this->runOtherCommand('log', array(
252
				'path' => $source_url,
253
				'--revisions' => implode(',', $this->_unmergedRevisions),
254
				'--with-full-message' => $this->io->getOption('with-full-message'),
255
				'--with-details' => $this->io->getOption('with-details'),
256
				'--with-summary' => $this->io->getOption('with-summary'),
257
				'--with-merge-oracle' => true,
258
			));
259
		}
260
	}
261
262
	/**
263
	 * Determines if all unmerged revisions should be merged.
264
	 *
265
	 * @param array $revisions Revisions.
266
	 *
267
	 * @return boolean
268
	 */
269
	protected function shouldMergeAll(array $revisions)
270
	{
271
		return $revisions === array(self::REVISION_ALL);
272
	}
273
274
	/**
275
	 * Ensures, that working copy is up to date.
276
	 *
277
	 * @param string $wc_path Working copy path.
278
	 *
279
	 * @return void
280
	 */
281
	protected function ensureLatestWorkingCopy($wc_path)
282
	{
283
		$this->io->write(' * Working Copy Status ... ');
284
		$update_revision = $this->io->getOption('update-revision');
285
286
		if ( $this->repositoryConnector->getWorkingCopyMissing($wc_path) ) {
287
			$this->io->writeln('<error>Locally deleted files found</error>');
288
			$this->updateWorkingCopy($wc_path, $update_revision);
289
290
			return;
291
		}
292
293
		$working_copy_revisions = $this->repositoryConnector->getWorkingCopyRevisions($wc_path);
294
295
		if ( count($working_copy_revisions) > 1 ) {
296
			$this->io->writeln(
297
				'<error>Mixed revisions: ' . implode(', ', $working_copy_revisions) . '</error>'
298
			);
299
			$this->updateWorkingCopy($wc_path, $update_revision);
300
301
			return;
302
		}
303
304
		$update_revision = $this->getWorkingCopyUpdateRevision($wc_path);
305
306
		if ( isset($update_revision) ) {
307
			$this->io->writeln('<error>Not at ' . $update_revision . ' revision</error>');
308
			$this->updateWorkingCopy($wc_path, $update_revision);
309
310
			return;
311
		}
312
313
		$this->io->writeln('<info>Up to date</info>');
314
	}
315
316
	/**
317
	 * Returns revision, that working copy needs to be updated to.
318
	 *
319
	 * @param string $wc_path Working copy path.
320
	 *
321
	 * @return integer|null
322
	 */
323
	protected function getWorkingCopyUpdateRevision($wc_path)
324
	{
325
		$update_revision = $this->io->getOption('update-revision');
326
		$actual_revision = $this->repositoryConnector->getLastRevision($wc_path);
327
328
		if ( isset($update_revision) ) {
329
			if ( is_numeric($update_revision) ) {
330
				return (int)$update_revision === (int)$actual_revision ? null : $update_revision;
331
			}
332
333
			return $update_revision;
334
		}
335
336
		$repository_revision = $this->repositoryConnector->getLastRevision(
337
			$this->repositoryConnector->getWorkingCopyUrl($wc_path)
338
		);
339
340
		return $repository_revision > $actual_revision ? $repository_revision : null;
341
	}
342
343
	/**
344
	 * Updates working copy.
345
	 *
346
	 * @param string     $wc_path  Working copy path.
347
	 * @param mixed|null $revision Revision.
348
	 *
349
	 * @return void
350
	 */
351
	protected function updateWorkingCopy($wc_path, $revision = null)
352
	{
353
		$arguments = array('path' => $wc_path, '--ignore-externals' => true);
354
355
		if ( isset($revision) ) {
356
			$arguments['--revision'] = $revision;
357
		}
358
359
		$this->runOtherCommand('update', $arguments);
360
	}
361
362
	/**
363
	 * Returns source url for merge.
364
	 *
365
	 * @param string $wc_path Working copy path.
366
	 *
367
	 * @return string
368
	 * @throws CommandException When source path is invalid.
369
	 */
370
	protected function getSourceUrl($wc_path)
371
	{
372
		$source_url = $this->io->getOption('source-url');
373
374
		if ( $source_url === null ) {
375
			$source_url = $this->getSetting(self::SETTING_MERGE_SOURCE_URL);
376
		}
377
		elseif ( !$this->repositoryConnector->isUrl($source_url) ) {
378
			$wc_url = $this->repositoryConnector->getWorkingCopyUrl($wc_path);
379
			$source_url = $this->_urlResolver->resolve($wc_url, $source_url);
380
		}
381
382
		if ( !$source_url ) {
383
			$wc_url = $this->repositoryConnector->getWorkingCopyUrl($wc_path);
384
			$source_url = $this->_mergeSourceDetector->detect($wc_url);
385
386
			if ( $source_url ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $source_url of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
387
				$this->setSetting(self::SETTING_MERGE_SOURCE_URL, $source_url);
388
			}
389
		}
390
391
		if ( !$source_url ) {
392
			$wc_url = $this->repositoryConnector->getWorkingCopyUrl($wc_path);
393
			$error_msg = 'Unable to guess "--source-url" option value. Please specify it manually.' . PHP_EOL;
394
			$error_msg .= 'Working Copy URL: ' . $wc_url . '.';
395
			throw new CommandException($error_msg);
396
		}
397
398
		return $source_url;
399
	}
400
401
	/**
402
	 * Prints information about merge source & target.
403
	 *
404
	 * @param string $source_url Merge source: url.
405
	 * @param string $wc_path    Merge target: working copy path.
406
	 *
407
	 * @return void
408
	 */
409
	protected function printSourceAndTarget($source_url, $wc_path)
410
	{
411
		$relative_source_url = $this->repositoryConnector->getRelativePath($source_url);
412
		$relative_target_url = $this->repositoryConnector->getRelativePath($wc_path);
413
414
		$this->io->writeln(' * Merge Source ... <info>' . $relative_source_url . '</info>');
415
		$this->io->writeln(' * Merge Target ... <info>' . $relative_target_url . '</info>');
416
	}
417
418
	/**
419
	 * Ensures, that there are some unmerged revisions.
420
	 *
421
	 * @param string $source_url Merge source: url.
422
	 * @param string $wc_path    Merge target: working copy path.
423
	 *
424
	 * @return array
425
	 */
426
	protected function getUnmergedRevisions($source_url, $wc_path)
427
	{
428
		// Avoid missing revision query progress bar overwriting following output.
429
		$revision_log = $this->getRevisionLog($source_url);
430
431
		$this->io->write(' * Upcoming Merge Status ... ');
432
		$unmerged_revisions = $this->calculateUnmergedRevisions($source_url, $wc_path);
433
434
		if ( $unmerged_revisions ) {
435
			$unmerged_bugs = $revision_log->getBugsFromRevisions($unmerged_revisions);
436
			$error_msg = '<error>%d revision(-s) or %d bug(-s) not merged</error>';
437
			$this->io->writeln(sprintf($error_msg, count($unmerged_revisions), count($unmerged_bugs)));
438
		}
439
		else {
440
			$this->io->writeln('<info>Up to date</info>');
441
		}
442
443
		return $unmerged_revisions;
444
	}
445
446
	/**
447
	 * Returns not merged revisions.
448
	 *
449
	 * @param string $source_url Merge source: url.
450
	 * @param string $wc_path    Merge target: working copy path.
451
	 *
452
	 * @return array
453
	 */
454
	protected function calculateUnmergedRevisions($source_url, $wc_path)
455
	{
456
		$command = $this->repositoryConnector->getCommand(
457
			'mergeinfo',
458
			'--show-revs eligible {' . $source_url . '} {' . $wc_path . '}'
459
		);
460
461
		$merge_info = $this->repositoryConnector->getProperty('svn:mergeinfo', $wc_path);
462
463
		$cache_invalidator = array(
464
			'source:' . $this->repositoryConnector->getLastRevision($source_url),
465
			'merged_hash:' . crc32($merge_info),
466
		);
467
		$command->setCacheInvalidator(implode(';', $cache_invalidator));
468
469
		$merge_info = $command->run();
470
		$merge_info = explode(PHP_EOL, $merge_info);
471
472
		foreach ( $merge_info as $index => $revision ) {
473
			$merge_info[$index] = ltrim($revision, 'r');
474
		}
475
476
		return array_filter($merge_info);
477
	}
478
479
	/**
480
	 * Parses information from "svn:mergeinfo" property.
481
	 *
482
	 * @param string $source_path Merge source: path in repository.
483
	 * @param string $wc_path     Merge target: working copy path.
484
	 *
485
	 * @return array
486
	 */
487
	protected function getMergedRevisions($source_path, $wc_path)
488
	{
489
		$merge_info = $this->repositoryConnector->getProperty('svn:mergeinfo', $wc_path);
490
		$merge_info = array_filter(explode("\n", $merge_info));
491
492
		foreach ( $merge_info as $merge_info_line ) {
493
			list($path, $revisions) = explode(':', $merge_info_line, 2);
494
495
			if ( $path === $source_path ) {
496
				return $this->_revisionListParser->expandRanges(explode(',', $revisions));
497
			}
498
		}
499
500
		return array();
501
	}
502
503
	/**
504
	 * Validates revisions to actually exist.
505
	 *
506
	 * @param array  $revisions      Revisions.
507
	 * @param string $repository_url Repository url.
508
	 *
509
	 * @return array
510
	 * @throws CommandException When revision doesn't exist.
511
	 */
512
	protected function getDirectRevisions(array $revisions, $repository_url)
513
	{
514
		$revision_log = $this->getRevisionLog($repository_url);
515
516
		try {
517
			$revisions = $this->_revisionListParser->expandRanges($revisions);
518
			$revision_log->getRevisionsData('summary', $revisions);
519
		}
520
		catch ( \InvalidArgumentException $e ) {
521
			throw new CommandException($e->getMessage());
522
		}
523
524
		return $revisions;
525
	}
526
527
	/**
528
	 * Performs merge.
529
	 *
530
	 * @param string $source_url Merge source: url.
531
	 * @param string $wc_path    Merge target: working copy path.
532
	 * @param array  $revisions  Revisions to merge.
533
	 *
534
	 * @return void
535
	 */
536
	protected function performMerge($source_url, $wc_path, array $revisions)
537
	{
538
		sort($revisions, SORT_NUMERIC);
539
		$revision_count = count($revisions);
540
541
		$merged_revision_count = 0;
542
		$merged_revisions = $this->repositoryConnector->getFreshMergedRevisions($wc_path);
543
544
		if ( $merged_revisions ) {
545
			$merged_revisions = call_user_func_array('array_merge', $merged_revisions);
546
			$merged_revision_count = count($merged_revisions);
547
			$revision_count += $merged_revision_count;
548
		}
549
550
		$param_string_ending = '{' . $source_url . '} {' . $wc_path . '}';
551
552
		if ( $this->io->getOption('record-only') ) {
553
			$param_string_ending = '--record-only ' . $param_string_ending;
554
		}
555
556
		$revision_title_mask = $this->getRevisionTitle($wc_path);
557
558
		foreach ( $revisions as $index => $revision ) {
559
			$command = $this->repositoryConnector->getCommand(
560
				'merge',
561
				'-c ' . $revision . ' ' . $param_string_ending
562
			);
563
564
			$progress_bar = $this->createMergeProgressBar($merged_revision_count + $index + 1, $revision_count);
565
			$merge_heading = PHP_EOL . '<fg=white;options=bold>';
566
			$merge_heading .= '--- Merging ' . \str_replace('{revision}', $revision, $revision_title_mask);
567
			$merge_heading .= " into '$1' " . $progress_bar . ':</>';
568
569
			$command->runLive(array(
570
				$wc_path => '.',
571
				'/--- Merging r' . $revision . " into '([^']*)':/" => $merge_heading,
572
			));
573
574
			$this->_unmergedRevisions = array_diff($this->_unmergedRevisions, array($revision));
575
			$this->ensureWorkingCopyWithoutConflicts($source_url, $wc_path, $revision);
576
		}
577
578
		$this->performCommit();
579
	}
580
581
	/**
582
	 * Returns revision title.
583
	 *
584
	 * @param string $wc_path Working copy path.
585
	 *
586
	 * @return string
587
	 */
588
	protected function getRevisionTitle($wc_path)
589
	{
590
		$arcanist_config_file = $wc_path . \DIRECTORY_SEPARATOR . '.arcconfig';
591
592
		if ( !\file_exists($arcanist_config_file) ) {
593
			return '<fg=white;options=underscore>{revision}</> revision';
594
		}
595
596
		$arcanist_config = \json_decode(\file_get_contents($arcanist_config_file), true);
597
598
		if ( !\is_array($arcanist_config)
599
			|| !isset($arcanist_config['repository.callsign'], $arcanist_config['phabricator.uri'])
600
		) {
601
			return '<fg=white;options=underscore>{revision}</> revision';
602
		}
603
604
		$revision_title = $arcanist_config['phabricator.uri'];
605
		$revision_title .= 'r' . $arcanist_config['repository.callsign'] . '{revision}';
606
607
		return '<fg=white;options=underscore>' . $revision_title . '</>';
608
	}
609
610
	/**
611
	 * Create merge progress bar.
612
	 *
613
	 * @param integer $current Current.
614
	 * @param integer $total   Total.
615
	 *
616
	 * @return string
617
	 */
618
	protected function createMergeProgressBar($current, $total)
619
	{
620
		$total_length = 28;
621
		$percent_used = floor(($current / $total) * 100);
622
		$length_used = floor(($total_length * $percent_used) / 100);
623
		$length_free = $total_length - $length_used;
624
625
		$ret = $length_used > 0 ? str_repeat('=', $length_used - 1) : '';
626
		$ret .= '>' . str_repeat('-', $length_free);
627
628
		return '[' . $ret . '] ' . $percent_used . '% (' . $current . ' of ' . $total . ')';
629
	}
630
631
	/**
632
	 * Ensures, that there are no unresolved conflicts in working copy.
633
	 *
634
	 * @param string  $source_url                 Source url.
635
	 * @param string  $wc_path                    Working copy path.
636
	 * @param integer $largest_suggested_revision Largest revision, that is suggested in error message.
637
	 *
638
	 * @return void
639
	 * @throws CommandException When merge conflicts detected.
640
	 */
641
	protected function ensureWorkingCopyWithoutConflicts($source_url, $wc_path, $largest_suggested_revision = null)
642
	{
643
		$this->io->write(' * Previous Merge Status ... ');
644
645
		$conflicts = $this->_workingCopyConflictTracker->getNewConflicts($wc_path);
646
647
		if ( !$conflicts ) {
648
			$this->io->writeln('<info>Successful</info>');
649
650
			return;
651
		}
652
653
		$this->_workingCopyConflictTracker->add($wc_path);
654
		$this->io->writeln('<error>' . count($conflicts) . ' conflict(-s)</error>');
655
656
		$table = new Table($this->io->getOutput());
657
658
		if ( $largest_suggested_revision ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $largest_suggested_revision of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
659
			$table->setHeaders(array(
660
				'Path',
661
				'Associated Revisions (before ' . $largest_suggested_revision . ')',
662
			));
663
		}
664
		else {
665
			$table->setHeaders(array(
666
				'Path',
667
				'Associated Revisions',
668
			));
669
		}
670
671
		$revision_log = $this->getRevisionLog($source_url);
672
		$source_path = $this->repositoryConnector->getRelativePath($source_url) . '/';
673
674
		/** @var OutputHelper $output_helper */
675
		$output_helper = $this->getHelper('output');
676
677
		foreach ( $conflicts as $conflict_path ) {
678
			$path_revisions = $revision_log->find('paths', $source_path . $conflict_path);
679
			$path_revisions = array_intersect($this->_unmergedRevisions, $path_revisions);
680
681
			if ( $path_revisions && isset($largest_suggested_revision) ) {
682
				$path_revisions = $this->limitRevisions($path_revisions, $largest_suggested_revision);
683
			}
684
685
			$table->addRow(array(
686
				$conflict_path,
687
				$path_revisions ? $output_helper->formatArray($path_revisions, 4) : '-',
688
			));
689
		}
690
691
		$table->render();
692
693
		throw new CommandException('Working copy contains unresolved merge conflicts.');
694
	}
695
696
	/**
697
	 * Returns revisions not larger, then given one.
698
	 *
699
	 * @param array   $revisions    Revisions.
700
	 * @param integer $max_revision Maximal revision.
701
	 *
702
	 * @return array
703
	 */
704
	protected function limitRevisions(array $revisions, $max_revision)
705
	{
706
		$ret = array();
707
708
		foreach ( $revisions as $revision ) {
709
			if ( $revision < $max_revision ) {
710
				$ret[] = $revision;
711
			}
712
		}
713
714
		return $ret;
715
	}
716
717
	/**
718
	 * Performs commit unless user doesn't want it.
719
	 *
720
	 * @return void
721
	 */
722
	protected function performCommit()
723
	{
724
		$auto_commit = $this->io->getOption('auto-commit');
725
726
		if ( $auto_commit !== null ) {
727
			$auto_commit = $auto_commit === 'yes';
728
		}
729
		else {
730
			$auto_commit = (boolean)$this->getSetting(self::SETTING_MERGE_AUTO_COMMIT);
731
		}
732
733
		if ( $auto_commit ) {
734
			$this->io->writeln(array('', 'Commencing automatic commit after merge ...'));
735
			$this->runOtherCommand('commit');
736
		}
737
	}
738
739
	/**
740
	 * Returns list of config settings.
741
	 *
742
	 * @return AbstractConfigSetting[]
743
	 */
744
	public function getConfigSettings()
745
	{
746
		return array(
747
			new StringConfigSetting(self::SETTING_MERGE_SOURCE_URL, ''),
748
			new ArrayConfigSetting(self::SETTING_MERGE_RECENT_CONFLICTS, array()),
749
			new ChoiceConfigSetting(
750
				self::SETTING_MERGE_AUTO_COMMIT,
751
				array(1 => 'Yes', 0 => 'No'),
752
				1
753
			),
754
		);
755
	}
756
757
	/**
758
	 * Returns option names, that makes sense to use in aggregation mode.
759
	 *
760
	 * @return array
761
	 */
762
	public function getAggregatedOptions()
763
	{
764
		return array('with-full-message', 'with-details', 'with-summary');
765
	}
766
767
}
768