Skip to contentSkip to conversation

archive: learn to include submodules in output archive

PATCH In review Nikhil Benesch 3 participants 6 messages Mar 12, 2017 – Mar 13, 2017 Documentation

lore
Share & tools
lore
#0 lore raw

cover archive: learn to include submodules in output archive

Reviving an old patch from Lars Hjemli to teach git archive to --recurse-submodules, which is, in my opinion, a sorely needed feature. I'm afraid this is the first patch I've submitted for review, so I wasn't sure who to CC. Apologies if I've gotten the wrong folks.

Nikhil Benesch (1):
archive: learn to include submodules in output archive

 Documentation/git-archive.txt |   8 ++-
 archive.c                     |  22 +++++----
 archive.h                     |   1 +
 submodule.c                   |   2 +-
 submodule.h                   |   1 +
 t/t5005-archive-submodules.sh | 112 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 133 insertions(+), 13 deletions(-)
 create mode 100755 t/t5005-archive-submodules.sh
Signature · 1 line
2.12.0.244.g625568cd8.dirty
#1 lore raw

PATCH 1/1 archive: learn to include submodules in output archive

This commit is a revival of Lars Hjemli's 2009 patch to provide an option to include submodules in the output of git archive.

The --recurse-submodules option (named consistently with fetch, clone, and ls-files) will recursively traverse submodules in the repository and consider their contents for inclusion in the output archive, subject to any pathspec filters. Like other commands that have learned --recurse-submodules, submodules that have not been checked out will not be traversed.

---
 Documentation/git-archive.txt |   8 ++-
 archive.c                     |  22 +++++----
 archive.h                     |   1 +
 submodule.c                   |   2 +-
 submodule.h                   |   1 +
 t/t5005-archive-submodules.sh | 112 ++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 133 insertions(+), 13 deletions(-)
 create mode 100755 t/t5005-archive-submodules.sh
Documentation/git-archive.txt +6 -2
index cfa1e4ebe..f223f9e05 100644
@@ -11,8 +11,8 @@ SYNOPSIS
 [verse]
 'git archive' [--format=<fmt>] [--list] [--prefix=<prefix>/] [<extra>]
 	      [-o <file> | --output=<file>] [--worktree-attributes]
-	      [--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish>
-	      [<path>...]
+	      [--recurse-submodules] [--remote=<repo> [--exec=<git-upload-archive>]]
+	      <tree-ish> [<path>...]
 
 DESCRIPTION
 -----------
@@ -59,6 +59,10 @@ OPTIONS
 	Look for attributes in .gitattributes files in the working tree
 	as well (see <<ATTRIBUTES>>).
 
+--recurse-submodules::
+	Recursively include the contents of any checked-out submodules in
+	the archive.
+
 <extra>::
 	This can be any options that the archiver backend understands.
 	See next section.
archive.c +12 -10
index 60b889198..8d060bad3 100644
@@ -7,6 +7,7 @@
 #include "parse-options.h"
 #include "unpack-trees.h"
 #include "dir.h"
+#include "submodule.h"
 
 static char const * const archive_usage[] = {
 	N_("git archive [<options>] <tree-ish> [<path>...]"),
@@ -132,18 +133,15 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
 		args->convert = ATTR_TRUE(check->items[1].value);
 	}
 
-	if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
-		if (args->verbose)
-			fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
-		err = write_entry(args, sha1, path.buf, path.len, mode);
-		if (err)
-			return err;
-		return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
-	}
-
 	if (args->verbose)
 		fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
