Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

feat: git wf done detects squash commits #12

Merged
merged 4 commits into from
Oct 29, 2020
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
56 changes: 45 additions & 11 deletions lib/commands/done.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@ const {
inferRemote,
} = require('../common');

/**
* @param {string} diff
*/
function stripIndexLines(diff) {
return diff.replace(/^index .*\n/gm, '');
}

/**
*
* @param {import('simple-git/promise').SimpleGit} git
* @param {string} feature
* @param {string} parent
*/
async function findSquashedDiff(git, feature, parent) {
// collect the combined set of diffs on the feature branch
const base = (await git.raw(['merge-base', parent, feature])).trim();
const diff = stripIndexLines(await git.diff([`${base}..${feature}`]));

const { all: commits } = await git.log({ from: base, to: parent });
for (const { hash, message } of commits) {
const show = await git.show([hash]);
// strip off the commit msg at the beginning of the show
const commitDiff = stripIndexLines(show).replace(/^[\s\S]+?\ndiff/, 'diff');
if (commitDiff === diff) return `[${hash.slice(0, 7)}] ${message}`;
}

return null;
}

/** @type {import('../typedefs').ActionFn} */
async function doneAction({ deps: { git, log, forceBool } }) {
const { current: feature } = await git.branchLocal();
Expand All @@ -54,18 +83,23 @@ async function doneAction({ deps: { git, log, forceBool } }) {

const unmerged = (await git.log([`..${feature}`])).total;
if (unmerged > 0) {
const ok = await yesNo(
`Feature branch '${feature}' contains ${plural(
unmerged,
'commit'
)} not present in '${parent}'. Delete anyway?`,
false,
forceBool
);
if (!ok) {
throw new UIError(
`Refusing to cleanup unmerged feature branch '${feature}'`
const squashMsg = await findSquashedDiff(git, feature, parent);
if (squashMsg) {
log(`Found squashed commit in ${parent}: ${squashMsg}`);
} else {
const ok = await yesNo(
`Feature branch '${feature}' contains ${plural(
unmerged,
'commit'
)} not present in '${parent}'. Delete anyway?`,
false,
forceBool
);
if (!ok) {
throw new UIError(
`Refusing to cleanup unmerged feature branch '${feature}'`
);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/commands/merge-back.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ async function tryMerge(deps, from, to, main) {
try {
// https://github.com/steveukx/git-js/issues/204
const output = await git.merge([from]);
if (/\nCONFLICT /.test(output)) throw new Error(output);
if (output.failed)
throw new Error(output.conflicts.map(c => `${c}`).join('\n\n'));
} catch (err) {
log('Automated merge failed; creating feature branch for resolution');
await createFeatureMerge(deps, from, main); // will throw
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"commander": "^2.11.0",
"debug": "^3.0.1",
"open": "^6.3.0",
"simple-git": "^1.77.0"
"simple-git": "^1.132.0"
},
"devDependencies": {
"@types/debug": "^4.1.4",
Expand Down
20 changes: 20 additions & 0 deletions test/done.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const fs = require('fs');

const assert = require('assertive');

const addHooks = require('./test-common');
Expand All @@ -14,6 +16,24 @@ describe('done', () => {
assert.notInclude('kittens', (await t.git.branchLocal()).all);
});

it('cleans up a squash merged feature branch', async () => {
// 1. creates a feature branch and switches to it
await startAction({ deps: t, args: ['kittens'], opts: {} });

// 2. simulate a squash merge with a specific diff
await t.git.checkout('master');
fs.writeFileSync('README', 'foobar\n');
await t.git.commit('squash merge', ['README']);

// 3. simulate the same change in the feature branch
await t.git.checkout('kittens');
fs.writeFileSync('README', 'foobar\n');
await t.git.commit('my change', ['README']);

await doneAction({ deps: t });
assert.notInclude('kittens', (await t.git.branchLocal()).all);
});

it('prompts and aborts on unmerged branch', async () => {
await startAction({ deps: t, args: ['kittens'], opts: {} });
await t.changeSomething();
Expand Down