Merge branch 'jk/stash-options'
authorJunio C Hamano <gitster@pobox.com>
Mon, 1 Jun 2015 19:45:14 +0000 (12:45 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 1 Jun 2015 19:45:14 +0000 (12:45 -0700)
Make "git stash something --help" error out, so that users can
safely say "git stash drop --help".

* jk/stash-options:
stash: recognize "--help" for subcommands
stash: complain about unknown flags

1  2 
git-stash.sh
t/t3903-stash.sh
diff --combined git-stash.sh
index 7911f30c631fe58d6e5655f5213cae4e72ef99e1,23bd1d3db2a8097e901726add2476dd7090ee337..1f5ea877d719715760d42a3ee1d0950a1adaf9fc
@@@ -20,7 -20,7 +20,7 @@@ require_work_tre
  cd_to_toplevel
  
  TMP="$GIT_DIR/.git-stash.$$"
 -TMPindex=${GIT_INDEX_FILE-"$GIT_DIR/index"}.stash.$$
 +TMPindex=${GIT_INDEX_FILE-"$(git rev-parse --git-path index)"}.stash.$$
  trap 'rm -f "$TMP-"* "$TMPindex"' 0
  
  ref_stash=refs/stash
@@@ -184,7 -184,7 +184,7 @@@ store_stash () 
        fi
  
        # Make sure the reflog for stash is kept.
 -      : >>"$GIT_DIR/logs/$ref_stash"
 +      : >>"$(git rev-parse --git-path logs/$ref_stash)"
        git update-ref -m "$stash_msg" $ref_stash $w_commit
        ret=$?
        test $ret != 0 && test -z $quiet &&
@@@ -219,6 -219,9 +219,9 @@@ save_stash () 
                -a|--all)
                        untracked=all
                        ;;
+               --help)
+                       show_help
+                       ;;
                --)
                        shift
                        break
                say "$(gettext "No local changes to save")"
                exit 0
        fi
 -      test -f "$GIT_DIR/logs/$ref_stash" ||
 +      test -f "$(git rev-parse --git-path logs/$ref_stash)" ||
                clear_stash || die "$(gettext "Cannot initialize stash")"
  
        create_stash "$stash_msg" $untracked
@@@ -301,11 -304,17 +304,17 @@@ list_stash () 
  }
  
  show_stash () {
+       ALLOW_UNKNOWN_FLAGS=t
        assert_stash_like "$@"
  
        git diff ${FLAGS:---stat} $b_commit $w_commit
  }
  
+ show_help () {
+       exec git help stash
+       exit 1
+ }
  #
  # Parses the remaining options looking for flags and
  # at most one revision defaulting to ${ref_stash}@{0}
  #
  #   GIT_QUIET is set to t if -q is specified
  #   INDEX_OPTION is set to --index if --index is specified.
- #   FLAGS is set to the remaining flags
+ #   FLAGS is set to the remaining flags (if allowed)
  #
  # dies if:
  #   * too many revisions specified
  #   * no revision is specified and there is no stash stack
  #   * a revision is specified which cannot be resolve to a SHA1
  #   * a non-existent stash reference is specified
+ #   * unknown flags were set and ALLOW_UNKNOWN_FLAGS is not "t"
  #
  
  parse_flags_and_rev()
                        --index)
                                INDEX_OPTION=--index
                        ;;
+                       --help)
+                               show_help
+                       ;;
                        -*)
+                               test "$ALLOW_UNKNOWN_FLAGS" = t ||
+                                       die "$(eval_gettext "unknown option: \$opt")"
                                FLAGS="${FLAGS}${FLAGS:+ }$opt"
                        ;;
                esac
@@@ -442,8 -457,6 +457,8 @@@ apply_stash () 
        assert_stash_like "$@"
  
        git update-index -q --refresh || die "$(gettext "unable to refresh index")"
 +      git diff-index --cached --quiet --ignore-submodules HEAD -- ||
 +              die "$(gettext "Cannot apply stash: Your index contains uncommitted changes.")"
  
        # current index state
        c_tree=$(git write-tree) ||
diff --combined t/t3903-stash.sh
index 0746eeeff70c29c91accacdfa1efa4bc2e612715,384678953e81745c048f357cdb05d2322e88a7b2..7396ca991132a88012955a281a71c330eab2092b
@@@ -10,8 -10,6 +10,8 @@@ test_description='Test git stash
  test_expect_success 'stash some dirty working directory' '
        echo 1 > file &&
        git add file &&
 +      echo unrelated >other-file &&
 +      git add other-file &&
        test_tick &&
        git commit -m initial &&
        echo 2 > file &&
@@@ -45,15 -43,10 +45,15 @@@ test_expect_success 'applying bogus sta
        test_cmp expect file
  '
  
 +test_expect_success 'apply requires a clean index' '
 +      test_when_finished "git reset --hard" &&
 +      echo changed >other-file &&
 +      git add other-file &&
 +      test_must_fail git stash apply
 +'
 +
  test_expect_success 'apply does not need clean working directory' '
        echo 4 >other-file &&
 -      git add other-file &&
 -      echo 5 >other-file &&
        git stash apply &&
        echo 3 >expect &&
        test_cmp expect file
@@@ -100,6 -93,10 +100,10 @@@ test_expect_success 'unstashing in a su
        )
  '
  
+ test_expect_success 'stash drop complains of extra options' '
+       test_must_fail git stash drop --foo
+ '
  test_expect_success 'drop top stash' '
        git reset --hard &&
        git stash list > stashlist1 &&
@@@ -702,8 -699,8 +706,8 @@@ test_expect_success 'setup stash with i
  '
  
  test_expect_success 'stash list implies --first-parent -m' '
 -      cat >expect <<-\EOF &&
 -      stash@{0}: WIP on master: b27a2bc subdir
 +      cat >expect <<-EOF &&
 +      stash@{0}
  
        diff --git a/file b/file
        index 257cc56..d26b33d 100644
        -foo
        +working
        EOF
 -      git stash list -p >actual &&
 +      git stash list --format=%gd -p >actual &&
        test_cmp expect actual
  '
  
  test_expect_success 'stash list --cc shows combined diff' '
        cat >expect <<-\EOF &&
 -      stash@{0}: WIP on master: b27a2bc subdir
 +      stash@{0}
  
        diff --cc file
        index 257cc56,9015a7a..d26b33d
         -index
        ++working
        EOF
 -      git stash list -p --cc >actual &&
 +      git stash list --format=%gd -p --cc >actual &&
        test_cmp expect actual
  '