mirror of
https://codeberg.org/git-pages/git-pages.git
synced 2026-07-20 16:32:19 +00:00
Unpublish site when pushing an empty repository.
This commit is contained in:
@@ -12,6 +12,18 @@ import (
|
||||
"google.golang.org/protobuf/proto"
|
||||
)
|
||||
|
||||
func IsManifestEmpty(manifest *Manifest) bool {
|
||||
if len(manifest.Tree) > 1 {
|
||||
return false
|
||||
}
|
||||
for name, entry := range manifest.Tree {
|
||||
if name == "" && entry.Type == Type_Directory {
|
||||
return true
|
||||
}
|
||||
}
|
||||
panic(fmt.Errorf("malformed manifest %v", manifest))
|
||||
}
|
||||
|
||||
// Returns `true` if `left` and `right` contain the same files with the same types and data.
|
||||
func CompareManifest(left *Manifest, right *Manifest) bool {
|
||||
if len(left.Tree) != len(right.Tree) {
|
||||
|
||||
+8
-7
@@ -192,21 +192,19 @@ func putPage(w http.ResponseWriter, r *http.Request) error {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), updateTimeout)
|
||||
defer cancel()
|
||||
result := Update(ctx, webRoot, repoURL, branch)
|
||||
if result.manifest != nil {
|
||||
w.Header().Add("Content-Location", r.URL.String())
|
||||
}
|
||||
switch result.outcome {
|
||||
case UpdateError:
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
case UpdateTimeout:
|
||||
w.WriteHeader(http.StatusGatewayTimeout)
|
||||
// HTTP prescribes these response codes to be used
|
||||
case UpdateNoChange:
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
w.Header().Add("X-Pages-Outcome", "no-change")
|
||||
case UpdateCreated:
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
w.Header().Add("X-Pages-Outcome", "created")
|
||||
case UpdateReplaced:
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Header().Add("X-Pages-Outcome", "replaced")
|
||||
case UpdateDeleted:
|
||||
w.Header().Add("X-Pages-Outcome", "deleted")
|
||||
}
|
||||
if result.manifest != nil {
|
||||
fmt.Fprintln(w, result.manifest.Commit)
|
||||
@@ -331,6 +329,9 @@ func postPage(w http.ResponseWriter, r *http.Request) error {
|
||||
case UpdateReplaced:
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintln(w, "replaced")
|
||||
case UpdateDeleted:
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintln(w, "deleted")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+22
-8
@@ -14,6 +14,7 @@ const (
|
||||
UpdateTimeout
|
||||
UpdateCreated
|
||||
UpdateReplaced
|
||||
UpdateDeleted
|
||||
UpdateNoChange
|
||||
)
|
||||
|
||||
@@ -41,14 +42,25 @@ func Update(
|
||||
err = fmt.Errorf("update timeout")
|
||||
} else if err == nil {
|
||||
oldManifest, _ = backend.GetManifest(webRoot)
|
||||
newManifest, err = StoreManifest(webRoot, fetchManifest)
|
||||
if err == nil {
|
||||
if oldManifest == nil {
|
||||
outcome = UpdateCreated
|
||||
} else if CompareManifest(oldManifest, newManifest) {
|
||||
outcome = UpdateNoChange
|
||||
} else {
|
||||
outcome = UpdateReplaced
|
||||
if IsManifestEmpty(fetchManifest) {
|
||||
newManifest, err = fetchManifest, backend.DeleteManifest(webRoot)
|
||||
if err == nil {
|
||||
if oldManifest == nil {
|
||||
outcome = UpdateNoChange
|
||||
} else {
|
||||
outcome = UpdateDeleted
|
||||
}
|
||||
}
|
||||
} else {
|
||||
newManifest, err = StoreManifest(webRoot, fetchManifest)
|
||||
if err == nil {
|
||||
if oldManifest == nil {
|
||||
outcome = UpdateCreated
|
||||
} else if CompareManifest(oldManifest, newManifest) {
|
||||
outcome = UpdateNoChange
|
||||
} else {
|
||||
outcome = UpdateReplaced
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -60,6 +72,8 @@ func Update(
|
||||
status = "created"
|
||||
case UpdateReplaced:
|
||||
status = "replaced"
|
||||
case UpdateDeleted:
|
||||
status = "deleted"
|
||||
case UpdateNoChange:
|
||||
status = "unchanged"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user