Skip to content

Commit

Permalink
Probe for remote URL before loading it.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Jan 28, 2025
1 parent 5a8a459 commit 71c4be6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# Changelog

## 0.7.5 (2025-01-29)

- **[IMPROVED]** `grit browse` (aka `open`) now checks if the remote URL exists
before opening the tree view. If it does not it opens the repository's "root"
page instead. This prevents opening a 404 page when the current branch has not
yet been pushed to `origin`.

## 0.7.4 (2024-10-21)

- **[IMPROVED]** `grit open` (aka `browse`) now opens the GitHub tree view for
- **[IMPROVED]** `grit browse` (aka `open`) now opens the GitHub tree view for
the current branch, tag or hash, instead of always showing the default branch.

## 0.7.3 (2022-03-23)
Expand Down
10 changes: 9 additions & 1 deletion cmd/grit/browse.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"net/http"
"net/url"
"path"
"strings"
Expand Down Expand Up @@ -48,7 +49,6 @@ func browse(cfg grit.Config, idx *index.Index, c *cli.Context) error {
// If we can determine the "HEAD" of the local clone, open the tree view for
// that commit. Otherwise, open the repository's root.
if head, err := r.Head(); err == nil {

// If the head's name is "HEAD", it's a detached HEAD. If the HEAD
// refers to a branch, the name will be the reference to that branch.
if head.Name() == "HEAD" {
Expand All @@ -67,7 +67,15 @@ func browse(cfg grit.Config, idx *index.Index, c *cli.Context) error {
ref = head.Hash().String()
}

orig := u.Path
u.Path = path.Join(u.Path, "tree", ref)

// Check if the target URL actually exists, and if not, revert to the
// original path.
res, err := http.Head(u.String())
if err != nil || res.StatusCode < http.StatusOK || res.StatusCode >= http.StatusBadRequest {
u.Path = orig
}
}

writef(c, "opening %s", u.String())
Expand Down

0 comments on commit 71c4be6

Please sign in to comment.