git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Pierre Habouzit <madcoder@debian.org>
To: git@vger.kernel.org
Cc: Pierre Habouzit <madcoder@debian.org>
Subject: [PATCH] Simplify strbuf uses in fast-import.c using the proper functions.
Date: Tue,  4 Sep 2007 16:01:13 +0200	[thread overview]
Message-ID: <11889144743483-git-send-email-madcoder@debian.org> (raw)
In-Reply-To: <20070904115317.GA3381@artemis.corp>

  This is just cleaner way to deal with strbufs, using its API rather than
reinventing it in the module (e.g. strbuf_append_string is just the plain
strbuf_addstr function, and it was used to perform what strbuf_addch does
anyways).
---
 archive-tar.c |   65 ++++++++++++++-------------------------------------------
 1 files changed, 16 insertions(+), 49 deletions(-)

diff --git a/archive-tar.c b/archive-tar.c
index a0763c5..c84d7c0 100644
--- a/archive-tar.c
+++ b/archive-tar.c
@@ -78,19 +78,6 @@ static void write_trailer(void)
 	}
 }
 
-static void strbuf_append_string(struct strbuf *sb, const char *s)
-{
-	int slen = strlen(s);
-	int total = sb->len + slen;
-	if (total + 1 > sb->alloc) {
-		sb->buf = xrealloc(sb->buf, total + 1);
-		sb->alloc = total + 1;
-	}
-	memcpy(sb->buf + sb->len, s, slen);
-	sb->len = total;
-	sb->buf[total] = '\0';
-}
-
 /*
  * pax extended header records have the format "%u %s=%s\n".  %u contains
  * the size of the whole string (including the %u), the first %s is the
@@ -100,26 +87,17 @@ static void strbuf_append_string(struct strbuf *sb, const char *s)
 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
                                      const char *value, unsigned int valuelen)
 {
-	char *p;
-	int len, total, tmp;
+	int len, tmp;
 
 	/* "%u %s=%s\n" */
 	len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
 	for (tmp = len; tmp > 9; tmp /= 10)
 		len++;
 
-	total = sb->len + len;
-	if (total > sb->alloc) {
-		sb->buf = xrealloc(sb->buf, total);
-		sb->alloc = total;
-	}
-
-	p = sb->buf;
-	p += sprintf(p, "%u %s=", len, keyword);
-	memcpy(p, value, valuelen);
-	p += valuelen;
-	*p = '\n';
-	sb->len = total;
+	strbuf_grow(sb, len);
+	strbuf_addf(sb, "%u %s=", len, keyword);
+	strbuf_add(sb, value, valuelen);
+	strbuf_addch(sb, '\n');
 }
 
 static unsigned int ustar_header_chksum(const struct ustar_header *header)
@@ -153,8 +131,7 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
 	struct strbuf ext_header;
 
 	memset(&header, 0, sizeof(header));
