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