Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support file:// URLs in externals #4092

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading