Skip to content

Commit

Permalink
git {ref,}log calls: pass --no-show-signature (#1285)
Browse files Browse the repository at this point in the history
* git {ref,}log calls: pass --no-show-signature

If the `git` configuration `log.showSignature` is set to `true`, then
some `git log` and `git reflog` invocations performed by `git-machete`
return GPG signature output that causes `git-machete` to fail. The
failures appear to occur because GPG signatures are assumed not to
appear in the output of those commands.

This commit resolves that failure mode by passing the
`--no-show-signature` flag to all `git log` and `git reflog`
queries. (Passing that flag to `git reflog` queries is necessary
because `git reflog` is porcelain for `git log` plus some additional
flags.)

Signed-off-by: Geoffrey M. Oxberry <[email protected]>
Signed-off-by: Geoffrey M. Oxberry <[email protected]>

* git {ref}log: use `-c log.showSignature=0` instead

The `--no-show-signature` flag and `log.showSignature` configuration
options were both added in the `git` 2.10.0 release. This commit tries
to use `-c log.showSignature=0` instead of `--no-show-signature` to
see whether it improves CI test results.

Signed-off-by: Geoffrey M. Oxberry <[email protected]>
Signed-off-by: Geoffrey M. Oxberry <[email protected]>

* flake8: fix instances of rules violations

Signed-off-by: Geoffrey M. Oxberry <[email protected]>
Signed-off-by: Geoffrey M. Oxberry <[email protected]>

* git_operations: set log.showSignature in one place

Pawel Lipski suggested setting the `log.showSignature` git
configuration option in a single location rather than passing it in 9
separate locations. This commit makes that change to simplify
maintenance, and documents the rationale for passing that setting.

Signed-off-by: Geoffrey M. Oxberry <[email protected]>
Signed-off-by: Geoffrey M. Oxberry <[email protected]>

* RELEASE_NOTES: add log.showSignature changes

To keep `git machete`'s release notes up to date, this commit mentions
the `log.showSignature` fix in those release notes.

Signed-off-by: Geoffrey M. Oxberry <[email protected]>
Signed-off-by: Geoffrey M. Oxberry <[email protected]>

* test_git_operations: test log.showsignature bug

To prevent the bug from issue #1286 from recurring, this commit adds a
test to the `git_operations` tests that will fail if the active `git`
configuration sets `log.showSignature` to `true`.

Signed-off-by: Geoffrey M. Oxberry <[email protected]>

* git_operations: make git {ref,}log calls 1-liners

Moving the location at which `git-machete` injects `-c
log.showSignature=false` into `git` command invocations means we no
longer need to break up so many long lines.

This commit reverts a collection of changes related to `git log` and
`git reflog` calls. Adding the `-c log.showSignature=false` flags to
`git log` and `git reflog` invocations caused some `flake8` line
length lint errors, which motivated splitting those invocations into
multiple lines. Removing those flags means that splitting those
invocations into multiple lines is no longer necessary. Invoking those
subcommands in Python on single lines is regarded as a readability
improvement.

Signed-off-by: Geoffrey M. Oxberry <[email protected]>

---------

Signed-off-by: Geoffrey M. Oxberry <[email protected]>
Signed-off-by: Geoffrey M. Oxberry <[email protected]>
  • Loading branch information
goxberry authored Jul 24, 2024
1 parent 3f5604a commit 029cd67
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- improved: performance of listing commits for red-edge branches on large repos
- improved: message in case of missing `.git/machete` file suggests to use `git machete git{hub,lab} checkout-{prs,mrs}`
- fixed: pass `-c log.showSignature=false` to all `git` invocations to hide GPG signatures in logs; if `log.showSignature` were set to a value equivalent to `true` in a user's `git` configuration, the GPG signatures shown in logs would cause errors in `git log` and `git reflog` parsing internal to `git machete` (reported and contributed by @goxberry)

## New in git-machete 3.26.2

Expand Down
14 changes: 12 additions & 2 deletions git_machete/git_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,16 @@ class BranchPair(NamedTuple):

HEAD = AnyRevision.of("HEAD")

# For simplicity, explicitly suppress showing GPG signatures in `git log`
# operations to simplify log parsing. This option must be passed as a
# configuration parameter because the `git log` command's `--no-show-signature`
# flag does not exist prior to `git` version 2.10.0; `git` does not emit an
# error if it is passed via the `-c` flag a configuration setting that does not
# exist, so compatibility with `git` versions earlier than version 2.10.0 is
# preserved, even though the `log.showSignature` setting also does not exist
# prior to version 2.10.0. Fixes a bug documented in GitHub issue #1286.
GIT_EXEC = ("git", "-c", "log.showSignature=false")


class GitContext:

Expand Down Expand Up @@ -224,7 +234,7 @@ def flush_caches(self) -> None:
self.__short_commit_hash_by_revision_cached = {}

def _run_git(self, git_cmd: str, *args: str, flush_caches: bool, allow_non_zero: bool = False) -> int:
exit_code = utils.run_cmd("git", git_cmd, *args)
exit_code = utils.run_cmd(*GIT_EXEC, git_cmd, *args)
if flush_caches:
self.flush_caches()
if not allow_non_zero and exit_code != 0:
Expand All @@ -234,7 +244,7 @@ def _run_git(self, git_cmd: str, *args: str, flush_caches: bool, allow_non_zero:

def _popen_git(self, git_cmd: str, *args: str,
allow_non_zero: bool = False, env: Optional[Dict[str, str]] = None, input: Optional[str] = None) -> CommandResult:
exit_code, stdout, stderr = utils.popen_cmd("git", git_cmd, *args, env=env, input=input)
exit_code, stdout, stderr = utils.popen_cmd(*GIT_EXEC, git_cmd, *args, env=env, input=input)
if not allow_non_zero and exit_code != 0:
exit_code_msg: str = fmt(f"`{utils.get_cmd_shell_repr('git', git_cmd, *args, env=env)}` returned {exit_code}\n")
stdout_msg: str = f"\n{utils.bold('stdout')}:\n{utils.dim(stdout)}" if stdout else ""
Expand Down
22 changes: 20 additions & 2 deletions tests/test_git_operations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

from git_machete.git_operations import (AnyRevision, FullCommitHash,
GitContext, LocalBranchShortName)
from git_machete.git_operations import (AnyBranchName, AnyRevision,
FullCommitHash, GitContext,
LocalBranchShortName)

from .base_test import BaseTest

Expand Down Expand Up @@ -169,3 +170,20 @@ def test_git_config_with_newlines(self) -> None:
self.repo_sandbox.write_to_file(".git/config", '[foo]\n bar = "hello\\nworld"')
git = GitContext()
assert git.get_config_attr_or_none("foo.bar") == "hello\nworld"

def test_get_reflog_when_log_showsignature_is_true(self) -> None:
(
self.repo_sandbox.new_branch("master")
.commit("master first commit")
.new_branch("feature")
.commit("feature commit")
.check_out("master")
.commit("extra commit")
)
self.repo_sandbox.set_git_config_key("log.showSignature", "true")

git = GitContext()

# If the bug reported in GitHub issue #1286 is not fixed, this method call
# should raise an UnexpectedMacheteException.
git.get_reflog(AnyBranchName.of("feature"))

0 comments on commit 029cd67

Please sign in to comment.