MCPcopy Create free account
hub / github.com/QodeXcli/QodeX / execute

Method execute

src/tools/git/branch.ts:32–87  ·  view source on GitHub ↗
(args: z.infer<typeof GitBranchArgs>, ctx: ToolContext)

Source from the content-addressed store, hash-verified

30 argsSchema = GitBranchArgs;
31
32 async execute(args: z.infer<typeof GitBranchArgs>, ctx: ToolContext): Promise<ToolResult> {
33 if (!await isGitRepo(ctx.cwd, ctx.signal)) {
34 return { content: '[NOT_A_GIT_REPO] Current directory is not inside a git working tree.', isError: true };
35 }
36
37 switch (args.action) {
38 case 'current': {
39 const r = await git(['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: ctx.cwd, signal: ctx.signal });
40 if (r.exitCode !== 0) return { content: `[ERROR] ${r.stderr.trim()}`, isError: true };
41 return { content: r.stdout.trim() };
42 }
43
44 case 'list': {
45 // Local + remote heads in one shot, with sortable date
46 const r = await git(
47 ['for-each-ref', '--sort=-committerdate', '--format=%(refname:short)\t%(committerdate:short)\t%(authorname)',
48 'refs/heads/', 'refs/remotes/'],
49 { cwd: ctx.cwd, signal: ctx.signal },
50 );
51 if (r.exitCode !== 0) return { content: `[ERROR] ${r.stderr.trim()}`, isError: true };
52 const cur = await git(['rev-parse', '--abbrev-ref', 'HEAD'], { cwd: ctx.cwd, signal: ctx.signal });
53 const currentBranch = cur.stdout.trim();
54 const lines = r.stdout.split('\n').filter(Boolean).map(l => {
55 const [name, date, author] = l.split('\t');
56 const marker = name === currentBranch ? '* ' : ' ';
57 return `${marker}${name?.padEnd(40).slice(0, 40)} ${date} ${author}`;
58 });
59 return { content: `Branches (newest first):\n${lines.join('\n')}`, metadata: { count: lines.length, current: currentBranch } };
60 }
61
62 case 'create': {
63 if (!args.name) return { content: '[INVALID_ARGS] action="create" requires `name`.', isError: true };
64 const baseArgs = ['branch', args.name];
65 if (args.from) baseArgs.push(args.from);
66 const r = await git(baseArgs, { cwd: ctx.cwd, signal: ctx.signal });
67 if (r.exitCode !== 0) return { content: `[ERROR] ${r.stderr.trim()}`, isError: true };
68 return { content: `Created branch '${args.name}'${args.from ? ` from ${args.from}` : ''}.` };
69 }
70
71 case 'checkout': {
72 if (!args.name) return { content: '[INVALID_ARGS] action="checkout" requires `name`.', isError: true };
73 const flags = args.create_if_missing ? ['switch', '-c', args.name] : ['switch', args.name];
74 const r = await git(flags, { cwd: ctx.cwd, signal: ctx.signal });
75 if (r.exitCode !== 0) return { content: `[ERROR] ${r.stderr.trim()}`, isError: true };
76 return { content: `Switched to branch '${args.name}'.${r.stdout.trim() ? '\n' + r.stdout.trim() : ''}` };
77 }
78
79 case 'delete': {
80 if (!args.name) return { content: '[INVALID_ARGS] action="delete" requires `name`.', isError: true };
81 const flag = args.force ? '-D' : '-d';
82 const r = await git(['branch', flag, args.name], { cwd: ctx.cwd, signal: ctx.signal });
83 if (r.exitCode !== 0) return { content: `[ERROR] ${r.stderr.trim()}`, isError: true };
84 return { content: `Deleted branch '${args.name}'.${r.stdout.trim() ? '\n' + r.stdout.trim() : ''}` };
85 }
86 }
87 }
88}

Callers

nothing calls this directly

Calls 3

isGitRepoFunction · 0.85
gitFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected