Merge branch 'lt/tree-2'
[git/git.git] / git-reset.sh
1 #!/bin/sh
2
3 USAGE='[--mixed | --soft | --hard] [<commit-ish>]'
4 . git-sh-setup
5
6 tmp=${GIT_DIR}/reset.$$
7 trap 'rm -f $tmp-*' 0 1 2 3 15
8
9 update=
10 reset_type=--mixed
11 case "$1" in
12 --mixed | --soft | --hard)
13 reset_type="$1"
14 shift
15 ;;
16 -*)
17 usage ;;
18 esac
19
20 rev=$(git-rev-parse --verify --default HEAD "$@") || exit
21 rev=$(git-rev-parse --verify $rev^0) || exit
22
23 # We need to remember the set of paths that _could_ be left
24 # behind before a hard reset, so that we can remove them.
25 if test "$reset_type" = "--hard"
26 then
27 update=-u
28 fi
29
30 # Soft reset does not touch the index file nor the working tree
31 # at all, but requires them in a good order. Other resets reset
32 # the index file to the tree object we are switching to.
33 if test "$reset_type" = "--soft"
34 then
35 if test -f "$GIT_DIR/MERGE_HEAD" ||
36 test "" != "$(git-ls-files --unmerged)"
37 then
38 die "Cannot do a soft reset in the middle of a merge."
39 fi
40 else
41 git-read-tree --reset $update "$rev" || exit
42 fi
43
44 # Any resets update HEAD to the head being switched to.
45 if orig=$(git-rev-parse --verify HEAD 2>/dev/null)
46 then
47 echo "$orig" >"$GIT_DIR/ORIG_HEAD"
48 else
49 rm -f "$GIT_DIR/ORIG_HEAD"
50 fi
51 git-update-ref -m "reset $reset_type $@" HEAD "$rev"
52
53 case "$reset_type" in
54 --hard )
55 ;; # Nothing else to do
56 --soft )
57 ;; # Nothing else to do
58 --mixed )
59 # Report what has not been updated.
60 git-update-index --refresh
61 ;;
62 esac
63
64 rm -f "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/rr-cache/MERGE_RR"