ignore: fix file read with diff

This commit is contained in:
Dax Raad 2025-07-01 20:06:11 -04:00
parent b199a609a8
commit c68aeed8d9
2 changed files with 25 additions and 17 deletions

View file

@ -1,7 +1,6 @@
import { File } from "../../../file" import { File } from "../../../file"
import { bootstrap } from "../../bootstrap" import { bootstrap } from "../../bootstrap"
import { cmd } from "../cmd" import { cmd } from "../cmd"
import path from "path"
export const FileCommand = cmd({ export const FileCommand = cmd({
command: "file", command: "file",
@ -19,7 +18,7 @@ const FileReadCommand = cmd({
}), }),
async handler(args) { async handler(args) {
await bootstrap({ cwd: process.cwd() }, async () => { await bootstrap({ cwd: process.cwd() }, async () => {
const content = await File.read(path.resolve(args.path)) const content = await File.read(args.path)
console.log(content) console.log(content)
}) })
}, },

View file

@ -3,8 +3,14 @@ import { Bus } from "../bus"
import { $ } from "bun" import { $ } from "bun"
import { createPatch } from "diff" import { createPatch } from "diff"
import path from "path" import path from "path"
import { status } from "isomorphic-git"
import { App } from "../app/app"
import fs from "fs"
import { Log } from "../util/log"
export namespace File { export namespace File {
const log = Log.create({ service: "files" })
export const Event = { export const Event = {
Edited: Bus.event( Edited: Bus.event(
"file.edited", "file.edited",
@ -15,21 +21,24 @@ export namespace File {
} }
export async function read(file: string) { export async function read(file: string) {
const content = await Bun.file(file).text() using _ = log.time("read", { file })
const gitDiff = await $`git diff HEAD -- ${file}` const app = App.info()
.cwd(path.dirname(file)) const full = path.join(app.path.cwd, file)
.quiet() const content = await Bun.file(full).text()
.nothrow() if (app.git) {
.text() const rel = path.relative(app.path.root, full)
if (gitDiff.trim()) { const diff = await status({
const relativePath = path.relative(process.cwd(), file) fs,
const originalContent = await $`git show HEAD:./${relativePath}` dir: app.path.root,
.cwd(process.cwd()) filepath: rel,
.quiet() })
.nothrow() if (diff !== "unmodified") {
.text() const original = await $`git show HEAD:${rel}`
if (originalContent.trim()) { .cwd(app.path.root)
const patch = createPatch(file, originalContent, content) .quiet()
.nothrow()
.text()
const patch = createPatch(file, original, content)
return patch return patch
} }
} }