feat: generate commit for staged changes if any (#6177)
* feat: generate commit for staged changes if any - Replace getWorkingState with getGitDiff function that return diff for staged changes only with unstaged change as fallback - Add structured PROMPT constant with system and message templates - Wrap generation logic in try-catch for better error handling - Move input validation and progress handling into main generate function - Improve error messages with more descriptive context * changeset added * clean up
This commit is contained in:
3 files changed
+87
-34
No files matched your search
@@ -185,6 +185,42 @@ export async function getWorkingState(cwd: string): Promise<string> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function getGitDiff(cwd: string, stagedOnly = false): Promise<string> {
|
||||
try {
|
||||
const isInstalled = await checkGitInstalled()
|
||||
if (!isInstalled) {
|
||||
throw new Error("Git is not installed")
|
||||
}
|
||||
|
||||
const isRepo = await checkGitRepo(cwd)
|
||||
if (!isRepo) {
|
||||
throw new Error("Not a git repository")
|
||||
}
|
||||
|
||||
let diff = ""
|
||||
let command = "git --no-pager diff --staged --diff-filter=d"
|
||||
if (await checkGitRepoHasCommits(cwd)) {
|
||||
// Only run git diff if there are commits
|
||||
const { stdout: staged } = await execAsync(command, { cwd })
|
||||
diff = staged.trim()
|
||||
}
|
||||
|
||||
if (!stagedOnly && !diff) {
|
||||
command = "git --no-pager diff HEAD --diff-filter=d"
|
||||
const { stdout: unstaged } = await execAsync(command, { cwd })
|
||||
diff = unstaged.trim()
|
||||
}
|
||||
|
||||
if (!diff) {
|
||||
throw new Error("No changes in workspace for commit message")
|
||||
}
|
||||
|
||||
return truncateOutput(`'${command}' Output:\n\n${diff}`.trim())
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
export async function getGitRemoteUrls(cwd: string): Promise<string[]> {
|
||||
try {
|
||||
const isInstalled = await checkGitInstalled()
|
||||
|
||||
Reference in new issue
Block a user