Skip to content

Commit add30a8

Browse files
committed
date: rfc2822 formatting uses a git_buf instead of a static string
1 parent b2c4031 commit add30a8

File tree

5 files changed

+39
-39
lines changed

5 files changed

+39
-39
lines changed

src/date.c

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "util.h"
1414
#include "cache.h"
1515
#include "posix.h"
16+
#include "date.h"
1617

1718
#include <ctype.h>
1819
#include <time.h>
@@ -875,31 +876,24 @@ int git_date_parse(git_time_t *out, const char *date)
875876
return error_ret;
876877
}
877878

878-
int git_date_rfc2822_fmt(char *out, size_t len, const git_time *date)
879+
int git_date_rfc2822_fmt(git_str *out, git_time_t time, int offset)
879880
{
880-
int written;
881-
struct tm gmt;
882881
time_t t;
882+
struct tm gmt;
883883

884884
GIT_ASSERT_ARG(out);
885-
GIT_ASSERT_ARG(date);
886885

887-
t = (time_t) (date->time + date->offset * 60);
886+
t = (time_t) (time + offset * 60);
888887

889-
if (p_gmtime_r (&t, &gmt) == NULL)
888+
if (p_gmtime_r(&t, &gmt) == NULL)
890889
return -1;
891890

892-
written = p_snprintf(out, len, "%.3s, %u %.3s %.4u %02u:%02u:%02u %+03d%02d",
891+
return git_str_printf(out, "%.3s, %u %.3s %.4u %02u:%02u:%02u %+03d%02d",
893892
weekday_names[gmt.tm_wday],
894893
gmt.tm_mday,
895894
month_names[gmt.tm_mon],
896895
gmt.tm_year + 1900,
897896
gmt.tm_hour, gmt.tm_min, gmt.tm_sec,
898-
date->offset / 60, date->offset % 60);
899-
900-
if (written < 0 || (written > (int) len - 1))
901-
return -1;
902-
903-
return 0;
897+
offset / 60, offset % 60);
904898
}
905899

src/date.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ extern int git_date_parse(git_time_t *out, const char *date);
2323
/*
2424
* Format a git_time as a RFC2822 string
2525
*
26-
* @param out buffer to store formatted date; a '\\0' terminator will automatically be added.
27-
* @param len size of the buffer; should be atleast `GIT_DATE_RFC2822_SZ` in size;
28-
* @param date the date to be formatted
26+
* @param out buffer to store formatted date
27+
* @param time the time to be formatted
28+
* @param offset the timezone offset
2929
* @return 0 if successful; -1 on error
3030
*/
31-
extern int git_date_rfc2822_fmt(char *out, size_t len, const git_time *date);
31+
extern int git_date_rfc2822_fmt(git_str *out, git_time_t time, int offset);
3232

3333
#endif

src/email.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ static int append_prefix(
7373
return git_str_oom(out) ? -1 : 0;
7474
}
7575

76+
static int append_date(
77+
git_str *out,
78+
const git_time *date)
79+
{
80+
int error;
81+
82+
if ((error = git_str_printf(out, "Date: ")) == 0 &&
83+
(error = git_date_rfc2822_fmt(out, date->time, date->offset)) == 0)
84+
error = git_str_putc(out, '\n');
85+
86+
return error;
87+
}
88+
7689
static int append_subject(
7790
git_str *out,
7891
size_t patch_idx,
@@ -118,14 +131,12 @@ static int append_header(
118131
git_email_create_options *opts)
119132
{
120133
char id[GIT_OID_HEXSZ];
121-
char date[GIT_DATE_RFC2822_SZ];
122134
int error;
123135

124136
if ((error = git_oid_fmt(id, commit_id)) < 0 ||
125137
(error = git_str_printf(out, "From %.*s %s\n", GIT_OID_HEXSZ, id, EMAIL_TIMESTAMP)) < 0 ||
126138
(error = git_str_printf(out, "From: %s <%s>\n", author->name, author->email)) < 0 ||
127-
(error = git_date_rfc2822_fmt(date, sizeof(date), &author->when)) < 0 ||
128-
(error = git_str_printf(out, "Date: %s\n", date)) < 0 ||
139+
(error = append_date(out, &author->when)) < 0 ||
129140
(error = append_subject(out, patch_idx, patch_count, summary, opts)) < 0)
130141
return error;
131142

src/util.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
# define GIT_CONTAINER_OF(ptr, type, member) (type *)(ptr)
3838
#endif
3939

40-
#define GIT_DATE_RFC2822_SZ 32
41-
4240
/**
4341
* Return the length of a constant string.
4442
* We are aware that `strlen` performs the same task and is usually

tests/date/rfc2822.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,33 @@
55
void test_date_rfc2822__format_rfc2822_no_offset(void)
66
{
77
git_time t = {1397031663, 0};
8-
char buf[GIT_DATE_RFC2822_SZ];
8+
git_str buf = GIT_STR_INIT;
99

10-
cl_git_pass(git_date_rfc2822_fmt(buf, sizeof(buf), &t));
11-
cl_assert(strcmp(buf, "Wed, 9 Apr 2014 08:21:03 +0000") == 0);
10+
cl_git_pass(git_date_rfc2822_fmt(&buf, t.time, t.offset));
11+
cl_assert_equal_s("Wed, 9 Apr 2014 08:21:03 +0000", buf.ptr);
12+
13+
git_str_dispose(&buf);
1214
}
1315

1416
void test_date_rfc2822__format_rfc2822_positive_offset(void)
1517
{
1618
git_time t = {1397031663, 120};
17-
char buf[GIT_DATE_RFC2822_SZ];
19+
git_str buf = GIT_STR_INIT;
20+
21+
cl_git_pass(git_date_rfc2822_fmt(&buf, t.time, t.offset));
22+
cl_assert_equal_s("Wed, 9 Apr 2014 10:21:03 +0200", buf.ptr);
1823

19-
cl_git_pass(git_date_rfc2822_fmt(buf, sizeof(buf), &t));
20-
cl_assert(strcmp(buf, "Wed, 9 Apr 2014 10:21:03 +0200") == 0);
24+
git_str_dispose(&buf);
2125
}
2226

2327
void test_date_rfc2822__format_rfc2822_negative_offset(void)
2428
{
2529
git_time t = {1397031663, -120};
26-
char buf[GIT_DATE_RFC2822_SZ];
30+
git_str buf = GIT_STR_INIT;
2731

28-
cl_git_pass(git_date_rfc2822_fmt(buf, sizeof(buf), &t));
29-
cl_assert(strcmp(buf, "Wed, 9 Apr 2014 06:21:03 -0200") == 0);
30-
}
31-
32-
void test_date_rfc2822__format_rfc2822_buffer_too_small(void)
33-
{
34-
/* "Wed, 10 Apr 2014 08:21:03 +0000" */
35-
git_time t = {1397031663 + 86400, 0};
36-
char buf[GIT_DATE_RFC2822_SZ-1];
32+
cl_git_pass(git_date_rfc2822_fmt(&buf, t.time, t.offset));
33+
cl_assert_equal_s("Wed, 9 Apr 2014 06:21:03 -0200", buf.ptr);
3734

38-
cl_git_fail(git_date_rfc2822_fmt(buf, sizeof(buf), &t));
35+
git_str_dispose(&buf);
3936
}
4037

0 commit comments

Comments
 (0)