Skip to content

Commit

Permalink
Correctly handle merge commits in verbose mode
Browse files Browse the repository at this point in the history
Also fix incorrect shortening of multi-line strings which caused
verbose git stats to be removed unexpectedly.

Fix #343.
  • Loading branch information
psss authored and lukaszachy committed Feb 1, 2024
1 parent 9c6f40b commit 0584a30
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
38 changes: 27 additions & 11 deletions did/plugins/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ def commits(self, user, options):
""" List commits for given user. """
# Prepare the command
command = "git log --all --author={0}".format(user.login).split()
command.append("--format=format:%h - %s")
command.append("--since='{0} 00:00:00'".format(options.since))
command.append("--until='{0} 00:00:00'".format(options.until))
if options.verbose:
command.append("--name-only")
# Need an extra new line to separate merge commits otherwise
# they are squeezed together without an empty line between
command.append("--format=format:%n%h - %s")
else:
command.append("--format=format:%h - %s")
log.info("Checking commits in {0}".format(self.path))
log.details(pretty(command))

Expand All @@ -61,21 +65,33 @@ def commits(self, user, options):
raise did.base.ReportError(
"Unable to access git repo '{0}'".format(self.path))
output, errors = process.communicate()
output = output.strip()
log.debug("git log output:")
log.debug(output)
if process.returncode == 0:
if not output:
return []
else:
if not options.verbose:
return output.split("\n")
commits = []
for commit in output.split("\n\n"):
summary = commit.split("\n")[0]
directory = re.sub("/[^/]+$", "", commit.split("\n")[1])
commits.append("{0}\n{1}* {2}".format(
summary, 8 * " ", directory))
return commits

# Single commit per line in non-verbose mode
if not options.verbose:
return output.split("\n")

# In verbose mode commits separated by two empty lines
commits = []
for commit in re.split("\n\n+", output):
lines = commit.split("\n")

# Use a single line if no files changed (e.g. merges)
if len(lines) == 1:
commits.append(lines[0])

# Show the first directory with modified files
# FIXME: But why just the first one? Shouldn't we show
# all? Or at least more? With a maximum limit?
else:
directory = re.sub("/[^/]+$", "", lines[1])
commits.append("{0}\n{1}* {2}".format(lines[0], 8 * " ", directory))
return commits
else:
log.debug(errors.strip())
log.warning("Unable to check commits in '{0}'".format(self.path))
Expand Down
21 changes: 16 additions & 5 deletions did/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,22 @@ def header(text, separator=DEFAULT_SEPARATOR, separator_width=MAX_WIDTH):


def shorted(text, width=MAX_WIDTH):
""" Shorten text, make sure it's not cut in the middle of a word """
if len(text) <= width:
return text
# We remove any word after first overlapping non-word character
return "{0}...".format(re.sub(r"\W+\w*$", "", text[:width - 2]))
"""
Shorten text, make sure it's not cut in the middle of a word
When multiple lines are provided in the text, each of them is
shortened separately.
"""
lines = []

for line in text.split("\n"):
if len(line) <= width:
lines.append(line)
else:
# Remove any word after first overlapping non-word character
lines.append("{0}...".format(re.sub(r"\W+\w*$", "", line[:width - 2])))

return "\n".join(lines)


def item(text, level=0, options=None):
Expand Down

0 comments on commit 0584a30

Please sign in to comment.