Linus Torvalds | 40d8cfe | 2005-06-14 18:56:05 -0700 | [diff] [blame] | 1 | #!/bin/sh |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 2 | . git-sh-setup || die "Not a git archive" |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 3 | |
| 4 | tmp=/var/tmp/reset.$$ |
| 5 | trap 'rm -f $tmp-*' 0 1 2 3 15 |
| 6 | |
| 7 | reset_type=--mixed |
| 8 | case "$1" in |
| 9 | --mixed | --soft | --hard) |
| 10 | reset_type="$1" |
| 11 | shift |
| 12 | ;; |
| 13 | esac |
| 14 | |
Junio C Hamano | 2ad77e6 | 2005-08-15 15:37:37 -0700 | [diff] [blame] | 15 | rev=$(git-rev-parse --verify --default HEAD "$@") || exit |
| 16 | rev=$(git-rev-parse --verify $rev^0) || exit |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 17 | |
| 18 | # We need to remember the set of paths that _could_ be left |
| 19 | # behind before a hard reset, so that we can remove them. |
| 20 | if test "$reset_type" = "--hard" |
| 21 | then |
| 22 | { |
| 23 | git-ls-files --stage -z |
| 24 | git-rev-parse --verify HEAD 2>/dev/null && |
| 25 | git-ls-tree -r -z HEAD |
| 26 | } | perl -e ' |
| 27 | use strict; |
| 28 | my %seen; |
| 29 | $/ = "\0"; |
| 30 | while (<>) { |
| 31 | chomp; |
| 32 | my ($info, $path) = split(/\t/, $_); |
| 33 | next if ($info =~ / tree /); |
| 34 | if (!$seen{$path}) { |
| 35 | $seen{$path} = 1; |
| 36 | print "$path\0"; |
| 37 | } |
| 38 | } |
| 39 | ' >$tmp-exists |
| 40 | fi |
| 41 | |
| 42 | # Soft reset does not touch the index file nor the working tree |
| 43 | # at all, but requires them in a good order. Other resets reset |
| 44 | # the index file to the tree object we are switching to. |
| 45 | if test "$reset_type" = "--soft" |
| 46 | then |
| 47 | if test -f "$GIT_DIR/MERGE_HEAD" || |
| 48 | test "" != "$(git-ls-files --unmerged)" |
Junio C Hamano | 32173e6 | 2005-08-06 20:59:47 -0700 | [diff] [blame] | 49 | then |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 50 | die "Cannot do a soft reset in the middle of a merge." |
Junio C Hamano | 32173e6 | 2005-08-06 20:59:47 -0700 | [diff] [blame] | 51 | fi |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 52 | else |
| 53 | git-read-tree --reset "$rev" || exit |
| 54 | fi |
| 55 | |
| 56 | # Any resets update HEAD to the head being switched to. |
| 57 | if orig=$(git-rev-parse --verify HEAD 2>/dev/null) |
| 58 | then |
| 59 | echo "$orig" >"$GIT_DIR/ORIG_HEAD" |
| 60 | else |
| 61 | rm -f "$GIT_DIR/ORIG_HEAD" |
| 62 | fi |
| 63 | echo "$rev" >"$GIT_DIR/HEAD" |
| 64 | |
| 65 | case "$reset_type" in |
| 66 | --hard ) |
| 67 | # Hard reset matches the working tree to that of the tree |
| 68 | # being switched to. |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 69 | git-checkout-index -f -u -q -a |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 70 | git-ls-files --cached -z | |
| 71 | perl -e ' |
| 72 | use strict; |
| 73 | my (%keep, $fh); |
| 74 | $/ = "\0"; |
| 75 | while (<STDIN>) { |
| 76 | chomp; |
| 77 | $keep{$_} = 1; |
| 78 | } |
| 79 | open $fh, "<", $ARGV[0] |
| 80 | or die "cannot open $ARGV[0]"; |
| 81 | while (<$fh>) { |
| 82 | chomp; |
| 83 | if (! exists $keep{$_}) { |
| 84 | print "$_\0"; |
| 85 | } |
| 86 | } |
Junio C Hamano | e8623d4 | 2005-09-08 23:14:33 -0700 | [diff] [blame] | 87 | ' $tmp-exists | xargs -0 rm -f -- |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 88 | ;; |
| 89 | --soft ) |
| 90 | ;; # Nothing else to do |
| 91 | --mixed ) |
| 92 | # Report what has not been updated. |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 93 | git-update-index --refresh |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 94 | ;; |
| 95 | esac |
| 96 | |
Linus Torvalds | ef0bfa2 | 2005-06-21 15:40:00 -0700 | [diff] [blame] | 97 | rm -f "$GIT_DIR/MERGE_HEAD" |