unpack-trees.c: assume submodules are clean during check-out
In particular, when moving back to a commit without a given submodule and then moving back forward to a commit with the given submodule, we shouldn't complain that updating would lose untracked file in the submodule, because git currently does not checkout subprojects during superproject check-out. Signed-off-by: Sven Verdoolaege <skimo@kotnet.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Sven Verdoolaege committed
Jul 17, 2007 at 20:28 UTC
0cf7375542504e3762753cdc4cff3bb5c8fc628e
2 files changed
+59
-25
t/t7400-submodule-basic.sh
+9
@@ -21,6 +21,10 @@ subcommands of git-submodule.
21
# -add an entry to .gitmodules for submodule 'example'
22
#
23
test_expect_success 'Prepare submodule testing' '
24
+ : > t &&
25
+ git-add t &&
26
+ git-commit -m "initial commit" &&
27
+ git branch initial HEAD &&
28
mkdir lib &&
29
cd lib &&
30
git init &&
@@ -166,4 +170,9 @@ test_expect_success 'status should be "up-to-date" after update' '
170
git-submodule status | grep "^ $rev1"
171
'
172
173
+test_expect_success 'checkout superproject with subproject already present' '
174
+ git-checkout initial &&
175
+ git-checkout master
176
+'
177
+
178
test_done
unpack-trees.c
+50
-25
@@ -5,6 +5,7 @@
5
#include "cache-tree.h"
6
#include "unpack-trees.h"
7
#include "progress.h"
8
+#include "refs.h"
9
10
#define DBRT_DEBUG 1
11
@@ -425,11 +426,24 @@ static void invalidate_ce_path(struct cache_entry *ce)
426
cache_tree_invalidate_path(active_cache_tree, ce->name);
427
}
428
428
-static int verify_clean_subdirectory(const char *path, const char *action,
429
+/*
430
+ * Check that checking out ce->sha1 in subdir ce->name is not
431
+ * going to overwrite any working files.
432
+ *
433
+ * Currently, git does not checkout subprojects during a superproject
434
+ * checkout, so it is not going to overwrite anything.
435
+ */
436
+static int verify_clean_submodule(struct cache_entry *ce, const char *action,
437
+ struct unpack_trees_options *o)
438
+{
439
+ return 0;
440
+}
441
+
442
+static int verify_clean_subdirectory(struct cache_entry *ce, const char *action,
443
struct unpack_trees_options *o)
444
{
445
/*
432
- * we are about to extract "path"; we would not want to lose
446
+ * we are about to extract "ce->name"; we would not want to lose
447
* anything in the existing directory there.
448
*/
449
int namelen;
@@ -437,13 +451,24 @@ static int verify_clean_subdirectory(const char *path, const char *action,
451
struct dir_struct d;
452
char *pathbuf;
453
int cnt = 0;
454
+ unsigned char sha1[20];
455
+
456
+ if (S_ISGITLINK(ntohl(ce->ce_mode)) &&
457
+ resolve_gitlink_ref(ce->name, "HEAD", sha1) == 0) {
458
+ /* If we are not going to update the submodule, then
459
+ * we don't care.
460
+ */
461
+ if (!hashcmp(sha1, ce->sha1))
462
+ return 0;
463
+ return verify_clean_submodule(ce, action, o);
464
+ }
465
466
/*
467
* First let's make sure we do not have a local modification
468
* in that directory.
469
*/
445
- namelen = strlen(path);
446
- pos = cache_name_pos(path, namelen);
470
+ namelen = strlen(ce->name);
471
+ pos = cache_name_pos(ce->name, namelen);
472
if (0 <= pos)
473
return cnt; /* we have it as nondirectory */
474
pos = -pos - 1;
@@ -451,7 +476,7 @@ static int verify_clean_subdirectory(const char *path, const char *action,
476
struct cache_entry *ce = active_cache[i];
477
int len = ce_namelen(ce);
478
if (len < namelen ||
454
- strncmp(path, ce->name, namelen) ||
479
+ strncmp(ce->name, ce->name, namelen) ||
480
ce->name[namelen] != '/')
481
break;
482
/*
@@ -469,16 +494,16 @@ static int verify_clean_subdirectory(const char *path, const char *action,
494
* present file that is not ignored.
495
*/
496
pathbuf = xmalloc(namelen + 2);
472
- memcpy(pathbuf, path, namelen);
497
+ memcpy(pathbuf, ce->name, namelen);
498
strcpy(pathbuf+namelen, "/");
499
500
memset(&d, 0, sizeof(d));
501
if (o->dir)
502
d.exclude_per_dir = o->dir->exclude_per_dir;
478
- i = read_directory(&d, path, pathbuf, namelen+1, NULL);
503
+ i = read_directory(&d, ce->name, pathbuf, namelen+1, NULL);
504
if (i)
505
die("Updating '%s' would lose untracked files in it",
481
- path);
506
+ ce->name);
507
free(pathbuf);
508
return cnt;
509
}
@@ -487,7 +512,7 @@ static int verify_clean_subdirectory(const char *path, const char *action,
512
* We do not want to remove or overwrite a working tree file that
513
* is not tracked, unless it is ignored.
514
*/
490
-static void verify_absent(const char *path, const char *action,
515
+static void verify_absent(struct cache_entry *ce, const char *action,
516
struct unpack_trees_options *o)
517
{
518
struct stat st;
@@ -495,15 +520,15 @@ static void verify_absent(const char *path, const char *action,
520
if (o->index_only || o->reset || !o->update)
521
return;
522
498
- if (has_symlink_leading_path(path, NULL))
523
+ if (has_symlink_leading_path(ce->name, NULL))
524
return;
525
501
- if (!lstat(path, &st)) {
526
+ if (!lstat(ce->name, &st)) {
527
int cnt;
528
504
- if (o->dir && excluded(o->dir, path))
529
+ if (o->dir && excluded(o->dir, ce->name))
530
/*
506
- * path is explicitly excluded, so it is Ok to
531
+ * ce->name is explicitly excluded, so it is Ok to
532
* overwrite it.
533
*/
534
return;
@@ -515,7 +540,7 @@ static void verify_absent(const char *path, const char *action,
540
* files that are in "foo/" we would lose
541
* it.
542
*/
518
- cnt = verify_clean_subdirectory(path, action, o);
543
+ cnt = verify_clean_subdirectory(ce, action, o);
544
545
/*
546
* If this removed entries from the index,
@@ -543,7 +568,7 @@ static void verify_absent(const char *path, const char *action,
568
* delete this path, which is in a subdirectory that
569
* is being replaced with a blob.
570
*/
546
- cnt = cache_name_pos(path, strlen(path));
571
+ cnt = cache_name_pos(ce->name, strlen(ce->name));
572
if (0 <= cnt) {
573
struct cache_entry *ce = active_cache[cnt];
574
if (!ce_stage(ce) && !ce->ce_mode)
@@ -551,7 +576,7 @@ static void verify_absent(const char *path, const char *action,
576
}
577
578
die("Untracked working tree file '%s' "
554
- "would be %s by merge.", path, action);
579
+ "would be %s by merge.", ce->name, action);
580
}
581
}
582
@@ -575,7 +600,7 @@ static int merged_entry(struct cache_entry *merge, struct cache_entry *old,
600
}
601
}
602
else {
578
- verify_absent(merge->name, "overwritten", o);
603
+ verify_absent(merge, "overwritten", o);
604
invalidate_ce_path(merge);
605
}
606
@@ -590,7 +615,7 @@ static int deleted_entry(struct cache_entry *ce, struct cache_entry *old,
615
if (old)
616
verify_uptodate(old, o);
617
else
593
- verify_absent(ce->name, "removed", o);
618
+ verify_absent(ce, "removed", o);
619
ce->ce_mode = 0;
620
add_cache_entry(ce, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
621
invalidate_ce_path(ce);
@@ -707,18 +732,18 @@ int threeway_merge(struct cache_entry **stages,
732
if (o->aggressive) {
733
int head_deleted = !head && !df_conflict_head;
734
int remote_deleted = !remote && !df_conflict_remote;
710
- const char *path = NULL;
735
+ struct cache_entry *ce = NULL;
736
737
if (index)
713
- path = index->name;
738
+ ce = index;
739
else if (head)
715
- path = head->name;
740
+ ce = head;
741
else if (remote)
717
- path = remote->name;
742
+ ce = remote;
743
else {
744
for (i = 1; i < o->head_idx; i++) {
745
if (stages[i] && stages[i] != o->df_conflict_entry) {
721
- path = stages[i]->name;
746
+ ce = stages[i];
747
break;
748
}
749
}
@@ -733,8 +758,8 @@ int threeway_merge(struct cache_entry **stages,
758
(remote_deleted && head && head_match)) {
759
if (index)
760
return deleted_entry(index, index, o);
736
- else if (path && !head_deleted)
737
- verify_absent(path, "removed", o);
761
+ else if (ce && !head_deleted)
762
+ verify_absent(ce, "removed", o);
763
return 0;
764
}
765
/*