Skip to content

Commit

Permalink
Merge pull request #140 from amir-s/checkout
Browse files Browse the repository at this point in the history
add dev checkout
  • Loading branch information
amir-s authored Nov 16, 2024
2 parents ab1844e + 8e34418 commit a30ad46
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const modules = {
default: { indirect: true },
clone: {},
clean: {},
checkout: {},
cd: {},
shell: {},
update: {},
Expand Down
38 changes: 38 additions & 0 deletions modules/checkout/help.ts
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);
};
54 changes: 54 additions & 0 deletions modules/checkout/index.ts
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);
};

0 comments on commit a30ad46

Please sign in to comment.