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
|
#define USE_THE_REPOSITORY_VARIABLE
#include "cgit.h"
#include "html.h"
#include "ui-shared.h"
#include "ui-tree.h"
#include <string.h>
#include <sys/stat.h>
/* Mapping of file extensions to languages */
struct lang_extension {
const char *ext;
const char *lang;
};
static const struct lang_extension lang_map[] = {{"c", "C"},
{"h", "C"},
{"js", "JavaScript"},
{"jsx", "JavaScript"},
{"py", "Python"},
{"go", "Go"},
{"rs", "Rust"},
{"rb", "Ruby"},
{"java", "Java"},
{"ts", "TypeScript"},
{"tsx", "TypeScript"},
{"cpp", "C++"},
{"cc", "C++"},
{"cxx", "C++"},
{"cs", "C#"},
{"php", "PHP"},
{"sh", "Shell"},
{"bash", "Shell"},
{"lua", "Lua"},
{"vim", "VimL"},
{"clj", "Clojure"},
{"scala", "Scala"},
{"kt", "Kotlin"},
{"swift", "Swift"},
{"m", "Objective-C"},
{"pl", "Perl"},
{"r", "R"},
{"sql", "SQL"},
{"html", "HTML"},
{"htm", "HTML"},
{"css", "CSS"},
{"scss", "SCSS"},
{"json", "JSON"},
{"yaml", "YAML"},
{"yml", "YAML"},
{"md", "Markdown"},
{"rst", "ReStructuredText"},
{NULL, NULL}};
struct lang_count {
const char *lang;
int count;
};
struct lang_detect_context {
struct lang_count counts[100];
int count_nr;
int total_files;
int max_count;
const char *primary_lang;
};
static const char *get_lang_from_ext(const char *filename) {
const char *ext = strrchr(filename, '.');
if (!ext || ext == filename)
return NULL;
ext++; /* skip the dot */
for (int i = 0; lang_map[i].ext; i++) {
if (!strcasecmp(ext, lang_map[i].ext))
return lang_map[i].lang;
}
return NULL;
}
static void count_language(struct lang_detect_context *ctx, const char *lang) {
int i, found = 0;
if (!lang)
return;
ctx->total_files++;
/* Find or add language in counts */
for (i = 0; i < ctx->count_nr; i++) {
if (!strcmp(ctx->counts[i].lang, lang)) {
ctx->counts[i].count++;
if (ctx->counts[i].count > ctx->max_count) {
ctx->max_count = ctx->counts[i].count;
ctx->primary_lang = ctx->counts[i].lang;
}
found = 1;
break;
}
}
if (!found && ctx->count_nr < 100) {
ctx->counts[ctx->count_nr].lang = lang;
ctx->counts[ctx->count_nr].count = 1;
if (1 > ctx->max_count) {
ctx->max_count = 1;
ctx->primary_lang = lang;
}
ctx->count_nr++;
}
}
static int count_files_cb(const struct object_id *oid, struct strbuf *base,
const char *pathname, unsigned mode, void *cbdata) {
struct lang_detect_context *ctx = cbdata;
const char *lang;
/* Skip directories */
if (S_ISDIR(mode))
return READ_TREE_RECURSIVE;
/* Skip hidden files */
if (pathname[0] == '.')
return 0;
lang = get_lang_from_ext(pathname);
count_language(ctx, lang);
return 0;
}
void detect_repo_languages(struct cgit_repo *repo) {
struct object_id oid;
struct commit *commit;
struct tree *tree;
struct pathspec paths = {.nr = 0};
struct lang_detect_context ctx;
if (!repo->defbranch)
return;
memset(&ctx, 0, sizeof(ctx));
/* Get the commit for the default branch */
if (repo_get_oid(the_repository, repo->defbranch, &oid))
return;
commit = lookup_commit_reference(the_repository, &oid);
if (!commit)
return;
if (repo_parse_commit(the_repository, commit))
return;
tree = repo_get_commit_tree(the_repository, commit);
if (!tree)
return;
/* Walk the tree and count file extensions */
read_tree(the_repository, tree, &paths, count_files_cb, &ctx);
/* Store results in repo */
if (ctx.primary_lang) {
repo->primary_lang = xstrdup(ctx.primary_lang);
repo->lang_confidence =
ctx.total_files > 0 ? (100 * ctx.max_count) / ctx.total_files : 0;
}
}
|