1 | initial version |
To retrieve the number of modifications made between branch A and branch B in a VSCode extension, you can use the Git API provided by VSCode. Here's how to achieve this:
Get the Git Repository object representing the current workspace:
const repository = await workspace.getWorkspaceFolder(uri)?.uri.fsPath;
const gitExtension = extensions.getExtension<GitExtension>('vscode.git')!.exports;
const git = gitExtension.getAPI(1);
const repo = git.getRepository(repository!);
Get the commit hash of the two branches, A and B:
const branchA = repo.refs.find(ref => ref.name === 'refs/heads/A')?.commit;
const branchB = repo.refs.find(ref => ref.name === 'refs/heads/B')?.commit;
Use the Git.diffWith
method to get the difference between the two commit trees:
const diff = await git.diffWith(branchA!.hash, branchB!.hash);
Use the GitDiffParser
class provided by the git extension and parse the diff output to get the number of modifications:
const changes = GitDiffParser.parse(diff);
const numModifications = changes.chunks.reduce((total, chunk) => {
return total + chunk.newLines - chunk.oldLines;
}, 0);
The numModifications
variable will now hold the number of modifications made between branch A and B.