-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
56b0b8c
commit 6860c22
Showing
19 changed files
with
548 additions
and
247 deletions.
There are no files selected for viewing
44 changes: 23 additions & 21 deletions
44
src/extension/webview-api/chat-context-processor/types/chat-context/file-context.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/extension/webview-api/controllers/git.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { getWorkspaceFolder } from '@extension/utils' | ||
import simpleGit, { SimpleGit } from 'simple-git' | ||
|
||
import type { | ||
GitCommit, | ||
GitDiff | ||
} from '../chat-context-processor/types/chat-context' | ||
import { Controller } from '../types' | ||
|
||
export class GitController extends Controller { | ||
readonly name = 'git' | ||
|
||
private git: SimpleGit | ||
|
||
constructor() { | ||
super() | ||
const workspaceFolder = getWorkspaceFolder() | ||
this.git = simpleGit(workspaceFolder.uri.fsPath) | ||
} | ||
|
||
async getHistoryCommits(req: { maxCount?: number }): Promise<GitCommit[]> { | ||
const { maxCount = 50 } = req | ||
const log = await this.git.log({ maxCount }) | ||
|
||
const commits: GitCommit[] = await Promise.all( | ||
log.all.map(async commit => { | ||
const diff = await this.git.diff([`${commit.hash}^`, commit.hash]) | ||
return { | ||
sha: commit.hash, | ||
message: commit.message, | ||
diff: this.parseDiff(diff), | ||
author: commit.author_name, | ||
date: commit.date | ||
} | ||
}) | ||
) | ||
|
||
return commits | ||
} | ||
|
||
async getDiffWithRemoteMainBranch(req: { | ||
file?: string | ||
}): Promise<GitDiff[]> { | ||
const mainBranchName = await this.getMainBranchName() | ||
const { file } = req | ||
let diff: string | ||
|
||
if (file) { | ||
diff = await this.git.diff([`origin/${mainBranchName}`, '--', file]) | ||
} else { | ||
diff = await this.git.diff([`origin/${mainBranchName}`]) | ||
} | ||
|
||
return this.parseDiff(diff) | ||
} | ||
|
||
async getDiffWithWorkingState(req: { file?: string }): Promise<GitDiff[]> { | ||
const { file } = req | ||
let diff: string | ||
|
||
if (file) { | ||
diff = await this.git.diff(['HEAD', '--', file]) | ||
} else { | ||
diff = await this.git.diff(['HEAD']) | ||
} | ||
|
||
return this.parseDiff(diff) | ||
} | ||
|
||
private parseDiff(diff: string): GitDiff[] { | ||
const files = diff.split('diff --git') | ||
|
||
const diffs: GitDiff[] = [] | ||
|
||
files.slice(1).forEach(file => { | ||
const [header, ...chunks] = file.split('@@') | ||
if (!header) return | ||
|
||
const [from, to] = header.match(/a\/(.+) b\/(.+)/)?.slice(1) || ['', ''] | ||
|
||
if (!from || !to) return | ||
|
||
diffs.push({ | ||
from, | ||
to, | ||
chunks: chunks.map(chunk => { | ||
const [content, ...lines] = chunk.split('\n') | ||
return { | ||
content: `@@ ${content}`, | ||
lines: lines.filter(line => line.trim() !== '') | ||
} | ||
}) | ||
}) | ||
}) | ||
|
||
return diffs | ||
} | ||
|
||
async getCurrentBranch(): Promise<string> { | ||
return await this.git.revparse(['--abbrev-ref', 'HEAD']) | ||
} | ||
|
||
async getStatus(): Promise<any> { | ||
return await this.git.status() | ||
} | ||
|
||
async getRemotes(): Promise<any[]> { | ||
const remotes = await this.git.getRemotes(true) | ||
return remotes | ||
} | ||
|
||
private async getMainBranchName(): Promise<string> { | ||
const branches = await this.git.branch() | ||
const mainBranch = ['main', 'master', 'trunk', 'development'].find(branch => | ||
branches.all.includes(branch) | ||
) | ||
|
||
return mainBranch || 'main' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
src/webview/components/chat/selectors/mention-selector/files/mention-file-item.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
src/webview/components/chat/selectors/mention-selector/folders/mention-folder-item.tsx
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
src/webview/components/chat/selectors/mention-selector/folders/mention-folder-preview.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { memo, useCallback, useMemo } from 'react' | ||
import { ChevronDownIcon, ChevronRightIcon } from '@radix-ui/react-icons' | ||
import { FileIcon } from '@webview/components/file-icon' | ||
import { Tree, type TreeNodeRenderProps } from '@webview/components/tree' | ||
import { useFilesTreeItems } from '@webview/hooks/chat/use-files-tree-items' | ||
import type { FolderInfo, MentionOption } from '@webview/types/chat' | ||
|
||
export const MentionFolderPreview: React.FC<MentionOption> = memo( | ||
mentionOption => { | ||
const folderInfo = mentionOption.data as FolderInfo | ||
const { treeItems, traverseTree } = useFilesTreeItems({ | ||
files: [folderInfo] | ||
}) | ||
|
||
const allExpandedIds = useMemo(() => { | ||
const ids: string[] = [] | ||
|
||
traverseTree(item => { | ||
if (item.children?.length) { | ||
ids.push(item.id) | ||
} | ||
}) | ||
|
||
return ids | ||
}, [traverseTree]) | ||
|
||
const renderItem = useCallback( | ||
({ item, isExpanded, onToggleExpand, level }: TreeNodeRenderProps) => { | ||
const ArrowIcon = isExpanded ? ChevronDownIcon : ChevronRightIcon | ||
|
||
return ( | ||
<div | ||
className="flex items-center py-1 text-sm cursor-pointer" | ||
style={{ marginLeft: `${level * 20}px` }} | ||
onClick={onToggleExpand} | ||
> | ||
{!item.isLeaf && <ArrowIcon className="size-4 mr-1" />} | ||
|
||
<FileIcon | ||
className="size-4 mr-1" | ||
isFolder={!item.isLeaf} | ||
isOpen={isExpanded} | ||
filePath={item.name} | ||
/> | ||
|
||
<span>{item.name}</span> | ||
</div> | ||
) | ||
}, | ||
[] | ||
) | ||
|
||
return ( | ||
<div className="flex flex-col h-full"> | ||
<div className="flex flex-col flex-1 overflow-auto"> | ||
<Tree | ||
items={treeItems} | ||
expandedItemIds={allExpandedIds} | ||
renderItem={renderItem} | ||
/> | ||
</div> | ||
</div> | ||
) | ||
} | ||
) |
Oops, something went wrong.