@matheusbsbs / matheus-bagliano / commits / c68998f5b5
Fork 0

Simplify "git reset --hard"

Now that the one-way merge strategy does the right thing wrt files that do not exist in the result, just remove all the random crud we did in "git reset" to do this all by hand. Instead, just pass in "-u" to git-read-tree when we do a hard reset, and depend on git-read-tree to update the working tree appropriately. This basically means that git reset turns into # Always update the HEAD ref git update-ref HEAD "$rev" case "--soft" # do nothing to index/working tree case "--hard" # read index _and_ update working tree git-read-tree --reset -u "$rev" case "--mixed" # update just index, report on working tree differences git-read-tree --reset "$rev" git-update-index --refresh which is what it was always semantically doing, it just did it in a rather strange way because it was written to not expect git-read-tree to do anything to the working tree. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed May 14, 2006 at 11:20 UTC c68998f5b5f43717a27da82fac08a76d6588bae7
1 file changed +4 -46
git-reset.sh
+4 -46
@@ -6,6 +6,7 @@ USAGE='[--mixed | --soft | --hard] [<commit-ish>]'
6 tmp=${GIT_DIR}/reset.$$
7 trap 'rm -f $tmp-*' 0 1 2 3 15
8
9 +update=
10 reset_type=--mixed
11 case "$1" in
12 --mixed | --soft | --hard)
@@ -23,24 +24,7 @@ rev=$(git-rev-parse --verify $rev^0) || exit
24 # behind before a hard reset, so that we can remove them.
25 if test "$reset_type" = "--hard"
26 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
27 + update=-u
28 fi
29
30 # Soft reset does not touch the index file nor the working tree
@@ -54,7 +38,7 @@ then
38 die "Cannot do a soft reset in the middle of a merge."
39 fi
40 else
57 - git-read-tree --reset "$rev" || exit
41 + git-read-tree --reset $update "$rev" || exit
42 fi
43
44 # Any resets update HEAD to the head being switched to.
@@ -68,33 +52,7 @@ git-update-ref HEAD "$rev"
52
53 case "$reset_type" in
54 --hard )
71 - # Hard reset matches the working tree to that of the tree
72 - # being switched to.
73 - git-checkout-index -f -u -q -a
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{$_}) {
88 - # it is ok if this fails -- it may already
89 - # have been culled by checkout-index.
90 - unlink $_;
91 - while (s|/[^/]*$||) {
92 - rmdir($_) or last;
93 - }
94 - }
95 - }
96 - ' $tmp-exists
97 - ;;
55 + ;; # Nothing else to do
56 --soft )
57 ;; # Nothing else to do
58 --mixed )