Tidy Package¶
tidy¶
The core git-tidy
functions and classes that are utilized by
the Tidy CLI.
- class tidy.Commit(msg, schema, tag_match=None)[source]¶
Parses a commit message into structured components.
It is assumed the commit message is formatted as YAML by the appropriate “git log” command (see
CommitRange
). If data is able to be parsed, attributes of the commit can be accessed as attributes of this object. For example, atype
attribute in the schema is accessible asCommit().type
.If the commit cannot be parsed as valid YAML for unexpected reasons,
is_parsed
isFalse
and only a limited amount of attributes are available.- property is_parsed¶
True
if the commit has been parsed successfully.If
False
, only thesha
andmsg
attributes are available.
- property is_valid¶
True
if the commit was successfully validated against the schema. IfFalse
, some attributes in the schema may be missing.
- property msg¶
The raw git commit message
- property validation_errors¶
Returns the schema
formaldict.Errors
that occurred during validation
- class tidy.CommitRange(range='', tag_match=None, before=None, after=None, reverse=False)[source]¶
Represents a range of commits. The commit range can be filtered and grouped using all of the methods in
Commits
.When doing
git log
, the user can provide a range (e.g. “origin/develop..”). Any range used in “git log” can be used as a range to the CommitRange object.If the special
:github/pr
value is used as a range, the Github API is used to figure out the range based on a pull request opened from the current branch (if found).
- class tidy.Commits(commits)[source]¶
A filterable and groupable collection of commits
When a list of Commit objects is organized in this sequence, the “group”, “filter”, and “exclude” chainable methods can be used for various access patterns. These access patterns are typically used when writing git tidy log templates.
- group(attr, ascending_keys=False, descending_keys=False, none_key_first=False, none_key_last=False)[source]¶
Group commits by an attribute
- Parameters
attr (str) – The attribute to group by.
ascending_keys (bool, default=False) – Sort the keys in ascending order.
descending_keys (bool, default=False) – Sort the keys in descending order.
none_key_first (bool, default=False) – Make the “None” key be first.
none_key_last (bool, default=False) – Make the “None” key be last.
- Returns
A dictionary of
Commits
keyed on groups.- Return type
- class tidy.Tag(tag)[source]¶
A git tag.
- property date¶
Parse the date of the tag
- Returns
The tag parsed as a datetime object.
- Return type
datetime
- tidy.commit(no_verify=False, allow_empty=False, defaults=None)[source]¶
Performs a tidy git commit.
- Parameters
- Returns
The result from running git commit. Returns the git pre-commit hook results if failing during hook execution.
- Return type
- tidy.lint(range='', any=False)[source]¶
Lint commits against an upstream (branch, sha, etc).
- Parameters
range (str, default='') – The git revision range against which linting happens. The special value of “:github/pr” can be used to lint against the remote branch of the pull request that is opened from the local branch. No range means linting will happen against all commits.
any (bool, default=False) – If True, linting will pass if at least one commit passes.
- Raises
NoGithubPullRequestFoundError – When using
:github/pr
as the range and no pull requests are found.MultipleGithubPullRequestsFoundError – When using
:github/pr
as the range and multiple pull requests are found.
- Returns
A tuple of the lint result (True/False) and the associated CommitRange
- Return type
- tidy.log(range='', style='default', tag_match=None, before=None, after=None, reverse=False, output=None)[source]¶
Renders git logs using tidy rendering.
- Parameters
range (str, default='') – The git revision range over which logs are output. Using “:github/pr” as the range will use the base branch of an open github pull request as the range. No range will result in all commits being logged.
style (str, default="default") – The template to use when rendering. Defaults to “default”, which means
.git-tidy/log.tpl
will be used to render. When used, the.git-tidy/log_{{style}}.tpl
file will be rendered.tag_match (str, default=None) – A glob(7) pattern for matching tags when associating a tag with a commit in the log. Passed through to
git describe --contains --matches
when finding a tag.before (str, default=None) – Only return commits before a specific date. Passed directly to
git log --before
.after (str, default=None) – Only return commits after a specific date. Passed directly to
git log --after
.reverse (bool, default=False) – Reverse ordering of results. Passed directly to
git log --reverse
.output (str|file) – Path or file-like object to which the template is written. Using the special “:github/pr” output path will post the log as a comment on the pull request.
- Raises
NoGithubPullRequestFoundError – When using
:github/pr
as the range and no pull requests are found.MultipleGithubPullRequestsFoundError – When using
:github/pr
as the range and multiple pull requests are found.
- Returns
The rendered tidy log.
- Return type
- tidy.squash(ref, no_verify=False, allow_empty=False)[source]¶
Squashes all commits since the common ancestor of ref.
- Parameters
- Raises
NoSquashableCommitsError – When no commits can be squashed.
subprocess.CalledProcessError – If the first
git reset
call unexpectedly failsNoGithubPullRequestFoundError – When using
:github/pr
as the range and no pull requests are found.MultipleGithubPullRequestsFoundError – When using
:github/pr
as the range and multiple pull requests are found.
- Returns
The commit result. The commit result contains either a failed pre-commit hook result or a successful/failed commit result.
- Return type
tidy.exceptions¶
- exception tidy.exceptions.GithubConfigurationError[source]¶
When not correctly set up for Github access
- exception tidy.exceptions.GithubPullRequestAPIError[source]¶
When an unexpected error happens with the Github pull request API
- exception tidy.exceptions.MultipleGithubPullRequestsFoundError[source]¶
When multiple Github pull requests have been opened