Skip to content

Commit

Permalink
Add tortilla-status
Browse files Browse the repository at this point in the history
  • Loading branch information
DAB0mB committed May 15, 2019
1 parent 4fd9398 commit b8faa14
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,10 @@ Push a tutorial based on the provided branch. e.g. given `master` then `master-h

Pull a tutorial based on the provided branch. e.g. given `master` then `master-history`, `master-root`, `master@0.1.0`, etc, will be pulled.

**command:** `tortilla status`

Will print the tutorial status prior to git-status. If for example, we're editing step 1.2, this will print `Editing step 1.2`. In case there's a conflict, let's say between steps 1.2 and 1.3, this will print `Solving conflict between step 1.2 (HEAD) and step 1.3`.

### tortilla-dump CLI

**command:** `tortilla dump create [out]`
Expand Down
7 changes: 7 additions & 0 deletions src/cli/tortilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ Program
Git.pullTutorial(remote, branch);
});

Program
.command('status')
.description('Print edit status followed by git-status')
.action(() => {
Git.tutorialStatus();
});

Program
.command('dump <command...>', 'Manage dump file')
.command('manual <command...>', 'Manage manual files')
Expand Down
35 changes: 35 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,39 @@ function pullTutorial(remote: string, baseBranch: string) {
}
}

// Used internally by tutorialStatus() to get the right step message
function stepStatus(head = 'HEAD') {
if (getRootHash() === Git(['rev-parse', head])) {
return 'root';
}

const match = Git(['log', head, '-1', '--format=%s']).match(/^Step (\d+(?:\.\d+)?)/);

if (match) {
return `step ${match[1]}`;
}

return Git(['rev-parse', '--short', head]);
}

function isConflicting() {
return Git(['rev-parse', 'HEAD']) !== Git(['rev-parse', 'REBASE_HEAD']);
}

// Print edit status followed by git-status
function tutorialStatus() {
if (isRebasing()) {
if (isConflicting()) {
console.log(`Solving conflict between ${stepStatus('HEAD')} (HEAD) and ${stepStatus('REBASE_HEAD')}`);
}
else {
console.log(`Editing ${stepStatus()}`);
}
}

Git.print(['status']);
}

// The body of the git execution function, useful since we use the same logic both for
// exec and spawn
function gitBody(handler, argv, options) {
Expand Down Expand Up @@ -273,6 +306,8 @@ function getRevisionIdFromObject(object: string): string {
export const Git = Utils.extend(git.bind(null), git, {
pushTutorial,
pullTutorial,
tutorialStatus,
isConflicting,
conflict,
rebasing: isRebasing,
cherryPicking: isCherryPicking,
Expand Down
76 changes: 76 additions & 0 deletions tests/git.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,80 @@ describe('Git', () => {
);
});
});

describe('tutorialStatus()', () => {
it('should show step being edited', () => {
Fs.writeFileSync(`${context.cwd()}/foo`, 'foo');
context.git(['add', 'foo']);
context.tortilla(['step', 'push', '-m', 'foo']);

Fs.writeFileSync(`${context.cwd()}/bar`, 'bar');
context.git(['add', 'bar']);
context.tortilla(['step', 'push', '-m', 'bar']);

Fs.writeFileSync(`${context.cwd()}/baz`, 'baz');
context.git(['add', 'baz']);
context.tortilla(['step', 'push', '-m', 'baz']);

context.tortilla(['step', 'edit', '1.2']);

expect(
context.tortilla(['status'])
).toMatch(
/^Editing step 1\.2/
);
});

it('should show conflicting steps', () => {
Fs.writeFileSync(`${context.cwd()}/foo`, 'foo');
context.git(['add', 'foo']);
context.tortilla(['step', 'push', '-m', 'foo']);

Fs.writeFileSync(`${context.cwd()}/bar`, 'bar');
context.git(['add', 'bar']);
context.tortilla(['step', 'push', '-m', 'bar']);

Fs.writeFileSync(`${context.cwd()}/baz`, 'baz');
context.git(['add', 'baz']);
context.tortilla(['step', 'push', '-m', 'baz']);

context.tortilla(['step', 'edit', '1.2']);
Fs.writeFileSync(`${context.cwd()}/baz`, 'bazooka');

try {
context.git(['rebase', '--continue']);
}
catch (e) {
// Error is expected
}

expect(
context.tortilla(['status'])
).toMatch(
/^Solving conflict between step 1\.2 \(HEAD\) and step 1\.3/
);
});

it('should print root', () => {
Fs.writeFileSync(`${context.cwd()}/foo`, 'foo');
context.git(['add', 'foo']);
context.tortilla(['step', 'push', '-m', 'foo']);

Fs.writeFileSync(`${context.cwd()}/bar`, 'bar');
context.git(['add', 'bar']);
context.tortilla(['step', 'push', '-m', 'bar']);

Fs.writeFileSync(`${context.cwd()}/baz`, 'baz');
context.git(['add', 'baz']);
context.tortilla(['step', 'push', '-m', 'baz']);

context.tortilla(['step', 'edit', '--root']);

expect(
context.tortilla(['status'])
).toMatch(
/^Editing root/
);
});
});
});

0 comments on commit b8faa14

Please sign in to comment.