From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org, Elijah Newren <newren@gmail.com>
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH 03/17] Introduce $GIT_DIR/narrow
Date: Sun, 5 Sep 2010 16:47:30 +1000 [thread overview]
Message-ID: <1283669264-15759-4-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <1283669264-15759-1-git-send-email-pclouds@gmail.com>
This file contains a sorted list of narrow prefix, used in narrow
repositories.
rev-parse also learns --narrow-prefix to print $GIT_DIR/narrow out
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
Documentation/git-rev-parse.txt | 3 +
Documentation/gitrepository-layout.txt | 5 ++
Makefile | 2 +
builtin/rev-parse.c | 8 +++
cache.h | 3 +
environment.c | 2 +
narrow-tree.c | 106 ++++++++++++++++++++++++++++++++
narrow-tree.h | 3 +
t/t0063-narrow-repo.sh | 46 ++++++++++++++
9 files changed, 178 insertions(+), 0 deletions(-)
create mode 100644 narrow-tree.c
create mode 100644 narrow-tree.h
create mode 100755 t/t0063-narrow-repo.sh
diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt
index be4c053..5d5e77b 100644
--- a/Documentation/git-rev-parse.txt
+++ b/Documentation/git-rev-parse.txt
@@ -148,6 +148,9 @@ shown. If the pattern does not contain a globbing character (`?`,
--is-bare-repository::
When the repository is bare print "true", otherwise "false".
+--narrow-prefix::
+ Print narrow prefix git reads from $GIT_DIR/narrow.
+
--local-env-vars::
List the GIT_* environment variables that are local to the
repository (e.g. GIT_DIR or GIT_WORK_TREE, but not GIT_EDITOR).
diff --git a/Documentation/gitrepository-layout.txt b/Documentation/gitrepository-layout.txt
index eb3d040..82e0350 100644
--- a/Documentation/gitrepository-layout.txt
+++ b/Documentation/gitrepository-layout.txt
@@ -193,6 +193,11 @@ shallow::
and maintained by shallow clone mechanism. See `--depth`
option to linkgit:git-clone[1] and linkgit:git-fetch[1].
+narrow::
+ This file contains narrow prefix in sorted order. It is
+ internally used and maintained by narrow clone mechanism.
+ See `--narrow-tree` option to linkgit:git-clone[1].
+
SEE ALSO
--------
linkgit:git-init[1],
diff --git a/Makefile b/Makefile
index b4745a5..f1aaba9 100644
--- a/Makefile
+++ b/Makefile
@@ -525,6 +525,7 @@ LIB_H += sigchain.h
LIB_H += strbuf.h
LIB_H += string-list.h
LIB_H += submodule.h
+LIB_H += narrow-tree.h
LIB_H += tag.h
LIB_H += transport.h
LIB_H += tree.h
@@ -629,6 +630,7 @@ LIB_OBJS += sigchain.o
LIB_OBJS += strbuf.o
LIB_OBJS += string-list.o
LIB_OBJS += submodule.o
+LIB_OBJS += narrow-tree.o
LIB_OBJS += symlinks.o
LIB_OBJS += tag.o
LIB_OBJS += trace.o
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index a5a1c86..590df6f 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -675,6 +675,14 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
: "false");
continue;
}
+ if (!strcmp(arg, "--narrow-prefix")) {
+ const char **p = get_narrow_prefix();
+ if (!p)
+ continue;
+ while (*p)
+ printf("%s\n", *p++);
+ continue;
+ }
if (!prefixcmp(arg, "--since=")) {
show_datestring("--max-age=", arg+8);
continue;
diff --git a/cache.h b/cache.h
index eb77e1d..d09c4fc 100644
--- a/cache.h
+++ b/cache.h
@@ -1105,4 +1105,7 @@ const char *split_cmdline_strerror(int cmdline_errno);
/* builtin/merge.c */
int checkout_fast_forward(const unsigned char *from, const unsigned char *to);
+/* narrow-tree.c */
+extern const char **get_narrow_prefix();
+
#endif /* CACHE_H */
diff --git a/environment.c b/environment.c
index 83d38d3..41fcbd4 100644
--- a/environment.c
+++ b/environment.c
@@ -8,6 +8,7 @@
* are.
*/
#include "cache.h"
+#include "narrow-tree.h"
char git_default_email[MAX_GITNAME];
char git_default_name[MAX_GITNAME];
@@ -105,6 +106,7 @@ static void setup_git_env(void)
git_graft_file = git_pathdup("info/grafts");
if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT))
read_replace_refs = 0;
+ check_narrow_prefix();
}
int is_bare_repository(void)
diff --git a/narrow-tree.c b/narrow-tree.c
new file mode 100644
index 0000000..85dbab4
--- /dev/null
+++ b/narrow-tree.c
@@ -0,0 +1,106 @@
+#include "cache.h"
+#include "narrow-tree.h"
+
+static const char **narrow_prefix;
+static char *narrow_buf;
+
+int valid_narrow_prefix(const char *prefix, const char *prev_prefix, int quiet)
+{
+ int len = strlen(prefix);
+
+ if (!*prefix) {
+ if (!quiet)
+ error("Empty line in $GIT_DIR/narrow");
+ return 0;
+ }
+
+ if (prefix[len-1] == '/') {
+ if (!quiet)
+ error("Trailing slash not allowed in $GIT_DIR: %s", prefix);
+ return 0;
+ }
+
+ if (prev_prefix) {
+ if (strcmp(prev_prefix, prefix) >= 0) {
+ if (!quiet)
+ error("$GIT_DIR/narrow is unsorted at %s", prefix);
+ return 0;
+ }
+ len = strlen(prev_prefix);
+ if (!strncmp(prev_prefix, prefix, len) &&
+ prefix[len] == '/') {
+ if (!quiet) {
+ error("$GIT_DIR/narrow has nested prefix (%s and %s)",
+ prev_prefix, prefix);
+ return 0;
+ }
+ }
+ }
+ return 1;
+}
+
+int check_narrow_prefix()
+{
+ struct stat st;
+ int fd, i, n, len;
+ char *p, *pp;
+
+ if (stat(git_path("narrow"), &st) || st.st_size == 0)
+ return 0;
+
+ narrow_buf = xmalloc(st.st_size+2); /* NULL and \n */
+
+ fd = open(git_path("narrow"), O_RDONLY);
+ if (fd == -1)
+ return 0;
+
+ if (xread(fd, narrow_buf, st.st_size) != st.st_size)
+ die("failed to read $GIT_DIR/narrow");
+ close(fd);
+
+ if (narrow_buf[st.st_size-1] == '\n')
+ narrow_buf[st.st_size] = '\0';
+ else {
+ narrow_buf[st.st_size] = '\n';
+ narrow_buf[st.st_size+1] = '\0';
+ }
+ n = 0;
+ for (p = narrow_buf; *p; p = strchr(p, '\n')+1)
+ n++;
+ if (!n)
+ return 0;
+ narrow_prefix = xmalloc(sizeof(*narrow_prefix)*(n+1));
+ p = narrow_buf;
+ for (i = 0; i < n; i++) {
+ pp = p;
+ p = strchr(p, '\n') + 1;
+ len = p - pp;
+ while (len &&
+ (pp[len-1] == '\n' ||
+ pp[len-1] == '\r'))
+ len--;
+ pp[len] = '\0';
+ if (!valid_narrow_prefix(pp, i ? narrow_prefix[i-1] : NULL, 0))
+ die("Invalid $GIT_DIR/narrow");
+ narrow_prefix[i] = pp;
+ }
+ narrow_prefix[n] = NULL;
+ return 0;
+}
+
+const char **get_narrow_prefix()
+{
+ return narrow_prefix;
+}
+
+char *get_narrow_string()
+{
+ struct strbuf sb = STRBUF_INIT;
+ const char **prefix = get_narrow_prefix();
+ while (*prefix) {
+ strbuf_addstr(&sb, *prefix);
+ strbuf_addch(&sb, '\n');
+ prefix++;
+ }
+ return strbuf_detach(&sb, NULL);
+}
diff --git a/narrow-tree.h b/narrow-tree.h
new file mode 100644
index 0000000..2097436
--- /dev/null
+++ b/narrow-tree.h
@@ -0,0 +1,3 @@
+extern int valid_narrow_prefix(const char *prefix, const char *prev_prefix, int quiet);
+extern int check_narrow_prefix();
+extern char *get_narrow_string();
diff --git a/t/t0063-narrow-repo.sh b/t/t0063-narrow-repo.sh
new file mode 100755
index 0000000..51b753d
--- /dev/null
+++ b/t/t0063-narrow-repo.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+test_description='narrow index'
+
+. ./test-lib.sh
+
+test_expect_success 'empty $GIT_DIR/narrow' '
+ : >.git/narrow &&
+ git rev-parse --narrow-prefix >result &&
+ : >expected
+ test_cmp expected result
+'
+
+test_expect_success 'empty line' '
+ echo >.git/narrow &&
+ test_must_fail git rev-parse --narrow-prefix
+'
+
+test_expect_success 'single prefix' '
+ echo a >.git/narrow &&
+ git rev-parse --narrow-prefix >result &&
+ echo a >expected
+ test_cmp expected result
+'
+
+test_expect_success 'trailing slash in prefix' '
+ echo a/ >.git/narrow &&
+ test_must_fail git rev-parse --narrow-prefix
+'
+
+test_expect_success 'sorted multiple prefix' '
+ echo a >.git/narrow &&
+ echo b >>.git/narrow &&
+ git rev-parse --narrow-prefix >result &&
+ echo a >expected
+ echo b >>expected
+ test_cmp expected result
+'
+
+test_expect_success 'unsorted multiple prefix' '
+ echo b >.git/narrow &&
+ echo a >>.git/narrow &&
+ test_must_fail git rev-parse --narrow-prefix
+'
+
+test_done
--
1.7.1.rc1.69.g24c2f7
next prev parent reply other threads:[~2010-09-05 6:48 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-09-05 6:47 [PATCH 00/17] Narrow clone v3 (was subtree clone) Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 01/17] rev-list: do not do commit simplification if simplify_history = 0 Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 02/17] tree.c: add path_to_sha1() Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` Nguyễn Thái Ngọc Duy [this message]
2010-09-05 6:47 ` [PATCH 04/17] index: make narrow index incompatible with older git Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 05/17] pack-objects: support narrow packs with pathspecs Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 06/17] {fetch,upload}-pack: support narrow repository Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 07/17] unpack-trees: split traverse_trees() code into a separate function Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 08/17] unpack-trees: support unpack trees in narrow repository Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 09/17] cache-tree: only cache tree within narrow area Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 10/17] get_pathspec(): support narrow pathspec rewriting Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 11/17] pathspec retrieval fix Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 12/17] clone: support --narrow option Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 13/17] commit: add narrow's commit_tree version Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 14/17] commit: use commit_narrow_tree() to support narrow repo Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 15/17] write-tree: requires --narrow-base in narrow repository Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 16/17] merge: try to do local merge if possible in narrow repo Nguyễn Thái Ngọc Duy
2010-09-05 6:47 ` [PATCH 17/17] Add narrow clone demonstration test Nguyễn Thái Ngọc Duy
2010-09-05 6:55 ` [PATCH 00/17] Narrow clone v3 (was subtree clone) Sverre Rabbelier
2010-09-05 7:13 ` Nguyen Thai Ngoc Duy
2010-09-05 21:05 ` Elijah Newren
2010-09-06 5:17 ` Elijah Newren
2010-09-06 5:24 ` Nguyen Thai Ngoc Duy
2010-09-06 20:29 ` Sverre Rabbelier
2010-09-06 20:40 ` Elijah Newren
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1283669264-15759-4-git-send-email-pclouds@gmail.com \
--to=pclouds@gmail.com \
--cc=git@vger.kernel.org \
--cc=newren@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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.