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

[PATCH 15/25] config: add git_config_get_timestamp()

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Feb 23, 2022, 18:30 UTC
Message-ID
<7ec7ba3f328f9d6fa7f919ce18a138840d44ee49.1645641063.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1160.git.1645641063.gitgitgadget@gmail.com>
From: Derrick Stolee <derrickstolee@github.com>

The existing config parsing methods do not include a way to consistently parse timestamps across all platforms. Recall that "unsigned long" is 32 bits on 64-bit Windows, so git_config_get_ulong() is insufficient.

Adding a new type requires quite a bit of boilerplate to match the style of other types.

RFC-QUESTION: Would this be better to use uintmax_t, which could be cast
to timestamp_t or other types more robust than "unsigned long"?
Signed-off-by: Derrick Stolee <derrickstolee@github.com>
---
 config.c | 39 +++++++++++++++++++++++++++++++++++++++
 config.h | 14 ++++++++++++++
 2 files changed, 53 insertions(+)
diff --git a/config.c b/config.c
index e0c03d154c9..84021b7d504 100644
--- a/config.c
+++ b/config.c
@@ -1228,6 +1228,15 @@ int git_parse_ulong(const char *value, unsigned long *ret)
 	return 1;
 }
 
+int git_parse_timestamp(const char *value, timestamp_t *ret)
+{
+	uintmax_t tmp;
+	if (!git_parse_unsigned(value, &tmp, maximum_unsigned_value_of_type(timestamp_t)))
+		return 0;
+	*ret = tmp;
+	return 1;
+}
+
 int git_parse_ssize_t(const char *value, ssize_t *ret)
 {
 	intmax_t tmp;
@@ -1296,6 +1305,14 @@ unsigned long git_config_ulong(const char *name, const char *value)
 	return ret;
 }
 
+timestamp_t git_config_timestamp(const char *name, const char *value)
+{
+	timestamp_t ret;
+	if (!git_parse_timestamp(value, &ret))
+		die_bad_number(name, value);
+	return ret;
+}
+
 ssize_t git_config_ssize_t(const char *name, const char *value)
 {
 	ssize_t ret;
@@ -2328,6 +2345,16 @@ int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned lon
 		return 1;
 }
 
