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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
# frozen_string_literal: true
require "asciidoctor"
require "octokit"
require "time"
require "digest/sha1"
require "set"
def make_asciidoc(content)
Asciidoctor::Document.new(content,
attributes: {
"sectanchors" => "",
"litdd" => "&\#x2d;&\#x2d;",
"compat-mode" => "",
},
doctype: "book")
end
def expand_l10n(path, content, get_f_content, categories)
content.gsub!(/include::(\S+)\.txt/) do |line|
line.gsub!("include::", "")
if categories[line]
new_content = categories[line]
else
new_content, new_path = get_f_content.call(path, line)
end
if new_content
expand_l10n(new_path, new_content, get_f_content, categories)
else
"\n\n[WARNING]\n====\nMissing `#{new_path}`\n\nSee original version for this content.\n====\n\n"
end
end
content
end
def index_l10n_doc(filter_tags, doc_list, get_content)
ActiveRecord::Base.logger.level = Logger::WARN
rebuild = ENV.fetch("REBUILD_DOC", nil)
rerun = ENV["RERUN"] || rebuild || false
filter_tags.call(rebuild, false).sort_by { |tag| Version.version_to_num(tag.first[1..]) }.each do |tag|
name, commit_sha, tree_sha, ts = tag
puts "#{name}: #{ts}, #{commit_sha[0, 8]}, #{tree_sha[0, 8]}"
stag = Version.where(name: name.gsub("v", "l10n")).first_or_create
next if (stag.commit_sha == commit_sha) && !rerun
stag.commit_sha = commit_sha
stag.tree_sha = tree_sha
stag.committed = ts
stag.save
tag_files = doc_list.call(tree_sha)
doc_files = tag_files.select do |ent|
ent.first =~
/^([-_\w]+)\/(
(
git.*
)\.txt)/x
end
puts "Found #{doc_files.size} entries"
get_content_f = proc do |source, target|
name = File.join(File.dirname(source), target)
content_file = tag_files.detect { |ent| ent.first == name }
if content_file
new_content = get_content.call(content_file.second)
else
puts "Included file #{name} was not translated. Processing anyway\n"
end
[new_content, name]
end
doc_files.each do |entry|
full_path, sha = entry
ids = Set.new([])
lang = File.dirname(full_path)
path = File.basename(full_path, ".txt")
file = DocFile.where(name: path).first_or_create
puts " build: #{path} for #{lang}"
content = get_content.call sha
categories = {}
expand_l10n(full_path, content, get_content_f, categories)
content.gsub!(/link:(?:technical\/)?(\S*?)\.html(\#\S*?)?\[(.*?)\]/m, "link:/docs/\\1/#{lang}\\2[\\3]")
asciidoc = make_asciidoc(content)
asciidoc_sha = Digest::SHA1.hexdigest(asciidoc.source)
doc = Doc.where(blob_sha: asciidoc_sha).first_or_create
if rerun || !doc.plain || !doc.html
html = asciidoc.render
html.gsub!(/linkgit:(\S+)\[(\d+)\]/) do |line|
x = /^linkgit:(\S+)\[(\d+)\]/.match(line)
"<a href='/docs/#{x[1]}/#{lang}'>#{x[1]}[#{x[2]}]</a>"
end
# HTML anchor on hdlist1 (i.e. command options)
html.gsub!(/<dt class="hdlist1">(.*?)<\/dt>/) do |_m|
text = $1.tr("^A-Za-z0-9-", "")
anchor = "#{path}-#{text}"
# handle anchor collisions by appending -1
anchor += "-1" while ids.include?(anchor)
ids.add(anchor)
"<dt class=\"hdlist1\" id=\"#{anchor}\"> <a class=\"anchor\" href=\"##{anchor}\"></a>#{$1} </dt>"
end
doc.plain = asciidoc.source
doc.html = html
doc.save
end
dv = DocVersion.where(version_id: stag.id, doc_file_id: file.id, language: lang).first_or_create
dv.doc_id = doc.id
dv.language = lang
dv.save
end
end
end
def drop_uninteresting_tags(tags)
# proceed in reverse-chronological order, as we'll pick only the
# highest-numbered point release for older versions
ret = []
tags.reverse_each do |tag|
numeric = Version.version_to_num(tag.first[1..])
# drop anything older than v2.0
next if numeric < 2_000_000
# older than v2.17, take only the highest release
if (numeric < 2_170_000) && !ret.empty?
old = Version.version_to_num(ret[0].first[1..])
next if old.to_i.div(10_000) == numeric.to_i.div(10_000)
end
# keep everything else
ret.unshift(tag)
end
ret
end
def expand_content(content, path, get_f_content, generated)
content.gsub(/include::(\S+)\.txt\[\]/) do |_line|
if File.dirname(path) == "."
new_fname = "#{$1}.txt"
else
new_fname = (Pathname.new(path).dirname + Pathname.new("#{$1}.txt")).cleanpath.to_s
end
if generated[new_fname]
new_content = generated[new_fname]
else
new_content = get_f_content.call(new_fname)
if new_content
expand_content(new_content.force_encoding("UTF-8"), new_fname, get_f_content, generated)
else
puts "#{new_fname} could not be resolved for expansion"
end
end
new_content
end
end
def index_doc(filter_tags, doc_list, get_content)
ActiveRecord::Base.logger.level = Logger::WARN
rebuild = ENV.fetch("REBUILD_DOC", nil)
rerun = ENV["RERUN"] || rebuild || false
tags = filter_tags.call(rebuild).sort_by { |tag| Version.version_to_num(tag.first[1..]) }
drop_uninteresting_tags(tags).each do |tag|
name, commit_sha, tree_sha, ts = tag
puts "#{name}: #{ts}, #{commit_sha[0, 8]}, #{tree_sha[0, 8]}"
stag = Version.where(name: name.delete("v")).first
next if stag && !rerun
stag = Version.where(name: name.delete("v")).first_or_create
stag.commit_sha = commit_sha
stag.tree_sha = tree_sha
stag.committed = ts
stag.save
tag_files = doc_list.call(tree_sha)
doc_files = tag_files.select do |ent|
ent.first =~
/^Documentation\/(
SubmittingPatches |
MyFirstContribution.txt |
MyFirstObjectWalk.txt |
(
git.* |
everyday |
howto-index |
user-manual |
diff.* |
fetch.* |
merge.* |
rev.* |
pretty.* |
pull.* |
technical\/.*
)\.txt)/x
end
puts "Found #{doc_files.size} entries"
doc_limit = ENV.fetch("ONLY_BUILD_DOC", nil)
# generate command-list content
generated = {}
cmd = tag_files.detect { |f| f.first == "command-list.txt" }
if cmd
cmd_list =
get_content
.call(cmd.second)
.match(/(### command list.*|# command name.*)/m)[0]
.split("\n")
.grep_v(/^#/)
.each_with_object({}) do |cmd, list|
name, kind, attr = cmd.split(/\s+/)
list[kind] ||= []
list[kind] << [name, attr]
end
generated = cmd_list.keys.inject({}) do |list, category|
links = cmd_list[category].map do |cmd, attr|
cmd_file = tag_files.detect { |ent| ent.first == "Documentation/#{cmd}.txt" }
next unless cmd_file
content = get_content.call(cmd_file.second)
section = content.match(/^git.*\(([1-9])\)/)[1]
match = content.match(/NAME\n----\n\S+ - (.*)$/)
if match
"linkgit:#{cmd}[#{section}]::\n\t#{attr == 'deprecated' ? '(deprecated) ' : ''}#{match[1]}\n"
end
end
list.merge!("Documentation/cmds-#{category}.txt" => links.compact.join("\n"))
end
tools = tag_files.select { |ent| ent.first =~ /^mergetools\// }.map do |entry|
path, sha = entry
tool = File.basename path
content = get_content.call sha
merge = content.include?("can_merge") ? "" : " * #{tool}\n"
diff = content.include?("can_diff") ? "" : " * #{tool}\n"
[merge, diff]
end
can_merge, can_diff = tools.transpose.map(&:join)
generated["Documentation/mergetools-diff.txt"] = can_diff
generated["Documentation/mergetools-merge.txt"] = can_merge
get_content_f = proc do |name|
content_file = tag_files.detect { |ent| ent.first == name }
if content_file
new_content = get_content.call(content_file.second)
end
new_content
end
doc_files.each do |entry|
path, sha = entry
ids = Set.new([])
docname = File.basename(path, ".txt")
next if doc_limit && path !~ /#{doc_limit}/
file = DocFile.where(name: docname).first_or_create
puts " build: #{docname}"
content = expand_content((get_content.call sha).force_encoding("UTF-8"), path, get_content_f, generated)
content.gsub!(/link:(?:technical\/)?(\S*?)\.html(\#\S*?)?\[(.*?)\]/m, "link:/docs/\\1\\2[\\3]")
asciidoc = make_asciidoc(content)
asciidoc_sha = Digest::SHA1.hexdigest(asciidoc.source)
doc = Doc.where(blob_sha: asciidoc_sha).first_or_create
if rerun || !doc.plain || !doc.html
html = asciidoc.render
html.gsub!(/linkgit:(\S+)\[(\d+)\]/) do |line|
x = /^linkgit:(\S+)\[(\d+)\]/.match(line)
"<a href='/docs/#{x[1]}'>#{x[1]}[#{x[2]}]</a>"
end
# HTML anchor on hdlist1 (i.e. command options)
html.gsub!(/<dt class="hdlist1">(.*?)<\/dt>/) do |_m|
text = $1.tr("^A-Za-z0-9-", "")
anchor = "#{path}-#{text}"
# handle anchor collisions by appending -1
anchor += "-1" while ids.include?(anchor)
ids.add(anchor)
"<dt class=\"hdlist1\" id=\"#{anchor}\"> <a class=\"anchor\" href=\"##{anchor}\"></a>#{$1} </dt>"
end
doc.plain = asciidoc.source
doc.html = html
doc.save
end
dv = DocVersion.where(version_id: stag.id, doc_file_id: file.id, language: "en").first_or_create
dv.doc_id = doc.id
dv.language = "en"
dv.save
end
end
Rails.cache.write("latest-version", Version.latest_version.name)
end
end
def github_index_doc(index_fun, repo)
Octokit.auto_paginate = true
@octokit = Octokit::Client.new(access_token: ENV.fetch("GITHUB_API_TOKEN", nil))
repo = ENV["GIT_REPO"] || repo
blob_content = Hash.new do |blobs, sha|
content = Base64.decode64(@octokit.blob(repo, sha, encoding: "base64").content)
blobs[sha] = content.force_encoding("UTF-8")
end
tag_filter = lambda do |tagname, gettags = true|
# find all tags
if gettags
tags = @octokit.tags(repo).select { |tag| !tag.nil? && tag.name =~ /v\d([.\d])+$/ } # just get release tags
if tagname
tags = tags.select { |t| t.name == tagname }
end
else
tags = [Struct.new(:name).new("HEAD")]
end
tags.collect do |tag|
# extract metadata
commit_info = @octokit.commit(repo, tag.name)
commit_sha = commit_info.sha
tree_sha = commit_info.commit.tree.sha
# ts = Time.parse( commit_info.commit.committer.date )
ts = commit_info.commit.committer.date
[tag.name, commit_sha, tree_sha, ts]
end
end
get_content = ->(sha) do blob_content[sha] end
get_file_list = lambda do |tree_sha|
tree_info = @octokit.tree(repo, tree_sha, recursive: true)
tree_info.tree.collect { |ent| [ent.path, ent.sha] }
end
send(index_fun, tag_filter, get_file_list, get_content)
end
def local_index_doc(index_fun)
dir = ENV.fetch("GIT_REPO", nil)
Dir.chdir(dir) do
tag_filter = lambda do |tagname, gettags = true|
if gettags
# find all tags
tags = `git tag | egrep 'v1|v2'`.strip.split("\n")
tags = tags.grep(/v\d([.\d])+$/) # just get release tags
if tagname
tags = tags.select { |t| t == tagname }
end
else
tags = ["HEAD"]
end
tags.collect do |tag|
# extract metadata
commit_sha = `git rev-parse #{tag}`.chomp
tree_sha = `git rev-parse #{tag}^{tree}`.chomp
tagger = `git cat-file commit #{tag} | grep committer`.chomp.split
_tz = tagger.pop
ts = tagger.pop
ts = Time.at(ts.to_i)
[tag, commit_sha, tree_sha, ts]
end
end
get_content = ->(sha) do `git cat-file blob #{sha}` end
get_file_list = lambda do |tree_sha|
entries = `git ls-tree -r #{tree_sha}`.strip.split("\n")
entries.map do |e|
_mode, _type, sha, path = e.split
[path, sha]
end
end
send(index_fun, tag_filter, get_file_list, get_content)
end
end
task local_index: :environment do
local_index_doc(:index_doc)
end
task local_index_l10n: :environment do
local_index_doc(:index_l10n_doc)
end
task preindex: :environment do
github_index_doc(:index_doc, "gitster/git")
end
task preindex_l10n: :environment do
github_index_doc(:index_l10n_doc, "jnavila/git-html-l10n")
end
|