git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 5/7] Add strbuf_read_file().

From
Kristian Høgsberg <krh@redhat.com>
Date
Sep 18, 2007, 00:06 UTC
Message-ID
<11900740154136-git-send-email-krh@redhat.com>
In-Reply-To
<11900740153845-git-send-email-krh@redhat.com>
Signed-off-by: Kristian Høgsberg <krh@redhat.com>
---
 builtin-tag.c |   10 +++-------
 strbuf.c      |   15 +++++++++++++++
 strbuf.h      |    1 +
 3 files changed, 19 insertions(+), 7 deletions(-)
diff --git a/builtin-tag.c b/builtin-tag.c
index 68c9a20..c97673f 100644
--- a/builtin-tag.c
+++ b/builtin-tag.c
@@ -53,13 +53,9 @@ static void launch_editor(const char *path, struct strbuf *buffer)
 	if (run_command(&child))
 		die("There was a problem with the editor %s.", editor);
 
-	fd = open(path, O_RDONLY);
-	if (fd < 0)
-		die("could not open '%s': %s", path, strerror(errno));
-	if (strbuf_read(buffer, fd, 0) < 0) {
-		die("could not read message file '%s': %s", path, strerror(errno));
-	}
-	close(fd);
+	if (strbuf_read_file(buffer, path) < 0)
+		die("could not read message file '%s': %s",
+		    path, strerror(errno));
 }
 
 struct tag_filter {
diff --git a/strbuf.c b/strbuf.c
index d919047..438606d 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -110,6 +110,21 @@ ssize_t strbuf_read(struct strbuf *sb, int fd, size_t hint)
 	return sb->len - oldlen;
 }
 
+int strbuf_read_file(struct strbuf *sb, const char *path)
+{
+	int fd, len;
+
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return -1;
+	len = strbuf_read(sb, fd, 0);
+	close(fd);
+	if (len < 0)
+		return -1;
+
+	return len;
+}
+
 void read_line(struct strbuf *sb, FILE *fp, int term) {
 	int ch;
 	if (feof(fp)) {
diff --git a/strbuf.h b/strbuf.h
index 5960637..3a523ab 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -95,6 +95,7 @@ extern void strbuf_addf(struct strbuf *sb, const char *fmt, ...);
 extern size_t strbuf_fread(struct strbuf *, size_t, FILE *);
 /* XXX: if read fails, any partial read is undone */
 extern ssize_t strbuf_read(struct strbuf *, int fd, size_t hint);
+extern int strbuf_read_file(struct strbuf *sb, const char *path);
 
 extern void read_line(struct strbuf *, FILE *, int);
 
-- 
1.5.3.1.993.gbf388-dirty
Previous: Kristian HøgsbergNext: Kristian Høgsberg
Message 5 of 18 in “Enable wt-status output to a given FILE pointer.”
  1. 1/7 Enable wt-status output to a given FILE pointer.Kristian Høgsberg, Sep 18, 2007
  2. 2/7 Enable wt-status to run against non-standard index file.Kristian Høgsberg, Sep 18, 2007
  3. 3/7 Introduce entry point for launching add--interactive.Kristian Høgsberg, Sep 18, 2007
  4. 4/7 Clean up stripspace a bit, use strbuf even more.Kristian Høgsberg, Sep 18, 2007
  5. 5/7 Add strbuf_read_file().Kristian Høgsberg, Sep 18, 2007
  6. 6/7 Export rerere() and launch_editor().Kristian Høgsberg, Sep 18, 2007
  7. 7/7 Implement git commit as a builtin command.Kristian Høgsberg, Sep 18, 2007
  8. Johannes SchindelinSep 18, 2007
  9. Kristian HøgsbergSep 18, 2007
  10. Junio C HamanoSep 20, 2007
  11. Kristian HøgsbergSep 21, 2007
  12. Junio C HamanoSep 21, 2007
  13. Kristian HøgsbergSep 24, 2007
  14. Johannes SchindelinSep 18, 2007
  15. Junio C HamanoSep 19, 2007
  16. Kristian HøgsbergSep 21, 2007
  17. Johannes SchindelinSep 18, 2007
  18. Kristian HøgsbergSep 18, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.