Skip to content

Commit 8ca12c0

Browse files
aspotashevgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c123b7c commit 8ca12c0

File tree

9 files changed

+31
-47
lines changed

9 files changed

+31
-47
lines changed

builtin-count-objects.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include "cache.h"
8+
#include "dir.h"
89
#include "builtin.h"
910
#include "parse-options.h"
1011

@@ -21,9 +22,7 @@ static void count_objects(DIR *d, char *path, int len, int verbose,
2122
const char *cp;
2223
int bad = 0;
2324

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))
2726
continue;
2827
for (cp = ent->d_name; *cp; cp++) {
2928
int ch = *cp;

builtin-fsck.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "tree-walk.h"
1111
#include "fsck.h"
1212
#include "parse-options.h"
13+
#include "dir.h"
1314

1415
#define REACHABLE 0x0001
1516
#define SEEN 0x0002
@@ -395,19 +396,12 @@ static void fsck_dir(int i, char *path)
395396
while ((de = readdir(dir)) != NULL) {
396397
char name[100];
397398
unsigned char sha1[20];
398-
int len = strlen(de->d_name);
399399

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))
407401
continue;
408-
case 38:
402+
if (strlen(de->d_name) == 38) {
409403
sprintf(name, "%02x", i);
410-
memcpy(name+2, de->d_name, len+1);
404+
memcpy(name+2, de->d_name, 39);
411405
if (get_sha1_hex(name, sha1) < 0)
412406
break;
413407
add_sha1_list(sha1, DIRENT_SORT_HINT(de));

builtin-prune.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "builtin.h"
66
#include "reachable.h"
77
#include "parse-options.h"
8+
#include "dir.h"
89

910
static const char * const prune_usage[] = {
1011
"git prune [-n] [-v] [--expire <time>] [--] [<head>...]",
@@ -61,19 +62,12 @@ static int prune_dir(int i, char *path)
6162
while ((de = readdir(dir)) != NULL) {
6263
char name[100];
6364
unsigned char sha1[20];
64-
int len = strlen(de->d_name);
6565

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))
7367
continue;
74-
case 38:
68+
if (strlen(de->d_name) == 38) {
7569
sprintf(name, "%02x", i);
76-
memcpy(name+2, de->d_name, len+1);
70+
memcpy(name+2, de->d_name, 39);
7771
if (get_sha1_hex(name, sha1) < 0)
7872
break;
7973

builtin-rerere.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "builtin.h"
22
#include "cache.h"
3+
#include "dir.h"
34
#include "string-list.h"
45
#include "rerere.h"
56
#include "xdiff/xdiff.h"
@@ -59,17 +60,15 @@ static void garbage_collect(struct string_list *rr)
5960
git_config(git_rerere_gc_config, NULL);
6061
dir = opendir(git_path("rr-cache"));
6162
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))
6564
continue;
66-
then = rerere_created_at(name);
65+
then = rerere_created_at(e->d_name);
6766
if (!then)
6867
continue;
69-
cutoff = (has_resolution(name)
68+
cutoff = (has_resolution(e->d_name)
7069
? cutoff_resolve : cutoff_noresolve);
7170
if (then < now - cutoff * 86400)
72-
string_list_append(name, &to_remove);
71+
string_list_append(e->d_name, &to_remove);
7372
}
7473
for (i = 0; i < to_remove.nr; i++)
7574
unlink_rr_item(to_remove.items[i].string);

dir.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -585,10 +585,8 @@ static int read_directory_recursive(struct dir_struct *dir, const char *path, co
585585
int len, dtype;
586586
int exclude;
587587

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"))
592590
continue;
593591
len = strlen(de->d_name);
594592
/* Ignore overly long pathnames! */
@@ -793,10 +791,8 @@ int remove_dir_recursively(struct strbuf *path, int only_empty)
793791
len = path->len;
794792
while ((e = readdir(dir)) != NULL) {
795793
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;
800796

801797
strbuf_setlen(path, len);
802798
strbuf_addstr(path, e->d_name);

dir.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ extern int file_exists(const char *);
7777
extern char *get_relative_cwd(char *buffer, int size, const char *dir);
7878
extern int is_inside_dir(const char *dir);
7979

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+
8087
extern void setup_standard_excludes(struct dir_struct *dir);
8188
extern int remove_dir_recursively(struct strbuf *path, int only_empty);
8289

entry.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "cache.h"
22
#include "blob.h"
3+
#include "dir.h"
34

45
static void create_directories(const char *path, const struct checkout *state)
56
{
@@ -62,9 +63,7 @@ static void remove_subtree(const char *path)
6263
*name++ = '/';
6364
while ((de = readdir(dir)) != NULL) {
6465
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))
6867
continue;
6968
strcpy(name, de->d_name);
7069
if (lstat(pathbuf, &st))

remote.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "commit.h"
55
#include "diff.h"
66
#include "revision.h"
7+
#include "dir.h"
78

89
static struct refspec s_tag_refspec = {
910
0,
@@ -634,10 +635,7 @@ static struct refspec *parse_push_refspec(int nr_refspec, const char **refspec)
634635

635636
static int valid_remote_nick(const char *name)
636637
{
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))
641639
return 0;
642640
return !strchr(name, '/'); /* no slash */
643641
}

transport.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,7 @@ static int read_loose_refs(struct strbuf *path, int name_offset,
5050
memset (&list, 0, sizeof(list));
5151

5252
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))
5654
continue;
5755
ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
5856
list.entries[list.nr++] = xstrdup(de->d_name);

0 commit comments

Comments
 (0)