-	return write_entry(args, sha1, path.buf, path.len, mode);
+	err = write_entry(args, sha1, path.buf, path.len, mode);
+	if (err)
+		return err;
+	if (S_ISDIR(mode) || (S_ISGITLINK(mode) && args->recurse_submodules &&
+			      !add_submodule_odb(path_without_prefix)))
+		return READ_TREE_RECURSIVE;
+	return 0;
 }
 
 static int write_archive_entry_buf(const unsigned char *sha1, struct strbuf *base,
@@ -411,6 +409,7 @@ static int parse_archive_args(int argc, const char **argv,
 	int verbose = 0;
 	int i;
 	int list = 0;
+	int recurse_submodules = 0;
 	int worktree_attributes = 0;
 	struct option opts[] = {
 		OPT_GROUP(""),
@@ -419,6 +418,8 @@ static int parse_archive_args(int argc, const char **argv,
 			N_("prepend prefix to each pathname in the archive")),
 		OPT_STRING('o', "output", &output, N_("file"),
 			N_("write the archive to this file")),
+		OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
+			N_("recurse through submodules")),
 		OPT_BOOL(0, "worktree-attributes", &worktree_attributes,
 			N_("read .gitattributes in working directory")),
 		OPT__VERBOSE(&verbose, N_("report archived files on stderr")),
@@ -484,6 +485,7 @@ static int parse_archive_args(int argc, const char **argv,
 		}
 	}
 	args->verbose = verbose;
+	args->recurse_submodules = recurse_submodules;
 	args->base = base;
 	args->baselen = strlen(base);
 	args->worktree_attributes = worktree_attributes;
archive.h +1 -0
index 415e0152e..96e217ac5 100644
@@ -12,6 +12,7 @@ struct archiver_args {
 	time_t time;
 	struct pathspec pathspec;
 	unsigned int verbose : 1;
+	unsigned int recurse_submodules : 1;
 	unsigned int worktree_attributes : 1;
 	unsigned int convert : 1;
 	int compression_level;
submodule.c +1 -1
index 3b98766a6..5fe5a3a8e 100644
@@ -121,7 +121,7 @@ void stage_updated_gitmodules(void)
 		die(_("staging updated .gitmodules failed"));
 }
 
-static int add_submodule_odb(const char *path)
+int add_submodule_odb(const char *path)
 {
 	struct strbuf objects_directory = STRBUF_INIT;
 	int ret = 0;
submodule.h +1 -0
index 05ab674f0..d59fd2537 100644
@@ -35,6 +35,7 @@ extern int is_staging_gitmodules_ok(void);
 extern int update_path_in_gitmodules(const char *oldpath, const char *newpath);
 extern int remove_path_from_gitmodules(const char *path);
 extern void stage_updated_gitmodules(void);
+extern int add_submodule_odb(const char *path);
 extern void set_diffopt_flags_from_submodule_config(struct diff_options *,
 		const char *path);
 extern int submodule_config(const char *var, const char *value, void *cb);
t/t5005-archive-submodules.sh new file +112 -0

112 lines not on this page · raw message

Signature · 1 line
2.12.0.244.g625568cd8.dirty
#2 lore raw
Quoted 10 lines · Nikhil Benesch

This commit is a revival of Lars Hjemli's 2009 patch to provide an option to include submodules in the output of git archive.

The --recurse-submodules option (named consistently with fetch, clone, and ls-files) will recursively traverse submodules in the repository and consider their contents for inclusion in the output archive, subject to any pathspec filters. Like other commands that have learned --recurse-submodules, submodules that have not been checked out will not be traversed.

Quoted 2 lines

---

I'll let others comment on the proposed log message.

Documentation/git-archive.txt SYNOPSIS · line 15

Quoted 12 lines
Documentation/git-archive.txt +2 -2
index cfa1e4ebe..f223f9e05 100644
@@ -11,8 +11,8 @@ SYNOPSIS
 [verse]
 'git archive' [--format=<fmt>] [--list] [--prefix=<prefix>/] [<extra>]
 	      [-o <file> | --output=<file>] [--worktree-attributes]
-	      [--remote=<repo> [--exec=<git-upload-archive>]] <tree-ish>
-	      [<path>...]
+	      [--recurse-submodules] [--remote=<repo> [--exec=<git-upload-archive>]]
+	      <tree-ish> [<path>...]

With the reflowing of lines it was not immediately obvious, but the only change is to addd [--recurse-submodules] in front of the existing [--remote=<repo>] option. It would have probably made more sense to add new things at the end, just because these options can be given in any order.

Documentation/git-archive.txt OPTIONS · line 64

Quoted 3 lines
Documentation/git-archive.txt +3 -0
+--recurse-submodules::
+	Recursively include the contents of any checked-out submodules in
+	the archive.

OK.

A question to the submodule folks: Is "checked-out" the right terminology in this context? I am wondering if we have reached more official set of words to express submodule states discussed in [*1*].

Quoted 9 lines
Documentation/git-archive.txt +0 -9
-	if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
-		if (args->verbose)
-			fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
-		err = write_entry(args, sha1, path.buf, path.len, mode);
-		if (err)
-			return err;
-		return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
-	}
-

Documentation/git-archive.txt SYNOPSIS

Quoted 10 lines
Documentation/git-archive.txt +7 -1
 	if (args->verbose)
 		fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
-	return write_entry(args, sha1, path.buf, path.len, mode);
+	err = write_entry(args, sha1, path.buf, path.len, mode);
+	if (err)
+		return err;
+	if (S_ISDIR(mode) || (S_ISGITLINK(mode) && args->recurse_submodules &&
+			      !add_submodule_odb(path_without_prefix)))
+		return READ_TREE_RECURSIVE;
+	return 0;

So, we used to (and without the option we still) stop recursing when we see a gitlink after asking write_entry() to write the path itself out, but with the option, we keep going.

How does pathspec limiting work with this codepath? If you have a superproject with a regular file "sub/README" that belongs to the superproject itself, and a submodule at "sub/module/", what makes sure that this command

    $ git archive --recurse-submodules HEAD -- 'sub/R*' 'sub/module/lib/'

looks only in 'lib/' part of the submodule?

Quoted 6 lines
t/t5005-archive-submodules.sh new file +0 -0
new file mode 100755
index 000000000..747e38627

t/t5005-archive-submodules.sh line 8

Quoted 8 lines
t/t5005-archive-submodules.sh new file +8 -0
@@ -0,0 +1,112 @@
+#!/bin/sh
+
+test_description='git archive can include submodule content'
+
+. ./test-lib.sh
+
+add_file()
+{

Style. A shell function begins like so (see Documentation/CodingGuidelines):

	add_file () {

t/t5005-archive-submodules.sh line 9

Quoted 1 line
t/t5005-archive-submodules.sh new file +1 -0
+	git add $1 &&

Put dq around "$1" to make it easier to reuse, even if you know you only happen to use pathnames without any $IFS whitespace characters.

I think all of these comments apply to the other shell function you added to this file..

t/t5005-archive-submodules.sh line 33

Quoted 10 lines
t/t5005-archive-submodules.sh new file +10 -0
+test_expect_success 'by default, submodules are not included' '
+	echo "File 1" >1 &&
+	add_file 1 &&
+	add_submodule 2 3 &&
+	add_submodule 4 5 &&
+	cat <<EOF >expected &&
+1
+2/
+4/
+EOF

Use <<-EOF to allow you to indent your here-doc data. Also when your here-doc data does not need any $variable_interpolation, quote the end marker to help reduce reviewer's mental burden, i.e.

	cat <<-\EOF >expected &&
	1
	2/
	4/
	EOF

The same comment applies to other tests (I won't even repeat this).

t/t5005-archive-submodules.sh line 35

Quoted 2 lines
t/t5005-archive-submodules.sh new file +2 -0
+	git archive HEAD >normal.tar &&
+	tar -tf normal.tar >actual &&

Lose "-" from "-tf"; that's the canonical and the most portable way to spell the option to "tar".

t/t5005-archive-submodules.sh line 75

Quoted 7 lines
t/t5005-archive-submodules.sh new file +7 -0
+test_expect_success 'packed submodules are supported' '
+	msg=$(cd 2 && git repack -ad && git count-objects) &&
+	test "$msg" = "0 objects, 0 kilobytes" &&
+	git archive --recurse-submodules HEAD >packed.tar &&
+	tar -tf packed.tar >actual &&
+	test_cmp expected actual
+'

I am not sure how packing would be expected to break anything, but an extra test may not hurt.

I notice that you only test the case where the submodule's ".git" is embedded in its working tree (as opposed to ".git" being a "gitdir:" file that points at the real location of the repository, somewhere inside ".git/modules/" of the superproject). You probably want to make sure that also works, too.

I also notice that you do not use any pathspec in your "git archive" invocation. You'd want to make sure that works as expected.

Thanks.

[Reference]

*1* <[email protected]>

#3 lore raw
In reply to Nikhil Benesch

Welcome to the Git community!

Quoted 2 lines · Nikhil Benesch

This commit is a revival of Lars Hjemli's 2009 patch to provide an option to include submodules in the output of git archive.

I am unaware of said patch, could you link to it, e.g. on https://public-inbox.org/git/ ?

Quoted 3 lines

The --recurse-submodules option (named consistently with fetch, clone, and ls-files)

... and unlike fetch and push we do not need to introduce extra options? (or they can be added later when the need arises)

Quoted 3 lines

will recursively traverse submodules in the repository and consider their contents for inclusion in the output archive, subject to any pathspec filters.

ok.

Quoted 3 lines

Like other commands that have learned --recurse-submodules, submodules that have not been checked out will not be traversed.

Quoted 3 lines · Junio

A question to the submodule folks: Is "checked-out" the right terminology in this context? I am wondering if we have reached more official set of words to express submodule states discussed in [*1*].

http://public-inbox.org/git/[email protected]/

The "checked-out" here would translate to "populated" in said proposal.

I guess that is sufficient for the first implementation, but eventually we might be interested in "recurse-submodules=active/init" as well. Consider the case, where you delete a submodule and commit it. Then you still want to be able to create an archive for HEAD^ (with submodules). So in that case we'd want to recurse into any submodule that has a git directory with the commit as recorded by the superproject, i.e. the example given would refer to "depopulated" in the referenced proposal.

When the initialization is missing, we may not be interested in that submodule any more, but we don't know for the user, so a user may want to ask for "archive --recurse-submodules={all / initialized-only / all-submoduless-with-git-dir, none, working-tree}". No need to add all these options in the first patch, but keep that in mind for future extensions.

Documentation/git-archive.txt OPTIONS · line 65

Quoted 8 lines
Documentation/git-archive.txt +4 -0
@@ -59,6 +59,10 @@ OPTIONS
        Look for attributes in .gitattributes files in the working tree
        as well (see <<ATTRIBUTES>>).
 
+--recurse-submodules::
+       Recursively include the contents of any checked-out submodules in
+       the archive.
+

Here is "any", in the commit message we had "subject to any pathspec filters."

Quoted 11 lines
archive.c +0 -7
@@ -132,18 +133,15 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
                args->convert = ATTR_TRUE(check->items[1].value);
        }
 
-       if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
-               if (args->verbose)
-                       fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
-               err = write_entry(args, sha1, path.buf, path.len, mode);
-               if (err)
-                       return err;
-               return (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);

archive.c static int write_archive_entry(const unsigned char *sha1, const char *base, · line 142

Quoted 10 lines
archive.c +5 -3
-       }
-
        if (args->verbose)
                fprintf(stderr, "%.*s\n", (int)path.len, path.buf);
-       return write_entry(args, sha1, path.buf, path.len, mode);
+       err = write_entry(args, sha1, path.buf, path.len, mode);
+       if (err)
+               return err;
+       if (S_ISDIR(mode) || (S_ISGITLINK(mode) && args->recurse_submodules &&
+                             !add_submodule_odb(path_without_prefix)))

This is a bit hard to read. Maybe reformatting can help

     if (S_ISDIR(mode) ||
        (S_ISGITLINK(mode) &&
         args->recurse_submodules &&
         !add_submodule_odb(path_without_prefix)))
        return ...

Though I wonder if we need to special case the
return value of add_submodule_odb as that could
be an error instead of fall through "return 0;" ?

archive.c static int parse_archive_args(int argc, const char **argv, · line 422

Quoted 6 lines
archive.c +2 -0
@@ -419,6 +418,8 @@ static int parse_archive_args(int argc, const char **argv,
                        N_("prepend prefix to each pathname in the archive")),
                OPT_STRING('o', "output", &output, N_("file"),
                        N_("write the archive to this file")),
+               OPT_BOOL(0, "recurse-submodules", &recurse_submodules,
+                       N_("recurse through submodules")),

This is the first time hearing "through" used with submodules. I guess it makes sense when looking at the contents on disk as a tree data structure.

push:    N_("control recursive pushing of submodules"),
fetch:    N_("control recursive fetching of submodules"),
grep:    N_("recursivley search in each submodule")),
ls-files:    N_("recurse through submodules")),

Ok, we introduced recursing "through" submodules with ls-files.

...

t/t5005-archive-submodules.sh line 19

Quoted 8 lines
t/t5005-archive-submodules.sh new file +8 -0
+
+add_submodule()
+{
+       mkdir $1 && (
+               cd $1 &&
+               git init &&
+               echo "File $2" >$2 &&
+               add_file $2

test_create_repo / test_commit
might come in handy here as well.

t/t5005-archive-submodules.sh line 24

Quoted 5 lines
t/t5005-archive-submodules.sh new file +5 -0
+       ) &&
+       add_file $1
+}
+
+test_expect_success 'by default, submodules are not included' '

This test case may be covered elsewhere (in the default archive tests) already, though not spelled out as such?

Review

Quoted 2 lines
(quoted diff) +2 -0
+test_expect_success 'with --recurse-submodules, checked out submodules are included' '
+       cat <<EOF >expected &&

In the modern parts of Git we indent the content of the here-doc-text (If the operator is “<<-” instead of “<<”, then leading tabs in the here-doc-text are stripped.)
i.e. cat <<-\EOF >expect
well indented text
that doesn't break visual alignment
of tests.
EOF
git archive ...

There are a lot of old tests still out there, so it is easy to find them for guidance first. :/

Quoted 1 line

+test_expect_success 'submodules in submodules are supported' '

cool! Usually these are referred to as "nested submodules" in case you want to shorten the description.

Quoted 1 line

+test_expect_success 'packed submodules are supported' '

Not sure what we try to test here? The internal representation of the submodule ought to not matter.

t/t5005-archive-submodules.sh line 71

Quoted 2 lines
t/t5005-archive-submodules.sh new file +2 -0
+       msg=$(cd 2 && git repack -ad && git count-objects) &&
+       test "$msg" = "0 objects, 0 kilobytes" &&

I read that in submodule tests before; t7408 that tests if submodules with alternates are useful. I am not convinced we need this test here?

Quoted 1 line

+test_expect_success 'pathspecs supported' '

..

Quoted 1 line

+ git archive --recurse-submodules HEAD >recursive.tar &&

The test ought to test that Git can use pathspecs ...

Quoted 1 line

+ tar -tf recursive.tar 4/6/7 2/3 >actual &&

... unlike tar. We consider all external tools to be perfect within the git test suite. So we rather want to test for the absence of paths, that were not given as a pathspec to the git archive command.

Quoted 1 line

+test_expect_success 'missing submodule packs triggers an error' '

cool!

Quoted 1 line

+ mv 2/.git/objects/pack .git/packdir2 &&

Uh :/ please do not assume we have the submodule at <path>/.git. See 293ab15ee (submodule: teach rm to remove submodules unless they contain a git directory, 2012) for example.

Quoted 1 line

+ test_must_fail git archive --recurse-submodules HEAD

Do we want to also check for a sensible error message here? e.g.

    test_must_fail git archive ... >out &&
    test_i18n_grep "<message>" out
Quoted 1 line

+test_expect_success '--recurse-submodules skips non-checked out submodules' '

I read that as "skips unpopulated" ...

Thanks,
Stefan

#4 lore raw
Quoted 3 lines · Stefan Beller
Quoted 3 lines

will recursively traverse submodules in the repository and consider their contents for inclusion in the output archive, subject to any pathspec filters.

git-archive pays attention to export-ignore and export-subst attribute as read from .gitattributes or $GIT_DIR/info/attributes

When recursing into submodules, we'd need to clarify if the attributes from the superproject or from the submodules are applied; or both.

Thanks,
Stefan

#5 lore raw
Quoted 11 lines · Stefan Beller
Quoted 3 lines · Stefan Beller
Quoted 3 lines

will recursively traverse submodules in the repository and consider their contents for inclusion in the output archive, subject to any pathspec filters.

git-archive pays attention to export-ignore and export-subst attribute as read from .gitattributes or $GIT_DIR/info/attributes

When recursing into submodules, we'd need to clarify if the attributes from the superproject or from the submodules are applied; or both.

I think the most natural expectation is for the output from the "archive --recurse-submodules" command is to be logical concatenation of "archive" run at the top-level and submodules, adjusting the output from the latter with leading paths to the submodules. For that to happen, the attributes that apply to paths inside a submodule must come from that submodule's setting.