git-read-tree: add "--reset" flag
authorLinus Torvalds <torvalds@ppc970.osdl.org>
Thu, 9 Jun 2005 19:51:01 +0000 (12:51 -0700)
committerLinus Torvalds <torvalds@ppc970.osdl.org>
Thu, 9 Jun 2005 19:51:01 +0000 (12:51 -0700)
This is the same as "-m", but it will silently ignore any unmerged
entries, which makes it useful for efficiently forcing a new position
regardless of the state of the current index file.

IOW, to reset to a previous HEAD (in case you have had a failed
merge, for example), you'd just do

git-read-tree -u --reset HEAD

which will also update your working tree to the right state.

NOTE! The "update" will not remove files that may have been added by the
merge.  Yet.

read-tree.c

index fbd0da0..b3a3c4a 100644 (file)
@@ -275,13 +275,35 @@ static void merge_cache(struct cache_entry **src, int nr, merge_fn_t fn)
        check_updates(active_cache, active_nr);
 }
 
+static int read_cache_unmerged(void)
+{
+       int i, deleted;
+       struct cache_entry **dst;
+
+       read_cache();
+       dst = active_cache;
+       deleted = 0;
+       for (i = 0; i < active_nr; i++) {
+               struct cache_entry *ce = active_cache[i];
+               if (ce_stage(ce)) {
+                       deleted++;
+                       continue;
+               }
+               if (deleted)
+                       *dst = ce;
+               dst++;
+       }
+       active_nr -= deleted;
+       return deleted;
+}
+
 static char *read_tree_usage = "git-read-tree (<sha> | -m [-u] <sha1> [<sha2> [<sha3>]])";
 
 static struct cache_file cache_file;
 
 int main(int argc, char **argv)
 {
-       int i, newfd, merge;
+       int i, newfd, merge, reset;
        unsigned char sha1[20];
 
        newfd = hold_index_file_for_update(&cache_file, get_index_file());
@@ -289,6 +311,7 @@ int main(int argc, char **argv)
                die("unable to create new cachefile");
 
        merge = 0;
+       reset = 0;
        for (i = 1; i < argc; i++) {
                const char *arg = argv[i];
 
@@ -298,16 +321,22 @@ int main(int argc, char **argv)
                        continue;
                }
 
+               /* This differs from "-m" in that we'll silently ignore unmerged entries */
+               if (!strcmp(arg, "--reset")) {
+                       if (stage || merge)
+                               usage(read_tree_usage);
+                       reset = 1;
+                       merge = 1;
+                       stage = 1;
+                       read_cache_unmerged();
+               }
+
                /* "-m" stands for "merge", meaning we start in stage 1 */
                if (!strcmp(arg, "-m")) {
-                       int i;
-                       if (stage)
-                               die("-m needs to come first");
-                       read_cache();
-                       for (i = 0; i < active_nr; i++) {
-                               if (ce_stage(active_cache[i]))
-                                       die("you need to resolve your current index first");
-                       }
+                       if (stage || merge)
+                               usage(read_tree_usage);
+                       if (read_cache_unmerged())
+                               die("you need to resolve your current index first");
                        stage = 1;
                        merge = 1;
                        continue;