Skip to content

Instantly share code, notes, and snippets.

@ukyo
Created July 10, 2017 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukyo/24174d78bc2d5aacea8883fcfbaf3072 to your computer and use it in GitHub Desktop.
Save ukyo/24174d78bc2d5aacea8883fcfbaf3072 to your computer and use it in GitHub Desktop.
対象のブランチの派生元commitかmerge commitのhashを表示する
const gmk = require('git-miru-kun');
(async () => {
const gitDir = await gmk.readGitDir('.git');
const parentHashDict = {};
const targetBranch = await gitDir.readHead();
const { branchName } = targetBranch;
const commits = (await gitDir.readBranches())
.filter(b => b.branchName !== branchName)
.map(b => b.commit);
let baseCommitHash;
let x = 1;
while (!baseCommitHash) {
do {
for (let i = commits.length - 1; i >= 0; i--) {
let commit = commits[i];
for (let j = 0; j < 1000; j++) {
const { parentHash } = commit;
if (!commit.hasParent() || parentHashDict[parentHash]) {
commits.splice(i, 1);
break;
}
if (commit.isMergeCommit()) {
commits.push(await commit.readMergedParent());
}
parentHashDict[parentHash] = true;
try {
commit = await commit.readParent();
} catch (e) {
commits.splice(i, 1);
break;
}
}
}
} while (commits.length > 1);
let { commit } = targetBranch;
for (let i = 0, n = x * 16; i < n; i++) {
console.log(commit.hash, commit.isMergeCommit());
if (commit.isMergeCommit()) {
baseCommitHash = commit.hash;
break;
}
if (parentHashDict[commit.parentHash]) {
baseCommitHash = commit.parentHash;
break;
}
commit = await commit.readParent();
}
x++;
}
console.log('base commit hash is ', baseCommitHash);
})();

.gitディレクトリ内のファイルからcommitを探索するやつ https://github.com/ukyo/git-miru-kun

$ npm i -g https://github.com/ukyo/git-miru-kun
$ cd path/to/repos
$ node hoge.js

まぁ、これ100%確実に発見できるわけではなくて、現実的には平気だろっていうレベルのやつです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment