Skip to content

Commit 1c92bf8

Browse files
authored
fix: support branch pull for detached primaries (#696)
1 parent 0ab1d86 commit 1c92bf8

3 files changed

Lines changed: 30 additions & 5 deletions

File tree

inc/Abilities/WorkspaceAbilities.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,14 @@ private function registerAbilities(): void {
841841
'type' => 'boolean',
842842
'description' => 'Permit mutation on the primary checkout (default false). Worktrees are always allowed.',
843843
),
844+
'remote' => array(
845+
'type' => 'string',
846+
'description' => 'Remote name for pull when branch is supplied (default origin).',
847+
),
848+
'branch' => array(
849+
'type' => 'string',
850+
'description' => 'Remote branch to pull. Useful when the checkout is detached.',
851+
),
844852
),
845853
'required' => array( 'name' ),
846854
),
@@ -2991,7 +2999,9 @@ public static function gitPull( array $input ): array|\WP_Error {
29912999
return $workspace->git_pull(
29923000
$input['name'] ?? '',
29933001
! empty($input['allow_dirty']),
2994-
! empty($input['allow_primary_mutation'])
3002+
! empty($input['allow_primary_mutation']),
3003+
(string) ( $input['remote'] ?? 'origin' ),
3004+
isset($input['branch']) ? (string) $input['branch'] : null
29953005
);
29963006
}
29973007

inc/Cli/Commands/WorkspaceCommand.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,10 +2554,10 @@ private function resolveAtFile( string $value ): string {
25542554
* : Permit mutating ops (pull/add/commit/push) on the primary checkout. Default-deny — use a worktree handle (`<repo>@<branch-slug>`) instead whenever possible.
25552555
*
25562556
* [--remote=<remote>]
2557-
* : Remote name for push (default: origin).
2557+
* : Remote name for pull/push (default: origin).
25582558
*
25592559
* [--branch=<branch>]
2560-
* : Branch override for push.
2560+
* : Branch override for pull/push. For pull, supplies an explicit remote branch when the checkout is detached.
25612561
*
25622562
* [--from=<ref>]
25632563
* : From ref for diff.
@@ -2640,6 +2640,10 @@ public function git( array $args, array $assoc_args ): void {
26402640

26412641
if ( 'pull' === $operation ) {
26422642
$input['allow_dirty'] = ! empty($assoc_args['allow-dirty']);
2643+
$input['remote'] = $assoc_args['remote'] ?? 'origin';
2644+
if ( ! empty($assoc_args['branch']) ) {
2645+
$input['branch'] = (string) $assoc_args['branch'];
2646+
}
26432647
}
26442648

26452649
if ( 'add' === $operation ) {

inc/Workspace/WorkspaceGitOperations.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function git_status( string $handle ): array|\WP_Error {
7777
* @param bool $allow_dirty Allow pull with dirty working tree.
7878
* @return array
7979
*/
80-
public function git_pull( string $handle, bool $allow_dirty = false, bool $allow_primary_mutation = false ): array|\WP_Error {
80+
public function git_pull( string $handle, bool $allow_dirty = false, bool $allow_primary_mutation = false, string $remote = 'origin', ?string $branch = null ): array|\WP_Error {
8181
if ( WorkspaceAliasResolver::is_context_repository($handle) ) {
8282
return WorkspaceAliasResolver::mutation_error($handle, 'git pull');
8383
}
@@ -107,7 +107,18 @@ public function git_pull( string $handle, bool $allow_dirty = false, bool $allow
107107
return new \WP_Error('dirty_working_tree', 'Working tree is dirty. Commit/stash changes first or pass allow_dirty=true.', array( 'status' => 400 ));
108108
}
109109

110-
$result = $this->run_git($repo_path, 'pull --ff-only');
110+
$remote = trim($remote);
111+
$branch = null !== $branch ? trim($branch) : null;
112+
if ( '' === $remote ) {
113+
$remote = 'origin';
114+
}
115+
116+
$command = 'pull --ff-only';
117+
if ( null !== $branch && '' !== $branch ) {
118+
$command .= ' ' . escapeshellarg($remote) . ' ' . escapeshellarg($branch);
119+
}
120+
121+
$result = $this->run_git($repo_path, $command);
111122

112123
if ( is_wp_error($result) ) {
113124
return $result;

0 commit comments

Comments
 (0)