Git fork

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>

authored by

Calvin Wan and committed by
Junio C Hamano
5d1344b4 16b171fd

+59 -59
+36
abspath.c
··· 289 289 return xstrdup(arg); 290 290 return prefix_filename(pfx, arg); 291 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 + }
+21
abspath.h
··· 30 30 return is_dir_sep(path[0]) || has_dos_drive_prefix(path); 31 31 } 32 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 + 33 54 #endif /* ABSPATH_H */
+1
hook.c
··· 1 1 #include "git-compat-util.h" 2 + #include "abspath.h" 2 3 #include "advice.h" 3 4 #include "gettext.h" 4 5 #include "hook.h"
-37
strbuf.c
··· 1 1 #include "git-compat-util.h" 2 - #include "abspath.h" 3 2 #include "alloc.h" 4 3 #include "environment.h" 5 4 #include "gettext.h" ··· 898 897 void strbuf_humanise_rate(struct strbuf *buf, off_t bytes) 899 898 { 900 899 strbuf_humanise(buf, bytes, 1); 901 - } 902 - 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 900 } 938 901 939 902 int printf_ln(const char *fmt, ...)
-22
strbuf.h
··· 536 536 int strbuf_getcwd(struct strbuf *sb); 537 537 538 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 - 560 - /** 561 539 * Normalize in-place the path contained in the strbuf. See 562 540 * normalize_path_copy() for details. If an error occurs, the contents of "sb" 563 541 * are left untouched, and -1 is returned.
+1
tempfile.c
··· 43 43 */ 44 44 45 45 #include "git-compat-util.h" 46 + #include "abspath.h" 46 47 #include "path.h" 47 48 #include "tempfile.h" 48 49 #include "sigchain.h"