-	ext_header.buf = NULL;
-	ext_header.len = ext_header.alloc = 0;
+	strbuf_init(&ext_header);
 
 	if (!sha1) {
 		*header.typeflag = TYPEFLAG_GLOBAL_HEADER;
@@ -225,8 +202,8 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
 
 	if (ext_header.len > 0) {
 		write_entry(sha1, NULL, 0, ext_header.buf, ext_header.len);
-		free(ext_header.buf);
 	}
+	strbuf_release(&ext_header);
 	write_blocked(&header, sizeof(header));
 	if (S_ISREG(mode) && buffer && size > 0)
 		write_blocked(buffer, size);
@@ -235,11 +212,11 @@ static void write_entry(const unsigned char *sha1, struct strbuf *path,
 static void write_global_extended_header(const unsigned char *sha1)
 {
 	struct strbuf ext_header;
-	ext_header.buf = NULL;
-	ext_header.len = ext_header.alloc = 0;
+
+	strbuf_init(&ext_header);
 	strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
 	write_entry(NULL, NULL, 0, ext_header.buf, ext_header.len);
-	free(ext_header.buf);
+	strbuf_release(&ext_header);
 }
 
 static int git_tar_config(const char *var, const char *value)
@@ -260,28 +237,18 @@ static int write_tar_entry(const unsigned char *sha1,
                            const char *base, int baselen,
                            const char *filename, unsigned mode, int stage)
 {
-	static struct strbuf path;
+	static struct strbuf path = STRBUF_INIT;
 	int filenamelen = strlen(filename);
 	void *buffer;
 	enum object_type type;
 	unsigned long size;
 
-	if (!path.alloc) {
-		path.buf = xmalloc(PATH_MAX);
-		path.alloc = PATH_MAX;
-		path.len = path.eof = 0;
-	}
-	if (path.alloc < baselen + filenamelen + 1) {
-		free(path.buf);
-		path.buf = xmalloc(baselen + filenamelen + 1);
-		path.alloc = baselen + filenamelen + 1;
-	}
-	memcpy(path.buf, base, baselen);
-	memcpy(path.buf + baselen, filename, filenamelen);
-	path.len = baselen + filenamelen;
-	path.buf[path.len] = '\0';
+	strbuf_grow(&path, MAX(PATH_MAX, baselen + filenamelen + 1));
+	strbuf_reset(&path);
+	strbuf_add(&path, base, baselen);
+	strbuf_add(&path, filename, filenamelen);
 	if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
-		strbuf_append_string(&path, "/");
+		strbuf_addch(&path, '/');
 		buffer = NULL;
 		size = 0;
 	} else {
-- 
1.5.3

  parent reply	other threads:[~2007-09-04 14:01 UTC|newest]

Thread overview: 90+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-09-02 22:42 strbuf API Pierre Habouzit
2007-09-03  5:43 ` Johan Herland
2007-09-03  8:46   ` Pierre Habouzit
2007-09-04  1:52     ` Miles Bader
2007-09-04  8:47       ` strbuf new semantics, let's give it a try Pierre Habouzit
2007-09-04  8:47       ` [PATCH] Rework strbuf API and semantics Pierre Habouzit
2007-09-04 11:11         ` Johannes Schindelin
2007-09-04 11:53           ` Pierre Habouzit
2007-09-04 13:34             ` Andreas Ericsson
2007-09-04 14:01             ` Pierre Habouzit
2007-09-04 15:44               ` Johannes Schindelin
2007-09-04 16:18                 ` Pierre Habouzit
2007-09-04 17:18                   ` Wincent Colaiuta
2007-09-04 14:01             ` Pierre Habouzit [this message]
2007-09-04 23:46               ` [PATCH] Simplify strbuf uses in fast-import.c using the proper functions René Scharfe
2007-09-04 23:46               ` René Scharfe
2007-09-05  7:48                 ` Pierre Habouzit
2007-09-05  8:05                   ` Junio C Hamano
2007-09-05  8:57                     ` Pierre Habouzit
2007-09-05 19:18                       ` [PATCH] Rework strbuf API and semantics Pierre Habouzit
2007-09-05 19:18                         ` [PATCH] Simplify strbuf uses in archive-tar.c using the proper functions Pierre Habouzit
2007-09-05 19:18                           ` [PATCH] Simplify strbuf uses in fast-import.c " Pierre Habouzit
2007-09-05 19:18                             ` [PATCH] Use proper strbuf API, and also simplify cmd_data code Pierre Habouzit
2007-09-05 19:18                               ` [PATCH] Simplify write_tree using strbuf's Pierre Habouzit
2007-09-05 19:18                                 ` [PATCH] Further strbuf re-engineering Pierre Habouzit
2007-09-05 19:18                                   ` [PATCH] Eradicate yet-another-buffer implementation in buitin-rerere.c Pierre Habouzit
2007-09-05 19:18                                     ` [PATCH] More strbuf uses in cache-tree.c Pierre Habouzit
2007-09-19  8:05                                   ` [PATCH] Further strbuf re-engineering Junio C Hamano
2007-09-05 19:21                             ` [PATCH] Simplify strbuf uses in fast-import.c using the proper functions Pierre Habouzit
2007-09-19  8:06                           ` [PATCH] Simplify strbuf uses in archive-tar.c " Junio C Hamano
2007-09-19  8:36                             ` Pierre Habouzit
2007-09-06  9:31                         ` [PATCH] Rework strbuf API and semantics Junio C Hamano
2007-09-06  9:49                           ` Pierre Habouzit
2007-09-06 10:03                         ` Junio C Hamano
2007-09-06 10:22                           ` Pierre Habouzit
2007-09-04 14:01             ` [PATCH] Use proper strbuf API, and also simplify cmd_data code Pierre Habouzit
2007-09-05  4:44             ` [PATCH] Rework strbuf API and semantics Miles Bader
2007-09-04  8:48       ` [PATCH] Add strbuf_fread, use it in fast-import.c Pierre Habouzit
2007-09-03  8:32 ` strbuf API Matthieu Moy
2007-09-03  8:49   ` Pierre Habouzit
2007-09-03  9:02     ` Matthieu Moy
2007-09-03  9:18     ` Junio C Hamano
2007-09-03 11:53       ` Pierre Habouzit
2007-09-03 12:29       ` Johannes Schindelin
2007-09-06 11:20 ` strbuf new API, take 2 for inclusion Pierre Habouzit
2007-09-06 11:20   ` [PATCH 1/7] Rework strbuf API and semantics Pierre Habouzit
2007-09-06 11:20     ` [PATCH 2/7] Simplify strbuf uses in archive-tar.c using the proper functions Pierre Habouzit
2007-09-06 11:20       ` [PATCH 3/7] Use proper strbuf API, and also simplify cmd_data code Pierre Habouzit
2007-09-06 11:20         ` [PATCH 4/7] Simplify write_tree using strbuf's Pierre Habouzit
2007-09-06 11:20           ` [PATCH 5/7] Further strbuf re-engineering Pierre Habouzit
2007-09-06 11:20             ` [PATCH 6/7] Eradicate yet-another-buffer implementation in buitin-rerere.c Pierre Habouzit
2007-09-06 11:20               ` [PATCH 7/7] More strbuf uses in cache-tree.c Pierre Habouzit
2007-09-06 14:05               ` [PATCH 6/7] Eradicate yet-another-buffer implementation in buitin-rerere.c Johannes Schindelin
2007-09-06 17:17                 ` Pierre Habouzit
2007-09-06 20:16                   ` David Kastrup
2007-09-06 20:54                     ` Pierre Habouzit
2007-09-07  8:03                   ` Junio C Hamano
2007-09-07  9:02                     ` Pierre Habouzit
2007-09-06 17:59       ` [PATCH 2/7] Simplify strbuf uses in archive-tar.c using the proper functions Kristian Høgsberg
2007-09-06 18:08         ` Pierre Habouzit
2007-09-06 18:18           ` Kristian Høgsberg
2007-09-06 18:27             ` Pierre Habouzit
2007-09-06 22:54               ` René Scharfe
2007-09-06 14:09     ` [PATCH 1/7] Rework strbuf API and semantics Johannes Schindelin
2007-09-06 14:21       ` Jeff King
2007-09-06 14:44         ` David Kastrup
2007-09-06 14:50           ` Jeff King
2007-09-06 15:06             ` David Kastrup
2007-09-06 15:36               ` Jeff King
2007-09-06 15:53                 ` David Kastrup
2007-09-06 15:45               ` Johannes Sixt
2007-09-06 14:43       ` David Kastrup
2007-09-06 14:52         ` Jeff King
2007-09-06 17:49     ` Kristian Høgsberg
2007-09-06 12:58   ` strbuf new API, take 2 for inclusion Jeff King
2007-09-06 17:15     ` Pierre Habouzit
2007-09-06 17:16       ` Jeff King
2007-09-06 17:19         ` Pierre Habouzit
2007-09-08 11:53 ` Use strbufs in commit.c (pretty printing) Pierre Habouzit
2007-09-08 11:53   ` [PATCH 1/3] Add strbuf_rtrim (to remove trailing spaces) Pierre Habouzit
2007-09-08 11:53     ` [PATCH 2/3] Change semantics of interpolate to work like snprintf Pierre Habouzit
2007-09-08 11:53       ` [PATCH 3/3] Rework pretty_print_commit to use strbufs instead of custom buffers Pierre Habouzit
2007-09-08 11:59         ` David Kastrup
2007-09-08 12:17           ` Pierre Habouzit
2007-09-08 12:28           ` Pierre Habouzit
2007-09-08 18:40         ` René Scharfe
2007-09-08 18:49           ` Pierre Habouzit
2007-09-08 16:18     ` [PATCH 1/3] Add strbuf_rtrim (to remove trailing spaces) René Scharfe
2007-09-08 22:53       ` Pierre Habouzit
2007-09-08 23:44         ` Pierre Habouzit

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=11889144743483-git-send-email-madcoder@debian.org \
    --to=madcoder@debian.org \
    --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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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).