1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-04-19 15:13:52 +02:00

abspath: move related functions to abspath

Move abspath-related functions from strbuf.[ch] to abspath.[ch] so that
strbuf is focused on string manipulation routines with minimal
dependencies.

Signed-off-by: Calvin Wan <calvinwan@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Calvin Wan 2023-05-12 17:15:11 +00:00 committed by Junio C Hamano
parent d0ca92a7fe
commit 689ece6602
6 changed files with 59 additions and 59 deletions

View File

@ -289,3 +289,39 @@ char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
return xstrdup(arg);
return prefix_filename(pfx, arg);
}
void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
{
if (!*path)
die("The empty string is not a valid path");
if (!is_absolute_path(path)) {
struct stat cwd_stat, pwd_stat;
size_t orig_len = sb->len;
char *cwd = xgetcwd();
char *pwd = getenv("PWD");
if (pwd && strcmp(pwd, cwd) &&
!stat(cwd, &cwd_stat) &&
(cwd_stat.st_dev || cwd_stat.st_ino) &&
!stat(pwd, &pwd_stat) &&
pwd_stat.st_dev == cwd_stat.st_dev &&
pwd_stat.st_ino == cwd_stat.st_ino)
strbuf_addstr(sb, pwd);
else
strbuf_addstr(sb, cwd);
if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
strbuf_addch(sb, '/');
free(cwd);
}
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);
}

View File

@ -30,4 +30,25 @@ static inline int is_absolute_path(const char *path)
return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
}
/**
* Add a path to a buffer, converting a relative path to an
* absolute one in the process. Symbolic links are not
* resolved.
*/
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.
*/
void strbuf_add_real_path(struct strbuf *sb, const char *path);
#endif /* ABSPATH_H */

1
hook.c
View File

@ -1,4 +1,5 @@
#include "git-compat-util.h"
#include "abspath.h"
#include "advice.h"
#include "gettext.h"
#include "hook.h"

View File

@ -1,5 +1,4 @@
#include "git-compat-util.h"
#include "abspath.h"
#include "alloc.h"
#include "environment.h"
#include "gettext.h"
@ -899,42 +898,6 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
strbuf_humanise(buf, bytes, 1);
}
void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
{
if (!*path)
die("The empty string is not a valid path");
if (!is_absolute_path(path)) {
struct stat cwd_stat, pwd_stat;
size_t orig_len = sb->len;
char *cwd = xgetcwd();
char *pwd = getenv("PWD");
if (pwd && strcmp(pwd, cwd) &&
!stat(cwd, &cwd_stat) &&
(cwd_stat.st_dev || cwd_stat.st_ino) &&
!stat(pwd, &pwd_stat) &&
pwd_stat.st_dev == cwd_stat.st_dev &&
pwd_stat.st_ino == cwd_stat.st_ino)
strbuf_addstr(sb, pwd);
else
strbuf_addstr(sb, cwd);
if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
strbuf_addch(sb, '/');
free(cwd);
}
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

@ -535,28 +535,6 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term);
*/
int strbuf_getcwd(struct strbuf *sb);
/**
* Add a path to a buffer, converting a relative path to an
* absolute one in the process. Symbolic links are not
* resolved.
*/
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.
*/
void strbuf_add_real_path(struct strbuf *sb, const char *path);
/**
* Normalize in-place the path contained in the strbuf. See
* normalize_path_copy() for details. If an error occurs, the contents of "sb"

View File

@ -43,6 +43,7 @@
*/
#include "git-compat-util.h"
#include "abspath.h"
#include "path.h"
#include "tempfile.h"
#include "sigchain.h"