OverviewFilesCommitsIssuesDocsReleasesPeers

pear-git / lib / backends / base.js

161 lines · javascript ref:
/**
 * Repository Backend Base Class
 *
 * Abstract interface for different storage strategies.
 * All backends must implement these methods.
 */
export class RepositoryBackend {
  /**
   * Get repository information
   */
  async getRepoInfo(repoName) {
    throw new Error('getRepoInfo() must be implemented by subclass')
  }

  /**
   * List files in a directory
   * @param {string} repoName - Repository name
   * @param {string} ref - Git ref (branch, tag, commit hash)
   * @param {string} path - Directory path
   * @returns {Promise<Array>} Array of file objects
   */
  async listFiles(repoName, ref, path) {
    throw new Error('listFiles() must be implemented by subclass')
  }

  /**
   * Get commit history
   * @param {string} repoName - Repository name
   * @param {string} ref - Git ref
   * @param {number} limit - Maximum number of commits
   * @returns {Promise<Array>} Array of commit objects
   */
  async getCommits(repoName, ref, limit) {
    throw new Error('getCommits() must be implemented by subclass')
  }

  /**
   * Read file contents
   * @param {string} repoName - Repository name
   * @param {string} filePath - File path
   * @param {string} ref - Git ref
   * @returns {Promise<string>} File contents
   */
  async readFile(repoName, filePath, ref) {
    throw new Error('readFile() must be implemented by subclass')
  }

  /**
   * Get branches
   * @param {string} repoName - Repository name
   * @returns {Promise<Array>} Array of branch objects
   */
  async getBranches(repoName) {
    throw new Error('getBranches() must be implemented by subclass')
  }

  /**
   * Get tags
   * @param {string} repoName - Repository name
   * @returns {Promise<Array>} Array of tag names
   */
  async getTags(repoName) {
    throw new Error('getTags() must be implemented by subclass')
  }

  /**
   * Get repository statistics
   * @param {string} repoName - Repository name
   * @returns {Promise<Object>} Stats object
   */
  async getStats(repoName) {
    throw new Error('getStats() must be implemented by subclass')
  }

  /**
   * Get README content
   * @param {string} repoName - Repository name
   * @param {string} ref - Git ref
   * @returns {Promise<Object|null>} README object or null
   */
  async getReadme(repoName, ref) {
    throw new Error('getReadme() must be implemented by subclass')
  }

  /**
   * Get submodules from .gitmodules
   * @param {string} repoName - Repository name
   * @param {string} ref - Git ref
   * @returns {Promise<Array>} Array of submodule objects
   */
  async getSubmodules(repoName, ref) {
    throw new Error('getSubmodules() must be implemented by subclass')
  }

  /**
   * Get specific submodule info
   * @param {string} repoName - Repository name
   * @param {string} submodulePath - Submodule path
   * @param {string} ref - Git ref
   * @returns {Promise<Object|null>} Submodule info or null
   */
  async getSubmoduleInfo(repoName, submodulePath, ref) {
    throw new Error('getSubmoduleInfo() must be implemented by subclass')
  }

  /**
   * Parse repo name from git URL
   * @param {string} url - Git URL
   * @returns {string|null} Repository name
   */
  parseRepoNameFromURL(url) {
    throw new Error('parseRepoNameFromURL() must be implemented by subclass')
  }

  // P2P Support methods

  /**
   * Check if this backend supports P2P replication
   * @returns {boolean}
   */
  canReplicate() {
    return false
  }

  /**
   * Get replication key for P2P distribution
   * @param {string} repoName - Repository name
   * @returns {Promise<string|null>} Replication key (hex string) or null
   */
  async getReplicationKey(repoName) {
    return null
  }

  /**
   * Get storage size used by this backend for a repo
   * @param {string} repoName - Repository name
   * @returns {Promise<number>} Size in bytes
   */
  async getStorageSize(repoName) {
    return 0
  }

  // Backend metadata

  /**
   * Get strategy name
   * @returns {string}
   */
  getStrategy() {
    return 'unknown'
  }

  /**
   * Get human-readable description
   * @returns {string}
   */
  getDescription() {
    return 'Unknown backend strategy'
  }
}