Ask Your Question

Revision history [back]

click to hide/show revision 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:

  1. 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!);
    
  2. 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;
    
  3. Use the Git.diffWith method to get the difference between the two commit trees:

    const diff = await git.diffWith(branchA!.hash, branchB!.hash);
    
  4. 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.