{"thread":{"id":"46146","subject":"[PATCH v1] dir: create function count_slashes","startedAt":"2017-06-08T18:08:30Z","lastAt":"2017-06-12T05:33:12Z","messageCount":2,"participants":["Prathamesh Chavan","Stefan Beller"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"321760","messageId":"20170608180812.6678-1-pc44800@gmail.com","threadId":"46146","inReplyTo":null,"subject":"[PATCH v1] dir: create function count_slashes","fromName":"Prathamesh Chavan","fromEmail":"pc44800@gmail.com","sentAt":"2017-06-08T18:08:12Z","receivedAt":"2017-06-08T18:08:30Z","isPatch":true,"sender":{"key":"pc44800@gmail.com","avatar":"https://avatars.githubusercontent.com/u/17272661?v=4"},"body":"Similar functions exist in apply.c and builtin/show-branch.c for\ncounting the number of slashes in a string. Also in the later\npatches, we introduce a third caller for the same. Hence, we unify\nit now by cleaning the existing functions and declaring a common\nfunction count_slashes in dir.h and implementing it in dir.c to\nremove this code duplication.\n\nMentored-by: Christian Couder <christian.couder@gmail.com>\nMentored-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Prathamesh Chavan <pc44800@gmail.com>\n---\nIn future, I intend to use this function in builtin/submodule--helper.c\nas well, hence this change was introduced now.\n\nThe patch passes all the tests.\nComplete build report of this patch is available at:\nhttps://travis-ci.org/pratham-pc/git/builds\nBranch: count_slashes\nBuild #97\n\n apply.c               | 11 -----------\n builtin/show-branch.c | 13 +++----------\n dir.c                 |  9 +++++++++\n dir.h                 |  3 +++\n 4 files changed, 15 insertions(+), 21 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex c49cef063..121e53406 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -762,17 +762,6 @@ static char *find_name_traditional(struct apply_state *state,\n \treturn find_name_common(state, line, def, p_value, line + len, 0);\n }\n \n-static int count_slashes(const char *cp)\n-{\n-\tint cnt = 0;\n-\tchar ch;\n-\n-\twhile ((ch = *cp++))\n-\t\tif (ch == '/')\n-\t\t\tcnt++;\n-\treturn cnt;\n-}\n-\n /*\n  * Given the string after \"--- \" or \"+++ \", guess the appropriate\n  * p_value for the given patch.\ndiff --git a/builtin/show-branch.c b/builtin/show-branch.c\nindex 4a6cc6f49..3636a0559 100644\n--- a/builtin/show-branch.c\n+++ b/builtin/show-branch.c\n@@ -5,6 +5,7 @@\n #include \"color.h\"\n #include \"argv-array.h\"\n #include \"parse-options.h\"\n+#include \"dir.h\"\n \n static const char* show_branch_usage[] = {\n     N_(\"git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\\n\"\n@@ -421,14 +422,6 @@ static int append_tag_ref(const char *refname, const struct object_id *oid,\n \n static const char *match_ref_pattern = NULL;\n static int match_ref_slash = 0;\n-static int count_slash(const char *s)\n-{\n-\tint cnt = 0;\n-\twhile (*s)\n-\t\tif (*s++ == '/')\n-\t\t\tcnt++;\n-\treturn cnt;\n-}\n \n static int append_matching_ref(const char *refname, const struct object_id *oid,\n \t\t\t       int flag, void *cb_data)\n@@ -438,7 +431,7 @@ static int append_matching_ref(const char *refname, const struct object_id *oid,\n \t * refs/tags/v0.99.9a and friends.\n \t */\n \tconst char *tail;\n-\tint slash = count_slash(refname);\n+\tint slash = count_slashes(refname);\n \tfor (tail = refname; *tail && match_ref_slash < slash; )\n \t\tif (*tail++ == '/')\n \t\t\tslash--;\n@@ -529,7 +522,7 @@ static void append_one_rev(const char *av)\n \t\tint saved_matches = ref_name_cnt;\n \n \t\tmatch_ref_pattern = av;\n-\t\tmatch_ref_slash = count_slash(av);\n+\t\tmatch_ref_slash = count_slashes(av);\n \t\tfor_each_ref(append_matching_ref, NULL);\n \t\tif (saved_matches == ref_name_cnt &&\n \t\t    ref_name_cnt < MAX_REVS)\ndiff --git a/dir.c b/dir.c\nindex 9efcf1eab..4a953c16a 100644\n--- a/dir.c\n+++ b/dir.c\n@@ -52,6 +52,15 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,\n static int get_dtype(struct dirent *de, struct index_state *istate,\n \t\t     const char *path, int len);\n \n+int count_slashes(const char *s)\n+{\n+\tint cnt = 0;\n+\twhile (*s)\n+\t\tif (*s++ == '/')\n+\t\t\tcnt++;\n+\treturn cnt;\n+}\n+\n int fspathcmp(const char *a, const char *b)\n {\n \treturn ignore_case ? strcasecmp(a, b) : strcmp(a, b);\ndiff --git a/dir.h b/dir.h\nindex a89c13e27..e3717055d 100644\n--- a/dir.h\n+++ b/dir.h\n@@ -197,6 +197,9 @@ struct dir_struct {\n \tunsigned unmanaged_exclude_files;\n };\n \n+/*Count the number of slashes for string s*/\n+extern int count_slashes(const char *s);\n+\n /*\n  * The ordering of these constants is significant, with\n  * higher-numbered match types signifying \"closer\" (i.e. more\n-- \n2.13.0\n\n"},{"id":"321947","messageId":"CAGZ79kaKfdeyM41S2huGwYq8z3brRVpotA-CZzYe429-cA=mZg@mail.gmail.com","threadId":"46146","inReplyTo":"20170608180812.6678-1-pc44800@gmail.com","subject":"Re: [PATCH v1] dir: create function count_slashes","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2017-06-12T05:33:07Z","receivedAt":"2017-06-12T05:33:12Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Thu, Jun 8, 2017 at 11:08 AM, Prathamesh Chavan <pc44800@gmail.com> wrote:\n> Similar functions exist in apply.c and builtin/show-branch.c for\n> counting the number of slashes in a string. Also in the later\n> patches, we introduce a third caller for the same. Hence, we unify\n> it now by cleaning the existing functions and declaring a common\n> function count_slashes in dir.h and implementing it in dir.c to\n> remove this code duplication.\n>\n> Mentored-by: Christian Couder <christian.couder@gmail.com>\n> Mentored-by: Stefan Beller <sbeller@google.com>\n> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n> Signed-off-by: Prathamesh Chavan <pc44800@gmail.com>\n> ---\n> In future, I intend to use this function in builtin/submodule--helper.c\n> as well, hence this change was introduced now.\n>\n\nThanks for upstreaming this early!\n\nI think this is a good change even\nwithout the submodule work thrown into the soup as well,\nbut with the given promise to have it used a third time this\nis a no-brainer later on.\n"}]}