Skip to content

Commit 5677857

Browse files
authored
Allow checkout branch from private fork (#394)
* Try fetching by sha if ref fails * Add test * fix linting
1 parent 47514bd commit 5677857

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/stages/switchBranch.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,11 @@ export const checkoutRef = async (
6464
} catch (err) {
6565
console.warn('Error fetching git repository', err);
6666
}
67-
await exec(`git checkout ${ref.ref} -f`);
67+
try {
68+
await exec(`git checkout ${ref.ref} -f`);
69+
} catch {
70+
await exec(`git checkout ${ref.sha} -f`);
71+
}
6872
}
6973
};
7074

tests/stages/switchBranch.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,23 @@ describe('checkoutRef', () => {
114114

115115
expect(exec).toBeCalledWith('git checkout hello -f');
116116
});
117+
118+
it('should try git checkout by sha if ref fails', async () => {
119+
(exec as jest.Mock<any, any>).mockImplementation((command) => {
120+
if (command === 'git checkout hello -f') throw 0;
121+
});
122+
123+
await checkoutRef(
124+
{
125+
ref: 'hello',
126+
sha: '123456',
127+
} as GithubRef,
128+
'remote-1',
129+
'branch-1'
130+
);
131+
132+
expect(exec).toHaveBeenNthCalledWith(3, 'git checkout 123456 -f');
133+
});
117134
});
118135

119136
describe('getCurrentBranch', () => {

0 commit comments

Comments
 (0)