gix-archive: create an archive of files from a named tree support
Summary 💡
The goal of this issue is to track the tasks, requirements and analysis that will lead to adding functionality for archiving of files from a named tree to gitoxide.
Background
git has archive command, allowing a user to to add a list of files to an archive. While the files normally originate from a named tree, identified by commit or tag ID/SHA1 hash, users can also add additional files by providing paths to these and/or their content. The files can be added to various archive types, such as tar, zip, gzip/tgz as well as to any custom archive type by utilising external tools/commands. The user can change file attributes prior to adding them to an archive, by looking for attributes in .gitattributes files, or explicitly setting modification time. Users can also select only certain parts of a named tree to be archived. Archives are written to stdout or to a file. The tree to be archived can be both local or from a remote repository.
Method
Having the rich archive functionality described above, the plan to add support for it in gitoxide is as follows:
- Define MVP functionality
- Implement it and integrate it in the project, utilising external as well as internal/gitoxide crates
- Iterate on potentially adding more features
Scope
- MVP for API #796
- gitattributes and archive specific filters
- possibility to query by path .gitattributes for export-subst and export-ignore (to be implemented in another crate)
- act on .gitattributes in the tree to be archived
- optionally act on .gitattributes in the work tree
export-substfilter expandinggit-logplaceholders (the expansion is to be handled by another to be created crate, e.g.gix log)
- filter files of tree - control what goes into the archive
- add extra files
- [ ] as read file paths
- [ ] By providing file paths and content
- archive formats
- [x] zip
- [x] tar
- [x] convenient compression of
tararchives - [ ] describe how to implement custom archives by leveraging the existing
tarstream - [ ] extra options for archive backends
- [x] integrate into
gixas optional dependency with optional API. - [x] make available via
gix archiveon the CLI
Q&A
- How to support zip as well as tar?
- These are built-in and additional formats can be implemented by re-encoding the
tarstream.
- These are built-in and additional formats can be implemented by re-encoding the
- Do gitattributes always contribute to the archive, so not only when --worktree-attributes is set?
- Yes, gitattributes alway contribute to the archive as attributes are by default taken from the .gitattributes files in the tree that is being archived. Setting --worktree-attributes option makes git look at .gitattributes files in the work tree as well. Here is a use case for the option: "If you want to tweak the way the output is generated after the fact (e.g. you committed without adding an appropriate export-ignore in its .gitattributes), adjust the checked out .gitattributes file as necessary and use --worktree-attributes option".
- How can attributes per path be passed?
- They are read exclusively from the git-tree and can't be passed by the API.
- How does
export-substfiltering work?- If the attribute export-subst is set for a file then Git will expand several placeholders when adding this file to an archive. For example, when git archive is called with commit ID, having
$Format:%H$inVERSION.txtand havingVERSION.txt export-substin .gitattributes, will result in VERSION.txt containing 40 chars commit ID. See export_subst.
- If the attribute export-subst is set for a file then Git will expand several placeholders when adding this file to an archive. For example, when git archive is called with commit ID, having
- Can there be custom filters as USP?
- No API support is planned, so the git builtin filters have to be used.
- Does it archive filtered or unfiltered files (i.e. worktree files or git files)?
- Filtered. I write
* text=auto eol=crlfin .gitattributes to forcecrlf. When runninggit ls-files --eolI geti/lf w/crlf attr/text=auto eol=crlf .gitattributes, solfin index andcrlfin work tree. Then the file in the archive hascrlf.
- Filtered. I write
- Does it only writes text files?
- It writes binary as well. I put jpeg in the repo and its in the archive. There seems to be something going on with
git-lfsthough, as such files aren't present even though they should
- It writes binary as well. I put jpeg in the repo and its in the archive. There seems to be something going on with
git archive --prefix=fooand--prefix=foo/is not the same at all? Feature or bug?- Feature IMO, docs says:
--prefix=<prefix>/Prepend<prefix>/to paths in the archive. So-prefix=foowill prependfooand--prefix=foo/will prependfoo/.
- Feature IMO, docs says:
References
Baseline functionality, from the checklist above, seems like a reasonable MVP to me.
That is, given a commit id and an output file name, create tar archive, prefixing all file paths with a certain prefix, so that when untared all is in a separate folder.
Session Notes
Notes from our call.
- can you make it more readable. Optimize the information to aid implementation and its rough order
- no need to add incredible detail or even documentation here
- I think
gitattributesalways contribute to the archive, not only when--worktree-attributesis set - How can attributes per path be passed? How does
export-substfiltering work? Can there be custom filters as USP? - When checking out a worktree, git already makes transformations to the files, like
eolconversions at the easiest orgit-lfssubstitutions for a more radical filter. What doesgit-archiveput in the archive? Filtered or unfiltered files? - did you know it only writes text files? Could supporting writing more files into the archive (like binary files, or truly the entire tree as it would be checked out) be another USP?
- as a word of warning:
git-attributesand filters is a complex topic and I am still implementing it, so a lot still to uncover and learn.gitoxidehas no facilities yet to tell if a file is text or not, but would definitely need that to correctly implementgix-archive. - how to support
zipas well astar? Exploration option for MVP API - no need to explicitly support
Files-std::io::Writeseems to be the way - did you know that
git archive --prefix=fooand--prefix=foo/is not the same at all? Feature or bug?
Regarding the MVP scope, here is my thoughts:
That is, given a commit id and an output file name, create tar archive, prefixing all file paths with a certain prefix, so that when untared all is in a separate folder.
I recommend: an object ID as input, and optionally one or more subtrees to select which part of the tree goes into the archive, a std::io::Write as output of formatted data. Let's be sure other forms of input like filters to apply and obtaining attributes per path can be implemented, even though it most definitely doesn't have to happen as part of the MVP.
Thanks for the notes! I updated the issue and will start looking at the open questions.
Please feedback and let me know if I missed something
I'd rewrite the MVP portion so it's more structured than a paragraph of text. What are the parts that matter? To me it's mostly a visual thing to make it pop. I did some formatting to do exactly that to other parts and added a TBD portion about git attributes, a vital part of that's needed to do this correctly - most of the open questions are about gitattributes.
I would out-scope everything of dubious value, like archiving of remote repositories. It's like a special case of git clone --depth 1 <remote> && git archive. It's even using a custom remote archive upload program akin to upload-pack to make that happen, interestingly a feature added after shallow clones were invented.
Thanks for the feedback! Please take a look at the update above (MVP and Open questions)
Thanks for the update! I am bringing up a few more questions, but even without them answered an MVP API is probably feasible (and any open questions will come up there again for sure which might make them more tractable, too).
Here a few more comments.
Q: How can attributes per path be passed? A: By having multiple lines with paths in .gitattributes.
This was a question for the API exploration, probably specifically to handling the --worktree-attributes flag. It's still a question if this refers to only a single worktree .gitattributes file or all of them, hierarchically.
Q: How does export-subst filtering work? A:
The question was about the filtering machinery in general, which ties into the question whether or not filters are applied. What about CRLF transformations for example? I'd be surprised if there was a separate mechanism for each of them, and knowing how this is done will affect the implementation. I am saying this knowing that gitattributes + filters are a big topic which is probably best left out of here and delegated. However, it would be worth validating this assumption which hinges on thinking that it will apply the standard filters that happen when checking out files - what's going to the archive should be what would be checked out. But is that so? Looking at the answer to the next question, the answer is yes, the standard filtering applies.
Q: Does it only writes text files? A: It writes binary as well. I put jpeg in the repo and its in the archive.
That's interesting, as determining what goes in might be a little more complex then (as in my tests, files that were in git-lfs would not appear in the archive). Maybe there is a difference between having git-lfs files or not? Maybe it doesn't want to call external filter programs to obtain the contents of a file? If that was the case, it's definitely using a customized version of the filtering engine that is used for checkouts. Relating to my previous conclusion, yes, the standard filtering applies, the answer is probably more like yes, but*….
Alright. I added a small API PR to get started and will keep on looking at the open questions.
This is actually available by now via gix archive.