X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=builtin-rm.c;h=57975dbcfd7c0dbcba03b88a1bf403f1ec5f528c;hb=e4d1afbcf2e39a997ae90befd629ee53e444c4c9;hp=e06640cf8d3418cbbe177b8fdcdccd19e0f3379f;hpb=a1eb73d917e15cd97314e0a39cbe857329339a96;p=git.git diff --git a/builtin-rm.c b/builtin-rm.c index e06640cf8..57975dbcf 100644 --- a/builtin-rm.c +++ b/builtin-rm.c @@ -31,7 +31,8 @@ static void add_list(const char *name) static int check_local_mod(unsigned char *head, int index_only) { - /* items in list are already sorted in the cache order, + /* + * Items in list are already sorted in the cache order, * so we could do this a lot more efficiently by using * tree_desc based traversal if we wanted to, but I am * lazy, and who cares if removal of files is a tad @@ -58,8 +59,7 @@ static int check_local_mod(unsigned char *head, int index_only) if (lstat(ce->name, &st) < 0) { if (errno != ENOENT) - fprintf(stderr, "warning: '%s': %s", - ce->name, strerror(errno)); + warning("'%s': %s", ce->name, strerror(errno)); /* It already vanished from the working tree */ continue; } @@ -71,24 +71,55 @@ static int check_local_mod(unsigned char *head, int index_only) */ continue; } + + /* + * "rm" of a path that has changes need to be treated + * carefully not to allow losing local changes + * accidentally. A local change could be (1) file in + * work tree is different since the index; and/or (2) + * the user staged a content that is different from + * the current commit in the index. + * + * In such a case, you would need to --force the + * removal. However, "rm --cached" (remove only from + * the index) is safe if the index matches the file in + * the work tree or the HEAD commit, as it means that + * the content being removed is available elsewhere. + */ + + /* + * Is the index different from the file in the work tree? + */ if (ce_match_stat(ce, &st, 0)) local_changes = 1; + + /* + * Is the index different from the HEAD commit? By + * definition, before the very initial commit, + * anything staged in the index is treated by the same + * way as changed from the HEAD. + */ if (no_head || get_tree_entry(head, name, sha1, &mode) || ce->ce_mode != create_ce_mode(mode) || hashcmp(ce->sha1, sha1)) staged_changes = 1; - if (local_changes && staged_changes) - errs = error("'%s' has staged content different " - "from both the file and the HEAD\n" - "(use -f to force removal)", name); + /* + * If the index does not match the file in the work + * tree and if it does not match the HEAD commit + * either, (1) "git rm" without --cached definitely + * will lose information; (2) "git rm --cached" will + * lose information unless it is about removing an + * "intent to add" entry. + */ + if (local_changes && staged_changes) { + if (!index_only || !(ce->ce_flags & CE_INTENT_TO_ADD)) + errs = error("'%s' has staged content different " + "from both the file and the HEAD\n" + "(use -f to force removal)", name); + } else if (!index_only) { - /* It's not dangerous to "git rm --cached" a - * file if the index matches the file or the - * HEAD, since it means the deleted content is - * still available somewhere. - */ if (staged_changes) errs = error("'%s' has changes staged in the index\n" "(use --cached to keep the file, " @@ -126,7 +157,8 @@ int cmd_rm(int argc, const char **argv, const char *prefix) git_config(git_default_config, NULL); - argc = parse_options(argc, argv, builtin_rm_options, builtin_rm_usage, 0); + argc = parse_options(argc, argv, prefix, builtin_rm_options, + builtin_rm_usage, 0); if (!argc) usage_with_options(builtin_rm_usage, builtin_rm_options); @@ -225,7 +257,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix) continue; } if (!removed) - die("git rm: %s: %s", path, strerror(errno)); + die_errno("git rm: '%s'", path); } }