+int git_configset_get_timestamp(struct config_set *cs, const char *key, timestamp_t *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_timestamp(key, value);
+		return 0;
+	} else
+		return 1;
+}
+
 int git_configset_get_bool(struct config_set *cs, const char *key, int *dest)
 {
 	const char *value;
@@ -2471,6 +2498,13 @@ int repo_config_get_ulong(struct repository *repo,
 	return git_configset_get_ulong(repo->config, key, dest);
 }
 
+int repo_config_get_timestamp(struct repository *repo,
+			      const char *key, timestamp_t *dest)
+{
+	git_config_check_init(repo);
+	return git_configset_get_timestamp(repo->config, key, dest);
+}
+
 int repo_config_get_bool(struct repository *repo,
 			 const char *key, int *dest)
 {
@@ -2544,6 +2578,11 @@ int git_config_get_ulong(const char *key, unsigned long *dest)
 	return repo_config_get_ulong(the_repository, key, dest);
 }
 
+int git_config_get_timestamp(const char *key, timestamp_t *dest)
+{
+	return repo_config_get_timestamp(the_repository, key, dest);
+}
+
 int git_config_get_bool(const char *key, int *dest)
 {
 	return repo_config_get_bool(the_repository, key, dest);
diff --git a/config.h b/config.h
index ab0106d2875..a6e4d35da0a 100644
--- a/config.h
+++ b/config.h
@@ -206,6 +206,7 @@ int config_with_options(config_fn_t fn, void *,
 
 int git_parse_ssize_t(const char *, ssize_t *);
 int git_parse_ulong(const char *, unsigned long *);
+int git_parse_timestamp(const char *, timestamp_t *);
 
 /**
  * Same as `git_config_bool`, except that it returns -1 on error rather
@@ -226,6 +227,11 @@ int64_t git_config_int64(const char *, const char *);
  */
 unsigned long git_config_ulong(const char *, const char *);
 
+/**
+ * Identical to `git_config_int`, but for (unsigned) timestamps.
+ */
+timestamp_t git_config_timestamp(const char *name, const char *value);
+
 ssize_t git_config_ssize_t(const char *, const char *);
 
 /**
@@ -469,6 +475,7 @@ int git_configset_get_string(struct config_set *cs, const char *key, char **dest
 int git_configset_get_string_tmp(struct config_set *cs, const char *key, const char **dest);
 int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
 int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest);
+int git_configset_get_timestamp(struct config_set *cs, const char *key, timestamp_t *dest);
 int git_configset_get_bool(struct config_set *cs, const char *key, int *dest);
 int git_configset_get_bool_or_int(struct config_set *cs, const char *key, int *is_bool, int *dest);
 int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest);
@@ -489,6 +496,8 @@ int repo_config_get_int(struct repository *repo,
 			const char *key, int *dest);
 int repo_config_get_ulong(struct repository *repo,
 			  const char *key, unsigned long *dest);
+int repo_config_get_timestamp(struct repository *repo,
+			      const char *key, timestamp_t *dest);
 int repo_config_get_bool(struct repository *repo,
 			 const char *key, int *dest);
 int repo_config_get_bool_or_int(struct repository *repo,
@@ -558,6 +567,11 @@ int git_config_get_int(const char *key, int *dest);
  */
 int git_config_get_ulong(const char *key, unsigned long *dest);
 
+/**
+ * Similar to `git_config_get_int` but for (unsigned) timestamps.
+ */
+int git_config_get_timestamp(const char *key, timestamp_t *dest);
+
 /**
  * Finds and parses the value into a boolean value, for the configuration
  * variable `key` respecting keywords like "true" and "false". Integer
-- 
gitgitgadget
Previous: Derrick Stolee via GitGitGadgetNext: Ævar Arnfjörð Bjarmason
Message 23 of 46 in “[RFC] Bundle URIs”
  1. 00/25 [RFC] Bundle URIsDerrick Stolee via GitGitGadget, Feb 23, 2022
  2. 02/25 bundle: alphabetize subcommands betterDerrick Stolee via GitGitGadget, Feb 23, 2022
  3. Ævar Arnfjörð BjarmasonMar 11, 2022
  4. 05/25 remote: allow relative_url() to return an absolute urlDerrick Stolee via GitGitGadget, Feb 23, 2022
  5. 03/25 dir: extract starts_with_dot[_dot]_slash()Derrick Stolee via GitGitGadget, Feb 23, 2022
  6. 04/25 remote: move relative_url()Derrick Stolee via GitGitGadget, Feb 23, 2022
  7. 01/25 docs: document bundle URI standardDerrick Stolee via GitGitGadget, Feb 23, 2022
  8. Elijah NewrenMar 2, 2022
  9. Derrick StoleeMar 2, 2022
  10. Elijah NewrenMar 3, 2022
  11. Derrick StoleeMar 3, 2022
  12. 06/25 http: make http_get_file() externalDerrick Stolee via GitGitGadget, Feb 23, 2022
  13. 07/25 remote-curl: add 'get' capabilityDerrick Stolee via GitGitGadget, Feb 23, 2022
  14. 08/25 bundle: implement 'fetch' command for direct bundlesDerrick Stolee via GitGitGadget, Feb 23, 2022
  15. 09/25 bundle: parse table of contents during 'fetch'Derrick Stolee via GitGitGadget, Feb 23, 2022
  16. 11/25 bundle: allow relative URLs in table of contentsDerrick Stolee via GitGitGadget, Feb 23, 2022
  17. Ævar Arnfjörð BjarmasonMar 11, 2022
  18. 10/25 bundle: add --filter option to 'fetch'Derrick Stolee via GitGitGadget, Feb 23, 2022
  19. Ævar Arnfjörð BjarmasonMar 11, 2022
  20. 12/25 bundle: make it easy to call 'git bundle fetch'Derrick Stolee via GitGitGadget, Feb 23, 2022
  21. 13/25 clone: add --bundle-uri optionDerrick Stolee via GitGitGadget, Feb 23, 2022
  22. 14/25 clone: --bundle-uri cannot be combined with --depthDerrick Stolee via GitGitGadget, Feb 23, 2022
  23. 15/25 config: add git_config_get_timestamp()Derrick Stolee via GitGitGadget, Feb 23, 2022
  24. Ævar Arnfjörð BjarmasonMar 11, 2022
  25. 18/25 connect.c: refactor sending of agent & object-formatÆvar Arnfjörð Bjarmason via GitGitGadget, Feb 23, 2022
  26. 17/25 fetch: fetch bundles before fetching original dataDerrick Stolee via GitGitGadget, Feb 23, 2022
  27. 16/25 bundle: only fetch bundles if timestamp is newDerrick Stolee via GitGitGadget, Feb 23, 2022
  28. 19/25 protocol-caps: implement cap_features()Derrick Stolee via GitGitGadget, Feb 23, 2022
  29. 21/25 serve: advertise 'features' when config existsDerrick Stolee via GitGitGadget, Feb 23, 2022
  30. 20/25 serve: understand but do not advertise 'features' capabilityDerrick Stolee via GitGitGadget, Feb 23, 2022
  31. 22/25 connect: implement get_recommended_features()Derrick Stolee via GitGitGadget, Feb 23, 2022
  32. 23/25 transport: add connections for 'features' capabilityDerrick Stolee via GitGitGadget, Feb 23, 2022
  33. 24/25 clone: use server-recommended bundle URIDerrick Stolee via GitGitGadget, Feb 23, 2022
  34. 25/25 t5601: basic bundle URI testDerrick Stolee via GitGitGadget, Feb 23, 2022
  35. Ævar Arnfjörð BjarmasonFeb 23, 2022
  36. Derrick StoleeFeb 24, 2022
  37. Derrick StoleeMar 4, 2022
  38. Ævar Arnfjörð BjarmasonMar 4, 2022
  39. Derrick StoleeMar 4, 2022
  40. Derrick StoleeMar 8, 2022
  41. Johannes SchindelinMar 10, 2022
  42. Derrick StoleeApr 7, 2022
  43. Ævar Arnfjörð BjarmasonApr 8, 2022
  44. Derrick StoleeApr 8, 2022
  45. Junio C HamanoApr 8, 2022
  46. Teng LongMar 8, 2022

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.