git 4.4.6 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (275) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -0
  3. data/CHANGELOG.md +348 -39
  4. data/CONTRIBUTING.md +1014 -144
  5. data/README.md +386 -504
  6. data/UPGRADING.md +1822 -0
  7. data/git.gemspec +102 -22
  8. data/lib/git/author_info.rb +65 -0
  9. data/lib/git/branch_delete_failure.rb +34 -0
  10. data/lib/git/branch_delete_result.rb +66 -0
  11. data/lib/git/branch_info.rb +237 -0
  12. data/lib/git/command_line/base.rb +247 -0
  13. data/lib/git/command_line/capturing.rb +308 -0
  14. data/lib/git/command_line/result.rb +88 -0
  15. data/lib/git/command_line/streaming.rb +236 -0
  16. data/lib/git/command_line.rb +45 -323
  17. data/lib/git/commands/add.rb +139 -0
  18. data/lib/git/commands/am/abort.rb +43 -0
  19. data/lib/git/commands/am/apply.rb +263 -0
  20. data/lib/git/commands/am/continue.rb +43 -0
  21. data/lib/git/commands/am/quit.rb +43 -0
  22. data/lib/git/commands/am/retry.rb +49 -0
  23. data/lib/git/commands/am/show_current_patch.rb +64 -0
  24. data/lib/git/commands/am/skip.rb +42 -0
  25. data/lib/git/commands/am.rb +33 -0
  26. data/lib/git/commands/apply.rb +242 -0
  27. data/lib/git/commands/archive/list_formats.rb +46 -0
  28. data/lib/git/commands/archive.rb +145 -0
  29. data/lib/git/commands/arguments.rb +4521 -0
  30. data/lib/git/commands/base.rb +522 -0
  31. data/lib/git/commands/branch/copy.rb +102 -0
  32. data/lib/git/commands/branch/create.rb +177 -0
  33. data/lib/git/commands/branch/delete.rb +88 -0
  34. data/lib/git/commands/branch/list.rb +178 -0
  35. data/lib/git/commands/branch/move.rb +102 -0
  36. data/lib/git/commands/branch/set_upstream.rb +86 -0
  37. data/lib/git/commands/branch/show_current.rb +49 -0
  38. data/lib/git/commands/branch/unset_upstream.rb +53 -0
  39. data/lib/git/commands/branch.rb +34 -0
  40. data/lib/git/commands/cat_file/batch.rb +385 -0
  41. data/lib/git/commands/cat_file/filtered.rb +105 -0
  42. data/lib/git/commands/cat_file/raw.rb +203 -0
  43. data/lib/git/commands/cat_file.rb +49 -0
  44. data/lib/git/commands/checkout/branch.rb +153 -0
  45. data/lib/git/commands/checkout/files.rb +116 -0
  46. data/lib/git/commands/checkout.rb +38 -0
  47. data/lib/git/commands/checkout_index.rb +106 -0
  48. data/lib/git/commands/clean.rb +102 -0
  49. data/lib/git/commands/clone.rb +241 -0
  50. data/lib/git/commands/commit.rb +273 -0
  51. data/lib/git/commands/commit_tree.rb +101 -0
  52. data/lib/git/commands/config_option_syntax/add.rb +86 -0
  53. data/lib/git/commands/config_option_syntax/get.rb +121 -0
  54. data/lib/git/commands/config_option_syntax/get_all.rb +118 -0
  55. data/lib/git/commands/config_option_syntax/get_color.rb +95 -0
  56. data/lib/git/commands/config_option_syntax/get_color_bool.rb +96 -0
  57. data/lib/git/commands/config_option_syntax/get_regexp.rb +119 -0
  58. data/lib/git/commands/config_option_syntax/get_urlmatch.rb +111 -0
  59. data/lib/git/commands/config_option_syntax/list.rb +111 -0
  60. data/lib/git/commands/config_option_syntax/remove_section.rb +79 -0
  61. data/lib/git/commands/config_option_syntax/rename_section.rb +83 -0
  62. data/lib/git/commands/config_option_syntax/replace_all.rb +109 -0
  63. data/lib/git/commands/config_option_syntax/set.rb +119 -0
  64. data/lib/git/commands/config_option_syntax/unset.rb +92 -0
  65. data/lib/git/commands/config_option_syntax/unset_all.rb +94 -0
  66. data/lib/git/commands/config_option_syntax.rb +56 -0
  67. data/lib/git/commands/describe.rb +156 -0
  68. data/lib/git/commands/diff.rb +657 -0
  69. data/lib/git/commands/diff_files.rb +519 -0
  70. data/lib/git/commands/diff_index.rb +499 -0
  71. data/lib/git/commands/fetch.rb +354 -0
  72. data/lib/git/commands/fsck.rb +138 -0
  73. data/lib/git/commands/gc.rb +134 -0
  74. data/lib/git/commands/grep.rb +339 -0
  75. data/lib/git/commands/init.rb +101 -0
  76. data/lib/git/commands/log.rb +634 -0
  77. data/lib/git/commands/ls_files.rb +195 -0
  78. data/lib/git/commands/ls_remote.rb +161 -0
  79. data/lib/git/commands/ls_tree.rb +135 -0
  80. data/lib/git/commands/maintenance/register.rb +73 -0
  81. data/lib/git/commands/maintenance/run.rb +105 -0
  82. data/lib/git/commands/maintenance/start.rb +67 -0
  83. data/lib/git/commands/maintenance/stop.rb +56 -0
  84. data/lib/git/commands/maintenance/unregister.rb +80 -0
  85. data/lib/git/commands/maintenance.rb +29 -0
  86. data/lib/git/commands/merge/abort.rb +44 -0
  87. data/lib/git/commands/merge/continue.rb +44 -0
  88. data/lib/git/commands/merge/quit.rb +46 -0
  89. data/lib/git/commands/merge/start.rb +250 -0
  90. data/lib/git/commands/merge.rb +28 -0
  91. data/lib/git/commands/merge_base.rb +91 -0
  92. data/lib/git/commands/mv.rb +82 -0
  93. data/lib/git/commands/name_rev.rb +119 -0
  94. data/lib/git/commands/pull.rb +382 -0
  95. data/lib/git/commands/push.rb +251 -0
  96. data/lib/git/commands/read_tree.rb +154 -0
  97. data/lib/git/commands/remote/add.rb +96 -0
  98. data/lib/git/commands/remote/get_url.rb +68 -0
  99. data/lib/git/commands/remote/list.rb +56 -0
  100. data/lib/git/commands/remote/prune.rb +63 -0
  101. data/lib/git/commands/remote/remove.rb +52 -0
  102. data/lib/git/commands/remote/rename.rb +76 -0
  103. data/lib/git/commands/remote/set_branches.rb +70 -0
  104. data/lib/git/commands/remote/set_head.rb +89 -0
  105. data/lib/git/commands/remote/set_url.rb +78 -0
  106. data/lib/git/commands/remote/set_url_add.rb +70 -0
  107. data/lib/git/commands/remote/set_url_delete.rb +71 -0
  108. data/lib/git/commands/remote/show.rb +77 -0
  109. data/lib/git/commands/remote/update.rb +79 -0
  110. data/lib/git/commands/remote.rb +42 -0
  111. data/lib/git/commands/repack.rb +281 -0
  112. data/lib/git/commands/reset.rb +154 -0
  113. data/lib/git/commands/rev_parse.rb +304 -0
  114. data/lib/git/commands/revert/abort.rb +45 -0
  115. data/lib/git/commands/revert/continue.rb +62 -0
  116. data/lib/git/commands/revert/quit.rb +47 -0
  117. data/lib/git/commands/revert/skip.rb +44 -0
  118. data/lib/git/commands/revert/start.rb +158 -0
  119. data/lib/git/commands/revert.rb +29 -0
  120. data/lib/git/commands/rm.rb +113 -0
  121. data/lib/git/commands/show.rb +632 -0
  122. data/lib/git/commands/show_ref/exclude_existing.rb +119 -0
  123. data/lib/git/commands/show_ref/exists.rb +75 -0
  124. data/lib/git/commands/show_ref/list.rb +149 -0
  125. data/lib/git/commands/show_ref/verify.rb +122 -0
  126. data/lib/git/commands/show_ref.rb +42 -0
  127. data/lib/git/commands/stash/apply.rb +81 -0
  128. data/lib/git/commands/stash/branch.rb +67 -0
  129. data/lib/git/commands/stash/clear.rb +43 -0
  130. data/lib/git/commands/stash/create.rb +60 -0
  131. data/lib/git/commands/stash/drop.rb +73 -0
  132. data/lib/git/commands/stash/list.rb +43 -0
  133. data/lib/git/commands/stash/pop.rb +87 -0
  134. data/lib/git/commands/stash/push.rb +112 -0
  135. data/lib/git/commands/stash/show.rb +158 -0
  136. data/lib/git/commands/stash/store.rb +72 -0
  137. data/lib/git/commands/stash.rb +38 -0
  138. data/lib/git/commands/status.rb +174 -0
  139. data/lib/git/commands/symbolic_ref/delete.rb +72 -0
  140. data/lib/git/commands/symbolic_ref/read.rb +99 -0
  141. data/lib/git/commands/symbolic_ref/update.rb +79 -0
  142. data/lib/git/commands/symbolic_ref.rb +38 -0
  143. data/lib/git/commands/tag/create.rb +142 -0
  144. data/lib/git/commands/tag/delete.rb +57 -0
  145. data/lib/git/commands/tag/list.rb +146 -0
  146. data/lib/git/commands/tag/verify.rb +71 -0
  147. data/lib/git/commands/tag.rb +26 -0
  148. data/lib/git/commands/update_ref/batch.rb +145 -0
  149. data/lib/git/commands/update_ref/delete.rb +90 -0
  150. data/lib/git/commands/update_ref/update.rb +103 -0
  151. data/lib/git/commands/update_ref.rb +42 -0
  152. data/lib/git/commands/version.rb +60 -0
  153. data/lib/git/commands/worktree/add.rb +139 -0
  154. data/lib/git/commands/worktree/list.rb +64 -0
  155. data/lib/git/commands/worktree/lock.rb +58 -0
  156. data/lib/git/commands/worktree/management_base.rb +51 -0
  157. data/lib/git/commands/worktree/move.rb +66 -0
  158. data/lib/git/commands/worktree/prune.rb +67 -0
  159. data/lib/git/commands/worktree/remove.rb +63 -0
  160. data/lib/git/commands/worktree/repair.rb +71 -0
  161. data/lib/git/commands/worktree/unlock.rb +47 -0
  162. data/lib/git/commands/worktree.rb +43 -0
  163. data/lib/git/commands/write_tree.rb +68 -0
  164. data/lib/git/commands.rb +88 -0
  165. data/lib/git/config.rb +64 -1
  166. data/lib/git/config_entry_info.rb +106 -0
  167. data/lib/git/configuring.rb +795 -0
  168. data/lib/git/detached_head_info.rb +57 -0
  169. data/lib/git/diff.rb +295 -12
  170. data/lib/git/diff_file_numstat_info.rb +31 -0
  171. data/lib/git/diff_file_patch_info.rb +136 -0
  172. data/lib/git/diff_file_raw_info.rb +129 -0
  173. data/lib/git/diff_info.rb +162 -0
  174. data/lib/git/diff_path_status.rb +80 -19
  175. data/lib/git/diff_result.rb +34 -0
  176. data/lib/git/diff_stats.rb +66 -14
  177. data/lib/git/dirstat_info.rb +102 -0
  178. data/lib/git/encoding_utils.rb +57 -1
  179. data/lib/git/errors.rb +107 -26
  180. data/lib/git/escaped_path.rb +55 -3
  181. data/lib/git/execution_context/global.rb +31 -0
  182. data/lib/git/execution_context/repository.rb +151 -0
  183. data/lib/git/execution_context.rb +559 -0
  184. data/lib/git/factories.rb +1049 -0
  185. data/lib/git/file_ref.rb +77 -0
  186. data/lib/git/fsck_object.rb +17 -9
  187. data/lib/git/fsck_result.rb +27 -16
  188. data/lib/git/log.rb +172 -65
  189. data/lib/git/object.rb +323 -108
  190. data/lib/git/parsers/branch.rb +240 -0
  191. data/lib/git/parsers/cat_file.rb +111 -0
  192. data/lib/git/parsers/config_entry.rb +110 -0
  193. data/lib/git/parsers/diff.rb +792 -0
  194. data/lib/git/parsers/fsck.rb +144 -0
  195. data/lib/git/parsers/grep.rb +42 -0
  196. data/lib/git/parsers/ls_remote.rb +79 -0
  197. data/lib/git/parsers/ls_tree.rb +58 -0
  198. data/lib/git/parsers/remote.rb +162 -0
  199. data/lib/git/parsers/stash.rb +292 -0
  200. data/lib/git/parsers/status.rb +251 -0
  201. data/lib/git/parsers/tag.rb +341 -0
  202. data/lib/git/parsers/worktree.rb +185 -0
  203. data/lib/git/path_resolver.rb +274 -0
  204. data/lib/git/remote_info.rb +203 -0
  205. data/lib/git/repository/branching.rb +796 -0
  206. data/lib/git/repository/committing.rb +221 -0
  207. data/lib/git/repository/context_helpers.rb +454 -0
  208. data/lib/git/repository/diffing.rb +743 -0
  209. data/lib/git/repository/inspecting.rb +252 -0
  210. data/lib/git/repository/logging.rb +410 -0
  211. data/lib/git/repository/maintenance.rb +65 -0
  212. data/lib/git/repository/merging.rb +429 -0
  213. data/lib/git/repository/object_operations.rb +1334 -0
  214. data/lib/git/repository/remote_operations.rb +772 -0
  215. data/lib/git/repository/shared_private.rb +120 -0
  216. data/lib/git/repository/staging.rb +409 -0
  217. data/lib/git/repository/stashing.rb +619 -0
  218. data/lib/git/repository/status_operations.rb +196 -0
  219. data/lib/git/repository/worktree_operations.rb +252 -0
  220. data/lib/git/repository.rb +204 -1
  221. data/lib/git/stash_info.rb +102 -0
  222. data/lib/git/status_file_info.rb +258 -0
  223. data/lib/git/status_info.rb +189 -0
  224. data/lib/git/system_call_guard.rb +95 -0
  225. data/lib/git/tag_delete_failure.rb +34 -0
  226. data/lib/git/tag_delete_result.rb +66 -0
  227. data/lib/git/tag_info.rb +99 -0
  228. data/lib/git/url.rb +12 -5
  229. data/lib/git/version.rb +111 -2
  230. data/lib/git/version_constraint.rb +85 -0
  231. data/lib/git/worktree_info.rb +128 -0
  232. data/lib/git.rb +319 -332
  233. metadata +318 -74
  234. data/.commitlintrc.yml +0 -38
  235. data/.github/copilot-instructions.md +0 -2733
  236. data/.github/issue_template.md +0 -15
  237. data/.github/pull_request_template.md +0 -17
  238. data/.github/workflows/continuous_integration.yml +0 -120
  239. data/.github/workflows/enforce_conventional_commits.yml +0 -29
  240. data/.github/workflows/experimental_continuous_integration.yml +0 -59
  241. data/.github/workflows/release.yml +0 -53
  242. data/.gitignore +0 -15
  243. data/.husky/commit-msg +0 -1
  244. data/.release-please-manifest.json +0 -3
  245. data/.rubocop.yml +0 -55
  246. data/.rubocop_todo.yml +0 -12
  247. data/Gemfile +0 -5
  248. data/Rakefile +0 -18
  249. data/lib/git/args_builder.rb +0 -111
  250. data/lib/git/author.rb +0 -16
  251. data/lib/git/base.rb +0 -1140
  252. data/lib/git/branch.rb +0 -423
  253. data/lib/git/branches.rb +0 -68
  254. data/lib/git/command_line_result.rb +0 -92
  255. data/lib/git/index.rb +0 -6
  256. data/lib/git/lib.rb +0 -2333
  257. data/lib/git/path.rb +0 -38
  258. data/lib/git/remote.rb +0 -39
  259. data/lib/git/stash.rb +0 -26
  260. data/lib/git/stashes.rb +0 -56
  261. data/lib/git/status.rb +0 -159
  262. data/lib/git/working_directory.rb +0 -6
  263. data/lib/git/worktree.rb +0 -39
  264. data/lib/git/worktrees.rb +0 -47
  265. data/package.json +0 -10
  266. data/redesign/1_architecture_existing.md +0 -66
  267. data/redesign/2_architecture_redesign.md +0 -130
  268. data/redesign/3_architecture_implementation.md +0 -138
  269. data/redesign/index.md +0 -34
  270. data/release-please-config.json +0 -36
  271. data/tasks/gem_tasks.rake +0 -10
  272. data/tasks/rubocop.rake +0 -12
  273. data/tasks/test.rake +0 -13
  274. data/tasks/test_gem.rake +0 -12
  275. data/tasks/yard.rake +0 -23
