+72
-22
.forgejo/workflows/ci.yaml
+72
-22
.forgejo/workflows/ci.yaml
···
7
7
8
8
jobs:
9
9
check:
10
-
runs-on: codeberg-small-lazy
10
+
runs-on: debian-trixie
11
11
container:
12
-
image: docker.io/library/node:24-trixie-slim@sha256:ef4ca6d078dd18322059a1f051225f7bbfc2bb60c16cbb5d8a1ba2cc8964fe8a
12
+
image: docker.io/library/node:24-trixie-slim@sha256:b05474903f463ce4064c09986525e6588c3e66c51b69be9c93a39fb359f883ce
13
13
steps:
14
14
- name: Check out source code
15
-
uses: https://code.forgejo.org/actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
15
+
uses: https://code.forgejo.org/actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
16
16
- name: Set up toolchain
17
-
uses: https://code.forgejo.org/actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6
17
+
uses: https://code.forgejo.org/actions/setup-go@4dc6199c7b1a012772edbd06daecab0f50c9053c # v6.1.0
18
18
with:
19
19
go-version: '>=1.25.0'
20
20
- name: Install dependencies
···
24
24
go install honnef.co/go/tools/cmd/staticcheck@latest
25
25
- name: Build application
26
26
run: |
27
-
go build .
27
+
go build
28
28
- name: Run static analysis
29
29
run: |
30
-
go vet .
31
-
staticcheck .
30
+
go vet
31
+
staticcheck
32
+
33
+
release:
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:
46
+
go-version: '>=1.25.0'
47
+
- name: Install dependencies
48
+
run: |
49
+
apt-get -y update
50
+
apt-get -y install ca-certificates
51
+
- name: Build release assets
52
+
# If you want more platforms to be represented, send a pull request.
53
+
run: |
54
+
set -x
55
+
build() { GOOS=$1 GOARCH=$2 go build -o assets/git-pages-cli.$1-$2$3; }
56
+
build windows amd64 .exe
57
+
build windows arm64 .exe
58
+
build linux amd64
59
+
build linux arm64
60
+
build darwin arm64
61
+
- name: Create release
62
+
uses: https://code.forgejo.org/actions/forgejo-release@fc0488c944626f9265d87fbc4dd6c08f78014c63 # v2.7.3
63
+
with:
64
+
tag: ${{ startsWith(forge.event.ref, 'refs/tags/v') && forge.ref_name || 'latest' }}
65
+
release-dir: assets
66
+
direction: upload
67
+
override: true
68
+
prerelease: ${{ !startsWith(forge.event.ref, 'refs/tags/v') }}
32
69
33
70
package:
34
-
if: ${{ forge.ref == 'refs/heads/main' }}
71
+
if: ${{ forge.ref == 'refs/heads/main' || startsWith(forge.event.ref, 'refs/tags/v') }}
35
72
needs: [check]
36
-
runs-on: codeberg-small-lazy
73
+
runs-on: debian-trixie
37
74
container:
38
-
image: docker.io/library/node:24-trixie-slim@sha256:45babd1b4ce0349fb12c4e24bf017b90b96d52806db32e001e3013f341bef0fe
75
+
image: docker.io/library/node:24-trixie-slim@sha256:b05474903f463ce4064c09986525e6588c3e66c51b69be9c93a39fb359f883ce
39
76
steps:
40
77
- name: Install dependencies
41
78
run: |
42
79
apt-get -y update
43
-
apt-get -y install buildah ca-certificates
80
+
apt-get -y install ca-certificates buildah qemu-user-binfmt
44
81
- name: Check out source code
45
-
uses: https://code.forgejo.org/actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
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
46
91
- if: ${{ forge.repository == 'git-pages/git-pages-cli' }}
47
-
name: Log into container registry
92
+
name: Push container to Codeberg
48
93
run: |
49
94
buildah login --authfile=/tmp/authfile.json \
50
-
-u git-pages-bot -p ${{ secrets.PACKAGES_TOKEN }} codeberg.org
51
-
- name: Build container
52
-
run: |
53
-
printf '[storage]\ndriver="vfs"\nrunroot="/run/containers/storage"\ngraphroot="/var/lib/containers/storage"\n' | tee /etc/containers/storage.conf
54
-
buildah build --arch=amd64 --tag="codeberg.org/git-pages/git-pages-cli:latest-amd64" --isolation=chroot .
55
-
buildah build --arch=arm64 --tag="codeberg.org/git-pages/git-pages-cli:latest-arm64" --isolation=chroot .
56
-
buildah manifest create codeberg.org/git-pages/git-pages-cli:latest codeberg.org/git-pages/git-pages-cli:latest-amd64 codeberg.org/git-pages/git-pages-cli:latest-arm64
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' }}
57
101
- if: ${{ forge.repository == 'git-pages/git-pages-cli' }}
58
-
name: Push container
102
+
name: Push container to code.forgejo.org
59
103
run: |
60
-
env BUILDAH_ISOLATION=chroot REGISTRY_AUTH_FILE=/tmp/authfile.json buildah manifest push --all codeberg.org/git-pages/git-pages-cli:latest "docker://codeberg.org/git-pages/git-pages-cli:latest"
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
-2
Dockerfile
+1
-2
Dockerfile
···
1
-
FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.25-alpine@sha256:aee43c3ccbf24fdffb7295693b6e33b21e01baec1b2a55acc351fde345e9ec34 AS builder
1
+
FROM --platform=$BUILDPLATFORM docker.io/library/golang:1.25-alpine@sha256:ac09a5f469f307e5da71e766b0bd59c9c49ea460a528cc3e6686513d64a6f1fb AS builder
2
2
ARG TARGETOS TARGETARCH
3
3
RUN apk --no-cache add ca-certificates git
4
4
WORKDIR /build
···
10
10
FROM scratch
11
11
COPY --from=builder /etc/ssl/cert.pem /etc/ssl/cert.pem
12
12
COPY --from=builder /build/git-pages-cli /bin/git-pages-cli
13
-
14
13
ENTRYPOINT ["git-pages-cli"]
-14
LICENSE-0BSD.txt
-14
LICENSE-0BSD.txt
···
1
-
Copyright (C) git-pages contributors
2
-
Copyright (C) Catherine 'whitequark'
3
-
4
-
Permission to use, copy, modify, and/or distribute this software for
5
-
any purpose with or without fee is hereby granted.
6
-
7
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
12
-
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
13
-
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
-
+14
LICENSE.txt
+14
LICENSE.txt
···
1
+
Copyright (C) git-pages contributors
2
+
Copyright (C) Catherine 'whitequark'
3
+
4
+
Permission to use, copy, modify, and/or distribute this software for
5
+
any purpose with or without fee is hereby granted.
6
+
7
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
12
+
AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
13
+
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
+
+46
-16
README.md
+46
-16
README.md
···
3
3
4
4
_git-pages-cli_ is a command-line application for publishing sites to [git-pages].
5
5
6
-
If you want to publish a site from a Forgejo Actions workflow, use [git-pages/action] instead.
6
+
> [!TIP]
7
+
> If you want to publish a site from a CI workflow, use the [Forgejo Action][git-pages-action] instead.
7
8
8
9
[git-pages]: https://codeberg.org/git-pages/git-pages
9
-
[git-pages/action]: https://codeberg.org/git-pages/action
10
+
[git-pages-action]: https://codeberg.org/git-pages/action
10
11
11
12
12
13
Installation
13
14
------------
14
15
15
-
You will need [Go](https://go.dev/) 1.25 or newer. Run:
16
+
You can install _git-pages-cli_ using one of the following methods:
16
17
17
-
```console
18
-
$ go install codeberg.org/git-pages/git-pages-cli
19
-
```
18
+
1. **Downloading a binary**. You can download the [latest build][latest] or pick a [release][releases].
19
+
20
+
1. **Using a Docker container**. Choose between the latest build or a [release tag][containers]. Then run:
21
+
22
+
```console
23
+
$ docker run --rm codeberg.org/git-pages/git-pages-cli:latest ...
24
+
```
20
25
21
-
If you prefer, you may also use a [Docker container][docker]:
26
+
1. **Installing from source**. First, install [Go](https://go.dev/) 1.25 or newer. Then run:
22
27
23
-
```console
24
-
docker run --rm codeberg.org/git-pages/git-pages-cli:latest ...
25
-
```
28
+
```console
29
+
$ go install codeberg.org/git-pages/git-pages-cli@latest
30
+
```
26
31
27
-
[docker]: https://codeberg.org/git-pages/-/packages/container/git-pages-cli/latest
32
+
[latest]: https://codeberg.org/git-pages/git-pages-cli/releases/tag/latest
33
+
[releases]: https://codeberg.org/git-pages/git-pages-cli/releases
34
+
[containers]: https://codeberg.org/git-pages/-/packages/container/git-pages-cli/versions
28
35
29
36
30
37
Usage
···
33
40
To prepare a DNS challenge for a given site and password:
34
41
35
42
```console
43
+
$ git-pages-cli https://example.org --challenge # generate a random password
44
+
password: 28a616f4-2fbe-456b-8456-056d1f38e815
45
+
_git-pages-challenge.example.org. 3600 IN TXT "a59ecb58f7256fc5afb6b96892501007b0b65d64f251b1aca749b0fca61d582c"
36
46
$ git-pages-cli https://example.org --password xyz --challenge
37
-
_git-pages-challenge.example.org. 3600 IN TXT "317716dee4379c167e8b5ce9df38eb880e043e5a842d160fe8d5bb408ee0c191"
47
+
_git-pages-challenge.example.org. 3600 IN TXT "6c47172c027b3c79358f9f8c110886baf4826d9bc2a1c7d0f439cc770ed42dc8"
38
48
$ git-pages-cli https://example.org --password xyz --challenge-bare
39
-
317716dee4379c167e8b5ce9df38eb880e043e5a842d160fe8d5bb408ee0c191
49
+
6c47172c027b3c79358f9f8c110886baf4826d9bc2a1c7d0f439cc770ed42dc8
40
50
```
41
51
42
52
To publish a site from a git repository available on the internet (`--password` may be omitted if the repository is allowlisted via DNS):
···
64
74
$ git-pages-cli https://example.org --server grebedoc.dev --password xyz --upload-dir ...
65
75
```
66
76
77
+
### Forge authorization
78
+
79
+
Uploading a directory to a site on a wildcard domain (e.g. `https://<owner>.grebedoc.dev/<repo>`) requires the use of an access token with push permissions for the corresponding repository (`https://codeberg.org/<owner>/<repo>.git` in this case).
80
+
81
+
To create such an access token on Forgejo:
82
+
1. Open _Settings_ > _Applications_ > _Access tokens_.
83
+
1. Expand _Select permissions_, pick _Read and write_ under _repository_.
84
+
1. Set _Token name_ to something informative (e.g. "git-pages publishing").
85
+
1. Click _Generate token_.
86
+
1. The token will appear in a notification (a long string of hexadecimal numbers all on its own).
87
+
88
+
To deploy using an access token:
89
+
90
+
```console
91
+
$ git-pages-cli https://username.grebedoc.dev --token <token> --upload-dir ...
92
+
```
93
+
94
+
**Keep the access token safe and secure!** Anyone who has it will be able to change the data in any of your repositories.
95
+
67
96
68
97
Advanced usage
69
98
--------------
70
99
71
-
To retrieve the site manifest (for debugging only: manifest schema is **not versioned** and **subject to change**):
100
+
To retrieve the site manifest (for debugging only: manifest schema is not versioned and **subject to change without notice**, including renaming of existing fields):
72
101
73
102
```console
74
103
$ git-pages-cli https://example.org --password xyz --debug-manifest
···
84
113
"contentType": "text/html; charset=utf-8"
85
114
}
86
115
},
87
-
"totalSize": "5",
116
+
"originalSize": "5",
117
+
"compressedSize": "5",
88
118
"storedSize": "0",
89
119
"redirects": [],
90
120
"headers": [],
···
96
126
License
97
127
-------
98
128
99
-
[0-clause BSD](LICENSE-0BSD.txt)
129
+
[0-clause BSD](LICENSE.txt)
+24
flake.lock
+24
flake.lock
···
18
18
"type": "github"
19
19
}
20
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
+
},
21
44
"nix-filter": {
22
45
"locked": {
23
46
"lastModified": 1757882181,
···
52
75
"root": {
53
76
"inputs": {
54
77
"flake-utils": "flake-utils",
78
+
"gomod2nix": "gomod2nix",
55
79
"nix-filter": "nix-filter",
56
80
"nixpkgs": "nixpkgs"
57
81
}
+22
-4
flake.nix
+22
-4
flake.nix
···
3
3
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
4
4
flake-utils.url = "github:numtide/flake-utils";
5
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
+
};
6
12
};
7
13
8
14
outputs =
···
11
17
nixpkgs,
12
18
flake-utils,
13
19
nix-filter,
14
-
}:
20
+
...
21
+
}@inputs:
15
22
flake-utils.lib.eachDefaultSystem (
16
23
system:
17
24
let
18
-
pkgs = nixpkgs.legacyPackages.${system};
25
+
pkgs = import nixpkgs {
26
+
inherit system;
19
27
20
-
git-pages-cli = pkgs.buildGo125Module {
28
+
overlays = [
29
+
inputs.gomod2nix.overlays.default
30
+
];
31
+
};
32
+
33
+
git-pages-cli = pkgs.buildGoApplication {
21
34
pname = "git-pages-cli";
22
35
version = "0";
23
36
···
41
54
"-s -w"
42
55
];
43
56
44
-
vendorHash = "sha256-5vjUhN3lCr41q91lOD7v0F9c6a8GJj7wBGnnzgFBhJU=";
57
+
go = pkgs.go_1_25;
58
+
modules = ./gomod2nix.toml;
45
59
};
46
60
in
47
61
{
···
50
64
devShells.default = pkgs.mkShell {
51
65
inputsFrom = [
52
66
git-pages-cli
67
+
];
68
+
69
+
packages = with pkgs; [
70
+
gomod2nix
53
71
];
54
72
};
55
73
+2
-2
go.mod
+2
-2
go.mod
+2
-2
go.sum
+2
-2
go.sum
···
1
1
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2
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=
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
5
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
6
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="
+256
-63
main.go
+256
-63
main.go
···
2
2
3
3
import (
4
4
"archive/tar"
5
+
"bufio"
5
6
"bytes"
7
+
"crypto"
6
8
"crypto/sha256"
9
+
"encoding/hex"
10
+
"errors"
7
11
"fmt"
8
12
"io"
9
13
"io/fs"
10
14
"net/http"
11
15
"net/url"
12
16
"os"
17
+
"runtime/debug"
18
+
"strconv"
19
+
"strings"
13
20
14
21
"github.com/google/uuid"
15
22
"github.com/klauspost/compress/zstd"
16
23
"github.com/spf13/pflag"
17
24
)
18
25
26
+
// By default the version information is retrieved from VCS. If not available during build,
27
+
// override this variable using linker flags to change the displayed version.
28
+
// Example: `-ldflags "-X main.versionOverride=v1.2.3"`
29
+
var versionOverride = ""
30
+
31
+
func versionInfo() string {
32
+
version := "(unknown)"
33
+
if versionOverride != "" {
34
+
version = versionOverride
35
+
} else if buildInfo, ok := debug.ReadBuildInfo(); ok {
36
+
version = buildInfo.Main.Version
37
+
}
38
+
return fmt.Sprintf("git-pages-cli %s", version)
39
+
}
40
+
19
41
var passwordFlag = pflag.String("password", "", "password for DNS challenge authorization")
42
+
var tokenFlag = pflag.String("token", "", "token for forge authorization")
20
43
var challengeFlag = pflag.Bool("challenge", false, "compute DNS challenge entry from password (output zone file record)")
21
44
var challengeBareFlag = pflag.Bool("challenge-bare", false, "compute DNS challenge entry from password (output bare TXT value)")
22
45
var uploadGitFlag = pflag.String("upload-git", "", "replace site with contents of specified git repository")
23
-
var uploadDirFlag = pflag.String("upload-dir", "", "replace site with contents of specified directory")
24
-
var deleteFlag = pflag.Bool("delete", false, "delete site")
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")
25
48
var debugManifestFlag = pflag.Bool("debug-manifest", false, "retrieve site manifest as ProtoJSON, for debugging")
26
49
var serverFlag = pflag.String("server", "", "hostname of server to connect to")
27
-
var verboseFlag = pflag.Bool("verbose", false, "display more information for debugging")
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")
28
56
29
57
func singleOperation() bool {
30
58
operations := 0
···
46
74
if *debugManifestFlag {
47
75
operations++
48
76
}
77
+
if *versionFlag {
78
+
operations++
79
+
}
49
80
return operations == 1
50
81
}
51
82
52
-
func displayFS(root fs.FS) error {
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 {
53
93
return fs.WalkDir(root, ".", func(name string, entry fs.DirEntry, err error) error {
54
94
if err != nil {
55
95
return err
56
96
}
57
97
switch {
58
-
case entry.Type() == 0:
59
-
fmt.Fprintln(os.Stderr, "file", name)
60
-
case entry.Type() == fs.ModeDir:
61
-
fmt.Fprintln(os.Stderr, "dir", name)
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)
62
102
case entry.Type() == fs.ModeSymlink:
63
-
fmt.Fprintln(os.Stderr, "symlink", name)
103
+
fmt.Fprintf(os.Stderr, "symlink %s%s\n", prefix, name)
64
104
default:
65
-
fmt.Fprintln(os.Stderr, "other", name)
105
+
fmt.Fprintf(os.Stderr, "other %s%s\n", prefix, name)
66
106
}
67
107
return nil
68
108
})
69
109
}
70
110
71
-
func archiveFS(root fs.FS) (result []byte, err error) {
72
-
buffer := bytes.Buffer{}
73
-
zstdWriter, _ := zstd.NewWriter(&buffer)
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)
74
121
tarWriter := tar.NewWriter(zstdWriter)
75
-
err = tarWriter.AddFS(root)
76
-
if err != nil {
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 {
77
169
return
78
170
}
79
-
err = tarWriter.Close()
80
-
if err != nil {
171
+
if err = tarWriter.Close(); err != nil {
81
172
return
82
173
}
83
-
err = zstdWriter.Close()
84
-
if err != nil {
174
+
if err = zstdWriter.Close(); err != nil {
85
175
return
86
176
}
87
-
result = buffer.Bytes()
88
177
return
89
178
}
90
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
206
+
207
+
func usage() {
208
+
fmt.Fprintf(os.Stderr,
209
+
"Usage: %s <site-url> {--challenge|--upload-git url|--upload-dir path|--delete} [options...]\n",
210
+
os.Args[0],
211
+
)
212
+
pflag.PrintDefaults()
213
+
}
214
+
91
215
func main() {
216
+
pflag.Usage = usage
92
217
pflag.Parse()
93
-
if !singleOperation() || len(pflag.Args()) != 1 {
94
-
fmt.Fprintf(os.Stderr,
95
-
"Usage: %s <site-url> [--challenge|--upload-git url|--upload-dir path|--delete]\n",
96
-
os.Args[0],
97
-
)
98
-
os.Exit(125)
218
+
if !singleOperation() || (!*versionFlag && len(pflag.Args()) != 1) {
219
+
pflag.Usage()
220
+
os.Exit(usageExitCode)
221
+
}
222
+
223
+
if *versionFlag {
224
+
fmt.Fprintln(os.Stdout, versionInfo())
225
+
os.Exit(0)
226
+
}
227
+
228
+
if *passwordFlag != "" && *tokenFlag != "" {
229
+
fmt.Fprintf(os.Stderr, "--password and --token are mutually exclusive")
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
+
}
99
241
}
100
242
101
243
var err error
···
106
248
}
107
249
108
250
var request *http.Request
251
+
var uploadDir *os.Root
109
252
switch {
110
253
case *challengeFlag || *challengeBareFlag:
111
254
if *passwordFlag == "" {
···
137
280
request.Header.Add("Content-Type", "application/x-www-form-urlencoded")
138
281
139
282
case *uploadDirFlag != "":
140
-
uploadDirFS, err := os.OpenRoot(*uploadDirFlag)
283
+
uploadDir, err = os.OpenRoot(*uploadDirFlag)
141
284
if err != nil {
142
285
fmt.Fprintf(os.Stderr, "error: invalid directory: %s\n", err)
143
286
os.Exit(1)
144
287
}
145
288
146
289
if *verboseFlag {
147
-
err := displayFS(uploadDirFS.FS())
290
+
err := displayFS(uploadDir.FS(), pathPrefix)
148
291
if err != nil {
149
292
fmt.Fprintf(os.Stderr, "error: %s\n", err)
150
293
os.Exit(1)
151
294
}
152
295
}
153
296
154
-
requestBody, err := archiveFS(uploadDirFS.FS())
155
-
if err != nil {
156
-
fmt.Fprintf(os.Stderr, "error: %s\n", err)
157
-
os.Exit(1)
297
+
if *pathFlag == "" {
298
+
request, err = http.NewRequest("PUT", siteURL.String(), nil)
299
+
} else {
300
+
request, err = http.NewRequest("PATCH", siteURL.String(), nil)
158
301
}
159
-
160
-
request, err = http.NewRequest("PUT", siteURL.String(), bytes.NewReader(requestBody))
161
302
if err != nil {
162
303
fmt.Fprintf(os.Stderr, "error: %s\n", err)
163
304
os.Exit(1)
164
305
}
306
+
request.Body = streamArchiveFS(uploadDir.FS(), pathPrefix, []string{})
307
+
request.ContentLength = -1
165
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
+
}
166
315
167
316
case *deleteFlag:
168
-
request, err = http.NewRequest("DELETE", siteURL.String(), bytes.NewReader([]byte{}))
169
-
if err != nil {
170
-
fmt.Fprintf(os.Stderr, "error: %s\n", err)
171
-
os.Exit(1)
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")
172
330
}
173
331
174
332
case *debugManifestFlag:
175
333
manifestURL := siteURL.ResolveReference(&url.URL{Path: ".git-pages/manifest.json"})
176
-
request, err = http.NewRequest("GET", manifestURL.String(), bytes.NewReader([]byte{}))
334
+
request, err = http.NewRequest("GET", manifestURL.String(), nil)
177
335
if err != nil {
178
336
fmt.Fprintf(os.Stderr, "error: %s\n", err)
179
337
os.Exit(1)
···
182
340
default:
183
341
panic("no operation chosen")
184
342
}
185
-
if *passwordFlag != "" {
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 != "":
186
355
request.Header.Add("Authorization", fmt.Sprintf("Pages %s", *passwordFlag))
356
+
case *tokenFlag != "":
357
+
request.Header.Add("Forge-Authorization", fmt.Sprintf("token %s", *tokenFlag))
187
358
}
188
359
if *serverFlag != "" {
189
360
// Send the request to `--server` host, but set the `Host:` header to the site host.
190
361
// This allows first-time publishing to proceed without the git-pages server yet having
191
362
// a TLS certificate for the site host (which has a circular dependency on completion of
192
363
// first-time publishing).
193
-
newURL := *siteURL
364
+
newURL := *request.URL
194
365
newURL.Host = *serverFlag
195
366
request.URL = &newURL
196
367
request.Header.Set("Host", siteURL.Host)
197
368
}
198
369
199
-
response, err := http.DefaultClient.Do(request)
200
-
if err != nil {
201
-
fmt.Fprintf(os.Stderr, "error: %s\n", err)
202
-
os.Exit(1)
203
-
}
204
-
if *verboseFlag {
205
-
fmt.Fprintf(os.Stderr, "server: %s\n", response.Header.Get("Server"))
206
-
}
207
-
if *debugManifestFlag {
208
-
if response.StatusCode == 200 {
209
-
io.Copy(os.Stdout, response.Body)
210
-
fmt.Fprintf(os.Stdout, "\n")
211
-
} else {
212
-
io.Copy(os.Stderr, response.Body)
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)
213
375
os.Exit(1)
214
376
}
215
-
} else { // an update operation
216
-
if response.StatusCode == 200 {
217
-
fmt.Fprintf(os.Stdout, "result: %s\n", response.Header.Get("Update-Result"))
218
-
io.Copy(os.Stdout, response.Body)
219
-
} else {
220
-
fmt.Fprintf(os.Stderr, "result: error\n")
221
-
io.Copy(os.Stderr, response.Body)
222
-
os.Exit(1)
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
+
}
223
415
}
416
+
break
224
417
}
225
418
}