Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get remote branches correctly when there are multiple origins #974

Merged
merged 3 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,28 @@ describe('getRemoteBranches', () => {
'renovate/rollup-1.x',
]);
});

it('works with multiple origins', () => {
silentExec
.mockImplementationOnce(() => 'origin\norigin2')
.mockImplementationOnce(
() => ` origin/HEAD -> origin/master
origin/chore/all-contributors
origin/master
origin/renovate/pin-dependencies
origin/renovate/rollup-1.x
origin2/fix/something
origin2/chore/test
`
);
const result = getRemoteBranches();
expect(result).toEqual([
'chore/all-contributors',
'master',
'renovate/pin-dependencies',
'renovate/rollup-1.x',
'fix/something',
'chore/test',
]);
});
});
12 changes: 9 additions & 3 deletions packages/shipjs-lib/src/lib/git/getRemoteBranches.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ $ git branch -r
*/

export default function getRemoteBranches(dir = '.') {
const remote = silentExec('git remote', { dir }).toString().trim();
const origins = silentExec('git remote', { dir })
.toString()
.trim()
.split('\n');
return silentExec('git branch -r', { dir })
.toString()
.trim()
.split('\n')
.map((line) => line.trim())
.filter(Boolean)
.filter((line) => !line.includes(' -> '))
.map((line) => line.slice(remote.length + 1));
.map((line) => {
const origin = origins.find((_origin) => line.startsWith(`${_origin}/`));
return line.slice(origin.length + 1);
})
.filter(Boolean);
}