From mboxrd@z Thu Jan 1 00:00:00 1970 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Subject: [PATCH v4 05/27] Make git_path() aware of file relocation in $GIT_DIR Date: Sat, 1 Mar 2014 19:12:41 +0700 Message-ID: <1393675983-3232-6-git-send-email-pclouds@gmail.com> References: <1392730814-19656-1-git-send-email-pclouds@gmail.com> <1393675983-3232-1-git-send-email-pclouds@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Junio C Hamano , =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= To: git@vger.kernel.org X-From: git-owner@vger.kernel.org Sat Mar 01 13:21:29 2014 Return-path: Envelope-to: gcvg-git-2@plane.gmane.org Received: from vger.kernel.org ([209.132.180.67]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WJiv1-0001op-M7 for gcvg-git-2@plane.gmane.org; Sat, 01 Mar 2014 13:21:28 +0100 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752670AbaCAMVW convert rfc822-to-quoted-printable (ORCPT ); Sat, 1 Mar 2014 07:21:22 -0500 Received: from mail-pd0-f173.google.com ([209.85.192.173]:56318 "EHLO mail-pd0-f173.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752429AbaCAMVU (ORCPT ); Sat, 1 Mar 2014 07:21:20 -0500 Received: by mail-pd0-f173.google.com with SMTP id z10so1859410pdj.4 for ; Sat, 01 Mar 2014 04:21:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-type:content-transfer-encoding; bh=p/ZAWTuB8Fcg2qvB0M32Ftp+LaC/+oct3OIAbcz85/Q=; b=Q2bc1JY/J7RaUOOWSP00Pki4+XEfD9rz60wMmz+vlkEJ4z2byoyUoV47uJaZjC3o5i 5GNtJkIMIJjzzledHOtsvQ04SA+yF1s3uNbTt2SP5tUzlXAm9tU0r4dYoeI6xF8UcD9e seizMCgMMZUeD4HqJnWDeP+nwBgoRvnvKj4pbpreW6D944ECOYQxbTC4fkHFSTnV9Agx ew6XRMXDGeCRV20zjF2Womt5/YURLZnooG/JYpEN/sL8cSMswsayrisyBvmK5H6OFiEj bQukVE0Kga9Kve8UuhPqRBB5f8AVocfPCaU+0IitapqqxTmumxdxJizITDGR2uYhWtaa d94w== X-Received: by 10.68.131.100 with SMTP id ol4mr9322610pbb.134.1393676480500; Sat, 01 Mar 2014 04:21:20 -0800 (PST) Received: from lanh ([115.73.238.45]) by mx.google.com with ESMTPSA id qh2sm37131333pab.13.2014.03.01.04.21.17 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sat, 01 Mar 2014 04:21:19 -0800 (PST) Received: by lanh (sSMTP sendmail emulation); Sat, 01 Mar 2014 19:21:45 +0700 X-Mailer: git-send-email 1.9.0.40.gaa8c3ea In-Reply-To: <1393675983-3232-1-git-send-email-pclouds@gmail.com> Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org Archived-At: We allow the user to relocate certain paths out of $GIT_DIR via environment variables, e.g. GIT_OBJECT_DIRECTORY, GIT_INDEX_FILE and GIT_GRAFT_FILE. All callers are not supposed to use git_path() or git_pathdup() to get those paths. Instead they must use get_object_directory(), get_index_file() and get_graft_file() respectively. This is inconvenient and could be missed in review (there's git_path("objects/info/alternates") somewhere in sha1_file.c). This patch makes git_path() and git_pathdup() understand those environment variables. So if you set GIT_OBJECT_DIRECTORY to /foo/bar, git_path("objects/abc") should return /tmp/bar/abc. The same is done for the two remaining env variables. "git rev-parse --git-path" is the wrapper for script use. Signed-off-by: Nguy=E1=BB=85n Th=C3=A1i Ng=E1=BB=8Dc Duy --- Documentation/git-rev-parse.txt | 5 +++++ builtin/rev-parse.c | 7 +++++++ cache.h | 1 + environment.c | 9 ++++++-- path.c | 46 +++++++++++++++++++++++++++++++++= ++++++++ t/t0060-path-utils.sh | 19 +++++++++++++++++ 6 files changed, 85 insertions(+), 2 deletions(-) diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-pa= rse.txt index 0d2cdcd..33e4e90 100644 --- a/Documentation/git-rev-parse.txt +++ b/Documentation/git-rev-parse.txt @@ -232,6 +232,11 @@ print a message to stderr and exit with nonzero st= atus. repository. If is a gitfile then the resolved path to the real repository is printed. =20 +--git-path :: + Resolve "$GIT_DIR/" and takes other path relocation + variables such as $GIT_OBJECT_DIRECTORY, + $GIT_INDEX_FILE... into account. + --show-cdup:: When the command is invoked from a subdirectory, show the path of the top-level directory relative to the current diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index aaeb611..e50bc65 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -518,6 +518,13 @@ int cmd_rev_parse(int argc, const char **argv, con= st char *prefix) for (i =3D 1; i < argc; i++) { const char *arg =3D argv[i]; =20 + if (!strcmp(arg, "--git-path")) { + if (!argv[i + 1]) + die("--git-path requires an argument"); + puts(git_path("%s", argv[i + 1])); + i++; + continue; + } if (as_is) { if (show_file(arg, output_prefix) && as_is < 2) verify_filename(prefix, arg, 0); diff --git a/cache.h b/cache.h index 8d3697e..6c08e4a 100644 --- a/cache.h +++ b/cache.h @@ -585,6 +585,7 @@ extern int fsync_object_files; extern int core_preload_index; extern int core_apply_sparse_checkout; extern int precomposed_unicode; +extern int git_db_env, git_index_env, git_graft_env; =20 /* * The character that begins a commented line in user-editable file diff --git a/environment.c b/environment.c index 4a3437d..f513479 100644 --- a/environment.c +++ b/environment.c @@ -82,6 +82,7 @@ static size_t namespace_len; =20 static const char *git_dir; static char *git_object_dir, *git_index_file, *git_graft_file; +int git_db_env, git_index_env, git_graft_env; =20 /* * Repository-local GIT_* environment variables; see cache.h for detai= ls. @@ -137,15 +138,19 @@ static void setup_git_env(void) if (!git_object_dir) { git_object_dir =3D xmalloc(strlen(git_dir) + 9); sprintf(git_object_dir, "%s/objects", git_dir); - } + } else + git_db_env =3D 1; git_index_file =3D getenv(INDEX_ENVIRONMENT); if (!git_index_file) { git_index_file =3D xmalloc(strlen(git_dir) + 7); sprintf(git_index_file, "%s/index", git_dir); - } + } else + git_index_env =3D 1; git_graft_file =3D getenv(GRAFT_ENVIRONMENT); if (!git_graft_file) git_graft_file =3D git_pathdup("info/grafts"); + else + git_graft_env =3D 1; if (getenv(NO_REPLACE_OBJECTS_ENVIRONMENT)) read_replace_refs =3D 0; namespace =3D expand_namespace(getenv(GIT_NAMESPACE_ENVIRONMENT)); diff --git a/path.c b/path.c index ccd7228..e020530 100644 --- a/path.c +++ b/path.c @@ -60,13 +60,59 @@ char *mksnpath(char *buf, size_t n, const char *fmt= , ...) return cleanup_path(buf); } =20 +static int dir_prefix(const char *buf, const char *dir) +{ + int len =3D strlen(dir); + return !strncmp(buf, dir, len) && + (is_dir_sep(buf[len]) || buf[len] =3D=3D '\0'); +} + +/* $buf =3D~ m|$dir/+$file| but without regex */ +static int is_dir_file(const char *buf, const char *dir, const char *f= ile) +{ + int len =3D strlen(dir); + if (strncmp(buf, dir, len) || !is_dir_sep(buf[len])) + return 0; + while (is_dir_sep(buf[len])) + len++; + return !strcmp(buf + len, file); +} + +static void replace_dir(struct strbuf *buf, int len, const char *newdi= r) +{ + int newlen =3D strlen(newdir); + int need_sep =3D (buf->buf[len] && !is_dir_sep(buf->buf[len])) && + !is_dir_sep(newdir[newlen - 1]); + if (need_sep) + len--; /* keep one char, to be replaced with '/' */ + strbuf_splice(buf, 0, len, newdir, newlen); + if (need_sep) + buf->buf[newlen] =3D '/'; +} + +static void adjust_git_path(struct strbuf *buf, int git_dir_len) +{ + const char *base =3D buf->buf + git_dir_len; + if (git_graft_env && is_dir_file(base, "info", "grafts")) + strbuf_splice(buf, 0, buf->len, + get_graft_file(), strlen(get_graft_file())); + else if (git_index_env && !strcmp(base, "index")) + strbuf_splice(buf, 0, buf->len, + get_index_file(), strlen(get_index_file())); + else if (git_db_env && dir_prefix(base, "objects")) + replace_dir(buf, git_dir_len + 7, get_object_directory()); +} + static void do_git_path(struct strbuf *buf, const char *fmt, va_list a= rgs) { const char *git_dir =3D get_git_dir(); + int gitdir_len; strbuf_addstr(buf, git_dir); if (buf->len && !is_dir_sep(buf->buf[buf->len - 1])) strbuf_addch(buf, '/'); + gitdir_len =3D buf->len; strbuf_vaddf(buf, fmt, args); + adjust_git_path(buf, gitdir_len); strbuf_cleanup_path(buf); } =20 diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh index 07c10c8..1d29901 100755 --- a/t/t0060-path-utils.sh +++ b/t/t0060-path-utils.sh @@ -19,6 +19,14 @@ relative_path() { "test \"\$(test-path-utils relative_path '$1' '$2')\" =3D '$expected'= " } =20 +test_git_path() { + test_expect_success "git-path $1 $2 =3D> $3" " + $1 git rev-parse --git-path $2 >actual && + echo $3 >expect && + test_cmp expect actual + " +} + # On Windows, we are using MSYS's bash, which mangles the paths. # Absolute paths are anchored at the MSYS installation directory, # which means that the path / accounts for this many characters: @@ -223,4 +231,15 @@ relative_path "" "" ./ relative_path "" "" ./ relative_path "" /foo/a/b ./ =20 +test_git_path A=3DB info/grafts .git/info/grafts +test_git_path GIT_GRAFT_FILE=3Dfoo info/grafts foo +test_git_path GIT_GRAFT_FILE=3Dfoo info/////grafts foo +test_git_path GIT_INDEX_FILE=3Dfoo index foo +test_git_path GIT_INDEX_FILE=3Dfoo index/foo .git/index/foo +test_git_path GIT_INDEX_FILE=3Dfoo index2 .git/index2 +test_expect_success 'setup fake objects directory foo' 'mkdir foo' +test_git_path GIT_OBJECT_DIRECTORY=3Dfoo objects foo +test_git_path GIT_OBJECT_DIRECTORY=3Dfoo objects/foo foo/foo +test_git_path GIT_OBJECT_DIRECTORY=3Dfoo objects2 .git/objects2 + test_done --=20 1.9.0.40.gaa8c3ea