Merge branch 'js/maint-apply-tab-in-indent-fix' into HEAD
authorJunio C Hamano <gitster@pobox.com>
Wed, 1 Dec 2010 22:42:00 +0000 (14:42 -0800)
committerJunio C Hamano <gitster@pobox.com>
Wed, 1 Dec 2010 22:42:00 +0000 (14:42 -0800)
* js/maint-apply-tab-in-indent-fix:
apply --whitespace=fix: fix tab-in-indent

1  2 
t/t4124-apply-ws-rule.sh
ws.c
diff --combined t/t4124-apply-ws-rule.sh
index 61bfc569c37eefa6dba4dd6f26513c61d71cfb9c,aea052346eab0d2a0b409fdb736279bcfc6b0af2..40e58429976a0145f4efcbcc24521e691df76753
@@@ -44,7 -44,7 +44,7 @@@ test_fix () 
        apply_patch --whitespace=fix || return 1
  
        # find touched lines
 -      diff file target | sed -n -e "s/^> //p" >fixed
 +      $DIFF file target | sed -n -e "s/^> //p" >fixed
  
        # the changed lines are all expeced to change
        fixed_cnt=$(wc -l <fixed)
@@@ -85,14 -85,14 +85,14 @@@ test_expect_success setup 
  test_expect_success 'whitespace=nowarn, default rule' '
  
        apply_patch --whitespace=nowarn &&
 -      diff file target
 +      test_cmp file target
  
  '
  
  test_expect_success 'whitespace=warn, default rule' '
  
        apply_patch --whitespace=warn &&
 -      diff file target
 +      test_cmp file target
  
  '
  
@@@ -108,7 -108,7 +108,7 @@@ test_expect_success 'whitespace=error-a
  
        git config core.whitespace -trailing,-space-before,-indent &&
        apply_patch --whitespace=error-all &&
 -      diff file target
 +      test_cmp file target
  
  '
  
@@@ -117,10 -117,38 +117,38 @@@ test_expect_success 'whitespace=error-a
        git config --unset core.whitespace &&
        echo "target -whitespace" >.gitattributes &&
        apply_patch --whitespace=error-all &&
 -      diff file target
 +      test_cmp file target
  
  '
  
+ test_expect_success 'spaces inserted by tab-in-indent' '
+       git config core.whitespace -trailing,-space,-indent,tab &&
+       rm -f .gitattributes &&
+       test_fix % &&
+       sed -e "s/_/ /g" -e "s/>/       /" <<-\EOF >expect &&
+               An_SP in an ordinary line>and a HT.
+               ________A HT (%).
+               ________A SP and a HT (@%).
+               _________A SP, a HT and a SP (@%).
+               _______Seven SP.
+               ________Eight SP (#).
+               ________Seven SP and a HT (@%).
+               ________________Eight SP and a HT (@#%).
+               _________Seven SP, a HT and a SP (@%).
+               _________________Eight SP, a HT and a SP (@#%).
+               _______________Fifteen SP (#).
+               ________________Fifteen SP and a HT (@#%).
+               ________________Sixteen SP (#).
+               ________________________Sixteen SP and a HT (@#%).
+               _____a__Five SP, a non WS, two SP.
+               A line with a (!) trailing SP_
+               A line with a (!) trailing HT>
+       EOF
+       test_cmp expect target
+ '
  for t in - ''
  do
        case "$t" in '') tt='!' ;; *) tt= ;; esac
@@@ -176,8 -204,9 +204,8 @@@ test_expect_success 'trailing whitespac
  '
  
  test_expect_success 'blank at EOF with --whitespace=fix (1)' '
 -      : these can fail depending on what we did before
 -      git config --unset core.whitespace
 -      rm -f .gitattributes
 +      test_might_fail git config --unset core.whitespace &&
 +      rm -f .gitattributes &&
  
        { echo a; echo b; echo c; } >one &&
        git add one &&
@@@ -329,18 -358,6 +357,18 @@@ test_expect_success 'two missing blank 
        test_cmp one expect
  '
  
 +test_expect_success 'missing blank line at end, insert before end, --whitespace=fix' '
 +      { echo a; echo; } >one &&
 +      git add one &&
 +      { echo b; echo a; echo; } >one &&
 +      cp one expect &&
 +      git diff -- one >patch &&
 +      echo a >one &&
 +      test_must_fail git apply patch &&
 +      git apply --whitespace=fix patch &&
 +      test_cmp one expect
 +'
 +
  test_expect_success 'shrink file with tons of missing blanks at end of file' '
        { echo a; echo b; echo c; } >one &&
        cp one no-blank-lines &&
@@@ -367,7 -384,7 +395,7 @@@ test_expect_success 'missing blanks at 
        git diff -- one >patch &&
  
        echo a >one &&
 -      test_must_fail git apply patch
 +      test_must_fail git apply patch &&
        test_must_fail git apply --whitespace=fix patch &&
        test_must_fail git apply --ignore-space-change --whitespace=fix patch
  '
@@@ -418,7 -435,7 +446,7 @@@ test_expect_success 'same, but with CR-
        printf "b\r\n" >>one &&
        printf "c\r\n" >>one &&
        cp one save-one &&
 -      printf "                 \r\n" >>one
 +      printf "                 \r\n" >>one &&
        git add one &&
        printf "d\r\n" >>one &&
        cp one expect &&
@@@ -435,7 -452,7 +463,7 @@@ test_expect_success 'same, but with CR-
        printf "b\r\n" >>one &&
        printf "c\r\n" >>one &&
        cp one save-one &&
 -      printf "                 \r\n" >>one
 +      printf "                 \r\n" >>one &&
        git add one &&
        cp one expect &&
        printf "d\r\n" >>one &&
diff --combined ws.c
index 7302f8f5a2cd450771aefcba0a385a90121ba0cf,b282e8c1005875232a46619c0e45f70a5a298e99..e3504360dab46231298eda0c3ffadfb69e79c609
--- 1/ws.c
--- 2/ws.c
+++ b/ws.c
@@@ -174,11 -174,8 +174,11 @@@ static unsigned ws_check_emit_1(const c
                }
        }
  
 +      if (trailing_whitespace == -1)
 +              trailing_whitespace = len;
 +
        /* Check indentation */
 -      for (i = 0; i < len; i++) {
 +      for (i = 0; i < trailing_whitespace; i++) {
                if (line[i] == ' ')
                        continue;
                if (line[i] != '\t')
                 * Now the rest of the line starts at "written".
                 * The non-highlighted part ends at "trailing_whitespace".
                 */
 -              if (trailing_whitespace == -1)
 -                      trailing_whitespace = len;
  
                /* Emit non-highlighted (middle) segment. */
                if (trailing_whitespace - written > 0) {
@@@ -363,12 -362,13 +363,13 @@@ void ws_fix_copy(struct strbuf *dst, co
                fixed = 1;
        } else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) {
                /* Expand tabs into spaces */
+               int start = dst->len;
                int last = last_tab_in_indent + 1;
                for (i = 0; i < last; i++) {
                        if (src[i] == '\t')
                                do {
                                        strbuf_addch(dst, ' ');
-                               } while (dst->len % 8);
+                               } while ((dst->len - start) % 8);
                        else
                                strbuf_addch(dst, src[i]);
                }