Files
AI-Commit-Ext/src/gitService.ts
T

186 lines
5.0 KiB
TypeScript

import * as vscode from "vscode";
export interface GitExtension {
getAPI(version: number): GitAPI;
}
export interface GitAPI {
repositories: Repository[];
}
export 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>;
}
export interface RepositoryState {
indexChanges: Change[];
workingTreeChanges: Change[];
}
interface Change {
uri: vscode.Uri;
status: number;
}
export interface GitChange {
path: string;
status: "added" | "modified" | "deleted" | "renamed" | "untracked";
diff?: string;
}
export async function getGitChanges(
repository: Repository,
): Promise<GitChange[]> {
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) {
for (const change of unstagedChanges) {
changes.push({
path: change.uri.fsPath,
status: getChangeStatus(change.status),
});
}
}
}
return changes;
}
export async function getGitDiff(
repository: Repository,
): Promise<string> {
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 diff = await repository.diffWithHEAD(change.uri.fsPath);
if (diff) {
diffOutput += diff + "\n";
}
} catch {
diffOutput += "(Unable to get diff)\n";
}
}
}
if (!diffOutput) {
throw new Error("No changes to commit");
}
return diffOutput;
}
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) {
throw new Error("Git extension not found");
}
const api = gitExtension.exports.getAPI(1);
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 bestMatch;
}
function getChangeStatus(status: number): GitChange["status"] {
const Status = {
INDEX_MODIFIED: 0,
INDEX_ADDED: 1,
INDEX_DELETED: 2,
INDEX_RENAMED: 3,
MODIFIED: 4,
DELETED: 5,
UNTRACKED: 6,
};
if (status === Status.INDEX_ADDED || status === Status.UNTRACKED) {
return "added";
}
if (status === Status.INDEX_DELETED || status === Status.DELETED) {
return "deleted";
}
if (status === Status.INDEX_RENAMED) {
return "renamed";
}
return "modified";
}