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

Navi collapses multiple newlines into a single newline #539

Closed
knpwrs opened this issue Apr 28, 2021 · 5 comments · Fixed by #595
Closed

Navi collapses multiple newlines into a single newline #539

knpwrs opened this issue Apr 28, 2021 · 5 comments · Fixed by #595
Labels
bug Something isn't working

Comments

@knpwrs
Copy link

knpwrs commented Apr 28, 2021

Describe the bug

Navi collapses multiple newlines into a single newline.

To Reproduce

Consider the following snippet to compose a commit message with a co-author:

% Git

# Commit with co-author
git commit -m "$(cat <<EOF
<commit_message>

<co_author>
EOF
)"

$ co_author: git log --pretty="%an <%ae>" | sort | uniq | sed 's|^|Co-authored-by: |g'

I expect the following to be populated to my shell:

git commit -m "$(cat <<EOF
My awesome commit message

Co-authored-by: User <[email protected]>
EOF
)"

Instead, the following is populated to my shell:

git commit -m "$(cat <<EOF
My awesome commit message
Co-authored-by: User <[email protected]>
EOF
)"

The newlines between the first line of the message on the Co-authored-by line are collapsed into a single newline.

Versions:

  • OS: macOS 11
  • Shell Version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin20)
@knpwrs knpwrs added the bug Something isn't working label Apr 28, 2021
@welcome
Copy link

welcome bot commented Apr 28, 2021

Thanks for opening your first issue here! In case you're facing a bug, please update navi to the latest version first. Maybe the bug is already solved! :)

@denisidoro
Copy link
Owner

denisidoro commented Apr 29, 2021

Interesting use case, but I'm afraid multi-line snippets will never be reliable. If one of the lines between the EOFs start with #, navi will think a new snippet is starting.

Anyway, consecutive newlines get collapsed because navi ignores empty lines.

Considering empty lines is tricky because it's not clear where snippets start/end.

I guess I could right trim them, though.

@knpwrs
Copy link
Author

knpwrs commented Apr 29, 2021

I managed to get something that works for my use case (i.e., pre-populating co-authors for git commit messages based on authors in the current repo).

Feel free to close this issue if newlines aren't going to be supported.

For anyone reading this issue looking for a similar snippet, my solution is very git-specific (it doesn't actually solve the newlines issue outside of git snippets):

% Git

# Commit with co-author
git commit -e -m "<commit_message>" -m "<co_authors>"

$ co_authors: git log --pretty="%an <%ae>" | sort | uniq | sed 's|^|Co-authored-by: |g' --- --multi

I found out through this answer on StackOverflow that you can pass multiple -m flags to git, and git will automatically put a blank line between each message passed to each subsequent -m. The snippet above expands to the following:

git commit -e -m "Blah blah blah" -m "Co-authored-by: user1 <[email protected]>
Co-authored-by: user2 <[email protected]>"

This works for me in zsh and results in the following commit message:

Blah blah blah

Co-authored-by: user1 <[email protected]>
Co-authored-by: user2 <[email protected]>

The -e flag also opens $EDITOR or $GIT_EDITOR, giving you the opportunity to modify the commit message. You can remove -e if you'd like to just commit directly from the shell. Alternatively, if you would like to compose your commit message in your editor rather than navi you can change the snippet to the following:

% Git

# Commit with co-author
git commit -e -m "commit message" -m "<co_authors>"

$ co_authors: git log --pretty="%an <%ae>" | sort | uniq | sed 's|^|Co-authored-by: |g' --- --multi

My $EDITOR is set to vim, so from there I can just press cc, compose my message, and not have to remember full names and emails for my co-authors!

@denisidoro
Copy link
Owner

denisidoro commented Apr 29, 2021

Nice!

I intend to make navi not ignore blank lines in the near future, but multi-line snippets will always have some caveats, as I said.

Regarding workarounds, there are a multitude of ways to build multi-line strings. Example:

# Commit with pre-defined message
git commit -am "$(echo -e "<foo>\nlorem ipsum\n<bar>")"

$ foo: ls
$ bar: ls

@knpwrs
Copy link
Author

knpwrs commented Apr 29, 2021

I could have sworn I tried echo -e but it wasn't working for me. Just tried it again and it works 🤦🏻 .

EDIT: I know what I did. I tried git commit -em "$(echo -e "\n\n<co_authors>")". git trimmed the leading newlines when opening the editor. The equivalent of my latest snippet would be git commit -em "$(echo -e "commit message\n\n<co_authors>")".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants