+19
-24
.forgejo/workflows/ci.yaml
+19
-24
.forgejo/workflows/ci.yaml
···
7
8
jobs:
9
check:
10
-
runs-on: codeberg-small-lazy
11
container:
12
-
image: docker.io/library/node:24-trixie-slim@sha256:fcdfd7bcd8f641c8c76a8950343c73912d68ba341e8dd1074e663b784d3e76f4
13
steps:
14
- name: Check out source code
15
-
uses: https://code.forgejo.org/actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
16
- name: Set up toolchain
17
uses: https://code.forgejo.org/actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
18
with:
···
34
# IMPORTANT: This workflow step will not work without the Releases unit enabled!
35
if: ${{ forge.ref == 'refs/heads/main' || startsWith(forge.event.ref, 'refs/tags/v') }}
36
needs: [check]
37
-
runs-on: codeberg-small-lazy
38
container:
39
-
image: docker.io/library/node:24-trixie-slim@sha256:fcdfd7bcd8f641c8c76a8950343c73912d68ba341e8dd1074e663b784d3e76f4
40
steps:
41
- name: Check out source code
42
-
uses: https://code.forgejo.org/actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
43
- name: Set up toolchain
44
uses: https://code.forgejo.org/actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
45
with:
···
70
package:
71
if: ${{ forge.ref == 'refs/heads/main' || startsWith(forge.event.ref, 'refs/tags/v') }}
72
needs: [check]
73
-
runs-on: codeberg-small-lazy
74
container:
75
-
image: docker.io/library/node:24-trixie-slim@sha256:fcdfd7bcd8f641c8c76a8950343c73912d68ba341e8dd1074e663b784d3e76f4
76
steps:
77
- name: Install dependencies
78
run: |
79
apt-get -y update
80
-
apt-get -y install buildah ca-certificates
81
- name: Check out source code
82
-
uses: https://code.forgejo.org/actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
83
- name: Build container
84
run: |
85
printf '[storage]\ndriver="vfs"\nrunroot="/run/containers/storage"\ngraphroot="/var/lib/containers/storage"\n' | tee /etc/containers/storage.conf
86
-
buildah build --arch=amd64 --tag=container:${VER}-amd64 .
87
-
buildah build --arch=arm64 --tag=container:${VER}-arm64 .
88
-
buildah manifest create container:${VER} \
89
-
container:${VER}-amd64 \
90
-
container:${VER}-arm64
91
env:
92
BUILDAH_ISOLATION: chroot
93
-
VER: ${{ startsWith(forge.event.ref, 'refs/tags/v') && forge.ref_name || 'latest' }}
94
- if: ${{ forge.repository == 'git-pages/git-pages-cli' }}
95
name: Push container to Codeberg
96
run: |
97
-
buildah login --authfile=/tmp/authfile-${FORGE}.json \
98
-u ${{ vars.PACKAGES_USER }} -p ${{ secrets.PACKAGES_TOKEN }} ${FORGE}
99
-
buildah manifest push --authfile=/tmp/authfile-${FORGE}.json \
100
-
--all container:${VER} "docker://${FORGE}/${{ forge.repository }}:${VER/v/}"
101
env:
102
-
BUILDAH_ISOLATION: chroot
103
FORGE: codeberg.org
104
VER: ${{ startsWith(forge.event.ref, 'refs/tags/v') && forge.ref_name || 'latest' }}
105
- if: ${{ forge.repository == 'git-pages/git-pages-cli' }}
106
name: Push container to code.forgejo.org
107
run: |
108
-
buildah login --authfile=/tmp/authfile-${FORGE}.json \
109
-u ${{ vars.PACKAGES_USER }} -p ${{ secrets.CFO_PACKAGES_TOKEN }} ${FORGE}
110
-
buildah manifest push --authfile=/tmp/authfile-${FORGE}.json \
111
-
--all container:${VER} "docker://${FORGE}/${{ forge.repository }}:${VER/v/}"
112
env:
113
-
BUILDAH_ISOLATION: chroot
114
FORGE: code.forgejo.org
115
VER: ${{ startsWith(forge.event.ref, 'refs/tags/v') && forge.ref_name || 'latest' }}
···
7
8
jobs:
9
check:
10
+
runs-on: debian-trixie
11
container:
12
+
image: docker.io/library/node:24-trixie-slim@sha256:b05474903f463ce4064c09986525e6588c3e66c51b69be9c93a39fb359f883ce
13
steps:
14
- name: Check out source code
15
+
uses: https://code.forgejo.org/actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
16
- name: Set up toolchain
17
uses: https://code.forgejo.org/actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
18
with:
···
34
# IMPORTANT: This workflow step will not work without the Releases unit enabled!
35
if: ${{ forge.ref == 'refs/heads/main' || startsWith(forge.event.ref, 'refs/tags/v') }}
36
needs: [check]
37
+
runs-on: debian-trixie
38
container:
39
+
image: docker.io/library/node:24-trixie-slim@sha256:b05474903f463ce4064c09986525e6588c3e66c51b69be9c93a39fb359f883ce
40
steps:
41
- name: Check out source code
42
+
uses: https://code.forgejo.org/actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
43
- name: Set up toolchain
44
uses: https://code.forgejo.org/actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
45
with:
···
70
package:
71
if: ${{ forge.ref == 'refs/heads/main' || startsWith(forge.event.ref, 'refs/tags/v') }}
72
needs: [check]
73
+
runs-on: debian-trixie
74
container:
75
+
image: docker.io/library/node:24-trixie-slim@sha256:b05474903f463ce4064c09986525e6588c3e66c51b69be9c93a39fb359f883ce
76
steps:
77
- name: Install dependencies
78
run: |
79
apt-get -y update
80
+
apt-get -y install ca-certificates buildah qemu-user-binfmt
81
- name: Check out source code
82
+
uses: https://code.forgejo.org/actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
83
- name: Build container
84
run: |
85
printf '[storage]\ndriver="vfs"\nrunroot="/run/containers/storage"\ngraphroot="/var/lib/containers/storage"\n' | tee /etc/containers/storage.conf
86
+
buildah build --arch=amd64 --tag=container:amd64
87
+
buildah build --arch=arm64 --tag=container:arm64
88
+
buildah manifest create container container:amd64 container:arm64
89
env:
90
BUILDAH_ISOLATION: chroot
91
- if: ${{ forge.repository == 'git-pages/git-pages-cli' }}
92
name: Push container to Codeberg
93
run: |
94
+
buildah login --authfile=/tmp/authfile.json \
95
-u ${{ vars.PACKAGES_USER }} -p ${{ secrets.PACKAGES_TOKEN }} ${FORGE}
96
+
buildah manifest push --authfile=/tmp/authfile.json \
97
+
--all container "docker://${FORGE}/${{ forge.repository }}:${VER/v/}"
98
env:
99
FORGE: codeberg.org
100
VER: ${{ startsWith(forge.event.ref, 'refs/tags/v') && forge.ref_name || 'latest' }}
101
- if: ${{ forge.repository == 'git-pages/git-pages-cli' }}
102
name: Push container to code.forgejo.org
103
run: |
104
+
buildah login --authfile=/tmp/authfile.json \
105
-u ${{ vars.PACKAGES_USER }} -p ${{ secrets.CFO_PACKAGES_TOKEN }} ${FORGE}
106
+
buildah manifest push --authfile=/tmp/authfile.json \
107
+
--all container "docker://${FORGE}/${{ forge.repository }}:${VER/v/}"
108
env:
109
FORGE: code.forgejo.org
110
VER: ${{ startsWith(forge.event.ref, 'refs/tags/v') && forge.ref_name || 'latest' }}
+1
-1
Dockerfile
+1
-1
Dockerfile
+3
-2
README.md
+3
-2
README.md
···
3
4
_git-pages-cli_ is a command-line application for publishing sites to [git-pages].
5
6
-
If you want to publish a site from a Forgejo Actions workflow, use [git-pages/action] instead.
7
8
[git-pages]: https://codeberg.org/git-pages/git-pages
9
-
[git-pages/action]: https://codeberg.org/git-pages/action
10
11
12
Installation
···
3
4
_git-pages-cli_ is a command-line application for publishing sites to [git-pages].
5
6
+
> [!TIP]
7
+
> If you want to publish a site from a CI workflow, use the [Forgejo Action][git-pages-action] instead.
8
9
[git-pages]: https://codeberg.org/git-pages/git-pages
10
+
[git-pages-action]: https://codeberg.org/git-pages/action
11
12
13
Installation
+24
flake.lock
+24
flake.lock
···
18
"type": "github"
19
}
20
},
21
+
"gomod2nix": {
22
+
"inputs": {
23
+
"flake-utils": [
24
+
"flake-utils"
25
+
],
26
+
"nixpkgs": [
27
+
"nixpkgs"
28
+
]
29
+
},
30
+
"locked": {
31
+
"lastModified": 1763982521,
32
+
"narHash": "sha256-ur4QIAHwgFc0vXiaxn5No/FuZicxBr2p0gmT54xZkUQ=",
33
+
"owner": "nix-community",
34
+
"repo": "gomod2nix",
35
+
"rev": "02e63a239d6eabd595db56852535992c898eba72",
36
+
"type": "github"
37
+
},
38
+
"original": {
39
+
"owner": "nix-community",
40
+
"repo": "gomod2nix",
41
+
"type": "github"
42
+
}
43
+
},
44
"nix-filter": {
45
"locked": {
46
"lastModified": 1757882181,
···
75
"root": {
76
"inputs": {
77
"flake-utils": "flake-utils",
78
+
"gomod2nix": "gomod2nix",
79
"nix-filter": "nix-filter",
80
"nixpkgs": "nixpkgs"
81
}
+22
-4
flake.nix
+22
-4
flake.nix
···
3
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
4
flake-utils.url = "github:numtide/flake-utils";
5
nix-filter.url = "github:numtide/nix-filter";
6
};
7
8
outputs =
···
11
nixpkgs,
12
flake-utils,
13
nix-filter,
14
-
}:
15
flake-utils.lib.eachDefaultSystem (
16
system:
17
let
18
-
pkgs = nixpkgs.legacyPackages.${system};
19
20
-
git-pages-cli = pkgs.buildGo125Module {
21
pname = "git-pages-cli";
22
version = "0";
23
···
41
"-s -w"
42
];
43
44
-
vendorHash = "sha256-5vjUhN3lCr41q91lOD7v0F9c6a8GJj7wBGnnzgFBhJU=";
45
};
46
in
47
{
···
50
devShells.default = pkgs.mkShell {
51
inputsFrom = [
52
git-pages-cli
53
];
54
};
55
···
3
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
4
flake-utils.url = "github:numtide/flake-utils";
5
nix-filter.url = "github:numtide/nix-filter";
6
+
7
+
gomod2nix = {
8
+
url = "github:nix-community/gomod2nix";
9
+
inputs.nixpkgs.follows = "nixpkgs";
10
+
inputs.flake-utils.follows = "flake-utils";
11
+
};
12
};
13
14
outputs =
···
17
nixpkgs,
18
flake-utils,
19
nix-filter,
20
+
...
21
+
}@inputs:
22
flake-utils.lib.eachDefaultSystem (
23
system:
24
let
25
+
pkgs = import nixpkgs {
26
+
inherit system;
27
28
+
overlays = [
29
+
inputs.gomod2nix.overlays.default
30
+
];
31
+
};
32
+
33
+
git-pages-cli = pkgs.buildGoApplication {
34
pname = "git-pages-cli";
35
version = "0";
36
···
54
"-s -w"
55
];
56
57
+
go = pkgs.go_1_25;
58
+
modules = ./gomod2nix.toml;
59
};
60
in
61
{
···
64
devShells.default = pkgs.mkShell {
65
inputsFrom = [
66
git-pages-cli
67
+
];
68
+
69
+
packages = with pkgs; [
70
+
gomod2nix
71
];
72
};
73
+2
-2
go.mod
+2
-2
go.mod
+2
-2
go.sum
+2
-2
go.sum
···
1
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3
-
github.com/klauspost/compress v1.18.1 h1:bcSGx7UbpBqMChDtsF28Lw6v/G94LPrrbMbdC3JH2co=
4
-
github.com/klauspost/compress v1.18.1/go.mod h1:ZQFFVG+MdnR0P+l6wpXgIL4NTtwiKIdBnrBd8Nrxr+0=
5
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
6
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
···
1
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3
+
github.com/klauspost/compress v1.18.2 h1:iiPHWW0YrcFgpBYhsA6D1+fqHssJscY/Tm/y2Uqnapk=
4
+
github.com/klauspost/compress v1.18.2/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
5
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
6
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+12
gomod2nix.toml
+12
gomod2nix.toml
···
···
1
+
schema = 3
2
+
3
+
[mod]
4
+
[mod."github.com/google/uuid"]
5
+
version = "v1.6.0"
6
+
hash = "sha256-VWl9sqUzdOuhW0KzQlv0gwwUQClYkmZwSydHG2sALYw="
7
+
[mod."github.com/klauspost/compress"]
8
+
version = "v1.18.2"
9
+
hash = "sha256-mRa+6qEi5joqQao13ZFogmq67rOQzHCVbCCjKA+HKEc="
10
+
[mod."github.com/spf13/pflag"]
11
+
version = "v1.0.10"
12
+
hash = "sha256-uDPnWjHpSrzXr17KEYEA1yAbizfcsfo5AyztY2tS6ZU="
+204
-58
main.go
+204
-58
main.go
···
2
3
import (
4
"archive/tar"
5
"bytes"
6
"crypto/sha256"
7
"fmt"
8
"io"
9
"io/fs"
···
11
"net/url"
12
"os"
13
"runtime/debug"
14
15
"github.com/google/uuid"
16
"github.com/klauspost/compress/zstd"
···
37
var challengeFlag = pflag.Bool("challenge", false, "compute DNS challenge entry from password (output zone file record)")
38
var challengeBareFlag = pflag.Bool("challenge-bare", false, "compute DNS challenge entry from password (output bare TXT value)")
39
var uploadGitFlag = pflag.String("upload-git", "", "replace site with contents of specified git repository")
40
-
var uploadDirFlag = pflag.String("upload-dir", "", "replace site with contents of specified directory")
41
-
var deleteFlag = pflag.Bool("delete", false, "delete site")
42
var debugManifestFlag = pflag.Bool("debug-manifest", false, "retrieve site manifest as ProtoJSON, for debugging")
43
var serverFlag = pflag.String("server", "", "hostname of server to connect to")
44
-
var verboseFlag = pflag.Bool("verbose", false, "display more information for debugging")
45
-
var versionFlag = pflag.Bool("version", false, "display version information")
46
47
func singleOperation() bool {
48
operations := 0
···
70
return operations == 1
71
}
72
73
-
func displayFS(root fs.FS) error {
74
return fs.WalkDir(root, ".", func(name string, entry fs.DirEntry, err error) error {
75
if err != nil {
76
return err
77
}
78
switch {
79
-
case entry.Type() == 0:
80
-
fmt.Fprintln(os.Stderr, "file", name)
81
-
case entry.Type() == fs.ModeDir:
82
-
fmt.Fprintln(os.Stderr, "dir", name)
83
case entry.Type() == fs.ModeSymlink:
84
-
fmt.Fprintln(os.Stderr, "symlink", name)
85
default:
86
-
fmt.Fprintln(os.Stderr, "other", name)
87
}
88
return nil
89
})
90
}
91
92
-
func archiveFS(writer io.Writer, root fs.FS) (err error) {
93
zstdWriter, _ := zstd.NewWriter(writer)
94
tarWriter := tar.NewWriter(zstdWriter)
95
-
err = tarWriter.AddFS(root)
96
-
if err != nil {
97
return
98
}
99
-
err = tarWriter.Close()
100
-
if err != nil {
101
return
102
}
103
-
err = zstdWriter.Close()
104
-
if err != nil {
105
return
106
}
107
return
108
}
109
110
const usageExitCode = 125
···
135
os.Exit(usageExitCode)
136
}
137
138
var err error
139
siteURL, err := url.Parse(pflag.Args()[0])
140
if err != nil {
···
143
}
144
145
var request *http.Request
146
switch {
147
case *challengeFlag || *challengeBareFlag:
148
if *passwordFlag == "" {
···
174
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
175
176
case *uploadDirFlag != "":
177
-
uploadDirFS, err := os.OpenRoot(*uploadDirFlag)
178
if err != nil {
179
fmt.Fprintf(os.Stderr, "error: invalid directory: %s\n", err)
180
os.Exit(1)
181
}
182
183
if *verboseFlag {
184
-
err := displayFS(uploadDirFS.FS())
185
if err != nil {
186
fmt.Fprintf(os.Stderr, "error: %s\n", err)
187
os.Exit(1)
188
}
189
}
190
191
-
// Stream archive data without ever loading the entire working set into RAM.
192
-
reader, writer := io.Pipe()
193
-
go func() {
194
-
err = archiveFS(writer, uploadDirFS.FS())
195
-
if err != nil {
196
-
fmt.Fprintf(os.Stderr, "error: %s\n", err)
197
-
os.Exit(1)
198
-
}
199
-
writer.Close()
200
-
}()
201
-
202
-
request, err = http.NewRequest("PUT", siteURL.String(), reader)
203
if err != nil {
204
fmt.Fprintf(os.Stderr, "error: %s\n", err)
205
os.Exit(1)
206
}
207
request.ContentLength = -1
208
request.Header.Add("Content-Type", "application/x-tar+zstd")
209
210
case *deleteFlag:
211
-
request, err = http.NewRequest("DELETE", siteURL.String(), nil)
212
-
if err != nil {
213
-
fmt.Fprintf(os.Stderr, "error: %s\n", err)
214
-
os.Exit(1)
215
}
216
217
case *debugManifestFlag:
···
226
panic("no operation chosen")
227
}
228
request.Header.Add("User-Agent", versionInfo())
229
switch {
230
case *passwordFlag != "":
231
request.Header.Add("Authorization", fmt.Sprintf("Pages %s", *passwordFlag))
···
243
request.Header.Set("Host", siteURL.Host)
244
}
245
246
-
response, err := http.DefaultClient.Do(request)
247
-
if err != nil {
248
-
fmt.Fprintf(os.Stderr, "error: %s\n", err)
249
-
os.Exit(1)
250
-
}
251
-
if *verboseFlag {
252
-
fmt.Fprintf(os.Stderr, "server: %s\n", response.Header.Get("Server"))
253
-
}
254
-
if *debugManifestFlag {
255
-
if response.StatusCode == 200 {
256
-
io.Copy(os.Stdout, response.Body)
257
-
fmt.Fprintf(os.Stdout, "\n")
258
-
} else {
259
-
io.Copy(os.Stderr, response.Body)
260
os.Exit(1)
261
}
262
-
} else { // an update operation
263
-
if response.StatusCode == 200 {
264
-
fmt.Fprintf(os.Stdout, "result: %s\n", response.Header.Get("Update-Result"))
265
-
io.Copy(os.Stdout, response.Body)
266
-
} else {
267
-
fmt.Fprintf(os.Stderr, "result: error\n")
268
-
io.Copy(os.Stderr, response.Body)
269
-
os.Exit(1)
270
}
271
}
272
}
···
2
3
import (
4
"archive/tar"
5
+
"bufio"
6
"bytes"
7
+
"crypto"
8
"crypto/sha256"
9
+
"encoding/hex"
10
+
"errors"
11
"fmt"
12
"io"
13
"io/fs"
···
15
"net/url"
16
"os"
17
"runtime/debug"
18
+
"strconv"
19
+
"strings"
20
21
"github.com/google/uuid"
22
"github.com/klauspost/compress/zstd"
···
43
var challengeFlag = pflag.Bool("challenge", false, "compute DNS challenge entry from password (output zone file record)")
44
var challengeBareFlag = pflag.Bool("challenge-bare", false, "compute DNS challenge entry from password (output bare TXT value)")
45
var uploadGitFlag = pflag.String("upload-git", "", "replace site with contents of specified git repository")
46
+
var uploadDirFlag = pflag.String("upload-dir", "", "replace whole site or a path with contents of specified directory")
47
+
var deleteFlag = pflag.Bool("delete", false, "delete whole site or a path")
48
var debugManifestFlag = pflag.Bool("debug-manifest", false, "retrieve site manifest as ProtoJSON, for debugging")
49
var serverFlag = pflag.String("server", "", "hostname of server to connect to")
50
+
var pathFlag = pflag.String("path", "", "partially update site at specified path")
51
+
var parentsFlag = pflag.Bool("parents", false, "create parent directories of --path")
52
+
var atomicFlag = pflag.Bool("atomic", false, "require partial updates to be atomic")
53
+
var incrementalFlag = pflag.Bool("incremental", false, "make --upload-dir only upload changed files")
54
+
var verboseFlag = pflag.BoolP("verbose", "v", false, "display more information for debugging")
55
+
var versionFlag = pflag.BoolP("version", "V", false, "display version information")
56
57
func singleOperation() bool {
58
operations := 0
···
80
return operations == 1
81
}
82
83
+
func gitBlobSHA256(data []byte) string {
84
+
h := crypto.SHA256.New()
85
+
h.Write([]byte("blob "))
86
+
h.Write([]byte(strconv.FormatInt(int64(len(data)), 10)))
87
+
h.Write([]byte{0})
88
+
h.Write(data)
89
+
return hex.EncodeToString(h.Sum(nil))
90
+
}
91
+
92
+
func displayFS(root fs.FS, prefix string) error {
93
return fs.WalkDir(root, ".", func(name string, entry fs.DirEntry, err error) error {
94
if err != nil {
95
return err
96
}
97
switch {
98
+
case entry.Type().IsDir():
99
+
fmt.Fprintf(os.Stderr, "dir %s%s\n", prefix, name)
100
+
case entry.Type().IsRegular():
101
+
fmt.Fprintf(os.Stderr, "file %s%s\n", prefix, name)
102
case entry.Type() == fs.ModeSymlink:
103
+
fmt.Fprintf(os.Stderr, "symlink %s%s\n", prefix, name)
104
default:
105
+
fmt.Fprintf(os.Stderr, "other %s%s\n", prefix, name)
106
}
107
return nil
108
})
109
}
110
111
+
// It doesn't make sense to use incremental updates for very small files since the cost of
112
+
// repeating a request to fill in a missing blob is likely to be higher than any savings gained.
113
+
const incrementalSizeThreshold = 256
114
+
115
+
func archiveFS(writer io.Writer, root fs.FS, prefix string, needBlobs []string) (err error) {
116
+
requestedSet := make(map[string]struct{})
117
+
for _, hash := range needBlobs {
118
+
requestedSet[hash] = struct{}{}
119
+
}
120
zstdWriter, _ := zstd.NewWriter(writer)
121
tarWriter := tar.NewWriter(zstdWriter)
122
+
if err = fs.WalkDir(root, ".", func(name string, entry fs.DirEntry, err error) error {
123
+
if err != nil {
124
+
return err
125
+
}
126
+
header := &tar.Header{}
127
+
data := []byte{}
128
+
if prefix == "" && name == "." {
129
+
return nil
130
+
} else if name == "." {
131
+
header.Name = prefix
132
+
} else {
133
+
header.Name = prefix + name
134
+
}
135
+
switch {
136
+
case entry.Type().IsDir():
137
+
header.Typeflag = tar.TypeDir
138
+
header.Name += "/"
139
+
case entry.Type().IsRegular():
140
+
header.Typeflag = tar.TypeReg
141
+
if data, err = fs.ReadFile(root, name); err != nil {
142
+
return err
143
+
}
144
+
if *incrementalFlag && len(data) > incrementalSizeThreshold {
145
+
hash := gitBlobSHA256(data)
146
+
if _, requested := requestedSet[hash]; !requested {
147
+
header.Typeflag = tar.TypeSymlink
148
+
header.Linkname = "/git/blobs/" + hash
149
+
data = nil
150
+
}
151
+
}
152
+
case entry.Type() == fs.ModeSymlink:
153
+
header.Typeflag = tar.TypeSymlink
154
+
if header.Linkname, err = fs.ReadLink(root, name); err != nil {
155
+
return err
156
+
}
157
+
default:
158
+
return errors.New("tar: cannot add non-regular file")
159
+
}
160
+
header.Size = int64(len(data))
161
+
if err = tarWriter.WriteHeader(header); err != nil {
162
+
return err
163
+
}
164
+
if _, err = tarWriter.Write(data); err != nil {
165
+
return err
166
+
}
167
+
return err
168
+
}); err != nil {
169
return
170
}
171
+
if err = tarWriter.Close(); err != nil {
172
return
173
}
174
+
if err = zstdWriter.Close(); err != nil {
175
return
176
}
177
return
178
+
}
179
+
180
+
// Stream archive data without ever loading the entire working set into RAM.
181
+
func streamArchiveFS(root fs.FS, prefix string, needBlobs []string) io.ReadCloser {
182
+
reader, writer := io.Pipe()
183
+
go func() {
184
+
err := archiveFS(writer, root, prefix, needBlobs)
185
+
if err != nil {
186
+
writer.CloseWithError(err)
187
+
} else {
188
+
writer.Close()
189
+
}
190
+
}()
191
+
return reader
192
+
}
193
+
194
+
func makeWhiteout(path string) (reader io.Reader) {
195
+
buffer := &bytes.Buffer{}
196
+
tarWriter := tar.NewWriter(buffer)
197
+
tarWriter.WriteHeader(&tar.Header{
198
+
Typeflag: tar.TypeChar,
199
+
Name: path,
200
+
})
201
+
tarWriter.Flush()
202
+
return buffer
203
}
204
205
const usageExitCode = 125
···
230
os.Exit(usageExitCode)
231
}
232
233
+
var pathPrefix string
234
+
if *pathFlag != "" {
235
+
if *uploadDirFlag == "" && !*deleteFlag {
236
+
fmt.Fprintf(os.Stderr, "--path requires --upload-dir or --delete")
237
+
os.Exit(usageExitCode)
238
+
} else {
239
+
pathPrefix = strings.Trim(*pathFlag, "/") + "/"
240
+
}
241
+
}
242
+
243
var err error
244
siteURL, err := url.Parse(pflag.Args()[0])
245
if err != nil {
···
248
}
249
250
var request *http.Request
251
+
var uploadDir *os.Root
252
switch {
253
case *challengeFlag || *challengeBareFlag:
254
if *passwordFlag == "" {
···
280
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
281
282
case *uploadDirFlag != "":
283
+
uploadDir, err = os.OpenRoot(*uploadDirFlag)
284
if err != nil {
285
fmt.Fprintf(os.Stderr, "error: invalid directory: %s\n", err)
286
os.Exit(1)
287
}
288
289
if *verboseFlag {
290
+
err := displayFS(uploadDir.FS(), pathPrefix)
291
if err != nil {
292
fmt.Fprintf(os.Stderr, "error: %s\n", err)
293
os.Exit(1)
294
}
295
}
296
297
+
if *pathFlag == "" {
298
+
request, err = http.NewRequest("PUT", siteURL.String(), nil)
299
+
} else {
300
+
request, err = http.NewRequest("PATCH", siteURL.String(), nil)
301
+
}
302
if err != nil {
303
fmt.Fprintf(os.Stderr, "error: %s\n", err)
304
os.Exit(1)
305
}
306
+
request.Body = streamArchiveFS(uploadDir.FS(), pathPrefix, []string{})
307
request.ContentLength = -1
308
request.Header.Add("Content-Type", "application/x-tar+zstd")
309
+
request.Header.Add("Accept", "application/vnd.git-pages.unresolved;q=1.0, text/plain;q=0.9")
310
+
if *parentsFlag {
311
+
request.Header.Add("Create-Parents", "yes")
312
+
} else {
313
+
request.Header.Add("Create-Parents", "no")
314
+
}
315
316
case *deleteFlag:
317
+
if *pathFlag == "" {
318
+
request, err = http.NewRequest("DELETE", siteURL.String(), nil)
319
+
if err != nil {
320
+
fmt.Fprintf(os.Stderr, "error: %s\n", err)
321
+
os.Exit(1)
322
+
}
323
+
} else {
324
+
request, err = http.NewRequest("PATCH", siteURL.String(), makeWhiteout(pathPrefix))
325
+
if err != nil {
326
+
fmt.Fprintf(os.Stderr, "error: %s\n", err)
327
+
os.Exit(1)
328
+
}
329
+
request.Header.Add("Content-Type", "application/x-tar")
330
}
331
332
case *debugManifestFlag:
···
341
panic("no operation chosen")
342
}
343
request.Header.Add("User-Agent", versionInfo())
344
+
if request.Method == "PATCH" {
345
+
if *atomicFlag {
346
+
request.Header.Add("Atomic", "yes")
347
+
request.Header.Add("Race-Free", "yes") // deprecated name, to be removed soon
348
+
} else {
349
+
request.Header.Add("Atomic", "no")
350
+
request.Header.Add("Race-Free", "no") // deprecated name, to be removed soon
351
+
}
352
+
}
353
switch {
354
case *passwordFlag != "":
355
request.Header.Add("Authorization", fmt.Sprintf("Pages %s", *passwordFlag))
···
367
request.Header.Set("Host", siteURL.Host)
368
}
369
370
+
displayServer := *verboseFlag
371
+
for {
372
+
response, err := http.DefaultClient.Do(request)
373
+
if err != nil {
374
+
fmt.Fprintf(os.Stderr, "error: %s\n", err)
375
os.Exit(1)
376
}
377
+
if displayServer {
378
+
fmt.Fprintf(os.Stderr, "server: %s\n", response.Header.Get("Server"))
379
+
displayServer = false
380
+
}
381
+
if *debugManifestFlag {
382
+
if response.StatusCode == http.StatusOK {
383
+
io.Copy(os.Stdout, response.Body)
384
+
fmt.Fprintf(os.Stdout, "\n")
385
+
} else {
386
+
io.Copy(os.Stderr, response.Body)
387
+
os.Exit(1)
388
+
}
389
+
} else { // an update operation
390
+
if *verboseFlag {
391
+
fmt.Fprintf(os.Stderr, "response: %d %s\n",
392
+
response.StatusCode, response.Header.Get("Content-Type"))
393
+
}
394
+
if response.StatusCode == http.StatusUnprocessableEntity &&
395
+
response.Header.Get("Content-Type") == "application/vnd.git-pages.unresolved" {
396
+
needBlobs := []string{}
397
+
scanner := bufio.NewScanner(response.Body)
398
+
for scanner.Scan() {
399
+
needBlobs = append(needBlobs, scanner.Text())
400
+
}
401
+
response.Body.Close()
402
+
if *verboseFlag {
403
+
fmt.Fprintf(os.Stderr, "incremental: need %d blobs\n", len(needBlobs))
404
+
}
405
+
request.Body = streamArchiveFS(uploadDir.FS(), pathPrefix, needBlobs)
406
+
continue // resubmit
407
+
} else if response.StatusCode == http.StatusOK {
408
+
fmt.Fprintf(os.Stdout, "result: %s\n", response.Header.Get("Update-Result"))
409
+
io.Copy(os.Stdout, response.Body)
410
+
} else {
411
+
fmt.Fprintf(os.Stderr, "result: error\n")
412
+
io.Copy(os.Stderr, response.Body)
413
+
os.Exit(1)
414
+
}
415
}
416
+
break
417
}
418
}