Skip to content

Commit

Permalink
improve gitflow checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
mceck committed Jan 19, 2023
1 parent e6c1d8a commit 68060c0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how
- task creation
- drag and drop

## [1.1.11]

### Added

- improve gtiflow checkout

## [1.1.10]

### Added
Expand Down
38 changes: 32 additions & 6 deletions src/services/task-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,36 +37,62 @@ class TaskService {
gitCurrent: 'git branch --show-current',
gitCheckout: (name: string, isNew: boolean = false) =>
`git checkout ${isNew ? '-b ' : ''}${name}`,
gitDevAndPull: (dev: string) => `git checkout ${dev} && git pull`,
};
}

async gitCheckout(customId: string) {
let res = await exec(this.cmd.gitCurrent);
if (res.includes(customId)) {
vscode.window.showInformationMessage(`Already on ${res}`);
const currentBranch = await exec(this.cmd.gitCurrent);
if (currentBranch.includes(customId)) {
vscode.window.showInformationMessage(`Already on ${currentBranch}`);
return;
}

res = await exec(this.cmd.gitStatus);
let res = await exec(this.cmd.gitStatus);
if (res) {
throw new Error('Stash changes before checkout');
}

res = await exec(this.cmd.gitBranches);
const existingBranch = res.split('\n').find((b) => b.includes(customId));
const branchList = (await exec(this.cmd.gitBranches)).split('\n');
const existingBranch = branchList.find((b) => b.includes(customId));
if (existingBranch) {
// checkout existing branch
res = await exec(this.cmd.gitCheckout(existingBranch));
if (res) {
throw new Error(res);
}
vscode.window.showInformationMessage(`Checkout branch ${existingBranch}`);
} else {
// create new branch
// search develop branch in local branches to checkout from there
let devBranch: any = 'develop';
if (!branchList.includes(devBranch)) {
// not found, manually select
devBranch = (
await vscode.window.showQuickPick(
branchList.map((b) => ({
id: b,
label: b,
description: `Checkout from branch ${b}`,
}))
)
)?.id;
}
if (!devBranch) {
return;
}

// select gtiflow branch type
const branchType = await vscode.window.showQuickPick(
this.gitflowOptions(customId)
);
if (!branchType) {
return;
}

// checkout and pull develop
await exec(this.cmd.gitDevAndPull(devBranch));
// checkout new branch
const branchName = `${branchType.id}/${customId}`;
res = await exec(this.cmd.gitCheckout(branchName, true));
if (res) {
Expand Down

0 comments on commit 68060c0

Please sign in to comment.