Skip to content

Commit

Permalink
book: add aliases for the old ProGit v1 links
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dscho committed Nov 17, 2024
1 parent d2fb555 commit a073826
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .github/workflows/update-book.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
32 changes: 31 additions & 1 deletion script/book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a073826

Please sign in to comment.