stripspace.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # stripspace.py -- Git stripspace functionality
  2. # Copyright (C) 2025 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. """Git stripspace functionality for cleaning up text and commit messages."""
  20. def stripspace(
  21. text: bytes,
  22. *,
  23. strip_comments: bool = False,
  24. comment_char: bytes = b"#",
  25. comment_lines: bool = False,
  26. ) -> bytes:
  27. """Strip unnecessary whitespace from text.
  28. This function mimics the behavior of `git stripspace`, which is commonly
  29. used to clean up commit messages and other text content.
  30. Args:
  31. text: The text to process (as bytes)
  32. strip_comments: If True, remove lines that begin with comment_char
  33. comment_char: The comment character to use (default: b"#")
  34. comment_lines: If True, prepend comment_char to each line
  35. Returns:
  36. The processed text as bytes
  37. The function performs the following operations (in order):
  38. 1. If comment_lines is True, prepend comment_char + space to each line
  39. 2. Strip trailing whitespace from each line
  40. 3. If strip_comments is True, remove lines starting with comment_char
  41. 4. Collapse multiple consecutive blank lines into a single blank line
  42. 5. Remove leading blank lines
  43. 6. Remove trailing blank lines
  44. 7. Ensure the text ends with a newline (unless empty)
  45. """
  46. if not text:
  47. return b""
  48. # Split into lines (preserving line endings for processing)
  49. lines = text.splitlines(keepends=True)
  50. # Step 1 & 2: Strip leading and trailing whitespace from each line (but keep newlines)
  51. processed_lines = []
  52. for line in lines:
  53. # Determine line ending
  54. line_ending = b""
  55. if line.endswith(b"\r\n"):
  56. line_ending = b"\r\n"
  57. elif line.endswith(b"\n"):
  58. line_ending = b"\n"
  59. elif line.endswith(b"\r"):
  60. line_ending = b"\r"
  61. # Strip all whitespace from the line content
  62. stripped_content = line.rstrip(b"\r\n\t ").lstrip()
  63. # If comment_lines is True, prepend comment char to non-empty lines only
  64. if comment_lines and stripped_content:
  65. stripped_content = comment_char + b" " + stripped_content
  66. # Reassemble with line ending
  67. processed_lines.append(stripped_content + line_ending)
  68. # Step 3: Strip comments if requested
  69. if strip_comments:
  70. processed_lines = [
  71. line
  72. for line in processed_lines
  73. if not line.lstrip().startswith(comment_char)
  74. ]
  75. if not processed_lines:
  76. return b""
  77. # Step 4 & 5 & 6: Collapse multiple blank lines, remove leading/trailing blank lines
  78. # First, identify blank lines (lines that are just whitespace/newline)
  79. def is_blank(line: bytes) -> bool:
  80. return line.strip() == b""
  81. # Remove leading blank lines
  82. while processed_lines and is_blank(processed_lines[0]):
  83. processed_lines.pop(0)
  84. # Remove trailing blank lines
  85. while processed_lines and is_blank(processed_lines[-1]):
  86. processed_lines.pop()
  87. # Collapse consecutive blank lines
  88. collapsed_lines = []
  89. prev_was_blank = False
  90. for line in processed_lines:
  91. is_current_blank = is_blank(line)
  92. if is_current_blank and prev_was_blank:
  93. # Skip this blank line
  94. continue
  95. collapsed_lines.append(line)
  96. prev_was_blank = is_current_blank
  97. if not collapsed_lines:
  98. return b""
  99. # Step 7: Ensure text ends with newline
  100. # Join all lines together
  101. result = b"".join(collapsed_lines)
  102. # If the result doesn't end with a newline, add one
  103. if not result.endswith((b"\n", b"\r\n", b"\r")):
  104. result += b"\n"
  105. return result