Skip to content

Commit 5d1344b

Browse files
calvin-wan-googlegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 16b171f commit 5d1344b

File tree

6 files changed

+59
-59
lines changed

6 files changed

+59
-59
lines changed

abspath.c

+36
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,39 @@ char *prefix_filename_except_for_dash(const char *pfx, const char *arg)
289289
return xstrdup(arg);
290290
return prefix_filename(pfx, arg);
291291
}
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
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,25 @@ static inline int is_absolute_path(const char *path)
3030
return is_dir_sep(path[0]) || has_dos_drive_prefix(path);
3131
}
3232

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+
3354
#endif /* ABSPATH_H */

hook.c

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "git-compat-util.h"
2+
#include "abspath.h"
23
#include "advice.h"
34
#include "gettext.h"
45
#include "hook.h"

strbuf.c

-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "git-compat-util.h"
2-
#include "abspath.h"
32
#include "alloc.h"
43
#include "environment.h"
54
#include "gettext.h"
@@ -900,42 +899,6 @@ void strbuf_humanise_rate(struct strbuf *buf, off_t bytes)
900899
strbuf_humanise(buf, bytes, 1);
901900
}
902901

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-
939902
int printf_ln(const char *fmt, ...)
940903
{
941904
int ret;

strbuf.h

-22
Original file line numberDiff line numberDiff line change
@@ -535,28 +535,6 @@ int strbuf_getwholeline_fd(struct strbuf *sb, int fd, int term);
535535
*/
536536
int strbuf_getcwd(struct strbuf *sb);
537537

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-
560538
/**
561539
* Normalize in-place the path contained in the strbuf. See
562540
* normalize_path_copy() for details. If an error occurs, the contents of "sb"

tempfile.c

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
*/
4444

4545
#include "git-compat-util.h"
46+
#include "abspath.h"
4647
#include "path.h"
4748
#include "tempfile.h"
4849
#include "sigchain.h"

0 commit comments

Comments
 (0)