git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v7 0/7] Enable doing a shallow clone of a specific git revision

From
Toon Claes <toon@iotcl.com>
Date
Feb 6, 2025, 06:33 UTC
Message-ID
<20250206-toon-clone-refs-v7-0-4622b7392202@iotcl.com>
In-Reply-To
<20250205-toon-clone-refs-v6-0-0bbc8e6d89fd@iotcl.com>

The goal of this series is to add an option `--revision` to git-clone(1).

This series starts with a handful of preparatory refactoring commits that make it more straight-forward to add this new option. In the last commit we're actually adding the feature.

This series sets an example on how I think we can further refactor builtin/clone.c to increase the maintainability of the code.

---
Changes in v7:
- Further enhance documentation of option --revision on git-clone(1).
- Indentation fix in builtin/clone.c.
- Link to v6: https://lore.kernel.org/r/20250205-toon-clone-refs-v6-0-0bbc8e6d89fd@iotcl.com
Changes in v6:
- Rewrite the documentation for git-clone(1) --[no-]tags.
- Remove unneeded conditional around die_for_incompatible_opt2() in
  builtin/replay.c.
- Fix typo in code comment in builtin/clone.c.
- Link to v5: https://lore.kernel.org/r/20250204-toon-clone-refs-v5-0-37e34af283c8@iotcl.com
Changes in v5:
- Add separate commit to introduce die_for_incompatible_opt2()
- Small tweaks in documentation about `--[no-]tags` and `--revision`.
- Better explain the refactoring of wanted_peer_refs() in the commit
  message.
- Change type from `int` to `size_t` in wanted_peer_refs().
- Use lookup_commit_or_die() instead lookup_commit_reference() to avoid
  checking the result ourself.
- Add a few code comments to explain some things.
- Stylish cleanups like removal of unneeded empty lines, commented out
  test-code and remarks.
- Link to v4: https://lore.kernel.org/r/20250131-toon-clone-refs-v4-0-2a4ff851498f@iotcl.com
Changes in v4:
- Introduce a new commit to reduce the use of global variables.
- Introduce a new commit to invert the flag --no-tags to --tags.
- Introduce a new commit to refactor wanted_peer_refs() in
  builtin/clone.c.
- Introduce a new commit to shuffle the handling of tags refspec.
- Introduce a new commit to introduce a `struct clone_opts`.
- Link to v3: https://lore.kernel.org/r/20241219-toon-clone-refs-v3-1-1484faea3008@iotcl.com
Changes in v3:
- Fail early when the revision was not found on the remote, instead of
  creating a clone that's in an invalid state.
- State more clearly in the commit message adding this option is useful
  for a not uncommon use-case.
- Be explicit in the documentation the ref needs to peel down to a
  commit.
- Die in case we try to update_head() to an object that's not a commit.
- Allow combining `--revision` with `--bare`.
- Add die_for_incompatible_opt2() to parse-options.h and use it for the
  options that are not compatible with the new `--revision` option.
- Small tweaks to the added tests.
- Small touchups on commit messages.
- Link to v2: https://lore.kernel.org/r/20241129-toon-clone-refs-v2-1-dca4c19a3510@iotcl.com
---
Toon Claes (7):
      clone: cut down on global variables in clone.c
      clone: make it possible to specify --tags
      clone: refactor wanted_peer_refs()
      clone: add tags refspec earlier to fetch refspec
      clone: introduce struct clone_opts in builtin/clone.c
      parse-options: introduce die_for_incompatible_opt2()
      builtin/clone: teach git-clone(1) the --revision= option
 Documentation/git-clone.txt |  26 +++-
 builtin/clone.c             | 350 +++++++++++++++++++++++++-------------------
 builtin/replay.c            |   7 +-
 parse-options.h             |   9 ++
 remote.c                    |   2 +-
 remote.h                    |   5 +
 t/meson.build               |   1 +
 t/t5621-clone-revision.sh   | 123 ++++++++++++++++
 8 files changed, 358 insertions(+), 165 deletions(-)
