Git Mailing List Archive mirror
 help / color / mirror / Atom feed
From: Calvin Wan <calvinwan@google.com>
To: git@vger.kernel.org
Cc: Calvin Wan <calvinwan@google.com>
Subject: [PATCH v7 3/7] abspath: move related functions to abspath
Date: Tue,  6 Jun 2023 19:48:39 +0000	[thread overview]
Message-ID: <20230606194843.2054314-3-calvinwan@google.com> (raw)
In-Reply-To: <20230606194720.2053551-1-calvinwan@google.com>

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>
---
 abspath.c  | 36 ++++++++++++++++++++++++++++++++++++
 abspath.h  | 21 +++++++++++++++++++++
 hook.c     |  1 +
 strbuf.c   | 37 -------------------------------------
 strbuf.h   | 22 ----------------------
 tempfile.c |  1 +
 6 files changed, 59 insertions(+), 59 deletions(-)

diff --git a/abspath.c b/abspath.c
index d032f5dce5..1202cde23d 100644
--- a/abspath.c
+++ b/abspath.c
@@ -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);
+}
diff --git a/abspath.h b/abspath.h
index 7cd3de5e9d..4653080d5e 100644
--- a/abspath.h
+++ b/abspath.h
@@ -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 */
diff --git a/hook.c b/hook.c
index 3ca5e60895..f6306d72b3 100644
--- a/hook.c
+++ b/hook.c
@@ -1,4 +1,5 @@
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "advice.h"
 #include "gettext.h"
 #include "hook.h"
diff --git a/strbuf.c b/strbuf.c
index f5dfd093a0..a2249ae7ed 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -1,5 +1,4 @@
 #include "git-compat-util.h"
-#include "abspath.h"
 #include "alloc.h"
 #include "environment.h"
 #include "gettext.h"
@@ -900,42 +899,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;
diff --git a/strbuf.h b/strbuf.h
index 70778c6e10..207efb4f98 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -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"
diff --git a/tempfile.c b/tempfile.c
index 50c377134c..6c88a63b42 100644
--- a/tempfile.c
+++ b/tempfile.c
@@ -43,6 +43,7 @@
  */
 
 #include "git-compat-util.h"
+#include "abspath.h"
 #include "path.h"
 #include "tempfile.h"
 #include "sigchain.h"
-- 
2.41.0.162.gfafddb0af9-goog


  parent reply	other threads:[~2023-06-06 19:49 UTC|newest]