@@ -0,0 +1,274 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'git/commands/rev_parse'
4
+ require 'git/errors'
5
+ require 'git/execution_context'
6
+ require 'git/execution_context/global'
7
+ require 'git/system_call_guard'
8
+
9
+ module Git
10
+ # Resolves and normalizes the filesystem paths that locate a Git repository
11
+ #
12
+ # `PathResolver` is the single home for the path-resolution logic used by the
13
+ # `Git` factory methods ({Git.open}, {Git.bare}, {Git.init}, and {Git.clone}).
14
+ # It computes the absolute working-directory, repository (`.git`), and index
15
+ # paths from the caller-supplied values, following the same rules Git itself
16
+ # uses (including gitdir-pointer files for submodules and linked worktrees).
17
+ #
18
+ # A relative path given by the caller is expanded against the process working
19
+ # directory, as git does for a path on its command line. Only the defaults are
20
+ # derived from the layout: an omitted repository is `<working_directory>/.git`
21
+ # and an omitted index is `<repository>/index`.
22
+ #
23
+ # @api private
24
+ #
25
+ module PathResolver
26
+ # The {Git::Error} message when a working directory path cannot be expanded
27
+ WORKING_DIRECTORY_MESSAGE = 'Failed to resolve the working directory'
28
+
29
+ # The {Git::Error} message when a repository directory path cannot be expanded
30
+ REPOSITORY_MESSAGE = 'Failed to resolve the repository directory'
31
+
32
+ # The {Git::Error} message when an index file path cannot be expanded
33
+ INDEX_MESSAGE = 'Failed to resolve the index file'
34
+
35
+ # The {Git::Error} message when the `:chdir` option of `Git.clone` cannot be expanded
36
+ CHDIR_MESSAGE = 'Failed to resolve the chdir directory'
37
+
38
+ module_function
39
+
40
+ # Resolve and normalize the paths that locate a Git repository
41
+ #
42
+ # Returns a new hash containing the resolved absolute paths for:
43
+ # * `:working_directory` — the working tree root (`nil` for bare repos)
44
+ # * `:repository` — the `.git` directory
45
+ # * `:index` — the index file
46
+ #
47
+ # This method does not mutate any inputs.
48
+ #
49
+ # @example Resolve paths for a working tree
50
+ # Git::PathResolver.resolve_paths(working_directory: '/repo')
51
+ # #=> { working_directory: '/repo', repository: '/repo/.git', index: '/repo/.git/index' }
52
+ #
53
+ # @param working_directory [String, nil] the working directory path
54
+ #
55
+ # @param repository [String, nil] the repository (`.git`) directory path
56
+ #
57
+ # @param index [String, nil] the index file path
58
+ #
59
+ # @param bare [Boolean] whether this is a bare repository
60
+ #
61
+ # @return [Hash{Symbol => (String, nil)}] a hash with `:working_directory`,
62
+ # `:repository`, and `:index` keys
63
+ #
64
+ # @raise [Git::Error] if the repository path is a gitdir pointer file that
65
+ # cannot be read
66
+ #
67
+ def resolve_paths(working_directory: nil, repository: nil, index: nil, bare: false)
68
+ working_dir = resolve_working_directory(working_directory, bare: bare)
69
+ repo_path = resolve_repository(repository, working_dir)
70
+ index_path = resolve_index(index, repo_path)
71
+
72
+ {
73
+ working_directory: working_dir,
74
+ repository: repo_path,
75
+ index: index_path
76
+ }
77
+ end
78
+
79
+ # Find the root of the working tree that contains `working_dir`
80
+ #
81
+ # Runs `git rev-parse --show-toplevel` from `working_dir` to locate the
82
+ # top-level directory of the working tree.
83
+ #
84
+ # @example Find the worktree root from a subdirectory
85
+ # Git::PathResolver.root_of_worktree('/repo/subdir') #=> '/repo'
86
+ #
87
+ # @param working_dir [String] a path inside the working tree
88
+ #
89
+ # @param binary_path [String, :use_global_config] path to the git binary
90
+ #
91
+ # Controls which git binary is invoked during root detection. Defaults to
92
+ # `:use_global_config`, which resolves to `Git.config.binary_path`.
93
+ #
94
+ # @param git_ssh [String, nil, :use_global_config] the SSH wrapper path
95
+ #
96
+ # Forwarded as `GIT_SSH`. Defaults to `:use_global_config`.
97
+ #
98
+ # @return [String] the absolute path to the root of the working tree
99
+ #
100
+ # @raise [ArgumentError] if `working_dir` does not exist, is not a
101
+ # directory, or is not inside a git working tree
102
+ #
103
+ # @raise [Git::Error] if the git binary cannot be found or fails to launch, or
104
+ # if `working_dir` cannot be expanded to an absolute path
105
+ #
106
+ def root_of_worktree(working_dir, binary_path: :use_global_config, git_ssh: :use_global_config)
107
+ expanded_dir = expand_path(working_dir, 'Failed to expand the working directory path')
108
+ raise ArgumentError, "'#{working_dir}' does not exist or is not a directory" unless Dir.exist?(expanded_dir)
109
+
110
+ execute_rev_parse_toplevel(expanded_dir, binary_path: binary_path, git_ssh: git_ssh)
111
+ end
112
+
113
+ # Run `git rev-parse --show-toplevel` from `working_dir` and return stdout
114
+ #
115
+ # @param working_dir [String] the absolute path of a directory inside the
116
+ # working tree
117
+ #
118
+ # @param binary_path [String, :use_global_config] path to the git binary
119
+ #
120
+ # @param git_ssh [String, nil, :use_global_config] the SSH wrapper path
121
+ #
122
+ # @return [String] the top-level directory reported by git
123
+ #
124
+ # @raise [ArgumentError] if `working_dir` is not inside a git working tree
125
+ #
126
+ # @raise [Git::Error] if the git binary cannot be found or fails to launch
127
+ #
128
+ # @api private
129
+ #
130
+ def execute_rev_parse_toplevel(working_dir, binary_path: :use_global_config, git_ssh: :use_global_config)
131
+ execution_context = Git::ExecutionContext::Global.new(binary_path: binary_path, git_ssh: git_ssh)
132
+
133
+ Git::Commands::RevParse.new(execution_context).call(show_toplevel: true, chdir: working_dir).stdout
134
+ rescue Git::FailedError
135
+ raise ArgumentError, "'#{working_dir}' is not in a git working tree"
136
+ end
137
+ private_class_method :execute_rev_parse_toplevel
138
+
139
+ # Resolve the working directory path
140
+ #
141
+ # @param path [String, nil] the working directory path or `nil`
142
+ #
143
+ # @param bare [Boolean] whether this is a bare repository
144
+ #
145
+ # @return [String, nil] the absolute path, or `nil` for bare repos
146
+ #
147
+ # @raise [Git::Error] if the path cannot be expanded, which happens when the
148
+ # process working directory has been removed
149
+ #
150
+ # @api private
151
+ #
152
+ def resolve_working_directory(path, bare:)
153
+ return nil if bare
154
+
155
+ expand_path(path, WORKING_DIRECTORY_MESSAGE)
156
+ end
157
+ private_class_method :resolve_working_directory
158
+
159
+ # Resolve the repository (`.git`) directory path
160
+ #
161
+ # Handles the gitdir-pointer file case for submodules and linked worktrees.
162
+ #
163
+ # @param path [String, nil] the repository path or `nil`
164
+ #
165
+ # @param working_dir [String, nil] the working directory whose `.git` is the
166
+ # default when `path` is `nil`, or `nil` for a bare repository, whose
167
+ # default is the process working directory
168
+ #
169
+ # @return [String] the absolute path to the repository
170
+ #
171
+ # @raise [Git::Error] if the path cannot be expanded, which happens when the
172
+ # process working directory has been removed
173
+ #
174
+ # @api private
175
+ #
176
+ def resolve_repository(path, working_dir)
177
+ initial_path =
178
+ if working_dir && path.nil?
179
+ File.join(working_dir, '.git')
180
+ else
181
+ expand_path(path, REPOSITORY_MESSAGE)
182
+ end
183
+
184
+ resolve_gitdir_pointer(initial_path)
185
+ end
186
+ private_class_method :resolve_repository
187
+
188
+ # Resolve gitdir-pointer files used by submodules and linked worktrees
189
+ #
190
+ # If `path` points to a file containing `"gitdir: <path>"`, returns the
191
+ # resolved target path. Otherwise returns `path` unchanged.
192
+ #
193
+ # @param path [String] the path to check
194
+ #
195
+ # @return [String] the resolved absolute path
196
+ #
197
+ # @raise [Git::Error] if `path` is a file that cannot be read
198
+ #
199
+ # Relative pointer targets are resolved from the directory containing the
200
+ # pointer file itself, matching git's pointer-file semantics.
201
+ #
202
+ # @api private
203
+ #
204
+ def resolve_gitdir_pointer(path)
205
+ return path unless File.file?(path)
206
+
207
+ gitdir_content = Git::SystemCallGuard.call('Failed to read the gitdir pointer file') { File.read(path) }.strip
208
+ return path unless gitdir_content.start_with?('gitdir: ')
209
+
210
+ gitdir_path = gitdir_content.sub(/\Agitdir: /, '')
211
+ File.expand_path(gitdir_path, File.dirname(path))
212
+ end
213
+ private_class_method :resolve_gitdir_pointer
214
+
215
+ # Resolve the index file path
216
+ #
217
+ # @param path [String, nil] the index path or `nil`
218
+ #
219
+ # @param repository [String] the repository directory whose `index` is the
220
+ # default when `path` is `nil`
221
+ #
222
+ # @return [String] the absolute path to the index file
223
+ #
224
+ # @raise [Git::Error] if the path cannot be expanded, which happens when the
225
+ # process working directory has been removed
226
+ #
227
+ # @api private
228
+ #
229
+ def resolve_index(path, repository)
230
+ return File.join(repository, 'index') unless path
231
+
232
+ expand_path(path, INDEX_MESSAGE)
233
+ end
234
+ private_class_method :resolve_index
235
+
236
+ # Expand a path the way a shell prompt would before git runs
237
+ #
238
+ # Follows `File.expand_path`: `~` is the home directory, a relative path is
239
+ # joined onto `base` (or onto the process working directory when `base` is
240
+ # `nil`), `.` and `..` are normalized, and an absolute path is unchanged.
241
+ #
242
+ # @example Expand a relative path against the process working directory
243
+ # Git::PathResolver.expand_path('repo', 'Failed to resolve the working directory')
244
+ # #=> '/current/dir/repo'
245
+ #
246
+ # @example Expand a relative path against another directory
247
+ # Git::PathResolver.expand_path('repo', 'Failed to resolve the working directory', base: '/output')
248
+ # #=> '/output/repo'
249
+ #
250
+ # @param path [String, Pathname, nil] the path to expand, or `nil` for
251
+ # `base` itself
252
+ #
253
+ # @param message [String] the message of the {Git::Error} raised when the
254
+ # expansion fails
255
+ #
256
+ # @param base [String, Pathname, nil] the directory a relative `path` is
257
+ # expanded against, or `nil` for the process working directory
258
+ #
259
+ # @return [String] the absolute path
260
+ #
261
+ # @raise [ArgumentError] if `~` cannot be expanded because no home directory
262
+ # can be determined, or if the path begins with `~user` for a user that
263
+ # does not exist
264
+ #
265
+ # @raise [Git::Error] if the path cannot be expanded, which happens when the
266
+ # process working directory has been removed
267
+ #
268
+ # @api private
269
+ #
270
+ def expand_path(path, message, base: nil)
271
+ Git::SystemCallGuard.call(message) { File.expand_path(path || '.', base) }
272
+ end
273
+ end
274
+ end
@@ -0,0 +1,203 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Git
4
+ # Value object representing a configured git remote
5
+ #
6
+ # Each instance holds the parsed configuration for a single remote as read
7
+ # from the repository's git config. Multi-value fields (`:url`, `:push_url`,
8
+ # `:fetch`, `:push`) are always `Array<String>` (never `nil`; may be empty).
9
+ # Those arrays are frozen copies of the values given, so the set of URLs and
10
+ # refspecs cannot change after construction; use `with` to derive a modified
11
+ # copy. The immutability is shallow, as with any `Data` member: the strings
12
+ # inside those arrays and the scalar members are the objects the caller
13
+ # passed in, not copies. All other fields are nilable except `:name`.
14
+ #
15
+ # @example Minimal remote (fetch-only, one URL)
16
+ # info = Git::RemoteInfo.new(
17
+ # name: 'origin',
18
+ # url: ['https://github.com/ruby-git/ruby-git.git'],
19
+ # push_url: [],
20
+ # fetch: ['+refs/heads/*:refs/remotes/origin/*'],
21
+ # push: []
22
+ # )
23
+ # info.name # => "origin"
24
+ # info.url # => ["https://github.com/ruby-git/ruby-git.git"]
25
+ # info.prune # => nil
26
+ #
27
+ # @api public
28
+ #
29
+ # @!attribute [r] name
30
+ # @return [String] the name of the remote (e.g. `'origin'`)
31
+ #
32
+ # @!attribute [r] url
33
+ # @return [Array<String>] the fetch URL(s) for this remote (`remote.<name>.url`)
34
+ #
35
+ # @!attribute [r] push_url
36
+ # @return [Array<String>] the push URL(s) for this remote (`remote.<name>.pushurl`)
37
+ #
38
+ # @!attribute [r] fetch
39
+ # @return [Array<String>] the fetch refspec(s) for this remote (`remote.<name>.fetch`)
40
+ #
41
+ # @!attribute [r] push
42
+ # @return [Array<String>] the push refspec(s) for this remote (`remote.<name>.push`)
43
+ #
44
+ # @!attribute [r] mirror
45
+ # @return [Boolean, nil] `true`/`false` per `remote.<name>.mirror`, or `nil` when not set
46
+ #
47
+ # @!attribute [r] skip_default_update
48
+ # @return [Boolean, nil] `true`/`false` per `remote.<name>.skipDefaultUpdate`, or `nil` when not set
49
+ #
50
+ # @!attribute [r] tag_opt
51
+ # @return [String, nil] the tag-fetching option (`remote.<name>.tagOpt`), or `nil` when not set
52
+ #
53
+ # @!attribute [r] prune
54
+ # @return [Boolean, nil] `true`/`false` per `remote.<name>.prune`; `nil` inherits `fetch.prune`
55
+ #
56
+ # @!attribute [r] prune_tags
57
+ # @return [Boolean, nil] `true`/`false` per `remote.<name>.pruneTags`; `nil` inherits `fetch.pruneTags`
58
+ #
59
+ # @!attribute [r] receivepack
60
+ # @return [String, nil] the `git-receive-pack` path on the remote (`remote.<name>.receivepack`)
61
+ #
62
+ # @!attribute [r] uploadpack
63
+ # @return [String, nil] the `git-upload-pack` path on the remote (`remote.<name>.uploadpack`)
64
+ #
65
+ # @!attribute [r] promisor
66
+ # @return [Boolean, nil] `true`/`false` per `remote.<name>.promisor`, or `nil` when not set
67
+ #
68
+ # @!attribute [r] partial_clone_filter
69
+ # @return [String, nil] the partial-clone object filter (`remote.<name>.partialclonefilter`)
70
+ #
71
+ # @!attribute [r] vcs
72
+ # @return [String, nil] the VCS type for git-remote helpers (`remote.<name>.vcs`)
73
+ #
74
+ RemoteInfo = Data.define(
75
+ :name,
76
+ :url,
77
+ :push_url,
78
+ :fetch,
79
+ :push,
80
+ :mirror,
81
+ :skip_default_update,
82
+ :tag_opt,
83
+ :prune,
84
+ :prune_tags,
85
+ :receivepack,
86
+ :uploadpack,
87
+ :promisor,
88
+ :partial_clone_filter,
89
+ :vcs
90
+ ) do
91
+ # Create a new RemoteInfo
92
+ #
93
+ # @param name [String] the name of the remote (required)
94
+ #
95
+ # @param url [Array<String>] fetch URLs (default `[]`); stored as a frozen copy
96
+ #
97
+ # @param push_url [Array<String>] push URLs (default `[]`); stored as a frozen copy
98
+ #
99
+ # @param fetch [Array<String>] fetch refspecs (default `[]`); stored as a frozen copy
100
+ #
101
+ # @param push [Array<String>] push refspecs (default `[]`); stored as a frozen copy
102
+ #
103
+ # @param mirror [Boolean, nil] mirror flag (default `nil`)
104
+ #
105
+ # @param skip_default_update [Boolean, nil] skip-default-update flag (default `nil`)
106
+ #
107
+ # @param tag_opt [String, nil] tag-fetching option (default `nil`)
108
+ #
109
+ # @param prune [Boolean, nil] prune flag (default `nil`)
110
+ #
111
+ # @param prune_tags [Boolean, nil] prune-tags flag (default `nil`)
112
+ #
113
+ # @param receivepack [String, nil] receive-pack path (default `nil`)
114
+ #
115
+ # @param uploadpack [String, nil] upload-pack path (default `nil`)
116
+ #
117
+ # @param promisor [Boolean, nil] promisor flag (default `nil`)
118
+ #
119
+ # @param partial_clone_filter [String, nil] partial-clone filter (default `nil`)
120
+ #
121
+ # @param vcs [String, nil] VCS type (default `nil`)
122
+ #
123
+ # @return [Git::RemoteInfo]
124
+ #
125
+ def initialize( # rubocop:disable Metrics/ParameterLists
126
+ name:,
127
+ url: [],
128
+ push_url: [],
129
+ fetch: [],
130
+ push: [],
131
+ mirror: nil,
132
+ skip_default_update: nil,
133
+ tag_opt: nil,
134
+ prune: nil,
135
+ prune_tags: nil,
136
+ receivepack: nil,
137
+ uploadpack: nil,
138
+ promisor: nil,
139
+ partial_clone_filter: nil,
140
+ vcs: nil
141
+ )
142
+ super(
143
+ name:, url: Array(url).dup.freeze, push_url: Array(push_url).dup.freeze,
144
+ fetch: Array(fetch).dup.freeze, push: Array(push).dup.freeze, mirror:,
145
+ skip_default_update:, tag_opt:, prune:, prune_tags:, receivepack:, uploadpack:,
146
+ promisor:, partial_clone_filter:, vcs:
147
+ )
148
+ end
149
+
150
+ # Return a copy of this RemoteInfo with the given fields replaced
151
+ #
152
+ # Routes through {#initialize} so the multi-value fields of the copy are
153
+ # frozen copies, the same as on construction. `Data#with` bypasses
154
+ # `initialize` on Ruby 3.2, which would leave those arrays mutable.
155
+ #
156
+ # @example Replace the fetch URL
157
+ # info.with(url: ['https://example.com/other.git']).url
158
+ # # => ["https://example.com/other.git"]
159
+ #
160
+ # @param fields [Hash{Symbol => Object}] the fields to replace, keyed by
161
+ # member name
162
+ #
163
+ # @option fields [String] :name the name of the remote
164
+ #
165
+ # @option fields [Array<String>] :url fetch URLs
166
+ #
167
+ # @option fields [Array<String>] :push_url push URLs
168
+ #
169
+ # @option fields [Array<String>] :fetch fetch refspecs
170
+ #
171
+ # @option fields [Array<String>] :push push refspecs
172
+ #
173
+ # @option fields [Boolean, nil] :mirror mirror flag
174
+ #
175
+ # @option fields [Boolean, nil] :skip_default_update skip-default-update flag
176
+ #
177
+ # @option fields [String, nil] :tag_opt tag-fetching option
178
+ #
179
+ # @option fields [Boolean, nil] :prune prune flag
180
+ #
181
+ # @option fields [Boolean, nil] :prune_tags prune-tags flag
182
+ #
183
+ # @option fields [String, nil] :receivepack receive-pack path
184
+ #
185
+ # @option fields [String, nil] :uploadpack upload-pack path
186
+ #
187
+ # @option fields [Boolean, nil] :promisor promisor flag
188
+ #
189
+ # @option fields [String, nil] :partial_clone_filter partial-clone filter
190
+ #
191
+ # @option fields [String, nil] :vcs VCS type
192
+ #
193
+ # @return [Git::RemoteInfo] a new instance; `self` when no fields are given
194
+ #
195
+ # @raise [ArgumentError] if a key is not a member of this Data class
196
+ #
197
+ def with(**fields)
198
+ return self if fields.empty?
199
+
200
+ self.class.new(**to_h, **fields)
201
+ end
202
+ end
203
+ end