submodule.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # config.py - Reading and writing Git config files
  2. # Copyright (C) 2011-2013 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as published by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Working with Git submodules."""
  22. import os
  23. from collections.abc import Iterator
  24. from typing import TYPE_CHECKING, Union
  25. from .object_store import iter_tree_contents
  26. from .objects import S_ISGITLINK
  27. if TYPE_CHECKING:
  28. from .pack import ObjectContainer
  29. from .repo import Repo
  30. def iter_cached_submodules(
  31. store: "ObjectContainer", root_tree_id: bytes
  32. ) -> Iterator[tuple[bytes, bytes]]:
  33. """Iterate over cached submodules.
  34. Args:
  35. store: Object store to iterate
  36. root_tree_id: SHA of root tree
  37. Returns:
  38. Iterator over over (path, sha) tuples
  39. """
  40. for entry in iter_tree_contents(store, root_tree_id):
  41. assert entry.mode is not None
  42. if S_ISGITLINK(entry.mode):
  43. assert entry.path is not None
  44. assert entry.sha is not None
  45. yield entry.path, entry.sha
  46. def ensure_submodule_placeholder(
  47. repo: "Repo",
  48. submodule_path: Union[str, bytes],
  49. ) -> None:
  50. """Create a submodule placeholder directory with .git file.
  51. This creates the minimal structure needed for a submodule:
  52. - The submodule directory
  53. - A .git file pointing to the submodule's git directory
  54. Args:
  55. repo: Parent repository
  56. submodule_path: Path to the submodule relative to repo root
  57. """
  58. # Ensure path is bytes
  59. if isinstance(submodule_path, str):
  60. submodule_path = submodule_path.encode()
  61. # Get repo path as bytes
  62. repo_path = repo.path if isinstance(repo.path, bytes) else repo.path.encode()
  63. # Create full path to submodule
  64. full_path = os.path.join(repo_path, submodule_path)
  65. # Create submodule directory if it doesn't exist
  66. if not os.path.exists(full_path):
  67. os.makedirs(full_path)
  68. # Create .git file pointing to the submodule's git directory
  69. git_filename = b".git" if isinstance(full_path, bytes) else ".git"
  70. git_file_path = os.path.join(full_path, git_filename)
  71. if not os.path.exists(git_file_path):
  72. # Submodule git directories are typically stored in .git/modules/<name>
  73. # The relative path from the submodule to the parent's .git directory
  74. # depends on the submodule's depth
  75. depth = submodule_path.count(b"/") + 1
  76. relative_git_dir = b"../" * depth + b".git/modules/" + submodule_path
  77. with open(git_file_path, "wb") as f:
  78. f.write(b"gitdir: " + relative_git_dir + b"\n")