refs: clean up common_list

Instead of common_list having formatting like ! and /, use a struct to hold common_list data in a structured form. We don't use 'exclude' yet; instead, we keep the old codepath that handles info/sparse-checkout and logs/HEAD. Later, we will use exclude. [jc: with "make common_list[] static" clean-up from Ramsay squashed in] Signed-off-by: David Turner <dturner@twopensource.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

David Turner committed Aug 31, 2015 at 22:13 UTC 0701530c2663f82ff4b678de27db4c61f8b3092f
1 file changed +37 -21
path.c
+37 -21
@@ -91,35 +91,51 @@ static void replace_dir(struct strbuf *buf, int len, const char *newdir)
91 buf->buf[newlen] = '/';
92 }
93
94 -static const char *common_list[] = {
95 - "/branches", "/hooks", "/info", "!/logs", "/lost-found",
96 - "/objects", "/refs", "/remotes", "/worktrees", "/rr-cache", "/svn",
97 - "config", "!gc.pid", "packed-refs", "shallow",
98 - NULL
94 +struct common_dir {
95 + /* Not considered garbage for report_linked_checkout_garbage */
96 + unsigned ignore_garbage:1;
97 + unsigned is_dir:1;
98 + /* Not common even though its parent is */
99 + unsigned exclude:1;
100 + const char *dirname;
101 +};
102 +
103 +static struct common_dir common_list[] = {
104 + { 0, 1, 0, "branches" },
105 + { 0, 1, 0, "hooks" },
106 + { 0, 1, 0, "info" },
107 + { 0, 0, 1, "info/sparse-checkout" },
108 + { 1, 1, 0, "logs" },
109 + { 1, 1, 1, "logs/HEAD" },
110 + { 0, 1, 0, "lost-found" },
111 + { 0, 1, 0, "objects" },
112 + { 0, 1, 0, "refs" },
113 + { 0, 1, 0, "remotes" },
114 + { 0, 1, 0, "worktrees" },
115 + { 0, 1, 0, "rr-cache" },
116 + { 0, 1, 0, "svn" },
117 + { 0, 0, 0, "config" },
118 + { 1, 0, 0, "gc.pid" },
119 + { 0, 0, 0, "packed-refs" },
120 + { 0, 0, 0, "shallow" },
121 + { 0, 0, 0, NULL }
122 };
123
124 static void update_common_dir(struct strbuf *buf, int git_dir_len)
125 {
126 char *base = buf->buf + git_dir_len;
104 - const char **p;
127 + const struct common_dir *p;
128
129 if (is_dir_file(base, "logs", "HEAD") ||
130 is_dir_file(base, "info", "sparse-checkout"))
131 return; /* keep this in $GIT_DIR */
109 - for (p = common_list; *p; p++) {
110 - const char *path = *p;
111 - int is_dir = 0;
112 - if (*path == '!')
113 - path++;
114 - if (*path == '/') {
115 - path++;
116 - is_dir = 1;
117 - }
118 - if (is_dir && dir_prefix(base, path)) {
132 + for (p = common_list; p->dirname; p++) {
133 + const char *path = p->dirname;
134 + if (p->is_dir && dir_prefix(base, path)) {
135 replace_dir(buf, git_dir_len, get_git_common_dir());
136 return;
137 }
122 - if (!is_dir && !strcmp(base, path)) {
138 + if (!p->is_dir && !strcmp(base, path)) {
139 replace_dir(buf, git_dir_len, get_git_common_dir());
140 return;
141 }
@@ -129,16 +145,16 @@ static void update_common_dir(struct strbuf *buf, int git_dir_len)
145 void report_linked_checkout_garbage(void)
146 {
147 struct strbuf sb = STRBUF_INIT;
132 - const char **p;
148 + const struct common_dir *p;
149 int len;
150
151 if (!git_common_dir_env)
152 return;
153 strbuf_addf(&sb, "%s/", get_git_dir());
154 len = sb.len;
139 - for (p = common_list; *p; p++) {
140 - const char *path = *p;
141 - if (*path == '!')
155 + for (p = common_list; p->dirname; p++) {
156 + const char *path = p->dirname;
157 + if (p->ignore_garbage)
158 continue;
159 strbuf_setlen(&sb, len);
160 strbuf_addstr(&sb, path);