Skip to content

Commit 3ee8946

Browse files
committed
Check branch sync status before squash
1 parent d5f874b commit 3ee8946

File tree

2 files changed

+36
-46
lines changed

2 files changed

+36
-46
lines changed

src/Helper/GitHelper.php

Lines changed: 35 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Gush\Helper;
1313

1414
use Gush\Exception\CannotSquashMultipleAuthors;
15+
use Gush\Exception\MergeWorkflowException;
1516
use Gush\Exception\UserException;
1617
use Gush\Exception\WorkingTreeIsNotReady;
1718
use Gush\Operation\RemoteMergeOperation;
@@ -399,7 +400,7 @@ public function mergeBranch($base, $sourceBranch, $commitMessage, $fastForward =
399400
'allow_failures' => false,
400401
],
401402
[
402-
'line' => ['git', 'commit', '-F', $tmpName],
403+
'line' => ['git', 'commit', '--file', $tmpName],
403404
'allow_failures' => false,
404405
],
405406
]
@@ -446,7 +447,7 @@ public function mergeBranchWithLog($base, $sourceBranch, $commitMessage, $source
446447
$tmpName = $this->filesystemHelper->newTempFilename();
447448
file_put_contents($tmpName, $commitMessage);
448449

449-
$this->processHelper->runCommand(['git', 'commit', '-F', $tmpName]);
450+
$this->processHelper->runCommand(['git', 'commit', '--file', $tmpName]);
450451

451452
return trim($this->processHelper->runCommand('git rev-parse HEAD'));
452453
}
@@ -456,15 +457,7 @@ public function addNotes($notes, $commitHash, $ref)
456457
$tmpName = $this->filesystemHelper->newTempFilename();
457458
file_put_contents($tmpName, $notes);
458459

459-
$commands = [
460-
'git',
461-
'notes',
462-
'--ref='.$ref,
463-
'add',
464-
'-F',
465-
$tmpName,
466-
$commitHash,
467-
];
460+
$commands = ['git', 'notes', '--ref='.$ref, 'add', '--file', $tmpName, $commitHash];
468461

469462
$this->processHelper->runCommand($commands, true);
470463
}
@@ -474,7 +467,7 @@ public function pushToRemote($remote, $ref, $setUpstream = false, $force = false
474467
$command = ['git', 'push', $remote];
475468

476469
if ($setUpstream) {
477-
$command[] = '-u';
470+
$command[] = '--set-upstream';
478471
}
479472

480473
if ($force) {
@@ -608,21 +601,26 @@ public function squashCommits($base, $branchName, $ignoreMultipleAuthors = false
608601
// Get commits only in the branch but not in base (in reverse order)
609602
// we can't use --max-count here because that is applied before the reversing!
610603
//
611-
// using git-log works better then finding the fork-point with git-merge-base
604+
// using git-log works better than finding the fork-point with git-merge-base
612605
// because this protects against edge cases were there is no valid fork-point
613606

614-
$firstCommitHash = StringUtil::splitLines($this->processHelper->runCommand(
615-
[
616-
'git',
617-
'--no-pager',
618-
'log',
619-
'--oneline',
620-
'--no-color',
621-
'--format=%H',
622-
'--reverse',
623-
$base.'..'.$branchName,
624-
]
625-
))[0];
607+
$firstCommitHash = StringUtil::splitLines($this->processHelper->runCommand([
608+
'git',
609+
'--no-pager',
610+
'log',
611+
'--oneline',
612+
'--no-color',
613+
'--format=%H',
614+
'--reverse',
615+
$base.'..'.$branchName,
616+
]))[0];
617+
618+
$currentBaseHeadCommit = $this->processHelper->runCommand(['git', 'rev-parse', $base]);
619+
$lastKnownCommonCommit = $this->processHelper->runCommand(['git', 'merge-base', '--fork-point', $base, $branchName]);
620+
621+
if ($currentBaseHeadCommit !== $lastKnownCommonCommit) {
622+
throw new MergeWorkflowException(sprintf('Failed while trying to perform merge against "%s", history is out of sync.', $base));
623+
}
626624

627625
// 0=author anything higher then 0 is the full body
628626
$commitData = StringUtil::splitLines(
@@ -643,23 +641,7 @@ public function squashCommits($base, $branchName, $ignoreMultipleAuthors = false
643641
$message = implode("\n", $commitData);
644642

645643
$this->reset($base);
646-
$this->commit(
647-
$message,
648-
[
649-
'a',
650-
'-author' => $author,
651-
]
652-
);
653-
654-
try {
655-
// Ensure squashed commits don't introduce regressions at base branch
656-
$this->processHelper->runCommand(['git', 'pull', '--rebase', $base]);
657-
} catch (\Exception $e) {
658-
// Error, abort the rebase process
659-
$this->processHelper->runCommand(['git', 'rebase', '--abort'], true);
660-
661-
throw new \RuntimeException(sprintf('Git rebase failed while trying to synchronize history against "%s".', $base), 0, $e);
662-
}
644+
$this->commit($message, ['all', 'author' => $author]);
663645
}
664646

665647
public function syncWithRemote($remote, $branchName = null)
@@ -693,17 +675,25 @@ public function commit($message, array $options = [])
693675

694676
foreach ($options as $option => $value) {
695677
if (is_int($option)) {
696-
$params[] = '-'.$value;
678+
if (1 === strlen($value)) {
679+
$params[] = '-'.$value;
680+
} else {
681+
$params[] = '--'.$value;
682+
}
697683
} else {
698-
$params[] = '-'.$option;
684+
if (1 === strlen($option)) {
685+
$params[] = '-'.$option;
686+
} else {
687+
$params[] = '--'.$option;
688+
}
699689
$params[] = $value;
700690
}
701691
}
702692

703693
$tmpName = $this->filesystemHelper->newTempFilename();
704694
file_put_contents($tmpName, $message);
705695

706-
$this->processHelper->runCommand(array_merge(['git', 'commit', '-F', $tmpName], $params));
696+
$this->processHelper->runCommand(array_merge(['git', 'commit', '--file', $tmpName], $params));
707697
}
708698

709699
/**

tests/Helper/GitHelperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public function merges_remote_branch_in_clean_wc()
176176
'allow_failures' => false,
177177
],
178178
[
179-
'line' => ['git', 'commit', '-F', $tmpName],
179+
'line' => ['git', 'commit', '--file', $tmpName],
180180
'allow_failures' => false,
181181
],
182182
]

0 commit comments

Comments
 (0)