scai
Version:
> AI-powered CLI tool for commit messages **and** pull request reviews — using local models.
58 lines (57 loc) • 2.2 kB
JavaScript
import path from 'path';
import { execSync } from 'child_process';
import { Config } from '../config.js';
/**
* Executes a Git command inside the specified working directory.
*/
function runGitCommand(cmd, cwd) {
return execSync(cmd, { cwd, encoding: 'utf-8' }).trim();
}
/**
* Retrieves the owner and repo name from the Git remote URL inside the indexDir.
* This ensures we get the correct GitHub repo owner and name, regardless of current working directory.
*/
function getRepoOwnerAndNameFromGit(indexDir) {
try {
const originUrl = runGitCommand('git config --get remote.origin.url', indexDir);
console.log(`🔗 Git origin URL from '${indexDir}': ${originUrl}`);
const match = originUrl.match(/github\.com[:/](.+?)(?:\.git)?$/);
if (!match)
throw new Error("❌ Could not parse GitHub repo from origin URL.");
const [owner, repo] = match[1].split('/');
console.log(`✅ Parsed from Git: owner='${owner}', repo='${repo}'`);
return { owner, repo };
}
catch (error) {
console.warn(`⚠️ Failed to parse GitHub info from Git config in '${indexDir}': ${error instanceof Error ? error.message : error}`);
throw error;
}
}
/**
* Fallback: Extracts GitHub repo owner and name from the index directory path.
*/
function getRepoOwnerAndNameFromIndexDir(indexDir) {
const parts = path.resolve(indexDir).split(path.sep);
const repo = parts.pop();
const owner = parts.pop();
console.log(`📁 Parsed from indexDir: owner='${owner}', repo='${repo}'`);
return { owner, repo };
}
/**
* Get the GitHub repo details, always from the configured indexDir.
* Prefers Git config, falls back to parsing the path.
*/
export function getRepoDetails() {
const indexDir = Config.getIndexDir();
if (!indexDir) {
throw new Error("❌ indexDir is not configured.");
}
console.log(`📦 Resolving GitHub repo info from indexDir: ${indexDir}`);
try {
return getRepoOwnerAndNameFromGit(indexDir);
}
catch {
console.log("🔁 Falling back to extracting from indexDir path...");
return getRepoOwnerAndNameFromIndexDir(indexDir);
}
}