From: dturner@twopensource.com
To: git@vger.kernel.org
Cc: David Turner <dturner@twitter.com>
Subject: [PATCH 3/3] cat-file: add --follow-symlinks to --batch
Date: Fri, 8 May 2015 14:13:39 -0400 [thread overview]
Message-ID: <1431108819-6831-3-git-send-email-dturner@twopensource.com> (raw)
In-Reply-To: <1431108819-6831-1-git-send-email-dturner@twopensource.com>
From: David Turner <dturner@twitter.com>
This wires the in-repo-symlink following code through to the cat-file
builtin. In the event of an out-of-repo link, cat-file will print
the link in a new format.
Signed-off-by: David Turner <dturner@twitter.com>
---
Documentation/git-cat-file.txt | 28 ++++++-
builtin/cat-file.c | 23 +++++-
t/t1006-cat-file.sh | 184 +++++++++++++++++++++++++++++++++++++++++
3 files changed, 230 insertions(+), 5 deletions(-)
diff --git a/Documentation/git-cat-file.txt b/Documentation/git-cat-file.txt
index f6a16f4..18b67a3 100644
--- a/Documentation/git-cat-file.txt
+++ b/Documentation/git-cat-file.txt
@@ -10,7 +10,7 @@ SYNOPSIS
--------
[verse]
'git cat-file' (-t | -s | -e | -p | <type> | --textconv ) <object>
-'git cat-file' (--batch | --batch-check) < <list-of-objects>
+'git cat-file' (--batch | --batch-check) [--follow-symlinks] < <list-of-objects>
DESCRIPTION
-----------
@@ -69,6 +69,19 @@ OPTIONS
not be combined with any other options or arguments. See the
section `BATCH OUTPUT` below for details.
+--follow-symlinks::
+ Follow symlinks inside the repository. Instead of providing
+ output about the link itself, provide output about the linked-to
+ object. This option requires --batch or --batch-check. In the
+ event of a symlink loop (or more than 40 symlinks in a symlink
+ resolution chain), the file will be treated as missing. If a
+ symlink points outside the repository (e.g. a link to /foo or a
+ root-level link to ../foo), the portion of the link which is
+ outside the repository will be printed. Follow-symlinks will
+ be silently turned off if <object> specifies an object in the
+ index rather than one in the object database.
+
+
OUTPUT
------
If '-t' is specified, one of the <type>.
@@ -148,6 +161,19 @@ the repository, then `cat-file` will ignore any custom format and print:
<object> SP missing LF
------------
+If --follow-symlinks is used, and a symlink in the repository points
+outside the repository, then `cat-file` will ignore any custom format
+and print:
+
+------------
+symlink SP <size> LF <symlink> LF
+------------
+
+The symlink will either be absolute (beginning with a /), or relative
+to the repository root. For instance, if dir/link points to ../../foo,
+then <symlink> will be ../foo. <size> is the size of the symlink in
+bytes.
+
CAVEATS
-------
diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index df99df4..277af32 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -224,6 +224,7 @@ static void print_object_or_die(int fd, struct expand_data *data)
struct batch_options {
int enabled;
+ int follow_symlinks;
int print_contents;
const char *format;
};
@@ -232,16 +233,24 @@ static int batch_one_object(const char *obj_name, struct batch_options *opt,
struct expand_data *data)
{
struct strbuf buf = STRBUF_INIT;
+ struct object_context ctx;
+ int flags = opt->follow_symlinks ? GET_SHA1_FOLLOW_SYMLINKS : 0;
if (!obj_name)
return 1;
- if (get_sha1(obj_name, data->sha1)) {
+ if (get_sha1_with_context(obj_name, flags, data->sha1, &ctx)) {
printf("%s missing\n", obj_name);
fflush(stdout);
return 0;
}
+ if (ctx.mode == 0) {
+ printf("symlink %"PRIuMAX"\n%s\n", (uintmax_t)strlen(ctx.path),
+ ctx.path);
+ return 0;
+ }
+
if (sha1_object_info_extended(data->sha1, &data->info, LOOKUP_REPLACE_OBJECT) < 0) {
printf("%s missing\n", obj_name);
fflush(stdout);
@@ -342,9 +351,8 @@ static int batch_option_callback(const struct option *opt,
{
struct batch_options *bo = opt->value;
- if (unset) {
- memset(bo, 0, sizeof(*bo));
- return 0;
+ if (bo->enabled) {
+ return 1;
}
bo->enabled = 1;
@@ -369,6 +377,9 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
OPT_SET_INT('p', NULL, &opt, N_("pretty-print object's content"), 'p'),
OPT_SET_INT(0, "textconv", &opt,
N_("for blob objects, run textconv on object's content"), 'c'),
+ OPT_SET_INT(0, "follow-symlinks", &batch.follow_symlinks,
+ N_("Follow in-repo symlinks; report out-of-repo symlinks (requires --batch or --batch-check)"),
+ 1),
{ OPTION_CALLBACK, 0, "batch", &batch, "format",
N_("show info and content of objects fed from the standard input"),
PARSE_OPT_OPTARG, batch_option_callback },
@@ -402,6 +413,10 @@ int cmd_cat_file(int argc, const char **argv, const char *prefix)
usage_with_options(cat_file_usage, options);
}
+ if (batch.follow_symlinks && !batch.enabled) {
+ usage_with_options(cat_file_usage, options);
+ }
+
if (batch.enabled)
return batch_objects(&batch);
diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh
index ab36b1e..a9ef3a6 100755
--- a/t/t1006-cat-file.sh
+++ b/t/t1006-cat-file.sh
@@ -189,6 +189,13 @@ do
'
done
+for opt in t s e p
+do
+ test_expect_success "Passing -$opt with --follow-symlinks fails" '
+ test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1
+ '
+done
+
test_expect_success "--batch-check for a non-existent named object" '
test "foobar42 missing
foobar84 missing" = \
@@ -296,4 +303,181 @@ test_expect_success '%(deltabase) reports packed delta bases' '
}
'
+# Tests for git cat-file --follow-symlinks
+test_expect_success 'prep for symlink tests' '
+ echo_without_newline "$hello_content" > morx &&
+ ln -s morx same-dir-link &&
+ ln -s ../fleem out-of-repo-link &&
+ ln -s .. out-of-repo-link-dir &&
+ ln -s same-dir-link link-to-link &&
+ ln -s nope broken-same-dir-link &&
+ mkdir dir &&
+ ln -s ../morx dir/parent-dir-link &&
+ ln -s .. dir/link-dir &&
+ ln -s ../../escape dir/out-of-repo-link &&
+ ln -s ../.. dir/out-of-repo-link-dir &&
+ ln -s nope dir/broken-link-in-dir &&
+ mkdir dir/subdir &&
+ ln -s ../../morx dir/subdir/grandparent-dir-link &&
+ ln -s ../../../great-escape dir/subdir/out-of-repo-link &&
+ ln -s ../../.. dir/subdir/out-of-repo-link-dir &&
+ ln -s ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
+ ln -s ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
+ echo_without_newline "$hello_content" >dir/subdir/ind2 &&
+ echo_without_newline "$hello_content" >dir/ind1 &&
+ ln -s dir dirlink &&
+ ln -s dir/subdir subdirlink &&
+ ln -s subdir/ind2 dir/link-to-child &&
+ ln -s dir/link-to-child link-to-down-link &&
+ ln -s dir/.. up-down &&
+ ln -s dir/../ up-down-trailing &&
+ ln -s dir/../morx up-down-file &&
+ ln -s dir/../../morx up-up-down-file &&
+ ln -s subdirlink/../../morx up-two-down-file &&
+ ln -s loop1 loop2 &&
+ ln -s loop2 loop1 &&
+ git add . &&
+ git commit -am "test"
+'
+
+echo $hello_sha1 blob $hello_size > found
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' '
+ echo HEAD:morx | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp found actual &&
+ echo HEAD:nope missing > expect &&
+ echo HEAD:nope | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
+ echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp found actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
+ echo HEAD:broken-same-dir-link missing > expect &&
+ echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
+ echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp found actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
+ echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp found actual &&
+ echo HEAD:dir/parent-dir-link/nope missing > expect &&
+ echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' '
+ echo HEAD:dir/link-dir/nope missing > expect &&
+ echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual &&
+ echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp found actual &&
+ echo HEAD:dir/broken-link-in-dir missing > expect &&
+ echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' '
+ echo HEAD:dir/subdir/grandparent-dir-link/nope missing > expect &&
+ echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual &&
+ echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp found actual &&
+ echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp found actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' '
+ echo HEAD:dirlink/morx missing > expect &&
+ echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual &&
+ echo $hello_sha1 blob $hello_size > expect &&
+ echo HEAD:dirlink/ind1 | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/subdir links' '
+ echo HEAD:subdirlink/morx missing > expect &&
+ echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual &&
+ echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp found actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir -> subdir links' '
+ echo HEAD:dir/link-to-child/morx missing > expect &&
+ echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual &&
+ echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp found actual &&
+ echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp found actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
+ echo symlink 8 > expect &&
+ echo ../fleem >> expect &&
+ echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual &&
+ echo symlink 2 > expect &&
+ echo .. >> expect &&
+ echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
+ echo symlink 9 > expect &&
+ echo ../escape >> expect &&
+ echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual &&
+ echo symlink 2 > expect &&
+ echo .. >> expect &&
+ echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
+ echo symlink 15 > expect &&
+ echo ../great-escape >> expect &&
+ echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual &&
+ echo symlink 2 > expect &&
+ echo .. >> expect &&
+ echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual &&
+ echo symlink 3 > expect &&
+ echo ../ >> expect &&
+ echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
+ echo HEAD: | git cat-file --batch-check > expect &&
+ echo HEAD:up-down | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual &&
+ echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual &&
+ echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp found actual &&
+ echo symlink 7 > expect &&
+ echo ../morx >> expect &&
+ echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual &&
+ echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp found actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' '
+ echo HEAD:loop1 missing > expect &&
+ echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks > actual &&
+ test_cmp expect actual
+'
test_done
--
2.0.4.315.gad8727a-twtrsrc
next prev parent reply other threads:[~2015-05-08 18:14 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-08 18:13 [PATCH 1/3] tree-walk: learn get_tree_enty_follow_symlinks dturner
2015-05-08 18:13 ` [PATCH 2/3] sha1_name: get_sha1_with_context learns to follow symlinks dturner
2015-05-08 19:45 ` Eric Sunshine
2015-05-08 20:17 ` Junio C Hamano
2015-05-08 20:25 ` Junio C Hamano
2015-05-08 20:39 ` Jeff King
2015-05-08 21:22 ` Junio C Hamano
2015-05-08 20:27 ` David Turner
2015-05-08 20:38 ` Philip Oakley
2015-05-08 21:31 ` Junio C Hamano
2015-05-08 18:13 ` dturner [this message]
2015-05-08 19:51 ` [PATCH 3/3] cat-file: add --follow-symlinks to --batch Eric Sunshine
2015-05-08 19:26 ` [PATCH 1/3] tree-walk: learn get_tree_enty_follow_symlinks Junio C Hamano
2015-05-08 20:02 ` David Turner
2015-05-08 19:43 ` Eric Sunshine
-- strict thread matches above, loose matches on Subject: below --
2015-05-13 18:21 [PATCH v8 0/3] git cat-file --follow-symlinks David Turner
2015-05-13 18:21 ` [PATCH 3/3] cat-file: add --follow-symlinks to --batch David Turner
2015-05-13 18:23 [PATCH v9 0/3] David Turner
2015-05-13 18:23 ` [PATCH 3/3] cat-file: add --follow-symlinks to --batch David Turner
2015-05-14 20:17 [PATCH v10 0/3] David Turner
2015-05-14 20:17 ` [PATCH 3/3] cat-file: add --follow-symlinks to --batch David Turner
2015-05-14 20:38 [PATCH v11 0/3] cat-file --follow-symlinks David Turner
2015-05-14 20:38 ` [PATCH 3/3] cat-file: add --follow-symlinks to --batch David Turner
2015-05-14 21:58 [PATCH v12 0/3] git cat-file --follow-symlinks David Turner
2015-05-14 21:58 ` [PATCH 3/3] cat-file: add --follow-symlinks to --batch David Turner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1431108819-6831-3-git-send-email-dturner@twopensource.com \
--to=dturner@twopensource.com \
--cc=dturner@twitter.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).