UNPKG

relaycode

Version:

A developer assistant that automates applying code changes from LLMs.

13 lines (9 loc) 2.57 kB
import {readAllStateFiles,markTransactionsAsGitCommitted}from'../core/state';import {logger}from'../utils/logger';import {executeShellCommand}from'../utils/shell';import {createConfirmationHandler}from'../utils/prompt';import {formatTransactionDetails}from'../utils/formatters';import {normalizeGitCommitMsg}from'relaycode-core';import e from'chalk';const F=async(f={},i=process.cwd(),u)=>{const p=createConfirmationHandler(f,u);if(logger.info("Looking for new transactions to commit..."),(await executeShellCommand("git rev-parse --is-inside-work-tree",i)).exitCode!==0){logger.error("This does not appear to be a git repository. Aborting commit.");return}const c=await executeShellCommand("git log -1 --format=%ct",i),d=c.exitCode===0?parseInt(c.stdout,10)*1e3:0,m=(await readAllStateFiles(i)??[]).filter(t=>{const a=new Date(t.createdAt).getTime()>d,T=!t.gitCommittedAt;return (a||d===0)&&T}).sort((t,a)=>new Date(t.createdAt).getTime()-new Date(a.createdAt).getTime());if(m.length===0){logger.info("No new transactions to commit since the last git commit.");return}const g=m.map(t=>normalizeGitCommitMsg(t.gitCommitMsg)).filter(t=>!!t);if(g.length===0){logger.warn("No new transactions with git commit messages found.");return}const l=g.map(t=>t.trim()).filter(t=>t.length>0).join(` `);logger.log("Found new transactions to commit:"),m.forEach(t=>{formatTransactionDetails(t).forEach(a=>logger.log(a));});const C=l.split(` `).map(t=>` ${t}`).join(` `);if(!await p(` Do you want to run 'git add .' and commit with the following message? ${e.yellow(C)} (y/N)`)){logger.info("Commit operation cancelled.");return}logger.info(`Running ${e.magenta("'git add .'")}...`);const s=await executeShellCommand("git add .",i);if(s.exitCode!==0){logger.error(`${e.magenta("'git add .'")} failed with exit code ${e.red(s.exitCode)}.`),logger.error(s.stderr);return}logger.success(`${e.magenta("'git add .'")} completed successfully.`);const w=`git commit${l.split(` `).map(t=>` -m "${t.replace(/"/g,'\\"')}"`).join("")}`;logger.info("Running commit command...");const n=await executeShellCommand(w,i);if(n.exitCode!==0){logger.error(`${e.magenta("'git commit'")} failed with exit code ${e.red(n.exitCode)}.`),logger.error(n.stderr),n.stdout&&logger.log(n.stdout),logger.warn("You may need to resolve commit issues manually.");return}const h=m.map(t=>t.uuid);await markTransactionsAsGitCommitted(i,h),logger.success("\u2705 Git commit successful!"),logger.log(n.stdout);};export{F as gitCommitCommand};//# sourceMappingURL=git-commit.js.map //# sourceMappingURL=git-commit.js.map