Merge branch 'rs/archive'
[git/builtin-gsoc.git] / git-reset.sh
blob1dc606fbd3fd29f5f4c9928a2518df46109ef449
1 #!/bin/sh
3 # Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
5 USAGE='[--mixed | --soft | --hard] [<commit-ish>] [ [--] <paths>...]'
6 SUBDIRECTORY_OK=Yes
7 . git-sh-setup
8 set_reflog_action "reset $*"
9 require_work_tree
11 update= reset_type=--mixed
12 unset rev
14 while case $# in 0) break ;; esac
16 case "$1" in
17 --mixed | --soft | --hard)
18 reset_type="$1"
20 --)
21 break
23 -*)
24 usage
27 rev=$(git rev-parse --verify "$1") || exit
28 shift
29 break
31 esac
32 shift
33 done
35 : ${rev=HEAD}
36 rev=$(git rev-parse --verify $rev^0) || exit
38 # Skip -- in "git reset HEAD -- foo" and "git reset -- foo".
39 case "$1" in --) shift ;; esac
41 # git reset --mixed tree [--] paths... can be used to
42 # load chosen paths from the tree into the index without
43 # affecting the working tree nor HEAD.
44 if test $# != 0
45 then
46 test "$reset_type" = "--mixed" ||
47 die "Cannot do partial $reset_type reset."
49 git diff-index --cached $rev -- "$@" |
50 sed -e 's/^:\([0-7][0-7]*\) [0-7][0-7]* \([0-9a-f][0-9a-f]*\) [0-9a-f][0-9a-f]* [A-Z] \(.*\)$/\1 \2 \3/' |
51 git update-index --add --remove --index-info || exit
52 git update-index --refresh
53 exit
56 cd_to_toplevel
58 if test "$reset_type" = "--hard"
59 then
60 update=-u
63 # Soft reset does not touch the index file nor the working tree
64 # at all, but requires them in a good order. Other resets reset
65 # the index file to the tree object we are switching to.
66 if test "$reset_type" = "--soft"
67 then
68 if test -f "$GIT_DIR/MERGE_HEAD" ||
69 test "" != "$(git ls-files --unmerged)"
70 then
71 die "Cannot do a soft reset in the middle of a merge."
73 else
74 git read-tree -v --reset $update "$rev" || exit
77 # Any resets update HEAD to the head being switched to.
78 if orig=$(git rev-parse --verify HEAD 2>/dev/null)
79 then
80 echo "$orig" >"$GIT_DIR/ORIG_HEAD"
81 else
82 rm -f "$GIT_DIR/ORIG_HEAD"
84 git update-ref -m "$GIT_REFLOG_ACTION" HEAD "$rev"
85 update_ref_status=$?
87 case "$reset_type" in
88 --hard )
89 test $update_ref_status = 0 && {
90 printf "HEAD is now at "
91 GIT_PAGER= git log --max-count=1 --pretty=oneline \
92 --abbrev-commit HEAD
95 --soft )
96 ;; # Nothing else to do
97 --mixed )
98 # Report what has not been updated.
99 git update-index --refresh
101 esac
103 rm -f "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/rr-cache/MERGE_RR" \
104 "$GIT_DIR/SQUASH_MSG" "$GIT_DIR/MERGE_MSG"
106 exit $update_ref_status