make 'got/tog tree' show symlink targets like 'ls -lF' does: link@ -> target
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
diff --git a/got/got.1 b/got/got.1
index 37523a3..170dd8d 100644
--- a/got/got.1
+++ b/got/got.1
@@ -843,6 +843,8 @@ annotations:
.It $ Ta entry is a Git submodule
.El
.Pp
+Symbolic link entries are also annotated with the target path of the link.
+.Pp
If no
.Ar path
is specified, list the repository path corresponding to the current
diff --git a/got/got.c b/got/got.c
index e8ed834..f14e875 100644
--- a/got/got.c
+++ b/got/got.c
@@ -4299,13 +4299,15 @@ usage_tree(void)
exit(1);
}
-static void
+static const struct got_error *
print_entry(struct got_tree_entry *te, const char *id, const char *path,
- const char *root_path)
+ const char *root_path, struct got_repository *repo)
{
+ const struct got_error *err = NULL;
int is_root_path = (strcmp(path, root_path) == 0);
const char *modestr = "";
mode_t mode = got_tree_entry_get_mode(te);
+ char *link_target = NULL;
path += strlen(root_path);
while (path[0] == '/')
@@ -4313,15 +4315,30 @@ print_entry(struct got_tree_entry *te, const char *id, const char *path,
if (got_object_tree_entry_is_submodule(te))
modestr = "$";
- else if (S_ISLNK(mode))
+ else if (S_ISLNK(mode)) {
+ int i;
+
+ err = got_tree_entry_get_symlink_target(&link_target, te, repo);
+ if (err)
+ return err;
+ for (i = 0; i < strlen(link_target); i++) {
+ if (!isprint((unsigned char)link_target[i]))
+ link_target[i] = '?';
+ }
+
modestr = "@";
+ }
else if (S_ISDIR(mode))
modestr = "/";
else if (mode & S_IXUSR)
modestr = "*";
- printf("%s%s%s%s%s\n", id ? id : "", path,
- is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr);
+ printf("%s%s%s%s%s%s%s\n", id ? id : "", path,
+ is_root_path ? "" : "/", got_tree_entry_get_name(te), modestr,
+ link_target ? " -> ": "", link_target ? link_target : "");
+
+ free(link_target);
+ return NULL;
}
static const struct got_error *
@@ -4363,8 +4380,10 @@ print_tree(const char *path, struct got_object_id *commit_id,
}
free(id_str);
}
- print_entry(te, id, path, root_path);
+ err = print_entry(te, id, path, root_path, repo);
free(id);
+ if (err)
+ goto done;
if (recurse && S_ISDIR(got_tree_entry_get_mode(te))) {
char *child_path;
diff --git a/include/got_error.h b/include/got_error.h
index d17ff2d..50fe098 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -140,6 +140,7 @@
#define GOT_ERR_NO_REMOTE 123
#define GOT_ERR_FETCH_NO_BRANCH 124
#define GOT_ERR_FETCH_BAD_REF 125
+#define GOT_ERR_TREE_ENTRY_TYPE 126
static const struct got_error {
int code;
@@ -286,6 +287,7 @@ static const struct got_error {
{ GOT_ERR_NO_REMOTE, "remote repository not found" },
{ GOT_ERR_FETCH_NO_BRANCH, "could not find any branches to fetch" },
{ GOT_ERR_FETCH_BAD_REF, "reference cannot be fetched" },
+ { GOT_ERR_TREE_ENTRY_TYPE, "unexpected tree entry type" },
};
/*
diff --git a/include/got_object.h b/include/got_object.h
index 4619751..54c7093 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -191,6 +191,13 @@ const char *got_tree_entry_get_name(struct got_tree_entry *);
/* Get the object ID of a tree entry. */
struct got_object_id *got_tree_entry_get_id(struct got_tree_entry *);
+/*
+ * Get a string containing the target path of a given a symlink tree entry.
+ * The caller should dispose of it with free(3).
+ */
+const struct got_error *got_tree_entry_get_symlink_target(char **,
+ struct got_tree_entry *, struct got_repository *);
+
/* Get the index of a tree entry. */
int got_tree_entry_get_index(struct got_tree_entry *);
diff --git a/lib/object.c b/lib/object.c
index 6088425..9a3beff 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -862,6 +862,42 @@ got_tree_entry_get_id(struct got_tree_entry *te)
return &te->id;
}
+const struct got_error *
+got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
+ struct got_repository *repo)
+{
+ const struct got_error *err = NULL;
+ struct got_blob_object *blob = NULL;
+ size_t len;
+
+ *link_target = NULL;
+
+ /* S_IFDIR check avoids confusing symlinks with submodules. */
+ if ((te->mode & (S_IFDIR | S_IFLNK)) != S_IFLNK)
+ return got_error(GOT_ERR_TREE_ENTRY_TYPE);
+
+ err = got_object_open_as_blob(&blob, repo,
+ got_tree_entry_get_id(te), PATH_MAX);
+ if (err)
+ return err;
+
+ err = got_object_blob_read_block(&len, blob);
+ if (err)
+ goto done;
+
+ *link_target = malloc(len + 1);
+ if (*link_target == NULL) {
+ err = got_error_from_errno("malloc");
+ goto done;
+ }
+ memcpy(*link_target, got_object_blob_get_read_buf(blob), len);
+ (*link_target)[len] = '\0';
+done:
+ if (blob)
+ got_object_blob_close(blob);
+ return err;
+}
+
int
got_tree_entry_get_index(struct got_tree_entry *te)
{
diff --git a/tog/tog.1 b/tog/tog.1
index 1b3bc07..9311986 100644
--- a/tog/tog.1
+++ b/tog/tog.1
@@ -304,6 +304,8 @@ Displayed tree entries may carry one of the following trailing annotations:
.It $ Ta entry is a Git submodule
.El
.Pp
+Symbolic link entries are also annotated with the target path of the link.
+.Pp
The key bindings for
.Cm tog tree
are as follows:
diff --git a/tog/tog.c b/tog/tog.c
index de21c04..37110e9 100644
--- a/tog/tog.c
+++ b/tog/tog.c
@@ -18,6 +18,7 @@
#include <sys/stat.h>
#include <sys/ioctl.h>
+#include <ctype.h>
#include <errno.h>
#define _XOPEN_SOURCE_EXTENDED
#include <curses.h>
@@ -4622,7 +4623,7 @@ draw_tree_entries(struct tog_view *view,
struct got_tree_entry **selected_entry, int *ndisplayed,
const char *label, int show_ids, const char *parent_path,
struct got_tree_object *tree, int selected, int limit,
- int isroot, struct tog_colors *colors)
+ int isroot, struct tog_colors *colors, struct got_repository *repo)
{
const struct got_error *err = NULL;
struct got_tree_entry *te;
@@ -4693,7 +4694,7 @@ draw_tree_entries(struct tog_view *view,
nentries = got_object_tree_get_nentries(tree);
for (i = got_tree_entry_get_index(te); i < nentries; i++) {
- char *line = NULL, *id_str = NULL;
+ char *line = NULL, *id_str = NULL, *link_target = NULL;
const char *modestr = "";
mode_t mode;
@@ -4709,18 +4710,35 @@ draw_tree_entries(struct tog_view *view,
}
if (got_object_tree_entry_is_submodule(te))
modestr = "$";
- else if (S_ISLNK(mode))
+ else if (S_ISLNK(mode)) {
+ int i;
+
+ err = got_tree_entry_get_symlink_target(&link_target,
+ te, repo);
+ if (err) {
+ free(id_str);
+ return err;
+ }
+ for (i = 0; i < strlen(link_target); i++) {
+ if (!isprint((unsigned char)link_target[i]))
+ link_target[i] = '?';
+ }
modestr = "@";
+ }
else if (S_ISDIR(mode))
modestr = "/";
else if (mode & S_IXUSR)
modestr = "*";
- if (asprintf(&line, "%s %s%s", id_str ? id_str : "",
- got_tree_entry_get_name(te), modestr) == -1) {
+ if (asprintf(&line, "%s %s%s%s%s", id_str ? id_str : "",
+ got_tree_entry_get_name(te), modestr,
+ link_target ? " -> ": "",
+ link_target ? link_target : "") == -1) {
free(id_str);
+ free(link_target);
return got_error_from_errno("asprintf");
}
free(id_str);
+ free(link_target);
err = format_line(&wline, &width, line, view->ncols, 0);
if (err) {
free(line);
@@ -5121,7 +5139,7 @@ show_tree_view(struct tog_view *view)
&s->last_displayed_entry, &s->selected_entry,
&s->ndisplayed, s->tree_label, s->show_ids, parent_path,
s->tree, s->selected, view->nlines, s->tree == s->root,
- &s->colors);
+ &s->colors, s->repo);
free(parent_path);
view_vborder(view);