import util from 'util'
import childProcess from 'child_process'
const exec = util.promisify(childProcess.exec)
import {getDarcsDiffsFromPatchText} from '../../helpers.ts'
export const getFileList = async(repoName: string, branchName: string, repoLocation: string) => {
const command = "darcs show files"
const result = await exec(`(cd ${repoLocation} && ${command})`)
const files = result.stdout.split("\n").filter(item => item.length > 0 && item != ".")
return files
}
export const getBranchInfo = async (repoName: string, branchName: string, repoLocation: string) => {
const patches = new Map()
const totalPatchesCountRes = await exec(`(cd ${repoLocation} && darcs log --count)`)
const totalPatchesCount = parseInt(totalPatchesCountRes.stdout)
let hunkRegex = RegExp(/^ *hunk /)
for (let i = 1; i <= totalPatchesCount; i = i + 100) {
let patchesSubsetRes = await exec(`(cd ${repoLocation} && darcs log --index=${i}-${i+100} -v)`)
let patchesSubset = patchesSubsetRes.stdout.split("\n")
do {
const nextPatchStart = patchesSubset.findIndex((line, index) => {
return (index > 0 && line.startsWith("patch ")) || index === patchesSubset.length - 1
})
const isEndOfFile = nextPatchStart === patchesSubset.length - 1
const currentPatch = patchesSubset.slice(0, isEndOfFile ? nextPatchStart : nextPatchStart - 1)
patchesSubset = patchesSubset.slice(nextPatchStart)
const hash = currentPatch[0].replace("patch ", "").trim()
const author = currentPatch[1].replace("Author: ", "").trim()
const date = currentPatch[2].replace("Date: ", "").trim()
const name = currentPatch[3].replace(" * ", "").trim()
const diffStart = currentPatch.findIndex((line) => {
return line.match(hunkRegex)
})
const description = currentPatch.slice(5, diffStart).map(str => str.replace(" ", "")).join("\n").trim()
const diffs = getDarcsDiffsFromPatchText(currentPatch.slice(diffStart).map(str => str.trimStart()).join("\n"))
patches.set(hash, {
name,
description,
author,
date,
hash,
diffs,
})
} while (patchesSubset.length > 1)
}
return Array.from(patches.values())
}
export const getFileLastTouchInfo = async (repoName: string, branchName: string, filename: string, repoLocation: string) => {
const command = `darcs annotate --machine-readable ${filename}`
const res = await exec(`(cd ${repoLocation} && ${command})`)
const output = res.stdout
const outputLines = output.split("\n").map((line) => {
return line.split(' ')[0]
})
return outputLines.map((line) => {
return {sha: line, author: ''}
})
}