quilt-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Quilt-dev] [PATCH 2/3] Add support for generating git-style diffs


From: Michal Marek
Subject: [Quilt-dev] [PATCH 2/3] Add support for generating git-style diffs
Date: Mon, 1 Jun 2015 13:08:46 +0200

Add a --git option to diff and refresh, which adds the diff --git and
index lines like git diff does. The use case to minimize noise when
refreshing patches that have been originally generated by git. There are
a few limitations though:

- Mode changes are only noticed when the content also changes. And we
  ignore the 'new mode ...' lines when applying patches.
- The blob hashes have a hardcoded length of 7 digit, whereas this is
  configurable in git. git also adds digits as needed to make the
  abbreviation unambiguous in the current state of the repository.
- The diff algorithm used by git and diff(1) might give different but
  equivalent results.
- diff(1) trims the -p function name, whereas git does not.

Signed-off-by: Michal Marek <address@hidden>
---
 quilt/diff.in             | 12 ++++++++++--
 quilt/refresh.in          | 11 +++++++++--
 quilt/scripts/patchfns.in | 49 +++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 66 insertions(+), 6 deletions(-)

diff --git a/quilt/diff.in b/quilt/diff.in
index dc021f0..94586fc 100644
--- a/quilt/diff.in
+++ b/quilt/diff.in
@@ -21,7 +21,7 @@ setup_colors
 
 usage()
 {
-       printf $"Usage: quilt diff [-p n|-p ab] [-u|-U num|-c|-C num] 
[--combine patch|-z] [-R] [-P patch] [--snapshot] [--diff=utility] 
[--no-timestamps] [--no-index] [--sort] [--color[=always|auto|never]] [file 
...]\n"
+       printf $"Usage: quilt diff [-p n|-p ab] [-u|-U num|-c|-C num] 
[--combine patch|-z] [-R] [-P patch] [--snapshot] [--diff=utility] 
[--no-timestamps] [--no-index] [--sort] [--color[=always|auto|never]] [--git] 
[file ...]\n"
 
        if [ x$1 = x-h ]
        then
@@ -72,6 +72,8 @@ included.
        Use syntax coloring (auto activates it only if the output is a tty).
 
 --sort Sort files by their name instead of preserving the original order.
+
+--git  Generate a git-style diff. Implies --no-index --no-timestamps -p ab.
 "
                exit 0
        else
@@ -140,7 +142,7 @@ die()
 
 options=`getopt -o p:P:RuU:cC:zh --long diff:,snapshot,no-timestamps \
                                 --long no-index,combine:,color:: \
-                                --long sort -- "$@"`
+                                --long sort,git -- "$@"`
 
 if [ $? -ne 0 ]
 then
@@ -211,6 +213,12 @@ do
                        usage ;;
                esac
                shift 2 ;;
+       --git)
+               QUILT_GIT_DIFF=1
+               QUILT_NO_DIFF_INDEX=1
+               QUILT_NO_DIFF_TIMESTAMPS=1
+               opt_strip_level=ab
+               shift ;;
        --)
                shift
                break ;;
diff --git a/quilt/refresh.in b/quilt/refresh.in
index a10659f..3446764 100644
--- a/quilt/refresh.in
+++ b/quilt/refresh.in
@@ -19,7 +19,7 @@ fi
 
 usage()
 {
-       printf $"Usage: quilt refresh [-p n|-p ab] [-u|-U num|-c|-C num] 
[-z[new_name]] [-f] [--no-timestamps] [--no-index] [--diffstat] [--sort] 
[--backup] [--strip-trailing-whitespace] [patch]\n"
+       printf $"Usage: quilt refresh [-p n|-p ab] [-u|-U num|-c|-C num] 
[-z[new_name]] [-f] [--no-timestamps] [--no-index] [--diffstat] [--sort] 
[--backup] [--strip-trailing-whitespace] [--git] [patch]\n"
 
        if [ x$1 = x-h ]
        then
@@ -71,6 +71,7 @@ patch.
 
 --strip-trailing-whitespace
        Strip trailing whitespace at the end of lines.
+--git  Generate a git-style diff. Implies --no-index --no-timestamps -p ab.
 "
                exit 0
        else
@@ -89,7 +90,7 @@ die()
 
 options=`getopt -o p:uU:cC:fz::h --long no-timestamps,diffstat,backup,sort \
                                 --long no-index \
-                                --long strip-trailing-whitespace -- "$@"`
+                                --long strip-trailing-whitespace,git -- "$@"`
 
 if [ $? -ne 0 ]
 then
@@ -138,6 +139,12 @@ do
        --strip-trailing-whitespace)
                opt_strip_whitespace=1
                shift ;;
