Skip to content

Commit 420a153

Browse files
authored
Merge pull request #538 from barnabasbusa/master
feat: display PR URL and auto-set upstream when pushing new branches
2 parents f300b5d + fd9820d commit 420a153

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
@@ -85071,6 +85071,24 @@ var getGitRemotes = async () => {
8507185071
const { stdout } = await execa("git", ["remote"]);
8507285072
return stdout.split("\n").filter((remote) => Boolean(remote.trim()));
8507385073
};
85074+
var hasUpstreamBranch = async () => {
85075+
try {
85076+
await execa("git", ["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"]);
85077+
return true;
85078+
} catch {
85079+
return false;
85080+
}
85081+
};
85082+
var getCurrentBranch = async () => {
85083+
const { stdout } = await execa("git", ["branch", "--show-current"]);
85084+
return stdout.trim();
85085+
};
85086+
var displayPushUrl = (stderr2) => {
85087+
const urlMatch = stderr2.match(/https?:\/\/\S+/);
85088+
if (urlMatch) {
85089+
ce(`${source_default.cyan("Create a pull request:")} ${urlMatch[0]}`);
85090+
}
85091+
};
8507485092
var checkMessageTemplate = (extraArgs2) => {
8507585093
for (const key in extraArgs2) {
8507685094
if (extraArgs2[key].includes(config6.OCO_MESSAGE_TEMPLATE_PLACEHOLDER))
@@ -85142,8 +85160,13 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
8514285160
const remotes = await getGitRemotes();
8514385161
if (config6.OCO_GITPUSH === false) return;
8514485162
if (!remotes.length) {
85145-
const { stdout: stdout2 } = await execa("git", ["push"]);
85163+
const pushArgs = ["push"];
85164+
if (!await hasUpstreamBranch()) {
85165+
pushArgs.push("--set-upstream", "origin", await getCurrentBranch());
85166+
}
85167+
const { stdout: stdout2, stderr: stderr2 } = await execa("git", pushArgs);
8514685168
if (stdout2) ce(stdout2);
85169+
displayPushUrl(stderr2);
8514785170
process.exit(0);
8514885171
}
8514985172
if (remotes.length === 1) {
@@ -85154,15 +85177,16 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
8515485177
if (isPushConfirmedByUser) {
8515585178
const pushSpinner = le();
8515685179
pushSpinner.start(`Running 'git push ${remotes[0]}'`);
85157-
const { stdout: stdout2 } = await execa("git", [
85158-
"push",
85159-
"--verbose",
85160-
remotes[0]
85161-
]);
85180+
const pushArgs = ["push", "--verbose", remotes[0]];
85181+
if (!await hasUpstreamBranch()) {
85182+
pushArgs.push("--set-upstream", await getCurrentBranch());
85183+
}
85184+
const { stdout: stdout2, stderr: stderr2 } = await execa("git", pushArgs);
8516285185
pushSpinner.stop(
8516385186
`${source_default.green("\u2714")} Successfully pushed all commits to ${remotes[0]}`
8516485187
);
8516585188
if (stdout2) ce(stdout2);
85189+
displayPushUrl(stderr2);
8516685190
} else {
8516785191
ce("`git push` aborted");
8516885192
process.exit(0);
@@ -85180,13 +85204,18 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
8518085204
if (selectedRemote !== skipOption) {
8518185205
const pushSpinner = le();
8518285206
pushSpinner.start(`Running 'git push ${selectedRemote}'`);
85183-
const { stdout: stdout2 } = await execa("git", ["push", selectedRemote]);
85207+
const pushArgs = ["push", selectedRemote];
85208+
if (!await hasUpstreamBranch()) {
85209+
pushArgs.push("--set-upstream", await getCurrentBranch());
85210+
}
85211+
const { stdout: stdout2, stderr: stderr2 } = await execa("git", pushArgs);
8518485212
if (stdout2) ce(stdout2);
8518585213
pushSpinner.stop(
8518685214
`${source_default.green(
8518785215
"\u2714"
8518885216
)} successfully pushed all commits to ${selectedRemote}`
8518985217
);
85218+
displayPushUrl(stderr2);
8519085219
}
8519185220
}
8519285221
} else {

src/commands/commit.ts

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

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

132153
if (!remotes.length) {
133-
const { stdout } = await execa('git', ['push']);
154+
const pushArgs = ['push'];
155+
if (!(await hasUpstreamBranch())) {
156+
pushArgs.push('--set-upstream', 'origin', await getCurrentBranch());
157+
}
158+
const { stdout, stderr } = await execa('git', pushArgs);
134159
if (stdout) outro(stdout);
160+
displayPushUrl(stderr);
135161
process.exit(0);
136162
}
137163

@@ -147,11 +173,11 @@ ${chalk.grey('——————————————————')}`
147173

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

150-
const { stdout } = await execa('git', [
151-
'push',
152-
'--verbose',
153-
remotes[0]
154-
]);
176+
const pushArgs = ['push', '--verbose', remotes[0]];
177+
if (!(await hasUpstreamBranch())) {
178+
pushArgs.push('--set-upstream', await getCurrentBranch());
179+
}
180+
const { stdout, stderr } = await execa('git', pushArgs);
155181

156182
pushSpinner.stop(
157183
`${chalk.green('✔')} Successfully pushed all commits to ${
@@ -160,6 +186,7 @@ ${chalk.grey('——————————————————')}`
160186
);
161187

162188
if (stdout) outro(stdout);
189+
displayPushUrl(stderr);
163190
} else {
164191
outro('`git push` aborted');
165192
process.exit(0);
@@ -181,7 +208,11 @@ ${chalk.grey('——————————————————')}`
181208

182209
pushSpinner.start(`Running 'git push ${selectedRemote}'`);
183210

184-
const { stdout } = await execa('git', ['push', selectedRemote]);
211+
const pushArgs = ['push', selectedRemote];
212+
if (!(await hasUpstreamBranch())) {
213+
pushArgs.push('--set-upstream', await getCurrentBranch());
214+
}
215+
const { stdout, stderr } = await execa('git', pushArgs);
185216

186217
if (stdout) outro(stdout);
187218

@@ -190,6 +221,8 @@ ${chalk.grey('——————————————————')}`
190221
'✔'
191222
)} successfully pushed all commits to ${selectedRemote}`
192223
);
224+
225+
displayPushUrl(stderr);
193226
}
194227
}
195228
} else {

0 commit comments

Comments
 (0)