Skip to content

Commit 671f855

Browse files
authored
fix: go releases failing on non-default branches (#178)
When trying to release to a non-default branch, Go releases fail because during checkout, jsii-release tries to run `git checkout -B <branch>` (so that it creates the branch if it doesn't exist). The problem is that if the branch does exist in the remote but it is not the default, git creates the branch at origin/HEAD instead of at origin/<branch>. To fix this we check if the branch exists in the remote, and if so just run "git checkout" without -b (which will automatically track the correct branch). Example failure run: https://github.com/cdk8s-team/cdk8s-plus/runs/3657009630?check_suite_focus=true#step:4:40
1 parent 2a9d589 commit 671f855

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/help/git.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,18 @@ export function push(ref: string) {
8787
* @param options options.
8888
*/
8989
export function checkout(branch: string, options: { createIfMissing?: boolean } ) {
90-
const flags = [];
91-
if (options.createIfMissing) { flags.push('-B'); }
92-
shell.run(`git checkout${` ${flags.join(' ')}`} ${branch}`);
90+
if (options.createIfMissing) {
91+
try {
92+
shell.run(`git show-branch origin/${branch}`);
93+
} catch (e) {
94+
if (e instanceof Error && e.message.includes('fatal: bad sha1 reference')) {
95+
console.log('Remote branch not found, creating new branch.');
96+
shell.run(`git checkout -B ${branch}`);
97+
return;
98+
}
99+
}
100+
}
101+
shell.run(`git checkout ${branch}`);
93102
}
94103

95104
/**

test/targets/go.test.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ function initRepo(repoDir: string, postInit?: (repoDir: string) => void) {
2323
}
2424
}
2525

26-
function createReleaser(fixture: string, props: Omit<GoReleaserProps, 'dir' | 'dryRun'> = {}, initializers: Initializers = {}) {
26+
function createReleaser(
27+
fixture: string,
28+
props: Omit<GoReleaserProps, 'dir' | 'dryRun'> = {},
29+
initializers: Initializers = {},
30+
) {
2731

2832
const fixturePath = path.join(__dirname, '..', '__fixtures__', fixture);
2933
const sourceDir = path.join(os.mkdtempSync(), fixture);
@@ -45,6 +49,15 @@ function createReleaser(fixture: string, props: Omit<GoReleaserProps, 'dir' | 'd
4549
initRepo(targetDir, initializers.postInit);
4650
};
4751

52+
(git as any).checkout = function(branch: string, options: { createIfMissing?: boolean }) {
53+
// skip logic for comparing against remote since we don't have one
54+
if (options.createIfMissing) {
55+
shell.run(`git checkout -B ${branch}`);
56+
} else {
57+
shell.run(`git checkout ${branch}`);
58+
}
59+
};
60+
4861
return { releaser, sourceDir };
4962
}
5063

@@ -267,4 +280,18 @@ test('creates missing tags only', () => {
267280
const release = releaser.release();
268281
expect(release.tags).toEqual(['module1/v1.1.0']);
269282

270-
});
283+
});
284+
285+
test('releases on separate branch', () => {
286+
287+
const { releaser, sourceDir } = createReleaser('top-level', {
288+
branch: 'boo',
289+
});
290+
291+
fs.writeFileSync(path.join(sourceDir, 'file'), 'test');
292+
const release = releaser.release();
293+
294+
expect(release.tags).toEqual(['v1.1.0']);
295+
expect(release.commitMessage).toEqual('chore(release): v1.1.0');
296+
297+
});

0 commit comments

Comments
 (0)