fix 'got revert' on files in newly added subdirectories
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
diff --git a/lib/worktree.c b/lib/worktree.c
index 59a5e82..4f7c0c9 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -2645,29 +2645,32 @@ revert_file(struct got_worktree *worktree, struct got_fileindex *fileindex,
}
}
- err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
- tree_path);
- if (err)
- goto done;
-
- err = got_object_open_as_tree(&tree, repo, tree_id);
+ err = get_file_status(&status, &sb, ie, ondisk_path, repo);
if (err)
goto done;
- te_name = basename(ie->path);
- if (te_name == NULL) {
- err = got_error_from_errno2("basename", ie->path);
- goto done;
- }
+ err = got_object_id_by_path(&tree_id, repo, worktree->base_commit_id,
+ tree_path);
+ if (err) {
+ if (!(err->code == GOT_ERR_NO_TREE_ENTRY &&
+ status == GOT_STATUS_ADD))
+ goto done;
+ } else {
+ err = got_object_open_as_tree(&tree, repo, tree_id);
+ if (err)
+ goto done;
- err = get_file_status(&status, &sb, ie, ondisk_path, repo);
- if (err)
- goto done;
+ te_name = basename(ie->path);
+ if (te_name == NULL) {
+ err = got_error_from_errno2("basename", ie->path);
+ goto done;
+ }
- te = got_object_tree_find_entry(tree, te_name);
- if (te == NULL && status != GOT_STATUS_ADD) {
- err = got_error(GOT_ERR_NO_TREE_ENTRY);
- goto done;
+ te = got_object_tree_find_entry(tree, te_name);
+ if (te == NULL && status != GOT_STATUS_ADD) {
+ err = got_error(GOT_ERR_NO_TREE_ENTRY);
+ goto done;
+ }
}
switch (status) {
diff --git a/regress/cmdline/add.sh b/regress/cmdline/add.sh
index da716a3..727c09d 100755
--- a/regress/cmdline/add.sh
+++ b/regress/cmdline/add.sh
@@ -96,6 +96,31 @@ function test_add_multiple {
test_done "$testroot" "$ret"
}
+function test_add_file_in_new_subdir {
+ local testroot=`test_init add_file_in_new_subdir`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ mkdir -p $testroot/wt/new
+ echo "new file" > $testroot/wt/new/foo
+
+ echo 'A new/foo' > $testroot/stdout.expected
+ (cd $testroot/wt && got add new/foo > $testroot/stdout)
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+}
+
run_test test_add_basic
run_test test_double_add
run_test test_add_multiple
+run_test test_add_file_in_new_subdir
diff --git a/regress/cmdline/revert.sh b/regress/cmdline/revert.sh
index 65ebe13..63d1e5e 100755
--- a/regress/cmdline/revert.sh
+++ b/regress/cmdline/revert.sh
@@ -183,7 +183,46 @@ function test_revert_multiple {
test_done "$testroot" "$ret"
}
+function test_revert_file_in_new_subdir {
+ local testroot=`test_init revert_file_in_new_subdir`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+
+ mkdir -p $testroot/wt/newdir
+ echo new > $testroot/wt/newdir/new
+ (cd $testroot/wt && got add newdir/new > /dev/null)
+
+ (cd $testroot/wt && got revert newdir/new > $testroot/stdout)
+
+ echo "R newdir/new" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ (cd $testroot/wt && got status > $testroot/stdout)
+
+ echo "? newdir/new" > $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "$ret"
+
+}
+
run_test test_revert_basic
run_test test_revert_rm
run_test test_revert_add
run_test test_revert_multiple
+run_test test_revert_file_in_new_subdir