dir: create function count_slashes()
Similar functions exist in apply.c and builtin/show-branch.c for counting the number of slashes in a string. Also in the later patches, we introduce a third caller for the same. Hence, we unify it now by cleaning the existing functions and declaring a common function count_slashes in dir.h and implementing it in dir.c to remove this code duplication. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Prathamesh Chavan <pc44800@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
b06d364310
commit
e0556a928f
11
apply.c
11
apply.c
@ -762,17 +762,6 @@ static char *find_name_traditional(struct apply_state *state,
|
|||||||
return find_name_common(state, line, def, p_value, line + len, 0);
|
return find_name_common(state, line, def, p_value, line + len, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int count_slashes(const char *cp)
|
|
||||||
{
|
|
||||||
int cnt = 0;
|
|
||||||
char ch;
|
|
||||||
|
|
||||||
while ((ch = *cp++))
|
|
||||||
if (ch == '/')
|
|
||||||
cnt++;
|
|
||||||
return cnt;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Given the string after "--- " or "+++ ", guess the appropriate
|
* Given the string after "--- " or "+++ ", guess the appropriate
|
||||||
* p_value for the given patch.
|
* p_value for the given patch.
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include "color.h"
|
#include "color.h"
|
||||||
#include "argv-array.h"
|
#include "argv-array.h"
|
||||||
#include "parse-options.h"
|
#include "parse-options.h"
|
||||||
|
#include "dir.h"
|
||||||
|
|
||||||
static const char* show_branch_usage[] = {
|
static const char* show_branch_usage[] = {
|
||||||
N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n"
|
N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n"
|
||||||
@ -421,14 +422,6 @@ static int append_tag_ref(const char *refname, const struct object_id *oid,
|
|||||||
|
|
||||||
static const char *match_ref_pattern = NULL;
|
static const char *match_ref_pattern = NULL;
|
||||||
static int match_ref_slash = 0;
|
static int match_ref_slash = 0;
|
||||||
static int count_slash(const char *s)
|
|
||||||
{
|
|
||||||
int cnt = 0;
|
|
||||||
while (*s)
|
|
||||||
if (*s++ == '/')
|
|
||||||
cnt++;
|
|
||||||
return cnt;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int append_matching_ref(const char *refname, const struct object_id *oid,
|
static int append_matching_ref(const char *refname, const struct object_id *oid,
|
||||||
int flag, void *cb_data)
|
int flag, void *cb_data)
|
||||||
@ -438,7 +431,7 @@ static int append_matching_ref(const char *refname, const struct object_id *oid,
|
|||||||
* refs/tags/v0.99.9a and friends.
|
* refs/tags/v0.99.9a and friends.
|
||||||
*/
|
*/
|
||||||
const char *tail;
|
const char *tail;
|
||||||
int slash = count_slash(refname);
|
int slash = count_slashes(refname);
|
||||||
for (tail = refname; *tail && match_ref_slash < slash; )
|
for (tail = refname; *tail && match_ref_slash < slash; )
|
||||||
if (*tail++ == '/')
|
if (*tail++ == '/')
|
||||||
slash--;
|
slash--;
|
||||||
@ -529,7 +522,7 @@ static void append_one_rev(const char *av)
|
|||||||
int saved_matches = ref_name_cnt;
|
int saved_matches = ref_name_cnt;
|
||||||
|
|
||||||
match_ref_pattern = av;
|
match_ref_pattern = av;
|
||||||
match_ref_slash = count_slash(av);
|
match_ref_slash = count_slashes(av);
|
||||||
for_each_ref(append_matching_ref, NULL);
|
for_each_ref(append_matching_ref, NULL);
|
||||||
if (saved_matches == ref_name_cnt &&
|
if (saved_matches == ref_name_cnt &&
|
||||||
ref_name_cnt < MAX_REVS)
|
ref_name_cnt < MAX_REVS)
|
||||||
|
9
dir.c
9
dir.c
@ -49,6 +49,15 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
|
|||||||
int check_only, const struct pathspec *pathspec);
|
int check_only, const struct pathspec *pathspec);
|
||||||
static int get_dtype(struct dirent *de, const char *path, int len);
|
static int get_dtype(struct dirent *de, const char *path, int len);
|
||||||
|
|
||||||
|
int count_slashes(const char *s)
|
||||||
|
{
|
||||||
|
int cnt = 0;
|
||||||
|
while (*s)
|
||||||
|
if (*s++ == '/')
|
||||||
|
cnt++;
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
|
|
||||||
int fspathcmp(const char *a, const char *b)
|
int fspathcmp(const char *a, const char *b)
|
||||||
{
|
{
|
||||||
return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
|
return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
|
||||||
|
3
dir.h
3
dir.h
@ -196,6 +196,9 @@ struct dir_struct {
|
|||||||
unsigned unmanaged_exclude_files;
|
unsigned unmanaged_exclude_files;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*Count the number of slashes for string s*/
|
||||||
|
extern int count_slashes(const char *s);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The ordering of these constants is significant, with
|
* The ordering of these constants is significant, with
|
||||||
* higher-numbered match types signifying "closer" (i.e. more
|
* higher-numbered match types signifying "closer" (i.e. more
|
||||||
|
Loading…
Reference in New Issue
Block a user