index-pack: write-or-die instead of unchecked write-in-full.
[git/gitweb.git] / git-reset.sh
blob76c8a818d421c796141fae3835e1cb4726d23ef3
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 $*"
10 update= reset_type=--mixed
11 unset rev
13 while case $# in 0) break ;; esac
15 case "$1" in
16 --mixed | --soft | --hard)
17 reset_type="$1"
19 --)
20 break
22 -*)
23 usage
26 rev=$(git-rev-parse --verify "$1") || exit
27 shift
28 break
30 esac
31 shift
32 done
34 : ${rev=HEAD}
35 rev=$(git-rev-parse --verify $rev^0) || exit
37 # Skip -- in "git reset HEAD -- foo" and "git reset -- foo".
38 case "$1" in --) shift ;; esac
40 # git reset --mixed tree [--] paths... can be used to
41 # load chosen paths from the tree into the index without
42 # affecting the working tree nor HEAD.
43 if test $# != 0
44 then
45 test "$reset_type" == "--mixed" ||
46 die "Cannot do partial $reset_type reset."
48 git-diff-index --cached $rev -- "$@" |
49 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/' |
50 git update-index --add --remove --index-info || exit
51 git update-index --refresh
52 exit
55 TOP=$(git-rev-parse --show-cdup)
56 if test ! -z "$TOP"
57 then
58 cd "$TOP"
61 if test "$reset_type" = "--hard"
62 then
63 update=-u
66 # Soft reset does not touch the index file nor the working tree
67 # at all, but requires them in a good order. Other resets reset
68 # the index file to the tree object we are switching to.
69 if test "$reset_type" = "--soft"
70 then
71 if test -f "$GIT_DIR/MERGE_HEAD" ||
72 test "" != "$(git-ls-files --unmerged)"
73 then
74 die "Cannot do a soft reset in the middle of a merge."
76 else
77 git-read-tree --reset $update "$rev" || exit
80 # Any resets update HEAD to the head being switched to.
81 if orig=$(git-rev-parse --verify HEAD 2>/dev/null)
82 then
83 echo "$orig" >"$GIT_DIR/ORIG_HEAD"
84 else
85 rm -f "$GIT_DIR/ORIG_HEAD"
87 git-update-ref -m "$GIT_REFLOG_ACTION" HEAD "$rev"
88 update_ref_status=$?
90 case "$reset_type" in
91 --hard )
92 test $update_ref_status = 0 && {
93 echo -n "HEAD is now at "
94 GIT_PAGER= git log --max-count=1 --pretty=oneline \
95 --abbrev-commit HEAD
98 --soft )
99 ;; # Nothing else to do
100 --mixed )
101 # Report what has not been updated.
102 git-update-index --refresh
104 esac
106 rm -f "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/rr-cache/MERGE_RR" \
107 "$GIT_DIR/SQUASH_MSG" "$GIT_DIR/MERGE_MSG"
109 exit $update_ref_status