Skip to content

Commit e88b667

Browse files
committed
feat: filter by author
1 parent 9212e3f commit e88b667

File tree

5 files changed

+83
-4
lines changed

5 files changed

+83
-4
lines changed

src/constant.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
export const WEBVIEW_CHANNEL = {
33
GET_HISTORY: 'get-history',
44
GET_ALL_BRANCHES: 'get-all-branches',
5+
GET_ALL_AUTHORS: 'get-all-authors',
56
SHOW_COMMIT_DETAILS: 'show-commit-details',
67
SHOW_CHANGES_PANEL: 'show-changes-panel',
78
} as const
@@ -10,6 +11,7 @@ export const WEBVIEW_CHANNEL = {
1011
export const CHANNEL = {
1112
HISTORY: 'history',
1213
BRANCHES: 'branches',
14+
AUTHORS: 'authors',
1315
CLEAR_SELECTED: 'clear-selected',
1416
}
1517

src/git/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ export const useGitService = createSingletonComposable(() => {
3030
}
3131
}
3232

33+
// 获取所有作者
34+
async function getAllAuthors(): Promise<string[]> {
35+
try {
36+
// 使用 git log --format="%an <%ae>" 获取所有作者
37+
const result = await git.raw(['log', '--format=%an <%ae>'])
38+
const authors = Array.from(new Set(result.split('\n').map(line => line.trim()).filter(Boolean)))
39+
return authors
40+
}
41+
catch (error) {
42+
logger.error('Failed to get all authors:', error)
43+
return []
44+
}
45+
}
46+
3347
async function getHistory(filter?: GitHistoryFilter): Promise<CommitGraph> {
3448
try {
3549
const search = filter?.search?.trim()
@@ -50,6 +64,11 @@ export const useGitService = createSingletonComposable(() => {
5064
// 构建 git log 参数(添加 --stat 获取文件变更统计)
5165
const logArgs = [...branchArgs, '--stat']
5266

67+
// 支持作者筛选
68+
if (filter?.author) {
69+
logArgs.push(`--author=${filter.author}`)
70+
}
71+
5372
// 分页处理:每页45条数据
5473
const pageSize = filter?.pageSize || 45
5574
const page = filter?.page || 1
@@ -230,6 +249,7 @@ export const useGitService = createSingletonComposable(() => {
230249
getHistory,
231250
getCommitByHash,
232251
getAllBranches,
252+
getAllAuthors,
233253
getPreviousCommit,
234254
}
235255
})

src/git/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export interface CommitGraph {
4747
export interface GitHistoryFilter {
4848
search?: string
4949
branches?: string[]
50+
author?: string
5051
page?: number
5152
pageSize?: number
5253
}

src/views/history/App.vue

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ const error = ref<string>('')
2525
2626
const searchText = ref<string>('')
2727
const selectedBranch = ref<string>('') // 改为单选分支
28+
const selectedAuthor = ref<string>('') // 作者筛选
2829
const isLoading = ref<boolean>(false)
2930
const currentPage = ref<number>(1)
3031
const pageSize = ref<number>(45)
3132
const allCommits = ref<Commit[]>([]) // 累积所有已加载的提交
3233
const hasMoreData = ref<boolean>(true) // 是否还有更多数据
3334
const availableBranches = ref<string[]>([]) // 可用分支
35+
const availableAuthors = ref<string[]>([]) // 可用作者
3436
// VSCode webview API
3537
const vscode = acquireVsCodeApi<State>()
3638
window.vscode = vscode
@@ -49,6 +51,10 @@ function applyFilter(resetPage: boolean = true) {
4951
filter.branches = [selectedBranch.value]
5052
}
5153
54+
if (selectedAuthor.value) {
55+
filter.author = selectedAuthor.value
56+
}
57+
5258
if (resetPage) {
5359
currentPage.value = 1
5460
allCommits.value = []
@@ -82,6 +88,7 @@ function handleSearchKeyup(event: KeyboardEvent) {
8288
function clearFilter() {
8389
searchText.value = ''
8490
selectedBranch.value = ''
91+
selectedAuthor.value = ''
8592
currentPage.value = 1 // 重置页面
8693
allCommits.value = [] // 清空累积数据
8794
hasMoreData.value = true // 重置数据状态
@@ -102,6 +109,10 @@ watch(() => selectedBranch.value, () => {
102109
applyFilter(true)
103110
})
104111
112+
watch(() => selectedAuthor.value, () => {
113+
applyFilter(true)
114+
})
115+
105116
// Handle messages from extension
106117
window.addEventListener('message', (event: { data: any }) => {
107118
const message = event.data
@@ -130,6 +141,9 @@ window.addEventListener('message', (event: { data: any }) => {
130141
case CHANNEL.BRANCHES:
131142
availableBranches.value = message.branches || []
132143
break
144+
case CHANNEL.AUTHORS:
145+
availableAuthors.value = message.authors || []
146+
break
133147
case 'error':
134148
error.value = message.message
135149
isLoading.value = false // 出错时也停止加载状态
@@ -139,6 +153,7 @@ window.addEventListener('message', (event: { data: any }) => {
139153
140154
onMounted(() => {
141155
vscode.postMessage({ command: WEBVIEW_CHANNEL.GET_ALL_BRANCHES })
156+
vscode.postMessage({ command: WEBVIEW_CHANNEL.GET_ALL_AUTHORS })
142157
applyFilter(true)
143158
})
144159
@@ -148,7 +163,7 @@ const transformedCommits = computed(() => {
148163
149164
// 计算筛选状态
150165
const hasActiveFilter = computed(() => {
151-
return searchText.value.trim() || selectedBranch.value
166+
return searchText.value.trim() || selectedBranch.value || selectedAuthor.value
152167
})
153168
</script>
154169

@@ -200,6 +215,22 @@ const hasActiveFilter = computed(() => {
200215
{{ branch }}
201216
</option>
202217
</select>
218+
<select
219+
v-model="selectedAuthor"
220+
class="author-select"
221+
:disabled="isLoading"
222+
>
223+
<option value="">
224+
所有作者
225+
</option>
226+
<option
227+
v-for="author in availableAuthors"
228+
:key="author"
229+
:value="author"
230+
>
231+
{{ author }}
232+
</option>
233+
</select>
203234
<button
204235
v-if="hasActiveFilter"
205236
class="clear-button"
@@ -314,7 +345,8 @@ const hasActiveFilter = computed(() => {
314345
justify-content: center;
315346
}
316347
317-
.branch-select {
348+
.branch-select,
349+
.author-select {
318350
min-width: 120px;
319351
padding: 4px 8px;
320352
border: 1px solid var(--vscode-input-border);
@@ -326,11 +358,13 @@ const hasActiveFilter = computed(() => {
326358
transition: border-color 0.2s ease;
327359
}
328360
329-
.branch-select:focus {
361+
.branch-select:focus,
362+
.author-select:focus {
330363
border-color: var(--vscode-focusBorder);
331364
}
332365
333-
.branch-select:disabled {
366+
.branch-select:disabled,
367+
.author-select:disabled {
334368
opacity: 0.6;
335369
cursor: not-allowed;
336370
}

src/views/webview.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ export const useGitPanelView = createSingletonComposable(() => {
113113
await getRepoBranches()
114114
break
115115

116+
case WEBVIEW_CHANNEL.GET_ALL_AUTHORS:
117+
await getRepoAuthors()
118+
break
119+
116120
case WEBVIEW_CHANNEL.SHOW_COMMIT_DETAILS:
117121
try {
118122
const hashes: string[] = JSON.parse(message.commitHashes)
@@ -178,6 +182,23 @@ export const useGitPanelView = createSingletonComposable(() => {
178182
}
179183
}
180184

185+
async function getRepoAuthors() {
186+
try {
187+
const authors = await git.getAllAuthors()
188+
189+
postMessage({
190+
command: CHANNEL.AUTHORS,
191+
authors,
192+
})
193+
}
194+
catch (error) {
195+
postMessage({
196+
command: 'Failed to get git authors',
197+
message: `${error}`,
198+
})
199+
}
200+
}
201+
181202
function clearSelection() {
182203
postMessage({ command: CHANNEL.CLEAR_SELECTED })
183204
gitChangesProvider.clearSelection()
@@ -189,6 +210,7 @@ export const useGitPanelView = createSingletonComposable(() => {
189210
postMessage,
190211
forceRefresh,
191212
getRepoBranches,
213+
getRepoAuthors,
192214
clearSelection,
193215
}
194216
})

0 commit comments

Comments
 (0)