diff --git a/src/index.ts b/src/index.ts index c25570b..200ac9e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,14 +5,7 @@ import Git from './git/git' import { log } from './git/log' import { MockStorage } from './storage/MockStorage' -<<<<<<< HEAD - - - -let git: Git -======= let git: Git; ->>>>>>> 35ca1be (wip git3) GitRemoteHelper({ env: process.env, stdin: process.stdin, diff --git a/src/storage/MockStorage.ts b/src/storage/MockStorage.ts new file mode 100644 index 0000000..6dd6edf --- /dev/null +++ b/src/storage/MockStorage.ts @@ -0,0 +1,74 @@ +import { promises as fs } from 'fs' +import pathUtil from 'path' +import { Ref, Status, Storage } from "./storage" +import { superpathjoin as join } from 'superpathjoin' +const mockPath = process.env.HOME + "/.git3/mock" +fs.mkdir(mockPath, { recursive: true }) +const log = console.error +log("mock path", mockPath) + +export class MockStorage implements Storage { + repoURI: string + + constructor(repoURI: string) { + this.repoURI = repoURI + } + + async listRefs(): Promise { + let stPath = join(mockPath, "refs.json") + try { + let refsJson = await fs.readFile(stPath) + let dict = JSON.parse(refsJson.toString()) + let list = [] + for (let key in dict) { + list.push({ ref: key, sha: dict[key] }) + } + return list + } + + catch (e) { + return [] + } + + } + async setRef(path: string, sha: string): Promise { + let dict + let stPath = join(mockPath, "refs.json") + try { + let refsJson = await fs.readFile(stPath) + dict = JSON.parse(refsJson.toString()) + } + catch (e) { + dict = {} + await fs.mkdir(pathUtil.dirname(stPath), { recursive: true }) + } + + dict[path] = sha + await fs.writeFile(stPath, JSON.stringify(dict)) + return Status.SUCCEED + } + async removeRef(path: string): Promise { + let stPath = join(mockPath, "refs.json") + let refsJson = await fs.readFile(stPath) + let dict = JSON.parse(refsJson.toString()) + delete dict[path] + await fs.writeFile(stPath, JSON.stringify(dict)) + return Status.SUCCEED + } + + async remove(path: string): Promise { + throw new Error("Method not implemented.") + } + async download(path: string): Promise<[Status, Buffer]> { + let buffer = await fs.readFile(join(mockPath, path)) + return [Status.SUCCEED, buffer] + } + + + async upload(path: string, file: Buffer): Promise { + let stPath = join(mockPath, path) + await fs.mkdir(pathUtil.dirname(stPath), { recursive: true }) + await fs.writeFile(stPath, file) + return Status.SUCCEED + } +} \ No newline at end of file