Skip to contentSkip to conversation

date: remove approxidate_relative()

PATCH In review René Scharfe 3 participants 9 messages Apr 8, 2023 – Apr 12, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 date: remove approxidate_relative()

When 29f4332e66 (Quit passing 'now' to date code, 2019-09-11) removed its timeval parameter, approxidate_relative() became equivalent to approxidate(). Convert its last two call sites and remove the redundant function.

---
Formatted with -U16 for easier review. Only useful in date.c, but cannot be restricted to just one file.

 date.c               | 14 --------------
 date.h               |  1 -
 t/helper/test-date.c |  4 ++--
 3 files changed, 2 insertions(+), 17 deletions(-)
date.c +0 -14
index 1fb2cd1b53..7c8650f799 100644
@@ -1353,46 +1353,32 @@ static timestamp_t approxidate_str(const char *date,
 		date++;
 		if (isdigit(c)) {
 			pending_number(&tm, &number);
 			date = approxidate_digit(date-1, &tm, &number, time_sec);
 			touched = 1;
 			continue;
 		}
 		if (isalpha(c))
 			date = approxidate_alpha(date-1, &tm, &now, &number, &touched);
 	}
 	pending_number(&tm, &number);
 	if (!touched)
 		*error_ret = 1;
 	return (timestamp_t)update_tm(&tm, &now, 0);
 }
 
-timestamp_t approxidate_relative(const char *date)
-{
-	struct timeval tv;
-	timestamp_t timestamp;
-	int offset;
-	int errors = 0;
-
-	if (!parse_date_basic(date, &timestamp, &offset))
-		return timestamp;
-
-	get_time(&tv);
-	return approxidate_str(date, (const struct timeval *) &tv, &errors);
-}
-
 timestamp_t approxidate_careful(const char *date, int *error_ret)
 {
 	struct timeval tv;
 	timestamp_t timestamp;
 	int offset;
 	int dummy = 0;
 	if (!error_ret)
 		error_ret = &dummy;
 
 	if (!parse_date_basic(date, &timestamp, &offset)) {
 		*error_ret = 0;
 		return timestamp;
 	}
 
 	get_time(&tv);
 	return approxidate_str(date, &tv, error_ret);
date.h +0 -1
index 5d4eaba0a9..6136212a19 100644
@@ -55,20 +55,19 @@ const char *show_date(timestamp_t time, int timezone, const struct date_mode *mo
  */
 void parse_date_format(const char *format, struct date_mode *mode);
 
 /**
  * Release a "struct date_mode", currently only required if
  * parse_date_format() has parsed a "DATE_STRFTIME" format.
  */
 void date_mode_release(struct date_mode *mode);
 
 void show_date_relative(timestamp_t time, struct strbuf *timebuf);
 int parse_date(const char *date, struct strbuf *out);
 int parse_date_basic(const char *date, timestamp_t *timestamp, int *offset);
 int parse_expiry_date(const char *date, timestamp_t *timestamp);
 void datestamp(struct strbuf *out);
 #define approxidate(s) approxidate_careful((s), NULL)
 timestamp_t approxidate_careful(const char *, int *);
-timestamp_t approxidate_relative(const char *date);
 int date_overflows(timestamp_t date);
 time_t tm_to_time_t(const struct tm *tm);
 #endif
t/helper/test-date.c +2 -2
index cd6a6df702..2d9232cc68 100644
@@ -68,42 +68,42 @@ static void parse_dates(const char **argv)
 
 		strbuf_reset(&result);
 		parse_date(*argv, &result);
 		if (sscanf(result.buf, "%"PRItime" %d", &t, &tz) == 2)
 			printf("%s -> %s\n",
 			       *argv, show_date(t, tz, DATE_MODE(ISO8601)));
 		else
 			printf("%s -> bad\n", *argv);
 	}
 	strbuf_release(&result);
 }
 
 static void parse_approxidate(const char **argv)
 {
 	for (; *argv; argv++) {
 		timestamp_t t;
-		t = approxidate_relative(*argv);
+		t = approxidate(*argv);
 		printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
 	}
 }
 
 static void parse_approx_timestamp(const char **argv)
 {
 	for (; *argv; argv++) {
 		timestamp_t t;
-		t = approxidate_relative(*argv);
+		t = approxidate(*argv);
 		printf("%s -> %"PRItime"\n", *argv, t);
 	}
 }
 
 static void getnanos(const char **argv)
 {
 	double seconds = getnanotime() / 1.0e9;
 
 	if (*argv)
 		seconds -= strtod(*argv, NULL);
 	printf("%lf\n", seconds);
 }
 
 int cmd__date(int argc UNUSED, const char **argv)
 {
 	const char *x;

--
2.40.0

#1 lore raw
Quoted 7 lines · René Scharfe

When 29f4332e66 (Quit passing 'now' to date code, 2019-09-11) removed its timeval parameter, approxidate_relative() became equivalent to approxidate(). Convert its last two call sites and remove the redundant function.

---

We might keep a backward compatibility definition or comment to help migrating in-flight (or pre-flight) callers if this were a function more often used, but seeing that the only remaining two callers are in the test helper, I agree the simplicity of straight removal like this patch does is appropriate.

Thanks, will queue.

Quoted 2 lines

Formatted with -U16 for easier review. Only useful in date.c, but cannot be restricted to just one file.

Perhaps

	$ git -c format-patch.date.c.context=16 format-patch -1

would be a workable idea? I do not think it makes any sense to say "when taking a diff for this path, always use 16 lines of context" so it should not be stored in a file to be used every time, like

[format-patch "date.c"] context = 16

and it makes even less sense to say that all project participants should use this context, always, so making it an attribute would be even less appropriate.

But this was the easiest way to prototype ;-) If this turns out to be useful, the real version should probably be done by:

* designing a command line option format, e.g.

    --per-path-context=date.c:16

* instead of adding an extra "int ctxlen" parameter to the call path(s), add a ctxlen_map that maps path to ctxlen we read from the command line options to the diff_options structure.

* In builtin_diff() where xecfg.ctxlen is assigned to, consult o->ctxlen_map and use o->context as a fallback.

or something along that line, I guess.

 diff.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)
diff.c +16 -2
index 469e18aed2..cbfbb4af96 100644
@@ -3460,6 +3460,7 @@ static void builtin_diff(const char *name_a,
 			 const char *xfrm_msg,
 			 int must_show_header,
 			 struct diff_options *o,
+			 int ctxlen,
 			 int complete_rewrite)
 {
 	mmfile_t mf1, mf2;
@@ -3657,7 +3658,7 @@ static void builtin_diff(const char *name_a,
 		xpp.ignore_regex_nr = o->ignore_regex_nr;
 		xpp.anchors = o->anchors;
 		xpp.anchors_nr = o->anchors_nr;
-		xecfg.ctxlen = o->context;
+		xecfg.ctxlen = (ctxlen < 0) ? o->context : ctxlen;
 		xecfg.interhunkctxlen = o->interhunkcontext;
 		xecfg.flags = XDL_EMIT_FUNCNAMES;
 		if (o->flags.funccontext)
@@ -4443,6 +4444,17 @@ static void fill_metainfo(struct strbuf *msg,
 	}
 }
 
+static int configured_ctxlen(const char *path)
+{
+	struct strbuf key = STRBUF_INIT;
+	int ctxlen;
+
+	strbuf_addf(&key, "format-patch.%s.context", path);
+	if (!repo_config_get_int(the_repository, key.buf, &ctxlen))
+		return ctxlen;
+	return -1;
+}
+
 static void run_diff_cmd(const char *pgm,
 			 const char *name,
 			 const char *other,
@@ -4480,12 +4492,14 @@ static void run_diff_cmd(const char *pgm,
 		return;
 	}
 	if (one && two) {
+		int ctxlen = configured_ctxlen(attr_path);
+
 		if (!o->ignore_driver_algorithm && drv && drv->algorithm)
 			set_diff_algorithm(o, drv->algorithm);
 
 		builtin_diff(name, other ? other : name,
 			     one, two, xfrm_msg, must_show_header,
-			     o, complete_rewrite);
+			     o, ctxlen, complete_rewrite);
 	} else {
 		fprintf(o->file, "* Unmerged path %s\n", name);
 	}
#2 lore raw
Quoted 8 lines · Junio C Hamano

Perhaps

	$ git -c format-patch.date.c.context=16 format-patch -1

would be a workable idea? I do not think it makes any sense to say "when taking a diff for this path, always use 16 lines of context" so it should not be stored in a file to be used every time, like

Quoted 8 lines

[format-patch "date.c"] context = 16

and it makes even less sense to say that all project participants should use this context, always, so making it an attribute would be even less appropriate.

But this was the easiest way to prototype ;-) If this turns out to be useful, the real version should probably be done by:

Cute. It feels like this only goes half-way, though. You really want per-hunk configurable context. This particular patch was just lucky that there was only one hunk in the date.c file.

I'm not sure of the least-confusing way to address a single hunk, though (by line number is one option, by hunk-number within the patch is another). I suspect the best workflow for a user would be to interactively say "show me more context for this hunk". Some viewers have support for that (e.g., GitHub's web view of a diff). But handling that for a one-shot CLI program is tricky, not to mention then feeding it back to format-patch to generate the output you want to send. :)

-Peff

#3 lore raw

Am 10.04.23 um 22:25 schrieb Jeff King:

Quoted 8 lines

I'm not sure of the least-confusing way to address a single hunk, though (by line number is one option, by hunk-number within the patch is another). I suspect the best workflow for a user would be to interactively say "show me more context for this hunk". Some viewers have support for that (e.g., GitHub's web view of a diff). But handling that for a one-shot CLI program is tricky, not to mention then feeding it back to format-patch to generate the output you want to send. :)

So basically you propose a format-patch --interactive mode that shows each hunk and allows extending its context. This could work. For hunks that span multiple screens it might be a bit iffy -- or perhaps not, if the scrollback buffer of the terminal or console is big enough.

René

#4 lore raw
Quoted 10 lines · René Scharfe

Am 10.04.23 um 22:25 schrieb Jeff King:

Quoted 8 lines

I'm not sure of the least-confusing way to address a single hunk, though (by line number is one option, by hunk-number within the patch is another). I suspect the best workflow for a user would be to interactively say "show me more context for this hunk". Some viewers have support for that (e.g., GitHub's web view of a diff). But handling that for a one-shot CLI program is tricky, not to mention then feeding it back to format-patch to generate the output you want to send. :)

Quoted 4 lines

So basically you propose a format-patch --interactive mode that shows each hunk and allows extending its context. This could work. For hunks that span multiple screens it might be a bit iffy -- or perhaps not, if the scrollback buffer of the terminal or console is big enough.

Well, less proposing and more thinking out loud. :)

One thing that I think would make it awkward is that some people (well, me at least) tend to show diffs repeatedly. So I might look at a diff several times, and then even run format-patch multiple times as part of my workflow of sending it. Interactively expanding the diff each time, and then losing that result, would be annoying.

So an interactive tool that somehow output "oh, and here are some parameters you can feed back to get the same view" would be my ideal.

-Peff

#5 lore raw
In reply to Jeff King
Quoted 3 lines · Jeff King

Cute. It feels like this only goes half-way, though. You really want per-hunk configurable context. This particular patch was just lucky that there was only one hunk in the date.c file.

"-U16" extends the context lines in both directions by the same number of lines, but most likely you need to extend asymmetrically. In René's patch, for example, much of the body of approxidate_str() is visible in the precontext of the hunk to remove approxidate_relative(), but the body of that function is irrelevant. What he wanted to show was the body of approxidate_careful() and the size of that function is where the -U16 came from.

Instead, imagine --extra-context='<range>:<path>' were the way to tell Git to include the specified range of lines in the post context even though they may not have been modified. Then René's patch could have been produced with

    $ git format-patch -1 \
      --extra-context='/^timestamp_t approxidate_careful/,/^}$/:date.c'

and would have shown 3 lines of precontext before the removed approxidate_relative(), plus the unchanged approxidate_careful() function in full in the postcontext.

#6 lore raw
Quoted 8 lines · Junio C Hamano
Quoted 3 lines · Jeff King

Cute. It feels like this only goes half-way, though. You really want per-hunk configurable context. This particular patch was just lucky that there was only one hunk in the date.c file.

"-U16" extends the context lines in both directions by the same number of lines, but most likely you need to extend asymmetrically.

Good point. This is sort of like grep's "-C" along with "-A" and "-B". All of which I generally find somewhat awkward. :)

Quoted 11 lines

In René's patch, for example, much of the body of approxidate_str() is visible in the precontext of the hunk to remove approxidate_relative(), but the body of that function is irrelevant. What he wanted to show was the body of approxidate_careful() and the size of that function is where the -U16 came from.

Instead, imagine --extra-context='<range>:<path>' were the way to tell Git to include the specified range of lines in the post context even though they may not have been modified. Then René's patch could have been produced with

Quoted 6 lines
    $ git format-patch -1 \
      --extra-context='/^timestamp_t approxidate_careful/,/^}$/:date.c'

and would have shown 3 lines of precontext before the removed approxidate_relative(), plus the unchanged approxidate_careful() function in full in the postcontext.

Ooh, I like that very much. In that sense it really feels like an extension of --function-context. Would the regexes be searches starting from the edge of some context (as they more or less are under the hood for function context), or would you search within the whole file for ranges (and then presumably use them when a hunk's context is adjacent to or overlaps a range)?

If the latter, I guess you could also allow both absolute and relative line numbers, similar to how "-L" accepts range input.

-Peff

#7 lore raw
Quoted 19 lines · Jeff King
Quoted 11 lines

Instead, imagine --extra-context='<range>:<path>' were the way to tell Git to include the specified range of lines in the post context even though they may not have been modified. Then René's patch could have been produced with

    $ git format-patch -1 \
      --extra-context='/^timestamp_t approxidate_careful/,/^}$/:date.c'

and would have shown 3 lines of precontext before the removed approxidate_relative(), plus the unchanged approxidate_careful() function in full in the postcontext.

Ooh, I like that very much. In that sense it really feels like an extension of --function-context. Would the regexes be searches starting from the edge of some context (as they more or less are under the hood for function context), or would you search within the whole file for ranges (and then presumably use them when a hunk's context is adjacent to or overlaps a range)?

Quoted 2 lines

If the latter, I guess you could also allow both absolute and relative line numbers, similar to how "-L" accepts range input.

We want the latter.

If we further imagine that approxidate_careful() were defined very far away (in either direction) from approxidate_relative() that "extending" the patch context to show the removal of the latter to cover the former would show too much irrelevant information, I think René would have wanted to show a normal patch plus an extra hunk that contains the entirety of approxidate_careful() that shows no modification (i.e. all lines are prefixed with an SP). The way I think about this new "feature" is "compute what hunks should be shown, honoring all other options. Then pretend no-op hunks to cover all specified lines in the postimage [*] are also in the result. Combine them all, ignoring parts of the made-up no-op hunks when they contradict the real hunks.". The end result should show all specified lines from the postimage plus the usual diff.

[Footnote]

* There is no need for a similar option to talk about lines in the preimage, because a line in the preimage would either appear in the postimage (in which case the range in the postimage can be used to show it), or otherwise it would appear as deleted line (in which case the reader will see it without the new feature.

#8 lore raw
Quoted 12 lines · Junio C Hamano
Quoted 9 lines

Ooh, I like that very much. In that sense it really feels like an extension of --function-context. Would the regexes be searches starting from the edge of some context (as they more or less are under the hood for function context), or would you search within the whole file for ranges (and then presumably use them when a hunk's context is adjacent to or overlaps a range)?

If the latter, I guess you could also allow both absolute and relative line numbers, similar to how "-L" accepts range input.

We want the latter.

Quoted 13 lines

If we further imagine that approxidate_careful() were defined very far away (in either direction) from approxidate_relative() that "extending" the patch context to show the removal of the latter to cover the former would show too much irrelevant information, I think René would have wanted to show a normal patch plus an extra hunk that contains the entirety of approxidate_careful() that shows no modification (i.e. all lines are prefixed with an SP). The way I think about this new "feature" is "compute what hunks should be shown, honoring all other options. Then pretend no-op hunks to cover all specified lines in the postimage [*] are also in the result. Combine them all, ignoring parts of the made-up no-op hunks when they contradict the real hunks.". The end result should show all specified lines from the postimage plus the usual diff.

Right, that makes perfect sense. Thanks for explaining.

Now we just need somebody to implement it. ;)

-Peff