git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 5/8] clone: support sparse checkout with --sparse-checkout option

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Nov 30, 2008, 10:54 UTC
Message-ID
<1228042478-1886-6-git-send-email-pclouds@gmail.com>
In-Reply-To
<1228042478-1886-5-git-send-email-pclouds@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 Documentation/git-clone.txt |   10 +++++++++-
 builtin-clone.c             |   14 ++++++++++++++
 t/t5703-clone-narrow.sh     |   41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 64 insertions(+), 1 deletions(-)
 create mode 100755 t/t5703-clone-narrow.sh
diff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt
index 95f08b9..acdced2 100644
--- a/Documentation/git-clone.txt
+++ b/Documentation/git-clone.txt
@@ -12,7 +12,8 @@ SYNOPSIS
 'git clone' [--template=<template_directory>]
 	  [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]
 	  [-o <name>] [-u <upload-pack>] [--reference <repository>]
-	  [--depth <depth>] [--] <repository> [<directory>]
+	  [--depth <depth>] [-S|--sparse-checkout=<sparse patterns>] [--]
+	  <repository> [<directory>]
 
 DESCRIPTION
 -----------
@@ -99,6 +100,13 @@ then the cloned repository will become corrupt.
 -n::
 	No checkout of HEAD is performed after the clone is complete.
 
+-S=<sparse patterns>::
+--sparse-checkout=<sparse patterns>::
+	Make a sparse checkout instead of a full one.
+	This option will not work with either --no-checkout or --bare.
+	Please refer to linkgit:git-checkout[1] for more detail on
+	sparse checkout and sparse patterns.
+
 --bare::
 	Make a 'bare' GIT repository.  That is, instead of
 	creating `<directory>` and placing the administrative
diff --git a/builtin-clone.c b/builtin-clone.c
index 2feac9c..ea341a1 100644
--- a/builtin-clone.c
+++ b/builtin-clone.c
@@ -36,6 +36,7 @@ static const char * const builtin_clone_usage[] = {
 static int option_quiet, option_no_checkout, option_bare, option_mirror;
 static int option_local, option_no_hardlinks, option_shared;
 static char *option_template, *option_reference, *option_depth;
+static char *option_narrow_path;
 static char *option_origin = NULL;
 static char *option_upload_pack = "git-upload-pack";
 static int option_verbose;
@@ -45,6 +46,8 @@ static struct option builtin_clone_options[] = {
 	OPT__VERBOSE(&option_verbose),
 	OPT_BOOLEAN('n', "no-checkout", &option_no_checkout,
 		    "don't create a checkout"),
+	OPT_STRING('S', "sparse-checkout", &option_narrow_path, "sparse patterns",
+		   "only checkout certain files based on patterns"),
 	OPT_BOOLEAN(0, "bare", &option_bare, "create a bare repository"),
 	OPT_BOOLEAN(0, "naked", &option_bare, "create a bare repository"),
 	OPT_BOOLEAN(0, "mirror", &option_mirror,
@@ -383,10 +386,15 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 		if (option_origin)
 			die("--bare and --origin %s options are incompatible.",
 			    option_origin);
+		if (option_narrow_path)
+			die("--bare and --sparse-checkout options are incompatible.");
 		option_no_checkout = 1;
 		use_separate_remote = 0;
 	}
 
+	if (option_no_checkout && option_narrow_path)
+		die("--no-checkout and --sparse-checkout options are incompatible.");
+
 	if (!option_origin)
 		option_origin = "origin";
 
@@ -597,10 +605,16 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
 		opts.src_index = &the_index;
 		opts.dst_index = &the_index;
 
+		if (option_narrow_path) {
+			opts.new_narrow_path = 1;
+			opts.narrow_spec = parse_narrow_spec(option_narrow_path, NULL);
+		}
+
 		tree = parse_tree_indirect(remote_head->old_sha1);
 		parse_tree(tree);
 		init_tree_desc(&t, tree->buffer, tree->size);
 		unpack_trees(1, &t, &opts);
+		free_narrow_spec(opts.narrow_spec);
 
 		if (write_cache(fd, active_cache, active_nr) ||
 		    commit_locked_index(lock_file))
diff --git a/t/t5703-clone-narrow.sh b/t/t5703-clone-narrow.sh
new file mode 100755
index 0000000..e34246d
--- /dev/null
+++ b/t/t5703-clone-narrow.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+test_description='narrow clone'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+	rm -fr .git &&
+	test_create_repo src &&
+	(
+		cd src
+		mkdir -p work/sub/dir
+		touch untracked tracked modified added
+		touch work/untracked work/tracked work/modified work/added
+		git add tracked work/tracked
+		git add modified work/modified
+		git commit -m initial
+	)
+
+'
+
+test_expect_success 'narrow clone incompatible with --bare' '
+	rm -fr dst &&
+	test_must_fail git clone --sparse-checkout=work/ --bare src dst
+'
+
+test_expect_success 'narrow clone incompatible with --no-checkout' '
+	rm -fr dst &&
+	test_must_fail git clone --sparse-checkout=work/ -n src dst
+'
+
+test_expect_success 'clone with --sparse-checkout' '
+	(
+	rm -fr dst &&
+	git clone --sparse-checkout=work/ src dst &&
+	cd dst &&
+	test -z "$(git ls-files --sparse | grep -v ^work/)"
+	)
+'
+
+test_done
-- 
1.6.0.3.890.g95457
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 6 of 13 in “Sparse checkout, the last half of the series”
  1. 0/8 Sparse checkout, the last half of the seriesNguyễn Thái Ngọc Duy, Nov 30, 2008
  2. 1/8 generate-cmdlist.sh: avoid selecting synopsis at wrong placeNguyễn Thái Ngọc Duy, Nov 30, 2008
  3. 2/8 Introduce "sparse patterns"Nguyễn Thái Ngọc Duy, Nov 30, 2008
  4. 3/8 unpack_trees(): keep track of unmerged entriesNguyễn Thái Ngọc Duy, Nov 30, 2008
  5. 4/8 unpack_trees(): add support for sparse checkoutNguyễn Thái Ngọc Duy, Nov 30, 2008
  6. 5/8 clone: support sparse checkout with --sparse-checkout optionNguyễn Thái Ngọc Duy, Nov 30, 2008
  7. 6/8 checkout: add new options to support sparse checkoutNguyễn Thái Ngọc Duy, Nov 30, 2008
  8. 7/8 Introduce default sparse patterns (core.defaultsparse)Nguyễn Thái Ngọc Duy, Nov 30, 2008
  9. 8/8 wt-status: show sparse checkout infoNguyễn Thái Ngọc Duy, Nov 30, 2008
  10. Johannes SchindelinDec 1, 2008
  11. Nguyen Thai Ngoc DuyDec 1, 2008
  12. Junio C HamanoDec 1, 2008
  13. Johannes SchindelinDec 1, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.