2023-05-03

An attempt at generating reasonable code. I used a scenario that had come up at work recently and that seemed to not require too much context.

ChatGPT came up with a naive solution first, and then when prompted for something a little more robust it ended up putting out a pile of over-complicated code that included an unnecessary checks for lines matching a certain string.


Me:

I need to make a script where I run git diff, and if the working directory is clean I want it to print an error message and exit with a non-zero exit code, but otherwise show the diff and exit with code zero. Can you help?

ChatGPT:

Certainly! Here's a possible implementation of the script in Bash: #!/bin/bash

# Run git diff and redirect its output to a variable diff_output=$(git diff)

# Check if the variable is empty (i.e., the working directory is clean) if [ -z "$diff_output" ]; then echo "Error: Working directory is clean." exit 1 fi

# Print the diff and exit with code zero echo "$diff_output" exit 0

Here's how it works:

You can save this code in a file, make it executable with chmod +x script.sh, and then run it with ./script.sh in the terminal.

Me:

I guess that works for small diffs, but when there's a really large diff it would be important to stream the diff out rather than capturing it.

ChatGPT:

You're right, streaming the diff output can be more efficient and prevent issues with capturing large diffs in memory. Here's a modified version of the script that streams the output:

#!/bin/bash

# Run `git diff` and pipe its output to a while loop
git diff | while read -r line; do
# Check if the line is not empty or starts with "diff --git"
if [ -n "$line" ] && [[ ! "$line" =~ ^diff\ --git.* ]]; then
  echo "$line"
  has_diff=true
fi
done

# Check if there was any diff output
if [ "$has_diff" = true ]; then
# Exit with code zero if there was a diff
exit 0
else
# Print an error message and exit with non-zero code if there was no diff
echo "Error: Working directory is clean."
exit 1
fi

Here's how it works:

You can save this code in a file, make it executable with chmod +x script.sh, and then run it with ./script.sh in the terminal.


This, more or less, is the code I was trying to get it to generate:

if git diff --exit-code; then
    echo "Error: No changes."
    exit 1
fi