Skip to content

Commit

Permalink
feat: Support file:// URLs in externals
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Nov 17, 2024
1 parent 440f56c commit 4c4bfe9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ archives to be included as if they were in the source state.
externals to be included on different machines.

If a `.chezmoiexternal.$FORMAT` file is located in an ignored directory (one
listed in [`.chezmoiignore`](chezmoiignore.md)), all entries within the file are also ignored.
listed in [`.chezmoiignore`](chezmoiignore.md)), all entries within the file are
also ignored.

Entries are indexed by target name relative to the directory of the
`.chezmoiexternal.$FORMAT` file, and must have a `type` and a `url` field.
Expand Down Expand Up @@ -48,6 +49,8 @@ Entries may have the following fields:
| `pull.args` | []string | *none* | Extra args to `git pull` |
| `archive.extractAppleDouble` | bool | `false` | If `true`, AppleDouble files are extracted |

`url` must be an `https://`, `http://`, or `file://` URL.

If any of the optional `checksum.sha256`, `checksum.sha384`, or
`checksum.sha512` fields are set, chezmoi will verify that the downloaded data
has the given checksum.
Expand Down
12 changes: 12 additions & 0 deletions internal/chezmoi/sourcestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,18 @@ func (s *SourceState) getExternalDataRaw(
external *External,
options *ReadOptions,
) ([]byte, error) {
// Handle file:// URLs by always reading from disk.
switch urlStruct, err := url.Parse(external.URL); {
case err != nil:
return nil, err
case urlStruct.Scheme == "file":
data, err := s.system.ReadFile(NewAbsPath(urlStruct.Path))
if err != nil {
return nil, err
}
return data, nil
}

var now time.Time
if options != nil && options.TimeNow != nil {
now = options.TimeNow()
Expand Down
11 changes: 11 additions & 0 deletions internal/cmd/testdata/scripts/externalfileurl.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exec chezmoi apply
cmp $HOME/.file golden/.file

-- golden/.file --
# contents of .file
-- home/user/.local/share/chezmoi/.chezmoiexternal.toml --
[".file"]
type = "file"
url = "file://{{ .chezmoi.homeDir }}/.local/share/file.txt"
-- home/user/.local/share/file.txt --
# contents of .file

0 comments on commit 4c4bfe9

Please sign in to comment.