+       --git)
+               QUILT_GIT_DIFF=1
+               QUILT_NO_DIFF_INDEX=1
+               QUILT_NO_DIFF_TIMESTAMPS=1
+               opt_strip_level=ab
+               shift ;;
        --)
                shift
                break ;;
diff --git a/quilt/scripts/patchfns.in b/quilt/scripts/patchfns.in
index ab4e725..513d942 100644
--- a/quilt/scripts/patchfns.in
+++ b/quilt/scripts/patchfns.in
@@ -690,10 +690,23 @@ files_in_patch_ordered()
        '
 }
 
+git_hash()
+{
+       local file=$1
+
+       if [ -s "$file" ]; then
+               git hash-object "$file" | sed -r 's/^(.{7}).*/\1/'
+               return ${PIPESTATUS[0]}
+       else
+               echo "0000000"
+       fi
+}
+
 diff_file()
 {
        local file=$1 old_file=$2 new_file=$3
-       local index old_hdr old_date new_hdr new_date line
+       local index old_hdr old_date old_hash new_hdr new_date new_hash line
+       local old_mode new_mode new_or_deleted
 
        : ${opt_strip_level:=1}
        if [ $opt_strip_level = ab ]
@@ -711,22 +724,41 @@ diff_file()
        fi
        index=$new_hdr
 
+       if [ -n "$QUILT_GIT_DIFF" ]; then
+               old_hash=$(git_hash "$old_file") && \
+                       new_hash=$(git_hash "$new_file")
+               if [ $? -ne 0 ]; then
+                       echo "warning: git not available, disabling --git" >&2
+                       QUILT_GIT_DIFF=
+               fi
+       fi
        if [ -s "$old_file" ]
        then
                [ -n "$QUILT_NO_DIFF_TIMESTAMPS" ] \
                || old_date=$'\t'$(date +'%Y-%m-%d %H:%M:%S.%N %z' \
                                        -r "$old_file")
+               if [ -n "$QUILT_GIT_DIFF" -a -x "$old_file" ]; then
+                       old_mode=100755
+               else
+                       old_mode=100644
+               fi
        else
                old_file=/dev/null
                old_hdr=/dev/null
                [ -n "$QUILT_NO_DIFF_TIMESTAMPS" ] \
                || old_date=$'\t'"1970-01-01 00:00:00.000000000 +0000"
+               new_or_deleted="new"
        fi
        if [ -e "$new_file" ]
        then
                [ -n "$QUILT_NO_DIFF_TIMESTAMPS" ] \
                || new_date=$'\t'$(date +'%Y-%m-%d %H:%M:%S.%N %z' \
                                        -r "$new_file")
+               if [ -n "$QUILT_GIT_DIFF" -a -x "$new_file" ]; then
+                       new_mode=100755
+               else
+                       new_mode=100644
+               fi
        else
                [ $opt_strip_level = 0 ] \
                && old_hdr=$new_hdr
@@ -734,6 +766,7 @@ diff_file()
                new_hdr=/dev/null
                [ -n "$QUILT_NO_DIFF_TIMESTAMPS" ] \
                || new_date=$'\t'"1970-01-01 00:00:00.000000000 +0000"
+               new_or_deleted="deleted"
        fi
 
        diff $QUILT_DIFF_OPTS \
@@ -741,7 +774,19 @@ diff_file()
             "$old_file" "$new_file" \
        | if read line
        then
-               if [ -z "$QUILT_NO_DIFF_INDEX" ]
+               if [ -n "$QUILT_GIT_DIFF" ]; then
+                       echo "diff --git a/$file b/$file"
+                       if [ -n "$new_or_deleted" ]; then
+                               echo "$new_or_deleted file mode 
$old_mode$new_mode"
+                               echo "index $old_hash..$new_hash"
+                       elif [ "$old_mode" != "$new_mode" ]; then
+                               echo "old mode $old_mode"
+                               echo "new mode $new_mode"
+                               echo "index $old_hash..$new_hash"
+                       else
+                               echo "index $old_hash..$new_hash $new_mode"
+                       fi
+               elif [ -z "$QUILT_NO_DIFF_INDEX" ]
                then
                        echo "Index: $index"
                        echo 
"==================================================================="
-- 
2.1.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]