-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from amir-s/checkout
add dev checkout
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import "colors"; | ||
|
||
export const cmd = "checkout <branch name>"; | ||
export const description = [ | ||
"checkout a new branch localy or from remote with tracking.", | ||
]; | ||
|
||
export const help = (DEV: string) => ({ | ||
description: [ | ||
`${ | ||
`${DEV} checkout <branch name>`.yellow | ||
} will checkout a new branch locally or from remote with tracking.`, | ||
"+ if the branch exists locally, it will be checked out.", | ||
"+ if the branch exists remotely, it will be checked out with tracking.", | ||
], | ||
commands: [ | ||
{ | ||
cmd: "checkout <branch name>", | ||
description: | ||
"checkout a new branch locally or from remote with tracking.", | ||
examples: [ | ||
{ | ||
cmd: "checkout fix-123", | ||
description: "checkout a new branch named fix-123.", | ||
}, | ||
{ | ||
cmd: "checkout brand-new-feature", | ||
description: | ||
"create a branch called `brand-new-feature` and check it out from remote and setup tracking.", | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
export const generic = () => { | ||
console.log("No branch specified.".gray); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { $ } from "zx"; | ||
import report from "yurnalist"; | ||
import process from "process"; | ||
|
||
$.verbose = false; | ||
|
||
import * as help from "./help.ts"; | ||
import type { ModuleRunOptions } from "../../utils/types.ts"; | ||
|
||
export const run = async ({ args }: ModuleRunOptions) => { | ||
if (args.length === 0) { | ||
help.generic(); | ||
return; | ||
} | ||
|
||
const remote = (await $`git remote`).stdout.trim(); | ||
if (remote === "") { | ||
report.error("no remote found."); | ||
process.exit(1); | ||
} | ||
|
||
if (remote.split("\n").length > 1) { | ||
report.error("multiple remotes found. this is currently not supported."); | ||
process.exit(1); | ||
} | ||
|
||
const branch = args[0]; | ||
|
||
const branchExistsLocally = await $`git branch --list ${branch}`; | ||
if (branchExistsLocally.stdout.trim() !== "") { | ||
report.command(`git checkout ${branch}`); | ||
await $`git checkout ${branch}`; | ||
process.exit(0); | ||
} | ||
|
||
const branchExistsRemotely = | ||
await $`git ls-remote --heads ${remote} ${branch}`; | ||
|
||
if (branchExistsRemotely.stdout.trim()) { | ||
report.success( | ||
`branch ${branch.green} exists on ${remote.yellow}. checking it out with tracking.` | ||
); | ||
report.command(`git checkout --track ${remote}/${branch}`); | ||
await $`git checkout --track ${remote}/${branch}`; | ||
} else { | ||
report.success( | ||
`Creating a new branch ${branch} and setting it to track on ${remote}.` | ||
); | ||
report.command(`git checkout -b ${branch}`); | ||
await $`git checkout -b ${branch}`; | ||
} | ||
|
||
process.exit(0); | ||
}; |