Compare commits
1 Commits
55df24e0fd
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| c887635a0c |
@@ -1,170 +1,173 @@
|
|||||||
import * as vscode from 'vscode';
|
import * as vscode from "vscode";
|
||||||
|
|
||||||
interface GitExtension {
|
interface GitExtension {
|
||||||
getAPI(version: number): GitAPI;
|
getAPI(version: number): GitAPI;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface GitAPI {
|
interface GitAPI {
|
||||||
repositories: Repository[];
|
repositories: Repository[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Repository {
|
interface Repository {
|
||||||
root: string;
|
root: string;
|
||||||
rootUri: vscode.Uri;
|
rootUri: vscode.Uri;
|
||||||
state: RepositoryState;
|
state: RepositoryState;
|
||||||
inputBox: { value: string };
|
inputBox: { value: string };
|
||||||
diffWithHEAD(path?: string): Promise<string>;
|
diffWithHEAD(path?: string): Promise<string>;
|
||||||
diffIndexWithHEAD(path?: string): Promise<string>;
|
diffIndexWithHEAD(path?: string): Promise<string>;
|
||||||
status(): Promise<void>;
|
status(): Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface RepositoryState {
|
interface RepositoryState {
|
||||||
indexChanges: Change[];
|
indexChanges: Change[];
|
||||||
workingTreeChanges: Change[];
|
workingTreeChanges: Change[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Change {
|
interface Change {
|
||||||
uri: vscode.Uri;
|
uri: vscode.Uri;
|
||||||
status: number;
|
status: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 changes: GitChange[] = [];
|
|
||||||
|
|
||||||
const stagedChanges = state.indexChanges || [];
|
|
||||||
const unstagedChanges = state.workingTreeChanges || [];
|
|
||||||
|
|
||||||
if (stagedChanges.length > 0) {
|
|
||||||
for (const change of stagedChanges) {
|
|
||||||
changes.push({
|
|
||||||
path: change.uri.fsPath,
|
|
||||||
status: getChangeStatus(change.status),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else if (unstagedChanges.length > 0) {
|
|
||||||
const config = vscode.workspace.getConfiguration('aiCommitExt');
|
|
||||||
const includeUnstaged = config.get<boolean>('includeUnstaged', false);
|
|
||||||
|
|
||||||
if (includeUnstaged) {
|
const state = repository.state;
|
||||||
for (const change of unstagedChanges) {
|
const changes: GitChange[] = [];
|
||||||
changes.push({
|
|
||||||
path: change.uri.fsPath,
|
const stagedChanges = state.indexChanges || [];
|
||||||
status: getChangeStatus(change.status),
|
const unstagedChanges = state.workingTreeChanges || [];
|
||||||
});
|
|
||||||
}
|
if (stagedChanges.length > 0) {
|
||||||
|
for (const change of stagedChanges) {
|
||||||
|
changes.push({
|
||||||
|
path: change.uri.fsPath,
|
||||||
|
status: getChangeStatus(change.status),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else if (unstagedChanges.length > 0) {
|
||||||
|
const config = vscode.workspace.getConfiguration("aiCommitExt");
|
||||||
|
const includeUnstaged = config.get<boolean>("includeUnstaged", false);
|
||||||
|
|
||||||
|
if (includeUnstaged) {
|
||||||
|
for (const change of unstagedChanges) {
|
||||||
|
changes.push({
|
||||||
|
path: change.uri.fsPath,
|
||||||
|
status: getChangeStatus(change.status),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return changes;
|
return changes;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
let diffOutput = '';
|
|
||||||
|
|
||||||
const stagedChanges = state.indexChanges || [];
|
|
||||||
const unstagedChanges = state.workingTreeChanges || [];
|
|
||||||
const config = vscode.workspace.getConfiguration('aiCommitExt');
|
|
||||||
const includeUnstaged = config.get<boolean>('includeUnstaged', false);
|
|
||||||
|
|
||||||
if (stagedChanges.length > 0) {
|
|
||||||
for (const change of stagedChanges) {
|
|
||||||
const fileName = change.uri.fsPath.split('/').pop() || '';
|
|
||||||
diffOutput += `## ${fileName} (staged)\n`;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const diff = await repository.diffIndexWithHEAD(change.uri.fsPath);
|
|
||||||
if (diff) {
|
|
||||||
diffOutput += diff + '\n';
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
diffOutput += '(Unable to get diff)\n';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (unstagedChanges.length > 0 && includeUnstaged) {
|
|
||||||
for (const change of unstagedChanges) {
|
|
||||||
const fileName = change.uri.fsPath.split('/').pop() || '';
|
|
||||||
diffOutput += `## ${fileName} (unstaged)\n`;
|
|
||||||
|
|
||||||
try {
|
const state = repository.state;
|
||||||
const diff = await repository.diffWithHEAD(change.uri.fsPath);
|
let diffOutput = "";
|
||||||
if (diff) {
|
|
||||||
diffOutput += diff + '\n';
|
const stagedChanges = state.indexChanges || [];
|
||||||
|
const unstagedChanges = state.workingTreeChanges || [];
|
||||||
|
const config = vscode.workspace.getConfiguration("aiCommitExt");
|
||||||
|
const includeUnstaged = config.get<boolean>("includeUnstaged", false);
|
||||||
|
|
||||||
|
if (stagedChanges.length > 0) {
|
||||||
|
for (const change of stagedChanges) {
|
||||||
|
const fileName = change.uri.fsPath.split("/").pop() || "";
|
||||||
|
diffOutput += `## ${fileName} (staged)\n`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const diff = await repository.diffIndexWithHEAD(
|
||||||
|
change.uri.fsPath,
|
||||||
|
);
|
||||||
|
if (diff) {
|
||||||
|
diffOutput += diff + "\n";
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
diffOutput += "(Unable to get diff)\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else if (unstagedChanges.length > 0 && includeUnstaged) {
|
||||||
|
for (const change of unstagedChanges) {
|
||||||
|
const fileName = change.uri.fsPath.split("/").pop() || "";
|
||||||
|
diffOutput += `## ${fileName} (unstaged)\n`;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const diff = await repository.diffWithHEAD(change.uri.fsPath);
|
||||||
|
if (diff) {
|
||||||
|
diffOutput += diff + "\n";
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
diffOutput += "(Unable to get diff)\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
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 =
|
||||||
if (!gitExtension) {
|
vscode.extensions.getExtension<GitExtension>("vscode.git");
|
||||||
throw new Error('Git extension not found');
|
if (!gitExtension) {
|
||||||
}
|
throw new Error("Git extension not found");
|
||||||
|
}
|
||||||
|
|
||||||
const api = gitExtension.exports.getAPI(1);
|
const api = gitExtension.exports.getAPI(1);
|
||||||
if (api.repositories.length === 0) {
|
if (api.repositories.length === 0) {
|
||||||
return null;
|
return 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,
|
||||||
INDEX_DELETED: 2,
|
INDEX_DELETED: 2,
|
||||||
INDEX_RENAMED: 3,
|
INDEX_RENAMED: 3,
|
||||||
MODIFIED: 4,
|
MODIFIED: 4,
|
||||||
DELETED: 5,
|
DELETED: 5,
|
||||||
UNTRACKED: 6,
|
UNTRACKED: 6,
|
||||||
};
|
};
|
||||||
|
|
||||||
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> {
|
||||||
const repository = await getActiveGitRepository();
|
const repository = await getActiveGitRepository();
|
||||||
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;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user