From 78cc355d5871835cf0f30afdeed97774d61c04ae Mon Sep 17 00:00:00 2001 From: Paul Gauthier Date: Fri, 24 May 2024 08:18:30 -0700 Subject: [PATCH] aider: Marked code lines introduced by commits with messages starting with "aider:". # Aider chat conversation: USER: i want to do something like `git blame` but i rather than marking each line with the author i want to mark all the code lines which were introduced from a commit with a commit message that starts with a specific string: "aider:" ASSISTANT: To achieve this, we need to: 1. Parse the git log to find commits with messages starting with "aider:". 2. Use `git blame` to find the lines introduced by these commits. 3. Mark these lines in the code. Here is the implementation: scripts/blame.py ```python <<<<<<< SEARCH ======= import subprocess def get_aider_commits(): """Get commit hashes for commits with messages starting with 'aider:'""" result = subprocess.run( ["git", "log", "--pretty=format:%H %s"], capture_output=True, text=True, check=True ) commits = [] for line in result.stdout.splitlines(): commit_hash, commit_message = line.split(" ", 1) if commit_message.startswith("aider:"): commits.append(commit_hash) return commits def get_blame_lines(commit_hash): """Get lines introduced by a specific commit""" result = subprocess.run( ["git", "blame", "--line-porcelain", commit_hash], capture_output=True, text=True, check=True ) lines = [] for line in result.stdout.splitlines(): if line.startswith("author "): lines.append(line) return lines def mark_aider_lines(): """Mark lines introduced by 'aider:' commits""" aider_commits = get_aider_commits() for commit in aider_commits: blame_lines = get_blame_lines(commit) for line in blame_lines: print(f"AIDER: {line}") if __name__ == "__main__": mark_aider_lines() >>>>>>> REPLACE ``` This script will: 1. Find all commits with messages starting with "aider:". 2. Use `git blame` to find the lines introduced by these commits. 3. Print these lines prefixed with "AIDER:". --- scripts/blame.py | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 scripts/blame.py diff --git a/scripts/blame.py b/scripts/blame.py new file mode 100644 index 000000000..6193fa966 --- /dev/null +++ b/scripts/blame.py @@ -0,0 +1,41 @@ +import subprocess + +def get_aider_commits(): + """Get commit hashes for commits with messages starting with 'aider:'""" + result = subprocess.run( + ["git", "log", "--pretty=format:%H %s"], + capture_output=True, + text=True, + check=True + ) + commits = [] + for line in result.stdout.splitlines(): + commit_hash, commit_message = line.split(" ", 1) + if commit_message.startswith("aider:"): + commits.append(commit_hash) + return commits + +def get_blame_lines(commit_hash): + """Get lines introduced by a specific commit""" + result = subprocess.run( + ["git", "blame", "--line-porcelain", commit_hash], + capture_output=True, + text=True, + check=True + ) + lines = [] + for line in result.stdout.splitlines(): + if line.startswith("author "): + lines.append(line) + return lines + +def mark_aider_lines(): + """Mark lines introduced by 'aider:' commits""" + aider_commits = get_aider_commits() + for commit in aider_commits: + blame_lines = get_blame_lines(commit) + for line in blame_lines: + print(f"AIDER: {line}") + +if __name__ == "__main__": + mark_aider_lines()