Hacker News new | ask | show | jobs
by MicKillah 909 days ago
I am not sure how you may have gone about it but I was able to get this script, from ChatGPT4:

  #!/bin/bash
# Script to convert git diff output to a searchable format

# Check if a git repository if [ ! -d .git ]; then echo "This directory is not a git repository." exit 1 fi

# Filename for the output output_file="git_diff_searchable.txt"

# Empty the output file or create it if it doesn't exist > "$output_file"

# Process git diff output git diff --unified=0 | while read line; do # Check for filename line if [[ $line =~ ^diff ]]; then filename=$(echo $line | sed 's/diff --git a\/\(.\) b\/./\1/') elif [[ $line =~ ^@@ ]]; then # Extract line numbers line_numbers=$(echo $line | sed -E 's/@@ -[0-9]+(,[0-9]+)? \+([0-9]+)(,[0-9]+)? @@./\2/') else # Write filename and line number to the output file echo "$filename:$line_numbers: $line" >> "$output_file" fi done

echo "Output saved to $output_file"

I then ran the following egrep [corrected to egrep, after mistakenly putting that I used gawk] command egrep -e 'agent.rs:[0-9]{1,}' git_diff_searchable.txt* to see the results. Everything worked as I expected.

Now, I don't claim that this is what you intended to achieve but I prompted it with the context of what you asked: Write a script that converts git-diff output to a file that can be easily grepped by filename and linenumber.

1 comments

However, I did do some prompt "engineering" alongside using your literal request. I definitely should make it clear that I didn't only use your request verbatim but I augmented it a bit with some additional prompting cues.
I'm not sure how to read your comment as everything in the script seems to be a comment? Probably format messed up.

What it usually fails at (maybe 4 can do it, I only use 3.5) is that the output should have one line per line-number, so nothing like 9-15 but 9,10,11,12 etc. I made this very explicit, I gave also examples on how the output should look like. Nothing helped.

Also, I explicitly set it should work under macOS but there were many syntax errors or uses of grep that are incompatible with macOS. So maybe part of it would have worked with linux, not sure. If you can maybe reformat, I could check