Skip to content

Commit ccc227e

Browse files
barnabasbusaclaude
andcommitted
feat: display PR URL and auto-set upstream when pushing new branches
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9ca7c02 commit ccc227e

File tree

2 files changed

+76
-14
lines changed

2 files changed

+76
-14
lines changed

out/cli.cjs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68272,6 +68272,24 @@ var getGitRemotes = async () => {
6827268272
const { stdout } = await execa("git", ["remote"]);
6827368273
return stdout.split("\n").filter((remote) => Boolean(remote.trim()));
6827468274
};
68275+
var hasUpstreamBranch = async () => {
68276+
try {
68277+
await execa("git", ["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"]);
68278+
return true;
68279+
} catch {
68280+
return false;
68281+
}
68282+
};
68283+
var getCurrentBranch = async () => {
68284+
const { stdout } = await execa("git", ["branch", "--show-current"]);
68285+
return stdout.trim();
68286+
};
68287+
var displayPushUrl = (stderr2) => {
68288+
const urlMatch = stderr2.match(/https?:\/\/\S+/);
68289+
if (urlMatch) {
68290+
ce(`${source_default.cyan("Create a pull request:")} ${urlMatch[0]}`);
68291+
}
68292+
};
6827568293
var checkMessageTemplate = (extraArgs2) => {
6827668294
for (const key in extraArgs2) {
6827768295
if (extraArgs2[key].includes(config6.OCO_MESSAGE_TEMPLATE_PLACEHOLDER))
@@ -68343,8 +68361,13 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
6834368361
const remotes = await getGitRemotes();
6834468362
if (config6.OCO_GITPUSH === false) return;
6834568363
if (!remotes.length) {
68346-
const { stdout: stdout2 } = await execa("git", ["push"]);
68364+
const pushArgs = ["push"];
68365+
if (!await hasUpstreamBranch()) {
68366+
pushArgs.push("--set-upstream", "origin", await getCurrentBranch());
68367+
}
68368+
const { stdout: stdout2, stderr: stderr2 } = await execa("git", pushArgs);
6834768369
if (stdout2) ce(stdout2);
68370+
displayPushUrl(stderr2);
6834868371
process.exit(0);
6834968372
}
6835068373
if (remotes.length === 1) {
@@ -68355,15 +68378,16 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
6835568378
if (isPushConfirmedByUser) {
6835668379
const pushSpinner = le();
6835768380
pushSpinner.start(`Running 'git push ${remotes[0]}'`);
68358-
const { stdout: stdout2 } = await execa("git", [
68359-
"push",
68360-
"--verbose",
68361-
remotes[0]
68362-
]);
68381+
const pushArgs = ["push", "--verbose", remotes[0]];
68382+
if (!await hasUpstreamBranch()) {
68383+
pushArgs.push("--set-upstream", await getCurrentBranch());
68384+
}
68385+
const { stdout: stdout2, stderr: stderr2 } = await execa("git", pushArgs);
6836368386
pushSpinner.stop(
6836468387
`${source_default.green("\u2714")} Successfully pushed all commits to ${remotes[0]}`
6836568388
);
6836668389
if (stdout2) ce(stdout2);
68390+
displayPushUrl(stderr2);
6836768391
} else {
6836868392
ce("`git push` aborted");
6836968393
process.exit(0);
@@ -68381,13 +68405,18 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
6838168405
if (selectedRemote !== skipOption) {
6838268406
const pushSpinner = le();
6838368407
pushSpinner.start(`Running 'git push ${selectedRemote}'`);
68384-
const { stdout: stdout2 } = await execa("git", ["push", selectedRemote]);
68408+
const pushArgs = ["push", selectedRemote];
68409+
if (!await hasUpstreamBranch()) {
68410+
pushArgs.push("--set-upstream", await getCurrentBranch());
68411+
}
68412+
const { stdout: stdout2, stderr: stderr2 } = await execa("git", pushArgs);
6838568413
if (stdout2) ce(stdout2);
6838668414
pushSpinner.stop(
6838768415
`${source_default.green(
6838868416
"\u2714"
6838968417
)} successfully pushed all commits to ${selectedRemote}`
6839068418
);
68419+
displayPushUrl(stderr2);
6839168420
}
6839268421
}
6839368422
} else {

src/commands/commit.ts

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ const getGitRemotes = async () => {
3232
return stdout.split('\n').filter((remote) => Boolean(remote.trim()));
3333
};
3434

35+
const hasUpstreamBranch = async (): Promise<boolean> => {
36+
try {
37+
await execa('git', ['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{u}']);
38+
return true;
39+
} catch {
40+
return false;
41+
}
42+
};
43+
44+
const getCurrentBranch = async (): Promise<string> => {
45+
const { stdout } = await execa('git', ['branch', '--show-current']);
46+
return stdout.trim();
47+
};
48+
49+
const displayPushUrl = (stderr: string) => {
50+
const urlMatch = stderr.match(/https?:\/\/\S+/);
51+
if (urlMatch) {
52+
outro(`${chalk.cyan('Create a pull request:')} ${urlMatch[0]}`);
53+
}
54+
};
55+
3556
// Check for the presence of message templates
3657
const checkMessageTemplate = (extraArgs: string[]): string | false => {
3758
for (const key in extraArgs) {
@@ -133,8 +154,13 @@ ${chalk.grey('——————————————————')}`
133154
if (config.OCO_GITPUSH === false) return;
134155

135156
if (!remotes.length) {
136-
const { stdout } = await execa('git', ['push']);
157+
const pushArgs = ['push'];
158+
if (!(await hasUpstreamBranch())) {
159+
pushArgs.push('--set-upstream', 'origin', await getCurrentBranch());
160+
}
161+
const { stdout, stderr } = await execa('git', pushArgs);
137162
if (stdout) outro(stdout);
163+
displayPushUrl(stderr);
138164
process.exit(0);
139165
}
140166

@@ -150,11 +176,11 @@ ${chalk.grey('——————————————————')}`
150176

151177
pushSpinner.start(`Running 'git push ${remotes[0]}'`);
152178

153-
const { stdout } = await execa('git', [
154-
'push',
155-
'--verbose',
156-
remotes[0]
157-
]);
179+
const pushArgs = ['push', '--verbose', remotes[0]];
180+
if (!(await hasUpstreamBranch())) {
181+
pushArgs.push('--set-upstream', await getCurrentBranch());
182+
}
183+
const { stdout, stderr } = await execa('git', pushArgs);
158184

159185
pushSpinner.stop(
160186
`${chalk.green('✔')} Successfully pushed all commits to ${
@@ -163,6 +189,7 @@ ${chalk.grey('——————————————————')}`
163189
);
164190

165191
if (stdout) outro(stdout);
192+
displayPushUrl(stderr);
166193
} else {
167194
outro('`git push` aborted');
168195
process.exit(0);
@@ -184,7 +211,11 @@ ${chalk.grey('——————————————————')}`
184211

185212
pushSpinner.start(`Running 'git push ${selectedRemote}'`);
186213

187-
const { stdout } = await execa('git', ['push', selectedRemote]);
214+
const pushArgs = ['push', selectedRemote];
215+
if (!(await hasUpstreamBranch())) {
216+
pushArgs.push('--set-upstream', await getCurrentBranch());
217+
}
218+
const { stdout, stderr } = await execa('git', pushArgs);
188219

189220
if (stdout) outro(stdout);
190221

@@ -193,6 +224,8 @@ ${chalk.grey('——————————————————')}`
193224
'✔'
194225
)} successfully pushed all commits to ${selectedRemote}`
195226
);
227+
228+
displayPushUrl(stderr);
196229
}
197230
}
198231
} else {

0 commit comments

Comments
 (0)