From: Junio C Hamano <gitster@pobox.com>
To: Jiang Xin <worldhello.net@gmail.com>
Cc: Johannes Sixt <j6t@kdbg.org>, Git List <git@vger.kernel.org>
Subject: Re: [PATCH v15 02/16] path.c: refactor relative_path(), not only strip prefix
Date: Wed, 26 Jun 2013 10:44:46 -0700 [thread overview]
Message-ID: <7v7ghgwyup.fsf@alter.siamese.dyndns.org> (raw)
In-Reply-To: 31fc3821962b83e79cd8f59127c8c11ba1551073.1372175282.git.worldhello.net@gmail.com
Jiang Xin <worldhello.net@gmail.com> writes:
> Original design of relative_path() is simple, just strip the prefix
> (*base) from the absolute path (*abs). In most cases, we need a real
> relative path, such as: ../foo, ../../bar. That's why there is another
> reimplementation (path_relative()) in quote.c.
>
> Borrowed some codes from path_relative() in quote.c to refactor
s/Borrowed/Borrow/; will locally amend.
> relative_path() in path.c, so that it could return real relative path,
> and user can reuse this function without reimplement his/her own.
s/reimplement/&ing/; will locally amend.
> The function path_relative() in quote.c will be substituted, and I
> would use the new relative_path() function when implement the
s/implement/&ing/; will locally amend.
> interactive git-clean later.
>
> Different results for relative_path() before and after this refactor:
I'd remove the rows that show the same result in the latter two
columns (will locally amend, if you do not object).
> abs path base path relative (original) relative (refactor)
> ======== ========= =================== ===================
> /a/b /a/b . ./
> /a/b/ /a/b . ./
> /a /a/b/ /a ../
> / /a/b/ / ../../
> /a/c /a/b/ /a/c ../c
> /x/y /a/b/ /x/y ../../x/y
>
> a/b/ a/b/ . ./
> a/b/ a/b . ./
> a a/b a ../
> x/y a/b/ x/y ../../x/y
> a/c a/b a/c ../c
>
> (empty) (null) (empty) ./
> (empty) (empty) (empty) ./
> (empty) /a/b (empty) ./
> (null) (null) (null) ./
> (null) (empty) (null) ./
> (null) /a/b (segfault) ./
> You may notice that return value "." has been changed to "./".
> It is because:
>
> * Function quote_path_relative() in quote.c will show the relative
> path as "./" if abs(in) and base(prefix) are the same.
>
> * Function relative_path() is called only once (in setup.c), and
> it will be OK for the return value as "./" instead of ".".
Good explanation. Thanks.
> Signed-off-by: Jiang Xin <worldhello.net@gmail.com>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> ---
> cache.h | 2 +-
> path.c | 116 +++++++++++++++++++++++++++++++++++++-------------
> setup.c | 5 ++-
> t/t0060-path-utils.sh | 39 ++++++++---------
> test-path-utils.c | 4 +-
> 5 files changed, 113 insertions(+), 53 deletions(-)
>
> diff --git a/cache.h b/cache.h
> index dd0fb..c2886 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -758,7 +758,7 @@ int is_directory(const char *);
> const char *real_path(const char *path);
> const char *real_path_if_valid(const char *path);
> const char *absolute_path(const char *path);
> -const char *relative_path(const char *abs, const char *base);
> +const char *relative_path(const char *in, const char *prefix, struct strbuf *sb);
> int normalize_path_copy(char *dst, const char *src);
> int longest_ancestor_length(const char *path, struct string_list *prefixes);
> char *strip_path_suffix(const char *path, const char *suffix);
> diff --git a/path.c b/path.c
> index 04ff..fbe52c9b 100644
> --- a/path.c
> +++ b/path.c
> @@ -441,42 +441,100 @@ int adjust_shared_perm(const char *path)
> return 0;
> }
>
> +/*
> + * Give path as relative to prefix.
> + *
> + * The strbuf may or may not be used, so do not assume it contains the
> + * returned path.
> + */
> +const char *relative_path(const char *in, const char *prefix,
> + struct strbuf *sb)
> {
> + int in_len = in ? strlen(in) : 0;
> + int prefix_len = prefix ? strlen(prefix) : 0;
> + int in_off = 0;
> + int prefix_off = 0;
> + int i = 0,j = 0;
s/0,j/0, j/; will locally amend.
> +
> + if (!in_len)
> + return "./";
> + else if (!prefix_len)
> + return in;
> +
> + while (i < prefix_len && j < in_len && prefix[i] == in[j]) {
> + if (is_dir_sep(prefix[i])) {
> + while (is_dir_sep(prefix[i]))
> i++;
> + while (is_dir_sep(in[j]))
> j++;
These i++/j++ are not checked for lengths of strings, but it is OK
because is_dir_sep() will never return true for NUL.
> + prefix_off = i;
> + in_off = j;
(mental note) *_off keeps track of the last directory separator of
the common part, i.e. where the difference begins.
> + } else {
> + i++;
> + j++;
> + }
> + }
> +
> + if (
> + /* "prefix" seems like prefix of "in" */
> + i >= prefix_len &&
So shouldn't this be "i == prefix_len"?
> + /*
> + * but "/foo" is not a prefix of "/foobar"
> + * (i.e. prefix not end with '/')
> + */
> + prefix_off < prefix_len) {
> + if (j >= in_len) {
Again, "j == in_len", isn't it? Or can i and j overrun in_len and
prefix_len?
> + /* in="/a/b", prefix="/a/b" */
> + in_off = in_len;
> + } else if (is_dir_sep(in[j])) {
> + /* in="/a/b/c", prefix="/a/b" */
> + while (is_dir_sep(in[j]))
> + j++;
> + in_off = j;
> + } else {
> + /* in="/a/bbb/c", prefix="/a/b" */
> + i = prefix_off;
> + }
> + } else if (
> + /* "in" is short than "prefix" */
> + j >= in_len &&
> + /* "in" not end with '/' */
> + in_off < in_len) {
> + if (is_dir_sep(prefix[i])) {
> + /* in="/a/b", prefix="/a/b/c/" */
> + while (is_dir_sep(prefix[i]))
> + i++;
> + in_off = in_len;
> + }
> + }
> + in += in_off;
> + in_len -= in_off;
(mental note) Now in points at the part after stripping common
leading directories and in_len is adjusted accordingly.
> + if (i >= prefix_len) {
(mental note) it turns out that "in" covered the entire prefix, so
there is no need for "../" in this case.
> + if (!in_len)
> + return "./";
> + else
> + return in;
> + }
> +
> + strbuf_reset(sb);
> + strbuf_grow(sb, in_len);
> +
> + while (i < prefix_len) {
> + if (is_dir_sep(prefix[i])) {
> + strbuf_addstr(sb, "../");
> + while (is_dir_sep(prefix[i]))
> + i++;
> continue;
> }
> i++;
> }
(mental note) otherwise count '/' in the remainder of the prefix and
prepend as many "../" as necessary.
> + if (!is_dir_sep(prefix[prefix_len - 1]))
> + strbuf_addstr(sb, "../");
(mental note) ... and one more? E.g. for prefix="/a/b/c" in="/a/x",
we stripped the common "/a/" and the above loop started counting
from 'b' in the prefix, which counted one slash between b and c, but
we need to go two levels up. If prefix were "/a/b/c/", we would
already have counted the slash after c, so we do not need to do so.
OK.
next prev parent reply other threads:[~2013-06-26 17:45 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-06-25 15:53 [PATCH v15 00/16] Interactive git clean Jiang Xin
2013-06-25 15:53 ` [PATCH v15 01/16] test: add test cases for relative_path Jiang Xin
2013-06-26 17:44 ` Junio C Hamano
2013-06-27 1:00 ` Jiang Xin
2013-06-25 15:53 ` [PATCH v15 02/16] path.c: refactor relative_path(), not only strip prefix Jiang Xin
2013-06-26 17:44 ` Junio C Hamano [this message]
2013-06-27 3:31 ` Jiang Xin
2013-06-25 15:53 ` [PATCH v15 03/16] quote.c: substitute path_relative with relative_path Jiang Xin
2013-06-26 17:44 ` Junio C Hamano
2013-06-25 15:53 ` [PATCH v15 04/16] Refactor quote_path_relative, remove unused params Jiang Xin
2013-06-26 17:47 ` Junio C Hamano
2013-06-27 1:47 ` Jiang Xin
2013-06-25 15:53 ` [PATCH v15 05/16] Refactor write_name_quoted_relative, " Jiang Xin
2013-06-25 15:53 ` [PATCH v15 06/16] git-clean: refactor git-clean into two phases Jiang Xin
2013-06-25 15:53 ` [PATCH v15 07/16] git-clean: add support for -i/--interactive Jiang Xin
2013-06-25 15:53 ` [PATCH v15 08/16] git-clean: show items of del_list in columns Jiang Xin
2013-06-25 15:53 ` [PATCH v15 09/16] git-clean: add colors to interactive git-clean Jiang Xin
2013-06-25 15:53 ` [PATCH v15 10/16] git-clean: use a git-add-interactive compatible UI Jiang Xin
2013-06-25 15:53 ` [PATCH v15 11/16] git-clean: add filter by pattern interactive action Jiang Xin
2013-06-25 15:53 ` [PATCH v15 12/16] git-clean: add select by numbers " Jiang Xin
2013-06-25 15:53 ` [PATCH v15 13/16] git-clean: add ask each " Jiang Xin
2013-06-25 15:53 ` [PATCH v15 14/16] git-clean: add documentation for interactive git-clean Jiang Xin
2013-06-25 15:53 ` [PATCH v15 15/16] test: add t7301 for git-clean--interactive Jiang Xin
2013-06-25 15:53 ` [PATCH v15 16/16] test: run testcases with POSIX absolute paths on Windows Jiang Xin
2013-06-26 18:03 ` [PATCH v15 00/16] Interactive git clean 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=7v7ghgwyup.fsf@alter.siamese.dyndns.org \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=j6t@kdbg.org \
--cc=worldhello.net@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).