add symlink support to 'got import'
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
diff --git a/lib/repository.c b/lib/repository.c
index dec9253..6234533 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -1454,7 +1454,12 @@ alloc_added_blob_tree_entry(struct got_tree_entry **new_te,
goto done;
}
- (*new_te)->mode = S_IFREG | (mode & ((S_IRWXU | S_IRWXG | S_IRWXO)));
+ if (S_ISLNK(mode)) {
+ (*new_te)->mode = S_IFLNK;
+ } else {
+ (*new_te)->mode = S_IFREG;
+ (*new_te)->mode |= (mode & (S_IRWXU | S_IRWXG | S_IRWXO));
+ }
memcpy(&(*new_te)->id, blob_id, sizeof((*new_te)->id));
done:
if (err && *new_te) {
@@ -1607,7 +1612,7 @@ write_tree(struct got_object_id **new_tree_id, const char *path_dir,
err = NULL;
continue;
}
- } else if (type == DT_REG) {
+ } else if (type == DT_REG || type == DT_LNK) {
err = import_file(&new_te, de, path_dir, repo);
if (err)
goto done;
@@ -1628,7 +1633,7 @@ write_tree(struct got_object_id **new_tree_id, const char *path_dir,
TAILQ_FOREACH(pe, &paths, entry) {
struct got_tree_entry *te = pe->data;
char *path;
- if (!S_ISREG(te->mode))
+ if (!S_ISREG(te->mode) && !S_ISLNK(te->mode))
continue;
if (asprintf(&path, "%s/%s", path_dir, pe->path) == -1) {
err = got_error_from_errno("asprintf");
diff --git a/regress/cmdline/common.sh b/regress/cmdline/common.sh
index 5104375..bc744fc 100644
--- a/regress/cmdline/common.sh
+++ b/regress/cmdline/common.sh
@@ -156,7 +156,8 @@ function get_blob_id
tree_path="$2"
filename="$3"
- got tree -r $repo -i $tree_path | grep ${filename}$ | cut -d' ' -f 1
+ got tree -r $repo -i $tree_path | grep "[0-9a-f] ${filename}$" | \
+ cut -d' ' -f 1
}
function test_init
diff --git a/regress/cmdline/import.sh b/regress/cmdline/import.sh
index a5da67c..82df005 100755
--- a/regress/cmdline/import.sh
+++ b/regress/cmdline/import.sh
@@ -241,7 +241,58 @@ function test_import_empty_dir {
test_done "$testroot" "$ret"
}
+function test_import_symlink {
+ local testname=import_symlink
+ local testroot=`mktemp -p /tmp -d got-test-$testname-XXXXXXXX`
+
+ got init $testroot/repo
+
+ mkdir $testroot/tree
+ echo 'this is file alpha' > $testroot/tree/alpha
+ ln -s alpha $testroot/tree/alpha.link
+
+ got import -m 'init' -r $testroot/repo $testroot/tree \
+ > $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ local head_commit=`git_show_head $testroot/repo`
+ echo "A $testroot/tree/alpha" > $testroot/stdout.expected
+ echo "A $testroot/tree/alpha.link" >> $testroot/stdout.expected
+ echo "Created branch refs/heads/main with commit $head_commit" \
+ >> $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
+
+ id_alpha=`get_blob_id $testroot/repo "" alpha`
+ id_alpha_link=$(got tree -r $testroot/repo -i | grep 'alpha.link@ -> alpha$' | cut -d' ' -f 1)
+ tree_id=`(cd $testroot/repo && got cat $head_commit | \
+ grep ^tree | cut -d ' ' -f 2)`
+
+ got tree -i -r $testroot/repo -c $head_commit > $testroot/stdout
+
+ echo "$id_alpha alpha" > $testroot/stdout.expected
+ echo "$id_alpha_link alpha.link@ -> alpha" >> $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_import_basic
run_test test_import_requires_new_branch
run_test test_import_ignores
run_test test_import_empty_dir
+run_test test_import_symlink