Skip to content

Commit

Permalink
feat: Create temporary branch as working branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mob-sakai committed Oct 25, 2019
1 parent 8a2cff6 commit 8947cce
Showing 1 changed file with 43 additions and 30 deletions.
73 changes: 43 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}`);
Expand All @@ -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.
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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);
}
}
}

Expand Down

0 comments on commit 8947cce

Please sign in to comment.