blob: cb1bbf3b90e911ac5fea09e00d6e0189b8ee7104 [file] [log] [blame]
Linus Torvalds40d8cfe2005-06-14 18:56:05 -07001#!/bin/sh
Junio C Hamano2ce633b2006-12-14 01:19:19 -08002#
3# Copyright (c) 2005, 2006 Linus Torvalds and Junio C Hamano
4#
5USAGE='[--mixed | --soft | --hard] [<commit-ish>] [ [--] <paths>...]'
Junio C Hamanoa81c3112006-12-14 00:40:15 -08006SUBDIRECTORY_OK=Yes
freku045@student.liu.se806f36d2005-12-13 23:30:31 +01007. git-sh-setup
Shawn O. Pearce42ea5a52006-12-28 02:34:52 -05008set_reflog_action "reset $*"
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -05009require_work_tree
c.shoemaker@cox.net2f9d6852005-10-29 00:16:20 -040010
Junio C Hamano2ce633b2006-12-14 01:19:19 -080011update= reset_type=--mixed
12unset rev
13
David Kastrup822f7c72007-09-23 22:42:08 +020014while test $# != 0
Junio C Hamano2ce633b2006-12-14 01:19:19 -080015do
16 case "$1" in
17 --mixed | --soft | --hard)
18 reset_type="$1"
19 ;;
20 --)
21 break
22 ;;
23 -*)
24 usage
25 ;;
26 *)
Junio C Hamano5be60072007-07-02 22:52:14 -070027 rev=$(git rev-parse --verify "$1") || exit
Junio C Hamano2ce633b2006-12-14 01:19:19 -080028 shift
29 break
30 ;;
31 esac
32 shift
33done
34
35: ${rev=HEAD}
Junio C Hamano5be60072007-07-02 22:52:14 -070036rev=$(git rev-parse --verify $rev^0) || exit
Junio C Hamano2ce633b2006-12-14 01:19:19 -080037
38# Skip -- in "git reset HEAD -- foo" and "git reset -- foo".
39case "$1" in --) shift ;; esac
40
41# git reset --mixed tree [--] paths... can be used to
42# load chosen paths from the tree into the index without
Justin Lebar01689902014-03-31 15:11:46 -070043# affecting the working tree or HEAD.
Junio C Hamano2ce633b2006-12-14 01:19:19 -080044if test $# != 0
45then
David Kågedala5cd09f2007-01-18 12:15:13 +010046 test "$reset_type" = "--mixed" ||
Junio C Hamano2ce633b2006-12-14 01:19:19 -080047 die "Cannot do partial $reset_type reset."
Junio C Hamanobc8c0292007-01-05 01:38:56 -080048
Junio C Hamano5be60072007-07-02 22:52:14 -070049 git diff-index --cached $rev -- "$@" |
Junio C Hamanobc8c0292007-01-05 01:38:56 -080050 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
Junio C Hamano2ce633b2006-12-14 01:19:19 -080052 git update-index --refresh
53 exit
54fi
55
Junio C Hamano514c09f2007-01-12 12:49:05 -080056cd_to_toplevel
Junio C Hamanoa81c3112006-12-14 00:40:15 -080057
Junio C Hamano45d197a2005-08-22 17:55:26 -070058if test "$reset_type" = "--hard"
59then
Linus Torvaldsc68998f2006-05-14 11:20:37 -070060 update=-u
Junio C Hamano45d197a2005-08-22 17:55:26 -070061fi
62
Justin Lebar01689902014-03-31 15:11:46 -070063# Soft reset does not touch the index file or the working tree
Junio C Hamano45d197a2005-08-22 17:55:26 -070064# at all, but requires them in a good order. Other resets reset
65# the index file to the tree object we are switching to.
66if test "$reset_type" = "--soft"
67then
68 if test -f "$GIT_DIR/MERGE_HEAD" ||
Junio C Hamano5be60072007-07-02 22:52:14 -070069 test "" != "$(git ls-files --unmerged)"
Junio C Hamano32173e62005-08-06 20:59:47 -070070 then
Junio C Hamano45d197a2005-08-22 17:55:26 -070071 die "Cannot do a soft reset in the middle of a merge."
Junio C Hamano32173e62005-08-06 20:59:47 -070072 fi
Junio C Hamano45d197a2005-08-22 17:55:26 -070073else
Junio C Hamano5be60072007-07-02 22:52:14 -070074 git read-tree -v --reset $update "$rev" || exit
Junio C Hamano45d197a2005-08-22 17:55:26 -070075fi
76
77# Any resets update HEAD to the head being switched to.
Junio C Hamano5be60072007-07-02 22:52:14 -070078if orig=$(git rev-parse --verify HEAD 2>/dev/null)
Junio C Hamano45d197a2005-08-22 17:55:26 -070079then
80 echo "$orig" >"$GIT_DIR/ORIG_HEAD"
81else
82 rm -f "$GIT_DIR/ORIG_HEAD"
83fi
Junio C Hamano5be60072007-07-02 22:52:14 -070084git update-ref -m "$GIT_REFLOG_ACTION" HEAD "$rev"
Junio C Hamanodee4e382006-07-27 22:27:44 -070085update_ref_status=$?
Junio C Hamano45d197a2005-08-22 17:55:26 -070086
87case "$reset_type" in
88--hard )
Johannes Schindelin95f2fb72006-12-21 15:26:35 +010089 test $update_ref_status = 0 && {
Jason Riedy2aad9572007-01-15 17:31:29 -080090 printf "HEAD is now at "
Johannes Schindelin95f2fb72006-12-21 15:26:35 +010091 GIT_PAGER= git log --max-count=1 --pretty=oneline \
92 --abbrev-commit HEAD
93 }
94 ;;
Junio C Hamano45d197a2005-08-22 17:55:26 -070095--soft )
96 ;; # Nothing else to do
97--mixed )
98 # Report what has not been updated.
Junio C Hamano5be60072007-07-02 22:52:14 -070099 git update-index --refresh
Junio C Hamano45d197a2005-08-22 17:55:26 -0700100 ;;
101esac
102
Junio C Hamano49ed2bc2006-12-04 19:44:40 -0800103rm -f "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/rr-cache/MERGE_RR" \
104 "$GIT_DIR/SQUASH_MSG" "$GIT_DIR/MERGE_MSG"
Junio C Hamanodee4e382006-07-27 22:27:44 -0700105
106exit $update_ref_status