Skip to content

Commit

Permalink
Allow checkout branch from private fork (#394)
Browse files Browse the repository at this point in the history
* Try fetching by sha if ref fails

* Add test

* fix linting
  • Loading branch information
ddlan authored Sep 22, 2023
1 parent 47514bd commit 5677857
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/stages/switchBranch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ export const checkoutRef = async (
} catch (err) {
console.warn('Error fetching git repository', err);
}
await exec(`git checkout ${ref.ref} -f`);
try {
await exec(`git checkout ${ref.ref} -f`);
} catch {
await exec(`git checkout ${ref.sha} -f`);
}
}
};

Expand Down
17 changes: 17 additions & 0 deletions tests/stages/switchBranch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,23 @@ describe('checkoutRef', () => {

expect(exec).toBeCalledWith('git checkout hello -f');
});

it('should try git checkout by sha if ref fails', async () => {
(exec as jest.Mock<any, any>).mockImplementation((command) => {
if (command === 'git checkout hello -f') throw 0;
});

await checkoutRef(
{
ref: 'hello',
sha: '123456',
} as GithubRef,
'remote-1',
'branch-1'
);

expect(exec).toHaveBeenNthCalledWith(3, 'git checkout 123456 -f');
});
});

describe('getCurrentBranch', () => {
Expand Down

0 comments on commit 5677857

Please sign in to comment.