Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
"category": "Git",
"command": "git-differ.compareWithCommit",
"title": "Git Differ: Compare with Commit"
},
{
"category": "Git",
"command": "git-differ.compareWithMaster",
"title": "Git Differ: Compare with Master"
}
],
"configuration": {
Expand Down Expand Up @@ -43,6 +48,11 @@
"command": "git-differ.compareWithCommit",
"group": "Git@15",
"when": "!explorerResourceIsFolder"
},
{
"command": "git-differ.compareWithMaster",
"group": "Git@15",
"when": "!explorerResourceIsFolder"
}
],
"editor/title/context": [
Expand All @@ -55,6 +65,11 @@
"command": "git-differ.compareWithCommit",
"group": "Git@15",
"when": "true"
},
{
"command": "git-differ.compareWithMaster",
"group": "Git@15",
"when": "true"
}
]
}
Expand Down
47 changes: 38 additions & 9 deletions src/cmd/compareWithBranch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,39 @@ type Branch = {
remote?: string;
};

export async function executeDiffComparison(
gitApi: API,
uri: vscode.Uri,
branchName: string
): Promise<void> {
const gitUri = gitApi.toGitUri(uri, branchName);
const filePath = uri.path.split("/").pop() || uri.path;

await vscode.commands.executeCommand("vscode.diff", gitUri, uri, `${branchName} ↔ ${filePath}`);
}

export async function findMasterBranch(gitApi: API, uri: vscode.Uri): Promise<Branch | null> {
try {
const localBranches = await getBranches(gitApi, uri, false);

let masterBranch = localBranches.find((branch) => branch.name === "master");
if (!masterBranch) {
masterBranch = localBranches.find((branch) => branch.name === "main");
}

if (!masterBranch) {
const remoteBranches = await getBranches(gitApi, uri, true);
masterBranch = remoteBranches.find(
(branch) => branch.name === "origin/master" || branch.name === "origin/main"
);
}

return masterBranch || null;
} catch (error) {
return null;
}
}

export function newCompareWithBranch(gitApi: API): vscode.Disposable {
return vscode.commands.registerCommand(
"git-differ.compareWithBranch",
Expand Down Expand Up @@ -47,15 +80,11 @@ export function newCompareWithBranch(gitApi: API): vscode.Disposable {
return;
}

const gitUri = gitApi.toGitUri(uri, branch.name);
const filePath = uri.path.split("/").pop() || uri.path;

vscode.commands.executeCommand(
"vscode.diff",
gitUri,
uri,
`${branch.name} compared with "${filePath}"`
);
try {
await executeDiffComparison(gitApi, uri, branch.name);
} catch (error) {
vscode.window.showErrorMessage(`An error occurred: ${error}`);
}
}
);
}
Expand Down
31 changes: 31 additions & 0 deletions src/cmd/compareWithMaster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as vscode from "vscode";
import { API } from "../git";
import { findMasterBranch, executeDiffComparison } from "./compareWithBranch";

export function newCompareWithMaster(gitApi: API): vscode.Disposable {
return vscode.commands.registerCommand(
"git-differ.compareWithMaster",
async (uri: vscode.Uri | undefined) => {
if (!uri) {
uri = vscode.window.activeTextEditor?.document.uri;
}
if (!uri) {
vscode.window.showErrorMessage("No file selected");
return;
}

try {
const masterBranch = await findMasterBranch(gitApi, uri);

if (!masterBranch || !masterBranch.name) {
vscode.window.showErrorMessage("Could not find master or main branch");
return;
}

await executeDiffComparison(gitApi, uri, masterBranch.name);
} catch (error) {
vscode.window.showErrorMessage(`An error occurred: ${error}`);
}
}
);
}
5 changes: 3 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import * as vscode from "vscode";
import { newCompareWithBranch } from "./cmd/compareWithBranch";
import { newCompareWithCommit } from "./cmd/compareWithCommit";
import { newCompareWithMaster } from "./cmd/compareWithMaster";
import { API, GitExtension } from "./git";

export function activate(context: vscode.ExtensionContext) {
const gitExtension = vscode.extensions.getExtension<GitExtension>("vscode.git")!.exports;
const gitApi: API = gitExtension.getAPI(1);

const compareWithCommit = newCompareWithCommit(gitApi);

const compareWithBranch = newCompareWithBranch(gitApi);
const compareWithMaster = newCompareWithMaster(gitApi);

context.subscriptions.push(compareWithBranch, compareWithCommit);
context.subscriptions.push(compareWithBranch, compareWithCommit, compareWithMaster);
}

export function deactivate() {}