Skip to content

Commit d987239

Browse files
committed
fix: improve commit behavior selection logic
1 parent 5ecf1b0 commit d987239

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

web/main.ts

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,7 @@ class GitGraphView {
839839
this.selectedCommits.clear();
840840
}
841841

842+
842843
private selectCommitRange(fromHash: string, toHash: string) {
843844
const fromIndex = this.commitLookup[fromHash];
844845
const toIndex = this.commitLookup[toHash];
@@ -887,7 +888,9 @@ class GitGraphView {
887888
}
888889

889890
private areSelectedCommitsOnCurrentBranch(): boolean {
890-
if (this.selectedCommits.size === 0 || !this.gitBranchHead) return false;
891+
if (this.selectedCommits.size === 0 || !this.gitBranchHead) {
892+
return false;
893+
}
891894

892895
// Find the commit that the current branch points to
893896
let currentBranchCommitIndex = -1;
@@ -898,18 +901,39 @@ class GitGraphView {
898901
}
899902
}
900903

901-
if (currentBranchCommitIndex === -1) return false;
904+
if (currentBranchCommitIndex === -1) {
905+
return false;
906+
}
902907

903-
for (const hash of Array.from(this.selectedCommits)) {
904-
const index = this.commitLookup[hash];
905-
if (index < currentBranchCommitIndex) {
906-
const commit = this.commits[index];
907-
if (!commit.heads || !commit.heads.includes(this.gitBranchHead)) {
908-
return false;
908+
// Build a set of all commits that are ancestors of the current branch head
909+
const branchCommits = new Set<string>();
910+
const queue = [currentBranchCommitIndex];
911+
const visited = new Set<number>();
912+
913+
while (queue.length > 0) {
914+
const index = queue.shift()!;
915+
if (visited.has(index)) continue;
916+
visited.add(index);
917+
918+
const commit = this.commits[index];
919+
branchCommits.add(commit.hash);
920+
921+
// Add parent commits to the queue
922+
for (const parentHash of commit.parents) {
923+
const parentIndex = this.commitLookup[parentHash];
924+
if (parentIndex !== undefined && !visited.has(parentIndex)) {
925+
queue.push(parentIndex);
909926
}
910927
}
911928
}
912929

930+
// Check if all selected commits are in the branch
931+
for (const hash of Array.from(this.selectedCommits)) {
932+
if (!branchCommits.has(hash)) {
933+
return false;
934+
}
935+
}
936+
913937
return true;
914938
}
915939

@@ -1489,7 +1513,7 @@ class GitGraphView {
14891513
}
14901514
}, {
14911515
title: 'Edit Message' + ELLIPSIS,
1492-
visible: visibility.editMessage,
1516+
visible: visibility.editMessage && this.areSelectedCommitsOnCurrentBranch(),
14931517
onClick: () => this.editCommitMessageAction(target)
14941518
}, {
14951519
title: 'Reset Last Commit' + ELLIPSIS,
@@ -2612,7 +2636,9 @@ class GitGraphView {
26122636
if (this.expandedCommit.commitHash === commit.hash) {
26132637
this.closeCommitDetails(true);
26142638
} else {
2639+
// Regular click with details open: select only this commit and load details
26152640
this.clearCommitSelection();
2641+
this.toggleCommitSelection(commit.hash, eventElem);
26162642
this.loadCommitDetails(eventElem);
26172643
}
26182644
} else {
@@ -2711,6 +2737,19 @@ class GitGraphView {
27112737
const commit = this.getCommitOfElem(eventElem);
27122738
if (commit === null) return;
27132739

2740+
// Close any open commit details to avoid visual confusion
2741+
// The commitDetailsOpen class can make commits appear selected
2742+
if (this.expandedCommit !== null && this.expandedCommit.commitHash !== commit.hash) {
2743+
this.closeCommitDetails(false);
2744+
}
2745+
2746+
// Only clear and select if the commit is not already selected
2747+
if (!this.selectedCommits.has(commit.hash)) {
2748+
this.clearCommitSelection();
2749+
this.toggleCommitSelection(commit.hash, eventElem);
2750+
}
2751+
2752+
// If the commit is already selected, keep the current selection for multi-select context menu
27142753
const target: ContextMenuTarget & DialogTarget & CommitTarget = {
27152754
type: TargetType.Commit,
27162755
hash: commit.hash,

web/styles/main.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -723,10 +723,11 @@ svg.openFolderIcon, svg.closedFolderIcon, svg.fileIcon{
723723
background-color:rgba(128,128,128,0.15);
724724
}
725725
#commitTable tr.commit.commitDetailsOpen{
726-
background-color:rgba(128,128,128,0.25);
726+
background-color:rgba(0,123,255,0.2);
727+
border-left:3px solid var(--vscode-focusBorder, #007ACC);
727728
}
728729
#commitTable tr.commit.commitDetailsOpen:hover, #commitTable tr.commit.commitDetailsOpen.contextMenuActive, #commitTable tr.commit.commitDetailsOpen.dialogActive, #commitTable tr.commit.commitDetailsOpen.graphVertexActive{
729-
background-color:rgba(128,128,128,0.35);
730+
background-color:rgba(0,123,255,0.3);
730731
}
731732

732733
#commitTable tr.commit.findCurrentCommit {

0 commit comments

Comments
 (0)