3 Commits
1.0.0 ... main

5 changed files with 735 additions and 710 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "ai-commit-ext", "name": "ai-commit-ext",
"version": "0.0.1", "version": "1.0.0",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "ai-commit-ext", "name": "ai-commit-ext",
"version": "0.0.1", "version": "1.0.0",
"devDependencies": { "devDependencies": {
"@types/node": "^25.5.2", "@types/node": "^25.5.2",
"@types/vscode": "^1.110.0", "@types/vscode": "^1.110.0",

View File

@@ -2,7 +2,7 @@
"name": "ai-commit-ext", "name": "ai-commit-ext",
"displayName": "AI Commit Ext", "displayName": "AI Commit Ext",
"description": "Generate commit messages using OpenCode AI", "description": "Generate commit messages using OpenCode AI",
"version": "1.0.0", "version": "1.0.1",
"publisher": "local", "publisher": "local",
"engines": { "engines": {
"vscode": "^1.110.0" "vscode": "^1.110.0"

View File

@@ -41,7 +41,11 @@ async function handleGenerateCommitMessage(): Promise<void> {
const showNotification = config.get<boolean>("showNotification", true); const showNotification = config.get<boolean>("showNotification", true);
try { try {
const opencodeAvailable = await isOpenCodeAvailable(); const [opencodeAvailable, gitExtension] = await Promise.all([
isOpenCodeAvailable(),
vscode.extensions.getExtension<GitExtension>("vscode.git"),
]);
if (!opencodeAvailable) { if (!opencodeAvailable) {
if (showNotification) { if (showNotification) {
vscode.window.showErrorMessage( vscode.window.showErrorMessage(
@@ -51,8 +55,6 @@ async function handleGenerateCommitMessage(): Promise<void> {
return; return;
} }
const gitExtension =
vscode.extensions.getExtension<GitExtension>("vscode.git");
if (!gitExtension) { if (!gitExtension) {
if (showNotification) { if (showNotification) {
vscode.window.showErrorMessage("Git extension not found"); vscode.window.showErrorMessage("Git extension not found");
@@ -114,3 +116,5 @@ async function handleGenerateCommitMessage(): Promise<void> {
} }
export function deactivate(): void {} export function deactivate(): void {}
export const output = vscode.window.createOutputChannel("ai-commit-ext");

View File

@@ -1,4 +1,4 @@
import * as vscode from 'vscode'; import * as vscode from "vscode";
interface GitExtension { interface GitExtension {
getAPI(version: number): GitAPI; getAPI(version: number): GitAPI;
@@ -30,14 +30,14 @@ interface Change {
export interface GitChange { export interface GitChange {
path: string; path: string;
status: 'added' | 'modified' | 'deleted' | 'renamed' | 'untracked'; status: "added" | "modified" | "deleted" | "renamed" | "untracked";
diff?: string; diff?: string;
} }
export async function getGitChanges(): Promise<GitChange[]> { export async function getGitChanges(): Promise<GitChange[]> {
const repository = await getActiveGitRepository(); const repository = await getActiveGitRepository();
if (!repository) { if (!repository) {
throw new Error('No Git repository found'); throw new Error("No Git repository found");
} }
const state = repository.state; const state = repository.state;
@@ -54,8 +54,8 @@ export async function getGitChanges(): Promise<GitChange[]> {
}); });
} }
} else if (unstagedChanges.length > 0) { } else if (unstagedChanges.length > 0) {
const config = vscode.workspace.getConfiguration('aiCommitExt'); const config = vscode.workspace.getConfiguration("aiCommitExt");
const includeUnstaged = config.get<boolean>('includeUnstaged', false); const includeUnstaged = config.get<boolean>("includeUnstaged", false);
if (includeUnstaged) { if (includeUnstaged) {
for (const change of unstagedChanges) { for (const change of unstagedChanges) {
@@ -73,58 +73,61 @@ export async function getGitChanges(): Promise<GitChange[]> {
export async function getGitDiff(): Promise<string> { export async function getGitDiff(): Promise<string> {
const repository = await getActiveGitRepository(); const repository = await getActiveGitRepository();
if (!repository) { if (!repository) {
throw new Error('No Git repository found'); throw new Error("No Git repository found");
} }
const state = repository.state; const state = repository.state;
let diffOutput = ''; let diffOutput = "";
const stagedChanges = state.indexChanges || []; const stagedChanges = state.indexChanges || [];
const unstagedChanges = state.workingTreeChanges || []; const unstagedChanges = state.workingTreeChanges || [];
const config = vscode.workspace.getConfiguration('aiCommitExt'); const config = vscode.workspace.getConfiguration("aiCommitExt");
const includeUnstaged = config.get<boolean>('includeUnstaged', false); const includeUnstaged = config.get<boolean>("includeUnstaged", false);
if (stagedChanges.length > 0) { if (stagedChanges.length > 0) {
for (const change of stagedChanges) { for (const change of stagedChanges) {
const fileName = change.uri.fsPath.split('/').pop() || ''; const fileName = change.uri.fsPath.split("/").pop() || "";
diffOutput += `## ${fileName} (staged)\n`; diffOutput += `## ${fileName} (staged)\n`;
try { try {
const diff = await repository.diffIndexWithHEAD(change.uri.fsPath); const diff = await repository.diffIndexWithHEAD(
change.uri.fsPath,
);
if (diff) { if (diff) {
diffOutput += diff + '\n'; diffOutput += diff + "\n";
} }
} catch { } catch {
diffOutput += '(Unable to get diff)\n'; diffOutput += "(Unable to get diff)\n";
} }
} }
} else if (unstagedChanges.length > 0 && includeUnstaged) { } else if (unstagedChanges.length > 0 && includeUnstaged) {
for (const change of unstagedChanges) { for (const change of unstagedChanges) {
const fileName = change.uri.fsPath.split('/').pop() || ''; const fileName = change.uri.fsPath.split("/").pop() || "";
diffOutput += `## ${fileName} (unstaged)\n`; diffOutput += `## ${fileName} (unstaged)\n`;
try { try {
const diff = await repository.diffWithHEAD(change.uri.fsPath); const diff = await repository.diffWithHEAD(change.uri.fsPath);
if (diff) { if (diff) {
diffOutput += diff + '\n'; diffOutput += diff + "\n";
} }
} catch { } catch {
diffOutput += '(Unable to get diff)\n'; diffOutput += "(Unable to get diff)\n";
} }
} }
} }
if (!diffOutput) { if (!diffOutput) {
throw new Error('No changes to commit'); throw new Error("No changes to commit");
} }
return diffOutput; return diffOutput;
} }
async function getActiveGitRepository(): Promise<Repository | null> { async function getActiveGitRepository(): Promise<Repository | null> {
const gitExtension = vscode.extensions.getExtension<GitExtension>('vscode.git'); const gitExtension =
vscode.extensions.getExtension<GitExtension>("vscode.git");
if (!gitExtension) { if (!gitExtension) {
throw new Error('Git extension not found'); throw new Error("Git extension not found");
} }
const api = gitExtension.exports.getAPI(1); const api = gitExtension.exports.getAPI(1);
@@ -134,7 +137,7 @@ async function getActiveGitRepository(): Promise<Repository | null> {
return api.repositories[0]; return api.repositories[0];
} }
function getChangeStatus(status: number): GitChange['status'] { function getChangeStatus(status: number): GitChange["status"] {
const Status = { const Status = {
INDEX_MODIFIED: 0, INDEX_MODIFIED: 0,
INDEX_ADDED: 1, INDEX_ADDED: 1,
@@ -146,16 +149,16 @@ function getChangeStatus(status: number): GitChange['status'] {
}; };
if (status === Status.INDEX_ADDED || status === Status.UNTRACKED) { if (status === Status.INDEX_ADDED || status === Status.UNTRACKED) {
return 'added'; return "added";
} }
if (status === Status.INDEX_DELETED || status === Status.DELETED) { if (status === Status.INDEX_DELETED || status === Status.DELETED) {
return 'deleted'; return "deleted";
} }
if (status === Status.INDEX_RENAMED) { if (status === Status.INDEX_RENAMED) {
return 'renamed'; return "renamed";
} }
return 'modified'; return "modified";
} }
export async function getRepositoryRoot(): Promise<string | null> { export async function getRepositoryRoot(): Promise<string | null> {
@@ -163,7 +166,7 @@ export async function getRepositoryRoot(): Promise<string | null> {
if (!repository) { if (!repository) {
return null; return null;
} }
if (typeof repository.root === 'string') { if (typeof repository.root === "string") {
return repository.root; return repository.root;
} }
return repository.rootUri.fsPath; return repository.rootUri.fsPath;

View File

@@ -1,11 +1,14 @@
import { exec, spawn, ExecException } from "child_process"; import { exec, spawn, ExecException } from "child_process";
import * as vscode from "vscode"; import * as vscode from "vscode";
import { getGitDiff, getRepositoryRoot } from "./gitService"; import { getGitDiff, getRepositoryRoot } from "./gitService";
import { output } from "./extension";
export interface GenerateOptions { export interface GenerateOptions {
model?: string; model?: string;
} }
let opencodeAvailableCache: boolean | null = null;
const DEFAULT_PROMPT = `You are a helpful assistant that generates git commit messages. const DEFAULT_PROMPT = `You are a helpful assistant that generates git commit messages.
Generate a concise Conventional Commit message (max 72 characters for the subject line). Generate a concise Conventional Commit message (max 72 characters for the subject line).
Format: <type>(<scope>): <description> Format: <type>(<scope>): <description>
@@ -15,9 +18,13 @@ Types: feat, fix, refactor, docs, style, test, chore, perf, ci, build, revert
Only output the commit message, nothing else.`; Only output the commit message, nothing else.`;
export async function isOpenCodeAvailable(): Promise<boolean> { export async function isOpenCodeAvailable(): Promise<boolean> {
if (opencodeAvailableCache !== null) {
return opencodeAvailableCache;
}
return new Promise((resolve) => { return new Promise((resolve) => {
exec("which opencode", (error: ExecException | null) => { exec("which opencode", (error: ExecException | null) => {
resolve(!error); opencodeAvailableCache = !error;
resolve(opencodeAvailableCache);
}); });
}); });
} }
@@ -32,8 +39,10 @@ export async function generateCommitMessage(
); );
} }
const diff = await getGitDiff(); const [diff, repoRoot] = await Promise.all([
const repoRoot = await getRepositoryRoot(); getGitDiff(),
getRepositoryRoot(),
]);
const config = vscode.workspace.getConfiguration("aiCommitExt"); const config = vscode.workspace.getConfiguration("aiCommitExt");
const model = options.model || config.get<string>("model", ""); const model = options.model || config.get<string>("model", "");
@@ -45,8 +54,17 @@ ${diff}
Generate a concise Conventional Commit message for these changes:`; Generate a concise Conventional Commit message for these changes:`;
const log = `[${Date.now()}]\r\n${prompt}`;
output.appendLine(log);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const args: string[] = ["run", "--format", "default"]; const args: string[] = [
"run",
"--format",
"default",
"--variant",
"minimal",
];
if (model) { if (model) {
args.push("--model", model); args.push("--model", model);