{"thread":{"id":"60","subject":"[PATCH] Optionally tell show-diff to show only named files","startedAt":"2005-04-16T23:15:00Z","lastAt":"2005-04-16T23:15:00Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"381","messageId":"7vy8bi48ln.fsf@assigned-by-dhcp.cox.net","threadId":"60","inReplyTo":null,"subject":"[PATCH] Optionally tell show-diff to show only named files","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-04-16T23:15:00Z","receivedAt":"2005-04-16T23:15:00Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"SCMs have ways to say \"I want diff only this particular file\",\nor \"I want diff files under this directory\".  This patch teaches\nshow-diff to do something similar.  Without command line\narguments, it still examines everything in the dircache as\nbefore.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n---\n\n show-diff.c |   38 ++++++++++++++++++++++++++++++--------\n 1 files changed, 30 insertions(+), 8 deletions(-)\n\nshow-diff.c:  5f3d4699566843a5448260e5da286ed65d90e397\n--- show-diff.c\n+++ show-diff.c\t2005-04-16 16:07:07.000000000 -0700\n@@ -55,6 +55,23 @@\n \t}\n }\n \n+static const char *show_diff_usage = \"show-diff [-s] [-q] [paths...]\";\n+\n+static int matches_pathspec(struct cache_entry *ce, char **spec, int cnt)\n+{\n+\tint i;\n+\tint namelen = ce_namelen(ce);\n+\tfor (i = 0; i < cnt; i++) {\n+\t\tint speclen = strlen(spec[i]);\n+\t\tif (! strncmp(spec[i], ce->name, speclen) &&\n+\t\t    speclen <= namelen &&\n+\t\t    (ce->name[speclen] == 0 ||\n+\t\t     ce->name[speclen] == '/'))\n+\t\t\treturn 1;\n+\t}\n+\treturn 0;\n+}\n+\n int main(int argc, char **argv)\n {\n \tint silent = 0;\n@@ -62,18 +79,19 @@\n \tint entries = read_cache();\n \tint i;\n \n-\tfor (i = 1; i < argc; i++) {\n-\t\tif (!strcmp(argv[i], \"-s\")) {\n+\twhile (1 < argc && argv[1][0] == '-') {\n+\t\tif (!strcmp(argv[1], \"-s\"))\n \t\t\tsilent_on_nonexisting_files = silent = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(argv[i], \"-q\")) {\n+\t\telse if (!strcmp(argv[1], \"-q\"))\n \t\t\tsilent_on_nonexisting_files = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tusage(\"show-diff [-s] [-q]\");\n+\t\telse\n+\t\t\tusage(show_diff_usage);\n+\t\targv++; argc--;\n \t}\n \n+\t/* At this point, if argc == 1, then we are doing everything.\n+\t * Otherwise argv[1] .. argv[argc-1] have the explicit paths.\n+\t */\n \tif (entries < 0) {\n \t\tperror(\"read_cache\");\n \t\texit(1);\n@@ -86,6 +104,10 @@\n \t\tchar type[20];\n \t\tvoid *new;\n \n+\t\tif (1 <argc &&\n+\t\t    ! matches_pathspec(ce, argv+1, argc-1))\n+\t\t\tcontinue;\n+\n \t\tif (stat(ce->name, &st) < 0) {\n \t\t\tif (errno == ENOENT && silent_on_nonexisting_files)\n \t\t\t\tcontinue;\n\n\n"}]}