From 8947ccefebc29a772376f115394b2116d356e325 Mon Sep 17 00:00:00 2001 From: mob-sakai Date: Fri, 25 Oct 2019 19:23:27 +0900 Subject: [PATCH] feat: Create temporary branch as working branch --- index.js | 73 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 43 insertions(+), 30 deletions(-) diff --git a/index.js b/index.js index 9f7a1eb..0cda7ad 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,6 @@ const git = require('./lib/git'); /** * Take a snapshot of the directory and creates/updates another branch, like `git subtree split --squash`. * - * @param {String} [pluginConfig.message] The message for the release commit. * @param {Object} argv git-snapshot options. * @param {String} argv.prefix the name of the subdir to split out * @param {String} argv.branch the name of branch for split to @@ -19,27 +18,29 @@ const git = require('./lib/git'); * @param {String} argv.remote remote repository * @param {Boolean} argv.dryRun skip publishing * @param {String} argv.cwd working directory + * @param {Boolean} argv.debug output debugging information */ async function gitSnapshot(argv) { - const {prefix, branch, message, auther, force, tag, remote, dryRun, cwd} = argv; + const {prefix, branch, message, auther, force, tag, remote, dryRun, cwd} = {cwd: process.cwd(), ...argv}; + + if (argv.debug) { + // Debug must be enabled before other requires in order to work + require('debug').enable('git-snapshot:*'); + } let isAddedWorktree = false; let isAddedTag = false; + let isAddedWorkBranch = false; let failed = false; const worktreePath = tempy.directory(); + const workBranch = path.basename(worktreePath); const refs = remote ? `refs/remotes/${remote}/${branch}` : `refs/heads/${branch}`; - const onCwdOpts = { - cwd - }; - const onWorktreeOpts = { - cwd: worktreePath - }; + const onCwdOpts = {cwd}; + const onWorktreeOpts = {cwd: worktreePath}; const autherOpt = auther ? ['--auther', auther] : []; - const prefixPath = path.join(cwd, prefix); + const prefixPath = path.resolve(prefix); const forceOpt = force ? '--force' : ''; - debug(`argv: ${JSON.stringify(argv)}`); - try { // Check that cwd and prefix directory exist. debug(`Check that cwd and prefix directory exist: ${cwd} ${prefix}`); @@ -49,11 +50,30 @@ async function gitSnapshot(argv) { throw new Error(`prefix directory '${prefixPath}' is not found`); } + // Add worktree for the task. + debug(`Check that the tag does not exist: ${tag}`); + await git(['worktree', 'add', '--detach', worktreePath], onCwdOpts); + isAddedWorktree = true; + + // Checkout working branch. + debug(`Checkout working branch: ${branch} as ${workBranch} on ${onWorktreeOpts.cwd}`); + await git(['checkout', '-B', workBranch, refs], onWorktreeOpts) // + .catch(async () => { + await git(['checkout', '-B', workBranch, `refs/heads/${branch}`], onWorktreeOpts) // + .catch(async () => { + await git(['checkout', '--orphan', workBranch], onWorktreeOpts); + }); + }); + isAddedWorkBranch = true; + + // Check empty commit + await git(['commit', '--allow-empty', '-m', 'empty commit'].concat(autherOpt), onWorktreeOpts); + // Check permissions for remote branch. if (remote) { - debug(`Check permissions for remote branch: ${remote} ${branch}`); - await git(['push', '--dry-run', '-f', remote, branch], onCwdOpts); + debug(`Check permissions for remote: \`${remote}\` ${branch}`); await git(['fetch', remote], onCwdOpts); + await git(['push', '--dry-run', '-f', remote, `${workBranch}:${branch}`], onCwdOpts); } // Check that the tag does not exist. @@ -71,21 +91,6 @@ async function gitSnapshot(argv) { // In dry-run mode, return. if (dryRun) return; - // Add worktree for the task. - debug(`Check that the tag does not exist: ${tag}`); - await git(['worktree', 'add', '--detach', worktreePath], onCwdOpts); - isAddedWorktree = true; - - // Checkout working branch. - debug(`Checkout working branch: ${onWorktreeOpts.cwd}`); - await git(['checkout', '-B', branch, refs], onWorktreeOpts) // - .catch(async () => { - await git(['checkout', '-B', branch, `refs/heads/${branch}`], onWorktreeOpts) // - .catch(async () => { - await git(['checkout', '--orphan', branch], onWorktreeOpts); - }); - }); - // Remove/copy all files in directory. debug(`Remove/copy all files in directory: ${prefix}`); await git(['rm', '-rf', '--ignore-unmatch', '.'], onWorktreeOpts); @@ -94,7 +99,10 @@ async function gitSnapshot(argv) { // Commit files. debug(`Commit files:`); await git(['add', '-A', '--ignore-errors'], onWorktreeOpts); - await git(['commit', '--allow-empty', '--allow-empty-message', '-m', message] + autherOpt, onWorktreeOpts); + await git( + ['commit', '--amend', '--allow-empty', '--allow-empty-message', '-m', message].concat(autherOpt), + onWorktreeOpts + ); // Add tag. if (tag) { @@ -106,7 +114,7 @@ async function gitSnapshot(argv) { // Push to remote repository. if (remote) { debug(`Push to remote repository: ${remote} ${branch}`); - await git(['push', forceOpt, remote, branch], onWorktreeOpts); + await git(['push', forceOpt, remote, `${workBranch}:${branch}`], onWorktreeOpts); if (tag) { await git(['push', forceOpt, remote, tag], onWorktreeOpts); } @@ -129,6 +137,11 @@ async function gitSnapshot(argv) { if (failed && isAddedTag) { await git(['tag', '-d', tag], onCwdOpts); } + + // Remove added work branch. + if (isAddedWorkBranch) { + await git(['branch', '-D', workBranch], onCwdOpts); + } } }