From 438195ccedce7270cf5ba167a940c90467cb72d7 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Thu, 9 Jun 2005 12:51:01 -0700 Subject: [PATCH] git-read-tree: add "--reset" flag 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 | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/read-tree.c b/read-tree.c index fbd0da0eb..b3a3c4ab5 100644 --- a/read-tree.c +++ b/read-tree.c @@ -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 ( | -m [-u] [ []])"; 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; -- 2.26.2