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

Commit e2e814b

Browse files
authored
Merge pull request #14 from groupon/dbushong/feature/master/squash-fudge
let squash merge detection be fuzzier
2 parents 01b0b2d + 3611eaf commit e2e814b

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

lib/commands/done.js

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
'use strict';
3434

35+
const debug = require('debug')('workflow:done');
36+
3537
const {
3638
featureParent,
3739
yesNo,
@@ -40,13 +42,21 @@ const {
4042
inferRemote,
4143
} = require('../common');
4244

43-
/**
44-
* @param {string} diff
45-
*/
45+
/** @param {string} diff */
4646
function stripIndexLines(diff) {
4747
return diff.replace(/^index .*\n/gm, '');
4848
}
4949

50+
/** @param {string} diff */
51+
function stripLineNums(diff) {
52+
return diff.replace(/^@@ .*\n/gm, '');
53+
}
54+
55+
/** @param {{ message: string, hash: string }} commit */
56+
function commitDescr({ message, hash }) {
57+
return `[${hash.slice(0, 7)}] ${message}`;
58+
}
59+
5060
/**
5161
*
5262
* @param {import('simple-git/promise').SimpleGit} git
@@ -59,13 +69,33 @@ async function findSquashedDiff(git, feature, parent) {
5969
const diff = stripIndexLines(await git.diff([`${base}..${feature}`]));
6070

6171
const { all: commits } = await git.log({ from: base, to: parent });
62-
for (const { hash, message } of commits) {
63-
const show = await git.show([hash]);
72+
73+
/** @type {Map<typeof commits[0], string>} */
74+
const cachedCommitDiffs = new Map();
75+
76+
for (const commit of commits) {
77+
const show = await git.show([commit.hash]);
6478
// strip off the commit msg at the beginning of the show
6579
const commitDiff = stripIndexLines(show).replace(/^[\s\S]+?\ndiff/, 'diff');
66-
if (commitDiff === diff) return `[${hash.slice(0, 7)}] ${message}`;
80+
if (commitDiff === diff) return commitDescr(commit);
81+
cachedCommitDiffs.set(commit, commitDiff);
6782
}
6883

84+
debug("Couldn't find squashed diff on exact match; fudging line numbers");
85+
const strippedDiff = stripLineNums(diff);
86+
87+
for (const commit of commits) {
88+
let commitDiff = cachedCommitDiffs.get(commit);
89+
if (!commitDiff) {
90+
throw new Error(`Couldn't find cached commit diff for ${commit.hash}`);
91+
}
92+
commitDiff = stripLineNums(commitDiff);
93+
if (commitDiff === strippedDiff) return commitDescr(commit);
94+
}
95+
96+
debug('Failed to find squashed diff match for:');
97+
debug(strippedDiff);
98+
6999
return null;
70100
}
71101

0 commit comments

Comments
 (0)