From 55120452c7d0a9e71a6e7322da06fb04649cff59 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 14 Sep 2025 16:10:36 -0500 Subject: [PATCH] Add git wrapper class --- src/lib/git.example.ts | 108 +++++++++ src/lib/git.ts | 496 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 604 insertions(+) create mode 100644 src/lib/git.example.ts create mode 100644 src/lib/git.ts diff --git a/src/lib/git.example.ts b/src/lib/git.example.ts new file mode 100644 index 00000000..93f73986 --- /dev/null +++ b/src/lib/git.example.ts @@ -0,0 +1,108 @@ +import { Git } from './git'; +import type { JSRuntimeFS } from './JSRuntime'; + +/** + * Example usage of the Git class + */ +export async function exampleGitUsage(fs: JSRuntimeFS) { + // Create a Git instance with fs and optional CORS proxy + const git = new Git(fs, 'https://cors.isomorphic-git.org'); + + // Initialize a new repository + await git.init({ + dir: '/my-project', + defaultBranch: 'main', + }); + + // Set up configuration + await git.setConfig({ + dir: '/my-project', + path: 'user.name', + value: 'John Doe', + }); + + await git.setConfig({ + dir: '/my-project', + path: 'user.email', + value: 'john@example.com', + }); + + // Add files to the index + await git.add({ + dir: '/my-project', + filepath: 'README.md', + }); + + // Make a commit + await git.commit({ + dir: '/my-project', + message: 'Initial commit', + author: { + name: 'John Doe', + email: 'john@example.com', + }, + }); + + // Get repository status + const statusMatrix = await git.statusMatrix({ + dir: '/my-project', + }); + + // List branches + const branches = await git.listBranches({ + dir: '/my-project', + }); + + // Get commit log + const commits = await git.log({ + dir: '/my-project', + depth: 10, + }); + + // Clone a repository + await git.clone({ + dir: '/cloned-project', + url: 'https://github.com/user/repo.git', + singleBranch: true, + depth: 1, + }); + + // Add a remote + await git.addRemote({ + dir: '/my-project', + remote: 'origin', + url: 'https://github.com/user/my-project.git', + }); + + // Push to remote (requires authentication) + try { + await git.push({ + dir: '/my-project', + remote: 'origin', + ref: 'main', + // onAuth would be needed for authentication + }); + } catch (error) { + console.log('Push failed (likely due to authentication):', error); + } + + return { + statusMatrix, + branches, + commits, + }; +} + +/** + * Example for GitHub/GitLab repositories with CORS proxy + */ +export function createGitForGitHub(fs: JSRuntimeFS) { + return new Git(fs, 'https://cors.isomorphic-git.org'); +} + +/** + * Example for self-hosted Git servers without CORS proxy + */ +export function createGitForSelfHosted(fs: JSRuntimeFS) { + return new Git(fs); // No CORS proxy needed +} \ No newline at end of file diff --git a/src/lib/git.ts b/src/lib/git.ts new file mode 100644 index 00000000..65ade641 --- /dev/null +++ b/src/lib/git.ts @@ -0,0 +1,496 @@ +import git from 'isomorphic-git'; +import http from 'isomorphic-git/http/web'; +import type { JSRuntimeFS } from './JSRuntime'; + +/** + * Git class that wraps isomorphic-git and provides a cleaner interface. + * Instantiate with an fs implementation and optional corsProxy string. + * All methods have the same names as isomorphic-git but don't require + * passing fs, http, and corsProxy each time. + */ +export class Git { + private fs: JSRuntimeFS; + private corsProxy?: string; + + constructor(fs: JSRuntimeFS, corsProxy?: string) { + this.fs = fs; + this.corsProxy = corsProxy; + } + + // Repository initialization and configuration + async init(options: Omit[0], 'fs'>) { + return git.init({ + fs: this.fs, + ...options, + }); + } + + async clone(options: Omit[0], 'fs' | 'http' | 'corsProxy'>) { + return git.clone({ + fs: this.fs, + http, + corsProxy: this.corsProxy, + ...options, + }); + } + + // Working with files + async add(options: Omit[0], 'fs'>) { + return git.add({ + fs: this.fs, + ...options, + }); + } + + async remove(options: Omit[0], 'fs'>) { + return git.remove({ + fs: this.fs, + ...options, + }); + } + + async status(options: Omit[0], 'fs'>) { + return git.status({ + fs: this.fs, + ...options, + }); + } + + async statusMatrix(options: Omit[0], 'fs'>) { + return git.statusMatrix({ + fs: this.fs, + ...options, + }); + } + + // Commits and history + async commit(options: Omit[0], 'fs'>) { + return git.commit({ + fs: this.fs, + ...options, + }); + } + + async log(options: Omit[0], 'fs'>) { + return git.log({ + fs: this.fs, + ...options, + }); + } + + async readCommit(options: Omit[0], 'fs'>) { + return git.readCommit({ + fs: this.fs, + ...options, + }); + } + + // Branches + async branch(options: Omit[0], 'fs'>) { + return git.branch({ + fs: this.fs, + ...options, + }); + } + + async checkout(options: Omit[0], 'fs'>) { + return git.checkout({ + fs: this.fs, + ...options, + }); + } + + async currentBranch(options: Omit[0], 'fs'>) { + return git.currentBranch({ + fs: this.fs, + ...options, + }); + } + + async deleteBranch(options: Omit[0], 'fs'>) { + return git.deleteBranch({ + fs: this.fs, + ...options, + }); + } + + async listBranches(options: Omit[0], 'fs'>) { + return git.listBranches({ + fs: this.fs, + ...options, + }); + } + + // Remote operations + async addRemote(options: Omit[0], 'fs'>) { + return git.addRemote({ + fs: this.fs, + ...options, + }); + } + + async deleteRemote(options: Omit[0], 'fs'>) { + return git.deleteRemote({ + fs: this.fs, + ...options, + }); + } + + async listRemotes(options: Omit[0], 'fs'>) { + return git.listRemotes({ + fs: this.fs, + ...options, + }); + } + + async getRemoteInfo(options: Omit[0], 'http' | 'corsProxy'>) { + return git.getRemoteInfo({ + http, + corsProxy: this.corsProxy, + ...options, + }); + } + + async fetch(options: Omit[0], 'fs' | 'http' | 'corsProxy'>) { + return git.fetch({ + fs: this.fs, + http, + corsProxy: this.corsProxy, + ...options, + }); + } + + async pull(options: Omit[0], 'fs' | 'http' | 'corsProxy'>) { + return git.pull({ + fs: this.fs, + http, + corsProxy: this.corsProxy, + ...options, + }); + } + + async push(options: Omit[0], 'fs' | 'http' | 'corsProxy'>) { + return git.push({ + fs: this.fs, + http, + corsProxy: this.corsProxy, + ...options, + }); + } + + // Tags + async tag(options: Omit[0], 'fs'>) { + return git.tag({ + fs: this.fs, + ...options, + }); + } + + async deleteTag(options: Omit[0], 'fs'>) { + return git.deleteTag({ + fs: this.fs, + ...options, + }); + } + + async listTags(options: Omit[0], 'fs'>) { + return git.listTags({ + fs: this.fs, + ...options, + }); + } + + // Configuration + async getConfig(options: Omit[0], 'fs'>) { + return git.getConfig({ + fs: this.fs, + ...options, + }); + } + + async setConfig(options: Omit[0], 'fs'>) { + return git.setConfig({ + fs: this.fs, + ...options, + }); + } + + async getConfigAll(options: Omit[0], 'fs'>) { + return git.getConfigAll({ + fs: this.fs, + ...options, + }); + } + + // Walking and comparisons + + async walk(options: Omit[0], 'fs'>) { + return git.walk({ + fs: this.fs, + ...options, + }); + } + + // Reset operations + async resetIndex(options: Omit[0], 'fs'>) { + return git.resetIndex({ + fs: this.fs, + ...options, + }); + } + + // Object operations + async readObject(options: Omit[0], 'fs'>) { + return git.readObject({ + fs: this.fs, + ...options, + }); + } + + async writeObject(options: Omit[0], 'fs'>) { + return git.writeObject({ + fs: this.fs, + ...options, + }); + } + + async readTree(options: Omit[0], 'fs'>) { + return git.readTree({ + fs: this.fs, + ...options, + }); + } + + async writeTree(options: Omit[0], 'fs'>) { + return git.writeTree({ + fs: this.fs, + ...options, + }); + } + + async readBlob(options: Omit[0], 'fs'>) { + return git.readBlob({ + fs: this.fs, + ...options, + }); + } + + async writeBlob(options: Omit[0], 'fs'>) { + return git.writeBlob({ + fs: this.fs, + ...options, + }); + } + + // Index operations + async updateIndex(options: Omit[0], 'fs'>) { + return git.updateIndex({ + fs: this.fs, + ...options, + }); + } + + // Merge operations + async merge(options: Omit[0], 'fs'>) { + return git.merge({ + fs: this.fs, + ...options, + }); + } + + async findMergeBase(options: Omit[0], 'fs'>) { + return git.findMergeBase({ + fs: this.fs, + ...options, + }); + } + + // Utilities + async isDescendent(options: Omit[0], 'fs'>) { + return git.isDescendent({ + fs: this.fs, + ...options, + }); + } + + async findRoot(options: Omit[0], 'fs'>) { + return git.findRoot({ + fs: this.fs, + ...options, + }); + } + + async getRemoteInfo2(options: Omit[0], 'http' | 'corsProxy'>) { + return git.getRemoteInfo2({ + http, + corsProxy: this.corsProxy, + ...options, + }); + } + + async listFiles(options: Omit[0], 'fs'>) { + return git.listFiles({ + fs: this.fs, + ...options, + }); + } + + async hashBlob(options: Parameters[0]) { + return git.hashBlob(options); + } + + async annotatedTag(options: Omit[0], 'fs'>) { + return git.annotatedTag({ + fs: this.fs, + ...options, + }); + } + + async readTag(options: Omit[0], 'fs'>) { + return git.readTag({ + fs: this.fs, + ...options, + }); + } + + async writeTag(options: Omit[0], 'fs'>) { + return git.writeTag({ + fs: this.fs, + ...options, + }); + } + + async writeRef(options: Omit[0], 'fs'>) { + return git.writeRef({ + fs: this.fs, + ...options, + }); + } + + async deleteRef(options: Omit[0], 'fs'>) { + return git.deleteRef({ + fs: this.fs, + ...options, + }); + } + + async listNotes(options: Omit[0], 'fs'>) { + return git.listNotes({ + fs: this.fs, + ...options, + }); + } + + async readNote(options: Omit[0], 'fs'>) { + return git.readNote({ + fs: this.fs, + ...options, + }); + } + + async addNote(options: Omit[0], 'fs'>) { + return git.addNote({ + fs: this.fs, + ...options, + }); + } + + async removeNote(options: Omit[0], 'fs'>) { + return git.removeNote({ + fs: this.fs, + ...options, + }); + } + + // Packfile operations + async packObjects(options: Omit[0], 'fs'>) { + return git.packObjects({ + fs: this.fs, + ...options, + }); + } + + async indexPack(options: Omit[0], 'fs'>) { + return git.indexPack({ + fs: this.fs, + ...options, + }); + } + + // Additional utility methods + async resolveRef(options: Omit[0], 'fs'>) { + return git.resolveRef({ + fs: this.fs, + ...options, + }); + } + + async expandRef(options: Omit[0], 'fs'>) { + return git.expandRef({ + fs: this.fs, + ...options, + }); + } + + async expandOid(options: Omit[0], 'fs'>) { + return git.expandOid({ + fs: this.fs, + ...options, + }); + } + + async listRefs(options: Omit[0], 'fs'>) { + return git.listRefs({ + fs: this.fs, + ...options, + }); + } + + async listServerRefs(options: Omit[0], 'http' | 'corsProxy'>) { + return git.listServerRefs({ + http, + corsProxy: this.corsProxy, + ...options, + }); + } + + async renameBranch(options: Omit[0], 'fs'>) { + return git.renameBranch({ + fs: this.fs, + ...options, + }); + } + + async isIgnored(options: Omit[0], 'fs'>) { + return git.isIgnored({ + fs: this.fs, + ...options, + }); + } + + async fastForward(options: Omit[0], 'fs'>) { + return git.fastForward({ + fs: this.fs, + ...options, + }); + } + + async abortMerge(options: Omit[0], 'fs'>) { + return git.abortMerge({ + fs: this.fs, + ...options, + }); + } + + async writeCommit(options: Omit[0], 'fs'>) { + return git.writeCommit({ + fs: this.fs, + ...options, + }); + } + + // Version info + version() { + return git.version(); + } +} \ No newline at end of file