Commit Diff


commit - 06285208fae857821ae1f92a774f2bc91c71ec7a9f5817693caf26bb5f88fad7
commit + a3e261487a807988b92001ccb748c29465526cb3f5be0bf2c2b6de6123a1a27b
blob - 1c12678e6769c0989f259822e9d4189fe14dbdbb0c6173845b48ffb7201fb8fd
blob + a66afeebc12460c1a63f9dbb241cbb9936afddd02f054f9ead638aae79dc3d61
--- got/got.1	
+++ got/got.1	
@@ -670,6 +670,7 @@ Show the status of each affected file, using the follo
 .Bl -column YXZ description
 .It A Ta new file was added
 .It E Ta file already exists in work tree's meta-data
+.It ? Ta file with a path outside of the work tree was skipped
 .El
 .Pp
 If the
blob - 343dbad110e6e5286cee88ff083dfac4426e794e8fb683c08a6553ac91287d4a
blob + 8f39b2d5a4a68a7bfe6810d23e4e867e30aa9851f11157a39319b132024fdfcd
--- lib/fileindex.c	
+++ lib/fileindex.c	
@@ -39,7 +39,16 @@
 #include "got_lib_hash.h"
 #include "got_lib_fileindex.h"
 #include "got_lib_worktree.h"
+#include "got_lib_delta.h"
+#include "got_lib_object.h"
+#include "got_lib_pack.h"
+#include "got_lib_object_cache.h"
+#include "got_lib_repository.h"
 
+#ifndef nitems
+#define nitems(_a)	(sizeof((_a)) / sizeof((_a)[0]))
+#endif
+
 /* got_fileindex_entry flags */
 #define GOT_FILEIDX_F_PATH_LEN		0x00000fff
 #define GOT_FILEIDX_F_STAGE		0x0000f000
@@ -158,11 +167,42 @@ got_fileindex_entry_mark_skipped(struct got_fileindex_
 }
 
 const struct got_error *
