1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-22 00:26:08 +02:00

strbuf: add strbuf_add_real_path()

Add a function for appending the canonized absolute pathname of a given
path to a strbuf.  It keeps the existing contents intact, as expected of
a function of the strbuf_add() family, while avoiding copying the result
if the given strbuf is empty.  It's more consistent with the rest of the
strbuf API than strbuf_realpath(), which it's wrapping.

Also add a semantic patch demonstrating its intended usage and apply it
to the current tree.  Using strbuf_add_real_path() instead of calling
strbuf_addstr() and real_path() avoids an extra copy to a static buffer.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
René Scharfe 2017-02-25 17:00:33 +01:00 committed by Junio C Hamano
parent 3f64699ffd
commit 33ad9ddd0b
4 changed files with 32 additions and 1 deletions

View File

@ -38,3 +38,9 @@ expression E1, E2, E3;
@@
- strbuf_addstr(E1, find_unique_abbrev(E2, E3));
+ strbuf_add_unique_abbrev(E1, E2, E3);
@@
expression E1, E2;
@@
- strbuf_addstr(E1, real_path(E2));
+ strbuf_add_real_path(E1, E2);

View File

@ -254,7 +254,7 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir)
if (!is_absolute_path(data.buf))
strbuf_addf(&path, "%s/", gitdir);
strbuf_addbuf(&path, &data);
strbuf_addstr(sb, real_path(path.buf));
strbuf_add_real_path(sb, path.buf);
ret = 1;
} else
strbuf_addstr(sb, gitdir);

View File

@ -707,6 +707,17 @@ void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
strbuf_addstr(sb, path);
}
void strbuf_add_real_path(struct strbuf *sb, const char *path)
{
if (sb->len) {
struct strbuf resolved = STRBUF_INIT;
strbuf_realpath(&resolved, path, 1);
strbuf_addbuf(sb, &resolved);
strbuf_release(&resolved);
} else
strbuf_realpath(sb, path, 1);
}
int printf_ln(const char *fmt, ...)
{
int ret;

View File

@ -443,6 +443,20 @@ extern int strbuf_getcwd(struct strbuf *sb);
*/
extern void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
/**
* Canonize `path` (make it absolute, resolve symlinks, remove extra
* slashes) and append it to `sb`. Die with an informative error
* message if there is a problem.
*
* The directory part of `path` (i.e., everything up to the last
* dir_sep) must denote a valid, existing directory, but the last
* component need not exist.
*
* Callers that don't mind links should use the more lightweight
* strbuf_add_absolute_path() instead.
*/
extern void strbuf_add_real_path(struct strbuf *sb, const char *path);
/**
* Normalize in-place the path contained in the strbuf. See