anbot

f432b40 / repository.mjs

View file at this commit

diff --git a/repository.mjs b/repository.mjs
index a3f12ca..1e3413a 100644
--- a/repository.mjs
+++ b/repository.mjs
@@ -249,8 +249,42 @@ export function readCommit(bare, hash) {
   return { hash: found, author, email, date, message: rest.join('\x00') }
 }
 
-export function commitStat(bare, hash) {
-  return git(bare, 'show', '--stat', '--format=', hash)
+export function commitFiles(bare, hash) {
+  const output = git(bare, 'show', '--numstat', '-z', '--format=', hash)
+  const tokens = output.split('\0').filter(Boolean)
+  const files = []
+  for (let index = 0; index < tokens.length; index++) {
+    const match = /^(\d+|-)\t(\d+|-)\t(.*)$/.exec(tokens[index])
+    if (!match) continue
+    let path = match[3]
+    if (!path) {
+      // A rename with -z carries the old and new paths as separate tokens.
+      index += 2
+      path = tokens[index]
+      if (!path) continue
+    }
+    const count = value => value === '-' ? null : Number(value)
+    files.push({ path, added: count(match[1]), removed: count(match[2]) })
+  }
+  return files
+}
+
+const maxDiffShown = 262144
+
+export function commitDiff(bare, hash, path) {
+  const bytes = gitBuffer(bare, 'show', '--format=', '--no-ext-diff', hash, '--', path)
+  if (bytes.length === 0) return null
+  if (bytes.length > maxDiffShown) {
+    return {
+      truncated: true, shownBytes: maxDiffShown,
+      text: new TextDecoder().decode(bytes.subarray(0, maxDiffShown))
+    }
+  }
+  return { text: new TextDecoder().decode(bytes) }
+}
+
+export function blobExists(bare, ref, path) {
+  return gitOrNull(bare, 'cat-file', '-t', `${ref}:${path}`) === 'blob'
 }
 
 export function listTree(bare, ref, path) {

Git ยท Social feed