Skip to content

Commit db4aa16

Browse files
committed
feat: path input
1 parent 04911be commit db4aa16

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

index.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,23 @@ async function main() {
3737
title: core.getInput("title"),
3838
body: core.getInput("body"),
3939
branch: core.getInput("branch"),
40+
path: core.getInput("path"),
4041
commitMessage: core.getInput("commit-message"),
4142
author: core.getInput("author")
4243
};
4344

4445
core.debug(`Inputs: ${inspect(inputs)}`);
4546

46-
const { hasChanges, hasUncommitedChanges } = await getLocalChanges();
47+
const { hasChanges, hasUncommitedChanges } = await getLocalChanges(
48+
inputs.path
49+
);
4750

4851
if (!hasChanges) {
49-
core.info("No local changes");
52+
if (inputs.path) {
53+
core.info(`No local changes matchin "${inputs.path}"`);
54+
} else {
55+
core.info("No local changes");
56+
}
5057
process.exit(0); // there is currently no neutral exit code
5158
}
5259

@@ -70,10 +77,16 @@ async function main() {
7077
});
7178
}
7279

73-
core.debug(`Comitting local changes`);
74-
await command("git add .", { shell: true });
80+
if (inputs.path) {
81+
core.debug(`Committing local changes matching "${inputs.path}"`);
82+
await command(`git add "${inputs.path}"`, { shell: true });
83+
} else {
84+
core.debug(`Committing all local changes`);
85+
await command("git add .", { shell: true });
86+
}
87+
7588
await command(
76-
`git commit -a -m "${inputs.commitMessage}" --author "${inputs.author}"`,
89+
`git commit -m "${inputs.commitMessage}" --author "${inputs.author}"`,
7790
{ shell: true }
7891
);
7992
} else {
@@ -84,12 +97,11 @@ async function main() {
8497
const remoteBranchExists = await checkOutRemoteBranch(inputs.branch);
8598

8699
core.debug(`Pushing local changes`);
87-
const { stdout: pushStdOut, stderr: pushStdErr } = await command(
100+
await command(
88101
`git push -f https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git HEAD:refs/heads/${inputs.branch}`,
89102
{ shell: true }
90103
);
91104

92-
// no idea why the `git push` output goes into stderr. Checking in both just in case.
93105
if (remoteBranchExists) {
94106
core.info(`Existing pull request for "${inputs.branch}" updated`);
95107
return;
@@ -115,8 +127,10 @@ async function main() {
115127
}
116128
}
117129

118-
async function getLocalChanges() {
119-
const { stdout } = await command("git status", { shell: true });
130+
async function getLocalChanges(path) {
131+
const { stdout } = await command(`git status ${path || "*"}`, {
132+
shell: true
133+
});
120134

121135
if (/Your branch is up to date/.test(stdout)) {
122136
return;
@@ -162,16 +176,27 @@ async function setGitUser({ name, email }) {
162176

163177
async function checkOutRemoteBranch(branch) {
164178
try {
179+
const { stdout } = await command(`git rev-parse --abbrev-ref HEAD`, {
180+
shell: true
181+
});
182+
183+
if (stdout === branch) {
184+
core.info(`Already in "${branch}".`);
185+
return true;
186+
}
187+
165188
await command(
166189
`git fetch https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git ${branch}:${branch}`,
167190
{ shell: true }
168191
);
192+
169193
await command(`git checkout ${branch}`, { shell: true });
170194
core.info(`Remote branch "${branch}" checked out locally.`);
171-
await command(`git rebase -Xtheirs -`, { shell: true });
195+
await command(`git rebase -Xtheirs --autostash -`, { shell: true });
172196
return true;
173197
} catch (error) {
174198
core.info(`Branch "${branch}" does not yet exist on remote.`);
199+
await command(`git checkout -b ${branch}`, { shell: true });
175200
return false;
176201
}
177202
}

0 commit comments

Comments
 (0)