diff --git a/out/cli.cjs b/out/cli.cjs index 46450c3b..2d2083b2 100755 --- a/out/cli.cjs +++ b/out/cli.cjs @@ -68272,6 +68272,24 @@ var getGitRemotes = async () => { const { stdout } = await execa("git", ["remote"]); return stdout.split("\n").filter((remote) => Boolean(remote.trim())); }; +var hasUpstreamBranch = async () => { + try { + await execa("git", ["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"]); + return true; + } catch { + return false; + } +}; +var getCurrentBranch = async () => { + const { stdout } = await execa("git", ["branch", "--show-current"]); + return stdout.trim(); +}; +var displayPushUrl = (stderr2) => { + const urlMatch = stderr2.match(/https?:\/\/\S+/); + if (urlMatch) { + ce(`${source_default.cyan("Create a pull request:")} ${urlMatch[0]}`); + } +}; var checkMessageTemplate = (extraArgs2) => { for (const key in extraArgs2) { 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 const remotes = await getGitRemotes(); if (config6.OCO_GITPUSH === false) return; if (!remotes.length) { - const { stdout: stdout2 } = await execa("git", ["push"]); + const pushArgs = ["push"]; + if (!await hasUpstreamBranch()) { + pushArgs.push("--set-upstream", "origin", await getCurrentBranch()); + } + const { stdout: stdout2, stderr: stderr2 } = await execa("git", pushArgs); if (stdout2) ce(stdout2); + displayPushUrl(stderr2); process.exit(0); } if (remotes.length === 1) { @@ -68355,15 +68378,16 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2 if (isPushConfirmedByUser) { const pushSpinner = le(); pushSpinner.start(`Running 'git push ${remotes[0]}'`); - const { stdout: stdout2 } = await execa("git", [ - "push", - "--verbose", - remotes[0] - ]); + const pushArgs = ["push", "--verbose", remotes[0]]; + if (!await hasUpstreamBranch()) { + pushArgs.push("--set-upstream", await getCurrentBranch()); + } + const { stdout: stdout2, stderr: stderr2 } = await execa("git", pushArgs); pushSpinner.stop( `${source_default.green("\u2714")} Successfully pushed all commits to ${remotes[0]}` ); if (stdout2) ce(stdout2); + displayPushUrl(stderr2); } else { ce("`git push` aborted"); process.exit(0); @@ -68381,13 +68405,18 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2 if (selectedRemote !== skipOption) { const pushSpinner = le(); pushSpinner.start(`Running 'git push ${selectedRemote}'`); - const { stdout: stdout2 } = await execa("git", ["push", selectedRemote]); + const pushArgs = ["push", selectedRemote]; + if (!await hasUpstreamBranch()) { + pushArgs.push("--set-upstream", await getCurrentBranch()); + } + const { stdout: stdout2, stderr: stderr2 } = await execa("git", pushArgs); if (stdout2) ce(stdout2); pushSpinner.stop( `${source_default.green( "\u2714" )} successfully pushed all commits to ${selectedRemote}` ); + displayPushUrl(stderr2); } } } else { diff --git a/src/commands/commit.ts b/src/commands/commit.ts index 70b5ef8b..140289e2 100644 --- a/src/commands/commit.ts +++ b/src/commands/commit.ts @@ -32,6 +32,27 @@ const getGitRemotes = async () => { return stdout.split('\n').filter((remote) => Boolean(remote.trim())); }; +const hasUpstreamBranch = async (): Promise => { + try { + await execa('git', ['rev-parse', '--abbrev-ref', '--symbolic-full-name', '@{u}']); + return true; + } catch { + return false; + } +}; + +const getCurrentBranch = async (): Promise => { + const { stdout } = await execa('git', ['branch', '--show-current']); + return stdout.trim(); +}; + +const displayPushUrl = (stderr: string) => { + const urlMatch = stderr.match(/https?:\/\/\S+/); + if (urlMatch) { + outro(`${chalk.cyan('Create a pull request:')} ${urlMatch[0]}`); + } +}; + // Check for the presence of message templates const checkMessageTemplate = (extraArgs: string[]): string | false => { for (const key in extraArgs) { @@ -133,8 +154,13 @@ ${chalk.grey('——————————————————')}` if (config.OCO_GITPUSH === false) return; if (!remotes.length) { - const { stdout } = await execa('git', ['push']); + const pushArgs = ['push']; + if (!(await hasUpstreamBranch())) { + pushArgs.push('--set-upstream', 'origin', await getCurrentBranch()); + } + const { stdout, stderr } = await execa('git', pushArgs); if (stdout) outro(stdout); + displayPushUrl(stderr); process.exit(0); } @@ -150,11 +176,11 @@ ${chalk.grey('——————————————————')}` pushSpinner.start(`Running 'git push ${remotes[0]}'`); - const { stdout } = await execa('git', [ - 'push', - '--verbose', - remotes[0] - ]); + const pushArgs = ['push', '--verbose', remotes[0]]; + if (!(await hasUpstreamBranch())) { + pushArgs.push('--set-upstream', await getCurrentBranch()); + } + const { stdout, stderr } = await execa('git', pushArgs); pushSpinner.stop( `${chalk.green('✔')} Successfully pushed all commits to ${ @@ -163,6 +189,7 @@ ${chalk.grey('——————————————————')}` ); if (stdout) outro(stdout); + displayPushUrl(stderr); } else { outro('`git push` aborted'); process.exit(0); @@ -184,7 +211,11 @@ ${chalk.grey('——————————————————')}` pushSpinner.start(`Running 'git push ${selectedRemote}'`); - const { stdout } = await execa('git', ['push', selectedRemote]); + const pushArgs = ['push', selectedRemote]; + if (!(await hasUpstreamBranch())) { + pushArgs.push('--set-upstream', await getCurrentBranch()); + } + const { stdout, stderr } = await execa('git', pushArgs); if (stdout) outro(stdout); @@ -193,6 +224,8 @@ ${chalk.grey('——————————————————')}` '✔' )} successfully pushed all commits to ${selectedRemote}` ); + + displayPushUrl(stderr); } } } else {