Skip to content

Commit dddbad7

Browse files
dschogitster
authored andcommitted
timestamp_t: a new data type for timestamps
Git's source code assumes that unsigned long is at least as precise as time_t. Which is incorrect, and causes a lot of problems, in particular where unsigned long is only 32-bit (notably on Windows, even in 64-bit versions). So let's just use a more appropriate data type instead. In preparation for this, we introduce the new `timestamp_t` data type. By necessity, this is a very, very large patch, as it has to replace all timestamps' data type in one go. As we will use a data type that is not necessarily identical to `time_t`, we need to be very careful to use `time_t` whenever we interact with the system functions, and `timestamp_t` everywhere else. Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cb71f8b commit dddbad7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+170
-158
lines changed

Documentation/technical/api-parse-options.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,13 @@ There are some macros to easily define options:
183183
scale the provided value by 1024, 1024^2 or 1024^3 respectively.
184184
The scaled value is put into `unsigned_long_var`.
185185

186-
`OPT_DATE(short, long, &int_var, description)`::
186+
`OPT_DATE(short, long, &timestamp_t_var, description)`::
187187
Introduce an option with date argument, see `approxidate()`.
188-
The timestamp is put into `int_var`.
188+
The timestamp is put into `timestamp_t_var`.
189189

190-
`OPT_EXPIRY_DATE(short, long, &int_var, description)`::
190+
`OPT_EXPIRY_DATE(short, long, &timestamp_t_var, description)`::
191191
Introduce an option with expiry date argument, see `parse_expiry_date()`.
192-
The timestamp is put into `int_var`.
192+
The timestamp is put into `timestamp_t_var`.
193193

194194
`OPT_CALLBACK(short, long, &var, arg_str, description, func_ptr)`::
195195
Introduce an option with argument.

archive-tar.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@ static int write_tar_filter_archive(const struct archiver *ar,
2727
*/
2828
#if ULONG_MAX == 0xFFFFFFFF
2929
#define USTAR_MAX_SIZE ULONG_MAX
30-
#define USTAR_MAX_MTIME ULONG_MAX
3130
#else
32-
#define USTAR_MAX_SIZE 077777777777UL
31+
#define USTAR_MAX_SIZE 077777777777ULL
32+
#endif
33+
#if TIME_MAX == 0xFFFFFFFF
34+
#define USTAR_MAX_MTIME TIME_MAX
35+
#else
3336
#define USTAR_MAX_MTIME 077777777777UL
3437
#endif
3538

archive-zip.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,17 @@ static void write_zip_trailer(const unsigned char *sha1)
545545
write_or_die(1, sha1_to_hex(sha1), GIT_SHA1_HEXSZ);
546546
}
547547

548-
static void dos_time(time_t *time, int *dos_date, int *dos_time)
548+
static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
549549
{
550-
struct tm *t = localtime(time);
550+
time_t time;
551+
struct tm *t;
552+
553+
if (date_overflows(*timestamp))
554+
die("timestamp too large for this system: %"PRItime,
555+
*timestamp);
556+
time = (time_t)*timestamp;
557+
t = localtime(&time);
558+
*timestamp = time;
551559

552560
*dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
553561
(t->tm_year + 1900 - 1980) * 512;

archive.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct archiver_args {
99
struct tree *tree;
1010
const unsigned char *commit_sha1;
1111
const struct commit *commit;
12-
time_t time;
12+
timestamp_t time;
1313
struct pathspec pathspec;
1414
unsigned int verbose : 1;
1515
unsigned int worktree_attributes : 1;

builtin/am.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ static int hg_patch_to_mail(FILE *out, FILE *in, int keep_cr)
877877
if (skip_prefix(sb.buf, "# User ", &str))
878878
fprintf(out, "From: %s\n", str);
879879
else if (skip_prefix(sb.buf, "# Date ", &str)) {
880-
unsigned long timestamp;
880+
timestamp_t timestamp;
881881
long tz, tz2;
882882
char *end;
883883

builtin/blame.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,13 +1561,13 @@ static void pass_blame(struct scoreboard *sb, struct origin *origin, int opt)
15611561
struct commit_info {
15621562
struct strbuf author;
15631563
struct strbuf author_mail;
1564-
unsigned long author_time;
1564+
timestamp_t author_time;
15651565
struct strbuf author_tz;
15661566

15671567
/* filled only when asked for details */
15681568
struct strbuf committer;
15691569
struct strbuf committer_mail;
1570-
unsigned long committer_time;
1570+
timestamp_t committer_time;
15711571
struct strbuf committer_tz;
15721572

15731573
struct strbuf summary;
@@ -1578,7 +1578,7 @@ struct commit_info {
15781578
*/
15791579
static void get_ac_line(const char *inbuf, const char *what,
15801580
struct strbuf *name, struct strbuf *mail,
1581-
unsigned long *time, struct strbuf *tz)
1581+
timestamp_t *time, struct strbuf *tz)
15821582
{
15831583
struct ident_split ident;
15841584
size_t len, maillen, namelen;
@@ -1837,7 +1837,7 @@ static void assign_blame(struct scoreboard *sb, int opt)
18371837
stop_progress(&pi.progress);
18381838
}
18391839

1840-
static const char *format_time(unsigned long time, const char *tz_str,
1840+
static const char *format_time(timestamp_t time, const char *tz_str,
18411841
int show_raw_time)
18421842
{
18431843
static struct strbuf time_buf = STRBUF_INIT;

builtin/fsck.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ static int fsck_obj_buffer(const unsigned char *sha1, enum object_type type,
397397
static int default_refs;
398398

399399
static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
400-
unsigned long timestamp)
400+
timestamp_t timestamp)
401401
{
402402
struct object *obj;
403403

@@ -418,7 +418,7 @@ static void fsck_handle_reflog_oid(const char *refname, struct object_id *oid,
418418
}
419419

420420
static int fsck_handle_reflog_ent(struct object_id *ooid, struct object_id *noid,
421-
const char *email, unsigned long timestamp, int tz,
421+
const char *email, timestamp_t timestamp, int tz,
422422
const char *message, void *cb_data)
423423
{
424424
const char *refname = cb_data;

builtin/gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static int aggressive_window = 250;
3333
static int gc_auto_threshold = 6700;
3434
static int gc_auto_pack_limit = 50;
3535
static int detach_auto = 1;
36-
static unsigned long gc_log_expire_time;
36+
static timestamp_t gc_log_expire_time;
3737
static const char *gc_log_expire = "1.day.ago";
3838
static const char *prune_expire = "2.weeks.ago";
3939
static const char *prune_worktrees_expire = "3.months.ago";

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ static void gen_message_id(struct rev_info *info, char *base)
911911
{
912912
struct strbuf buf = STRBUF_INIT;
913913
strbuf_addf(&buf, "%s.%"PRItime".git.%s", base,
914-
(unsigned long) time(NULL),
914+
(timestamp_t) time(NULL),
915915
git_committer_info(IDENT_NO_NAME|IDENT_NO_DATE|IDENT_STRICT));
916916
info->message_id = strbuf_detach(&buf, NULL);
917917
}

builtin/merge-base.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static void add_one_commit(struct object_id *oid, struct rev_collect *revs)
132132
}
133133

134134
static int collect_one_reflog_ent(struct object_id *ooid, struct object_id *noid,
135-
const char *ident, unsigned long timestamp,
135+
const char *ident, timestamp_t timestamp,
136136
int tz, const char *message, void *cbdata)
137137
{
138138
struct rev_collect *revs = cbdata;

0 commit comments

Comments
 (0)