Linus Torvalds | 40d8cfe | 2005-06-14 18:56:05 -0700 | [diff] [blame] | 1 | #!/bin/sh |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 2 | |
freku045@student.liu.se | 806f36d | 2005-12-13 23:30:31 +0100 | [diff] [blame] | 3 | USAGE='[--mixed | --soft | --hard] [<commit-ish>]' |
| 4 | . git-sh-setup |
c.shoemaker@cox.net | 2f9d685 | 2005-10-29 00:16:20 -0400 | [diff] [blame] | 5 | |
Alex Riesen | 6ff0b1c | 2006-01-05 12:52:07 +0100 | [diff] [blame] | 6 | tmp=${GIT_DIR}/reset.$$ |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 7 | trap 'rm -f $tmp-*' 0 1 2 3 15 |
| 8 | |
| 9 | reset_type=--mixed |
| 10 | case "$1" in |
| 11 | --mixed | --soft | --hard) |
| 12 | reset_type="$1" |
| 13 | shift |
| 14 | ;; |
c.shoemaker@cox.net | 2f9d685 | 2005-10-29 00:16:20 -0400 | [diff] [blame] | 15 | -*) |
| 16 | usage ;; |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 17 | esac |
| 18 | |
Junio C Hamano | 2ad77e6 | 2005-08-15 15:37:37 -0700 | [diff] [blame] | 19 | rev=$(git-rev-parse --verify --default HEAD "$@") || exit |
| 20 | rev=$(git-rev-parse --verify $rev^0) || exit |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 21 | |
| 22 | # We need to remember the set of paths that _could_ be left |
| 23 | # behind before a hard reset, so that we can remove them. |
| 24 | if test "$reset_type" = "--hard" |
| 25 | then |
| 26 | { |
| 27 | git-ls-files --stage -z |
| 28 | git-rev-parse --verify HEAD 2>/dev/null && |
| 29 | git-ls-tree -r -z HEAD |
| 30 | } | perl -e ' |
| 31 | use strict; |
| 32 | my %seen; |
| 33 | $/ = "\0"; |
| 34 | while (<>) { |
| 35 | chomp; |
| 36 | my ($info, $path) = split(/\t/, $_); |
| 37 | next if ($info =~ / tree /); |
| 38 | if (!$seen{$path}) { |
| 39 | $seen{$path} = 1; |
| 40 | print "$path\0"; |
| 41 | } |
| 42 | } |
| 43 | ' >$tmp-exists |
| 44 | fi |
| 45 | |
| 46 | # Soft reset does not touch the index file nor the working tree |
| 47 | # at all, but requires them in a good order. Other resets reset |
| 48 | # the index file to the tree object we are switching to. |
| 49 | if test "$reset_type" = "--soft" |
| 50 | then |
| 51 | if test -f "$GIT_DIR/MERGE_HEAD" || |
| 52 | test "" != "$(git-ls-files --unmerged)" |
Junio C Hamano | 32173e6 | 2005-08-06 20:59:47 -0700 | [diff] [blame] | 53 | then |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 54 | 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] | 55 | fi |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 56 | else |
| 57 | git-read-tree --reset "$rev" || exit |
| 58 | fi |
| 59 | |
| 60 | # Any resets update HEAD to the head being switched to. |
| 61 | if orig=$(git-rev-parse --verify HEAD 2>/dev/null) |
| 62 | then |
| 63 | echo "$orig" >"$GIT_DIR/ORIG_HEAD" |
| 64 | else |
| 65 | rm -f "$GIT_DIR/ORIG_HEAD" |
| 66 | fi |
Junio C Hamano | bf7960e | 2005-09-27 18:14:27 -0700 | [diff] [blame] | 67 | git-update-ref HEAD "$rev" |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 68 | |
| 69 | case "$reset_type" in |
| 70 | --hard ) |
| 71 | # Hard reset matches the working tree to that of the tree |
| 72 | # being switched to. |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 73 | git-checkout-index -f -u -q -a |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 74 | git-ls-files --cached -z | |
| 75 | perl -e ' |
| 76 | use strict; |
| 77 | my (%keep, $fh); |
| 78 | $/ = "\0"; |
| 79 | while (<STDIN>) { |
| 80 | chomp; |
| 81 | $keep{$_} = 1; |
| 82 | } |
| 83 | open $fh, "<", $ARGV[0] |
| 84 | or die "cannot open $ARGV[0]"; |
| 85 | while (<$fh>) { |
| 86 | chomp; |
| 87 | if (! exists $keep{$_}) { |
Junio C Hamano | c3bc895 | 2005-09-24 15:02:35 -0700 | [diff] [blame] | 88 | # it is ok if this fails -- it may already |
| 89 | # have been culled by checkout-index. |
| 90 | unlink $_; |
Shawn Pearce | 772d8a3 | 2006-02-17 02:26:16 -0500 | [diff] [blame] | 91 | while (s|/[^/]*$||) { |
| 92 | rmdir($_) or last; |
| 93 | } |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
Junio C Hamano | c3bc895 | 2005-09-24 15:02:35 -0700 | [diff] [blame] | 96 | ' $tmp-exists |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 97 | ;; |
| 98 | --soft ) |
| 99 | ;; # Nothing else to do |
| 100 | --mixed ) |
| 101 | # Report what has not been updated. |
Junio C Hamano | 215a7ad | 2005-09-07 17:26:23 -0700 | [diff] [blame] | 102 | git-update-index --refresh |
Junio C Hamano | 45d197a | 2005-08-22 17:55:26 -0700 | [diff] [blame] | 103 | ;; |
| 104 | esac |
| 105 | |
Junio C Hamano | 8389b52 | 2006-01-28 23:15:24 -0800 | [diff] [blame] | 106 | rm -f "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/rr-cache/MERGE_RR" |