Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
88bc27be40 |
+1
-1
@@ -2,7 +2,7 @@
|
||||
"name": "ai-commit-ext",
|
||||
"displayName": "AI Commit Ext",
|
||||
"description": "Generate commit messages using OpenCode AI",
|
||||
"version": "1.3.0",
|
||||
"version": "1.4.0",
|
||||
"publisher": "local",
|
||||
"engines": {
|
||||
"vscode": "^1.110.0"
|
||||
|
||||
+81
-45
@@ -1,50 +1,32 @@
|
||||
import * as vscode from "vscode";
|
||||
import { generateCommitMessage, isOpenCodeAvailable } from "./opencodeService";
|
||||
|
||||
interface GitExtension {
|
||||
getAPI(version: number): GitAPI;
|
||||
}
|
||||
|
||||
interface GitAPI {
|
||||
repositories: Repository[];
|
||||
}
|
||||
|
||||
interface Repository {
|
||||
root: string;
|
||||
rootUri: vscode.Uri;
|
||||
state: RepositoryState;
|
||||
inputBox: { value: string };
|
||||
diffIndexWithHEAD(path?: string): Promise<string>;
|
||||
diffWithHEAD(path?: string): Promise<string>;
|
||||
status(): Promise<void>;
|
||||
}
|
||||
|
||||
interface RepositoryState {
|
||||
indexChanges: any[];
|
||||
workingTreeChanges: any[];
|
||||
}
|
||||
import {
|
||||
Repository,
|
||||
getAllRepositories,
|
||||
getRepositoryLabel,
|
||||
getRepositoryRoot,
|
||||
} from "./gitService";
|
||||
|
||||
export async function activate(
|
||||
context: vscode.ExtensionContext,
|
||||
): Promise<void> {
|
||||
const commandHandler = vscode.commands.registerCommand(
|
||||
"aiCommitExt.generate",
|
||||
async () => {
|
||||
await handleGenerateCommitMessage();
|
||||
async (sc?: vscode.SourceControl) => {
|
||||
await handleGenerateCommitMessage(sc);
|
||||
},
|
||||
);
|
||||
context.subscriptions.push(commandHandler);
|
||||
}
|
||||
|
||||
async function handleGenerateCommitMessage(): Promise<void> {
|
||||
async function handleGenerateCommitMessage(
|
||||
sc?: vscode.SourceControl,
|
||||
): Promise<void> {
|
||||
const config = vscode.workspace.getConfiguration("aiCommitExt");
|
||||
const showNotification = config.get<boolean>("showNotification", true);
|
||||
|
||||
try {
|
||||
const [opencodeAvailable, gitExtension] = await Promise.all([
|
||||
isOpenCodeAvailable(),
|
||||
vscode.extensions.getExtension<GitExtension>("vscode.git"),
|
||||
]);
|
||||
const opencodeAvailable = await isOpenCodeAvailable();
|
||||
|
||||
if (!opencodeAvailable) {
|
||||
if (showNotification) {
|
||||
@@ -55,25 +37,15 @@ async function handleGenerateCommitMessage(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!gitExtension) {
|
||||
if (showNotification) {
|
||||
vscode.window.showErrorMessage("Git extension not found");
|
||||
}
|
||||
return;
|
||||
}
|
||||
const repository = await resolveRepository(sc);
|
||||
|
||||
const gitApi = gitExtension.exports.getAPI(1);
|
||||
const repositories = gitApi.repositories;
|
||||
|
||||
if (repositories.length === 0) {
|
||||
if (!repository) {
|
||||
if (showNotification) {
|
||||
vscode.window.showErrorMessage("No Git repository found");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const repository = repositories[0];
|
||||
|
||||
await repository.status();
|
||||
|
||||
const state = repository.state;
|
||||
@@ -98,21 +70,31 @@ async function handleGenerateCommitMessage(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
const repoLabel = getRepositoryLabel(repository);
|
||||
|
||||
const commitMessage = await vscode.window.withProgress(
|
||||
{
|
||||
location: vscode.ProgressLocation.Notification,
|
||||
cancellable: true,
|
||||
title: userSuggestion ? "Improving commit message..." : "Generating commit message...",
|
||||
title: userSuggestion
|
||||
? `Improving commit message (${repoLabel})...`
|
||||
: `Generating commit message (${repoLabel})...`,
|
||||
},
|
||||
async (_progress, token) => {
|
||||
return await generateCommitMessage({ userSuggestion: userSuggestion || undefined, cancellationToken: token });
|
||||
return await generateCommitMessage({
|
||||
repository,
|
||||
userSuggestion: userSuggestion || undefined,
|
||||
cancellationToken: token,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
repository.inputBox.value = commitMessage;
|
||||
|
||||
if (showNotification) {
|
||||
vscode.window.showInformationMessage("Commit message generated");
|
||||
vscode.window.showInformationMessage(
|
||||
`Commit message generated for ${repoLabel}`,
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
const message =
|
||||
@@ -125,6 +107,60 @@ async function handleGenerateCommitMessage(): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
async function resolveRepository(
|
||||
sc?: vscode.SourceControl,
|
||||
): Promise<Repository | undefined> {
|
||||
const repositories = getAllRepositories();
|
||||
|
||||
if (repositories.length === 0) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (sc) {
|
||||
output.appendLine(`[resolveRepository] sc provided, sc.rootUri=${sc.rootUri?.fsPath}`);
|
||||
for (const r of repositories) {
|
||||
const rRoot = getRepositoryRoot(r);
|
||||
const match = r.inputBox === sc.inputBox;
|
||||
output.appendLine(`[resolveRepository] repo root=${rRoot}, inputBox match=${match}, sc.inputBox===r.inputBox=${sc.inputBox === r.inputBox}`);
|
||||
}
|
||||
|
||||
const byInputBox = repositories.find((r) => r.inputBox === sc.inputBox);
|
||||
if (byInputBox) {
|
||||
output.appendLine(`[resolveRepository] matched by inputBox`);
|
||||
return byInputBox;
|
||||
}
|
||||
|
||||
if (sc.rootUri) {
|
||||
const byRootUri = repositories.find((r) => {
|
||||
const rRoot = getRepositoryRoot(r);
|
||||
return rRoot === sc.rootUri!.fsPath;
|
||||
});
|
||||
if (byRootUri) {
|
||||
output.appendLine(`[resolveRepository] matched by rootUri`);
|
||||
return byRootUri;
|
||||
}
|
||||
}
|
||||
|
||||
output.appendLine(`[resolveRepository] no match found for sc`);
|
||||
}
|
||||
|
||||
if (repositories.length === 1) {
|
||||
return repositories[0];
|
||||
}
|
||||
|
||||
const items = repositories.map((repo) => ({
|
||||
label: getRepositoryLabel(repo),
|
||||
description: getRepositoryRoot(repo),
|
||||
repository: repo,
|
||||
}));
|
||||
|
||||
const selected = await vscode.window.showQuickPick(items, {
|
||||
placeHolder: "Select a repository to generate a commit message for",
|
||||
});
|
||||
|
||||
return selected?.repository;
|
||||
}
|
||||
|
||||
export function deactivate(): void {}
|
||||
|
||||
export const output = vscode.window.createOutputChannel("ai-commit-ext");
|
||||
|
||||
+44
-32
@@ -1,24 +1,24 @@
|
||||
import * as vscode from "vscode";
|
||||
|
||||
interface GitExtension {
|
||||
export interface GitExtension {
|
||||
getAPI(version: number): GitAPI;
|
||||
}
|
||||
|
||||
interface GitAPI {
|
||||
export interface GitAPI {
|
||||
repositories: Repository[];
|
||||
}
|
||||
|
||||
interface Repository {
|
||||
export interface Repository {
|
||||
root: string;
|
||||
rootUri: vscode.Uri;
|
||||
state: RepositoryState;
|
||||
inputBox: { value: string };
|
||||
diffWithHEAD(path?: string): Promise<string>;
|
||||
diffIndexWithHEAD(path?: string): Promise<string>;
|
||||
diffWithHEAD(path?: string): Promise<string>;
|
||||
status(): Promise<void>;
|
||||
}
|
||||
|
||||
interface RepositoryState {
|
||||
export interface RepositoryState {
|
||||
indexChanges: Change[];
|
||||
workingTreeChanges: Change[];
|
||||
}
|
||||
@@ -34,12 +34,9 @@ export interface GitChange {
|
||||
diff?: string;
|
||||
}
|
||||
|
||||
export async function getGitChanges(): Promise<GitChange[]> {
|
||||
const repository = await getActiveGitRepository();
|
||||
if (!repository) {
|
||||
throw new Error("No Git repository found");
|
||||
}
|
||||
|
||||
export async function getGitChanges(
|
||||
repository: Repository,
|
||||
): Promise<GitChange[]> {
|
||||
const state = repository.state;
|
||||
const changes: GitChange[] = [];
|
||||
|
||||
@@ -70,12 +67,9 @@ export async function getGitChanges(): Promise<GitChange[]> {
|
||||
return changes;
|
||||
}
|
||||
|
||||
export async function getGitDiff(): Promise<string> {
|
||||
const repository = await getActiveGitRepository();
|
||||
if (!repository) {
|
||||
throw new Error("No Git repository found");
|
||||
}
|
||||
|
||||
export async function getGitDiff(
|
||||
repository: Repository,
|
||||
): Promise<string> {
|
||||
const state = repository.state;
|
||||
let diffOutput = "";
|
||||
|
||||
@@ -123,7 +117,20 @@ export async function getGitDiff(): Promise<string> {
|
||||
return diffOutput;
|
||||
}
|
||||
|
||||
async function getActiveGitRepository(): Promise<Repository | null> {
|
||||
export function getRepositoryRoot(repository: Repository): string {
|
||||
if (typeof repository.root === "string") {
|
||||
return repository.root;
|
||||
}
|
||||
return repository.rootUri.fsPath;
|
||||
}
|
||||
|
||||
export function getRepositoryLabel(repository: Repository): string {
|
||||
const root = getRepositoryRoot(repository);
|
||||
const folderName = root.split("/").pop() || root;
|
||||
return folderName;
|
||||
}
|
||||
|
||||
export function getAllRepositories(): Repository[] {
|
||||
const gitExtension =
|
||||
vscode.extensions.getExtension<GitExtension>("vscode.git");
|
||||
if (!gitExtension) {
|
||||
@@ -131,10 +138,26 @@ async function getActiveGitRepository(): Promise<Repository | null> {
|
||||
}
|
||||
|
||||
const api = gitExtension.exports.getAPI(1);
|
||||
if (api.repositories.length === 0) {
|
||||
return null;
|
||||
return api.repositories;
|
||||
}
|
||||
|
||||
export function findRepositoryForFile(
|
||||
repositories: Repository[],
|
||||
filePath: string,
|
||||
): Repository | undefined {
|
||||
const normalizedPath = filePath.toLowerCase();
|
||||
let bestMatch: Repository | undefined;
|
||||
let bestLength = 0;
|
||||
|
||||
for (const repo of repositories) {
|
||||
const root = getRepositoryRoot(repo).toLowerCase();
|
||||
if (normalizedPath.startsWith(root) && root.length > bestLength) {
|
||||
bestMatch = repo;
|
||||
bestLength = root.length;
|
||||
}
|
||||
}
|
||||
return api.repositories[0];
|
||||
|
||||
return bestMatch;
|
||||
}
|
||||
|
||||
function getChangeStatus(status: number): GitChange["status"] {
|
||||
@@ -160,14 +183,3 @@ function getChangeStatus(status: number): GitChange["status"] {
|
||||
|
||||
return "modified";
|
||||
}
|
||||
|
||||
export async function getRepositoryRoot(): Promise<string | null> {
|
||||
const repository = await getActiveGitRepository();
|
||||
if (!repository) {
|
||||
return null;
|
||||
}
|
||||
if (typeof repository.root === "string") {
|
||||
return repository.root;
|
||||
}
|
||||
return repository.rootUri.fsPath;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { exec, spawn, ExecException } from "child_process";
|
||||
import * as vscode from "vscode";
|
||||
import { getGitDiff, getRepositoryRoot } from "./gitService";
|
||||
import { getGitDiff, getRepositoryRoot, Repository } from "./gitService";
|
||||
import { output } from "./extension";
|
||||
|
||||
export interface GenerateOptions {
|
||||
repository: Repository;
|
||||
model?: string;
|
||||
userSuggestion?: string;
|
||||
cancellationToken?: vscode.CancellationToken;
|
||||
@@ -52,14 +53,16 @@ async function getOpenCodePath(): Promise<string> {
|
||||
}
|
||||
|
||||
export async function generateCommitMessage(
|
||||
options: GenerateOptions = {},
|
||||
options: GenerateOptions,
|
||||
): Promise<string> {
|
||||
const [opencodePath, diff, repoRoot] = await Promise.all([
|
||||
const { repository } = options;
|
||||
|
||||
const [opencodePath, diff] = await Promise.all([
|
||||
getOpenCodePath(),
|
||||
getGitDiff(),
|
||||
getRepositoryRoot(),
|
||||
getGitDiff(repository),
|
||||
]);
|
||||
|
||||
const repoRoot = getRepositoryRoot(repository);
|
||||
const config = vscode.workspace.getConfiguration("aiCommitExt");
|
||||
const model = options.model || config.get<string>("model", "");
|
||||
|
||||
@@ -103,7 +106,7 @@ Generate a concise Conventional Commit message for these changes:`;
|
||||
const proc = spawn(opencodePath, args, {
|
||||
stdio: ["pipe", "pipe", "pipe"],
|
||||
shell: true,
|
||||
cwd: repoRoot || undefined,
|
||||
cwd: repoRoot,
|
||||
env: {
|
||||
...process.env,
|
||||
OPENCODE_SERVER_PASSWORD: undefined,
|
||||
|
||||
Reference in New Issue
Block a user