Skip to content

Commit

Permalink
fix: get remote branches correctly when there are multiple origins (#974
Browse files Browse the repository at this point in the history
)

* fix: get remote branches correctly when there are multiple origins

* chore: make it throw in case origin is falsey
  • Loading branch information
eunjae-lee committed Feb 16, 2022
1 parent a526798 commit cbfe80d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
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);
}

0 comments on commit cbfe80d

Please sign in to comment.