swadhinbiswas feat: implement air-gapped mode, UI fixes, and project management integrations · 7/9/2026
e3bd4a1
240 lines · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/**
 * Branch Commands
 * Manage git branches from the CLI
 */

import { Command } from "commander";
import chalk from "chalk";
import ora from "ora";
import inquirer from "inquirer";
import { simpleGit } from "simple-git";

const git = simpleGit();

export const branchCommands = new Command("branch")
    .description("Manage branches");

// Branch Checkout (interactive)
branchCommands
    .command("checkout [branch]")
    .alias("co")
    .description("Checkout a branch (interactive if no branch specified)")
    .action(async (branch?: string) => {
        try {
            if (branch) {
                const spinner = ora(`Checking out ${branch}...`).start();
                await git.checkout(branch);
                spinner.succeed(`Checked out ${chalk.hex("#8be9fd")(branch)}`);
                return;
            }

            // Interactive mode
            const branches = await git.branchLocal();
            const currentBranch = branches.current;

            const choices = branches.all.map(b => ({
                name: b === currentBranch ? `${chalk.hex("#50fa7b")("●")} ${b} ${chalk.hex("#6272a4")("(current)")}` : `  ${b}`,
                value: b,
                short: b,
            }));

            const { selectedBranch } = await inquirer.prompt([
                {
                    type: "list",
                    name: "selectedBranch",
                    message: "Select branch to checkout:",
                    choices,
                    pageSize: 15,
                },
            ]);

            if (selectedBranch === currentBranch) {
                console.log(chalk.hex("#6272a4")("Already on that branch."));
                return;
            }

            const spinner = ora(`Checking out ${selectedBranch}...`).start();
            await git.checkout(selectedBranch);
            spinner.succeed(`Checked out ${chalk.hex("#8be9fd")(selectedBranch)}`);
        } catch (error) {
            console.error(chalk.hex("#ff5555")("Failed to checkout branch"));
            console.error(chalk.hex("#ff5555")(error instanceof Error ? error.message : "Unknown error"));
            process.exit(1);
        }
    });

// Branch Rename
branchCommands
    .command("rename [old-name] <new-name>")
    .alias("mv")
    .description("Rename a branch (renames current branch if old-name not specified)")
    .action(async (oldNameOrNewName: string, newName?: string) => {
        try {
            let oldBranch: string;
            let targetNewName: string;

            if (newName) {
                oldBranch = oldNameOrNewName;
                targetNewName = newName;
            } else {
                // Rename current branch
                const branches = await git.branchLocal();
                oldBranch = branches.current;
                targetNewName = oldNameOrNewName;
            }

            const spinner = ora(`Renaming ${oldBranch} to ${targetNewName}...`).start();

            await git.raw(["branch", "-m", oldBranch, targetNewName]);

            spinner.succeed(`Renamed ${chalk.hex("#8be9fd")(oldBranch)} to ${chalk.hex("#50fa7b")(targetNewName)}`);
        } catch (error) {
            console.error(chalk.hex("#ff5555")("Failed to rename branch"));
            console.error(chalk.hex("#ff5555")(error instanceof Error ? error.message : "Unknown error"));
            process.exit(1);
        }
    });

