-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add capability to gather the git commit log since the last release (#357
) Add capability to gather the commit log since the last release and use that to start the release notes. Warning: This is experimental. - Add a git toolchain - It gets the path to the git binary and the path to your source tree - therefore it can only be used locally. - Fixes #257 - Add a `git_changelog()` rule that will run git to get the change log. - Advances #228 - Change print_relnotes to accept a changelog file, instead of running git locally. Next steps: - Add the capability to reduce the change log from everything to only the commits that have RELNOTES sections.
- Loading branch information
Showing
12 changed files
with
475 additions
and
8 deletions.
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
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
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,96 @@ | ||
# Copyright 2021 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""A rule to extract the git changelog.""" | ||
|
||
def _git_changelog_impl(ctx): | ||
"""Implements to git_changelog rule.""" | ||
|
||
args = ctx.actions.args() | ||
tools = [] | ||
|
||
toolchain = ctx.toolchains["@rules_pkg//toolchains/git:git_toolchain_type"].git | ||
if not toolchain.valid: | ||
fail("The git_toolchain is not properly configured: " + | ||
toolchain.name) | ||
if toolchain.path: | ||
args.add("--git_path", toolchain.path) | ||
else: | ||
executable = toolchain.label.files_to_run.executable | ||
tools.append(executable) | ||
tools.append(toolchain.label.default_runfiles.files.to_list()) | ||
args.add("--git_path", executable.path) | ||
args.add("--git_root", toolchain.client_top) | ||
args.add("--from_ref", ctx.attr.from_ref) | ||
args.add("--to_ref", ctx.attr.to_ref) | ||
args.add("--out", ctx.outputs.out.path) | ||
if ctx.attr.verbose: | ||
args.add("--verbose") | ||
|
||
ctx.actions.run( | ||
mnemonic = "GitChangelog", | ||
executable = ctx.executable._git_changelog, | ||
use_default_shell_env = True, | ||
arguments = [args], | ||
outputs = [ctx.outputs.out], | ||
env = { | ||
"LANG": "en_US.UTF-8", | ||
"LC_CTYPE": "UTF-8", | ||
"PYTHONIOENCODING": "UTF-8", | ||
"PYTHONUTF8": "1", | ||
}, | ||
execution_requirements = { | ||
"local": "1", | ||
}, | ||
tools = tools, | ||
) | ||
|
||
# Define the rule. | ||
_git_changelog = rule( | ||
doc = "Extracts the git changelog between two refs.", | ||
attrs = { | ||
"from_ref": attr.string( | ||
doc = "lower commit ref. The default is to use the latest tag", | ||
default = "_LATEST_TAG_", | ||
), | ||
"to_ref": attr.string( | ||
doc = "upper commit ref. The default is HEAD", | ||
default = "HEAD", | ||
), | ||
"out": attr.output(mandatory = True), | ||
"verbose": attr.bool( | ||
doc = "Be verbose", | ||
default = False, | ||
), | ||
"_git_changelog": attr.label( | ||
default = Label("//releasing:git_changelog_private"), | ||
cfg = "exec", | ||
executable = True, | ||
allow_files = True, | ||
), | ||
}, | ||
implementation = _git_changelog_impl, | ||
toolchains = ["@rules_pkg//toolchains/git:git_toolchain_type"], | ||
) | ||
|
||
|
||
def git_changelog(name, **kwargs): | ||
_git_changelog( | ||
name = name, | ||
# This requires bazel 4.x | ||
target_compatible_with = select({ | ||
"//toolchains/git:have_git": [], | ||
"//conditions:default": ["//:not_compatible"], | ||
}), | ||
**kwargs, | ||
) |
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,72 @@ | ||
# Copyright 2021 The Bazel Authors. All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Utilities to extract git commit descriptions in useful ways.""" | ||
|
||
import argparse | ||
import os | ||
import subprocess | ||
import sys | ||
|
||
|
||
def guess_previous_release_tag(git_path, pattern=None): | ||
assert git_path | ||
most_recent = None | ||
cmd = [git_path, 'tag'] | ||
if pattern: | ||
cmd.extend(['--list', pattern]) | ||
# We are doing something dumb here for now. Grab the list of tags, and pick | ||
# the last one. | ||
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc: | ||
most_recent = proc.stdout.read().decode('utf-8') | ||
most_recent = most_recent.strip().replace('\n\n', '\n').split('\n')[-1] | ||
return most_recent | ||
|
||
|
||
def git_changelog(from_ref, to_ref='HEAD', git_path=None): | ||
assert from_ref | ||
assert to_ref | ||
assert git_path | ||
cmd = [git_path, 'log', '%s..%s' % (from_ref, to_ref)] | ||
with subprocess.Popen(cmd, stdout=subprocess.PIPE) as proc: | ||
return proc.stdout.read().decode('utf-8') | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser( | ||
description='Helper for extracting git changelog', | ||
fromfile_prefix_chars='@') | ||
parser.add_argument('--git_path', required=True, help='path to git binary') | ||
parser.add_argument('--git_root', required=True, help='path to git client') | ||
parser.add_argument('--out', required=True, help='output path') | ||
parser.add_argument('--from_ref', help='from REF') | ||
parser.add_argument('--to_ref', help='to REF') | ||
parser.add_argument('--verbose', action='store_true') | ||
|
||
options = parser.parse_args() | ||
|
||
with open(options.out, 'w', encoding='utf-8') as out: | ||
os.chdir(options.git_root) | ||
from_ref = options.from_ref | ||
if not from_ref or from_ref == '_LATEST_TAG_': | ||
from_ref = guess_previous_release_tag(options.git_path) | ||
to_ref = options.to_ref or 'HEAD' | ||
if options.verbose: | ||
print('Getting changelog from %s to %s' % (from_ref, to_ref)) | ||
changelog = git_changelog( | ||
from_ref=from_ref, to_ref=to_ref, git_path=options.git_path) | ||
out.write(changelog) | ||
return 0 | ||
|
||
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
Oops, something went wrong.