---
Range-diff versus v6:
1:  14e4210ef9 = 1:  e9d0d1bb4e clone: cut down on global variables in clone.c
2:  f85beed07e = 2:  5c570e08f6 clone: make it possible to specify --tags
3:  92b998a173 = 3:  bb5d206ee6 clone: refactor wanted_peer_refs()
4:  3fb2766728 = 4:  344a2f143c clone: add tags refspec earlier to fetch refspec
5:  d0341cad24 = 5:  93d074d17e clone: introduce struct clone_opts in builtin/clone.c
6:  d42f291e48 = 6:  457f21943e parse-options: introduce die_for_incompatible_opt2()
7:  3f332971dc ! 7:  fb4f05547e builtin/clone: teach git-clone(1) the --revision= option
    @@ Documentation/git-clone.txt: objects from the source repository into a pack in t
     +`--revision=<rev>`::
     +	Create a new repository, and fetch the history leading to the given
     +	revision _<rev>_ (and nothing else), without making any remote-tracking
    -+	branch, and without making any local branch, and point `HEAD` to
    -+	_<rev>_. When creating a non-bare repository, the revision is checked
    -+	out on a detached `HEAD`. The argument can be a ref name
    -+	(e.g. `refs/heads/main` or `refs/tags/v1.0`) that peels down to a
    -+	commit, or a hexadecimal object name.
    ++	branch, and without making any local branch, and detach `HEAD` to
    ++	_<rev>_. The argument can be a ref name (e.g. `refs/heads/main` or
    ++	`refs/tags/v1.0`) that peels down to a commit, or a hexadecimal object
    ++	name.
     +	This option is incompatible with `--branch` and `--mirror`.
     +
      `-u` _<upload-pack>_::
    @@ builtin/clone.c: static void update_head(const struct ref *our, const struct ref
     -		struct commit *c = lookup_commit_reference(the_repository,
     -							   &our->old_oid);
     +		struct commit *c = lookup_commit_or_die(&our->old_oid,
    -+						        our->name);
    ++							our->name);
     +
      		/* --branch specifies a non-branch (i.e. tags), detach HEAD */
      		refs_update_ref(get_main_ref_store(the_repository), msg,
---

base-commit: bc204b742735ae06f65bb20291c95985c9633b7f change-id: 20241129-toon-clone-refs-ad3623772f92

Thanks -- Toon

Previous: Junio C HamanoNext: Toon Claes
Message 48 of 57 in “builtin/clone: teach git-clone(1) the --revision= option”
  1. builtin/clone: teach git-clone(1) the --revision= optionToon Claes, Nov 29, 2024
  2. Patrick SteinhardtDec 2, 2024
  3. Jeff KingDec 2, 2024
  4. Michal SuchánekDec 3, 2024
  5. Toon ClaesDec 19, 2024
  6. builtin/clone: teach git-clone(1) the --revision= optionToon Claes, Dec 19, 2024
  7. Junio C HamanoDec 19, 2024
  8. 0/6 Enable doing a shallow clone of a specific git revisionToon Claes, Jan 31, 2025
  9. 1/6 clone: cut down on global variables in clone.cToon Claes, Jan 31, 2025
  10. 2/6 clone: make it possible to specify --tagsToon Claes, Jan 31, 2025
  11. Jean-Noël AVILAFeb 1, 2025
  12. 3/6 clone: refactor wanted_peer_refs()Toon Claes, Jan 31, 2025
  13. Patrick SteinhardtFeb 3, 2025
  14. 4/6 clone: add tags refspec earlier to fetch refspecToon Claes, Jan 31, 2025
  15. Patrick SteinhardtFeb 3, 2025
  16. 5/6 clone: introduce struct clone_opts in builtin/clone.cToon Claes, Jan 31, 2025
  17. Patrick SteinhardtFeb 3, 2025
  18. 6/6 builtin/clone: teach git-clone(1) the --revision= optionToon Claes, Jan 31, 2025
  19. Junio C HamanoJan 31, 2025
  20. Jean-Noël AVILAFeb 1, 2025
  21. Patrick SteinhardtFeb 3, 2025
  22. 0/7 Enable doing a shallow clone of a specific git revisionToon Claes, Feb 4, 2025
  23. 1/7 clone: cut down on global variables in clone.cToon Claes, Feb 4, 2025
  24. 2/7 clone: make it possible to specify --tagsToon Claes, Feb 4, 2025
  25. Patrick SteinhardtFeb 5, 2025
  26. Toon ClaesFeb 5, 2025
  27. Jean-Noël AVILAFeb 5, 2025
  28. 3/7 clone: refactor wanted_peer_refs()Toon Claes, Feb 4, 2025
  29. 4/7 clone: add tags refspec earlier to fetch refspecToon Claes, Feb 4, 2025
  30. Patrick SteinhardtFeb 5, 2025
  31. 5/7 clone: introduce struct clone_opts in builtin/clone.cToon Claes, Feb 4, 2025
  32. 6/7 parse-options: introduce die_for_incompatible_opt2()Toon Claes, Feb 4, 2025
  33. Patrick SteinhardtFeb 5, 2025
  34. 7/7 builtin/clone: teach git-clone(1) the --revision= optionToon Claes, Feb 4, 2025
  35. Patrick SteinhardtFeb 5, 2025
  36. Toon ClaesFeb 5, 2025
  37. Patrick SteinhardtFeb 5, 2025
  38. Junio C HamanoFeb 5, 2025
  39. 0/7 Enable doing a shallow clone of a specific git revisionToon Claes, Feb 5, 2025
  40. 1/7 clone: cut down on global variables in clone.cToon Claes, Feb 5, 2025
  41. 2/7 clone: make it possible to specify --tagsToon Claes, Feb 5, 2025
  42. 3/7 clone: refactor wanted_peer_refs()Toon Claes, Feb 5, 2025
  43. 4/7 clone: add tags refspec earlier to fetch refspecToon Claes, Feb 5, 2025
  44. 5/7 clone: introduce struct clone_opts in builtin/clone.cToon Claes, Feb 5, 2025
  45. 6/7 parse-options: introduce die_for_incompatible_opt2()Toon Claes, Feb 5, 2025
  46. 7/7 builtin/clone: teach git-clone(1) the --revision= optionToon Claes, Feb 5, 2025
  47. Junio C HamanoFeb 5, 2025
  48. 0/7 Enable doing a shallow clone of a specific git revisionToon Claes, Feb 6, 2025
  49. 1/7 clone: cut down on global variables in clone.cToon Claes, Feb 6, 2025
  50. 2/7 clone: make it possible to specify --tagsToon Claes, Feb 6, 2025
  51. 3/7 clone: refactor wanted_peer_refs()Toon Claes, Feb 6, 2025
  52. 4/7 clone: add tags refspec earlier to fetch refspecToon Claes, Feb 6, 2025
  53. 5/7 clone: introduce struct clone_opts in builtin/clone.cToon Claes, Feb 6, 2025
  54. 6/7 parse-options: introduce die_for_incompatible_opt2()Toon Claes, Feb 6, 2025
  55. 7/7 builtin/clone: teach git-clone(1) the --revision= optionToon Claes, Feb 6, 2025
  56. Patrick SteinhardtFeb 6, 2025
  57. Junio C HamanoFeb 6, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.