Skip to content
This repository
Browse code

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

0 comments on commit 8ca12c0

Please sign in to comment.
Something went wrong with that request. Please try again.