-
Notifications
You must be signed in to change notification settings - Fork 6
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
copy the pre-commit-hook into this repository and convert it to py3 #23
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#!/usr/bin/python | ||
""" | ||
This is a prepare-commit-msg hook that fill append commit messages with:: | ||
|
||
Resolves: {TRACKER}#{TICKET-ID} | ||
|
||
For example a RedHat BZ 0001 would look like:: | ||
|
||
Correct the syntax error | ||
|
||
Signed-off-by: Alfredo Deza <[email protected]> | ||
|
||
Resolves: rhbz#0001 | ||
|
||
The requirement is to branch with the right identifier. For the above example | ||
the branch name would need to be: rhbz-0001 | ||
|
||
This hook will split on `-` and use all lower casing to transform the branch | ||
into the "Resolves" line. | ||
|
||
Copy this file to $GITREPOSITORY/.git/hooks/prepare-commit-msg | ||
and mark it executable. | ||
""" | ||
import subprocess | ||
import sys | ||
import os | ||
|
||
|
||
def which(executable): | ||
locations = ( | ||
'/usr/local/bin', | ||
'/bin', | ||
'/usr/bin', | ||
'/usr/local/sbin', | ||
'/usr/sbin', | ||
'/sbin', | ||
) | ||
|
||
for location in locations: | ||
executable_path = os.path.join(location, executable) | ||
if os.path.exists(executable_path): | ||
return executable_path | ||
|
||
GIT = which('git') | ||
|
||
|
||
def branch_name(): | ||
try: | ||
name = subprocess.check_output( | ||
[GIT, "symbolic-ref", "HEAD"], | ||
stderr=subprocess.STDOUT).decode() | ||
except Exception as err: | ||
if 'fatal: ref HEAD is not a symbolic ref' in err.output: | ||
# we are in a rebase or detached head state | ||
return '' | ||
# This looks like: refs/heads/12345678/my-cool-feature | ||
# if we ever get a branch that has '/' in it we are going to have | ||
# some issues. | ||
return name.split('/')[-1] | ||
parts = name.split('/') | ||
if len(parts) != 4: | ||
raise ValueError("Branch name has '/' in it which is not allowed") | ||
branch = parts[-1] | ||
return branch | ||
|
||
|
||
def prepend_commit_msg(branch): | ||
"""Prepend the commit message with `text`""" | ||
msgfile = sys.argv[1] | ||
with open(msgfile) as f: | ||
contents = f.read() | ||
|
||
if not branch: | ||
return contents | ||
|
||
try: | ||
prefix, ticket_id = branch.split('-') | ||
ticket_id = int(ticket_id) | ||
except ValueError: | ||
# We used to raise here, but if cherry-picking to a different branch | ||
# that doesn't comply it would break preventing the cherry-pick. So we | ||
# print out the warning, but end up returning the contents. | ||
print('skipping commit msg change: Branch name "%s" does not follow required format: {tracker}-{ID}' % branch) # NOQA | ||
return contents | ||
|
||
if prefix == 'wip': | ||
# Skip "wip" branches, ie "work in progress" | ||
return contents | ||
|
||
resolves_line = "\nResolves: %s#%s" % (prefix.lower(), ticket_id) | ||
|
||
with open(msgfile, 'a') as f: | ||
# Don't append if it's already there | ||
if resolves_line not in contents: | ||
f.write(resolves_line) | ||
|
||
Comment on lines
+67
to
+96
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As we're invoking the git command, this could be easily done with subprocess.run(['git', 'interpret-trailers', f'--trailer Resolves: rhbz#{ticket_id}', '--in-place', sys.argv[1]], shell=True, check=True) That said, a pre-commit hook is not needed for this, and IMHO it should rather be done with interepreter trailers: - git('-c', 'core.editor=/bin/true', 'cherry-pick', '-x', '%s~..%s' % (first, last))
+ git('-c', 'core.editor=/bin/true', 'cherry-pick', '-x', '--no-commit', '%s~..%s' % (first, last))
+ git('commit', '--trailer "Resolves: rhbz#{$bz}"', '--no-edit')
# Merge this rhbz branch back into our starting branch.
git('checkout', starting_branch)
git('merge', '--ff-only', 'rhbz-' + bz)
git('branch', '-d', 'rhbz-' + bz)
|
||
|
||
def main(): | ||
branch = branch_name().strip().strip('\n') | ||
prepend_commit_msg(branch) | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shutil.which()
?