summaryrefslogtreecommitdiff
path: root/unpack-trees.c
diff options
context:
space:
mode:
Diffstat (limited to 'unpack-trees.c')
-rw-r--r--unpack-trees.c67
1 files changed, 49 insertions, 18 deletions
diff --git a/unpack-trees.c b/unpack-trees.c
index 89ca95ce90..360844bda3 100644
--- a/unpack-trees.c
+++ b/unpack-trees.c
@@ -36,6 +36,9 @@ static const char *unpack_plumbing_errors[NB_UNPACK_TREES_WARNING_TYPES] = {
/* ERROR_NOT_UPTODATE_DIR */
"Updating '%s' would lose untracked files in it",
+ /* ERROR_CWD_IN_THE_WAY */
+ "Refusing to remove '%s' since it is the current working directory.",
+
/* ERROR_WOULD_LOSE_UNTRACKED_OVERWRITTEN */
"Untracked working tree file '%s' would be overwritten by merge.",
@@ -131,6 +134,9 @@ void setup_unpack_trees_porcelain(struct unpack_trees_options *opts,
msgs[ERROR_NOT_UPTODATE_DIR] =
_("Updating the following directories would lose untracked files in them:\n%s");
+ msgs[ERROR_CWD_IN_THE_WAY] =
+ _("Refusing to remove the current working directory:\n%s");
+
if (!strcmp(cmd, "checkout"))
msg = advice_enabled(ADVICE_COMMIT_BEFORE_MERGE)
? _("The following untracked working tree files would be removed by checkout:\n%%s"
@@ -645,17 +651,24 @@ static void mark_ce_used_same_name(struct cache_entry *ce,
}
}
-static struct cache_entry *next_cache_entry(struct unpack_trees_options *o)
+static struct cache_entry *next_cache_entry(struct unpack_trees_options *o, int *hint)
{
const struct index_state *index = o->src_index;
int pos = o->cache_bottom;
+ if (*hint > pos)
+ pos = *hint;
+
while (pos < index->cache_nr) {
struct cache_entry *ce = index->cache[pos];
- if (!(ce->ce_flags & CE_UNPACKED))
+ if (!(ce->ce_flags & CE_UNPACKED)) {
+ *hint = pos + 1;
return ce;
+ }
pos++;
}
+
+ *hint = pos;
return NULL;
}
@@ -1231,7 +1244,9 @@ static int find_cache_pos(struct traverse_info *info,
/*
* Given a sparse directory entry 'ce', compare ce->name to
- * info->name + '/' + p->path + '/' if info->name is non-empty.
+ * info->traverse_path + p->path + '/' if info->traverse_path
+ * is non-empty.
+ *
* Compare ce->name to p->path + '/' otherwise. Note that
* ce->name must end in a trailing '/' because it is a sparse
* directory entry.
@@ -1243,11 +1258,11 @@ static int sparse_dir_matches_path(const struct cache_entry *ce,
assert(S_ISSPARSEDIR(ce->ce_mode));
assert(ce->name[ce->ce_namelen - 1] == '/');
- if (info->namelen)
- return ce->ce_namelen == info->namelen + p->pathlen + 2 &&
- ce->name[info->namelen] == '/' &&
- !strncmp(ce->name, info->name, info->namelen) &&
- !strncmp(ce->name + info->namelen + 1, p->path, p->pathlen);
+ if (info->pathlen)
+ return ce->ce_namelen == info->pathlen + p->pathlen + 1 &&
+ ce->name[info->pathlen - 1] == '/' &&
+ !strncmp(ce->name, info->traverse_path, info->pathlen) &&
+ !strncmp(ce->name + info->pathlen, p->path, p->pathlen);
return ce->ce_namelen == p->pathlen + 1 &&
!strncmp(ce->name, p->path, p->pathlen);
}
@@ -1365,12 +1380,13 @@ static int unpack_callback(int n, unsigned long mask, unsigned long dirmask, str
/* Are we supposed to look at the index too? */
if (o->merge) {
+ int hint = -1;
while (1) {
int cmp;
struct cache_entry *ce;
if (o->diff_index_cached)
- ce = next_cache_entry(o);
+ ce = next_cache_entry(o, &hint);
else
ce = find_cache_entry(info, p);
@@ -1690,7 +1706,7 @@ static int verify_absent(const struct cache_entry *,
int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options *o)
{
struct repository *repo = the_repository;
- int i, ret;
+ int i, hint, ret;
static struct cache_entry *dfc;
struct pattern_list pl;
int free_pattern_list = 0;
@@ -1779,13 +1795,15 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
info.pathspec = o->pathspec;
if (o->prefix) {
+ hint = -1;
+
/*
* Unpack existing index entries that sort before the
* prefix the tree is spliced into. Note that o->merge
* is always true in this case.
*/
while (1) {
- struct cache_entry *ce = next_cache_entry(o);
+ struct cache_entry *ce = next_cache_entry(o, &hint);
if (!ce)
break;
if (ce_in_traverse_path(ce, &info))
@@ -1806,8 +1824,9 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options
/* Any left-over entries in the index? */
if (o->merge) {
+ hint = -1;
while (1) {
- struct cache_entry *ce = next_cache_entry(o);
+ struct cache_entry *ce = next_cache_entry(o, &hint);
if (!ce)
break;
if (unpack_index_entry(ce, o) < 0)
@@ -2146,10 +2165,7 @@ static int verify_clean_subdirectory(const struct cache_entry *ce,
cnt++;
}
- /*
- * Then we need to make sure that we do not lose a locally
- * present file that is not ignored.
- */
+ /* Do not lose a locally present file that is not ignored. */
pathbuf = xstrfmt("%.*s/", namelen, ce->name);
memset(&d, 0, sizeof(d));
@@ -2160,6 +2176,12 @@ static int verify_clean_subdirectory(const struct cache_entry *ce,
free(pathbuf);
if (i)
return add_rejected_path(o, ERROR_NOT_UPTODATE_DIR, ce->name);
+
+ /* Do not lose startup_info->original_cwd */
+ if (startup_info->original_cwd &&
+ !strcmp(startup_info->original_cwd, ce->name))
+ return add_rejected_path(o, ERROR_CWD_IN_THE_WAY, ce->name);
+
return cnt;
}
@@ -2252,10 +2274,19 @@ static int verify_absent_1(const struct cache_entry *ce,
int len;
struct stat st;
- if (o->index_only || !o->update ||
- o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED)
+ if (o->index_only || !o->update)
return 0;
+ if (o->reset == UNPACK_RESET_OVERWRITE_UNTRACKED) {
+ /* Avoid nuking startup_info->original_cwd... */
+ if (startup_info->original_cwd &&
+ !strcmp(startup_info->original_cwd, ce->name))
+ return add_rejected_path(o, ERROR_CWD_IN_THE_WAY,
+ ce->name);
+ /* ...but nuke anything else. */
+ return 0;
+ }
+
len = check_leading_path(ce->name, ce_namelen(ce), 0);
if (!len)
return 0;