[PATCH 12/17] clone: support --narrow option
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
Documentation/git-clone.txt | 14 +++++++++++++-
builtin/clone.c | 43 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 56 insertions(+), 1 deletions(-)
diff --git a/Documentation/git-clone.txt b/Documentation/git-clone.txt
index dc7d3d1..d9a5729 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>] [-b <name>] [-u <upload-pack>] [--reference <repository>]
- [--depth <depth>] [--recursive] [--] <repository> [<directory>]
+ [--depth <depth>] [--narrow <path>] [--recursive]
+ [--] <repository> [<directory>]
DESCRIPTION
-----------
@@ -161,6 +162,17 @@ objects from the source repository into a pack in the cloned repository.
with a long history, and would want to send in fixes
as patches.
+--narrow <path>::
+ Create a 'narrow' clone with all commit trees limited to
+ the given path. A narrow repository has a number of
+ limititations (you cannot clone or fech from it, nor push
+ into it), but is adequate if you are only interested in
+ certain paths of a large repository, and would want to
+ push some fixes.
++
+Multiple --narrow can be given. This option can also be used together
+with --depth to truncate both history and path.
+
--recursive::
After the clone is created, initialize all submodules within,
using their default settings. This is equivalent to running
diff --git a/builtin/clone.c b/builtin/clone.c
index efb1e6f..439ce8a 100644
--- a/builtin/clone.c
+++ b/builtin/clone.c
@@ -23,6 +23,7 @@
#include "branch.h"
#include "remote.h"
#include "run-command.h"
+#include "narrow-tree.h"
/*
* Overall FIXMEs:
@@ -40,11 +41,15 @@ static const char * const builtin_clone_usage[] = {
static int option_no_checkout, option_bare, option_mirror;
static int option_local, option_no_hardlinks, option_shared, option_recursive;
static char *option_template, *option_reference, *option_depth;
+static const char **option_narrow;
static char *option_origin = NULL;
static char *option_branch = NULL;
static char *option_upload_pack = "git-upload-pack";
static int option_verbosity;
static int option_progress;
+static int narrow_nr;
+
+static int add_narrow_prefix(const struct option *opt, const char *arg, int unset);
static struct option builtin_clone_options[] = {
OPT__VERBOSITY(&option_verbosity),
@@ -78,6 +83,8 @@ static struct option builtin_clone_options[] = {
"path to git-upload-pack on the remote"),
OPT_STRING(0, "depth", &option_depth, "depth",
"create a shallow clone of that depth"),
+ OPT_CALLBACK(0, "narrow", NULL, "prefix", "narrow clone",
+ add_narrow_prefix),
OPT_END()
};
@@ -86,6 +93,22 @@ static const char *argv_submodule[] = {
"submodule", "update", "--init", "--recursive", NULL
};
+static int add_narrow_prefix(const struct option *opt, const char *arg, int unset)
+{
+ if (unset)
+ die("--no-narrow is not supported");
+
+ narrow_nr++;
+ option_narrow = xrealloc(option_narrow, sizeof(*option_narrow)*narrow_nr);
+ option_narrow[narrow_nr-1] = arg;
+ return 0;
+}
+
+static int narrow_cmp(const void *a, const void *b)
+{
+ return strcmp(*(const char**)a, *(const char **)b);
+}
+
static char *get_repo_path(const char *repo, int *is_bundle)
{
static char *suffix[] = { "/.git", ".git", "" };
@@ -443,6 +466,14 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
git_dir = xstrdup(mkpath("%s/.git", dir));
}
+ if (option_narrow) {
+ int i;
+ qsort(option_narrow, narrow_nr, sizeof(*option_narrow), narrow_cmp);
+ for (i = 0; i < narrow_nr; i++)
+ if (!valid_narrow_prefix(option_narrow[i], i ? option_narrow[i-1] : NULL, 0))
+ die("Invalid narrow prefix");
+ }
+
if (!option_bare) {
junk_work_tree = work_tree;
if (safe_create_leading_directories_const(work_tree) < 0)
@@ -515,6 +546,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
strbuf_reset(&value);
if (path && !is_bundle) {
+ if (option_narrow)
+ die("--narrow is not really for local clone. Please consider --shared");
refs = clone_local(path, git_dir);
mapped_refs = wanted_peer_refs(refs, refspec);
} else {
@@ -530,6 +563,16 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
transport_set_option(transport, TRANS_OPT_DEPTH,
option_depth);
+ /* Do this early in order to set get_narrow_prefix() != NULL */
+ if (option_narrow) {
+ int i;
+ FILE *fp = fopen(git_path("narrow"), "w+");
+ for (i = 0; i < narrow_nr; i++)
+ fprintf(fp, "%s\n", option_narrow[i]);
+ fclose(fp);
+ check_narrow_prefix(); /* Install the prefix */
+ }
+
transport_set_verbosity(transport, option_verbosity, option_progress);
if (option_upload_pack)
--
1.7.1.rc1.69.g24c2f7