0

Is it possible to operate on git diff output in case I wanted to script around it and edit the actual new/updated line of code?

Say I have file 'foo' tracked by git

foo:

line 1
line 2
line 3
line 4

I edit it (adding '(edit)' to line 3) and

git diff foo

gives me

workspace@workspace:~/gitRepo/$ git diff foo
diff --git a/foo b/foo
index 9c2a709..30fb870 100644
--- a/foo
+++ b/foo
@@ -1,4 +1,4 @@
line 1
line 2
-line 3
+line 3 (edit)
line 4

And I want to be able to run git diff | scriptThatAddsBarToNewStrings which would edit foo such that

cat foo would render

line 1
line 2
line 3 (edit)bar
line 4

Is that something that can conceivably be done?

user2347638
  • 1,363
  • 1
  • 8
  • 7

2 Answers2

0

Yes it can. You can pipe the output of any git command to any shell command (grep, sort, etc...) and process it however you like. And there's nothing stopping you from passing this output as input to your shell scripts.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

This script will append a string to every modfied and added lines (speaks: lines starting with a +).

If you need any modifications, let me know.

#!/bin/sh
# Change this string to whatever you want appended to a modified line
appending="bar"


write_file() {
    echo -ne "$content" > "$file"
}

skip_start=true
content=""
while read line; do
    if $skip_start; then
        # Check for the "starting line"
        [[ "$line" == @@* ]] && skip_start=false

        # Look for the file name
        if [[ "$line" == +++* ]]; then
            file=$(echo "$line" | sed -e 's/^+++ b\/\(.*\)$/\1/')
        fi

        continue
    fi

    # Check if the diff for the current file ended
    if [[ "$line" == diff\ --git* ]]; then
        write_file

        # Reset variables
        skip_start=true
        content=""

        continue
    fi

    # Process the diff
    first_char=${line:0:1}
    if [[ "#$first_char#" == '# #' || "$first_char" == '-' ]]; then
        continue
    elif [[ "$first_char" == '+' ]]; then
        line="${line:1}$appending"
    fi

    # Prevent a newline at the beginning
    if [[ -n "$content" ]]; then
        content+="\n"
    fi
    content+="$line"
done

write_file
Sascha Wolf
  • 18,810
  • 4
  • 51
  • 73
  • Sorry, I'm a complete noob. Naming the script 'append' and running `git diff | sh append` gave me `workspace@workspace:~/Desktop/foo$ git diff | sh append append: 15: append: [[: not found append: 18: append: [[: not found append: 15: append: [[: not found append: 18: append: [[: not found append: 15: append: [[: not found append: 18: append: [[: not found append: 15: append: [[: not found append: 18: append: [[: not found append: 15: append: [[: not found append: 18: append: [[: not found append: 15: append: [[: not found append: 18: append: [[: not found` any ideas? – user2347638 Jul 18 '14 at 18:10
  • @user2347638 On which system are you using the script? And in which environment? `[[` is a bash specific command. – Sascha Wolf Jul 18 '14 at 18:15
  • I'm running Ubuntu and using a GNOME shell. Should I try it out on a mac? Thanks for the help here btw--much appreciated – user2347638 Jul 18 '14 at 18:22
  • worked perfectly on a mac--I'll just do a bit of research on Mac vs Ubuntu. Thanks a ton! – user2347638 Jul 18 '14 at 18:26
  • Actually, I had to remove the -ne option on line 7 otherwise it was prepending '-ne' on line 1 `-ne line 1`. Other than that it worked like a charm! – user2347638 Jul 18 '14 at 18:39