-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitree.c
More file actions
299 lines (265 loc) · 6.68 KB
/
Copy pathgitree.c
File metadata and controls
299 lines (265 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* gitree can be used to:
* 1. Check git directory structure. (dir terminated with .git,
* including *.git and .git, and dir having objects and refs
* directories)
* 2. Find dir which has .git directory.
* 3. Find all files which are not in a bare Git tree.
*
* V1: Two problems left:
* 1. When checking a .git tree directly, it will report false
* warning "name not terminated with .git".
* 2. When checking a .git tree name not terminated with .git,
* it will report the files not in a git tree by mistake.
*
* V2: Redesign the core algorithm:
* 1. Check valid git tree through key git files.
* 2. For git tree, perform conformance check, give warnings when
* 1) files break Git repo layout rule,
* 2) git dirs name not terminated with .git,
* 3) git dirs non-bare git tree,
* 4) files not in a git tree.
* 3. For non git tree, print all the files under it and then
* continue the check with its sub directories.
*/
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define SUBDIRNO 4096
#define SUBFILENO 4096
static char git_files[][128] = {
/* git files */
"COMMIT_EDITMSG",
"config",
"description",
"FETCH_HEAD",
"HEAD",
"index",
"packed-refs",
"ORIG_HEAD",
"MERGE_HEAD",
"MERGE_MODE",
"MERGE_MSG",
"MERGE_RR",
"RENAMED-REF",
"gitk.cache",
/* git dirs */
"hooks",
"info",
"logs",
"objects",
"rebase-apply",
"refs",
"branches",
"remotes",
"shallow",
"rr-cache",
/* getweb */
"cloneurl",
/* repo files */
".repopickle_config",
"clone.bundle",
/* other files */
"config.bak",
"config_bak",
"config~",
"description~",
"hooks_bk",
"hooks.bak",
"hooks-bak",
"COMMIT_EDITMSG~",
".gitignore",
"pnt",
"svn",
"temp.patch",
};
static char exception_list[][128] = {
"/git/android/.repo",
};
static int git_files_array_size
= sizeof(git_files) / sizeof(git_files[0]);
static int exception_list_array_size
= sizeof(exception_list) / sizeof(exception_list[0]);
static int sum_break_layout_rule, sum_dir_name_not_with_git,
sum_non_bare_git, sum_not_in_git;
static void usage(void)
{
fprintf(stderr, "Usage: ./gitree pathname\n"
"Perform conformance check, give warnings when\n"
"1. files break Git repo layout rule\n"
"2. git dirs name not terminated with .git\n"
"3. git dirs non-bare git tree\n"
"4. files not in a git tree\n");
exit(-1);
}
static int in_exception_list(char *dirname)
{
int i, str_len;
for (i = 0; i < exception_list_array_size; i++) {
str_len = strlen(exception_list[i]);
if (!strncmp(dirname, exception_list[i], str_len))
break;
}
if (i == exception_list_array_size)
return 0;
else
return 1;
}
static void check_gitree(char *dirname)
{
char *last_dir;
int dir_name_with_git = 0;
int dir_len;
DIR *dirp;
struct dirent *direntp;
int i;
last_dir = strrchr(dirname, '/');
if (last_dir == NULL)
last_dir = dirname;
else
last_dir++;
dir_len = strlen(last_dir);
if ((dir_len >= 4) && !strncmp(last_dir + dir_len - 4, ".git", 4))
dir_name_with_git = 1;
if ((dir_name_with_git == 1) && (dir_len == 4)) {
if (!in_exception_list(dirname)) {
sum_non_bare_git++;
printf("WARNING: %s non-bare git tree\n", dirname);
}
}
if (!dir_name_with_git) {
sum_dir_name_not_with_git++;
printf("WARNING: %s name not terminated with .git\n",
dirname);
}
if ((dirp = opendir(dirname)) == NULL) {
fprintf(stderr, "check_gitree: opendir failed\n");
exit(-1);
}
while ((direntp = readdir(dirp)) != NULL) {
if (!strcmp(direntp->d_name, "."))
continue;
else if (!strcmp(direntp->d_name, ".."))
continue;
for (i = 0; i < git_files_array_size; i++) {
if (!strcmp(direntp->d_name, git_files[i]))
break;
}
if (i != git_files_array_size) {
continue;
} else {
sum_break_layout_rule++;
printf("WARNING: %s/%s breaks Git repo layout rule\n",
dirname, direntp->d_name);
}
}
closedir(dirp);
}
static void gitree(char *dirname)
{
/*
* Parameter order reveals the development history.
* The last one shows the latest feature development.
* DO NOT change the order of the parameters.
*/
DIR *dirp;
struct dirent *direntp;
char *subdir[SUBDIRNO];
int i = 0, subdirn, str_len, dir_len, subdir_len;
int has_dir_objects = 0, has_dir_refs = 0;
int has_file_HEAD = 0;
char *subfile[SUBFILENO];
int j = 0, subfilen, subfile_len;
if ((dirp = opendir(dirname)) == NULL) {
fprintf(stderr, "gitree: opendir failed\n");
exit(-1);
}
printf("Checking %s\n", dirname);
dir_len = strlen(dirname);
while ((direntp = readdir(dirp)) != NULL) {
if (!strcmp(direntp->d_name, "."))
continue;
else if (!strcmp(direntp->d_name, ".."))
continue;
if (direntp->d_type == DT_DIR) {
if (!strcmp(direntp->d_name, "objects"))
has_dir_objects = 1;
if (!strcmp(direntp->d_name, "refs"))
has_dir_refs = 1;
subdir_len = strlen(direntp->d_name);
str_len = dir_len + 1 + subdir_len;
subdir[i] = malloc(str_len + 1);
strcpy(subdir[i], dirname);
strcat(subdir[i], "/");
strcat(subdir[i], direntp->d_name);
subdir[i][str_len] = '\0';
i++;
if (i >= SUBDIRNO) {
fprintf(stderr, "ERROR: gitree: reach max dir num\n");
exit(-1);
}
} else if (direntp->d_type == DT_REG) {
if (!strcmp(direntp->d_name, "HEAD"))
has_file_HEAD = 1;
subfile_len = strlen(direntp->d_name);
subfile[j] = malloc(subfile_len + 1);
strcpy(subfile[j], direntp->d_name);
subfile[j][subfile_len] = '\0';
j++;
if (j >= SUBFILENO) {
fprintf(stderr, "ERROR: gitree: reach max file num\n");
exit(-1);
}
} else if (direntp->d_type == DT_UNKNOWN) {
fprintf(stderr, "ERROR: gitree: unknown file type\n");
exit(-2);
}
}
closedir(dirp);
subdirn = i;
subfilen = j;
if (has_dir_objects && has_dir_refs && has_file_HEAD) {
for (i = 0; i < subdirn; i++)
free(subdir[i]);
for (j = 0; j < subfilen; j++)
free(subfile[j]);
check_gitree(dirname);
} else {
for (j = 0; j < subfilen; j++) {
if (!in_exception_list(dirname)) {
sum_not_in_git++;
printf("WARNING: %s/%s not in a git tree\n",
dirname, subfile[j]);
}
free(subfile[j]);
}
for (i = 0; i < subdirn; i++) {
gitree(subdir[i]);
free(subdir[i]);
}
}
}
int main(int argc, char *argv[])
{
int dir_len;
if (argc != 2)
usage();
dir_len = strlen(argv[1]);
dir_len--;
while (argv[1][dir_len] == '/') {
argv[1][dir_len] = '\0';
dir_len--;
}
gitree(argv[1]);
printf("\nCheck Result:\n"
"%d files break Git repo layout rule\n"
"%d git dirs name not terminated with .git\n"
"%d git dirs non-bare git tree\n"
"%d files not in a git tree\n",
sum_break_layout_rule, sum_dir_name_not_with_git,
sum_non_bare_git, sum_not_in_git);
return 0;
}