Skip to content

Commit 5c9795e

Browse files
committed
book: add aliases for the old ProGit v1 links
Once upon a time, there first edition of the ProGit book was available on Git's home page, and the sun was shining. Then, one day in 2014, the sun shone brighter and work was begun to write the second edition of this book. At some stage, this became the default when directing web browsers to https://git-scm.com/book, and a few years later, 2020 or so, the links that formerly led to the first edition would redirect to v2. Then, one day, clouds moved across the sky and the redirects from v1 to v2 stopped working and instead a "500 Internal server error" page was shown. Time went by and nobody really knew how to fix it (or more likely, wasn't in the mood, or wanted other people to fix it). Finally, in the fall of 2024, git-scm.com was switched to a static web site, generated using Hugo, and local development became much easier. Naturally, the v1-to-v2 redirects were no longer in place and the v1 links therefore showed 500 no longer, but a 404. Still, nobody knew how to fix it, or wasn't in the mood, or wanted other people to fix it for them. Until now. Now is the day when we resurrect the v1-to-v2 redirects, in even more glory than ever before, for now we redirect to the v2 sections that correspond to the v1 sections (as far as possible, that is)! Only one (slight) fly in the ointment: URLs to v1 sections of the book which contain anchors will keep those anchors as-are, and not translate them to the corresponding new anchors. Example: Git-Basics-Getting-a-Git-Repository#Cloning-an-Existing-Repository should redirect to v2/Git-Basics-Getting-a-Git-Repository#_git_cloning, but does not. It redirects to that page but still tries to find the anchor `#Cloning-an-Existing-Repository`. Alas, this is where I do not know how to fix it, or ain't in the mood, or want other people to fix it for themselves. This commit addresses git#1782 Signed-off-by: Johannes Schindelin <[email protected]>
1 parent adea9ad commit 5c9795e

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

.github/workflows/update-book.yml

+2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ jobs:
5252
with:
5353
sparse-checkout: |
5454
script
55+
data/
5556
external/book/sync
5657
external/book/data
5758
external/book/content/book/${{ matrix.language.lang }}
5859
external/book/content/book${{ matrix.language.lang != 'en' && '/en' || '' }}
60+
external/book/content/book${{ matrix.language.lang != 'en' && '/en' || 'v1' }}
5961
external/book/static/book/${{ matrix.language.lang }}
6062
- name: clone ${{ matrix.language.repository }}
6163
run: |

script/book.rb

+31-1
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,13 @@ def save
133133
front_matter["url"] = "/book/#{@language_code}/v#{@edition}.html"
134134
front_matter["aliases"] = [
135135
"/book/#{@language_code}/v#{@edition}/index.html",
136+
"/book/#{@language_code}/v1/index.html",
136137
"/book/#{@language_code}/index.html"
137138
]
138-
front_matter["aliases"].push("/book/index.html") if @language_code == "en"
139+
if @language_code == "en"
140+
front_matter["aliases"].push("/book/index.html")
141+
front_matter["aliases"].push("/book/v1/index.html")
142+
end
139143
front_matter["book"]["front_page"] = true
140144
front_matter["book"]["repository_url"] = "https://github.com/#{@@all_books[@language_code]}"
141145
front_matter["book"]["sha"] = self.sha
@@ -157,9 +161,13 @@ def save
157161

158162
front_matter = { "redirect_to" => "book/#{@language_code}/v#{@edition}" }
159163
File.write(self.absolute_path("../_index.html"), self.wrap_front_matter(front_matter))
164+
FileUtils.mkdir_p(self.absolute_path("../v1"))
165+
File.write(self.absolute_path("../v1/_index.html"), self.wrap_front_matter(front_matter))
160166

161167
if @language_code == "en"
162168
File.write(self.absolute_path("../../_index.html"), self.wrap_front_matter(front_matter))
169+
FileUtils.mkdir_p(self.absolute_path("../../v1"))
170+
File.write(self.absolute_path("../../v1/_index.html"), self.wrap_front_matter(front_matter))
163171
end
164172

165173
FileUtils.mkdir_p(self.absolute_path("ch00"))
@@ -195,6 +203,19 @@ def save
195203
end
196204
end
197205
end
206+
207+
def book_v1_aliases(cs_number)
208+
if @book_v1_aliases.nil?
209+
path = File.absolute_path(File.join(File.dirname(__FILE__), "..", "data", "book_v1.yml"))
210+
if File.exists?(path)
211+
@book_v1_aliases = YAML.load_file(path)&.[](@language_code)
212+
end
213+
@book_v1_aliases = {} if @book_v1_aliases.nil?
214+
end
215+
return @book_v1_aliases[cs_number]&.flat_map { |title|
216+
["/book/#{@language_code}/#{title}.html", "/book/#{@language_code}/v1/#{title}.html"]
217+
}
218+
end
198219
end
199220

200221
class Chapter
@@ -254,6 +275,10 @@ def next_chapter
254275
def save
255276
# TODO
256277
end
278+
279+
def book_v1_aliases(cs_number)
280+
return @book.book_v1_aliases(cs_number)
281+
end
257282
end
258283

259284
class Section
@@ -295,6 +320,11 @@ def front_matter
295320
]
296321
end
297322
end
323+
v1_aliases = @chapter.book_v1_aliases(self.cs_number)
324+
unless v1_aliases.nil?
325+
front_matter["aliases"] = [] if front_matter["aliases"].nil?
326+
front_matter["aliases"] += v1_aliases
327+
end
298328
return front_matter
299329
end
300330

0 commit comments

Comments
 (0)