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>

Calvin Wan committed Jun 6, 2023 at 19:48 UTC 5d1344b4973c8ea4904005f3bb51a47334ebb370
6 files changed +59 -59
abspath.c
+36
@@ -289,3 +289,39 @@ char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
289 return xstrdup(arg);
290 return prefix_filename(pfx, arg);
291 }
292 +
293 +void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
294 +{
295 + if (!*path)
296 + die("The empty string is not a valid path");
297 + if (!is_absolute_path(path)) {
298 + struct stat cwd_stat, pwd_stat;
299 + size_t orig_len = sb->len;
300 + char *cwd = xgetcwd();
301 + char *pwd = getenv("PWD");
302 + if (pwd && strcmp(pwd, cwd) &&
303 + !stat(cwd, &cwd_stat) &&
304 + (cwd_stat.st_dev || cwd_stat.st_ino) &&
305 + !stat(pwd, &pwd_stat) &&
306 + pwd_stat.st_dev == cwd_stat.st_dev &&
307 + pwd_stat.st_ino == cwd_stat.st_ino)
308 + strbuf_addstr(sb, pwd);
309 + else
310 + strbuf_addstr(sb, cwd);
311 + if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
312 + strbuf_addch(sb, '/');
313 + free(cwd);
314 + }
315 + strbuf_addstr(sb, path);
316 +}
317 +
318 +void strbuf_add_real_path(struct strbuf *sb, const char *path)
319 +{
320 + if (sb->len) {
321 + struct strbuf resolved = STRBUF_INIT;
322 + strbuf_realpath(&resolved, path, 1);
323 + strbuf_addbuf(sb, &resolved);
324 + strbuf_release(&resolved);
325 + } else
326 + strbuf_realpath(sb, path, 1);
327 +}
abspath.h
+21
@@ -30,4 +30,25 @@ static inline int is_absolute_path(const char *path)
30 return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
31 }
32
33 +/**
34 + * Add a path to a buffer, converting a relative path to an
35 + * absolute one in the process. Symbolic links are not
36 + * resolved.
37 + */
38 +void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
39 +
40 +/**
41 + * Canonize `path` (make it absolute, resolve symlinks, remove extra
42 + * slashes) and append it to `sb`. Die with an informative error
43 + * message if there is a problem.
44 + *
45 + * The directory part of `path` (i.e., everything up to the last
46 + * dir_sep) must denote a valid, existing directory, but the last
47 + * component need not exist.
48 + *
49 + * Callers that don't mind links should use the more lightweight
50 + * strbuf_add_absolute_path() instead.
51 + */
52 +void strbuf_add_real_path(struct strbuf *sb, const char *path);
53 +
54 #endif /* ABSPATH_H */
hook.c
+1
@@ -1,4 +1,5 @@
1 #include "git-compat-util.h"
2 +#include "abspath.h"
3 #include "advice.h"
4 #include "gettext.h"
5 #include "hook.h"
strbuf.c
-37
@@ -1,5 +1,4 @@
1 #include "git-compat-util.h"
2 -#include "abspath.h"
2 #include "alloc.h"
3 #include "environment.h"
4 #include "gettext.h"
@@ -900,42 +899,6 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
899 strbuf_humanise(buf, bytes, 1);
900 }
901
903 -void strbuf_add_absolute_path(struct strbuf *sb, const char *path)
904 -{
905 - if (!*path)
906 - die("The empty string is not a valid path");
907 - if (!is_absolute_path(path)) {
908 - struct stat cwd_stat, pwd_stat;
909 - size_t orig_len = sb->len;
910 - char *cwd = xgetcwd();
911 - char *pwd = getenv("PWD");
912 - if (pwd && strcmp(pwd, cwd) &&
913 - !stat(cwd, &cwd_stat) &&
914 - (cwd_stat.st_dev || cwd_stat.st_ino) &&
915 - !stat(pwd, &pwd_stat) &&
916 - pwd_stat.st_dev == cwd_stat.st_dev &&
917 - pwd_stat.st_ino == cwd_stat.st_ino)
918 - strbuf_addstr(sb, pwd);
919 - else
920 - strbuf_addstr(sb, cwd);
921 - if (sb->len > orig_len && !is_dir_sep(sb->buf[sb->len - 1]))
922 - strbuf_addch(sb, '/');
923 - free(cwd);
924 - }
925 - strbuf_addstr(sb, path);
926 -}
927 -
928 -void strbuf_add_real_path(struct strbuf *sb, const char *path)
929 -{
930 - if (sb->len) {
931 - struct strbuf resolved = STRBUF_INIT;
932 - strbuf_realpath(&resolved, path, 1);
933 - strbuf_addbuf(sb, &resolved);
934 - strbuf_release(&resolved);
935 - } else
936 - strbuf_realpath(sb, path, 1);
937 -}
938 -
902 int printf_ln(const char *fmt, ...)
903 {
904 int ret;
strbuf.h
-22
@@ -535,28 +535,6 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term);
535 */
536 int strbuf_getcwd(struct strbuf *sb);
537
538 -/**
539 - * Add a path to a buffer, converting a relative path to an
540 - * absolute one in the process. Symbolic links are not
541 - * resolved.
542 - */
543 -void strbuf_add_absolute_path(struct strbuf *sb, const char *path);
544 -
545 -/**
546 - * Canonize `path` (make it absolute, resolve symlinks, remove extra
547 - * slashes) and append it to `sb`. Die with an informative error
548 - * message if there is a problem.
549 - *
550 - * The directory part of `path` (i.e., everything up to the last
551 - * dir_sep) must denote a valid, existing directory, but the last
552 - * component need not exist.
553 - *
554 - * Callers that don't mind links should use the more lightweight
555 - * strbuf_add_absolute_path() instead.
556 - */
557 -void strbuf_add_real_path(struct strbuf *sb, const char *path);
558 -
559 -
538 /**
539 * Normalize in-place the path contained in the strbuf. See
540 * normalize_path_copy() for details. If an error occurs, the contents of "sb"
tempfile.c
+1
@@ -43,6 +43,7 @@
43 */
44
45 #include "git-compat-util.h"
46 +#include "abspath.h"
47 #include "path.h"
48 #include "tempfile.h"
49 #include "sigchain.h"