From 55dc71c4d543bafab25c0904511861bd364bf379 Mon Sep 17 00:00:00 2001 From: Doron Pearl Date: Thu, 17 Jul 2025 09:24:15 -0400 Subject: [PATCH 1/2] add compare with master --- package-lock.json | 4 +-- package.json | 15 +++++++++++ src/cmd/compareWithBranch.ts | 52 +++++++++++++++++++++++++++++------- src/cmd/compareWithMaster.ts | 31 +++++++++++++++++++++ src/extension.ts | 5 ++-- 5 files changed, 94 insertions(+), 13 deletions(-) create mode 100644 src/cmd/compareWithMaster.ts diff --git a/package-lock.json b/package-lock.json index 66934d7..084a258 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "git-differ", - "version": "0.2.3", + "version": "1.0.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "git-differ", - "version": "0.2.3", + "version": "1.0.0", "license": "MIT", "devDependencies": { "@commitlint/cli": "^19.5.0", diff --git a/package.json b/package.json index 8a74624..937ce3e 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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": [ @@ -55,6 +65,11 @@ "command": "git-differ.compareWithCommit", "group": "Git@15", "when": "true" + }, + { + "command": "git-differ.compareWithMaster", + "group": "Git@15", + "when": "true" } ] } diff --git a/src/cmd/compareWithBranch.ts b/src/cmd/compareWithBranch.ts index ffc8c22..253036c 100644 --- a/src/cmd/compareWithBranch.ts +++ b/src/cmd/compareWithBranch.ts @@ -9,6 +9,44 @@ type Branch = { remote?: string; }; +// Shared helper function to execute diff comparison +export async function executeDiffComparison( + gitApi: API, + uri: vscode.Uri, + branchName: string +): Promise { + const gitUri = gitApi.toGitUri(uri, branchName); + const filePath = uri.path.split("/").pop() || uri.path; + + await vscode.commands.executeCommand("vscode.diff", gitUri, uri, `${branchName} ↔ ${filePath}`); +} + +// Helper function to find master or main branch automatically +export async function findMasterBranch(gitApi: API, uri: vscode.Uri): Promise { + try { + // Get all local branches first + const localBranches = await getBranches(gitApi, uri, false); + + // Look for master or main branch (in that order of preference) + let masterBranch = localBranches.find((branch) => branch.name === "master"); + if (!masterBranch) { + masterBranch = localBranches.find((branch) => branch.name === "main"); + } + + if (!masterBranch) { + // If no local master/main, try remote branches + 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", @@ -47,15 +85,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}`); + } } ); } diff --git a/src/cmd/compareWithMaster.ts b/src/cmd/compareWithMaster.ts new file mode 100644 index 0000000..a3f34c9 --- /dev/null +++ b/src/cmd/compareWithMaster.ts @@ -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}`); + } + } + ); +} diff --git a/src/extension.ts b/src/extension.ts index 52fa2ed..2c5f6e7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,6 +1,7 @@ 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) { @@ -8,10 +9,10 @@ export function activate(context: vscode.ExtensionContext) { 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() {} From 3dbd9874ae5d28d4348f70f03715b1dd44a08518 Mon Sep 17 00:00:00 2001 From: Doron Pearl Date: Thu, 17 Jul 2025 09:25:58 -0400 Subject: [PATCH 2/2] -comments --- src/cmd/compareWithBranch.ts | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/cmd/compareWithBranch.ts b/src/cmd/compareWithBranch.ts index 253036c..a83a428 100644 --- a/src/cmd/compareWithBranch.ts +++ b/src/cmd/compareWithBranch.ts @@ -9,7 +9,6 @@ type Branch = { remote?: string; }; -// Shared helper function to execute diff comparison export async function executeDiffComparison( gitApi: API, uri: vscode.Uri, @@ -21,20 +20,16 @@ export async function executeDiffComparison( await vscode.commands.executeCommand("vscode.diff", gitUri, uri, `${branchName} ↔ ${filePath}`); } -// Helper function to find master or main branch automatically export async function findMasterBranch(gitApi: API, uri: vscode.Uri): Promise { try { - // Get all local branches first const localBranches = await getBranches(gitApi, uri, false); - // Look for master or main branch (in that order of preference) let masterBranch = localBranches.find((branch) => branch.name === "master"); if (!masterBranch) { masterBranch = localBranches.find((branch) => branch.name === "main"); } if (!masterBranch) { - // If no local master/main, try remote branches const remoteBranches = await getBranches(gitApi, uri, true); masterBranch = remoteBranches.find( (branch) => branch.name === "origin/master" || branch.name === "origin/main"