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

Allow ignoring comment lines in commits from files #2

Merged

Conversation

PurpleBooth
Copy link
Contributor

@PurpleBooth PurpleBooth commented Apr 22, 2017

When git is creating a commit, the message it reads from a file does
not include the lines beginning with a comment character. This means
that the commit files that you get passed with a git commit message
hook is not exactly what will appear in git.

The comment character in versions of git later than 1.8.2 is
configurable, in versions prior to that it's always '#'.

In this implementation we do not preserve the comment lines at all.

@PurpleBooth PurpleBooth force-pushed the support-git-comments branch 2 times, most recently from 8ea0cd6 to 9d8ffcd Compare April 22, 2017 06:31
@sebastianfeldmann
Copy link
Owner

sebastianfeldmann commented Apr 22, 2017

Nice! Thanks for making the effort.

I have three questions regarding this.
Firstly some understanding. Has the line to start with a $commentChar or does it suffice if the $commentChar is only prefixed with white spaces
So that this is a comment line

# I'm a comment line

And this is also a comment line

    # I have leading spaces but I'm a comment as well?

If so, we should trim() the line before checking the "first" character.

Secondly a note to your concern. So far the CommitMessage holds all the lines. Internally it manages the lines separately in

  • $lines - all lines that where entered
  • $subject - the first line of the commit message
  • $body - all lines of the "body", so line 3 - end if exists

So we could keep the comment lines in $lines and remove them from $subject and $body.
This way we could have access to the comment lines as well if we would need to.
I'm not sure if this is wise or necessary so I'm hoping you have a thought on this.

And lastly a taste thing, wouldn't it be "cleaner" to set the default for $commentChar to '#'
This way it would only be of type string and if not given the git default behavior is emulated.

Thanks again, looking forward for your opinion.

Cheers, Sebastian

@PurpleBooth
Copy link
Contributor Author

PurpleBooth commented Apr 22, 2017

Those sound like some great improvements, I'll get on making those changes, I actually toyed with putting the logic inside the CommitMessage but it was a larger change and I wanted your feedback before I went for that.

I wasn't sure about the whitespace thing, but apparently it does have to be right at the start of the line:

commit 4b36929f1b102745732ac9c340cca57e0f0caacb
Author: Billie Thompson <billie@purplebooth.co.uk>
Date:   Sat Apr 22 11:07:09 2017 +0100

    Am I a commit Message
    
              # Am I a comment or not?

@PurpleBooth PurpleBooth force-pushed the support-git-comments branch 2 times, most recently from 30958a3 to f5376ee Compare April 22, 2017 11:36
@PurpleBooth
Copy link
Contributor Author

That's done now.

You might however notice that the CommitMessage defaults the character
to null. This is because a commit message from the repository may have
had any comment character set when it was created, so you could see
lines in a commit message from the repository that begin with what is
now the comment character.

The from file method defaults to '#' however.

The lines with the comments can still be accessed via getLines methods,
however the semantic methods of getBody and getSubject do not return
them.

When git is creating a commit, the message it reads from a file does
not include the lines beginning with a comment character. This means
that the commit files that you get passed with a git commit message
hook is not exactly what will appear in git if saved.

The comment character in versions of git later than 1.8.2 is
configurable, in versions prior to that it's always '#'.

You might however notice that the CommitMessage defaults the character
to null. This is because a commit message from the repository may have
had any comment character set when it was created, so you could see
lines in a commit message from the repository that begin with what is
now the comment character.

The lines with the comments can still be accessed via getLines methods,
however the semantic methods of getBody and getSubject do not return
them.
@sebastianfeldmann sebastianfeldmann merged commit abe6f00 into sebastianfeldmann:master Apr 22, 2017
@sebastianfeldmann
Copy link
Owner

Looks great!

@PurpleBooth PurpleBooth deleted the support-git-comments branch April 22, 2017 11:47
@sebastianfeldmann
Copy link
Owner

I made a slight adjustment, the method getContent now returns the content without comments.
To get the content including all comments I added a getRawContent method.
This way if we "talk" about the content we always "talk" about the message without the comments.

  • getContentLines
  • isEmpty
  • and now getContentas well

@PurpleBooth
Copy link
Contributor Author

PurpleBooth commented Apr 22, 2017 via email

@sebastianfeldmann
Copy link
Owner

Thanks again, I just tagged version 1.0.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants