{"thread":{"id":"1892","subject":"Teach \"git-rev-parse\" about date-based cut-offs","startedAt":"2005-09-20T21:13:24Z","lastAt":"2005-09-20T21:13:24Z","messageCount":1,"participants":["Linus Torvalds"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"9015","messageId":"Pine.LNX.4.58.0509201406190.2553@g5.osdl.org","threadId":"1892","inReplyTo":null,"subject":"Teach \"git-rev-parse\" about date-based cut-offs","fromName":"Linus Torvalds","fromEmail":"torvalds@osdl.org","sentAt":"2005-09-20T21:13:24Z","receivedAt":"2005-09-20T21:13:24Z","isPatch":false,"sender":{"key":"torvalds@linux-foundation.org","avatar":"https://avatars.githubusercontent.com/u/1024025?v=4"},"body":"\nThis adds the options \"--since=date\" and \"--before=date\" to git-rev-parse, \nwhich knows how to translate them into seconds since the epoch for \ngit-rev-list.\n\nWith this, you can do\n\n\tgit log --since=\"2 weeks ago\"\n\nor\n\n\tgit log --until=yesterday\n\nto show the commits that have happened in the last two weeks or are \nolder than 24 hours, respectively.\n\nThe flags \"--after=\" and \"--before\" are synonyms for --since and --until, \nand you can combine them, so\n\n\tgit log --after=\"Aug 5\" --before=\"Aug 10\"\n\nis a valid (but strange) thing to do. \n\nSigned-off-by: Linus Torvalds <torvalds@osdl.org>\n---\ndiff --git a/rev-parse.c b/rev-parse.c\n--- a/rev-parse.c\n+++ b/rev-parse.c\n@@ -6,6 +6,7 @@\n #include \"cache.h\"\n #include \"commit.h\"\n #include \"refs.h\"\n+#include \"quote.h\"\n \n #define DO_REVS\t\t1\n #define DO_NOREV\t2\n@@ -125,6 +126,30 @@ static int show_reference(const char *re\n \treturn 0;\n }\n \n+static void show_datestring(const char *flag, const char *datestr)\n+{\n+\tFILE *date;\n+\tstatic char buffer[100];\n+\tstatic char cmd[1000];\n+\tint len;\n+\n+\t/* date handling requires both flags and revs */\n+\tif ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))\n+\t\treturn;\n+\tlen = strlen(flag);\n+\tmemcpy(buffer, flag, len);\n+\n+\tsnprintf(cmd, sizeof(cmd), \"date --date=%s +%%s\", sq_quote(datestr));\n+\tdate = popen(cmd, \"r\");\n+\tif (!date || !fgets(buffer + len, sizeof(buffer) - len, date))\n+\t\tdie(\"git-rev-list: bad date string\");\n+\tpclose(date);\n+\tlen = strlen(buffer);\n+\tif (buffer[len-1] == '\\n')\n+\t\tbuffer[--len] = 0;\n+\tshow(buffer);\n+}\n+\n int main(int argc, char **argv)\n {\n \tint i, as_is = 0, verify = 0;\n@@ -207,6 +232,22 @@ int main(int argc, char **argv)\n \t\t\t\tprintf(\"%s/.git\\n\", cwd);\n \t\t\t\tcontinue;\n \t\t\t}\n+\t\t\tif (!strncmp(arg, \"--since=\", 8)) {\n+\t\t\t\tshow_datestring(\"--max-age=\", arg+8);\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tif (!strncmp(arg, \"--after=\", 8)) {\n+\t\t\t\tshow_datestring(\"--max-age=\", arg+8);\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tif (!strncmp(arg, \"--before=\", 9)) {\n+\t\t\t\tshow_datestring(\"--min-age=\", arg+9);\n+\t\t\t\tcontinue;\n+\t\t\t}\n+\t\t\tif (!strncmp(arg, \"--until=\", 8)) {\n+\t\t\t\tshow_datestring(\"--min-age=\", arg+8);\n+\t\t\t\tcontinue;\n+\t\t\t}\n \t\t\tif (verify)\n \t\t\t\tdie(\"Needed a single revision\");\n \t\t\tshow_flag(arg);\n"}]}