Skip to content

Commit 38edc9b

Browse files
committed
feat: git graph & branchName
1 parent 1611423 commit 38edc9b

File tree

11 files changed

+369
-248
lines changed

11 files changed

+369
-248
lines changed

demo2.html

Lines changed: 0 additions & 20 deletions
This file was deleted.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
"pack": "vsce package",
121121
"test": "vitest",
122122
"typecheck": "tsc --noEmit",
123-
"release": "bumpp && nr publish"
123+
"release": "nr prepare && bumpp && nr publish"
124124
},
125125
"dependencies": {
126126
"simple-git": "^3.27.0",

src/git/index.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createSingletonComposable, useWorkspaceFolders } from 'reactive-vscode'
33

44
import type { SimpleGit } from 'simple-git'
55
import type { Commit, CommitGraph, ExtendedLogResult, GitOperation } from './types'
6-
import { logger } from '@/utils'
6+
import { getBranchColor, logger } from '@/utils'
77

88
export * from './types'
99

@@ -44,6 +44,7 @@ export const useGitService = createSingletonComposable(() => {
4444
const { author_email, author_name } = commit
4545
commit.authorEmail = author_email
4646
commit.authorName = author_name
47+
4748
commit.children = []
4849
hashToCommit[commit.hash] = commit
4950

@@ -195,11 +196,15 @@ export const useGitService = createSingletonComposable(() => {
195196

196197
if (isMerge) {
197198
const sourceBranches: string[] = []
199+
const sourceBranchColors: Record<string, string> = {}
200+
198201
if (commit.parents) {
199202
for (let i = 1; i < commit.parents.length; i++) {
200-
const parentHash = commit.parents[i]
203+
const parentHash: string = commit.parents[i]
201204
if (commitToBranch[parentHash]) {
202-
sourceBranches.push(commitToBranch[parentHash])
205+
const sourceBranch = commitToBranch[parentHash]
206+
sourceBranches.push(sourceBranch)
207+
sourceBranchColors[sourceBranch] = getBranchColor(sourceBranch)
203208
}
204209
}
205210
}
@@ -212,15 +217,32 @@ export const useGitService = createSingletonComposable(() => {
212217
sourceBranches,
213218
targetBranch: branch,
214219
branchChanged: branchChanged[commit.hash],
220+
branchColor: getBranchColor(branch),
221+
targetBranchColor: getBranchColor(branch),
222+
sourceBranchColors,
215223
}
216224
}
217225
else {
226+
let targetBranch: string | undefined
227+
let targetBranchColor: string | undefined
228+
229+
if (branchChanged[commit.hash] && commit.parents && commit.parents.length > 0) {
230+
const parentHash = commit.parents[0]
231+
if (commitToBranch[parentHash] && commitToBranch[parentHash] !== branch) {
232+
targetBranch = commitToBranch[parentHash]
233+
targetBranchColor = getBranchColor(targetBranch)
234+
}
235+
}
236+
218237
return {
219238
type: 'commit',
220239
branch,
221240
hash: commit.hash,
222241
message: commit.message,
223242
branchChanged: branchChanged[commit.hash],
243+
branchColor: getBranchColor(branch),
244+
targetBranch,
245+
targetBranchColor,
224246
}
225247
}
226248
})

src/git/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ export interface BaseOperation {
2626
hash: string
2727
message: string
2828
branchChanged: boolean
29+
branchColor?: string
2930
}
3031

3132
export interface CommitOperation extends BaseOperation {
3233
sourceBranches?: string[]
3334
targetBranch?: string
35+
sourceBranchColors?: Record<string, string>
36+
targetBranchColor?: string
3437
}
3538

3639
export type GitOperation = CommitOperation

src/utils.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import type { Uri } from 'vscode'
55

66
import { displayName } from './generated/meta'
77

8+
const branchColorCache = new Map<string, string>()
9+
let colorCounter = 0
10+
const goldenRatio = 0.618033988749895
11+
812
export const logger = useLogger(displayName)
913

1014
export async function getGitPath() {
@@ -49,3 +53,32 @@ export function parseGitStatus(status: string): { type: string, similarity?: num
4953
}
5054
return { type: status }
5155
}
56+
57+
/**
58+
* @param branchName
59+
* @returns HSL color string
60+
*/
61+
export function getBranchColor(branchName: string): string {
62+
if (branchColorCache.has(branchName)) {
63+
return branchColorCache.get(branchName)!
64+
}
65+
66+
let hash = 0
67+
for (let i = 0; i < branchName.length; i++) {
68+
hash = branchName.charCodeAt(i) + ((hash << 5) - hash)
69+
}
70+
71+
const baseHue = Math.abs(hash) % 360
72+
73+
const hue = (baseHue + colorCounter * goldenRatio * 360) % 360
74+
colorCounter++
75+
76+
const saturation = 75
77+
const lightness = 50
78+
79+
const color = `hsl(${Math.round(hue)}, ${saturation}%, ${lightness}%)`
80+
81+
branchColorCache.set(branchName, color)
82+
83+
return color
84+
}

src/views/history/App.vue

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ const transformedCommits = computed(() => {
6262
<input v-model="filter" type="text" placeholder="Search commits..." class="search-input">
6363
</div> -->
6464

65-
<CommitTable v-model="selectedCommitHashes" :commits="transformedCommits" :graph-data="commits?.operations || []" class="git-graph-container" />
65+
<CommitTable
66+
v-model="selectedCommitHashes"
67+
:commits="transformedCommits"
68+
:graph-data="commits?.operations || []"
69+
class="git-graph-container"
70+
/>
6671

6772
<div v-if="error" class="error">
6873
{{ error }}

src/views/history/components/CommitTable/ColumnHeader.vue

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ResizeHandle from '../ResizeHandle/index.vue'
44
55
interface ColumnWidths {
66
branch: number
7+
branchName: number
78
hash: number
89
message: number
910
stats: number
@@ -67,10 +68,14 @@ function handleDragEnd() {
6768

6869
<template>
6970
<li class="commit-header">
70-
<!-- <span class="column-header" :style="{ width: `${modelValue?.branch}px` }">
71-
Branch
71+
<span class="column-header" :style="{ width: `${modelValue?.branchName}px` }">
72+
Branch/Tag
73+
<ResizeHandle :is-active="currentColumn === 'branchName'" @mousedown="handleDragStart($event, 'branchName')" />
74+
</span>
75+
<span class="column-header" :style="{ width: `${modelValue?.branch}px` }">
76+
Graph
7277
<ResizeHandle :is-active="currentColumn === 'branch'" @mousedown="handleDragStart($event, 'branch')" />
73-
</span> -->
78+
</span>
7479
<span class="hash-col column-header" :style="{ width: `${modelValue?.hash}px` }">
7580
CommitId
7681
<ResizeHandle :is-active="currentColumn === 'hash'" @mousedown="handleDragStart($event, 'hash')" />

0 commit comments

Comments
 (0)