feat: enhance git commands to use current branch for pull and push operations

This commit is contained in:
doum1004
2026-04-10 19:30:41 -04:00
parent e8df760de1
commit 987dc62504
6 changed files with 54 additions and 19 deletions

View File

@@ -13,7 +13,8 @@ export function makePullCommand(): Command {
process.exit(1);
}
const result = await git.pull(ctx.root);
const branch = await git.currentBranch(ctx.root);
const result = await git.pull(ctx.root, "origin", branch);
if (!result.ok) {
console.error(`Pull failed: ${result.output}`);
process.exit(1);

View File

@@ -13,7 +13,8 @@ export function makePushCommand(): Command {
process.exit(1);
}
const result = await git.push(ctx.root);
const branch = await git.currentBranch(ctx.root);
const result = await git.push(ctx.root, "origin", branch);
if (!result.ok) {
console.error(`Push failed: ${result.output}`);
process.exit(1);

View File

@@ -64,10 +64,16 @@ export function makeRepoCommand(): Command {
const repoName = `wiki-${name}`;
console.log(`Creating GitHub repo: ${repoName}...`);
const repo = await createRepo(repoName, {
private: !options.public,
description: `LLM Wiki: ${name} (${options.domain})`,
});
let repo;
try {
repo = await createRepo(repoName, {
private: !options.public,
description: `LLM Wiki: ${name} (${options.domain})`,
});
} catch (err: any) {
console.error(err.message);
process.exit(1);
}
console.log(`Created: ${repo.html_url}`);
@@ -91,12 +97,13 @@ export function makeRepoCommand(): Command {
// Add remote and push
await git.addRemote(localDir, "origin", repo.ssh_url);
const pushResult = await git.push(localDir);
const branch = await git.currentBranch(localDir);
const pushResult = await git.push(localDir, "origin", branch);
if (pushResult.ok) {
console.log("Pushed to GitHub.");
} else {
console.error(`Warning: push failed: ${pushResult.output}`);
console.log(`You can push manually: cd ${localDir} && git push -u origin main`);
console.log(`You can push manually: cd ${localDir} && git push -u origin ${branch}`);
}
},
);
@@ -202,19 +209,26 @@ export function makeRepoCommand(): Command {
const repoName = `wiki-${ctx.config.name}`;
console.log(`Creating GitHub repo: ${repoName}...`);
const repo = await createRepo(repoName, {
private: true,
description: `LLM Wiki: ${ctx.config.name} (${ctx.config.domain})`,
});
let repo;
try {
repo = await createRepo(repoName, {
private: true,
description: `LLM Wiki: ${ctx.config.name} (${ctx.config.domain})`,
});
} catch (err: any) {
console.error(err.message);
process.exit(1);
}
await git.addRemote(ctx.root, "origin", repo.ssh_url);
const pushResult = await git.push(ctx.root);
const branch = await git.currentBranch(ctx.root);
const pushResult = await git.push(ctx.root, "origin", branch);
if (pushResult.ok) {
console.log(`Connected and pushed to ${repo.html_url}`);
} else {
console.error(`Remote added but push failed: ${pushResult.output}`);
console.log("Try: git push -u origin main");
console.log(`Try: git push -u origin ${branch}`);
}
});

View File

@@ -13,8 +13,10 @@ export function makeSyncCommand(): Command {
process.exit(1);
}
const branch = await git.currentBranch(ctx.root);
console.log("Pulling...");
const pullResult = await git.pull(ctx.root);
const pullResult = await git.pull(ctx.root, "origin", branch);
if (!pullResult.ok) {
console.error(`Pull failed: ${pullResult.output}`);
process.exit(1);
@@ -22,7 +24,7 @@ export function makeSyncCommand(): Command {
console.log(pullResult.output || "Already up to date.");
console.log("Pushing...");
const pushResult = await git.push(ctx.root);
const pushResult = await git.push(ctx.root, "origin", branch);
if (!pushResult.ok) {
console.error(`Push failed: ${pushResult.output}`);
process.exit(1);

View File

@@ -41,6 +41,11 @@ export function diff(cwd: string, ref?: string): Promise<GitResult> {
return ref ? run(["show", ref], cwd) : run(["diff"], cwd);
}
export async function currentBranch(cwd: string): Promise<string> {
const result = await run(["rev-parse", "--abbrev-ref", "HEAD"], cwd);
return result.ok ? result.output : "main";
}
export async function hasRemote(cwd: string): Promise<boolean> {
const result = await run(["remote"], cwd);
return result.ok && result.output.length > 0;
@@ -51,7 +56,7 @@ export function addRemote(cwd: string, name: string, url: string): Promise<GitRe
}
export function push(cwd: string, remote = "origin", branch = "main"): Promise<GitResult> {
return run(["push", remote, branch], cwd);
return run(["push", "-u", remote, branch], cwd);
}
export function pull(cwd: string, remote = "origin", branch = "main"): Promise<GitResult> {

View File

@@ -68,8 +68,20 @@ export async function createRepo(
});
if (!res.ok) {
const err = (await res.json()) as { message: string };
throw new Error(`Failed to create repo: ${err.message}`);
const err = (await res.json()) as { message: string; errors?: { message: string }[] };
if (res.status === 422 && err.message?.includes("name already exists")) {
throw new Error(
`Repository "${name}" already exists. Delete it first with:\n gh repo delete ${name} --yes\nOr use a different name.`,
);
}
if (res.status === 401) {
throw new Error('Authentication failed. Run "wiki auth login" to re-authenticate.');
}
if (res.status === 403) {
throw new Error("Permission denied. Your token may lack the 'repo' scope.");
}
const detail = err.errors?.map((e) => e.message).join(", ") ?? err.message;
throw new Error(`Failed to create repo (${res.status}): ${detail}`);
}
return (await res.json()) as GitHubRepo;