// Branch Delete
branchCommands
    .command("delete <branch>")
    .alias("rm")
    .description("Delete a branch")
    .option("-f, --force", "Force delete even if not merged")
    .option("-r, --remote", "Also delete remote branch")
    .action(async (branch: string, options) => {
        try {
            // Check if trying to delete current branch
            const branches = await git.branchLocal();
            if (branch === branches.current) {
                console.error(chalk.hex("#ff5555")("Cannot delete the current branch. Switch to another branch first."));
                process.exit(1);
            }

            const spinner = ora(`Deleting branch ${branch}...`).start();

            // Delete local branch
            if (options.force) {
                await git.branch(["-D", branch]);
            } else {
                await git.branch(["-d", branch]);
            }

            spinner.text = `Deleted local branch ${branch}`;

            // Delete remote branch if requested
            if (options.remote) {
                try {
                    await git.push(["origin", "--delete", branch]);
                    spinner.succeed(`Deleted ${chalk.hex("#8be9fd")(branch)} (local and remote)`);
                } catch (remoteError) {
                    spinner.warn(`Deleted local branch ${chalk.hex("#8be9fd")(branch)}, but failed to delete remote`);
                    console.error(chalk.hex("#6272a4")(remoteError instanceof Error ? remoteError.message : ""));
                }
            } else {
                spinner.succeed(`Deleted ${chalk.hex("#8be9fd")(branch)}`);
            }
        } catch (error) {
            console.error(chalk.hex("#ff5555")("Failed to delete branch"));
            console.error(chalk.hex("#ff5555")(error instanceof Error ? error.message : "Unknown error"));
            process.exit(1);
        }
    });

// Branch List
branchCommands
    .command("list")
    .alias("ls")
    .description("List branches")
    .option("-a, --all", "Show all branches including remote")
    .option("-r, --remote", "Show only remote branches")
    .action(async (options) => {
        try {
            let branches;

            if (options.all) {
                branches = await git.branch(["-a"]);
            } else if (options.remote) {
                branches = await git.branch(["-r"]);
            } else {
                branches = await git.branchLocal();
            }

            console.log(chalk.bold("\n🌿 Branches\n"));

            for (const branch of branches.all) {
                if (branch === branches.current) {
                    console.log(chalk.hex("#50fa7b")(`● ${branch}`) + chalk.hex("#6272a4")(" (current)"));
                } else if (branch.startsWith("remotes/")) {
                    console.log(chalk.hex("#6272a4")(`  ${branch.replace("remotes/", "")}`));
                } else {
                    console.log(`  ${branch}`);
                }
            }

            console.log("");
        } catch (error) {
            console.error(chalk.hex("#ff5555")("Failed to list branches"));
            console.error(chalk.hex("#ff5555")(error instanceof Error ? error.message : "Unknown error"));
            process.exit(1);
        }
    });

// Branch Create
branchCommands
    .command("create <name>")
    .alias("new")
    .description("Create a new branch")
    .option("-b, --base <branch>", "Base branch to create from")
    .option("-c, --checkout", "Checkout the new branch after creation")
    .action(async (name: string, options) => {
        try {
            const spinner = ora(`Creating branch ${name}...`).start();

            if (options.base) {
                await git.checkoutBranch(name, options.base);
                if (!options.checkout) {
                    // Go back to previous branch
                    await git.checkout("-");
                }
            } else if (options.checkout) {
                await git.checkoutLocalBranch(name);
            } else {
                await git.branch([name]);
            }

            if (options.checkout) {
                spinner.succeed(`Created and checked out ${chalk.hex("#50fa7b")(name)}`);
            } else {
                spinner.succeed(`Created branch ${chalk.hex("#50fa7b")(name)}`);
            }
        } catch (error) {
            console.error(chalk.hex("#ff5555")("Failed to create branch"));
            console.error(chalk.hex("#ff5555")(error instanceof Error ? error.message : "Unknown error"));
            process.exit(1);
        }
    });

// Branch Track
branchCommands
    .command("track <remote-branch>")
    .description("Create a local branch tracking a remote branch")
    .action(async (remoteBranch: string) => {
        try {
            const spinner = ora(`Tracking ${remoteBranch}...`).start();

            // Extract branch name from remote/branch format
            const branchName = remoteBranch.replace(/^origin\//, "");

            await git.checkout(["-b", branchName, "--track", `origin/${branchName}`]);

            spinner.succeed(`Created ${chalk.hex("#50fa7b")(branchName)} tracking ${chalk.hex("#8be9fd")(`origin/${branchName}`)}`);
        } catch (error) {
            console.error(chalk.hex("#ff5555")("Failed to track branch"));
            console.error(chalk.hex("#ff5555")(error instanceof Error ? error.message : "Unknown error"));
            process.exit(1);
        }
    });

export default branchCommands;