add symlink support to 'got blame' and 'tog blame'
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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
diff --git a/got/got.c b/got/got.c
index f14e875..ab4db7c 100644
--- a/got/got.c
+++ b/got/got.c
@@ -4100,6 +4100,7 @@ cmd_blame(int argc, char *argv[])
struct got_repository *repo = NULL;
struct got_worktree *worktree = NULL;
char *path, *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
+ char *link_target = NULL;
struct got_object_id *obj_id = NULL;
struct got_object_id *commit_id = NULL;
struct got_blob_object *blob = NULL;
@@ -4214,7 +4215,13 @@ cmd_blame(int argc, char *argv[])
goto done;
}
- error = got_object_id_by_path(&obj_id, repo, commit_id, in_repo_path);
+ error = got_object_resolve_symlinks(&link_target, in_repo_path,
+ commit_id, repo);
+ if (error)
+ goto done;
+
+ error = got_object_id_by_path(&obj_id, repo, commit_id,
+ link_target ? link_target : in_repo_path);
if (error)
goto done;
@@ -4258,10 +4265,11 @@ cmd_blame(int argc, char *argv[])
}
bca.repo = repo;
- error = got_blame(in_repo_path, commit_id, repo, blame_cb, &bca,
- check_cancelled, NULL);
+ error = got_blame(link_target ? link_target : in_repo_path, commit_id,
+ repo, blame_cb, &bca, check_cancelled, NULL);
done:
free(in_repo_path);
+ free(link_target);
free(repo_path);
free(cwd);
free(commit_id);
diff --git a/regress/cmdline/blame.sh b/regress/cmdline/blame.sh
index 9e0475a..d3a3bf3 100755
--- a/regress/cmdline/blame.sh
+++ b/regress/cmdline/blame.sh
@@ -762,6 +762,126 @@ function test_blame_submodule {
test_done "$testroot" "$ret"
}
+function test_blame_symlink {
+ local testroot=`test_init blame_symlink`
+ local commit_id0=`git_show_head $testroot/repo`
+ local short_commit0=`trim_obj_id 32 $commit_id0`
+
+ (cd $testroot/repo && ln -s alpha alpha.link)
+ (cd $testroot/repo && ln -s epsilon epsilon.link)
+ (cd $testroot/repo && ln -s /etc/passwd passwd.link)
+ (cd $testroot/repo && ln -s ../beta epsilon/beta.link)
+ (cd $testroot/repo && ln -s nonexistent nonexistent.link)
+ (cd $testroot/repo && git add .)
+ git_commit $testroot/repo -m "add symlinks"
+
+ local commit_id1=`git_show_head $testroot/repo`
+ local short_commit1=`trim_obj_id 32 $commit_id1`
+ local author_time=`git_show_author_time $testroot/repo`
+
+ # got blame dereferences symlink to a regular file
+ got blame -r $testroot/repo alpha.link > $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "blame command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ d=`date -r $author_time +"%G-%m-%d"`
+ echo "1) $short_commit0 $d $GOT_AUTHOR_8 alpha" \
+ > $testroot/stdout.expected
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" -a "$xfail" == "" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ # got blame dereferences symlink with relative path
+ got blame -r $testroot/repo epsilon/beta.link > $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ echo "blame command failed unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ d=`date -r $author_time +"%G-%m-%d"`
+ echo "1) $short_commit0 $d $GOT_AUTHOR_8 beta" \
+ > $testroot/stdout.expected
+
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" -a "$xfail" == "" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ got blame -r $testroot/repo epsilon.link > $testroot/stdout \
+ 2> $testroot/stderr
+ ret="$?"
+ if [ "$ret" == "0" ]; then
+ echo "blame command succeeded unexpectedly" >&2
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ # blame dereferences symlink to a directory
+ echo "got: wrong type of object" > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ # got blame fails if symlink target does not exist in repo
+ got blame -r $testroot/repo passwd.link > $testroot/stdout \
+ 2> $testroot/stderr
+ ret="$?"
+ if [ "$ret" == "0" ]; then
+ echo "blame command succeeded unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "got: /etc/passwd: no such entry found in tree" \
+ > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ got blame -r $testroot/repo nonexistent.link > $testroot/stdout \
+ 2> $testroot/stderr
+ ret="$?"
+ if [ "$ret" == "0" ]; then
+ echo "blame command succeeded unexpectedly" >&2
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "got: /nonexistent: no such entry found in tree" \
+ > $testroot/stderr.expected
+ cmp -s $testroot/stderr.expected $testroot/stderr
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stderr.expected $testroot/stderr
+ test_done "$testroot" "1"
+ return 1
+ fi
+
+ test_done "$testroot" "$ret"
+}
+
run_test test_blame_basic
run_test test_blame_tag
run_test test_blame_file_single_line
@@ -773,3 +893,4 @@ run_test test_blame_commit_subsumed
run_test test_blame_blame_h
run_test test_blame_added_on_branch
run_test test_blame_submodule
+run_test test_blame_symlink
diff --git a/tog/tog.c b/tog/tog.c
index 37110e9..47ddd48 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -4499,6 +4499,7 @@ cmd_blame(int argc, char *argv[])
struct got_reflist_head refs;
struct got_worktree *worktree = NULL;
char *cwd = NULL, *repo_path = NULL, *in_repo_path = NULL;
+ char *link_target = NULL;
struct got_object_id *commit_id = NULL;
char *commit_id_str = NULL;
int ch;
@@ -4594,7 +4595,14 @@ cmd_blame(int argc, char *argv[])
error = got_error_from_errno("view_open");
goto done;
}
- error = open_blame_view(view, in_repo_path, commit_id, &refs, repo);
+
+ error = got_object_resolve_symlinks(&link_target, in_repo_path,
+ commit_id, repo);
+ if (error)
+ goto done;
+
+ error = open_blame_view(view, link_target ? link_target : in_repo_path,
+ commit_id, &refs, repo);
if (error)
goto done;
if (worktree) {
@@ -4606,6 +4614,7 @@ cmd_blame(int argc, char *argv[])
done:
free(repo_path);
free(in_repo_path);
+ free(link_target);
free(cwd);
free(commit_id);
if (worktree)