Skip to content

Commit

Permalink
Added exception handling to switch-branch command
Browse files Browse the repository at this point in the history
  • Loading branch information
shakilsiraj committed Nov 28, 2018
1 parent c3f71aa commit 4c1fc33
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
9 changes: 5 additions & 4 deletions bin/run
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node

require('@oclif/command').run()
.then(require('@oclif/command/flush'))
.catch(require('@oclif/errors/handle'))
process.env.NODE_ENV = 'prod';
require('@oclif/command')
.run()
.then(require('@oclif/command/flush'))
.catch(require('@oclif/errors/handle'));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "ogit",
"description": "A lazy developer's Git CLI made simple",
"version": "1.10.0",
"version": "1.10.2",
"author": "[email protected]",
"bin": {
"ogit": "./bin/run"
Expand Down
12 changes: 11 additions & 1 deletion src/commands/switch-branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ export class CreateBranchCommand extends Command {
public async preformBranchOperation(
branchInfo: CreateBranchStructure
): Promise<void> {
await GitWrapper.switchBranch(branchInfo.localBranchName);
try {
await GitWrapper.switchBranch(branchInfo.localBranchName);
} catch (err) {
console.error('Possible merge conflict with the following files:');
err.fileNamesArray.forEach((fileName: string) => {
console.error(fileName.trim());
});
console.error(
'Please commit your changes (ogit commit-changes) or stash (ogit stash-changes) them before you switch branches'
);
}
}
}
15 changes: 12 additions & 3 deletions src/wrapper/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export class GitWrapper {
*/
static listBranches = async (): Promise<GitBranch[]> => {
const branches: GitBranch[] = [];

console.log('Testing');
const remoteBranchesSummary = ObjectMapper.deserialize(
GitBranchSummary,
await SimpleGit().branch(['-r'])
Expand Down Expand Up @@ -319,8 +319,17 @@ export class GitWrapper {
*/
static switchBranch = async (branchName: string): Promise<void> => {
cli.action.start(`Switching to branch ${branchName}`);
await SimpleGit().checkout(branchName);
cli.action.stop();
try {
await SimpleGit().checkout(branchName);
cli.action.stop();
} catch (err) {
cli.action.stop('failed');
const errorRegex = /checkout:\n((.+\n)+)Please/;
const fileNames = errorRegex.exec(err.message)[1].trim();
err.fileNamesArray = fileNames.split('\n');

throw err;
}
};

/**
Expand Down

0 comments on commit 4c1fc33

Please sign in to comment.