Thread overview: 85+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-02 21:14 [PATCH 0/6] strbuf cleanups Calvin Wan
2023-05-02 21:14 ` [PATCH 1/6] abspath: move related functions to abspath Calvin Wan
2023-05-02 21:42   ` Junio C Hamano
2023-05-02 21:14 ` [PATCH 2/6] credential-store: move related functions to credential-store file Calvin Wan
2023-05-02 21:52   ` Junio C Hamano
2023-05-03 16:28   ` Jeff King
2023-05-03 16:34     ` Jeff King
2023-05-03 18:38       ` Calvin Wan
2023-05-02 21:14 ` [PATCH 3/6] object-name: move related functions to object-name Calvin Wan
2023-05-02 21:14 ` [PATCH 4/6] path: move related function to path Calvin Wan
2023-05-02 21:14 ` [PATCH 5/6] strbuf: clarify dependency Calvin Wan
2023-05-03  1:56   ` Elijah Newren
2023-05-02 21:14 ` [PATCH 6/6] strbuf: remove environment variables Calvin Wan
2023-05-03  2:15   ` Elijah Newren
2023-05-02 22:20 ` [PATCH 0/6] strbuf cleanups Junio C Hamano
2023-05-02 22:31   ` Junio C Hamano
2023-05-02 23:51     ` Felipe Contreras
2023-05-02 22:36   ` Calvin Wan
2023-05-03  2:37 ` Elijah Newren
2023-05-03 18:00   ` Calvin Wan
2023-05-07  0:14     ` Elijah Newren
2023-05-07 13:14       ` Jeff King
2023-05-03 18:48 ` [PATCH v2 0/7] " Calvin Wan
2023-05-03 18:50   ` [PATCH v2 1/7] strbuf: clarify API boundary Calvin Wan
2023-05-03 18:50   ` [PATCH v2 2/7] abspath: move related functions to abspath Calvin Wan
2023-05-03 18:50   ` [PATCH v2 3/7] credential-store: move related functions to credential-store file Calvin Wan
2023-05-03 18:50   ` [PATCH v2 4/7] object-name: move related functions to object-name Calvin Wan
2023-05-03 18:50   ` [PATCH v2 5/7] path: move related function to path Calvin Wan
2023-05-03 18:50   ` [PATCH v2 6/7] strbuf: clarify dependency Calvin Wan
2023-05-03 19:26     ` Junio C Hamano
2023-05-03 18:50   ` [PATCH v2 7/7] strbuf: remove environment variables Calvin Wan
2023-05-03 19:24     ` Junio C Hamano
2023-05-03 19:41       ` Calvin Wan
2023-05-03 19:45         ` Junio C Hamano
2023-05-03 19:42     ` [PATCH v3 7/7] strbuf: remove environment variable Calvin Wan
2023-05-05 22:33   ` [PATCH v2 0/7] strbuf cleanups Junio C Hamano
2023-05-08 16:38     ` Calvin Wan
2023-05-07  0:40   ` Elijah Newren
2023-05-07 21:47     ` Felipe Contreras
2023-05-08 16:57   ` [PATCH v4 " Calvin Wan
2023-05-08 16:59     ` [PATCH v4 1/7] strbuf: clarify API boundary Calvin Wan
2023-05-08 17:22       ` Eric Sunshine
2023-05-10 22:51         ` Junio C Hamano
2023-05-08 16:59     ` [PATCH v4 2/7] abspath: move related functions to abspath Calvin Wan
2023-05-08 16:59     ` [PATCH v4 3/7] credential-store: move related functions to credential-store file Calvin Wan
2023-05-08 16:59     ` [PATCH v4 4/7] object-name: move related functions to object-name Calvin Wan
2023-05-08 16:59     ` [PATCH v4 5/7] path: move related function to path Calvin Wan
2023-05-08 16:59     ` [PATCH v4 6/7] strbuf: clarify dependency Calvin Wan
2023-05-08 16:59     ` [PATCH v4 7/7] strbuf: remove global variable Calvin Wan
2023-05-10  8:12       ` Phillip Wood
2023-05-09  1:57     ` [PATCH v4 0/7] strbuf cleanups Elijah Newren
2023-05-09  2:13       ` Felipe Contreras
2023-05-11 19:44     ` [PATCH v5 " Calvin Wan
2023-05-11 19:48       ` [PATCH v5 1/7] strbuf: clarify API boundary Calvin Wan
2023-05-11 19:57         ` Eric Sunshine
2023-05-11 20:03           ` Calvin Wan
2023-05-11 19:48       ` [PATCH v5 2/7] abspath: move related functions to abspath Calvin Wan
2023-05-11 19:48       ` [PATCH v5 3/7] credential-store: move related functions to credential-store file Calvin Wan
2023-05-11 19:48       ` [PATCH v5 4/7] object-name: move related functions to object-name Calvin Wan
2023-05-11 19:48       ` [PATCH v5 5/7] path: move related function to path Calvin Wan
2023-05-11 19:48       ` [PATCH v5 6/7] strbuf: clarify dependency Calvin Wan
2023-05-11 19:48       ` [PATCH v5 7/7] strbuf: remove global variable Calvin Wan
2023-05-11 20:24         ` Eric Sunshine
2023-05-11 21:42         ` Junio C Hamano
2023-05-12 14:54           ` Phillip Wood
2023-05-12 14:53         ` Phillip Wood
2023-05-12 19:31           ` Junio C Hamano
2023-05-12 17:14       ` [PATCH v6 0/7] strbuf cleanups Calvin Wan
2023-05-12 17:15         ` [PATCH v6 1/7] strbuf: clarify API boundary Calvin Wan
2023-05-12 17:15         ` [PATCH v6 2/7] abspath: move related functions to abspath Calvin Wan
2023-05-12 17:15         ` [PATCH v6 3/7] credential-store: move related functions to credential-store file Calvin Wan
2023-05-12 17:15         ` [PATCH v6 4/7] object-name: move related functions to object-name Calvin Wan
2023-05-12 17:15         ` [PATCH v6 5/7] path: move related function to path Calvin Wan
2023-05-12 17:15         ` [PATCH v6 6/7] strbuf: clarify dependency Calvin Wan
2023-05-12 17:15         ` [PATCH v6 7/7] strbuf: remove global variable Calvin Wan
2023-05-12 20:24         ` [PATCH v6 0/7] strbuf cleanups Junio C Hamano
2023-05-13  5:54           ` Eric Sunshine
2023-06-06 19:47         ` [PATCH v7 " Calvin Wan
2023-06-06 19:48           ` [PATCH v7 1/7] strbuf: clarify API boundary Calvin Wan
2023-06-06 19:48           ` [PATCH v7 2/7] strbuf: clarify dependency Calvin Wan
2023-06-06 19:48           ` Calvin Wan [this message]
2023-06-06 19:48           ` [PATCH v7 4/7] credential-store: move related functions to credential-store file Calvin Wan
2023-06-06 19:48           ` [PATCH v7 5/7] object-name: move related functions to object-name Calvin Wan
2023-06-06 19:48           ` [PATCH v7 6/7] path: move related function to path Calvin Wan
2023-06-06 19:48           ` [PATCH v7 7/7] strbuf: remove global variable Calvin Wan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230606194843.2054314-3-calvinwan@google.com \
    --to=calvinwan@google.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).