Skip to content
Snippets Groups Projects
Select Git revision
1 result Searching

storage.js

Blame
  • storage.js 10.36 KiB
    const fs = require('fs-extra');
    const { join } = require('path');
    const path = require('path');
    const URL = require('url');
    const Git = require('simple-git/promise');
    const convertHrtime = require('convert-hrtime');
    
    class Storage {
      constructor() {
        let config = {};
        let git = null;
        let cwd = null;
    
        Object.assign(this, {
          initRepo,
          cleanRepo,
          getRepoStatus,
          setBaseBranch,
          branchExists,
          commitFilesToBranch,
          createBranch,
          deleteBranch,
          getAllRenovateBranches,
          getBranchCommit,
          getBranchLastCommitTime,
          getCommitMessages,
          getFile,
          getFileList,
          isBranchStale,
          mergeBranch,
        });
    
        // istanbul ignore next
        async function resetToBranch(branchName) {
          await git.raw(['reset', '--hard']);
          await git.checkout(branchName);
          await git.raw(['reset', '--hard', 'origin/' + branchName]);
        }
    
        // istanbul ignore next
        async function cleanLocalBranches() {
          const existingBranches = (await git.raw(['branch']))
            .split('\n')
            .map(branch => branch.trim())
            .filter(branch => branch.length)
            .filter(branch => !branch.startsWith('* '));
          logger.debug({ existingBranches });
          for (const branchName of existingBranches) {
            await deleteLocalBranch(branchName);
          }
        }
    
        async function initRepo(args) {
          cleanRepo();
          config = { ...args };
          cwd = config.localDir;
          logger.info('Initialising git repository into ' + cwd);
          const gitHead = path.join(cwd, '.git/HEAD');
          let clone = true;
          async function determineBaseBranch() {
            // see https://stackoverflow.com/a/44750379/1438522
            try {
              config.baseBranch =
                config.baseBranch ||
                (await git.raw(['symbolic-ref', 'refs/remotes/origin/HEAD']))
                  .replace('refs/remotes/origin/', '')
                  .trim();
            } catch (err) /* istanbul ignore next */ {
              if (
                err.message.startsWith(