gitoxide icon indicating copy to clipboard operation
gitoxide copied to clipboard

gix-archive: create an archive of files from a named tree support

Open svetli-n opened this issue 3 years ago • 9 comments

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:

  1. Define MVP functionality
  2. Implement it and integrate it in the project, utilising external as well as internal/gitoxide crates
  3. 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-subst filter expanding git-log placeholders (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 tar archives
    • [ ] describe how to implement custom archives by leveraging the existing tar stream
    • [ ] extra options for archive backends
  • [x] integrate into gix as optional dependency with optional API.
  • [x] make available via gix archive on 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 tar stream.
  • 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-subst filtering 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$ in VERSION.txt and having VERSION.txt export-subst in .gitattributes, will result in VERSION.txt containing 40 chars commit ID. See export_subst.
  • 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=crlf in .gitattributes to force crlf. When running git ls-files --eol I get i/lf w/crlf attr/text=auto eol=crlf .gitattributes, so lf in index and crlf in work tree. Then the file in the archive has crlf.
  • 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-lfs though, as such files aren't present even though they should
  • git archive --prefix=foo and --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=foo will prepend foo and --prefix=foo/ will prepend foo/.

References

svetli-n avatar Mar 17 '23 20:03 svetli-n

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.

svetli-n avatar Mar 20 '23 20:03 svetli-n

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 gitattributes always contribute to the archive, not only when --worktree-attributes is set
  • How can attributes per path be passed? How does export-subst filtering work? Can there be custom filters as USP?
  • When checking out a worktree, git already makes transformations to the files, like eol conversions at the easiest or git-lfs substitutions for a more radical filter. What does git-archive put 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-attributes and filters is a complex topic and I am still implementing it, so a lot still to uncover and learn. gitoxide has no facilities yet to tell if a file is text or not, but would definitely need that to correctly implement gix-archive.
  • how to support zip as well as tar? Exploration option for MVP API
  • no need to explicitly support Files - std::io::Write seems to be the way
  • did you know that git archive --prefix=foo and --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.

Byron avatar Mar 21 '23 15:03 Byron

Thanks for the notes! I updated the issue and will start looking at the open questions.

svetli-n avatar Mar 21 '23 23:03 svetli-n

Please feedback and let me know if I missed something

svetli-n avatar Mar 21 '23 23:03 svetli-n

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.

Byron avatar Mar 22 '23 07:03 Byron

Thanks for the feedback! Please take a look at the update above (MVP and Open questions)

svetli-n avatar Mar 25 '23 12:03 svetli-n

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*….

Byron avatar Mar 26 '23 07:03 Byron

Alright. I added a small API PR to get started and will keep on looking at the open questions.

svetli-n avatar Mar 27 '23 21:03 svetli-n

This is actually available by now via gix archive.

Byron avatar Sep 04 '23 07:09 Byron