Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.kernel.org/pub/scm/git/git.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2021-10-12 23:51:35 +0300
committerJunio C Hamano <gitster@pobox.com>2021-10-12 23:51:36 +0300
commit0a15e94e1065419da3ad1bab3b79501a90cf2777 (patch)
treef5b46f033fcc68c01b41b234debf8695ce566f11
parenta02d78c4aef0b2268846b8aebab1b93788e18967 (diff)
parent561fa03529202a36e0d77548fdcb5d81422c1865 (diff)
Merge branch 'ab/pack-stdin-packs-fix' into maint
Input validation of "git pack-objects --stdin-packs" has been corrected. * ab/pack-stdin-packs-fix: pack-objects: fix segfault in --stdin-packs option pack-objects tests: cover blindspots in stdin handling
-rw-r--r--builtin/pack-objects.c23
-rwxr-xr-xt/t5300-pack-object.sh104
2 files changed, 124 insertions, 3 deletions
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index de00adbb9e..df49f656b9 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -3311,9 +3311,26 @@ static void read_packs_list_from_stdin(void)
}
/*
- * First handle all of the excluded packs, marking them as kept in-core
- * so that later calls to add_object_entry() discards any objects that
- * are also found in excluded packs.
+ * Arguments we got on stdin may not even be packs. First
+ * check that to avoid segfaulting later on in
+ * e.g. pack_mtime_cmp(), excluded packs are handled below.
+ *
+ * Since we first parsed our STDIN and then sorted the input
+ * lines the pack we error on will be whatever line happens to
+ * sort first. This is lazy, it's enough that we report one
+ * bad case here, we don't need to report the first/last one,
+ * or all of them.
+ */
+ for_each_string_list_item(item, &include_packs) {
+ struct packed_git *p = item->util;
+ if (!p)
+ die(_("could not find pack '%s'"), item->string);
+ }
+
+ /*
+ * Then, handle all of the excluded packs, marking them as
+ * kept in-core so that later calls to add_object_entry()
+ * discards any objects that are also found in excluded packs.
*/
for_each_string_list_item(item, &exclude_packs) {
struct packed_git *p = item->util;
diff --git a/t/t5300-pack-object.sh b/t/t5300-pack-object.sh
index 5c5e53f0be..e13a884207 100755
--- a/t/t5300-pack-object.sh
+++ b/t/t5300-pack-object.sh
@@ -34,6 +34,110 @@ test_expect_success 'setup' '
} >expect
'
+test_expect_success 'setup pack-object <stdin' '
+ git init pack-object-stdin &&
+ test_commit -C pack-object-stdin one &&
+ test_commit -C pack-object-stdin two
+
+'
+
+test_expect_success 'pack-object <stdin parsing: basic [|--revs]' '
+ cat >in <<-EOF &&
+ $(git -C pack-object-stdin rev-parse one)
+ EOF
+
+ git -C pack-object-stdin pack-objects basic-stdin <in &&
+ idx=$(echo pack-object-stdin/basic-stdin-*.idx) &&
+ git show-index <"$idx" >actual &&
+ test_line_count = 1 actual &&
+
+ git -C pack-object-stdin pack-objects --revs basic-stdin-revs <in &&
+ idx=$(echo pack-object-stdin/basic-stdin-revs-*.idx) &&
+ git show-index <"$idx" >actual &&
+ test_line_count = 3 actual
+'
+
+test_expect_success 'pack-object <stdin parsing: [|--revs] bad line' '
+ cat >in <<-EOF &&
+ $(git -C pack-object-stdin rev-parse one)
+ garbage
+ $(git -C pack-object-stdin rev-parse two)
+ EOF
+
+ sed "s/^> //g" >err.expect <<-EOF &&
+ fatal: expected object ID, got garbage:
+ > garbage
+
+ EOF
+ test_must_fail git -C pack-object-stdin pack-objects bad-line-stdin <in 2>err.actual &&
+ test_cmp err.expect err.actual &&
+
+ cat >err.expect <<-EOF &&
+ fatal: bad revision '"'"'garbage'"'"'
+ EOF
+ test_must_fail git -C pack-object-stdin pack-objects --revs bad-line-stdin-revs <in 2>err.actual &&
+ test_cmp err.expect err.actual
+'
+
+test_expect_success 'pack-object <stdin parsing: [|--revs] empty line' '
+ cat >in <<-EOF &&
+ $(git -C pack-object-stdin rev-parse one)
+
+ $(git -C pack-object-stdin rev-parse two)
+ EOF
+
+ sed -e "s/^> //g" -e "s/Z$//g" >err.expect <<-EOF &&
+ fatal: expected object ID, got garbage:
+ > Z
+
+ EOF
+ test_must_fail git -C pack-object-stdin pack-objects empty-line-stdin <in 2>err.actual &&
+ test_cmp err.expect err.actual &&
+
+ git -C pack-object-stdin pack-objects --revs empty-line-stdin-revs <in &&
+ idx=$(echo pack-object-stdin/empty-line-stdin-revs-*.idx) &&
+ git show-index <"$idx" >actual &&
+ test_line_count = 3 actual
+'
+
+test_expect_success 'pack-object <stdin parsing: [|--revs] with --stdin' '
+ cat >in <<-EOF &&
+ $(git -C pack-object-stdin rev-parse one)
+ $(git -C pack-object-stdin rev-parse two)
+ EOF
+
+ # There is the "--stdin-packs is incompatible with --revs"
+ # test below, but we should make sure that the revision.c
+ # --stdin is not picked up
+ cat >err.expect <<-EOF &&
+ fatal: disallowed abbreviated or ambiguous option '"'"'stdin'"'"'
+ EOF
+ test_must_fail git -C pack-object-stdin pack-objects stdin-with-stdin-option --stdin <in 2>err.actual &&
+ test_cmp err.expect err.actual &&
+
+ test_must_fail git -C pack-object-stdin pack-objects --stdin --revs stdin-with-stdin-option-revs 2>err.actual <in &&
+ test_cmp err.expect err.actual
+'
+
+test_expect_success 'pack-object <stdin parsing: --stdin-packs handles garbage' '
+ cat >in <<-EOF &&
+ $(git -C pack-object-stdin rev-parse one)
+ $(git -C pack-object-stdin rev-parse two)
+ EOF
+
+ # That we get "two" and not "one" has to do with OID
+ # ordering. It happens to be the same here under SHA-1 and
+ # SHA-256. See commentary in pack-objects.c
+ cat >err.expect <<-EOF &&
+ fatal: could not find pack '"'"'$(git -C pack-object-stdin rev-parse two)'"'"'
+ EOF
+ test_must_fail git \
+ -C pack-object-stdin \
+ pack-objects stdin-with-stdin-option --stdin-packs \
+ <in 2>err.actual &&
+ test_cmp err.expect err.actual
+'
+
# usage: check_deltas <stderr_from_pack_objects> <cmp_op> <nr_deltas>
# e.g.: check_deltas stderr -gt 0
check_deltas() {