git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Nikhil Benesch <nikhil.benesch@gmail.com>
To: git@vger.kernel.org
Cc: Lars Hjemli <hjemli@gmail.com>,
	Nikhil Benesch <nikhil.benesch@gmail.com>
Subject: [PATCH 1/1] archive: learn to include submodules in output archive
Date: Sun, 12 Mar 2017 03:54:04 -0400	[thread overview]
Message-ID: <20170312075404.23951-2-nikhil.benesch@gmail.com> (raw)
In-Reply-To: <20170312075404.23951-1-nikhil.benesch@gmail.com>

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.

Signed-off-by: Nikhil Benesch <nikhil.benesch@gmail.com>
---
 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

diff --git a/Documentation/git-archive.txt b/Documentation/git-archive.txt
index cfa1e4ebe..f223f9e05 100644
--- a/Documentation/git-archive.txt
+++ b/Documentation/git-archive.txt
@@ -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.
diff --git a/archive.c b/archive.c
index 60b889198..8d060bad3 100644
--- a/archive.c
+++ b/archive.c
@@ -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;
diff --git a/archive.h b/archive.h
index 415e0152e..96e217ac5 100644
--- a/archive.h
+++ b/archive.h
@@ -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;
diff --git a/submodule.c b/submodule.c
index 3b98766a6..5fe5a3a8e 100644
--- a/submodule.c
+++ b/submodule.c
@@ -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;
diff --git a/submodule.h b/submodule.h
index 05ab674f0..d59fd2537 100644
--- a/submodule.h
+++ b/submodule.h
@@ -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);
diff --git a/t/t5005-archive-submodules.sh b/t/t5005-archive-submodules.sh
new file mode 100755
index 000000000..747e38627
--- /dev/null
+++ b/t/t5005-archive-submodules.sh
@@ -0,0 +1,112 @@
+#!/bin/sh
+
+test_description='git archive can include submodule content'
+
+. ./test-lib.sh
+
+add_file()
+{
+	git add $1 &&
+	git commit -m "added $1"
+}
+
+add_submodule()
+{
+	mkdir $1 && (
+		cd $1 &&
+		git init &&
+		echo "File $2" >$2 &&
+		add_file $2
+	) &&
+	add_file $1
+}
+
+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
+	git archive HEAD >normal.tar &&
+	tar -tf normal.tar >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'with --recurse-submodules, checked out submodules are included' '
+	cat <<EOF >expected &&
+1
+2/
+2/3
+4/
+4/5
+EOF
+	git archive --recurse-submodules HEAD >full.tar &&
+	tar -tf full.tar >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'submodules in submodules are supported' '
+	(cd 4 && add_submodule 6 7) &&
+	add_file 4 &&
+	cat <<EOF >expected &&
+1
+2/
+2/3
+4/
+4/5
+4/6/
+4/6/7
+EOF
+	git archive --recurse-submodules HEAD >recursive.tar &&
+	tar -tf recursive.tar >actual &&
+	test_cmp expected actual
+'
+
+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
+'
+
+test_expect_success 'pathspecs supported' '
+	cat <<EOF >expected &&
+2/3
+4/6/7
+EOF
+	git archive --recurse-submodules HEAD >recursive.tar &&
+	tar -tf recursive.tar 4/6/7 2/3 >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'missing submodule packs triggers an error' '
+	mv 2/.git/objects/pack .git/packdir2 &&
+	test_must_fail git archive --recurse-submodules HEAD
+'
+
+test_expect_success '--recurse-submodules skips non-checked out submodules' '
+	cat <<EOF >expected &&
+1
+2/
+4/
+4/5
+4/6/
+4/6/7
+EOF
+	rm -rf 2/.git &&
+	git archive --recurse-submodules HEAD >partial.tar &&
+	tar -tf partial.tar >actual &&
+	test_cmp expected actual
+'
+
+test_expect_success 'missing objects in a submodule triggers an error' '
+	find 4/.git/objects -type f | xargs rm &&
+	test_must_fail git archive --recurse-submodules HEAD
+'
+
+test_done
-- 
2.12.0.244.g625568cd8.dirty


  reply	other threads:[~2017-03-12  7:54 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-12  7:54 [PATCH 0/1] archive: learn to include submodules in output archive Nikhil Benesch
2017-03-12  7:54 ` Nikhil Benesch [this message]
2017-03-13  0:36   ` [PATCH 1/1] " Junio C Hamano
2017-03-13 22:12   ` Stefan Beller
2017-03-13 22:57     ` Stefan Beller
2017-03-13 23:18       ` Junio C Hamano

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=20170312075404.23951-2-nikhil.benesch@gmail.com \
    --to=nikhil.benesch@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=hjemli@gmail.com \
    /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).