Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to use a multi-word git tag message? #166

Closed
ruohola opened this issue Apr 13, 2021 · 10 comments · Fixed by #174
Closed

No way to use a multi-word git tag message? #166

ruohola opened this issue Apr 13, 2021 · 10 comments · Fixed by #174
Assignees
Labels
type: bug Verified problems that need to be worked on

Comments

@ruohola
Copy link

ruohola commented Apr 13, 2021

There doesn't seem to be any way to pass a --message that has multiple word to the Git tag command. For example this attempt:

  - name: "Commit changes"
    uses: EndBug/add-and-commit@v7
    with:
      add: 'CHANGELOG.md'
      message: 'Release 1.0.0'
      tag: "v1.0.0 --annotate --message 'Release 1.0.0'"

Fails with:

2021-04-13T10:51:08.1831581Z > Tagging commit...
2021-04-13T10:51:08.1889446Z ##[error]Error: fatal: Failed to resolve '1.0.0'' as a valid ref.

2021-04-13T10:51:08.1904561Z ##[error]Error: fatal: Failed to resolve '1.0.0'' as a valid ref.

2021-04-13T10:51:08.1908826Z > Pushing commit to repo...

and this:

 - name: "Commit changes"
    uses: EndBug/add-and-commit@v7
    with:
      add: 'CHANGELOG.md'
      message: 'Release 1.0.0'
      tag: 'v1.0.0 --annotate --message "Release 1.0.0"'

results in the tag message being literally "Release 1.0.0".

I tried all kinds of YAML quoting, but the problem is really this function:

export function matchGitArgs(string: string) {
return string.match(/(?:[^\s"]+|"[^"]*")+/g) || []
}

There is no way to pass arguments to that so that it would keep the Release 1.0.0 as one string with no extra quotes around it:

https://www.typescriptlang.org/play?ts=4.2.3#code/GYVwdgxgLglg9mABAWwIZQgCwOIygQQCcBzAZwApSpCYxiAuRKmugSkQG8AoRRQgUyghCSZrWIA6NBkzkA9OQD89ANoA9ADqkARAF0A1AB9t6vQCptrfXOLtDhxCt1cAvly4QEpOABt+EnzhicmksXAISCm0ANwBGCQAGRMQAWhTUMDA4KHR+VJTkflJSVGI8gHIAJX4-VFI8+KSE8stWAG4PL19-QODQnDwiMnJyuMTktIysnKg8tMLi0rztatr6xEbE7XLWds6wbz8AoJD0MMHI8hjNhPyp7Nz8hZKyxA0Vmv46hvGE99aOp4Dt1jn0zgMIsNRjc7pkHrMnkUXnkNFVPt8Nr9UTt2kA

@ruohola ruohola added the status: pending More info is needed before deciding what to do label Apr 13, 2021
@ruohola ruohola changed the title How to use a multi-word git tag message? No way to use a multi-word git tag message? Apr 13, 2021
@EndBug EndBug self-assigned this Apr 13, 2021
@EndBug EndBug added type: bug Verified problems that need to be worked on and removed status: pending More info is needed before deciding what to do labels Apr 13, 2021
@ruohola
Copy link
Author

ruohola commented Apr 13, 2021

I solved this for now by writing the tag message to a file in a previous step and then using tag: v1.0.0 --annotate -- file=message.txt in the add-and-commit step.

Btw, thanks for making this awesome action 🙏

@EndBug
Copy link
Owner

EndBug commented Apr 14, 2021

Happy to see that you found a workaround :)
Anyway, I'll try to see if I can fix it as soon as I can

@EndBug
Copy link
Owner

EndBug commented Apr 14, 2021

Hi, I think I've fixed the issue. I've edited the function to remove double quotes:

export function matchGitArgs(string: string) {
return (string.match(/(?:[^\s"]+|"[^"]*")+/g) || []).map((s) =>
// Removes double quotes, to avoid problems with simple-git
s.replace(/^"(.*)"$/, '$1')
)
}

Sorry it took so long but I had to troubleshoot an issue with the compiler dependency. I'd like you to test this fix by temporarily using the git-args branch:

- uses: EndBug/add-and-commit@git-args
  ...

Let me know if that solves the issue!

@ruohola
Copy link
Author

ruohola commented Apr 14, 2021

Hi, I think I've fixed the issue. I've edited the function to remove double quotes:

export function matchGitArgs(string: string) {
return (string.match(/(?:[^\s"]+|"[^"]*")+/g) || []).map((s) =>
// Removes double quotes, to avoid problems with simple-git
s.replace(/^"(.*)"$/, '$1')
)
}

Sorry it took so long but I had to troubleshoot an issue with the compiler dependency. I'd like you to test this fix by temporarily using the git-args branch:

- uses: EndBug/add-and-commit@git-args
  ...

Let me know if that solves the issue!

😅 actually it turned out that for my current use case the file method worked better, since the message will be quite long. Great if you got this fixed though since the current behavior is of course problematic. 💪

@ruohola
Copy link
Author

ruohola commented Apr 14, 2021

Not sure if removing double quotes is a good solution, since then one couldn't use --message 'This is "quoted" message' at all. Might not be trivial to solve that one though.

@EndBug
Copy link
Owner

EndBug commented Apr 15, 2021

You're right. Thanks to a guy on Stack Overflow I've gotten to this solution:

/**
* Matches the different pathspecs and arguments by removing spaces that are not inside quotes
* {@link https://stackoverflow.com/a/67103621/7133466}
* @example
* ```js
* matchGitArgs(`--message "This is a 'quoted' message" --other 'This uses the "other" quotes' --foo 1234`) => ["--message", "This is a 'quoted' message", "--other", "This uses the \"other\" quotes", "--foo", "1234"]
* matchGitArgs(' ') => [ ]
* ```
* @returns An array, if there's no match it'll be empty
*/
export function matchGitArgs(string: string) {
const tokens = String.raw`
(?<option> --\w+)
| (' (?<sq> (\\. | [^'])* ) ')
| (" (?<dq> (\\. | [^"])* ) ")
| (?<raw> \S+)
`
const re = tokens.replace(/\s+/g, '')
const result: string[] = []
for (const m of matchAll(string, re)) {
result.push(...Object.values(m.groups || {}).filter(Boolean))
}
return result
}

Here's an interactive version. This should correctly parse every argument.

Anyway, I agree that if you're going to use a very long commit message, using a file is the best solution.

@ruohola
Copy link
Author

ruohola commented Apr 15, 2021

Here's an interactive version. This should correctly parse every argument.

Looks solid 👍

@EndBug
Copy link
Owner

EndBug commented Apr 16, 2021

@all-contributors please add @ruohola for their bug report

@allcontributors
Copy link
Contributor

@EndBug

I've put up a pull request to add @ruohola! 🎉

@EndBug
Copy link
Owner

EndBug commented Apr 16, 2021

Fix has been published in v7.1.2 (also v7 and latest). Thanks again for the report!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Verified problems that need to be worked on
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants