From: Timo Sirainen <tss@iki.fi>
To: git@vger.kernel.org
Subject: [PATCH 3/3] Use stringbuf to clean up some string handling code.
Date: Sun, 20 May 2007 05:25:42 +0300 [thread overview]
Message-ID: <1179627942.32181.1288.camel@hurina> (raw)
[-- Attachment #1: Type: text/plain, Size: 4198 bytes --]
---
commit.c | 30 +++++++++++++-----------------
local-fetch.c | 34 ++++++++++++++++------------------
2 files changed, 29 insertions(+), 35 deletions(-)
diff --git a/commit.c b/commit.c
index bee066f..58f1718 100644
--- a/commit.c
+++ b/commit.c
@@ -6,6 +6,7 @@
#include "interpolate.h"
#include "diff.h"
#include "revision.h"
+#include "str.h"
int save_commit_buffer = 1;
@@ -821,7 +822,7 @@ static long format_commit_message(const struct
commit *commit,
ILEFT_RIGHT,
};
struct commit_list *p;
- char parents[1024];
+ stringbuf(parents, 1024);
int i;
enum { HEADER, SUBJECT, BODY } state;
@@ -853,22 +854,17 @@ static long format_commit_message(const struct
commit *commit,
? "<"
: ">");
- parents[1] = 0;
- for (i = 0, p = commit->parents;
- p && i < sizeof(parents) - 1;
- p = p->next)
- i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
- sha1_to_hex(p->item->object.sha1));
- interp_set_entry(table, IPARENTS, parents + 1);
-
- parents[1] = 0;
- for (i = 0, p = commit->parents;
- p && i < sizeof(parents) - 1;
- p = p->next)
- i += snprintf(parents + i, sizeof(parents) - i - 1, " %s",
- find_unique_abbrev(p->item->object.sha1,
- DEFAULT_ABBREV));
- interp_set_entry(table, IPARENTS_ABBREV, parents + 1);
+ str_c(parents)[1] = 0;
+ for (p = commit->parents; p; p = p->next)
+ str_printfa(parents, " %s", sha1_to_hex(p->item->object.sha1));
+ interp_set_entry(table, IPARENTS, str_c(parents) + 1);
+
+ str_c(parents)[1] = 0;
+ for (p = commit->parents; p; p = p->next)
+ str_printfa(parents, " %s",
+ find_unique_abbrev(p->item->object.sha1,
+ DEFAULT_ABBREV));
+ interp_set_entry(table, IPARENTS_ABBREV, str_c(parents) + 1);
for (i = 0, state = HEADER; msg[i] && state < BODY; i++) {
int eol;
diff --git a/local-fetch.c b/local-fetch.c
index 4b650ef..6d0599f 100644
--- a/local-fetch.c
+++ b/local-fetch.c
@@ -4,6 +4,7 @@
#include "cache.h"
#include "commit.h"
#include "fetch.h"
+#include "str.h"
static int use_link;
static int use_symlink;
@@ -21,12 +22,11 @@ static struct packed_git *packs;
static void setup_index(unsigned char *sha1)
{
struct packed_git *new_pack;
- char filename[PATH_MAX];
- strcpy(filename, path);
- strcat(filename, "/objects/pack/pack-");
- strcat(filename, sha1_to_hex(sha1));
- strcat(filename, ".idx");
- new_pack = parse_pack_index_file(sha1, filename);
+ stringbuf(filename, PATH_MAX);
+
+ str_printfa(filename, "%s/objects/pack/pack-%s.idx",
+ path, sha1_to_hex(sha1));
+ new_pack = parse_pack_index_file(sha1, str_c(filename));
new_pack->next = packs;
packs = new_pack;
}
@@ -35,10 +35,11 @@ static int setup_indices(void)
{
DIR *dir;
struct dirent *de;
- char filename[PATH_MAX];
+ stringbuf(filename, PATH_MAX);
unsigned char sha1[20];
- sprintf(filename, "%s/objects/pack/", path);
- dir = opendir(filename);
+
+ str_printfa(filename, "%s/objects/pack/", path);
+ dir = opendir(str_c(filename));
if (!dir)
return -1;
while ((de = readdir(dir)) != NULL) {
@@ -137,20 +138,17 @@ static int fetch_pack(const unsigned char *sha1)
static int fetch_file(const unsigned char *sha1)
{
static int object_name_start = -1;
- static char filename[PATH_MAX];
+ static stringbuf(filename, PATH_MAX);
char *hex = sha1_to_hex(sha1);
char *dest_filename = sha1_file_name(sha1);
if (object_name_start < 0) {
- strcpy(filename, path); /* e.g. git.git */
- strcat(filename, "/objects/");
- object_name_start = strlen(filename);
+ str_printfa(filename, "%s/objects/", path); /* e.g. git.git */
+ object_name_start = str_len(filename);
}
- filename[object_name_start+0] = hex[0];
- filename[object_name_start+1] = hex[1];
- filename[object_name_start+2] = '/';
- strcpy(filename + object_name_start + 3, hex + 2);
- return copy_file(filename, dest_filename, hex, 0);
+ str_truncate(filename, object_name_start);
+ str_printfa(filename, "%c%c/%s", hex[0], hex[1], hex + 2);
+ return copy_file(str_c(filename), dest_filename, hex, 0);
}
int fetch(unsigned char *sha1)
--
1.5.1.4
[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
next reply other threads:[~2007-05-20 2:58 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-05-20 2:25 Timo Sirainen [this message]
2007-05-20 9:56 ` [PATCH 3/3] Use stringbuf to clean up some string handling code Alex Riesen
2007-05-20 10:04 ` Junio C Hamano
2007-05-20 11:19 ` Timo Sirainen
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=1179627942.32181.1288.camel@hurina \
--to=tss@iki.fi \
--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