@jjdenhertog/ai-driven-development
Version:
AI-driven development workflow with learning capabilities for Claude
96 lines • 5.03 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkBranchMerged = checkBranchMerged;
/* eslint-disable max-depth */
const getGitInstance_1 = require("./getGitInstance");
const logger_1 = require("../logger");
function checkBranchMerged(branchName, targetBranch) {
return __awaiter(this, void 0, void 0, function* () {
try {
const git = (0, getGitInstance_1.getGitInstance)();
// First, fetch to ensure we have the latest remote information
yield git.fetch(['--prune']);
// Check if the branch exists on remote
const remoteBranches = yield git.branch(['-r']);
const remoteNames = remoteBranches.all.map(b => b.replace(/^origin\//, ''));
if (remoteNames.includes(branchName)) {
// Branch exists, check if it's merged
const remoteBranchRef = `origin/${branchName}`;
const remoteTargetRef = `origin/${targetBranch}`;
// Get the merge base between the remote branch and remote target branch
const mergeBase = yield git.raw(['merge-base', remoteBranchRef, remoteTargetRef]);
// Get the latest commit of the remote branch
const branchCommit = yield git.raw(['rev-parse', remoteBranchRef]);
// If merge base equals branch commit, the branch has been fully merged
const isMerged = mergeBase.trim() === branchCommit.trim();
(0, logger_1.log)(`Branch ${branchName} ${isMerged ? 'is' : 'is not'} merged into ${targetBranch}`, 'warn');
return isMerged;
}
// Branch doesn't exist on remote, check if it was merged and deleted
(0, logger_1.log)(`Branch ${branchName} not found on remote, checking merge history`, 'warn');
// Look for merge commits in target branch that reference this branch
const mergeCommits = yield git.raw([
'log',
`origin/${targetBranch}`,
'--merges',
'--grep', branchName,
'--pretty=format:%H %s',
'-n', '50'
]);
if (mergeCommits.trim()) {
// Found merge commits that might reference this branch
const lines = mergeCommits.trim().split('\n');
for (const line of lines) {
// Check if the merge commit message contains the branch name
// This is a heuristic but works for standard GitHub/GitLab merge messages
if (line.toLowerCase().includes(branchName.toLowerCase())) {
(0, logger_1.log)(`Found merge commit for deleted branch ${branchName}`, 'warn');
return true;
}
}
}
// Also check using our improved getCommits approach - if we can find commits, it was merged
try {
const checkMerge = yield git.raw([
'rev-list',
'--all',
'--merges',
'--pretty=format:%H %P',
'--grep', branchName,
'-n', '10'
]);
if (checkMerge.trim()) {
const lines = checkMerge.trim().split('\n');
for (const line of lines) {
if (line.startsWith('commit '))
continue;
const parts = line.split(' ');
if (parts.length >= 3) {
(0, logger_1.log)(`Found merge evidence for deleted branch ${branchName}`, 'warn');
return true;
}
}
}
}
catch (_a) {
// Ignore errors in this check
}
(0, logger_1.log)(`Branch ${branchName} is not merged into ${targetBranch}`, 'warn');
return false;
}
catch (error) {
(0, logger_1.log)(`Error checking if branch ${branchName} is merged: ${error instanceof Error ? error.message : String(error)}`, 'error');
return false;
}
});
}
//# sourceMappingURL=checkBranchMerged.js.map