UNPKG

git-intent

Version:

Git workflow tool for intentional commits — define your commit intentions first for clearer, more atomic changes.

709 lines (690 loc) 23.4 kB
#!/usr/bin/env node // src/index.ts import { program } from "commander"; // src/utils/storage.ts import path3 from "node:path"; import fs3 from "fs-extra"; // src/utils/generateId.ts import { nanoid } from "nanoid"; function generateId(size) { return nanoid(size); } // src/utils/get-package-info.ts import path from "node:path"; import fs from "fs-extra"; function getPackageInfo() { const possiblePaths = [ path.resolve(__dirname, "../../package.json"), path.resolve(__dirname, "../package.json"), path.resolve(process.cwd(), "package.json") ]; for (const packageJsonPath of possiblePaths) { try { if (fs.existsSync(packageJsonPath)) { const packageJson = fs.readJSONSync(packageJsonPath); if (packageJson.name === "git-intent") { return { version: packageJson.version || "0.0.0", description: packageJson.description || "Git Intent CLI" }; } } } catch (error) { } } return { version: "0.0.0", description: "Git Intent CLI" }; } // src/utils/git.ts import { spawnSync } from "node:child_process"; import path2 from "node:path"; import fs2 from "fs-extra"; import { simpleGit } from "simple-git"; function execGit(args, options = {}) { const { input, cwd } = options; const result = spawnSync("git", args, { input: input ? Buffer.from(input) : void 0, cwd, encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }); if (result.status !== 0) { throw new Error(`Git command failed: git ${args.join(" ")} ${result.stderr}`); } return result.stdout ? result.stdout.trim() : ""; } var createGit = (cwd) => simpleGit(cwd); async function findGitRoot(startDir = process.cwd()) { const dir = path2.resolve(startDir); const gitDir = path2.join(dir, ".git"); if (fs2.existsSync(gitDir) && fs2.statSync(gitDir).isDirectory()) { return dir; } const parentDir = path2.dirname(dir); if (parentDir === dir) { throw new Error("Not a git repository (or any of the parent directories)"); } return findGitRoot(parentDir); } async function checkIsRepo(cwd) { try { const git = createGit(cwd); await git.checkIsRepo(); return await findGitRoot(cwd); } catch { try { return await findGitRoot(cwd); } catch (error) { throw new Error("Not a git repository (or any of the parent directories)"); } } } async function getCurrentBranch(cwd) { const git = createGit(cwd); const branch = await git.revparse(["--abbrev-ref", "HEAD"]); return branch.trim(); } async function createCommit(message, cwd) { const git = createGit(cwd); const result = await git.commit(message); return result.commit; } async function hashObject(content, cwd) { return execGit(["hash-object", "-w", "--stdin"], { input: content, cwd }); } async function createTree(treeContent, cwd) { if (!treeContent || treeContent.trim() === "") { throw new Error("Invalid tree content: tree content cannot be empty"); } return execGit(["mktree"], { input: treeContent, cwd }); } async function createCommitTree(treeHash, message, cwd) { if (!treeHash || treeHash.trim() === "") { throw new Error("Invalid tree hash: tree hash cannot be empty"); } try { const result = execGit(["commit-tree", treeHash, "-m", message], { cwd }); if (!result || result.trim() === "") { throw new Error(`Failed to create commit tree from hash: ${treeHash}`); } return result; } catch (error) { console.error("Error creating commit tree:", error); throw error; } } async function updateRef(refName, commitHash, cwd) { if (!commitHash || commitHash.trim() === "") { throw new Error(`Invalid commit hash: commit hash cannot be empty for ref ${refName}`); } const git = createGit(cwd); await git.raw(["update-ref", refName, commitHash]); } async function deleteRef(refName, cwd) { const git = createGit(cwd); await git.raw(["update-ref", "-d", refName]); } async function checkRefExists(refName, cwd) { const git = createGit(cwd); try { await git.raw(["show-ref", "--verify", refName]); return true; } catch { return false; } } var git_default = createGit(); // src/utils/storage.ts var GitIntentionalCommitStorage = class _GitIntentionalCommitStorage { static instance; REFS_PREFIX = "refs/intentional-commits"; storageFilename; GIT_DIR = ".git"; gitRoot; constructor() { this.storageFilename = process.env.VITEST ? "test_intents.json" : "intents.json"; } static getInstance() { if (!_GitIntentionalCommitStorage.instance) { _GitIntentionalCommitStorage.instance = new _GitIntentionalCommitStorage(); } return _GitIntentionalCommitStorage.instance; } setGitRoot(root) { this.gitRoot = root; } async getGitRoot() { if (this.gitRoot) return this.gitRoot; return process.cwd(); } async getCommitsDir() { const root = await this.getGitRoot(); return path3.join(root, this.GIT_DIR, "intentional-commits"); } async getCommitsFile() { const commitsDir = await this.getCommitsDir(); return path3.join(commitsDir, "commits.json"); } getInitialData() { return { version: getPackageInfo().version, commits: [] }; } async ensureCommitsDir() { const commitsDir = await this.getCommitsDir(); const commitsFile = await this.getCommitsFile(); await fs3.ensureDir(commitsDir); try { await fs3.access(commitsFile); } catch { await fs3.writeJSON(commitsFile, this.getInitialData()); } } migrateData(data) { return data; } async loadCommits() { const root = await this.getGitRoot(); await checkIsRepo(root); await this.ensureCommitsDir(); try { const result = await git_default.cwd(root).show(`${this.REFS_PREFIX}/commits:${this.storageFilename}`); const data = this.migrateData(JSON.parse(result)); return data.commits; } catch { const commitsFile = await this.getCommitsFile(); const data = this.migrateData(await fs3.readJSON(commitsFile)); return data.commits; } } async saveCommitsData(data) { const root = await this.getGitRoot(); const commitsFile = await this.getCommitsFile(); const content = JSON.stringify(data, null, 2); const hash = await hashObject(content, root); const treeContent = `100644 blob ${hash} ${this.storageFilename} `; const treeHash = await createTree(treeContent, root); const commitHash = await createCommitTree(treeHash, "Update intent commits", root); await updateRef(`${this.REFS_PREFIX}/commits`, commitHash, root); await fs3.writeJSON(commitsFile, data, { spaces: 2 }); } async saveCommits(commits) { const data = { version: getPackageInfo().version, commits }; await this.saveCommitsData(data); } async addCommit(commit2) { const currentCommits = await this.loadCommits(); const newCommitId = generateId(8); const newCommit = { ...commit2, id: newCommitId }; const data = { version: getPackageInfo().version, commits: [...currentCommits, newCommit] }; await this.saveCommitsData(data); return newCommitId; } async updateCommitMessage(id, message) { const currentCommits = await this.loadCommits(); const existingCommit = currentCommits.find((c) => c.id === id); if (!existingCommit) { throw new Error("Commit not found"); } const data = { version: getPackageInfo().version, commits: currentCommits.map((c) => c.id === id ? { ...existingCommit, message } : c) }; await this.saveCommitsData(data); } async deleteCommit(id) { const currentCommits = await this.loadCommits(); const newCommits = currentCommits.filter((c) => c.id !== id); await this.saveCommits(newCommits); } async clearCommits() { const root = await this.getGitRoot(); const commitsFile = await this.getCommitsFile(); await fs3.remove(commitsFile); await deleteRef(`${this.REFS_PREFIX}/commits`, root); } async initializeRefs() { const root = await this.getGitRoot(); await checkIsRepo(root); const refExists = await checkRefExists(`${this.REFS_PREFIX}/commits`, root); if (!refExists) { const initialData = this.getInitialData(); const content = JSON.stringify(initialData, null, 2); const hash = await hashObject(content, root); const treeContent = `100644 blob ${hash} ${this.storageFilename} `; const treeHash = await createTree(treeContent, root); const commitHash = await createCommitTree(treeHash, "Initialize intent commits", root); if (!commitHash || commitHash.trim() === "") { throw new Error("Failed to create commit: commit hash is empty"); } await updateRef(`${this.REFS_PREFIX}/commits`, commitHash, root); } } }; var storage = GitIntentionalCommitStorage.getInstance(); // src/commands/list.ts import chalk from "chalk"; import { Command } from "commander"; var list = new Command().command("list").description("List all intentional commits").action(async () => { const commits = await storage.loadCommits(); if (commits.length === 0) { console.log("No intents found"); return; } console.log("\nCreated:"); const createdCommits = commits.filter((commit2) => commit2.status === "created"); for (const commit2 of createdCommits) { console.log(chalk.white(` [${commit2.id}] ${commit2.message}`)); } console.log("\nIn Progress:"); const inProgressCommits = commits.filter((commit2) => commit2.status === "in_progress"); for (const commit2 of inProgressCommits) { console.log(chalk.blue(` [${commit2.id}] ${commit2.message}`)); } }); var list_default = list; // src/commands/start.ts import chalk2 from "chalk"; import { Command as Command2 } from "commander"; import prompts from "prompts"; var start = new Command2().command("start").argument("[id]", "Intent ID").description("Start working on a planned intent").action(async (id) => { const commits = await storage.loadCommits(); let selectedId = id; if (!selectedId) { const createdCommits = commits.filter((c) => c.status === "created"); if (createdCommits.length === 0) { console.log("No created intents found"); return; } const response = await prompts({ type: "select", name: "id", message: "Select an intent to start:", choices: createdCommits.map((c) => ({ title: `${c.message} (${c.id})`, value: c.id })) }); selectedId = response.id; } if (!selectedId) { console.error("No intent selected"); return; } const targetCommit = commits.find((c) => c.id === selectedId); if (!targetCommit) { console.error("Intent not found"); return; } if (targetCommit.status !== "created") { console.error("Intent is not in created status"); return; } const currentBranch = await getCurrentBranch(); targetCommit.status = "in_progress"; targetCommit.metadata.startedAt = (/* @__PURE__ */ new Date()).toISOString(); targetCommit.metadata.branch = currentBranch; await storage.saveCommits(commits); console.log(chalk2.green("\u2713 Started working on:")); console.log(`ID: ${chalk2.blue(targetCommit.id)}`); console.log(`Message: ${targetCommit.message}`); }); var start_default = start; // src/commands/show.ts import chalk3 from "chalk"; import { Command as Command3 } from "commander"; var show = new Command3().command("show").description("Show current intention").action(async () => { const commits = await storage.loadCommits(); const currentCommit = commits.find((c) => c.status === "in_progress"); if (!currentCommit) { console.log("No active intention"); return; } console.log(chalk3.blue("Current intention:")); console.log(`ID: ${chalk3.dim(currentCommit.id)}`); console.log(`Message: ${currentCommit.message}`); console.log(`Status: ${chalk3.yellow(currentCommit.status)}`); console.log(`Created at: ${new Date(currentCommit.metadata.createdAt).toLocaleString()}`); }); var show_default = show; // src/commands/commit.ts import chalk4 from "chalk"; import { Command as Command4 } from "commander"; var commit = new Command4().command("commit").description("Complete current intention and commit").option("-m, --message <message>", "Additional commit message").action(async (options) => { const commits = await storage.loadCommits(); const currentCommit = commits.find((c) => c.status === "in_progress"); if (!currentCommit) { console.log("No active intention"); return; } const message = options.message ? `${currentCommit.message} ${options.message}` : currentCommit.message; await createCommit(message); await storage.deleteCommit(currentCommit.id); console.log(chalk4.green("\u2713 Intention completed and committed")); }); var commit_default = commit; // src/commands/drop.ts import chalk5 from "chalk"; import { Command as Command5 } from "commander"; import prompts2 from "prompts"; var drop = new Command5().command("drop").description("Drop a planned intent").argument("[id]", "Intent ID").option("-a, --all", "Drop all created intents").action(async (id, options) => { const commits = await storage.loadCommits(); const createdCommits = commits.filter((c) => c.status === "created"); if (options.all) { await storage.saveCommits([]); console.log(chalk5.green("\u2713 All created intents removed")); return; } let selectedId = id; if (!selectedId) { if (createdCommits.length === 0) { console.log("No created intents found. Nothing to remove."); return; } const response = await prompts2({ type: "select", name: "id", message: "Select an intent to remove:", choices: createdCommits.map((c) => ({ title: `${c.message} (${c.id})`, value: c.id })) }); selectedId = response.id; } if (!selectedId) { console.error("No intent selected"); return; } const targetCommit = commits.find((c) => c.id === selectedId); if (!targetCommit) { console.error("Intent not found"); return; } if (targetCommit.status !== "created") { console.error("Can only remove intents in created status"); return; } const updatedCommits = commits.filter((c) => c.id !== selectedId); await storage.saveCommits(updatedCommits); console.log(chalk5.green("\u2713 Intent removed:")); console.log(`ID: ${chalk5.blue(targetCommit.id)}`); console.log(`Message: ${targetCommit.message}`); }); var drop_default = drop; // src/commands/cancel.ts import chalk6 from "chalk"; import { Command as Command6 } from "commander"; import prompts3 from "prompts"; var cancel = new Command6().command("cancel").description("Cancel current intention").action(async () => { const commits = await storage.loadCommits(); const currentCommit = commits.find((c) => c.status === "in_progress"); if (!currentCommit) { console.log("No active intention"); return; } const { action } = await prompts3({ type: "select", name: "action", message: "What would you like to do with the intent?", choices: [ { title: "Reset to created status", value: "reset" }, { title: "Delete the intent", value: "delete" } ] }); if (!action) { return; } let updatedCommits; if (action === "reset") { updatedCommits = commits.map( (c) => c.id === currentCommit.id ? { ...c, status: "created", metadata: { ...c.metadata, startedAt: void 0 } } : c ); console.log(chalk6.green("\u2713 Intent reset to created status:")); } else { updatedCommits = commits.filter((c) => c.id !== currentCommit.id); console.log(chalk6.green("\u2713 Intent deleted:")); } await storage.saveCommits(updatedCommits); console.log(`ID: ${chalk6.blue(currentCommit.id)}`); console.log(`Message: ${currentCommit.message}`); console.log("\nNote: Your staged changes are preserved."); }); var cancel_default = cancel; // src/commands/reset.ts import { Command as Command7 } from "commander"; import prompts4 from "prompts"; var reset = new Command7().command("reset").description("Reset all intents").action(async () => { const response = await prompts4({ type: "confirm", name: "reset", message: "Are you sure you want to reset all intents?", initial: false }); if (!response.reset) { return; } await storage.clearCommits(); console.log("All intents reset"); }); var reset_default = reset; // src/commands/divide.ts import chalk7 from "chalk"; import { Command as Command8 } from "commander"; import edit from "external-editor"; import prompts5 from "prompts"; var divide = new Command8().command("divide").description("Divide an intent into smaller parts").action(async () => { const commits = await storage.loadCommits(); if (commits.length === 0) { console.log("No intents found to divide"); return; } const response = await prompts5({ type: "select", name: "id", message: "Select an intent to divide:", choices: commits.map((c) => ({ title: `[${c.status === "created" ? "Created" : "In Progress"}] ${c.message.split("\n")[0]} (${c.id})`, value: c.id })), onState: (state) => { if (state.aborted) { process.nextTick(() => { process.exit(0); }); } } }); const selectedId = response.id; if (!selectedId) { console.log("No intent selected"); return; } const targetCommit = commits.find((c) => c.id === selectedId); if (!targetCommit) { console.error("Intent not found"); return; } console.log(chalk7.blue("\nOriginal commit:"), targetCommit.message); const tasks = []; console.log(chalk7.yellow("\nDividing commit into two tasks.")); console.log( chalk7.dim( "Tip: Enter a title directly for a simple task, or leave it empty to open an editor for a detailed commit message." ) ); console.log(chalk7.blue("\nFirst task:")); const { taskTitle: firstTaskTitle } = await prompts5({ type: "text", name: "taskTitle", message: "Task 1 title:", onState: (state) => { if (state.aborted) { process.nextTick(() => { process.exit(0); }); } } }); if (firstTaskTitle && firstTaskTitle.trim() !== "") { tasks.push(firstTaskTitle.trim()); } else { console.log(chalk7.dim("Opening editor for commit message. Save and close the editor when done.")); let initialText = targetCommit.message; initialText = `# Enter commit message for the first task # Lines starting with # will be ignored ${initialText}`; const message = edit.edit(initialText, { postfix: ".git-intent-divide" }); const fullMessage = message.split("\n").filter((line) => !line.trim().startsWith("#")).join("\n").trim(); if (!fullMessage) { console.log("No message provided for the first task. Operation cancelled."); return; } tasks.push(fullMessage); } console.log(chalk7.blue("\nSecond task:")); const { taskTitle: secondTaskTitle } = await prompts5({ type: "text", name: "taskTitle", message: "Task 2 title:", onState: (state) => { if (state.aborted) { process.nextTick(() => { process.exit(0); }); } } }); if (secondTaskTitle && secondTaskTitle.trim() !== "") { tasks.push(secondTaskTitle.trim()); } else { console.log(chalk7.dim("Opening editor for commit message. Save and close the editor when done.")); const initialText = "# Enter commit message for the second task\n# Lines starting with # will be ignored\n"; const message = edit.edit(initialText, { postfix: ".git-intent-divide" }); const fullMessage = message.split("\n").filter((line) => !line.trim().startsWith("#")).join("\n").trim(); if (!fullMessage) { console.log("No message provided for the second task. Operation cancelled."); return; } tasks.push(fullMessage); } console.log(chalk7.blue("\nTasks to create:")); tasks.forEach((task, index) => { const title = task.split("\n")[0]; console.log(`${index + 1}. ${title}`); if (task.includes("\n")) { const preview = task.split("\n").slice(1).join(" ").trim(); const shortPreview = preview.length > 60 ? `${preview.substring(0, 57)}...` : preview; if (shortPreview) { console.log(` ${chalk7.dim(shortPreview)}`); } } }); const { confirmed } = await prompts5({ type: "confirm", name: "confirmed", message: "Do you want to divide the commit with these tasks?", initial: true, onState: (state) => { if (state.aborted) { process.nextTick(() => { process.exit(0); }); } } }); if (!confirmed) { console.log("Operation cancelled."); return; } const { shouldRemoveOriginal } = await prompts5({ type: "confirm", name: "shouldRemoveOriginal", message: "Do you want to remove the original commit?", initial: false, onState: (state) => { if (state.aborted) { process.nextTick(() => { process.exit(0); }); } } }); const newCommitIds = []; for (const task of tasks) { const newCommitId = await storage.addCommit({ message: task, status: "created", metadata: { createdAt: (/* @__PURE__ */ new Date()).toISOString() } }); newCommitIds.push(newCommitId); } if (shouldRemoveOriginal) { await storage.deleteCommit(targetCommit.id); } console.log(chalk7.green("\u2713 Successfully divided the commit:")); for (let i = 0; i < tasks.length; i++) { const title = tasks[i].split("\n")[0]; console.log(`${i + 1}. ${chalk7.blue(title)} (ID: ${newCommitIds[i]})`); } if (!shouldRemoveOriginal) { console.log(chalk7.yellow("\nOriginal commit was kept:")); const originalTitle = targetCommit.message.split("\n")[0]; console.log(`${chalk7.blue(originalTitle)} (ID: ${targetCommit.id})`); } }); var divide_default = divide; // src/commands/add.ts import chalk8 from "chalk"; import { Command as Command9 } from "commander"; import edit2 from "external-editor"; var add = new Command9().command("add").description("Add a new intentional commit before starting work").argument("[message]", "Intent message").action(async (message) => { let commitMessage = message; if (!commitMessage) { const text = edit2.edit("", { postfix: ".git-intent" }); commitMessage = text.trim(); } if (!commitMessage) { console.error("Commit message is required"); process.exit(1); } const newCommitId = await storage.addCommit({ message: commitMessage, status: "created", metadata: { createdAt: (/* @__PURE__ */ new Date()).toISOString() } }); console.log(chalk8.green("\u2713 Intent created:")); console.log(`ID: ${chalk8.blue(newCommitId)}`); console.log(`Message: ${commitMessage}`); }); var add_default = add; // src/index.ts (async () => { await storage.initializeRefs(); const { version, description } = getPackageInfo(); program.name("git-intent").description(description).version(version).addCommand(add_default).addCommand(list_default).addCommand(start_default).addCommand(show_default).addCommand(commit_default).addCommand(cancel_default).addCommand(reset_default).addCommand(divide_default).addCommand(drop_default); program.parse(); })();