+got_fileindex_entry_relpath_allowed(const char *relpath, size_t relpath_len)
+{
+	const char *forbidden[] = {
+		GOT_GIT_DIR,
+		GOT_WORKTREE_GOT_DIR,
+		GOT_WORKTREE_CVG_DIR
+	};
+	size_t i;
+
+	for (i = 0; i < nitems(forbidden); i++) {
+		if (got_path_cmp(relpath, forbidden[i],
+		    relpath_len, strlen(forbidden[i])) == 0 ||
+		    got_path_is_child(relpath, forbidden[i],
+		    strlen(forbidden[i]))) {
+			return got_error_fmt(GOT_ERR_BAD_PATH,
+			    "path '%s' cannot be added to the file-index",
+			    relpath);
+		}
+	}
+
+	return NULL;
+}
+
+const struct got_error *
 got_fileindex_entry_alloc(struct got_fileindex_entry **ie,
     const char *relpath)
 {
+	const struct got_error *err;
 	size_t len;
 
+	len = strlen(relpath);
+
+	err = got_fileindex_entry_relpath_allowed(relpath, len);
+	if (err)
+		return err;
+
 	*ie = calloc(1, sizeof(**ie));
 	if (*ie == NULL)
 		return got_error_from_errno("calloc");
@@ -175,7 +215,6 @@ got_fileindex_entry_alloc(struct got_fileindex_entry *
 		return err;
 	}
 
-	len = strlen(relpath);
 	if (len > GOT_FILEIDX_F_PATH_LEN)
 		len = GOT_FILEIDX_F_PATH_LEN;
 	(*ie)->flags |= len;
blob - e310f768068ec50c862c1bd0346a1f8f11893f9f87e5a8d1fc4cef0c6dd6d0aa
blob + 7f1b632332bcb798392c73bd256f282884a52762a01bff5bd2b9d3f7905af105
--- lib/got_lib_fileindex.h	
+++ lib/got_lib_fileindex.h	
@@ -124,6 +124,8 @@ const struct got_error *got_fileindex_entry_update(str
 void got_fileindex_entry_mark_skipped(struct got_fileindex_entry *);
 const struct got_error *got_fileindex_entry_alloc(struct got_fileindex_entry **,
     const char *);
+const struct got_error *got_fileindex_entry_relpath_allowed(const char *,
+    size_t );
 void got_fileindex_entry_free(struct got_fileindex_entry *);
 
 struct got_fileindex *got_fileindex_alloc(enum got_hash_algorithm);
blob - b4245b6245d5da06c08f336c3635840d2fcaf2480595f68541acd044757d765a
blob + c5b5dbc5f19754d24d01826a72bf2dd0402397e4082ec845347612d844de792f
--- lib/worktree.c	
+++ lib/worktree.c	
@@ -1947,6 +1947,14 @@ update_blob(struct got_worktree *worktree,
 	int fd1 = -1, fd2 = -1;
 	int update_timestamps = 0;
 
+	err = got_fileindex_entry_relpath_allowed(path, strlen(path));
+	if (err) {
+		if (err->code != GOT_ERR_BAD_PATH)
+			return err;
+		return (*progress_cb)(progress_arg,
+		    GOT_STATUS_UNVERSIONED, path);
+	}
+
 	if (asprintf(&ondisk_path, "%s/%s", worktree->root_path, path) == -1)
 		return got_error_from_errno("asprintf");
 
blob - 295d2f088f24d5a7a2ac852d40d14d0ec72fe233874793d9b34ecf956f2b94f7
blob + e1762f72984a700105f7cf53bc895c4462e347c6ca150fdfb8486e034017ff47
--- regress/cmdline/add.sh	
+++ regress/cmdline/add.sh	
@@ -485,6 +485,95 @@ test_add_symlink() {
 	test_done "$testroot" "$ret"
 }
 
+test_add_file_in_dotgit() {
+	local testroot=`test_init add_file_in_dotgit`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	mkdir -p $testroot/wt/.git
+	echo "new file" > $testroot/wt/.git/foo
+
+	(cd $testroot/wt && got add .git/foo \
+		> $testroot/stdout 2> $testroot/stderr)
+	ret=$?
+	if [ $ret -eq 0 ]; then
+		echo "got add succeeded unexpectedly" >&2
+		test_done "$testroot" 1
+		return 1
+	fi
+
+	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" 1
+		return 1
+	fi
+
+	cat > $testroot/stderr.expected <<EOF
+got: path '.git/foo' cannot be added to the file-index: bad path
+EOF
+	cmp -s $testroot/stderr.expected $testroot/stderr
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" 1
+		return 1
+	fi
+
+	test_done "$testroot" "$ret"
+}
+
+test_add_file_in_dotgot() {
+	local testroot=`test_init add_file_in_dotgot`
+
+	got checkout $testroot/repo $testroot/wt > /dev/null
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "new file" > $testroot/wt/.got/foo
+
+	(cd $testroot/wt && got add .got/foo \
+		> $testroot/stdout 2> $testroot/stderr)
+	ret=$?
+	if [ $ret -eq 0 ]; then
+		echo "got add succeeded unexpectedly" >&2
+		test_done "$testroot" 1
+		return 1
+	fi
+
+	echo -n > $testroot/stdout.expected
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" 1
+		return 1
+	fi
+
+	cat > $testroot/stderr.expected <<EOF
+got: path '.got/foo' cannot be added to the file-index: bad path
+EOF
+	cmp -s $testroot/stderr.expected $testroot/stderr
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stderr.expected $testroot/stderr
+		test_done "$testroot" 1
+		return 1
+	fi
+
+	test_done "$testroot" "$ret"
+}
+
 test_parseargs "$@"
 run_test test_add_basic
 run_test test_double_add
@@ -495,3 +584,5 @@ run_test test_add_force_delete_commit
 run_test test_add_directory
 run_test test_add_clashes_with_submodule
 run_test test_add_symlink
+run_test test_add_file_in_dotgit
+run_test test_add_file_in_dotgot
blob - a7c9621756dd805c2ce27021d5447e3ac5c1032a8ee612d344424dc623c8cdcb
blob + 7e2eef68ba9eebe707b274be59378b4d4205dcbcb0aef4bad77d2db8fdc006cb
--- regress/cmdline/checkout.sh	
+++ regress/cmdline/checkout.sh	
@@ -1075,6 +1075,55 @@ test_checkout_commit_keywords() {
 	test_done "$testroot" "$ret"
 }
 
+test_checkout_tree_with_dot_got() {
+	local testroot=`test_init checkout_tree_with_dot_got`
+
+	mkdir -p $testroot/repo/.got
+	echo 'foo' > $testroot/repo/.got/foo
+	git -C $testroot/repo add .got/foo
+	git_commit $testroot/repo -m "adding .got/foo"
+
+	local commit_id=`git_show_head $testroot/repo`
+
+	echo "?  $testroot/wt/.got/foo" > $testroot/stdout.expected
+	echo "A  $testroot/wt/alpha" >> $testroot/stdout.expected
+	echo "A  $testroot/wt/beta" >> $testroot/stdout.expected
+	echo "A  $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
+	echo "A  $testroot/wt/gamma/delta" >> $testroot/stdout.expected
+	echo "Checked out refs/heads/master: $commit_id" \
+		>> $testroot/stdout.expected
+	echo "Now shut up and hack" >> $testroot/stdout.expected
+
+	got checkout $testroot/repo $testroot/wt > $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	cmp -s $testroot/stdout.expected $testroot/stdout
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/stdout.expected $testroot/stdout
+		test_done "$testroot" "$ret"
+		return 1
+	fi
+
+	echo "alpha" > $testroot/content.expected
+	echo "beta" >> $testroot/content.expected
+	echo "zeta" >> $testroot/content.expected
+	echo "delta" >> $testroot/content.expected
+	cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
+	    $testroot/wt/gamma/delta > $testroot/content
+
+	cmp -s $testroot/content.expected $testroot/content
+	ret=$?
+	if [ $ret -ne 0 ]; then
+		diff -u $testroot/content.expected $testroot/content
+	fi
+	test_done "$testroot" "$ret"
+}
+
 test_parseargs "$@"
 run_test test_checkout_basic
 run_test test_checkout_dir_exists
@@ -1094,3 +1143,4 @@ run_test test_checkout_quiet
 run_test test_checkout_umask
 run_test test_checkout_ulimit_n
 run_test test_checkout_commit_keywords
+run_test test_checkout_tree_with_dot_got