give git-http-server requests ability to specify specific revision

---
type: change
message: give git-http-server requests ability to specify specific revision
change_hash: ABk9oYyjhioP0AKGoCd/G/y+foaAcwdeiCgkRzqrdhAk
credentials:
- type: pgp_signature
  pub_key_id: 95C46FA6A41148AC
  body: iQIzBAABAgAdFiEEJ6tQKp6olvZKJ0lwlcRvpqQRSKwFAl6A0x0ACgkQlcRvpqQRSKxfiA//bIumjB6wqTSkUThnUPVMVuaY3295w/a+jtYF/Uz+/tlRO+gcGT+xBfzMXNCUXrGFhG8wYu3ucUOQBmvbgHRuTw8LbQekv9j3Tot7e0YkoZJBZE+HVG8rWR6nzlNhtggtpHS3pdGnDwzWx29FRIreElmCLmxcirKPy1lf8MnMd24K+Ip5cFwqSmZtpq7MIc2eMZkEd4WWlCt8qRPL60grKFuP1OpVdMFv9Vfp4gQT7q7xRw7UZpZj5rt4nHzcro1qEL3ULMjGQvk8zgADKaiGWAKfo4O9KMlXoWTPC1Enu1SvDNq0wf08kBWBtS1UrM5f3ti1BabxdIlk+P+K4Aj/5iN4P3lrL7+YaNdipDJKWxx8duOf5jg5D7a05MXqkSbtMSV3AfXHbdfh75ow9xxN6jcNPramGOW90X4TgebW+hDc0SGqd4HQSbW0MjUUYNsxbxTFaebB1LNNpBcAS3tfo2g14wGaRJLsXiwJ5YgP8gyBBT17fHo6q/18z9g0fep3kWtfjxH86rNgD5ofy6fJtzQDE0hehr9FUCZ4shugHEFUho4dklkvtX57QvcoAR2agsLEy+Czj0xhLFW8ZnBD0S0SudG3cuJHDFZ9Juw8l9sU4W2/o85d5XR6AYeexq6SYtMxNsgR7sUUCXEeZ8DX6rHm8eeL1OyFhL3IDJ6qoQE=
  account: mediocregopher
This commit is contained in:
mediocregopher 2020-03-29 10:56:04 -06:00
parent d6f5bf2e38
commit ee19d2c37e
2 changed files with 29 additions and 21 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/dehub /dehub
*/git-http-server

View File

@ -25,6 +25,32 @@ type handler struct {
tpl *template.Template tpl *template.Template
} }
func (h handler) getTree(r *http.Request) (*object.Tree, int, error) {
rev := plumbing.Revision(r.FormValue("rev"))
if rev == "" {
rev = plumbing.Revision(h.branch)
}
hashPtr, err := h.repo.ResolveRevision(rev)
if err != nil {
return nil, 404, fmt.Errorf("resolving revision %q: %w", rev, err)
}
hash := *hashPtr // I don't know why ResolveRevision returns a pointer
commit, err := h.repo.CommitObject(hash)
if err != nil {
return nil, 404, fmt.Errorf("retrieving commit for revision %q (%q): %w",
rev, hash, err)
}
tree, err := h.repo.TreeObject(commit.TreeHash)
if err != nil {
return nil, 500, fmt.Errorf("fetching tree %q of commit %q: %v",
commit.TreeHash, hash, err)
}
return tree, 0, nil
}
func (h handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) { func (h handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
path := r.URL.Path path := r.URL.Path
var mdPath string var mdPath string
@ -42,28 +68,9 @@ func (h handler) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
path = strings.TrimPrefix(path, "/") path = strings.TrimPrefix(path, "/")
mdPath = strings.TrimPrefix(mdPath, "/") mdPath = strings.TrimPrefix(mdPath, "/")
ref, err := h.repo.Reference(h.branch, true) tree, errStatusCode, err := h.getTree(r)
if errors.Is(err, plumbing.ErrReferenceNotFound) {
http.Error(rw, "branch does not exist", 404)
return
} else if err != nil {
log.Printf("resolving reference %q: %v", h.branch, err)
http.Error(rw, "internal error", 500)
return
}
hash := ref.Hash()
commit, err := h.repo.CommitObject(hash)
if err != nil { if err != nil {
log.Printf("fetching commit %q: %v", hash, err) http.Error(rw, err.Error(), errStatusCode)
http.Error(rw, "internal error", 500)
return
}
tree, err := h.repo.TreeObject(commit.TreeHash)
if err != nil {
log.Printf("fetching tree %q of commit %q: %v", commit.TreeHash, hash, err)
http.Error(rw, "internal error", 500)
return return
} }