From 48fa5025bde598ec767753d8cfcfc5780a2b0ef5 Mon Sep 17 00:00:00 2001 From: Johannes Schindelin Date: Wed, 6 Nov 2024 13:54:38 +0100 Subject: [PATCH] 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 https://github.com/git/git-scm.com/issues/1782 Signed-off-by: Johannes Schindelin --- .github/workflows/update-book.yml | 2 ++ script/book.rb | 32 ++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/.github/workflows/update-book.yml b/.github/workflows/update-book.yml index 9f5eab6c63..491caa78e2 100644 --- a/.github/workflows/update-book.yml +++ b/.github/workflows/update-book.yml @@ -52,10 +52,12 @@ jobs: with: sparse-checkout: | script + data/ external/book/sync external/book/data external/book/content/book/${{ matrix.language.lang }} external/book/content/book${{ matrix.language.lang != 'en' && '/en' || '' }} + external/book/content/book${{ matrix.language.lang != 'en' && '/en' || 'v1' }} external/book/static/book/${{ matrix.language.lang }} - name: clone ${{ matrix.language.repository }} run: | diff --git a/script/book.rb b/script/book.rb index 2d3d9d96a6..9ccb851d19 100644 --- a/script/book.rb +++ b/script/book.rb @@ -133,9 +133,13 @@ def save front_matter["url"] = "/book/#{@language_code}/v#{@edition}.html" front_matter["aliases"] = [ "/book/#{@language_code}/v#{@edition}/index.html", + "/book/#{@language_code}/v1/index.html", "/book/#{@language_code}/index.html" ] - front_matter["aliases"].push("/book/index.html") if @language_code == "en" + if @language_code == "en" + front_matter["aliases"].push("/book/index.html") + front_matter["aliases"].push("/book/v1/index.html") + end front_matter["book"]["front_page"] = true front_matter["book"]["repository_url"] = "https://github.com/#{@@all_books[@language_code]}" front_matter["book"]["sha"] = self.sha @@ -157,9 +161,13 @@ def save front_matter = { "redirect_to" => "book/#{@language_code}/v#{@edition}" } File.write(self.absolute_path("../_index.html"), self.wrap_front_matter(front_matter)) + FileUtils.mkdir_p(self.absolute_path("../v1")) + File.write(self.absolute_path("../v1/_index.html"), self.wrap_front_matter(front_matter)) if @language_code == "en" File.write(self.absolute_path("../../_index.html"), self.wrap_front_matter(front_matter)) + FileUtils.mkdir_p(self.absolute_path("../../v1")) + File.write(self.absolute_path("../../v1/_index.html"), self.wrap_front_matter(front_matter)) end FileUtils.mkdir_p(self.absolute_path("ch00")) @@ -195,6 +203,19 @@ def save end end end + + def book_v1_aliases(cs_number) + if @book_v1_aliases.nil? + path = File.absolute_path(File.join(File.dirname(__FILE__), "..", "data", "book_v1.yml")) + if File.exists?(path) + @book_v1_aliases = YAML.load_file(path)&.[](@language_code) + end + @book_v1_aliases = {} if @book_v1_aliases.nil? + end + return @book_v1_aliases[cs_number]&.flat_map { |title| + ["/book/#{@language_code}/#{title}.html", "/book/#{@language_code}/v1/#{title}.html"] + } + end end class Chapter @@ -254,6 +275,10 @@ def next_chapter def save # TODO end + + def book_v1_aliases(cs_number) + return @book.book_v1_aliases(cs_number) + end end class Section @@ -295,6 +320,11 @@ def front_matter ] end end + v1_aliases = @chapter.book_v1_aliases(self.cs_number) + unless v1_aliases.nil? + front_matter["aliases"] = [] if front_matter["aliases"].nil? + front_matter["aliases"] += v1_aliases + end return front_matter end