@matheusbsbs / matheus-bagliano / commits / 8ca12c0d62
Fork 0

add is_dot_or_dotdot inline function

A new inline function is_dot_or_dotdot is used to check if the directory name is either "." or "..". It returns a non-zero value if the given string is "." or "..". It's applicable to a lot of Git source code. Signed-off-by: Alexander Potashev <aspotashev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Alexander Potashev committed Jan 10, 2009 at 15:07 UTC 8ca12c0d62c0be4a4987c4a936467ea2a92e915a
9 files changed +31 -47
builtin-count-objects.c
+2 -3
@@ -5,6 +5,7 @@
5 */
6
7 #include "cache.h"
8 +#include "dir.h"
9 #include "builtin.h"
10 #include "parse-options.h"
11
@@ -21,9 +22,7 @@ static void count_objects(DIR *d, char *path, int len, int verbose,
22 const char *cp;
23 int bad = 0;
24
24 - if ((ent->d_name[0] == '.') &&
25 - (ent->d_name[1] == 0 ||
26 - ((ent->d_name[1] == '.') && (ent->d_name[2] == 0))))
25 + if (is_dot_or_dotdot(ent->d_name))
26 continue;
27 for (cp = ent->d_name; *cp; cp++) {
28 int ch = *cp;
builtin-fsck.c
+4 -10
@@ -10,6 +10,7 @@
10 #include "tree-walk.h"
11 #include "fsck.h"
12 #include "parse-options.h"
13 +#include "dir.h"
14
15 #define REACHABLE 0x0001
16 #define SEEN 0x0002
@@ -395,19 +396,12 @@ static void fsck_dir(int i, char *path)
396 while ((de = readdir(dir)) != NULL) {
397 char name[100];
398 unsigned char sha1[20];
398 - int len = strlen(de->d_name);
399
400 - switch (len) {
401 - case 2:
402 - if (de->d_name[1] != '.')
403 - break;
404 - case 1:
405 - if (de->d_name[0] != '.')
406 - break;
400 + if (is_dot_or_dotdot(de->d_name))
401 continue;
408 - case 38:
402 + if (strlen(de->d_name) == 38) {
403 sprintf(name, "%02x", i);
410 - memcpy(name+2, de->d_name, len+1);
404 + memcpy(name+2, de->d_name, 39);
405 if (get_sha1_hex(name, sha1) < 0)
406 break;
407 add_sha1_list(sha1, DIRENT_SORT_HINT(de));
builtin-prune.c
+4 -10
@@ -5,6 +5,7 @@
5 #include "builtin.h"
6 #include "reachable.h"
7 #include "parse-options.h"
8 +#include "dir.h"
9
10 static const char * const prune_usage[] = {
11 "git prune [-n] [-v] [--expire <time>] [--] [<head>...]",
@@ -61,19 +62,12 @@ static int prune_dir(int i, char *path)
62 while ((de = readdir(dir)) != NULL) {
63 char name[100];
64 unsigned char sha1[20];
64 - int len = strlen(de->d_name);
65
66 - switch (len) {
67 - case 2:
68 - if (de->d_name[1] != '.')
69 - break;
70 - case 1:
71 - if (de->d_name[0] != '.')
72 - break;
66 + if (is_dot_or_dotdot(de->d_name))
67 continue;
74 - case 38:
68 + if (strlen(de->d_name) == 38) {
69 sprintf(name, "%02x", i);
76 - memcpy(name+2, de->d_name, len+1);
70 + memcpy(name+2, de->d_name, 39);
71 if (get_sha1_hex(name, sha1) < 0)
72 break;
73
builtin-rerere.c
+5 -6
@@ -1,5 +1,6 @@
1 #include "builtin.h"
2 #include "cache.h"
3 +#include "dir.h"
4 #include "string-list.h"
5 #include "rerere.h"
6 #include "xdiff/xdiff.h"
@@ -59,17 +60,15 @@ static void garbage_collect(struct string_list *rr)
60 git_config(git_rerere_gc_config, NULL);
61 dir = opendir(git_path("rr-cache"));
62 while ((e = readdir(dir))) {
62 - const char *name = e->d_name;
63 - if (name[0] == '.' &&
64 - (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')))
63 + if (is_dot_or_dotdot(e->d_name))
64 continue;
66 - then = rerere_created_at(name);
65 + then = rerere_created_at(e->d_name);
66 if (!then)
67 continue;
69 - cutoff = (has_resolution(name)
68 + cutoff = (has_resolution(e->d_name)
69 ? cutoff_resolve : cutoff_noresolve);
70 if (then < now - cutoff * 86400)
72 - string_list_append(name, &to_remove);
71 + string_list_append(e->d_name, &to_remove);
72 }
73 for (i = 0; i < to_remove.nr; i++)
74 unlink_rr_item(to_remove.items[i].string);
dir.c
+4 -8
@@ -585,10 +585,8 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
585 int len, dtype;
586 int exclude;
587
588 - if ((de->d_name[0] == '.') &&
589 - (de->d_name[1] == 0 ||
590 - !strcmp(de->d_name + 1, ".") ||
591 - !strcmp(de->d_name + 1, "git")))
588 + if (is_dot_or_dotdot(de->d_name) ||
589 + !strcmp(de->d_name, ".git"))
590 continue;
591 len = strlen(de->d_name);
592 /* Ignore overly long pathnames! */
@@ -793,10 +791,8 @@ int remove_dir_recursively(struct strbuf *path, int only_empty)
791 len = path->len;
792 while ((e = readdir(dir)) != NULL) {
793 struct stat st;
796 - if ((e->d_name[0] == '.') &&
797 - ((e->d_name[1] == 0) ||
798 - ((e->d_name[1] == '.') && e->d_name[2] == 0)))
799 - continue; /* "." and ".." */
794 + if (is_dot_or_dotdot(e->d_name))
795 + continue;
796
797 strbuf_setlen(path, len);
798 strbuf_addstr(path, e->d_name);
dir.h
+7
@@ -77,6 +77,13 @@ extern int file_exists(const char *);
77 extern char *get_relative_cwd(char *buffer, int size, const char *dir);
78 extern int is_inside_dir(const char *dir);
79
80 +static inline int is_dot_or_dotdot(const char *name)
81 +{
82 + return (name[0] == '.' &&
83 + (name[1] == '\0' ||
84 + (name[1] == '.' && name[2] == '\0')));
85 +}
86 +
87 extern void setup_standard_excludes(struct dir_struct *dir);
88 extern int remove_dir_recursively(struct strbuf *path, int only_empty);
89
entry.c
+2 -3
@@ -1,5 +1,6 @@
1 #include "cache.h"
2 #include "blob.h"
3 +#include "dir.h"
4
5 static void create_directories(const char *path, const struct checkout *state)
6 {
@@ -62,9 +63,7 @@ static void remove_subtree(const char *path)
63 *name++ = '/';
64 while ((de = readdir(dir)) != NULL) {
65 struct stat st;
65 - if ((de->d_name[0] == '.') &&
66 - ((de->d_name[1] == 0) ||
67 - ((de->d_name[1] == '.') && de->d_name[2] == 0)))
66 + if (is_dot_or_dotdot(de->d_name))
67 continue;
68 strcpy(name, de->d_name);
69 if (lstat(pathbuf, &st))
remote.c
+2 -4
@@ -4,6 +4,7 @@
4 #include "commit.h"
5 #include "diff.h"
6 #include "revision.h"
7 +#include "dir.h"
8
9 static struct refspec s_tag_refspec = {
10 0,
@@ -634,10 +635,7 @@ static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
635
636 static int valid_remote_nick(const char *name)
637 {
637 - if (!name[0] || /* not empty */
638 - (name[0] == '.' && /* not "." */
639 - (!name[1] || /* not ".." */
640 - (name[1] == '.' && !name[2]))))
638 + if (!name[0] || is_dot_or_dotdot(name))
639 return 0;
640 return !strchr(name, '/'); /* no slash */
641 }
transport.c
+1 -3
@@ -50,9 +50,7 @@ static int read_loose_refs(struct strbuf *path, int name_offset,
50 memset (&list, 0, sizeof(list));
51
52 while ((de = readdir(dir))) {
53 - if (de->d_name[0] == '.' && (de->d_name[1] == '\0' ||
54 - (de->d_name[1] == '.' &&
55 - de->d_name[2] == '\0')))
53 + if (is_dot_or_dotdot(de->d_name))
54 continue;
55 ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
56 list.entries[list.nr++] = xstrdup(de->d_name);