Compare

48fc56621cbea18dd0f23a87af90b7a930d1e1d2..a51b77aa1d8ed033a018849465a65fd4e1dde02b

odb: provide infrastructure for pluggable fsck checks

The on-disk consistency checks in git-fsck(1) are conceptually
backend-specific: while connectivity checks and object-level parsing
checks are generic, verifying the physical integrity of packfiles and
loose objects is meaningful only to backends that use these formats:
Having these checks live in "builtin/fsck.c" violates that layering,
because it forces the command to reach directly into format-specific
internals.

Provide new infrastructure to make these format-specific checks
pluggable and implement stubs for the different source types we already
have. In subsequent commits we'll move functionality over piece by
piece.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt <ps@pks.im> · 2026-09-11 13:27 UTC · committed by Junio C Hamano

Browse files
116 files changed, +4026 −1537
modified Documentation/Makefile
@@ -129,6 +129,7 @@ TECH_DOCS += technical/long-running-process-protocol
129 129 TECH_DOCS += technical/multi-pack-index
130 130 TECH_DOCS += technical/packfile-uri
131 131 TECH_DOCS += technical/pack-heuristics
132 + TECH_DOCS += technical/paint-down-to-common
132 133 TECH_DOCS += technical/parallel-checkout
133 134 TECH_DOCS += technical/partial-clone
134 135 TECH_DOCS += technical/platform-support
modified Documentation/RelNotes/2.56.0.adoc
@@ -101,6 +101,26 @@ UI, Workflows & Features
101 101 automatically run 'git bisect reset' to jump back to the original
102 102 state or to the found culprit.
103 103
104 + * The 'git branch' command has been taught the '--delete-merged' option
105 + to remove local branches that are already merged into their tracked
106 + remote-tracking branches.
107 +
108 + * The 'remote-object-info' command for 'git cat-file --batch-command'
109 + has been extended to support the '%(objecttype)' placeholder.
110 +
111 + * The usage string of 'git fast-import' has been updated to use the
112 + parse_options() API for displaying help, and its SYNOPSIS in the
113 + documentation has been standardized to match.
114 +
115 + * The error message given by 'git send-email' when a message file is
116 + missing a 'Subject:' header has been clarified, and the error string
117 + is now terminated with a newline so that Perl avoids appending its
118 + internal source location data.
119 +
120 + * The '--shallow-file' option of 'git' command requires a value, but the
121 + code did not check the presence of a value and instead segfaulted
122 + without one, which has been corrected.
123 +
104 124
105 125 Performance, Internal Implementation, Development Support etc.
106 126 --------------------------------------------------------------
@@ -371,6 +391,34 @@ Performance, Internal Implementation, Development Support etc.
371 391 Calls to write(3p) in send_sideband() and cat_blob() have been
372 392 refactored to use writev(3p) wrappers to reduce syscall overhead.
373 393
394 + * The creation of the on-disk data structures for the object database
395 + has been made pluggable, allowing future backends to customize their
396 + setup. As part of this, the initialization of the object database
397 + has been deferred, and the loading of the loose-object map has been
398 + detangled from repository initialization.
399 +
400 + * The 'struct odb_read_stream' and 'struct odb_write_stream'
401 + structures have been consolidated into a single unified 'struct
402 + odb_stream' structure, simplifying object database streaming APIs
403 + and enabling streaming of arbitrary object types.
404 +
405 + * The sequencer has been updated to release the object database before
406 + spawning 'git commit'. This prevents open file handles from
407 + blocking auto-maintenance tasks, such as repacking, on systems like
408 + Windows where open files cannot be easily unlinked.
409 +
410 + * The merge-base computation has been optimized by stopping the walk
411 + early when one side's exclusive commits in the queue are exhausted,
412 + yielding significant speedups for queries with one-sided histories.
413 +
414 + * A handful of code paths have been corrected to check return values
415 + from functions like curl_easy_duphandle(), deflateInit(), lseek(),
416 + dup(), and strbuf_getline_lf(), resolving several Coverity warnings
417 + about unchecked returns.
418 +
419 + * The setting of a now-unused member '.pretty_given' in the sequencer
420 + machinery has been removed.
421 +
374 422
375 423 Fixes since v2.55
376 424 -----------------
@@ -596,3 +644,28 @@ Fixes since v2.55
596 644 * Documentation for 'git replay' has been updated to refer to its
597 645 configuration variables.
598 646 (merge 48c0549f5c kh/doc-replay-config later to maint).
647 +
648 + * Documentation for 'git interpret-trailers' has been updated to explain
649 + the format of trailer keys (alphanumeric characters and hyphens),
650 + replace outdated terminology, define key terms upfront, and document
651 + how comment lines in the input are treated.
652 + (merge 4515c86fd9 kh/doc-trailers later to maint).
653 +
654 + * The 'pack-objects' and delta-encoding code paths have been updated to
655 + use 'size_t' instead of 'unsigned long' for object sizes and offset
656 + limits, avoiding potential truncation issues on 64-bit Windows.
657 + (merge d50ac11724 js/pack-objects-delta-size-t later to maint).
658 +
659 + * A client requesting the promisor-remote capability without a value
660 + caused a null pointer dereference, which has been corrected by
661 + rejecting a request without an argument.
662 + (merge dd6b35ff71 en/serve-promisor-remote-fix later to maint).
663 +
664 + * Various tests in 't7900-maintenance.sh' have been updated to use a
665 + throwaway repository, and auto-detaching of maintenance tasks is now
666 + disabled for these tests to fix flaky races with concurrent background
667 + maintenance jobs.
668 + (merge 2775d8bcd1 ps/t7900-deflake-maintenance later to maint).
669 +
670 + * The help text for the '-l' option of 'git diff' has been updated.
671 + (merge 764243bdf4 en/diff-l-opt-help later to maint).
modified Documentation/config/branch.adoc
@@ -103,3 +103,10 @@ for details).
103 103 `git branch --edit-description`. Branch description is
104 104 automatically added to the `format-patch` cover letter or
105 105 `request-pull` summary.
106 +
107 + `branch.<name>.deleteMerged`::
108 + If set to `false`, branch _<name>_ is exempt from
109 + `git branch --delete-merged`. Useful for a topic branch you
110 + intend to develop further after an initial round has been
111 + merged upstream. Defaults to true. Explicit deletion via
112 + `git branch -d` is unaffected.
modified Documentation/git-branch.adoc
@@ -13,6 +13,7 @@ git branch [--color[=<when>] | --no-color] [--show-current]
13 13 [--column[=<options>] | --no-column] [--sort=<key>]
14 14 [--merged [<commit>]] [--no-merged [<commit>]]
15 15 [--contains [<commit>]] [--no-contains [<commit>]]
16 + [(--forked <branch>)...]
16 17 [--points-at <object>] [--format=<format>]
17 18 [(-r|--remotes) | (-a|--all)]
18 19 [--list] [<pattern>...]
@@ -24,6 +25,7 @@ git branch (-m|-M) [<old-branch>] <new-branch>
24 25 git branch (-c|-C) [<old-branch>] <new-branch>
25 26 git branch (-d|-D) [-r] <branch-name>...
26 27 git branch --edit-description [<branch-name>]
28 + git branch [--dry-run] (--delete-merged <pattern>)... [<branch-pattern>...]
27 29
28 30 DESCRIPTION
29 31 -----------
@@ -51,7 +53,8 @@ merged into the named commit (i.e. the branches whose tip commits are
51 53 reachable from the named commit) will be listed. With `--no-merged` only
52 54 branches not merged into the named commit will be listed. If the _<commit>_
53 55 argument is missing it defaults to `HEAD` (i.e. the tip of the current
54 - branch).
56 + branch). With `--forked`, only branches whose configured upstream matches
57 + the given branch or pattern will be listed.
55 58
56 59 The command's second form creates a new branch head named _<branch-name>_
57 60 which points to the current `HEAD`, or _<start-point>_ if given. As a
@@ -199,6 +202,45 @@ This option is only applicable in non-verbose mode.
199 202 Print the name of the current branch. In detached `HEAD` state,
200 203 nothing is printed.
201 204
205 + `--delete-merged <pattern>`::
206 + Delete local branches whose configured upstream matches
207 + _<pattern>_, but only when their tip is reachable from that
208 + upstream. In other words, the work on the branch has already
209 + landed on the upstream it tracks, so the local copy is no longer
210 + needed. _<pattern>_ may name a ref, a remote (using the branch its
211 + `HEAD` points at), or a shell-style glob. The option can be
212 + repeated to widen the upstream match.
213 + Optional _<branch-pattern>_ arguments limit which local branches
214 + are considered, e.g. `git branch --delete-merged 'origin/*'
215 + 'topic-*'`.
216 + +
217 + A branch is not deleted when:
218 + +
219 + --
220 + * its configured upstream ref no longer exists,
221 + * it is checked out in any worktree,
222 + * pushing it to the remote configured by
223 + `branch.<name>.remote` would update its upstream, so it cannot be
224 + distinguished from a branch that just looks fully merged right
225 + after a pull; this is determined by the remote's configured push and
226 + fetch refspecs,
227 + * it is the local upstream of a branch that is not being deleted, or
228 + * `branch.<name>.deleteMerged` is set to `false`.
229 + --
230 + +
231 + When such a local upstream branch has its own upstream deleted by the
232 + same operation, its upstream configuration is cleared.
233 + +
234 + A branch whose work has not yet been merged into its upstream is
235 + silently skipped. Delete it with `git branch -D` if you want to
236 + remove it anyway.
237 +
238 + `--dry-run`::
239 + With `--delete-merged`, print which branches would be
240 + deleted and exit without touching any ref. Useful for
241 + sanity-checking a wide pattern like `'origin/*'` before
242 + committing to the deletion.
243 +
202 244 `-v`::
203 245 `-vv`::
204 246 `--verbose`::
@@ -311,6 +353,14 @@ superproject's "origin/main", but tracks the submodule's "origin/main".
311 353 Only list branches whose tips are not reachable from
312 354 _<commit>_ (`HEAD` if not specified). Implies `--list`.
313 355
356 + `--forked <branch>`::
357 + Only list branches whose configured upstream matches
358 + _<branch>_. The argument can be a ref (e.g. `origin/main`,
359 + `master`), a remote name like `origin` for the branch its
360 + `origin/HEAD` points at, or a shell-style glob (e.g.
361 + `'origin/*'`). The option can be repeated to widen the
362 + filter. Implies `--list`.
363 +
314 364 `--points-at <object>`::
315 365 Only list branches of _<object>_.
316 366
modified Documentation/git-cat-file.adoc
@@ -348,15 +348,12 @@ newline. The available atoms are:
348 348 after that first run of whitespace (i.e., the "rest" of the
349 349 line) are output in place of the `%(rest)` atom.
350 350
351 - The command `remote-object-info` only supports the `%(objectname)` and
352 - `%(objectsize)` placeholders. See `CAVEATS` below for more information.
351 + The command `remote-object-info` only supports the `%(objectname)`,
352 + `%(objectsize)` and `%(objecttype)` placeholders. See `CAVEATS` below for more
353 + information.
353 354
354 355 If no format is specified, the default format is `%(objectname)
355 - %(objecttype) %(objectsize)`, except for `remote-object-info` commands which
356 - use `%(objectname) %(objectsize)` because `%(objecttype)` is not supported yet.
357 -
358 - WARNING: When "%(objecttype)" is supported, the default format WILL be unified,
359 - so DO NOT RELY on the current default format to stay the same!!!
356 + %(objecttype) %(objectsize)`.
360 357
361 358 If `--batch` is specified, or if `--batch-command` is used with the `contents`
362 359 command, the object information is followed by the object contents (consisting
@@ -453,9 +450,9 @@ scripting purposes.
453 450 CAVEATS
454 451 -------
455 452
456 - Note that only `%(objectname)` and `%(objectsize)` are currently
457 - supported by the `remote-object-info` command. Using any other placeholder in
458 - the format string will return an empty string in its position.
453 + Note that only `%(objectname)`, `%(objectsize)` and `%(objecttype)` are
454 + currently supported by the `remote-object-info` command. Using any other
455 + placeholder in the format string will return an empty string in its position.
459 456
460 457 Note that the sizes of objects on disk are reported accurately, but care
461 458 should be taken in drawing conclusions about which refs or objects are
modified Documentation/git-fast-import.adoc
@@ -9,7 +9,7 @@ git-fast-import - Backend for fast Git data importers
9 9 SYNOPSIS
10 10 --------
11 11 [verse]
12 - frontend | 'git fast-import' [<options>]
12 + 'git fast-import' [<options>]
13 13
14 14 DESCRIPTION
15 15 -----------
@@ -65,6 +65,13 @@ Only enable this option if you trust the program generating the
65 65 fast-import stream! This option is enabled automatically for
66 66 remote-helpers that use the `import` capability, as they are
67 67 already trusted to run their own code.
68 + +
69 + Note that this option has to be spelled in full, and has to appear
70 + before any option whose value is separated from it by a space, for
71 + the unsafe `feature` commands in the stream to be allowed. So
72 + `--allow-unsafe` or `--depth 5 --allow-unsafe-features` still refuse
73 + them, while `--allow-unsafe-features --depth 5` and
74 + `--depth=5 --allow-unsafe-features` allow them.
68 75
69 76 `--signed-tags=<mode>`::
70 77 Specify how to handle signed tags. Behaves in the same way as
modified Documentation/git-interpret-trailers.adoc
@@ -3,7 +3,7 @@ git-interpret-trailers(1)
3 3
4 4 NAME
5 5 ----
6 - git-interpret-trailers - Add or parse structured information in commit messages
6 + git-interpret-trailers - Add or parse metadata in commit messages
7 7
8 8 SYNOPSIS
9 9 --------
@@ -14,9 +14,15 @@ git interpret-trailers [--in-place] [--trim-empty]
14 14
15 15 DESCRIPTION
16 16 -----------
17 - Add or parse _trailer_ lines that look similar to RFC 822 e-mail
18 - headers, at the end of the otherwise free-form part of a commit
19 - message. For example, in the following commit message
17 + Add or parse trailer metadata at the end of the otherwise
18 + free-form part of a commit message, or any other kind of text.
19 +
20 + A _trailer_ in its simplest form is a key-value pair with a colon as a
21 + separator. The _key_ consists of ASCII alphanumeric characters and
22 + hyphens (`-`). A _trailer block_ consists of one or more trailers. The
23 + trailer block needs to be preceded by a blank line, where a _blank line_
24 + is either an empty or a whitespace-only line. For example, in the
25 + following commit message
20 26
21 27 ------------------------------------------------
22 28 subject
@@ -54,10 +60,18 @@ are applied to each input and the way any existing trailer in
54 60 the input is changed. They also make it possible to
55 61 automatically add some trailers.
56 62
57 - By default, a `<key>=<value>` or `<key>:<value>` argument given
58 - using `--trailer` will be appended after the existing trailers only if
59 - the last trailer has a different (_<key>_, _<value>_) pair (or if there
60 - is no existing trailer). The _<key>_ and _<value>_ parts will be trimmed
63 + Let's consider new trailers added with `--trailer`.
64 + By default, the new trailer will appear at the end of the trailer block.
65 + Also by default, this new trailer will only be added
66 + if the last trailer is different to it.
67 + A trailer block will be created with only that trailer if a trailer
68 + block does not already exist. Recall that a trailer block needs to be
69 + preceded by a blank line, so a blank line will be inserted before the
70 + new trailer block in that case.
71 +
72 + This is how the new trailer is added: a `<key>=<value>` or
73 + `<key>:<value>` argument given using `--trailer` will be appended after
74 + the existing trailers. The _<key>_ and _<value>_ parts will be trimmed
61 75 to remove starting and trailing whitespace, and the resulting trimmed
62 76 _<key>_ and _<value>_ will appear in the output like this:
63 77
@@ -68,6 +82,16 @@ key: value
68 82 This means that the trimmed _<key>_ and _<value>_ will be separated by
69 83 "`:`{nbsp}" (one colon followed by one space).
70 84
85 + Existing trailers are extracted from the input by looking for the
86 + trailer block. A trailer block is a group of one or more lines that (i)
87 + is all trailers, or (ii) contains at least one Git-generated or
88 + user-configured trailer and consists of at
89 + least 25% trailers.
90 + The trailer block is by definition at the end of the commit message.
91 + The message in turn is either (i) at the end of the input, or (ii) the
92 + last non-whitespace lines before a line that starts with `---` (followed
93 + by a space or the end of the line).
94 +
71 95 For convenience, a _<key-alias>_ can be configured to make using `--trailer`
72 96 shorter to type on the command line. This can be configured using the
73 97 `trailer.<key-alias>.key` configuration variable. The _<key-alias>_ must be a prefix
@@ -81,20 +105,6 @@ trailer.sign.key "Signed-off-by: "
81 105 in your configuration, you only need to specify `--trailer="sign: foo"`
82 106 on the command line instead of `--trailer="Signed-off-by: foo"`.
83 107
84 - By default the new trailer will appear at the end of all the existing
85 - trailers. If there is no existing trailer, the new trailer will appear
86 - at the end of the input. A blank line will be added before the new
87 - trailer if there isn't one already.
88 -
89 - Existing trailers are extracted from the input by looking for
90 - a group of one or more lines that (i) is all trailers, or (ii) contains at
91 - least one Git-generated or user-configured trailer and consists of at
92 - least 25% trailers.
93 - The group must be preceded by one or more empty (or whitespace-only) lines.
94 - The group must either be at the end of the input or be the last
95 - non-whitespace lines before a line that starts with `---` (followed by a
96 - space or the end of the line).
97 -
98 108 When reading trailers, there can be no whitespace before or inside the
99 109 _<key>_, but any number of regular space and tab characters are allowed
100 110 between the _<key>_ and the separator. There can be whitespaces before,
@@ -107,8 +117,15 @@ key: This is a very long value, with spaces and
107 117 newlines in it.
108 118 ------------------------------------------------
109 119
110 - Note that trailers do not follow (nor are they intended to follow) many of the
111 - rules for RFC 822 headers. For example they do not follow the encoding rule.
120 + OTHER RULES
121 + -----------
122 +
123 + What was covered in the previous section are the rules that are relevant
124 + for regular use. The following points are included for completeness.
125 +
126 + This command ignores comment lines (see `core.commentString` in
127 + linkgit:git-config[1]). This is for use with the `prepare-commit-msg`
128 + and `commit-msg` hooks.
112 129
113 130 OPTIONS
114 131 -------
@@ -402,6 +419,29 @@ mv "\$1.new" "\$1"
402 419 $ chmod +x .git/hooks/commit-msg
403 420 ------------
404 421
422 + * Here we try to use three different trailer keys. But it fails because
423 + two of them are not recognized as trailer keys.
424 + +
425 + ----
426 + $ cat msg.txt
427 + subject
428 +
429 + Skapad-på: some-branch
430 + Hash-in-v6.11: 45c12d3269fe48f22834320c782ffe86c3560f2c
431 + Reviewed-by: Alice <alice@example.com>
432 + $ git interpret-trailers --only-trailers <msg.txt
433 + $
434 + ----
435 + +
436 + Recall that a trailer key has to consist of only ASCII alphanumeric
437 + characters and hyphens, and this does not hold for the two first
438 + supposed trailer keys. And now none are recognized as trailers because
439 + the candidate trailer block has at least one non-trailer line, even
440 + though `Reviewed-by` is a valid trailer key. Recall that a trailer block
441 + has to either (i) be all trailers, or (ii) consist of at least one
442 + Git-generated or user-configured trailer (and some other conditions).
443 + And (ii) is not satisfied since we have not configured any trailer keys.
444 +
405 445 SEE ALSO
406 446 --------
407 447 linkgit:git-commit[1], linkgit:git-format-patch[1], linkgit:git-config[1]
modified Documentation/gitprotocol-v2.adoc
@@ -558,14 +558,17 @@ object-info
558 558
559 559 `object-info` is the command to retrieve information about one or more objects.
560 560 Its main purpose is to allow a client to make decisions based on this
561 - information without having to fully fetch objects. Object size is the only
562 - information that is currently supported.
561 + information without having to fully fetch objects. Currently only object size
562 + and type are supported.
563 563
564 564 An `object-info` request takes the following arguments:
565 565
566 566 size
567 567 Requests size information to be returned for each listed object id.
568 568
569 + type
570 + Requests type information to be returned for each listed object id.
571 +
569 572 oid <oid>
570 573 Indicates to the server an object which the client wants to obtain
571 574 information for. They must be full OIDs.
@@ -580,11 +583,18 @@ space.
580 583 info = *PKT-LINE(attr LF)
581 584 *PKT-LINE(obj-info LF)
582 585
583 - attr = "size"
586 + attr = "size" | "type"
584 587
585 588 obj-size = 1*DIGIT
586 589
587 - obj-info = obj-id [SP [obj-size]]
590 + obj-type = "blob" | "tree" | "commit" | "tag"
591 +
592 + obj-val = obj-size | obj-type
593 +
594 + obj-info = obj-id [SP [obj-val *(SP obj-val)]]
595 +
596 + The values in `obj-info` appear in the same order as the corresponding `attr`
597 + lines, with exactly one value per requested attribute.
588 598
589 599 If the server does not recognize the OID, the response will be `<oid> SP`
590 600 regardless of the number of attributes requested.
modified Documentation/technical/api-parse-options.adoc
@@ -150,8 +150,77 @@ Data Structure
150 150
151 151 The main data structure is an array of the `option` struct,
152 152 say `static struct option builtin_add_options[]`.
153 +
154 + Option flags
155 + ~~~~~~~~~~~~
156 +
157 + Each option can carry flags in the `flags` field of its `option`
158 + struct. These are per-option flags and are distinct from the
159 + `parse_options()` flags described above; they are usually set through
160 + the `OPT_*_F()` macro variants (see below) rather than by hand. They
161 + are the bitwise-or of:
162 +
163 + `PARSE_OPT_OPTARG`::
164 + The option's argument is optional, i.e. both `--option` and
165 + `--option=<value>` are accepted.
166 +
167 + `PARSE_OPT_NOARG`::
168 + The option takes no argument at all. Using `--option=<value>`
169 + is rejected.
170 +
171 + `PARSE_OPT_NONEG`::
172 + Disable the automatically generated negated `--no-option`
173 + form.
174 +
175 + `PARSE_OPT_HIDDEN`::
176 + Hide the option: it is omitted from the usage shown by
177 + `git <cmd> -h`, but is still shown by `git <cmd> --help-all`.
178 + The option is parsed as usual either way. This is meant for
179 + deprecated, advanced or otherwise uncommon options.
180 +
181 + `PARSE_OPT_LASTARG_DEFAULT`::
182 + The no-argument form is only accepted when the option is the
183 + last token on the command line; used earlier, it still
184 + requires an argument. Should not be combined with
185 + `PARSE_OPT_OPTARG`.
186 +
187 + `PARSE_OPT_NODASH`::
188 + The option is a single character without a leading dash, such
189 + as the `+` used by some commands.
190 +
191 + `PARSE_OPT_LITERAL_ARGHELP`::
192 + Use the argument help string (`argh`) verbatim in the usage
193 + output instead of surrounding it with `<>` or `[]`. Useful when
194 + `argh` already contains a hand-formatted description.
195 +
196 + `PARSE_OPT_FROM_ALIAS`::
197 + Internal flag, set on options that were expanded from a
198 + configured alias. It should not be set by callers.
199 +
200 + `PARSE_OPT_NOCOMPLETE`::
201 + Do not offer this option for completion.
202 +
203 + `PARSE_OPT_COMP_ARG`::
204 + The option's argument, rather than the option itself, is what
205 + should be completed.
206 +
207 + `PARSE_OPT_CMDMODE`::
208 + The option is one of several mutually exclusive "command mode"
209 + options that share the same variable. Using more than one of
210 + them at once is rejected.
211 +
212 + Macros
213 + ~~~~~~
214 +
153 215 There are some macros to easily define options:
154 216
217 + Many of the macros below have an `_F` variant (for example `OPT_BOOL_F`,
218 + `OPT_STRING_F`, `OPT_INTEGER_F`, `OPT_SET_INT_F`, `OPT_BIT_F` and
219 + `OPT_CALLBACK_F`) that takes an additional trailing `flags` argument.
220 + That argument is the bitwise-or of the per-option flags described in the
221 + "Option flags" section above; the non-`_F` macros are simply defined
222 + with `flags` set to `0`.
223 +
155 224 `OPT__ABBREV(&int_var)`::
156 225 Add `--abbrev[=<n>]`.
157 226
@@ -175,10 +244,21 @@ There are some macros to easily define options:
175 244 describes the group or an empty string.
176 245 Start the description with an upper-case letter.
177 246
247 + `OPT_HIDDEN_GROUP(description)`::
248 + Like `OPT_GROUP()`, but the group header carries
249 + `PARSE_OPT_HIDDEN`, so it is only shown by `--help-all` and not
250 + by `-h`. Use it to label a group that contains only hidden
251 + options, which would otherwise show an empty header under `-h`.
252 +
178 253 `OPT_BOOL(short, long, &int_var, description)`::
179 254 Introduce a boolean option. `int_var` is set to one with
180 255 `--option` and set to zero with `--no-option`.
181 256
257 + `OPT_HIDDEN_BOOL(short, long, &int_var, description)`::
258 + Like `OPT_BOOL()`, but the option carries `PARSE_OPT_HIDDEN`,
259 + so it is hidden from `-h` while still being shown by
260 + `--help-all`.
261 +
182 262 `OPT_COUNTUP(short, long, &int_var, description)`::
183 263 Introduce a count-up option.
184 264 Each use of `--option` increments `int_var`, starting from zero
modified Documentation/technical/meson.build
@@ -18,6 +18,7 @@ articles = [
18 18 'multi-pack-index.adoc',
19 19 'packfile-uri.adoc',
20 20 'pack-heuristics.adoc',
21 + 'paint-down-to-common.adoc',
21 22 'parallel-checkout.adoc',
22 23 'partial-clone.adoc',
23 24 'platform-support.adoc',
added Documentation/technical/paint-down-to-common.adoc mode 100644
@@ -0,0 +1,155 @@
1 + Merge-Base Computation and paint_down_to_common()
2 + ==================================================
3 +
4 + The function `paint_down_to_common()` in `commit-reach.c` computes merge
5 + bases by walking the commit graph backwards from two sets of tips and
6 + finding where their ancestry meets.
7 +
8 + Use cases
9 + ---------
10 +
11 + Computing merge bases is used in two different ways:
12 +
13 + 1. *Finding all merge bases* (`merge-base --all`, `merge-tree`,
14 + `merge`, `rebase`). A merge base is a common ancestor that is
15 + not itself an ancestor of another common ancestor.
16 +
17 + 2. *Ancestry checks* (`in_merge_bases`, used by `merge-base
18 + --is-ancestor`, `branch -d`, `fetch`). These ask: "is commit A
19 + an ancestor of commit B?" If a common ancestor equals one of the
20 + inputs, that input is necessarily the only merge base -- no other
21 + common ancestor can be both as recent and not an ancestor of it.
22 +
23 + Both use cases share the same algorithm and implementation.
24 +
25 + Algorithm
26 + ---------
27 +
28 + Given a commit `one` and a set of commits `twos[]`, the walk paints
29 + commits with two colors:
30 +
31 + - PARENT1: reachable from `one`
32 + - PARENT2: reachable from any commit in `twos[]`
33 +
34 + The walk uses a priority queue ordered by generation number
35 + (highest first), breaking ties by commit date. Each step dequeues
36 + the highest-priority commit and propagates its paint flags to its
37 + parents, enqueuing any parent that gained new flags. When a
38 + commit receives both PARENT1 and PARENT2, it is a merge-base
39 + candidate. A candidate gains the STALE flag so its ancestors
40 + propagate staleness -- any deeper common ancestor is necessarily
41 + redundant.
42 +
43 + [[generation-regions]]
44 + Topologically ordered and unordered generation regions
45 + ------------------------------------------------------
46 +
47 + Commits fall into two regions based on whether their generation
48 + numbers provide a topological ordering guarantee:
49 +
50 + ....
51 + +------------------------------------------+
52 + | Unordered region |
53 + | generation = INFINITY or V1_MAX |
54 + | queue order: heuristic (commit date) |
55 + +------------------------------------------+
56 + |
57 + v
58 + +------------------------------------------+
59 + | Ordered region |
60 + | generation = finite, unsaturated |
61 + | queue order: topological |
62 + +------------------------------------------+
63 + ....
64 +
65 + In the ordered region, a child's generation is strictly greater
66 + than its parent's. Same-generation commits are necessarily
67 + independent, so the queue always processes children before
68 + their parents.
69 +
70 + In the unordered region, parent-child pairs can share the same
71 + generation number, so topological order is not guaranteed. The
72 + queue uses commit-date as a heuristic, which typically produces
73 + a reasonable traversal order but may process a parent before
74 + its child.
75 +
76 + Commits not in the commit-graph have generation INFINITY; v1
77 + commit-graphs saturate at V1_MAX. Both place commits in the
78 + unordered region. Any optimization that depends on generation
79 + ordering must account for this saturation boundary. The early
80 + exit gates compare against a topological ceiling --
81 + `GENERATION_NUMBER_V1_MAX` for v1 graphs and
82 + `GENERATION_NUMBER_INFINITY` for v2 graphs -- so that saturated
83 + commits are treated as unordered.
84 +
85 + With generation ordering, values in the unordered region exceed
86 + those in the ordered region. The walk may therefore transition
87 + from the unordered region into the ordered region, but never in
88 + the reverse direction. Without a commit-graph, every commit has INFINITY
89 + and the walk operates entirely in the unordered region.
90 +
91 + In the ordered region, paint on a dequeued commit is final --
92 + no future step can add flags to it. In the unordered region,
93 + a dequeued commit may later gain additional paint. Paint flags
94 + are only added, never removed, bounding the number of
95 + re-enqueues per commit.
96 +
97 + Termination
98 + -----------
99 +
100 + The walk tracks the number of commits of each type in the queue
101 + (PARENT1-only, PARENT2-only, pending merge-base). The main loop
102 + ends when one of the following conditions holds:
103 +
104 + 1. The queue is empty.
105 + 2. The queue contains only stale entries.
106 + 3. Generation cutoff: the dequeued commit's generation is below
107 + a caller-supplied `min_generation` threshold.
108 + 4. Single result: the caller only needs one merge base, one has
109 + been found, and the walk has entered the ordered region.
110 + 5. Side exhaustion: no pure PARENT1 or pure PARENT2 commits
111 + remain in the queue, no pending merge-base candidates exist,
112 + and the walk has entered the ordered region.
113 +
114 + Stale entry condition
115 + ~~~~~~~~~~~~~~~~~~~~~
116 + Once all queued entries are stale, no new merge-base candidates can
117 + be discovered -- that requires at least one non-stale commit from
118 + each side meeting. Continuing the walk could still invalidate
119 + existing candidates by proving one is an ancestor of another, but
120 + `remove_redundant()` handles that as a post-processing step, so it
121 + is safe to exit early.
122 +
123 + Side-exhaustion condition
124 + ~~~~~~~~~~~~~~~~~~~~~~~~~
125 + A new merge-base requires commits from both sides to meet. When one
126 + side's exclusive counter reaches zero and there are no pending
127 + merge-base candidates, no future traversal step can produce a new
128 + candidate. This optimization only activates in the ordered region,
129 + where paint flags are final at visit time; in the unordered region,
130 + a side that appears exhausted could reappear through late paint
131 + propagation.
132 +
133 + Generation cutoff
134 + ~~~~~~~~~~~~~~~~~
135 + Some callers (notably `remove_redundant()`) supply a `min_generation`
136 + threshold equal to the minimum generation of the input commits.
137 + These callers only need to determine reachability among the inputs,
138 + not find deep merge bases, so the walk can safely terminate when it
139 + dequeues a commit below this threshold.
140 +
141 + Single result
142 + ~~~~~~~~~~~~~
143 + When only one merge base is needed and the walk is in the
144 + ordered region with generation ordering, the first candidate
145 + found is necessarily the highest-generation common ancestor.
146 + No remaining commit in the queue can be a descendant of this
147 + candidate (generation ordering guarantees children are visited
148 + first), so it cannot be redundant and the walk can stop
149 + immediately.
150 +
151 + Related documentation
152 + ---------------------
153 +
154 + - `Documentation/technical/commit-graph.adoc` -- generation numbers
155 + and the reachability closure property.
modified archive-tar.c
@@ -129,20 +129,20 @@ static void write_trailer(void)
129 129 */
130 130 static int stream_blocked(struct repository *r, const struct object_id *oid)
131 131 {
132 - struct odb_read_stream *st;
132 + struct odb_stream *st;
133 133 char buf[BLOCKSIZE];
134 134 ssize_t readlen;
135 135
136 - st = odb_read_stream_open(r->objects, oid, NULL);
136 + st = odb_stream_from_object(r->objects, oid, NULL);
137 137 if (!st)
138 138 return error(_("cannot stream blob %s"), oid_to_hex(oid));
139 139 for (;;) {
140 - readlen = odb_read_stream_read(st, buf, sizeof(buf));
140 + readlen = odb_stream_read(st, buf, sizeof(buf));
141 141 if (readlen <= 0)
142 142 break;
143 143 do_write_blocked(buf, readlen);
144 144 }
145 - odb_read_stream_close(st);
145 + odb_stream_close(st);
146 146 if (!readlen)
147 147 finish_record();
148 148 return readlen;
modified archive-zip.c
@@ -206,7 +206,7 @@ static void *zlib_deflate_raw(void *data, unsigned long size,
206 206 unsigned long *compressed_size)
207 207 {
208 208 git_zstream stream;
209 - unsigned long maxsize;
209 + size_t maxsize;
210 210 void *buffer;
211 211 int result;
212 212
@@ -309,7 +309,7 @@ static int write_zip_entry(struct archiver_args *args,
309 309 enum zip_method method;
310 310 unsigned char *out;
311 311 void *deflated = NULL;
312 - struct odb_read_stream *stream = NULL;
312 + struct odb_stream *stream = NULL;
313 313 unsigned long flags = 0;
314 314 int is_binary = -1;
315 315 const char *path_without_prefix = path + args->baselen;
@@ -347,7 +347,7 @@ static int write_zip_entry(struct archiver_args *args,
347 347 method = ZIP_METHOD_DEFLATE;
348 348
349 349 if (!buffer) {
350 - stream = odb_read_stream_open(args->repo->objects, oid, NULL);
350 + stream = odb_stream_from_object(args->repo->objects, oid, NULL);
351 351 if (!stream)
352 352 return error(_("cannot stream blob %s"),
353 353 oid_to_hex(oid));
@@ -428,7 +428,7 @@ static int write_zip_entry(struct archiver_args *args,
428 428 ssize_t readlen;
429 429
430 430 for (;;) {
431 - readlen = odb_read_stream_read(stream, buf, sizeof(buf));
431 + readlen = odb_stream_read(stream, buf, sizeof(buf));
432 432 if (readlen <= 0)
433 433 break;
434 434 crc = crc32(crc, buf, readlen);
@@ -438,7 +438,7 @@ static int write_zip_entry(struct archiver_args *args,
438 438 buf, readlen);
439 439 write_or_die(1, buf, readlen);
440 440 }
441 - odb_read_stream_close(stream);
441 + odb_stream_close(stream);
442 442 if (readlen)
443 443 return readlen;
444 444
@@ -461,7 +461,7 @@ static int write_zip_entry(struct archiver_args *args,
461 461 zstream.avail_out = sizeof(compressed);
462 462
463 463 for (;;) {
464 - readlen = odb_read_stream_read(stream, buf, sizeof(buf));
464 + readlen = odb_stream_read(stream, buf, sizeof(buf));
465 465 if (readlen <= 0)
466 466 break;
467 467 crc = crc32(crc, buf, readlen);
@@ -485,7 +485,7 @@ static int write_zip_entry(struct archiver_args *args,
485 485 }
486 486
487 487 }
488 - odb_read_stream_close(stream);
488 + odb_stream_close(stream);
489 489 if (readlen)
490 490 return readlen;
491 491
modified bisect.c
@@ -1020,10 +1020,12 @@ void read_bisect_terms(char **read_bad, char **read_good)
1020 1020 die_errno(_("could not read file '%s'"), filename);
1021 1021 }
1022 1022 } else {
1023 - strbuf_getline_lf(&str, fp);
1023 + if (strbuf_getline_lf(&str, fp) == EOF)
1024 + die(_("could not read bad term from file '%s'"), filename);
1024 1025 free(*read_bad);
1025 1026 *read_bad = strbuf_detach(&str, NULL);
1026 - strbuf_getline_lf(&str, fp);
1027 + if (strbuf_getline_lf(&str, fp) == EOF)
1028 + die(_("could not read good term from file '%s'"), filename);
1027 1029 free(*read_good);
1028 1030 *read_good = strbuf_detach(&str, NULL);
1029 1031 }
modified builtin/bisect.c
@@ -566,7 +566,7 @@ static int bisect_next_check(const struct bisect_terms *terms,
566 566 return decide_next(terms, current_term, !state.nr_good, !state.nr_bad);
567 567 }
568 568
569 - static int get_terms(struct bisect_terms *terms)
569 + static int get_terms(struct bisect_terms *terms, int file_missing_is_ok)
570 570 {
571 571 struct strbuf str = STRBUF_INIT;
572 572 FILE *fp = NULL;
@@ -574,14 +574,21 @@ static int get_terms(struct bisect_terms *terms)
574 574
575 575 fp = fopen(git_path_bisect_terms(), "r");
576 576 if (!fp) {
577 - res = -1;
577 + res = file_missing_is_ok ? 0 : -1;
578 578 goto finish;
579 579 }
580 580
581 581 free_terms(terms);
582 - strbuf_getline_lf(&str, fp);
582 + if (strbuf_getline_lf(&str, fp) == EOF) {
583 + res = -1;
584 + goto finish;
585 + }
583 586 terms->term_bad = strbuf_detach(&str, NULL);
584 - strbuf_getline_lf(&str, fp);
587 + if (strbuf_getline_lf(&str, fp) == EOF) {
588 + res = -1;
589 + FREE_AND_NULL(terms->term_bad);
590 + goto finish;
591 + }
585 592 terms->term_good = strbuf_detach(&str, NULL);
586 593
587 594 finish:
@@ -593,7 +600,7 @@ finish:
593 600
594 601 static int bisect_terms(struct bisect_terms *terms, const char *option)
595 602 {
596 - if (get_terms(terms))
603 + if (get_terms(terms, 0))
597 604 return error(_("no terms defined"));
598 605
599 606 if (!option) {
@@ -1157,7 +1164,8 @@ static int process_replay_line(struct bisect_terms *terms, struct strbuf *line)
1157 1164 rev = word_end + strspn(word_end, " \t");
1158 1165 *word_end = '\0'; /* NUL-terminate the word */
1159 1166
1160 - get_terms(terms);
1167 + if (get_terms(terms, 1))
1168 + return error(_("no terms defined"));
1161 1169 if (check_and_set_terms(terms, p))
1162 1170 return -1;
1163 1171
@@ -1430,7 +1438,14 @@ static int bisect_run(struct bisect_terms *terms, int argc, const char **argv)
1430 1438
1431 1439 fflush(stdout);
1432 1440 saved_stdout = dup(1);
1433 - dup2(temporary_stdout_fd, 1);
1441 + if (saved_stdout < 0 ||
1442 + dup2(temporary_stdout_fd, 1) < 0) {
1443 + res = error_errno(_("could not duplicate stdout"));
1444 + if (saved_stdout >= 0)
1445 + close(saved_stdout);
1446 + close(temporary_stdout_fd);
1447 + break;
1448 + }
1434 1449
1435 1450 res = bisect_state(terms, 1, &new_state);
1436 1451
@@ -1510,7 +1525,8 @@ static int cmd_bisect__next(int argc, const char **argv UNUSED, const char *pref
1510 1525 if (argc)
1511 1526 return error(_("'%s' requires 0 arguments"),
1512 1527 "git bisect next");
1513 - get_terms(&terms);
1528 + if (get_terms(&terms, 1))
1529 + return error(_("no terms defined"));
1514 1530 res = bisect_next(&terms, prefix);
1515 1531 free_terms(&terms);
1516 1532 return res;
@@ -1544,7 +1560,8 @@ static int cmd_bisect__skip(int argc, const char **argv, const char *prefix UNUS
1544 1560 struct bisect_terms terms = { 0 };
1545 1561
1546 1562 set_terms(&terms, "bad", "good");
1547 - get_terms(&terms);
1563 + if (get_terms(&terms, 1))
1564 + return error(_("no terms defined"));
1548 1565 res = bisect_skip(&terms, argc, argv);
1549 1566 free_terms(&terms);
1550 1567 return res;
@@ -1556,7 +1573,8 @@ static int cmd_bisect__visualize(int argc, const char **argv, const char *prefix
1556 1573 int res;
1557 1574 struct bisect_terms terms = { 0 };
1558 1575
1559 - get_terms(&terms);
1576 + if (get_terms(&terms, 1))
1577 + return error(_("no terms defined"));
1560 1578 res = bisect_visualize(&terms, argc, argv);
1561 1579 free_terms(&terms);
1562 1580 return res;
@@ -1570,7 +1588,8 @@ static int cmd_bisect__run(int argc, const char **argv, const char *prefix UNUSE
1570 1588
1571 1589 if (!argc)
1572 1590 return error(_("'%s' failed: no command provided."), "git bisect run");
1573 - get_terms(&terms);
1591 + if (get_terms(&terms, 1))
1592 + return error(_("no terms defined"));
1574 1593 res = bisect_run(&terms, argc, argv);
1575 1594 free_terms(&terms);
1576 1595 return res;
@@ -1609,7 +1628,8 @@ int cmd_bisect(int argc,
1609 1628 usage_with_options(git_bisect_usage, options);
1610 1629
1611 1630 set_terms(&terms, "bad", "good");
1612 - get_terms(&terms);
1631 + if (get_terms(&terms, 1))
1632 + return error(_("no terms defined"));
1613 1633 if (check_and_set_terms(&terms, argv[0]) ||
1614 1634 !one_of(argv[0], terms.term_good, terms.term_bad, NULL))
1615 1635 usage_msg_optf(_("unknown command: '%s'"), git_bisect_usage,
modified builtin/branch.c
@@ -21,6 +21,7 @@
21 21 #include "branch.h"
22 22 #include "path.h"
23 23 #include "string-list.h"
24 + #include "strmap.h"
24 25 #include "column.h"
25 26 #include "utf8.h"
26 27 #include "ref-filter.h"
@@ -30,7 +31,7 @@
30 31 #include "commit-reach.h"
31 32
32 33 static const char * const builtin_branch_usage[] = {
33 - N_("git branch [<options>] [-r | -a] [--merged] [--no-merged]"),
34 + N_("git branch [<options>] [-r | -a] [--merged] [--no-merged] [(--forked <branch>)...]"),
34 35 N_("git branch [<options>] [-f] [--recurse-submodules] <branch-name> [<start-point>]"),
35 36 N_("git branch [<options>] [-l] [<pattern>...]"),
36 37 N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."),
@@ -38,6 +39,8 @@ static const char * const builtin_branch_usage[] = {
38 39 N_("git branch [<options>] (-c | -C) [<old-branch>] <new-branch>"),
39 40 N_("git branch [<options>] [-r | -a] [--points-at]"),
40 41 N_("git branch [<options>] [-r | -a] [--format]"),
42 + N_("git branch [<options>] (--delete-merged <pattern>)... "
43 + "[<branch-pattern>...]"),
41 44 NULL
42 45 };
43 46
@@ -168,10 +171,13 @@ static int branch_merged(int kind, const char *name,
168 171 * upstream, if any, otherwise with HEAD", we should just
169 172 * return the result of the repo_in_merge_bases() above without
170 173 * any of the following code, but during the transition period,
171 - * a gentle reminder is in order.
174 + * a gentle reminder is in order. Callers that opt out of the
175 + * HEAD fallback by passing head_rev=NULL are not interested in
176 + * the reminder either: they have already established that the
177 + * branch has an upstream, so HEAD is irrelevant to the decision.
172 178 */
173 - if (head_rev != reference_rev) {
174 - int expect = head_rev ? repo_in_merge_bases(the_repository, rev, head_rev) : 0;
179 + if (head_rev && head_rev != reference_rev) {
180 + int expect = repo_in_merge_bases(the_repository, rev, head_rev);
175 181 if (expect < 0)
176 182 exit(128);
177 183 if (expect == merged)
@@ -189,20 +195,32 @@ static int branch_merged(int kind, const char *name,
189 195 return merged;
190 196 }
191 197
198 + enum delete_branch_flags {
199 + DELETE_BRANCH_FORCE = (1 << 0),
200 + DELETE_BRANCH_QUIET = (1 << 1),
201 + DELETE_BRANCH_SKIP_UNMERGED = (1 << 2),
202 + DELETE_BRANCH_NO_HEAD_FALLBACK = (1 << 3),
203 + DELETE_BRANCH_DRY_RUN = (1 << 4),
204 + };
205 +
192 206 static int check_branch_commit(const char *branchname, const char *refname,
193 207 const struct object_id *oid, struct commit *head_rev,
194 - int kinds, int force)
208 + int kinds, unsigned int flags)
195 209 {
196 210 struct commit *rev = lookup_commit_reference(the_repository, oid);
197 - if (!force && !rev) {
211 + if (!(flags & DELETE_BRANCH_FORCE) && !rev) {
198 212 error(_("couldn't look up commit object for '%s'"), refname);
199 213 return -1;
200 214 }
201 - if (!force && !branch_merged(kinds, branchname, rev, head_rev)) {
202 - error(_("the branch '%s' is not fully merged"), branchname);
203 - advise_if_enabled(ADVICE_FORCE_DELETE_BRANCH,
204 - _("If you are sure you want to delete it, "
205 - "run 'git branch -D %s'"), branchname);
215 + if (!(flags & DELETE_BRANCH_FORCE) &&
216 + !branch_merged(kinds, branchname, rev, head_rev)) {
217 + if (!(flags & DELETE_BRANCH_SKIP_UNMERGED)) {
218 + error(_("the branch '%s' is not fully merged"),
219 + branchname);
220 + advise_if_enabled(ADVICE_FORCE_DELETE_BRANCH,
221 + _("If you are sure you want to delete it, "
222 + "run 'git branch -D %s'"), branchname);
223 + }
206 224 return -1;
207 225 }
208 226 return 0;
@@ -217,8 +235,8 @@ static void delete_branch_config(const char *branchname)
217 235 strbuf_release(&buf);
218 236 }
219 237
220 - static int delete_branches(int argc, const char **argv, int force, int kinds,
221 - int quiet)
238 + static int delete_branches(int argc, const char **argv, int kinds,
239 + unsigned int flags)
222 240 {
223 241 struct commit *head_rev = NULL;
224 242 struct object_id oid;
@@ -241,7 +259,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
241 259 remote_branch = 1;
242 260 allowed_interpret = INTERPRET_BRANCH_REMOTE;
243 261
244 - force = 1;
262 + flags |= DELETE_BRANCH_FORCE;
245 263 break;
246 264 case FILTER_REFS_BRANCHES:
247 265 fmt = "refs/heads/%s";
@@ -252,12 +270,13 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
252 270 }
253 271 branch_name_pos = strcspn(fmt, "%");
254 272
255 - if (!force)
273 + if (!(flags & DELETE_BRANCH_FORCE) &&
274 + !(flags & DELETE_BRANCH_NO_HEAD_FALLBACK))
256 275 head_rev = lookup_commit_reference(the_repository, &head_oid);
257 276
258 277 for (i = 0; i < argc; i++, strbuf_reset(&bname)) {
259 278 char *target = NULL;
260 - int flags = 0;
279 + int ref_flags = 0;
261 280
262 281 copy_branchname(the_repository, &bname,
263 282 argv[i], allowed_interpret);
@@ -287,7 +306,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
287 306 RESOLVE_REF_READING
288 307 | RESOLVE_REF_NO_RECURSE
289 308 | RESOLVE_REF_ALLOW_BAD_NAME,
290 - &oid, &flags);
309 + &oid, &ref_flags);
291 310 if (!target) {
292 311 if (remote_branch) {
293 312 error(_("remote-tracking branch '%s' not found"), bname.buf);
@@ -299,7 +318,7 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
299 318 | RESOLVE_REF_NO_RECURSE
300 319 | RESOLVE_REF_ALLOW_BAD_NAME,
301 320 &oid,
302 - &flags);
321 + &ref_flags);
303 322 FREE_AND_NULL(virtual_name);
304 323
305 324 if (virtual_target)
@@ -314,31 +333,39 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
314 333 continue;
315 334 }
316 335
317 - if (!(flags & (REF_ISSYMREF|REF_ISBROKEN)) &&
336 + if (!(ref_flags & (REF_ISSYMREF|REF_ISBROKEN)) &&
318 337 check_branch_commit(bname.buf, name, &oid, head_rev, kinds,
319 - force)) {
320 - ret = 1;
338 + flags)) {
339 + if (!(flags & DELETE_BRANCH_SKIP_UNMERGED))
340 + ret = 1;
321 341 goto next;
322 342 }
323 343
324 344 item = string_list_append(&refs_to_delete, name);
325 - item->util = xstrdup((flags & REF_ISBROKEN) ? "broken"
326 - : (flags & REF_ISSYMREF) ? target
345 + item->util = xstrdup((ref_flags & REF_ISBROKEN) ? "broken"
346 + : (ref_flags & REF_ISSYMREF) ? target
327 347 : repo_find_unique_abbrev(the_repository, &oid, DEFAULT_ABBREV));
328 348
329 349 next:
330 350 free(target);
331 351 }
332 352
333 - if (refs_delete_refs(get_main_ref_store(the_repository), NULL, &refs_to_delete, REF_NO_DEREF))
353 + if (!(flags & DELETE_BRANCH_DRY_RUN) &&
354 + refs_delete_refs(get_main_ref_store(the_repository), NULL, &refs_to_delete, REF_NO_DEREF))
334 355 ret = 1;
335 356
336 357 for_each_string_list_item(item, &refs_to_delete) {
337 358 char *describe_ref = item->util;
338 359 char *name = item->string;
339 - if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {
360 + if (flags & DELETE_BRANCH_DRY_RUN) {
361 + if (!(flags & DELETE_BRANCH_QUIET))
362 + printf(remote_branch
363 + ? _("Would delete remote-tracking branch %s (was %s).\n")
364 + : _("Would delete branch %s (was %s).\n"),
365 + name + branch_name_pos, describe_ref);
366 + } else if (!refs_ref_exists(get_main_ref_store(the_repository), name)) {
340 367 char *refname = name + branch_name_pos;
341 - if (!quiet)
368 + if (!(flags & DELETE_BRANCH_QUIET))
342 369 printf(remote_branch
343 370 ? _("Deleted remote-tracking branch %s (was %s).\n")
344 371 : _("Deleted branch %s (was %s).\n"),
@@ -681,6 +708,208 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int
681 708 free_worktrees(worktrees);
682 709 }
683 710
711 + static int parse_opt_forked(const struct option *opt, const char *arg, int unset)
712 + {
713 + struct ref_filter *filter = opt->value;
714 +
715 + BUG_ON_OPT_NEG(unset);
716 + if (ref_filter_forked_add(filter, arg) < 0)
717 + die(_("'%s' is not a valid branch or pattern"), arg);
718 + return 0;
719 + }
720 +
721 + struct stacked_branch_data {
722 + struct strset *deletable_branch_names;
723 + struct strset *protected_branch_names;
724 + };
725 +
726 + static int collect_stacked_branch_base(const struct reference *ref,
727 + void *cb_data)
728 + {
729 + struct stacked_branch_data *data = cb_data;
730 + const char *branch_name;
731 + struct branch *branch;
732 + const char *upstream_refname;
733 + const char *upstream_branch_name;
734 +
735 + if (!skip_prefix(ref->name, "refs/heads/", &branch_name))
736 + BUG("expected local branch ref, got '%s'", ref->name);
737 + if (strset_contains(data->deletable_branch_names, branch_name))
738 + return 0;
739 +
740 + branch = branch_get(branch_name);
741 + upstream_refname = branch_get_upstream(branch, NULL);
742 + if (!upstream_refname ||
743 + !skip_prefix(upstream_refname, "refs/heads/",
744 + &upstream_branch_name) ||
745 + !strset_contains(data->deletable_branch_names,
746 + upstream_branch_name))
747 + return 0;
748 +
749 + strset_add(data->protected_branch_names, upstream_branch_name);
750 + return 0;
751 + }
752 +
753 + static void protect_stacked_branch_bases(struct ref_store *refs,
754 + struct strset *deletable_branch_names,
755 + struct strset *protected_branch_names)
756 + {
757 + struct stacked_branch_data data = {
758 + .deletable_branch_names = deletable_branch_names,
759 + .protected_branch_names = protected_branch_names,
760 + };
761 + struct refs_for_each_ref_options opts = {
762 + .prefix = "refs/heads/",
763 + };
764 + struct hashmap_iter iter;
765 + struct strmap_entry *entry;
766 +
767 + refs_for_each_ref_ext(refs, collect_stacked_branch_base, &data, &opts);
768 +
769 + strset_for_each_entry(protected_branch_names, &iter, entry)
770 + strset_remove(deletable_branch_names, entry->key);
771 + }
772 +
773 + static void clear_deleted_upstreams(struct strset *protected_branch_names,
774 + struct strset *deletable_branch_names)
775 + {
776 + struct strbuf key = STRBUF_INIT;
777 + struct hashmap_iter iter;
778 + struct strmap_entry *entry;
779 +
780 + strset_for_each_entry(protected_branch_names, &iter, entry) {
781 + struct branch *branch = branch_get(entry->key);
782 + const char *upstream_refname = branch_get_upstream(branch, NULL);
783 + const char *upstream_branch_name;
784 +
785 + if (!upstream_refname ||
786 + !skip_prefix(upstream_refname, "refs/heads/",
787 + &upstream_branch_name) ||
788 + !strset_contains(deletable_branch_names,
789 + upstream_branch_name))
790 + continue;
791 +
792 + strbuf_addf(&key, "branch.%s.merge", branch->name);
793 + repo_config_set_gently(the_repository, key.buf, NULL);
794 + strbuf_reset(&key);
795 + strbuf_addf(&key, "branch.%s.remote", branch->name);
796 + repo_config_set_gently(the_repository, key.buf, NULL);
797 + strbuf_reset(&key);
798 + }
799 +
800 + strbuf_release(&key);
801 + }
802 +
803 + static int branch_pushes_to_upstream(struct branch *branch,
804 + const char *upstream)
805 + {
806 + struct remote *remote = remote_get(remote_for_branch(branch, NULL));
807 + char *push_refname = NULL;
808 + char *tracking = NULL;
809 + int ret = 0;
810 +
811 + if (!remote)
812 + return 0;
813 + if (remote->push.nr)
814 + push_refname = apply_refspecs(&remote->push, branch->refname);
815 + else
816 + push_refname = xstrdup(branch->refname);
817 + if (push_refname)
818 + tracking = apply_refspecs(&remote->fetch, push_refname);
819 + if (tracking && !strcmp(tracking, upstream))
820 + ret = 1;
821 +
822 + free(push_refname);
823 + free(tracking);
824 + return ret;
825 + }
826 +
827 + static int delete_merged_branches(const struct strvec *upstreams,
828 + const char **argv, unsigned int flags)
829 + {
830 + struct ref_store *refs = get_main_ref_store(the_repository);
831 + struct ref_filter filter = REF_FILTER_INIT;
832 + struct ref_array candidates = { 0 };
833 + struct strset deletable_branch_names = STRSET_INIT;
834 + struct strset protected_branch_names = STRSET_INIT;
835 + struct strvec branches_to_delete = STRVEC_INIT;
836 + struct strbuf key = STRBUF_INIT;
837 + struct hashmap_iter iter;
838 + struct strmap_entry *entry;
839 + int ret = 0;
840 +
841 + for (size_t i = 0; i < upstreams->nr; i++)
842 + if (ref_filter_forked_add(&filter, upstreams->v[i]) < 0)
843 + die(_("'%s' is not a valid branch or pattern"),
844 + upstreams->v[i]);
845 +
846 + filter.kind = FILTER_REFS_BRANCHES;
847 + filter.name_patterns = argv;
848 + filter_refs(&candidates, &filter, filter.kind);
849 +
850 + for (int i = 0; i < candidates.nr; i++) {
851 + const char *branch_refname = candidates.items[i]->refname;
852 + const char *branch_name;
853 + struct branch *branch;
854 + const char *upstream_refname;
855 + int opt_out;
856 +
857 + if (!skip_prefix(branch_refname, "refs/heads/", &branch_name))
858 + BUG("filter returned non-branch ref '%s'", branch_refname);
859 + if (branch_checked_out(branch_refname))
860 + continue;
861 +
862 + branch = branch_get(branch_name);
863 + upstream_refname = branch_get_upstream(branch, NULL);
864 + if (!upstream_refname || !refs_ref_exists(refs, upstream_refname))
865 + continue;
866 + if (branch_pushes_to_upstream(branch, upstream_refname))
867 + continue;
868 + if (check_branch_commit(branch_name, branch_name,
869 + &candidates.items[i]->objectname, NULL,
870 + FILTER_REFS_BRANCHES, DELETE_BRANCH_SKIP_UNMERGED))
871 + continue;
872 +
873 + strbuf_reset(&key);
874 + strbuf_addf(&key, "branch.%s.deletemerged", branch_name);
875 + if (!repo_config_get_bool(the_repository, key.buf, &opt_out) &&
876 + !opt_out) {
877 + if (!(flags & DELETE_BRANCH_QUIET))
878 + fprintf(stderr,
879 + _("Skipping '%s' (branch.%s.deleteMerged is false)\n"),
880 + branch_name, branch_name);
881 + continue;
882 + }
883 +
884 + strset_add(&deletable_branch_names, branch_name);
885 + }
886 +
887 + protect_stacked_branch_bases(refs, &deletable_branch_names,
888 + &protected_branch_names);
889 +
890 + strset_for_each_entry(&deletable_branch_names, &iter, entry)
891 + strvec_push(&branches_to_delete, entry->key);
892 +
893 + if (branches_to_delete.nr)
894 + ret = delete_branches(branches_to_delete.nr, branches_to_delete.v,
895 + FILTER_REFS_BRANCHES,
896 + DELETE_BRANCH_SKIP_UNMERGED |
897 + DELETE_BRANCH_NO_HEAD_FALLBACK |
898 + flags);
899 +
900 + if (!ret && !(flags & DELETE_BRANCH_DRY_RUN))
901 + clear_deleted_upstreams(&protected_branch_names,
902 + &deletable_branch_names);
903 +
904 + strbuf_release(&key);
905 + strvec_clear(&branches_to_delete);
906 + strset_clear(&protected_branch_names);
907 + strset_clear(&deletable_branch_names);
908 + ref_array_clear(&candidates);
909 + ref_filter_clear(&filter);
910 + return ret;
911 + }
912 +
684 913 static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION")
685 914
686 915 static int edit_branch_description(const char *branch_name)
@@ -745,6 +974,8 @@ int cmd_branch(int argc,
745 974 /* possible actions */
746 975 int delete = 0, rename = 0, copy = 0, list = 0,
747 976 unset_upstream = 0, show_current = 0, edit_description = 0;
977 + struct strvec delete_merged = STRVEC_INIT;
978 + int dry_run = 0;
748 979 const char *new_upstream = NULL;
749 980 int noncreate_actions = 0;
750 981 /* possible options */
@@ -798,9 +1029,17 @@ int cmd_branch(int argc,
798 1029 OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")),
799 1030 OPT_BOOL(0, "edit-description", &edit_description,
800 1031 N_("edit the description for the branch")),
1032 + OPT_CALLBACK_F(0, "delete-merged", &delete_merged, N_("pattern"),
1033 + N_("delete merged branches whose upstream matches <pattern> (repeatable)"),
1034 + PARSE_OPT_NONEG, parse_opt_strvec),
1035 + OPT_BOOL(0, "dry-run", &dry_run,
1036 + N_("with --delete-merged, only print which branches would be deleted")),
801 1037 OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE),
802 1038 OPT_MERGED(&filter, N_("print only branches that are merged")),
803 1039 OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
1040 + OPT_CALLBACK_F(0, "forked", &filter, N_("branch"),
1041 + N_("print only branches whose upstream matches <branch> (repeatable)"),
1042 + PARSE_OPT_NONEG, parse_opt_forked),
804 1043 OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")),
805 1044 OPT_REF_SORT(&sorting_options),
806 1045 OPT_CALLBACK(0, "points-at", &filter.points_at, N_("object"),
@@ -842,19 +1081,24 @@ int cmd_branch(int argc,
842 1081 0);
843 1082
844 1083 if (!delete && !rename && !copy && !edit_description && !new_upstream &&
845 - !show_current && !unset_upstream && argc == 0)
1084 + !show_current && !unset_upstream && !delete_merged.nr &&
1085 + argc == 0)
846 1086 list = 1;
847 1087
848 1088 if (filter.with_commit || filter.no_commit ||
849 - filter.reachable_from || filter.unreachable_from || filter.points_at.nr)
1089 + filter.reachable_from || filter.unreachable_from ||
1090 + filter.points_at.nr || filter.forked.nr)
850 1091 list = 1;
851 1092
852 1093 noncreate_actions = !!delete + !!rename + !!copy + !!new_upstream +
853 1094 !!show_current + !!list + !!edit_description +
854 - !!unset_upstream;
1095 + !!unset_upstream + !!delete_merged.nr;
855 1096 if (noncreate_actions > 1)
856 1097 usage_with_options(builtin_branch_usage, options);
857 1098
1099 + if (dry_run && !delete_merged.nr)
1100 + die(_("--dry-run requires --delete-merged"));
1101 +
858 1102 if (recurse_submodules_explicit) {
859 1103 if (!submodule_propagate_branches)
860 1104 die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled"));
@@ -889,7 +1133,14 @@ int cmd_branch(int argc,
889 1133 if (delete) {
890 1134 if (!argc)
891 1135 die(_("branch name required"));
892 - ret = delete_branches(argc, argv, delete > 1, filter.kind, quiet);
1136 + ret = delete_branches(argc, argv, filter.kind,
1137 + (delete > 1 ? DELETE_BRANCH_FORCE : 0) |
1138 + (quiet ? DELETE_BRANCH_QUIET : 0));
1139 + goto out;
1140 + } else if (delete_merged.nr) {
1141 + ret = delete_merged_branches(&delete_merged, argv,
1142 + (quiet ? DELETE_BRANCH_QUIET : 0) |
1143 + (dry_run ? DELETE_BRANCH_DRY_RUN : 0));
893 1144 goto out;
894 1145 } else if (show_current) {
895 1146 print_current_branch_name();
@@ -1062,6 +1313,7 @@ int cmd_branch(int argc,
1062 1313 ret = 0;
1063 1314
1064 1315 out:
1316 + strvec_clear(&delete_merged);
1065 1317 string_list_clear(&sorting_options, 0);
1066 1318 return ret;
1067 1319 }
modified builtin/bundle.c
@@ -68,32 +68,25 @@ static int parse_options_cmd_bundle(int argc,
68 68 }
69 69
70 70 static int cmd_bundle_create(int argc, const char **argv, const char *prefix,
71 - struct repository *repo UNUSED) {
72 - struct strvec pack_opts = STRVEC_INIT;
71 + struct repository *repo UNUSED)
72 + {
73 + int progress = isatty(STDERR_FILENO);
73 74 int version = -1;
74 - int ret;
75 75 struct option options[] = {
76 - OPT_PASSTHRU_ARGV('q', "quiet", &pack_opts, NULL,
77 - N_("do not show progress meter"),
78 - PARSE_OPT_NOARG),
79 - OPT_PASSTHRU_ARGV(0, "progress", &pack_opts, NULL,
80 - N_("show progress meter"),
81 - PARSE_OPT_NOARG),
82 - OPT_PASSTHRU_ARGV(0, "all-progress", &pack_opts, NULL,
83 - N_("historical; same as --progress"),
84 - PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
85 - OPT_PASSTHRU_ARGV(0, "all-progress-implied", &pack_opts, NULL,
86 - N_("historical; does nothing"),
87 - PARSE_OPT_NOARG | PARSE_OPT_HIDDEN),
76 + OPT_NEGBIT('q', "quiet", &progress,
77 + N_("do not show progress meter"), 1),
78 + OPT_BIT(0, "progress", &progress,
79 + N_("show progress meter"), 1),
80 + OPT_BIT_F(0, "all-progress", &progress,
81 + N_("historical; same as --progress"), 1,
82 + PARSE_OPT_HIDDEN),
83 + OPT_NOOP_NOARG(0, "all-progress-implied"),
88 84 OPT_INTEGER(0, "version", &version,
89 85 N_("specify bundle format version")),
90 86 OPT_END()
91 87 };
92 88 char *bundle_file;
93 -
94 - if (isatty(STDERR_FILENO))
95 - strvec_push(&pack_opts, "--progress");
96 - strvec_push(&pack_opts, "--all-progress-implied");
89 + int ret;
97 90
98 91 argc = parse_options_cmd_bundle(argc, argv, prefix,
99 92 builtin_bundle_create_usage, options, &bundle_file);
@@ -101,8 +94,7 @@ static int cmd_bundle_create(int argc, const char **argv, const char *prefix,
101 94
102 95 if (!startup_info->have_repository)
103 96 die(_("Need a repository to create a bundle."));
104 - ret = !!create_bundle(the_repository, bundle_file, argc, argv, &pack_opts, version);
105 - strvec_clear(&pack_opts);
97 + ret = !!create_bundle(the_repository, bundle_file, argc, argv, version, progress);
106 98 free(bundle_file);
107 99 return ret;
108 100 }
modified builtin/cat-file.c
@@ -31,6 +31,7 @@
31 31 #include "alias.h"
32 32 #include "remote.h"
33 33 #include "transport.h"
34 + #include "fetch-object-info.h"
34 35
35 36 /*
36 37 * Maximum length for a remote URL. While no universal standard exists,
@@ -681,9 +682,8 @@ out:
681 682
682 683 static int get_remote_info(int argc,
683 684 const char **argv,
684 - struct object_info **remote_object_info,
685 - struct oid_array *object_info_oids,
686 - struct string_list *object_info_options)
685 + struct fetch_object_info_results *results,
686 + struct oid_array *object_info_oids)
687 687 {
688 688 int retval = 0;
689 689 struct remote *remote = NULL;
@@ -724,12 +724,8 @@ static int get_remote_info(int argc,
724 724 goto cleanup;
725 725 }
726 726
727 - CALLOC_ARRAY(*remote_object_info, object_info_oids->nr);
728 - gtransport->smart_options->object_info_oids = object_info_oids;
729 -
730 - gtransport->smart_options->object_info_options = object_info_options;
731 - gtransport->smart_options->object_info_data = *remote_object_info;
732 - retval = transport_fetch_object_info(gtransport);
727 + retval = transport_fetch_object_info(gtransport, object_info_oids,
728 + results);
733 729 cleanup:
734 730 transport_disconnect(gtransport);
735 731 return retval;
@@ -819,21 +815,6 @@ static void parse_cmd_mailmap(struct batch_options *opt UNUSED,
819 815 load_mailmap();
820 816 }
821 817
822 - struct protocol_placeholder_entry {
823 - const char *option;
824 - const char *atom;
825 - };
826 -
827 - static const struct protocol_placeholder_entry remote_atom_map[] = {
828 - {"size", "objectsize"},
829 - {"type", "objecttype"},
830 - /*
831 - * Add new protocol options here. Even if the server doesn't support
832 - * them the allow_list will drop them if the server doesn't advertise
833 - * them.
834 - */
835 - };
836 -
837 818 static void parse_cmd_remote_object_info(struct batch_options *opt,
838 819 const char *line, struct strbuf *output,
839 820 struct expand_data *data)
@@ -841,18 +822,11 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
841 822 int count;
842 823 const char **argv;
843 824 char *line_to_split;
844 - struct object_info *remote_object_info = NULL;
825 + struct fetch_object_info_results results = FETCH_OBJECT_INFO_RESULTS_INIT;
845 826 struct oid_array object_info_oids = OID_ARRAY_INIT;
846 - struct string_list object_info_options = STRING_LIST_INIT_NODUP;
847 - const char *saved_format = opt->format;
848 827
849 828 if (strlen(line) >= MAX_REMOTE_OBJ_INFO_LINE)
850 829 die(_("remote-object-info command too long"));
851 - /*
852 - * TODO: Use the default format once %(objecttype) is supported.
853 - */
854 - if (!opt->format)
855 - opt->format = "%(objectname) %(objectsize)";
856 830
857 831 line_to_split = xstrdup(line);
858 832 count = split_cmdline(line_to_split, &argv);
@@ -864,26 +838,25 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
864 838 MAX_ALLOWED_OBJ_LIMIT);
865 839
866 840 if (data->info.sizep)
867 - string_list_append(&object_info_options, "size");
841 + results.wants_size = 1;
868 842 if (data->info.typep)
869 - string_list_append(&object_info_options, "type");
843 + results.wants_type = 1;
870 844
871 - if (get_remote_info(count, argv, &remote_object_info,
872 - &object_info_oids, &object_info_options))
845 + if (get_remote_info(count, argv, &results, &object_info_oids))
873 846 die(_("failed to get object info from the remote: %s"), argv[0]);
874 847
875 848 string_list_clear(&data->remote_allowed_atoms, 0);
876 849 string_list_append(&data->remote_allowed_atoms, "objectname");
877 - for (size_t i = 0; i < ARRAY_SIZE(remote_atom_map); i++)
878 - if (unsorted_string_list_has_string(&object_info_options, remote_atom_map[i].option))
879 - string_list_append(&data->remote_allowed_atoms,
880 - remote_atom_map[i].atom);
850 + if (results.sizes)
851 + string_list_append(&data->remote_allowed_atoms, "objectsize");
852 + if (results.types)
853 + string_list_append(&data->remote_allowed_atoms, "objecttype");
881 854
882 855 data->skip_object_info = 1;
883 - for (size_t i = 0; i < object_info_oids.nr; i++) {
856 + for (size_t i = 0; i < results.nr; i++) {
884 857 data->oid = object_info_oids.oid[i];
885 858
886 - if (remote_object_info[i].unrecognized) {
859 + if (results.unrecognized[i]) {
887 860 report_object_status(opt, oid_to_hex(&data->oid),
888 861 &data->oid, "missing");
889 862 continue;
@@ -893,13 +866,11 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
893 866 * When reaching here, it means remote-object-info can retrieve
894 867 * information from server without downloading them.
895 868 */
896 - if (remote_object_info[i].sizep) {
897 - data->size = *remote_object_info[i].sizep;
898 - }
869 + if (results.sizes)
870 + data->size = results.sizes[i];
899 871
900 - if (remote_object_info[i].typep) {
901 - data->type = *remote_object_info[i].typep;
902 - }
872 + if (results.types)
873 + data->type = results.types[i];
903 874
904 875 opt->batch_mode = BATCH_MODE_INFO;
905 876 data->is_remote = 1;
@@ -907,14 +878,10 @@ static void parse_cmd_remote_object_info(struct batch_options *opt,
907 878 data->is_remote = 0;
908 879 }
909 880 data->skip_object_info = 0;
910 - opt->format = saved_format;
911 881
912 - for (size_t i = 0; i < object_info_oids.nr; i++)
913 - free_object_info_contents(&remote_object_info[i]);
914 - string_list_clear(&object_info_options, 0);
882 + free_fetch_object_info_results(&results);
915 883 free(line_to_split);
916 884 free(argv);
917 - free(remote_object_info);
918 885 oid_array_clear(&object_info_oids);
919 886 }
920 887
modified builtin/config.c
@@ -1313,7 +1313,10 @@ static int show_editor(struct config_location_options *opts)
1313 1313 else if (errno != EEXIST)
1314 1314 die_errno(_("cannot create configuration file %s"), config_file);
1315 1315 }
1316 - launch_editor(config_file, NULL, NULL);
1316 + if (launch_editor(config_file, NULL, NULL)) {
1317 + free(config_file);
1318 + return -1;
1319 + }
1317 1320 free(config_file);
1318 1321
1319 1322 return 0;
modified builtin/fast-import.c
@@ -30,6 +30,7 @@
30 30 #include "khash.h"
31 31 #include "date.h"
32 32 #include "gpg-interface.h"
33 + #include "parse-options.h"
33 34
34 35 #define PACK_ID_BITS 16
35 36 #define MAX_PACK_ID ((1<<PACK_ID_BITS)-1)
@@ -182,12 +183,8 @@ static unsigned long branch_count;
182 183 static unsigned long branch_load_count;
183 184 static int failure;
184 185 static FILE *pack_edges;
185 - static unsigned int show_stats = 1;
186 - static unsigned int quiet;
187 - static int global_argc;
188 - static const char **global_argv;
189 - static const char *global_prefix;
190 -
186 + static int show_stats = 1;
187 + static int quiet;
191 188 static enum sign_mode signed_tag_mode = SIGN_VERBATIM;
192 189 static enum sign_mode signed_commit_mode = SIGN_VERBATIM;
193 190 static const char *signed_commit_keyid;
@@ -261,9 +258,7 @@ static struct recent_command *rc_free;
261 258 static unsigned int cmd_save = 100;
262 259 static uintmax_t next_mark;
263 260 static struct strbuf new_data = STRBUF_INIT;
264 - static int seen_data_command;
265 261 static int require_explicit_termination;
266 - static int allow_unsafe_features;
267 262
268 263 /* Signal handling */
269 264 static volatile sig_atomic_t checkpoint_requested;
@@ -276,10 +271,33 @@ static kh_oid_map_t *sub_oid_map;
276 271 /* Where to write output of cat-blob commands */
277 272 static int cat_blob_fd = STDOUT_FILENO;
278 273
279 - static void parse_argv(void);
280 - static void parse_get_mark(const char *p);
281 - static void parse_cat_blob(const char *p);
282 - static void parse_ls(const char *p, struct branch *b);
274 + /* Command state */
275 + struct fast_import_state {
276 + int argc;
277 + const char **argv;
278 + const char *prefix;
279 + int seen_data_command;
280 + int allow_unsafe_features;
281 + struct option *option;
282 + };
283 +
284 + static void fast_import_state_init(struct fast_import_state *state,
285 + int argc, const char **argv,
286 + const char *prefix, struct option *option)
287 + {
288 + memset(state, 0, sizeof(*state));
289 + state->argc = argc;
290 + state->argv = argv;
291 + state->prefix = prefix;
292 + state->option = option;
293 + }
294 +
295 + static void parse_argv(struct fast_import_state *state);
296 + static void parse_get_mark(struct fast_import_state *state, const char *p);
297 + static void parse_cat_blob(struct fast_import_state *state, const char *p);
298 + static void parse_ls(struct fast_import_state *state,
299 + const char *p,
300 + struct branch *b);
283 301
284 302 static void for_each_mark(struct mark_set *m, uintmax_t base, each_mark_fn_t callback, void *p)
285 303 {
@@ -962,7 +980,7 @@ static int store_object(
962 980 struct object_entry *e;
963 981 unsigned char hdr[96];
964 982 struct object_id oid;
965 - unsigned long hdrlen, deltalen;
983 + unsigned long hdrlen, deltalen = 0;
966 984 struct git_hash_ctx c;
967 985 git_zstream s;
968 986 struct repo_config_values *cfg = repo_config_values(the_repository);
@@ -998,11 +1016,13 @@ static int store_object(
998 1016
999 1017 if (last && last->data.len && last->data.buf && last->depth < max_depth
1000 1018 && dat->len > the_hash_algo->rawsz) {
1019 + size_t deltalen_st;
1001 1020
1002 1021 delta_count_attempts_by_type[type]++;
1003 1022 delta = diff_delta(last->data.buf, last->data.len,
1004 1023 dat->buf, dat->len,
1005 - &deltalen, dat->len - the_hash_algo->rawsz);
1024 + &deltalen_st, dat->len - the_hash_algo->rawsz);
1025 + deltalen = cast_size_t_to_ulong(deltalen_st);
1006 1026 } else
1007 1027 delta = NULL;
1008 1028
@@ -1845,7 +1865,7 @@ done:
1845 1865 }
1846 1866
1847 1867
1848 - static int read_next_command(void)
1868 + static int read_next_command(struct fast_import_state *state)
1849 1869 {
1850 1870 static int stdin_eof = 0;
1851 1871
@@ -1864,10 +1884,10 @@ static int read_next_command(void)
1864 1884 if (stdin_eof)
1865 1885 return EOF;
1866 1886
1867 - if (!seen_data_command
1887 + if (!state->seen_data_command
1868 1888 && !starts_with(command_buf.buf, "feature ")
1869 1889 && !starts_with(command_buf.buf, "option ")) {
1870 - parse_argv();
1890 + parse_argv(state);
1871 1891 }
1872 1892
1873 1893 rc = rc_free;
@@ -1899,22 +1919,22 @@ static void skip_optional_lf(void)
1899 1919 ungetc(term_char, stdin);
1900 1920 }
1901 1921
1902 - static void parse_mark(void)
1922 + static void parse_mark(struct fast_import_state *state)
1903 1923 {
1904 1924 const char *v;
1905 1925 if (skip_prefix(command_buf.buf, "mark :", &v)) {
1906 1926 next_mark = strtoumax(v, NULL, 10);
1907 - read_next_command();
1927 + read_next_command(state);
1908 1928 }
1909 1929 else
1910 1930 next_mark = 0;
1911 1931 }
1912 1932
1913 - static void parse_original_identifier(void)
1933 + static void parse_original_identifier(struct fast_import_state *state)
1914 1934 {
1915 1935 const char *v;
1916 1936 if (skip_prefix(command_buf.buf, "original-oid ", &v))
1917 - read_next_command();
1937 + read_next_command(state);
1918 1938 }
1919 1939
1920 1940 static int parse_data(struct strbuf *sb, uintmax_t limit, uintmax_t *len_res)
@@ -2068,11 +2088,11 @@ static void parse_and_store_blob(
2068 2088 }
2069 2089 }
2070 2090
2071 - static void parse_new_blob(void)
2091 + static void parse_new_blob(struct fast_import_state *state)
2072 2092 {
2073 - read_next_command();
2074 - parse_mark();
2075 - parse_original_identifier();
2093 + read_next_command(state);
2094 + parse_mark(state);
2095 + parse_original_identifier(state);
2076 2096 parse_and_store_blob(&last_blob, NULL, next_mark);
2077 2097 }
2078 2098
@@ -2368,7 +2388,9 @@ static void parse_path_space(struct strbuf *sb, const char *p,
2368 2388 (*endp)++;
2369 2389 }
2370 2390
2371 - static void file_change_m(const char *p, struct branch *b)
2391 + static void file_change_m(struct fast_import_state *state,
2392 + const char *p,
2393 + struct branch *b)
2372 2394 {
2373 2395 static struct strbuf path = STRBUF_INIT;
2374 2396 struct object_entry *oe;
@@ -2435,10 +2457,10 @@ static void file_change_m(const char *p, struct branch *b)
2435 2457 if (S_ISDIR(mode))
2436 2458 die(_("directories cannot be specified 'inline': %s"),
2437 2459 command_buf.buf);
2438 - while (read_next_command() != EOF) {
2460 + while (read_next_command(state) != EOF) {
2439 2461 const char *v;
2440 2462 if (skip_prefix(command_buf.buf, "cat-blob ", &v))
2441 - parse_cat_blob(v);
2463 + parse_cat_blob(state, v);
2442 2464 else {
2443 2465 parse_and_store_blob(&last_blob, &oid, 0);
2444 2466 break;
@@ -2512,7 +2534,10 @@ static void file_change_cr(const char *p, struct branch *b, int rename)
2512 2534 leaf.tree);
2513 2535 }
2514 2536
2515 - static void note_change_n(const char *p, struct branch *b, unsigned char *old_fanout)
2537 + static void note_change_n(struct fast_import_state *state,
2538 + const char *p,
2539 + struct branch *b,
2540 + unsigned char *old_fanout)
2516 2541 {
2517 2542 struct object_entry *oe;
2518 2543 struct branch *s;
@@ -2577,7 +2602,7 @@ static void note_change_n(const char *p, struct branch *b, unsigned char *old_fa
2577 2602 die(_("invalid ref name or SHA1 expression: %s"), p);
2578 2603
2579 2604 if (inline_data) {
2580 - read_next_command();
2605 + read_next_command(state);
2581 2606 parse_and_store_blob(&last_blob, &oid, 0);
2582 2607 } else if (oe) {
2583 2608 if (oe->type != OBJ_BLOB)
@@ -2644,7 +2669,9 @@ static void parse_from_existing(struct branch *b)
2644 2669 }
2645 2670 }
2646 2671
2647 - static int parse_objectish(struct branch *b, const char *objectish)
2672 + static int parse_objectish(struct fast_import_state *state,
2673 + struct branch *b,
2674 + const char *objectish)
2648 2675 {
2649 2676 struct branch *s;
2650 2677 struct object_id oid;
@@ -2687,31 +2714,34 @@ static int parse_objectish(struct branch *b, const char *objectish)
2687 2714 b->branch_tree.tree = NULL;
2688 2715 }
2689 2716
2690 - read_next_command();
2717 + read_next_command(state);
2691 2718 return 1;
2692 2719 }
2693 2720
2694 - static int parse_from(struct branch *b)
2721 + static int parse_from(struct fast_import_state *state, struct branch *b)
2695 2722 {
2696 2723 const char *from;
2697 2724
2698 2725 if (!skip_prefix(command_buf.buf, "from ", &from))
2699 2726 return 0;
2700 2727
2701 - return parse_objectish(b, from);
2728 + return parse_objectish(state, b, from);
2702 2729 }
2703 2730
2704 - static int parse_objectish_with_prefix(struct branch *b, const char *prefix)
2731 + static int parse_objectish_with_prefix(struct fast_import_state *state,
2732 + struct branch *b,
2733 + const char *prefix)
2705 2734 {
2706 2735 const char *base;
2707 2736
2708 2737 if (!skip_prefix(command_buf.buf, prefix, &base))
2709 2738 return 0;
2710 2739
2711 - return parse_objectish(b, base);
2740 + return parse_objectish(state, b, base);
2712 2741 }
2713 2742
2714 - static struct hash_list *parse_merge(unsigned int *count)
2743 + static struct hash_list *parse_merge(struct fast_import_state *state,
2744 + unsigned int *count)
2715 2745 {
2716 2746 struct hash_list *list = NULL, **tail = &list, *n;
2717 2747 const char *from;
@@ -2745,7 +2775,7 @@ static struct hash_list *parse_merge(unsigned int *count)
2745 2775 tail = &n->next;
2746 2776
2747 2777 (*count)++;
2748 - read_next_command();
2778 + read_next_command(state);
2749 2779 }
2750 2780 return list;
2751 2781 }
@@ -2756,7 +2786,9 @@ struct signature_data {
2756 2786 struct strbuf data; /* The actual signature data */
2757 2787 };
2758 2788
2759 - static void parse_one_signature(struct signature_data *sig, const char *v)
2789 + static void parse_one_signature(struct fast_import_state *state,
2790 + struct signature_data *sig,
2791 + const char *v)
2760 2792 {
2761 2793 char *args = xstrdup(v); /* Will be freed when sig->hash_algo is freed */
2762 2794 char *space = strchr(args, ' ');
@@ -2781,15 +2813,15 @@ static void parse_one_signature(struct signature_data *sig, const char *v)
2781 2813 warning(_("'unknown' signature format in gpgsig"));
2782 2814
2783 2815 /* Read signature data */
2784 - read_next_command();
2816 + read_next_command(state);
2785 2817 parse_data(&sig->data, 0, NULL);
2786 2818 }
2787 2819
2788 - static void discard_one_signature(void)
2820 + static void discard_one_signature(struct fast_import_state *state)
2789 2821 {
2790 2822 struct strbuf data = STRBUF_INIT;
2791 2823
2792 - read_next_command();
2824 + read_next_command(state);
2793 2825 parse_data(&data, 0, NULL);
2794 2826 strbuf_release(&data);
2795 2827 }
@@ -2827,13 +2859,14 @@ static void store_signature(struct signature_data *stored_sig,
2827 2859 }
2828 2860 }
2829 2861
2830 - static void import_one_signature(struct signature_data *sig_sha1,
2862 + static void import_one_signature(struct fast_import_state *state,
2863 + struct signature_data *sig_sha1,
2831 2864 struct signature_data *sig_sha256,
2832 2865 const char *v)
2833 2866 {
2834 2867 struct signature_data sig = { NULL, NULL, STRBUF_INIT };
2835 2868
2836 - parse_one_signature(&sig, v);
2869 + parse_one_signature(state, &sig, v);
2837 2870
2838 2871 if (!strcmp(sig.hash_algo, "sha1"))
2839 2872 store_signature(sig_sha1, &sig, "SHA-1");
@@ -2947,7 +2980,7 @@ static void handle_signature_if_invalid(struct strbuf *new_data,
2947 2980 strbuf_release(&tmp_buf);
2948 2981 }
2949 2982
2950 - static void parse_new_commit(const char *arg)
2983 + static void parse_new_commit(struct fast_import_state *state, const char *arg)
2951 2984 {
2952 2985 static struct strbuf msg = STRBUF_INIT;
2953 2986 struct signature_data sig_sha1 = { NULL, NULL, STRBUF_INIT };
@@ -2965,16 +2998,16 @@ static void parse_new_commit(const char *arg)
2965 2998 if (!b)
2966 2999 b = new_branch(arg);
2967 3000
2968 - read_next_command();
2969 - parse_mark();
2970 - parse_original_identifier();
3001 + read_next_command(state);
3002 + parse_mark(state);
3003 + parse_original_identifier(state);
2971 3004 if (skip_prefix(command_buf.buf, "author ", &v)) {
2972 3005 author = parse_ident(v);
2973 - read_next_command();
3006 + read_next_command(state);
2974 3007 }
2975 3008 if (skip_prefix(command_buf.buf, "committer ", &v)) {
2976 3009 committer = parse_ident(v);
2977 - read_next_command();
3010 + read_next_command(state);
2978 3011 }
2979 3012 if (!committer)
2980 3013 die(_("expected committer but didn't get one"));
@@ -2990,7 +3023,7 @@ static void parse_new_commit(const char *arg)
2990 3023 warning(_("stripping a commit signature"));
2991 3024 /* fallthru */
2992 3025 case SIGN_STRIP:
2993 - discard_one_signature();
3026 + discard_one_signature(state);
2994 3027 break;
2995 3028
2996 3029 /* Second, modes that parse the signature */
@@ -3001,24 +3034,24 @@ static void parse_new_commit(const char *arg)
3001 3034 case SIGN_STRIP_IF_INVALID:
3002 3035 case SIGN_SIGN_IF_INVALID:
3003 3036 case SIGN_ABORT_IF_INVALID:
3004 - import_one_signature(&sig_sha1, &sig_sha256, v);
3037 + import_one_signature(state, &sig_sha1, &sig_sha256, v);
3005 3038 break;
3006 3039
3007 3040 /* Third, BUG */
3008 3041 default:
3009 3042 BUG("invalid signed_commit_mode value %d", signed_commit_mode);
3010 3043 }
3011 - read_next_command();
3044 + read_next_command(state);
3012 3045 }
3013 3046
3014 3047 if (skip_prefix(command_buf.buf, "encoding ", &v)) {
3015 3048 encoding = xstrdup(v);
3016 - read_next_command();
3049 + read_next_command(state);
3017 3050 }
3018 3051 parse_data(&msg, 0, NULL);
3019 - read_next_command();
3020 - parse_from(b);
3021 - merge_list = parse_merge(&merge_count);
3052 + read_next_command(state);
3053 + parse_from(state, b);
3054 + merge_list = parse_merge(state, &merge_count);
3022 3055
3023 3056 /* ensure the branch is active/loaded */
3024 3057 if (!b->branch_tree.tree || !max_active_branches) {
@@ -3031,7 +3064,7 @@ static void parse_new_commit(const char *arg)
3031 3064 /* file_change* */
3032 3065 while (command_buf.len > 0) {
3033 3066 if (skip_prefix(command_buf.buf, "M ", &v))
3034 - file_change_m(v, b);
3067 + file_change_m(state, v, b);
3035 3068 else if (skip_prefix(command_buf.buf, "D ", &v))
3036 3069 file_change_d(v, b);
3037 3070 else if (skip_prefix(command_buf.buf, "R ", &v))
@@ -3039,18 +3072,18 @@ static void parse_new_commit(const char *arg)
3039 3072 else if (skip_prefix(command_buf.buf, "C ", &v))
3040 3073 file_change_cr(v, b, 0);
3041 3074 else if (skip_prefix(command_buf.buf, "N ", &v))
3042 - note_change_n(v, b, &prev_fanout);
3075 + note_change_n(state, v, b, &prev_fanout);
3043 3076 else if (!strcmp("deleteall", command_buf.buf))
3044 3077 file_change_deleteall(b);
3045 3078 else if (skip_prefix(command_buf.buf, "ls ", &v))
3046 - parse_ls(v, b);
3079 + parse_ls(state, v, b);
3047 3080 else if (skip_prefix(command_buf.buf, "cat-blob ", &v))
3048 - parse_cat_blob(v);
3081 + parse_cat_blob(state, v);
3049 3082 else {
3050 3083 unread_command_buf = 1;
3051 3084 break;
3052 3085 }
3053 - if (read_next_command() == EOF)
3086 + if (read_next_command(state) == EOF)
3054 3087 break;
3055 3088 }
3056 3089
@@ -3188,7 +3221,7 @@ static void handle_tag_signature(struct strbuf *buf, struct strbuf *msg, const c
3188 3221 }
3189 3222 }
3190 3223
3191 - static void parse_new_tag(const char *arg)
3224 + static void parse_new_tag(struct fast_import_state *state, const char *arg)
3192 3225 {
3193 3226 static struct strbuf msg = STRBUF_INIT;
3194 3227 const char *from;
@@ -3207,8 +3240,8 @@ static void parse_new_tag(const char *arg)
3207 3240 else
3208 3241 first_tag = t;
3209 3242 last_tag = t;
3210 - read_next_command();
3211 - parse_mark();
3243 + read_next_command(state);
3244 + parse_mark(state);
3212 3245
3213 3246 /* from ... */
3214 3247 if (!skip_prefix(command_buf.buf, "from ", &from))
@@ -3236,15 +3269,15 @@ static void parse_new_tag(const char *arg)
3236 3269 type = oe->type;
3237 3270 } else
3238 3271 die(_("invalid ref name or SHA1 expression: %s"), from);
3239 - read_next_command();
3272 + read_next_command(state);
3240 3273
3241 3274 /* original-oid ... */
3242 - parse_original_identifier();
3275 + parse_original_identifier(state);
3243 3276
3244 3277 /* tagger ... */
3245 3278 if (skip_prefix(command_buf.buf, "tagger ", &v)) {
3246 3279 tagger = parse_ident(v);
3247 - read_next_command();
3280 + read_next_command(state);
3248 3281 } else
3249 3282 tagger = NULL;
3250 3283
@@ -3275,7 +3308,7 @@ static void parse_new_tag(const char *arg)
3275 3308 t->pack_id = pack_id;
3276 3309 }
3277 3310
3278 - static void parse_reset_branch(const char *arg)
3311 + static void parse_reset_branch(struct fast_import_state *state, const char *arg)
3279 3312 {
3280 3313 struct branch *b;
3281 3314 const char *tag_name;
@@ -3292,8 +3325,8 @@ static void parse_reset_branch(const char *arg)
3292 3325 }
3293 3326 else
3294 3327 b = new_branch(arg);
3295 - read_next_command();
3296 - parse_from(b);
3328 + read_next_command(state);
3329 + parse_from(state, b);
3297 3330 if (b->delete && skip_prefix(b->name, "refs/tags/", &tag_name)) {
3298 3331 /*
3299 3332 * Elsewhere, we call dump_branches() before dump_tags(),
@@ -3390,7 +3423,8 @@ static void cat_blob(struct object_entry *oe, struct object_id *oid)
3390 3423 free(buf);
3391 3424 }
3392 3425
3393 - static void parse_get_mark(const char *p)
3426 + static void parse_get_mark(struct fast_import_state *state UNUSED,
3427 + const char *p)
3394 3428 {
3395 3429 struct object_entry *oe;
3396 3430 char output[GIT_MAX_HEXSZ + 2];
@@ -3407,7 +3441,8 @@ static void parse_get_mark(const char *p)
3407 3441 cat_blob_write(output, the_hash_algo->hexsz + 1);
3408 3442 }
3409 3443
3410 - static void parse_cat_blob(const char *p)
3444 + static void parse_cat_blob(struct fast_import_state *state UNUSED,
3445 + const char *p)
3411 3446 {
3412 3447 struct object_entry *oe;
3413 3448 struct object_id oid;
@@ -3572,7 +3607,9 @@ static void print_ls(int mode, const unsigned char *hash, const char *path)
3572 3607 cat_blob_write(line.buf, line.len);
3573 3608 }
3574 3609
3575 - static void parse_ls(const char *p, struct branch *b)
3610 + static void parse_ls(struct fast_import_state *state UNUSED,
3611 + const char *p,
3612 + struct branch *b)
3576 3613 {
3577 3614 static struct strbuf path = STRBUF_INIT;
3578 3615 struct tree_entry *root = NULL;
@@ -3619,13 +3656,13 @@ static void checkpoint(void)
3619 3656 dump_marks();
3620 3657 }
3621 3658
3622 - static void parse_checkpoint(void)
3659 + static void parse_checkpoint(struct fast_import_state *state UNUSED)
3623 3660 {
3624 3661 checkpoint_requested = 1;
3625 3662 skip_optional_lf();
3626 3663 }
3627 3664
3628 - static void parse_progress(void)
3665 + static void parse_progress(struct fast_import_state *state UNUSED)
3629 3666 {
3630 3667 fwrite(command_buf.buf, 1, command_buf.len, stdout);
3631 3668 fputc('\n', stdout);
@@ -3633,37 +3670,40 @@ static void parse_progress(void)
3633 3670 skip_optional_lf();
3634 3671 }
3635 3672
3636 - static void parse_alias(void)
3673 + static void parse_alias(struct fast_import_state *state)
3637 3674 {
3638 3675 struct object_entry *e;
3639 3676 struct branch b;
3640 3677
3641 3678 skip_optional_lf();
3642 - read_next_command();
3679 + read_next_command(state);
3643 3680
3644 3681 /* mark ... */
3645 - parse_mark();
3682 + parse_mark(state);
3646 3683 if (!next_mark)
3647 3684 die(_("expected 'mark' command, got %s"), command_buf.buf);
3648 3685
3649 3686 /* to ... */
3650 3687 memset(&b, 0, sizeof(b));
3651 - if (!parse_objectish_with_prefix(&b, "to "))
3688 + if (!parse_objectish_with_prefix(state, &b, "to "))
3652 3689 die(_("expected 'to' command, got %s"), command_buf.buf);
3653 3690 e = find_object(&b.oid);
3654 3691 assert(e);
3655 3692 insert_mark(&marks, next_mark, e);
3656 3693 }
3657 3694
3658 - static char* make_fast_import_path(const char *path)
3695 + static char* make_fast_import_path(struct fast_import_state *state,
3696 + const char *path)
3659 3697 {
3660 3698 if (!relative_marks_paths || is_absolute_path(path))
3661 - return prefix_filename(global_prefix, path);
3699 + return prefix_filename(state->prefix, path);
3662 3700 return repo_git_path(the_repository, "info/fast-import/%s", path);
3663 3701 }
3664 3702
3665 - static void option_import_marks(const char *marks,
3666 - int from_stream, int ignore_missing)
3703 + static void option_import_marks(struct fast_import_state *state,
3704 + const char *marks,
3705 + int from_stream,
3706 + int ignore_missing)
3667 3707 {
3668 3708 if (import_marks_file) {
3669 3709 if (from_stream)
@@ -3675,7 +3715,7 @@ static void option_import_marks(const char *marks,
3675 3715 }
3676 3716
3677 3717 free(import_marks_file);
3678 - import_marks_file = make_fast_import_path(marks);
3718 + import_marks_file = make_fast_import_path(state, marks);
3679 3719 import_marks_file_from_stream = from_stream;
3680 3720 import_marks_file_ignore_missing = ignore_missing;
3681 3721 }
@@ -3715,13 +3755,15 @@ static void option_active_branches(const char *branches)
3715 3755 max_active_branches = ulong_arg("--active-branches", branches);
3716 3756 }
3717 3757
3718 - static void option_export_marks(const char *marks)
3758 + static void option_export_marks(struct fast_import_state *state,
3759 + const char *marks)
3719 3760 {
3720 3761 free(export_marks_file);
3721 - export_marks_file = make_fast_import_path(marks);
3762 + export_marks_file = make_fast_import_path(state, marks);
3722 3763 }
3723 3764
3724 - static void option_cat_blob_fd(const char *fd)
3765 + static void option_cat_blob_fd(struct fast_import_state *state UNUSED,
3766 + const char *fd)
3725 3767 {
3726 3768 unsigned long n = ulong_arg("--cat-blob-fd", fd);
3727 3769 if (n > (unsigned long) INT_MAX)
@@ -3729,16 +3771,19 @@ static void option_cat_blob_fd(const char *fd)
3729 3771 cat_blob_fd = (int) n;
3730 3772 }
3731 3773
3732 - static void option_export_pack_edges(const char *edges)
3774 + static void option_export_pack_edges(struct fast_import_state *state,
3775 + const char *edges)
3733 3776 {
3734 - char *fn = prefix_filename(global_prefix, edges);
3777 + char *fn = prefix_filename(state->prefix, edges);
3735 3778 if (pack_edges)
3736 3779 fclose(pack_edges);
3737 3780 pack_edges = xfopen(fn, "a");
3738 3781 free(fn);
3739 3782 }
3740 3783
3741 - static void option_rewrite_submodules(const char *arg, struct string_list *list)
3784 + static void option_rewrite_submodules(struct fast_import_state *state,
3785 + const char *arg,
3786 + struct string_list *list)
3742 3787 {
3743 3788 struct mark_set *ms;
3744 3789 FILE *fp;
@@ -3750,7 +3795,7 @@ static void option_rewrite_submodules(const char *arg, struct string_list *list)
3750 3795 f++;
3751 3796 CALLOC_ARRAY(ms, 1);
3752 3797
3753 - f = prefix_filename(global_prefix, f);
3798 + f = prefix_filename(state->prefix, f);
3754 3799 fp = fopen(f, "r");
3755 3800 if (!fp)
3756 3801 die_errno(_("cannot read '%s'"), f);
@@ -3763,40 +3808,67 @@ static void option_rewrite_submodules(const char *arg, struct string_list *list)
3763 3808 free(s);
3764 3809 }
3765 3810
3766 - static int parse_one_option(const char *option)
3811 + static void option_max_pack_size(const char *arg)
3812 + {
3813 + unsigned long v;
3814 +
3815 + if (!git_parse_ulong(arg, &v))
3816 + die(_("--max-pack-size: argument must be a non-negative integer"));
3817 + if (v < 8192) {
3818 + warning(_("max-pack-size is now in bytes, assuming --max-pack-size=%lum"), v);
3819 + v *= 1024 * 1024;
3820 + } else if (v < 1024 * 1024) {
3821 + warning(_("minimum max-pack-size is 1 MiB"));
3822 + v = 1024 * 1024;
3823 + }
3824 + max_packsize = v;
3825 + }
3826 +
3827 + static void option_big_file_threshold(const char *arg)
3828 + {
3829 + unsigned long v;
3830 +
3831 + if (!git_parse_ulong(arg, &v))
3832 + die(_("--big-file-threshold: argument must be a non-negative integer"));
3833 + repo_settings_set_big_file_threshold(the_repository, v);
3834 + }
3835 +
3836 + static void option_signed_commits(const char *arg)
3837 + {
3838 + if (parse_sign_mode(arg, &signed_commit_mode, &signed_commit_keyid))
3839 + usagef(_("unknown --signed-commits mode '%s'"), arg);
3840 + }
3841 +
3842 + static void option_signed_tags(const char *arg)
3843 + {
3844 + if (parse_sign_mode(arg, &signed_tag_mode, &signed_tag_keyid))
3845 + usagef(_("unknown --signed-tags mode '%s'"), arg);
3846 + }
3847 +
3848 + static void option_quiet(void)
3849 + {
3850 + show_stats = 0;
3851 + quiet = 1;
3852 + }
3853 +
3854 + static int parse_one_option(struct fast_import_state *state, const char *option)
3767 3855 {
3768 3856 if (skip_prefix(option, "max-pack-size=", &option)) {
3769 - unsigned long v;
3770 - if (!git_parse_ulong(option, &v))
3771 - return 0;
3772 - if (v < 8192) {
3773 - warning(_("max-pack-size is now in bytes, assuming --max-pack-size=%lum"), v);
3774 - v *= 1024 * 1024;
3775 - } else if (v < 1024 * 1024) {
3776 - warning(_("minimum max-pack-size is 1 MiB"));
3777 - v = 1024 * 1024;
3778 - }
3779 - max_packsize = v;
3857 + option_max_pack_size(option);
3780 3858 } else if (skip_prefix(option, "big-file-threshold=", &option)) {
3781 - unsigned long v;
3782 - if (!git_parse_ulong(option, &v))
3783 - return 0;
3784 - repo_settings_set_big_file_threshold(the_repository, v);
3859 + option_big_file_threshold(option);
3785 3860 } else if (skip_prefix(option, "depth=", &option)) {
3786 3861 option_depth(option);
3787 3862 } else if (skip_prefix(option, "active-branches=", &option)) {
3788 3863 option_active_branches(option);
3789 3864 } else if (skip_prefix(option, "export-pack-edges=", &option)) {
3790 - option_export_pack_edges(option);
3865 + option_export_pack_edges(state, option);
3791 3866 } else if (skip_prefix(option, "signed-commits=", &option)) {
3792 - if (parse_sign_mode(option, &signed_commit_mode, &signed_commit_keyid))
3793 - usagef(_("unknown --signed-commits mode '%s'"), option);
3867 + option_signed_commits(option);
3794 3868 } else if (skip_prefix(option, "signed-tags=", &option)) {
3795 - if (parse_sign_mode(option, &signed_tag_mode, &signed_tag_keyid))
3796 - usagef(_("unknown --signed-tags mode '%s'"), option);
3869 + option_signed_tags(option);
3797 3870 } else if (!strcmp(option, "quiet")) {
3798 - show_stats = 0;
3799 - quiet = 1;
3871 + option_quiet();
3800 3872 } else if (!strcmp(option, "stats")) {
3801 3873 show_stats = 1;
3802 3874 } else if (!strcmp(option, "allow-unsafe-features")) {
@@ -3808,34 +3880,36 @@ static int parse_one_option(const char *option)
3808 3880 return 1;
3809 3881 }
3810 3882
3811 - static void check_unsafe_feature(const char *feature, int from_stream)
3883 + static void check_unsafe_feature(struct fast_import_state *state,
3884 + const char *feature)
3812 3885 {
3813 - if (from_stream && !allow_unsafe_features)
3886 + if (!state->allow_unsafe_features)
3814 3887 die(_("feature '%s' forbidden in input without --allow-unsafe-features"),
3815 3888 feature);
3816 3889 }
3817 3890
3818 - static int parse_one_feature(const char *feature, int from_stream)
3891 + static int parse_one_feature(struct fast_import_state *state,
3892 + const char *feature)
3819 3893 {
3820 3894 const char *arg;
3821 3895
3822 3896 if (skip_prefix(feature, "date-format=", &arg)) {
3823 3897 option_date_format(arg);
3824 3898 } else if (skip_prefix(feature, "import-marks=", &arg)) {
3825 - check_unsafe_feature("import-marks", from_stream);
3826 - option_import_marks(arg, from_stream, 0);
3899 + check_unsafe_feature(state, "import-marks");
3900 + option_import_marks(state, arg, 1, 0);
3827 3901 } else if (skip_prefix(feature, "import-marks-if-exists=", &arg)) {
3828 - check_unsafe_feature("import-marks-if-exists", from_stream);
3829 - option_import_marks(arg, from_stream, 1);
3902 + check_unsafe_feature(state, "import-marks-if-exists");
3903 + option_import_marks(state, arg, 1, 1);
3830 3904 } else if (skip_prefix(feature, "export-marks=", &arg)) {
3831 - check_unsafe_feature(feature, from_stream);
3832 - option_export_marks(arg);
3905 + check_unsafe_feature(state, feature);
3906 + option_export_marks(state, arg);
3833 3907 } else if (!strcmp(feature, "alias")) {
3834 3908 ; /* Don't die - this feature is supported */
3835 3909 } else if (skip_prefix(feature, "rewrite-submodules-to=", &arg)) {
3836 - option_rewrite_submodules(arg, &sub_marks_to);
3910 + option_rewrite_submodules(state, arg, &sub_marks_to);
3837 3911 } else if (skip_prefix(feature, "rewrite-submodules-from=", &arg)) {
3838 - option_rewrite_submodules(arg, &sub_marks_from);
3912 + option_rewrite_submodules(state, arg, &sub_marks_from);
3839 3913 } else if (!strcmp(feature, "get-mark")) {
3840 3914 ; /* Don't die - this feature is supported */
3841 3915 } else if (!strcmp(feature, "cat-blob")) {
@@ -3857,23 +3931,23 @@ static int parse_one_feature(const char *feature, int from_stream)
3857 3931 return 1;
3858 3932 }
3859 3933
3860 - static void parse_feature(const char *feature)
3934 + static void parse_feature(struct fast_import_state *state, const char *feature)
3861 3935 {
3862 - if (seen_data_command)
3936 + if (state->seen_data_command)
3863 3937 die(_("got feature command '%s' after data command"), feature);
3864 3938
3865 - if (parse_one_feature(feature, 1))
3939 + if (parse_one_feature(state, feature))
3866 3940 return;
3867 3941
3868 3942 die(_("this version of fast-import does not support feature %s."), feature);
3869 3943 }
3870 3944
3871 - static void parse_option(const char *option)
3945 + static void parse_option(struct fast_import_state *state, const char *option)
3872 3946 {
3873 - if (seen_data_command)
3947 + if (state->seen_data_command)
3874 3948 die(_("got option command '%s' after data command"), option);
3875 3949
3876 - if (parse_one_option(option))
3950 + if (parse_one_option(state, option))
3877 3951 return;
3878 3952
3879 3953 die(_("this version of fast-import does not support option: %s"), option);
@@ -3906,42 +3980,144 @@ static void git_pack_config(void)
3906 3980 repo_config(the_repository, git_default_config, NULL);
3907 3981 }
3908 3982
3909 - static const char fast_import_usage[] =
3910 - "git fast-import [--date-format=<f>] [--max-pack-size=<n>] [--big-file-threshold=<n>] [--depth=<n>] [--active-branches=<n>] [--export-marks=<marks.file>]";
3983 + static const char *const fast_import_usage[] = {
3984 + N_("git fast-import [<options>]"),
3985 + NULL
3986 + };
3911 3987
3912 - static void parse_argv(void)
3988 + static void parse_argv(struct fast_import_state *state)
3913 3989 {
3914 - unsigned int i;
3990 + int argc = parse_options(state->argc, state->argv, state->prefix,
3991 + state->option, fast_import_usage,
3992 + PARSE_OPT_KEEP_ARGV0);
3915 3993
3916 - for (i = 1; i < global_argc; i++) {
3917 - const char *a = global_argv[i];
3994 + if (argc > 1)
3995 + usage_with_options(fast_import_usage, state->option);
3918 3996
3919 - if (*a != '-' || !strcmp(a, "--"))
3920 - break;
3997 + state->seen_data_command = 1;
3998 + if (import_marks_file)
3999 + read_marks();
4000 + build_mark_map(&sub_marks_from, &sub_marks_to);
4001 + }
3921 4002
3922 - if (!skip_prefix(a, "--", &a))
3923 - die(_("unknown option %s"), a);
4003 + static int option_parse_date_format(const struct option *opt UNUSED,
4004 + const char *arg, int unset)
4005 + {
4006 + BUG_ON_OPT_NEG(unset);
4007 + option_date_format(arg);
4008 + return 0;
4009 + }
3924 4010
3925 - if (parse_one_option(a))
3926 - continue;
4011 + static int option_parse_export_pack_edges(const struct option *opt,
4012 + const char *arg, int unset)
4013 + {
4014 + BUG_ON_OPT_NEG(unset);
4015 + option_export_pack_edges(opt->value, arg);
4016 + return 0;
4017 + }
3927 4018
3928 - if (parse_one_feature(a, 0))
3929 - continue;
4019 + static int option_parse_max_pack_size(const struct option *opt UNUSED,
4020 + const char *arg, int unset)
4021 + {
4022 + BUG_ON_OPT_NEG(unset);
4023 + option_max_pack_size(arg);
4024 + return 0;
4025 + }
3930 4026
3931 - if (skip_prefix(a, "cat-blob-fd=", &a)) {
3932 - option_cat_blob_fd(a);
3933 - continue;
3934 - }
4027 + static int option_parse_big_file_threshold(const struct option *opt UNUSED,
4028 + const char *arg, int unset)
4029 + {
4030 + BUG_ON_OPT_NEG(unset);
4031 + option_big_file_threshold(arg);
4032 + return 0;
4033 + }
3935 4034
3936 - die(_("unknown option --%s"), a);
3937 - }
3938 - if (i != global_argc)
3939 - usage(fast_import_usage);
4035 + static int option_parse_signed_commits(const struct option *opt UNUSED,
4036 + const char *arg, int unset)
4037 + {
4038 + BUG_ON_OPT_NEG(unset);
4039 + option_signed_commits(arg);
4040 + return 0;
4041 + }
3940 4042
3941 - seen_data_command = 1;
3942 - if (import_marks_file)
3943 - read_marks();
3944 - build_mark_map(&sub_marks_from, &sub_marks_to);
4043 + static int option_parse_signed_tags(const struct option *opt UNUSED,
4044 + const char *arg, int unset)
4045 + {
4046 + BUG_ON_OPT_NEG(unset);
4047 + option_signed_tags(arg);
4048 + return 0;
4049 + }
4050 +
4051 + static int option_parse_rewrite_submodules_from(const struct option *opt,
4052 + const char *arg, int unset)
4053 + {
4054 + BUG_ON_OPT_NEG(unset);
4055 + option_rewrite_submodules(opt->value, arg, &sub_marks_from);
4056 + return 0;
4057 + }
4058 +
4059 + static int option_parse_rewrite_submodules_to(const struct option *opt,
4060 + const char *arg, int unset)
4061 + {
4062 + BUG_ON_OPT_NEG(unset);
4063 + option_rewrite_submodules(opt->value, arg, &sub_marks_to);
4064 + return 0;
4065 + }
4066 +
4067 + static int option_parse_cat_blob_fd(const struct option *opt,
4068 + const char *arg, int unset)
4069 + {
4070 + BUG_ON_OPT_NEG(unset);
4071 + option_cat_blob_fd(opt->value, arg);
4072 + return 0;
4073 + }
4074 +
4075 + static int option_parse_import_marks(const struct option *opt,
4076 + const char *arg, int unset)
4077 + {
4078 + BUG_ON_OPT_NEG(unset);
4079 + option_import_marks(opt->value, arg, 0, 0);
4080 + return 0;
4081 + }
4082 +
4083 + static int option_parse_import_marks_if_exists(const struct option *opt,
4084 + const char *arg, int unset)
4085 + {
4086 + BUG_ON_OPT_NEG(unset);
4087 + option_import_marks(opt->value, arg, 0, 1);
4088 + return 0;
4089 + }
4090 +
4091 + static int option_parse_export_marks(const struct option *opt,
4092 + const char *arg, int unset)
4093 + {
4094 + BUG_ON_OPT_NEG(unset);
4095 + option_export_marks(opt->value, arg);
4096 + return 0;
4097 + }
4098 +
4099 + static int option_parse_depth(const struct option *opt UNUSED,
4100 + const char *arg, int unset)
4101 + {
4102 + BUG_ON_OPT_NEG(unset);
4103 + option_depth(arg);
4104 + return 0;
4105 + }
4106 +
4107 + static int option_parse_active_branches(const struct option *opt UNUSED,
4108 + const char *arg, int unset)
4109 + {
4110 + BUG_ON_OPT_NEG(unset);
4111 + option_active_branches(arg);
4112 + return 0;
4113 + }
4114 +
4115 + static int option_parse_quiet(const struct option *opt UNUSED,
4116 + const char *arg UNUSED, int unset)
4117 + {
4118 + BUG_ON_OPT_NEG(unset);
4119 + option_quiet();
4120 + return 0;
3945 4121 }
3946 4122
3947 4123 int cmd_fast_import(int argc,
@@ -3949,9 +4125,80 @@ int cmd_fast_import(int argc,
3949 4125 const char *prefix,
3950 4126 struct repository *repo)
3951 4127 {
3952 - unsigned int i;
3953 -
3954 - show_usage_if_asked(argc, argv, fast_import_usage);
4128 + struct fast_import_state state;
4129 +
4130 + struct option fast_import_options[] = {
4131 + OPT_GROUP(N_("Common")),
4132 + OPT_CALLBACK_F(0, "date-format", NULL, N_("fmt"),
4133 + N_("format of the commit/tag dates"),
4134 + PARSE_OPT_NONEG, option_parse_date_format),
4135 + OPT_BOOL_F(0, "stats", &show_stats,
4136 + N_("display some basic statistics (objects, packfiles and memory)"),
4137 + PARSE_OPT_NONEG),
4138 + OPT_CALLBACK_F(0, "quiet", NULL, NULL,
4139 + N_("disable the output shown by --stats"),
4140 + PARSE_OPT_NOARG | PARSE_OPT_NONEG,
4141 + option_parse_quiet),
4142 + OPT_BOOL_F(0, "force", &force_update,
4143 + N_("force updating modified existing branches"), PARSE_OPT_NONEG),
4144 + OPT_BOOL_F(0, "done", &require_explicit_termination,
4145 + N_("require a terminating 'done' command"), PARSE_OPT_NONEG),
4146 + OPT_CALLBACK_F(0, "max-pack-size", NULL, N_("n"),
4147 + N_("maximum size of each output pack file"),
4148 + PARSE_OPT_NONEG, option_parse_max_pack_size),
4149 + OPT_CALLBACK_F(0, "big-file-threshold", NULL, N_("n"),
4150 + N_("maximum size of a blob that will be deltified"),
4151 + PARSE_OPT_NONEG, option_parse_big_file_threshold),
4152 + OPT_CALLBACK_F(0, "depth", NULL, N_("n"),
4153 + N_("maximum delta depth"),
4154 + PARSE_OPT_NONEG, option_parse_depth),
4155 + OPT_CALLBACK_F(0, "active-branches", NULL, N_("n"),
4156 + N_("maximum number of branches to maintain active"),
4157 + PARSE_OPT_NONEG, option_parse_active_branches),
4158 + OPT_GROUP(N_("Marks")),
4159 + OPT_CALLBACK_F(0, "import-marks", &state, N_("file"),
4160 + N_("import marks from <file>"),
4161 + PARSE_OPT_NONEG, option_parse_import_marks),
4162 + OPT_CALLBACK_F(0, "import-marks-if-exists", &state, N_("file"),
4163 + N_("import marks from <file> if it exists"),
4164 + PARSE_OPT_NONEG, option_parse_import_marks_if_exists),
4165 + OPT_CALLBACK_F(0, "export-marks", &state, N_("file"),
4166 + N_("dump marks to <file>"),
4167 + PARSE_OPT_NONEG, option_parse_export_marks),
4168 + OPT_BOOL(0, "relative-marks", &relative_marks_paths,
4169 + N_("are --(import|export)-marks= paths relative to '.git/info/fast-import'?")),
4170 + OPT_GROUP(N_("Submodule rewrite")),
4171 + OPT_CALLBACK_F(0, "rewrite-submodules-from", &state, N_("name:filename"),
4172 + N_("rewrite object IDs for submodule <name> from <filename>"),
4173 + PARSE_OPT_NONEG, option_parse_rewrite_submodules_from),
4174 + OPT_CALLBACK_F(0, "rewrite-submodules-to", &state, N_("name:filename"),
4175 + N_("rewrite object IDs for submodule <name> to <filename>"),
4176 + PARSE_OPT_NONEG, option_parse_rewrite_submodules_to),
4177 + OPT_GROUP(N_("Signing")),
4178 + OPT_CALLBACK_F(0, "signed-commits", NULL, N_("mode"),
4179 + N_("how to handle signed commits"),
4180 + PARSE_OPT_NONEG, option_parse_signed_commits),
4181 + OPT_CALLBACK_F(0, "signed-tags", NULL, N_("mode"),
4182 + N_("how to handle signed tags"),
4183 + PARSE_OPT_NONEG, option_parse_signed_tags),
4184 + OPT_HIDDEN_GROUP(N_("Advanced")),
4185 + OPT_BOOL_F(0, "allow-unsafe-features", &state.allow_unsafe_features,
4186 + N_("allow unsafe mark commands from the stream"),
4187 + PARSE_OPT_HIDDEN | PARSE_OPT_NONEG),
4188 + OPT_CALLBACK_F(0, "export-pack-edges", &state, N_("file"),
4189 + N_("dump edge commits to <file>"),
4190 + PARSE_OPT_HIDDEN | PARSE_OPT_NONEG,
4191 + option_parse_export_pack_edges),
4192 + OPT_CALLBACK_F(0, "cat-blob-fd", &state, N_("fd"),
4193 + N_("write some responses to <fd> instead of stdout"),
4194 + PARSE_OPT_HIDDEN | PARSE_OPT_NONEG,
4195 + option_parse_cat_blob_fd),
4196 + OPT_END()
4197 + };
4198 +
4199 + show_usage_with_options_if_asked(argc, argv, fast_import_usage, fast_import_options);
4200 +
4201 + fast_import_state_init(&state, argc, argv, prefix, fast_import_options);
3955 4202
3956 4203 reset_pack_idx_option(&pack_idx_opts);
3957 4204 git_pack_config();
@@ -3970,55 +4217,61 @@ int cmd_fast_import(int argc,
3970 4217 * "feature" lines at the start of the stream (which allows the command
3971 4218 * line to override stream data). But we must do an early parse of any
3972 4219 * command-line options that impact how we interpret the feature lines.
4220 + *
4221 + * NEEDSWORK: This scan only matches the exact "--allow-unsafe-features"
4222 + * spelling and stops at the first argument that doesn't start with a
4223 + * dash. As parse_options() below also accepts unambiguous abbreviations
4224 + * and values separated by a space from their option, the two disagree
4225 + * for command lines like "--allow-unsafe" or "--depth 5
4226 + * --allow-unsafe-features": parse_options() accepts the option, but
4227 + * this scan doesn't see it, so unsafe features from the stream are
4228 + * still refused. This errs on the safe side, but should be fixed by
4229 + * teaching this scan about the options that take a value.
3973 4230 */
3974 - for (i = 1; i < argc; i++) {
4231 + for (int i = 1; i < argc; i++) {
3975 4232 const char *arg = argv[i];
3976 4233 if (*arg != '-' || !strcmp(arg, "--"))
3977 4234 break;
3978 4235 if (!strcmp(arg, "--allow-unsafe-features"))
3979 - allow_unsafe_features = 1;
4236 + state.allow_unsafe_features = 1;
3980 4237 }
3981 4238
3982 - global_argc = argc;
3983 - global_argv = argv;
3984 - global_prefix = prefix;
3985 -
3986 4239 rc_free = mem_pool_alloc(&fi_mem_pool, cmd_save * sizeof(*rc_free));
3987 - for (i = 0; i < (cmd_save - 1); i++)
4240 + for (unsigned int i = 0; i < (cmd_save - 1); i++)
3988 4241 rc_free[i].next = &rc_free[i + 1];
3989 4242 rc_free[cmd_save - 1].next = NULL;
3990 4243
3991 4244 start_packfile();
3992 4245 set_die_routine(die_nicely);
3993 4246 set_checkpoint_signal();
3994 - while (read_next_command() != EOF) {
4247 + while (read_next_command(&state) != EOF) {
3995 4248 const char *v;
3996 4249 if (!strcmp("blob", command_buf.buf))
3997 - parse_new_blob();
4250 + parse_new_blob(&state);
3998 4251 else if (skip_prefix(command_buf.buf, "commit ", &v))
3999 - parse_new_commit(v);
4252 + parse_new_commit(&state, v);
4000 4253 else if (skip_prefix(command_buf.buf, "tag ", &v))
4001 - parse_new_tag(v);
4254 + parse_new_tag(&state, v);
4002 4255 else if (skip_prefix(command_buf.buf, "reset ", &v))
4003 - parse_reset_branch(v);
4256 + parse_reset_branch(&state, v);
4004 4257 else if (skip_prefix(command_buf.buf, "ls ", &v))
4005 - parse_ls(v, NULL);
4258 + parse_ls(&state, v, NULL);
4006 4259 else if (skip_prefix(command_buf.buf, "cat-blob ", &v))
4007 - parse_cat_blob(v);
4260 + parse_cat_blob(&state, v);
4008 4261 else if (skip_prefix(command_buf.buf, "get-mark ", &v))
4009 - parse_get_mark(v);
4262 + parse_get_mark(&state, v);
4010 4263 else if (!strcmp("checkpoint", command_buf.buf))
4011 - parse_checkpoint();
4264 + parse_checkpoint(&state);
4012 4265 else if (!strcmp("done", command_buf.buf))
4013 4266 break;
4014 4267 else if (!strcmp("alias", command_buf.buf))
4015 - parse_alias();
4268 + parse_alias(&state);
4016 4269 else if (starts_with(command_buf.buf, "progress "))
4017 - parse_progress();
4270 + parse_progress(&state);
4018 4271 else if (skip_prefix(command_buf.buf, "feature ", &v))
4019 - parse_feature(v);
4272 + parse_feature(&state, v);
4020 4273 else if (skip_prefix(command_buf.buf, "option git ", &v))
4021 - parse_option(v);
4274 + parse_option(&state, v);
4022 4275 else if (starts_with(command_buf.buf, "option "))
4023 4276 /* ignore non-git options*/;
4024 4277 else
@@ -4029,8 +4282,8 @@ int cmd_fast_import(int argc,
4029 4282 }
4030 4283
4031 4284 /* argv hasn't been parsed yet, do so */
4032 - if (!seen_data_command)
4033 - parse_argv();
4285 + if (!state.seen_data_command)
4286 + parse_argv(&state);
4034 4287
4035 4288 if (require_explicit_termination && feof(stdin))
4036 4289 die(_("stream ends early"));
@@ -4047,9 +4300,9 @@ int cmd_fast_import(int argc,
4047 4300
4048 4301 if (show_stats) {
4049 4302 uintmax_t total_count = 0, duplicate_count = 0;
4050 - for (i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
4303 + for (size_t i = 0; i < ARRAY_SIZE(object_count_by_type); i++)
4051 4304 total_count += object_count_by_type[i];
4052 - for (i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
4305 + for (size_t i = 0; i < ARRAY_SIZE(duplicate_count_by_type); i++)
4053 4306 duplicate_count += duplicate_count_by_type[i];
4054 4307
4055 4308 fprintf(stderr, "%s statistics:\n", argv[0]);
modified builtin/fsck.c
@@ -37,10 +37,8 @@ static int show_root;
37 37 static int show_tags;
38 38 static int show_unreachable;
39 39 static int include_reflogs = 1;
40 - static int check_full = 1;
41 40 static int connectivity_only;
42 41 static int check_strict;
43 - static int keep_cache_objects;
44 42 static struct fsck_options fsck_walk_options;
45 43 static struct fsck_options fsck_obj_options;
46 44 static int errors_found;
@@ -48,8 +46,6 @@ static int write_lost_and_found;
48 46 static int verbose;
49 47 static int show_progress = -1;
50 48 static int show_dangling = 1;
51 - static int name_objects;
52 - static int check_references = 1;
53 49 static timestamp_t now;
54 50 #define ERROR_OBJECT 01
55 51 #define ERROR_REACHABLE 02
@@ -401,14 +397,27 @@ static void check_connectivity(struct repository *repo)
401 397 }
402 398 }
403 399
404 - static int fsck_obj(struct repository *repo,
405 - struct object *obj, void *buffer, unsigned long size)
400 + static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
401 + unsigned long size, void *buffer, int *eaten, void *cb_data)
406 402 {
403 + struct repository *repo = cb_data;
404 + struct object *obj;
407 405 int err;
408 406
409 - if (obj->flags & SEEN)
410 - return 0;
411 - obj->flags |= SEEN;
407 + /*
408 + * Note, buffer may be NULL if type is OBJ_BLOB. See
409 + * verify_packfile(), data_valid variable for details.
410 + */
411 + obj = parse_object_buffer(repo, oid, type, size, buffer, eaten);
412 + if (!obj) {
413 + errors_found |= ERROR_OBJECT;
414 + err = error(_("%s: object corrupt or missing"),
415 + oid_to_hex(oid));
416 + goto out;
417 + }
418 +
419 + obj->flags &= ~REACHABLE;
420 + obj->flags |= HAS_OBJ | SEEN;
412 421
413 422 if (verbose)
414 423 fprintf_ln(stderr, _("Checking %s %s"),
@@ -417,6 +426,7 @@ static int fsck_obj(struct repository *repo,
417 426
418 427 if (fsck_walk(obj, NULL, &fsck_obj_options))
419 428 objerror(repo, obj, _("broken links"));
429 +
420 430 err = fsck_object(obj, buffer, size, &fsck_obj_options);
421 431 if (err)
422 432 goto out;
@@ -442,32 +452,11 @@ static int fsck_obj(struct repository *repo,
442 452 }
443 453
444 454 out:
445 - if (obj->type == OBJ_TREE)
455 + if (obj && obj->type == OBJ_TREE)
446 456 free_tree_buffer((struct tree *)obj);
447 457 return err;
448 458 }
449 459
450 - static int fsck_obj_buffer(const struct object_id *oid, enum object_type type,
451 - unsigned long size, void *buffer, int *eaten, void *cb_data)
452 - {
453 - struct repository *repo = cb_data;
454 - struct object *obj;
455 -
456 - /*
457 - * Note, buffer may be NULL if type is OBJ_BLOB. See
458 - * verify_packfile(), data_valid variable for details.
459 - */
460 - obj = parse_object_buffer(repo, oid, type, size, buffer, eaten);
461 - if (!obj) {
462 - errors_found |= ERROR_OBJECT;
463 - return error(_("%s: object corrupt or missing"),
464 - oid_to_hex(oid));
465 - }
466 - obj->flags &= ~(REACHABLE | SEEN);
467 - obj->flags |= HAS_OBJ;
468 - return fsck_obj(repo, obj, buffer, size);
469 - }
470 -
471 460 static int default_refs;
472 461
473 462 static void fsck_handle_reflog_oid(struct repository *repo,
@@ -722,7 +711,6 @@ static int fsck_loose(const struct object_id *oid, const char *path,
722 711 void *cb_data)
723 712 {
724 713 struct for_each_loose_cb *data = cb_data;
725 - struct object *obj;
726 714 enum object_type type = OBJ_NONE;
727 715 size_t size;
728 716 void *contents = NULL;
@@ -751,21 +739,7 @@ static int fsck_loose(const struct object_id *oid, const char *path,
751 739 if (!contents && type != OBJ_BLOB)
752 740 BUG("read_loose_object streamed a non-blob");
753 741
754 - obj = parse_object_buffer(data->repo, oid, type, size,
755 - contents, &eaten);
756 -
757 - if (!obj) {
758 - errors_found |= ERROR_OBJECT;
759 - error(_("%s: object could not be parsed: %s"),
760 - oid_to_hex(oid), path);
761 - if (!eaten)
762 - free(contents);
763 - return 0; /* keep checking other objects */
764 - }
765 -
766 - obj->flags &= ~(REACHABLE | SEEN);
767 - obj->flags |= HAS_OBJ;
768 - if (fsck_obj(data->repo, obj, contents, size))
742 + if (fsck_obj_buffer(oid, type, size, contents, &eaten, data->repo))
769 743 errors_found |= ERROR_OBJECT;
770 744
771 745 if (!eaten)
@@ -986,30 +960,36 @@ static char const * const fsck_usage[] = {
986 960 NULL
987 961 };
988 962
989 - static struct option fsck_opts[] = {
990 - OPT__VERBOSE(&verbose, N_("be verbose")),
991 - OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
992 - OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
993 - OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
994 - OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
995 - OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
996 - OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
997 - OPT_BOOL(0, "full", &check_full, N_("also consider packs and alternate objects")),
998 - OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
999 - OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
1000 - OPT_BOOL(0, "lost-found", &write_lost_and_found,
1001 - N_("write dangling objects in .git/lost-found")),
1002 - OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
1003 - OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
1004 - OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")),
1005 - OPT_END(),
1006 - };
1007 -
1008 963 int cmd_fsck(int argc,
1009 964 const char **argv,
1010 965 const char *prefix,
1011 966 struct repository *repo)
1012 967 {
968 + struct odb_fsck_options odb_fsck_opts = {
969 + .flags = ODB_FSCK_FULL,
970 + };
971 + int keep_cache_objects = 0;
972 + int name_objects = 0;
973 + int check_references = 1;
974 + struct option fsck_opts[] = {
975 + OPT__VERBOSE(&verbose, N_("be verbose")),
976 + OPT_BOOL(0, "unreachable", &show_unreachable, N_("show unreachable objects")),
977 + OPT_BOOL(0, "dangling", &show_dangling, N_("show dangling objects")),
978 + OPT_BOOL(0, "tags", &show_tags, N_("report tags")),
979 + OPT_BOOL(0, "root", &show_root, N_("report root nodes")),
980 + OPT_BOOL(0, "cache", &keep_cache_objects, N_("make index objects head nodes")),
981 + OPT_BOOL(0, "reflogs", &include_reflogs, N_("make reflogs head nodes (default)")),
982 + OPT_BIT(0, "full", &odb_fsck_opts.flags,
983 + N_("also consider packs and alternate objects"), ODB_FSCK_FULL),
984 + OPT_BOOL(0, "connectivity-only", &connectivity_only, N_("check only connectivity")),
985 + OPT_BOOL(0, "strict", &check_strict, N_("enable more strict checking")),
986 + OPT_BOOL(0, "lost-found", &write_lost_and_found,
987 + N_("write dangling objects in .git/lost-found")),
988 + OPT_BOOL(0, "progress", &show_progress, N_("show progress")),
989 + OPT_BOOL(0, "name-objects", &name_objects, N_("show verbose names for reachable objects")),
990 + OPT_BOOL(0, "references", &check_references, N_("check reference database consistency")),
991 + OPT_END(),
992 + };
1013 993 struct odb_source *source;
1014 994 struct snapshot snap = {
1015 995 .nr = 0,
@@ -1041,7 +1021,7 @@ int cmd_fsck(int argc,
1041 1021 show_progress = 0;
1042 1022
1043 1023 if (write_lost_and_found) {
1044 - check_full = 1;
1024 + odb_fsck_opts.flags |= ODB_FSCK_FULL;
1045 1025 include_reflogs = 0;
1046 1026 }
1047 1027
@@ -1069,11 +1049,14 @@ int cmd_fsck(int argc,
1069 1049 odb_for_each_object(repo->objects, NULL,
1070 1050 mark_object_for_connectivity, repo, 0);
1071 1051 } else {
1072 - odb_prepare_alternates(repo->objects);
1073 1052 for (source = repo->objects->sources; source; source = source->next)
1074 - fsck_source(repo, source);
1053 + if ((odb_fsck_opts.flags & ODB_FSCK_FULL) || source->local)
1054 + fsck_source(repo, source);
1055 +
1056 + if (odb_fsck(repo->objects, &odb_fsck_opts) < 0)
1057 + errors_found |= ERROR_OBJECT;
1075 1058
1076 - if (check_full) {
1059 + if (odb_fsck_opts.flags & ODB_FSCK_FULL) {
1077 1060 struct packed_git *p;
1078 1061 uint32_t total = 0, count = 0;
1079 1062 struct progress *progress = NULL;
@@ -1155,7 +1138,6 @@ int cmd_fsck(int argc,
1155 1138 if (repo->settings.core_commit_graph) {
1156 1139 struct child_process commit_graph_verify = CHILD_PROCESS_INIT;
1157 1140
1158 - odb_prepare_alternates(repo->objects);
1159 1141 for (source = repo->objects->sources; source; source = source->next) {
1160 1142 child_process_init(&commit_graph_verify);
1161 1143 commit_graph_verify.git_cmd = 1;
@@ -1173,7 +1155,6 @@ int cmd_fsck(int argc,
1173 1155 if (repo->settings.core_multi_pack_index) {
1174 1156 struct child_process midx_verify = CHILD_PROCESS_INIT;
1175 1157
1176 - odb_prepare_alternates(repo->objects);
1177 1158 for (source = repo->objects->sources; source; source = source->next) {
1178 1159 child_process_init(&midx_verify);
1179 1160 midx_verify.git_cmd = 1;
modified builtin/index-pack.c
@@ -763,7 +763,7 @@ static void find_ref_delta_children(const struct object_id *oid,
763 763
764 764 struct compare_data {
765 765 struct object_entry *entry;
766 - struct odb_read_stream *st;
766 + struct odb_stream *st;
767 767 unsigned char *buf;
768 768 unsigned long buf_size;
769 769 };
@@ -780,7 +780,7 @@ static int compare_objects(const unsigned char *buf, unsigned long size,
780 780 }
781 781
782 782 while (size) {
783 - ssize_t len = odb_read_stream_read(data->st, data->buf, size);
783 + ssize_t len = odb_stream_read(data->st, data->buf, size);
784 784 if (len == 0)
785 785 die(_("SHA1 COLLISION FOUND WITH %s !"),
786 786 oid_to_hex(&data->entry->idx.oid));
@@ -806,14 +806,14 @@ static int check_collison(struct object_entry *entry)
806 806
807 807 memset(&data, 0, sizeof(data));
808 808 data.entry = entry;
809 - data.st = odb_read_stream_open(the_repository->objects, &entry->idx.oid, NULL);
809 + data.st = odb_stream_from_object(the_repository->objects, &entry->idx.oid, NULL);
810 810 if (!data.st)
811 811 return -1;
812 812 if (data.st->size != entry->size || data.st->type != entry->type)
813 813 die(_("SHA1 COLLISION FOUND WITH %s !"),
814 814 oid_to_hex(&entry->idx.oid));
815 815 unpack_data(entry, compare_objects, &data);
816 - odb_read_stream_close(data.st);
816 + odb_stream_close(data.st);
817 817 free(data.buf);
818 818 return 0;
819 819 }
modified builtin/last-modified.c
@@ -290,7 +290,8 @@ static void process_parent(struct last_modified *lm,
290 290 {
291 291 struct bitmap *active_p;
292 292
293 - repo_parse_commit(lm->rev.repo, parent);
293 + if (repo_parse_commit(lm->rev.repo, parent))
294 + return;
294 295 active_p = active_paths_for(lm, parent);
295 296
296 297 /*
@@ -414,12 +415,14 @@ static int last_modified_run(struct last_modified *lm)
414 415 * Otherwise, make sure that 'c' isn't reachable from anything
415 416 * in the '--not' queue.
416 417 */
417 - repo_parse_commit(lm->rev.repo, c);
418 + if (repo_parse_commit(lm->rev.repo, c))
419 + goto cleanup;
418 420
419 421 while ((n = prio_queue_get(&not_queue))) {
420 422 struct commit_list *np;
421 423
422 - repo_parse_commit(lm->rev.repo, n);
424 + if (repo_parse_commit(lm->rev.repo, n))
425 + continue;
423 426
424 427 for (np = n->parents; np; np = np->next) {
425 428 if (!(np->item->object.flags & PARENT2)) {
modified builtin/pack-objects.c
@@ -261,8 +261,8 @@ static int exclude_promisor_objects_best_effort;
261 261
262 262 static int use_delta_islands;
263 263
264 - static unsigned long delta_cache_size = 0;
265 - static unsigned long max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
264 + static size_t delta_cache_size = 0;
265 + static size_t max_delta_cache_size = DEFAULT_DELTA_CACHE_SIZE;
266 266 static unsigned long cache_max_small_delta_size = 1000;
267 267
268 268 static unsigned long window_memory_limit = 0;
@@ -354,7 +354,8 @@ static void index_commit_for_bitmap(struct commit *commit)
354 354
355 355 static void *get_delta(struct object_entry *entry)
356 356 {
357 - unsigned long size, base_size, delta_size;
357 + unsigned long size, base_size;
358 + size_t delta_size;
358 359 void *buf, *base_buf, *delta_buf;
359 360 enum object_type type;
360 361 size_t size_st = 0, base_size_st = 0;
@@ -411,7 +412,7 @@ static unsigned long do_compress(void **pptr, unsigned long size)
411 412 return stream.total_out;
412 413 }
413 414
414 - static unsigned long write_large_blob_data(struct odb_read_stream *st, struct hashfile *f,
415 + static unsigned long write_large_blob_data(struct odb_stream *st, struct hashfile *f,
415 416 const struct object_id *oid)
416 417 {
417 418 git_zstream stream;
@@ -425,7 +426,7 @@ static unsigned long write_large_blob_data(struct odb_read_stream *st, struct ha
425 426 for (;;) {
426 427 ssize_t readlen;
427 428 int zret = Z_OK;
428 - readlen = odb_read_stream_read(st, ibuf, sizeof(ibuf));
429 + readlen = odb_stream_read(st, ibuf, sizeof(ibuf));
429 430 if (readlen == -1)
430 431 die(_("unable to read %s"), oid_to_hex(oid));
431 432
@@ -488,7 +489,7 @@ static void copy_pack_data(struct hashfile *f,
488 489 off_t len)
489 490 {
490 491 unsigned char *in;
491 - unsigned long avail;
492 + size_t avail;
492 493
493 494 while (len) {
494 495 in = use_pack(p, w_curs, offset, &avail);
@@ -521,15 +522,15 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
521 522 unsigned hdrlen;
522 523 enum object_type type;
523 524 void *buf;
524 - struct odb_read_stream *st = NULL;
525 + struct odb_stream *st = NULL;
525 526 const unsigned hashsz = the_hash_algo->rawsz;
526 527
527 528 if (!usable_delta) {
528 529 if (oe_type(entry) == OBJ_BLOB &&
529 530 oe_size_greater_than(&to_pack, entry,
530 531 repo_settings_get_big_file_threshold(the_repository)) &&
531 - (st = odb_read_stream_open(the_repository->objects, &entry->idx.oid,
532 - NULL)) != NULL) {
532 + (st = odb_stream_from_object(the_repository->objects, &entry->idx.oid,
533 + NULL)) != NULL) {
533 534 buf = NULL;
534 535 type = st->type;
535 536 size = st->size;
@@ -589,7 +590,7 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
589 590 dheader[--pos] = 128 | (--ofs & 127);
590 591 if (limit && hdrlen + sizeof(dheader) - pos + datalen + hashsz >= limit) {
591 592 if (st)
592 - odb_read_stream_close(st);
593 + odb_stream_close(st);
593 594 free(buf);
594 595 return 0;
595 596 }
@@ -603,7 +604,7 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
603 604 */
604 605 if (limit && hdrlen + hashsz + datalen + hashsz >= limit) {
605 606 if (st)
606 - odb_read_stream_close(st);
607 + odb_stream_close(st);
607 608 free(buf);
608 609 return 0;
609 610 }
@@ -613,7 +614,7 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
613 614 } else {
614 615 if (limit && hdrlen + datalen + hashsz >= limit) {
615 616 if (st)
616 - odb_read_stream_close(st);
617 + odb_stream_close(st);
617 618 free(buf);
618 619 return 0;
619 620 }
@@ -621,7 +622,7 @@ static unsigned long write_no_reuse_object(struct hashfile *f, struct object_ent
621 622 }
622 623 if (st) {
623 624 datalen = write_large_blob_data(st, f, &entry->idx.oid);
624 - odb_read_stream_close(st);
625 + odb_stream_close(st);
625 626 } else {
626 627 hashwrite(f, buf, datalen);
627 628 free(buf);
@@ -1759,7 +1760,7 @@ static int want_object_in_pack_mtime(const struct object_id *oid,
1759 1760 struct odb_source *source = the_repository->objects->sources->next;
1760 1761 for (; source; source = source->next) {
1761 1762 struct odb_source_files *files = odb_source_files_downcast(source);
1762 - if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0, NULL))
1763 + if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0))
1763 1764 return 0;
1764 1765 }
1765 1766 }
@@ -1779,14 +1780,12 @@ static int want_object_in_pack_mtime(const struct object_id *oid,
1779 1780 *found_offset = 0;
1780 1781 }
1781 1782
1782 - odb_prepare_alternates(the_repository->objects);
1783 -
1784 1783 for (source = the_repository->objects->sources; source; source = source->next) {
1785 1784 struct odb_source_files *files = odb_source_files_downcast(source);
1786 1785 struct multi_pack_index *m = get_multi_pack_index(files->packed);
1787 1786 struct pack_entry e;
1788 1787
1789 - if (m && fill_midx_entry(m, oid, &e, NULL)) {
1788 + if (m && fill_midx_entry(m, oid, &e)) {
1790 1789 want = want_object_in_pack_one(e.p, oid, exclude, found_pack, found_offset, found_mtime);
1791 1790 if (want != -1)
1792 1791 return want;
@@ -2259,8 +2258,7 @@ static void check_object(struct object_entry *entry, uint32_t object_index)
2259 2258 int have_base = 0;
2260 2259 struct object_id base_ref;
2261 2260 struct object_entry *base_entry;
2262 - unsigned long used, used_0;
2263 - unsigned long avail;
2261 + size_t used, used_0, avail;
2264 2262 off_t ofs;
2265 2263 unsigned char *buf, c;
2266 2264 enum object_type type;
@@ -2688,8 +2686,8 @@ struct unpacked {
2688 2686 unsigned depth;
2689 2687 };
2690 2688
2691 - static int delta_cacheable(unsigned long src_size, unsigned long trg_size,
2692 - unsigned long delta_size)
2689 + static int delta_cacheable(size_t src_size, size_t trg_size,
2690 + size_t delta_size)
2693 2691 {
2694 2692 if (max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)
2695 2693 return 0;
@@ -2772,8 +2770,7 @@ size_t oe_get_size_slow(struct packing_data *pack,
2772 2770 struct pack_window *w_curs;
2773 2771 unsigned char *buf;
2774 2772 enum object_type type;
2775 - unsigned long used, avail;
2776 - size_t size;
2773 + size_t used, avail, size;
2777 2774
2778 2775 if (e->type_ != OBJ_OFS_DELTA && e->type_ != OBJ_REF_DELTA) {
2779 2776 size_t sz;
@@ -2804,11 +2801,12 @@ size_t oe_get_size_slow(struct packing_data *pack,
2804 2801 }
2805 2802
2806 2803 static int try_delta(struct unpacked *trg, struct unpacked *src,
2807 - unsigned max_depth, unsigned long *mem_usage)
2804 + unsigned max_depth, size_t *mem_usage)
2808 2805 {
2809 2806 struct object_entry *trg_entry = trg->entry;
2810 2807 struct object_entry *src_entry = src->entry;
2811 - unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;
2808 + unsigned long trg_size, src_size, sizediff, max_size, sz;
2809 + size_t delta_size;
2812 2810 unsigned ref_depth;
2813 2811 enum object_type type;
2814 2812 void *delta_buf;
@@ -2972,9 +2970,9 @@ static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
2972 2970 return m;
2973 2971 }
2974 2972
2975 - static unsigned long free_unpacked(struct unpacked *n)
2973 + static size_t free_unpacked(struct unpacked *n)
2976 2974 {
2977 - unsigned long freed_mem = sizeof_delta_index(n->index);
2975 + size_t freed_mem = sizeof_delta_index(n->index);
2978 2976 free_delta_index(n->index);
2979 2977 n->index = NULL;
2980 2978 if (n->data) {
@@ -2991,7 +2989,7 @@ static void find_deltas(struct object_entry **list, unsigned *list_size,
2991 2989 {
2992 2990 uint32_t i, idx = 0, count = 0;
2993 2991 struct unpacked *array;
2994 - unsigned long mem_usage = 0;
2992 + size_t mem_usage = 0;
2995 2993
2996 2994 CALLOC_ARRAY(array, window);
2997 2995
@@ -3701,7 +3699,7 @@ static int git_pack_config(const char *k, const char *v,
3701 3699 return 0;
3702 3700 }
3703 3701 if (!strcmp(k, "pack.deltacachesize")) {
3704 - max_delta_cache_size = git_config_int(k, v, ctx->kvi);
3702 + max_delta_cache_size = git_config_size_t(k, v, ctx->kvi);
3705 3703 return 0;
3706 3704 }
3707 3705 if (!strcmp(k, "pack.deltacachelimit")) {
@@ -4171,7 +4169,7 @@ static void add_cruft_object_entry(const struct object_id *oid, enum object_type
4171 4169
4172 4170 for (; !found && source; source = source->next) {
4173 4171 struct odb_source_files *files = odb_source_files_downcast(source);
4174 - if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0, NULL))
4172 + if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0))
4175 4173 found = 1;
4176 4174 }
4177 4175
@@ -4520,7 +4518,6 @@ static void add_objects_in_unpacked_packs(void)
4520 4518 .source_infop = &source_info,
4521 4519 };
4522 4520
4523 - odb_prepare_alternates(to_pack.repo->objects);
4524 4521 for (source = to_pack.repo->objects->sources; source; source = source->next) {
4525 4522 struct odb_source_files *files = odb_source_files_downcast(source);
4526 4523
@@ -4637,7 +4634,7 @@ static int force_object_loose(struct odb_source *source,
4637 4634
4638 4635 for (struct odb_source *s = source->odb->sources; s; s = s->next) {
4639 4636 struct odb_source_files *files = odb_source_files_downcast(s);
4640 - if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0, NULL))
4637 + if (!odb_source_read_object_info(&files->loose->base, oid, NULL, 0))
4641 4638 return 0;
4642 4639 }
4643 4640
modified builtin/unpack-objects.c
@@ -358,51 +358,55 @@ static void unpack_non_delta_entry(enum object_type type, unsigned long size,
358 358 write_object(nr, type, buf, size);
359 359 }
360 360
361 - struct input_zstream_data {
361 + struct zlib_stream {
362 + struct odb_stream base;
362 363 git_zstream *zstream;
363 364 int status;
364 365 };
365 366
366 - static ssize_t feed_input_zstream(struct odb_write_stream *in_stream,
367 - unsigned char *buf, size_t buf_len)
367 + static ssize_t zlib_stream_read(struct odb_stream *in_stream,
368 + char *buf, size_t buf_len)
368 369 {
369 - struct input_zstream_data *data = in_stream->data;
370 + struct zlib_stream *data = container_of(in_stream, struct zlib_stream, base);
370 371 git_zstream *zstream = data->zstream;
371 - void *in = fill(1);
372 372
373 - if (in_stream->is_finished)
373 + if (data->status != Z_OK)
374 374 return 0;
375 375
376 - zstream->next_out = buf;
376 + zstream->next_out = (unsigned char *) buf;
377 377 zstream->avail_out = buf_len;
378 - zstream->next_in = in;
379 - zstream->avail_in = len;
380 378
381 - data->status = git_inflate(zstream, 0);
379 + while (data->status == Z_OK && zstream->avail_out == buf_len) {
380 + zstream->next_in = fill(1);
381 + zstream->avail_in = len;
382 + data->status = git_inflate(zstream, 0);
383 + use(len - zstream->avail_in);
384 + }
382 385
383 - in_stream->is_finished = data->status != Z_OK;
384 - use(len - zstream->avail_in);
385 386 return buf_len - zstream->avail_out;
386 387 }
387 388
388 389 static void stream_blob(unsigned long size, unsigned nr)
389 390 {
390 391 git_zstream zstream = { 0 };
391 - struct input_zstream_data data = { 0 };
392 - struct odb_write_stream in_stream = {
393 - .read = feed_input_zstream,
394 - .data = &data,
392 + struct zlib_stream in_stream = {
393 + .base = {
394 + .read = zlib_stream_read,
395 + .size = size,
396 + .type = OBJ_BLOB,
397 + },
398 + .zstream = &zstream,
399 + .status = Z_OK,
395 400 };
396 401 struct obj_info *info = &obj_list[nr];
397 402
398 - data.zstream = &zstream;
399 403 git_inflate_init(&zstream);
400 404
401 - if (odb_write_object_stream(the_repository->objects, &in_stream, size, &info->oid))
405 + if (odb_write_object_stream(the_repository->objects, &in_stream.base, &info->oid))
402 406 die(_("failed to write object in stream"));
403 407
404 - if (data.status != Z_STREAM_END)
405 - die(_("inflate returned (%d)"), data.status);
408 + if (in_stream.status != Z_STREAM_END)
409 + die(_("inflate returned (%d)"), in_stream.status);
406 410 git_inflate_end(&zstream);
407 411
408 412 if (strict) {
modified bundle.c
@@ -1,4 +1,3 @@
1 - #define USE_THE_REPOSITORY_VARIABLE
2 1 #define DISABLE_SIGN_COMPARE_WARNINGS
3 2
4 3 #include "git-compat-util.h"
@@ -21,6 +20,13 @@
21 20 #include "connected.h"
22 21 #include "write-or-die.h"
23 22
23 + /*
24 + * NEEDSWORK: this function implicitly depends on `the_repository` and is not
25 + * available because we dropped USE_THE_REPOSITORY_VARIABLE. We can remove the
26 + * declaration once it's accessible via `repo_config_values`.
27 + */
28 + extern const char *get_log_output_encoding(void);
29 +
24 30 static const char v2_bundle_signature[] = "# v2 git bundle\n";
25 31 static const char v3_bundle_signature[] = "# v3 git bundle\n";
26 32 static struct {
@@ -294,7 +300,8 @@ int list_bundle_refs(struct bundle_header *header, int argc, const char **argv)
294 300 return list_refs(&header->references, argc, argv);
295 301 }
296 302
297 - static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
303 + static int is_tag_in_date_range(struct repository *repo,
304 + struct object *tag, struct rev_info *revs)
298 305 {
299 306 size_t size;
300 307 enum object_type type;
@@ -305,7 +312,7 @@ static int is_tag_in_date_range(struct object *tag, struct rev_info *revs)
305 312 if (revs->max_age == -1 && revs->min_age == -1)
306 313 goto out;
307 314
308 - buf = odb_read_object(the_repository->objects, &tag->oid, &type, &size);
315 + buf = odb_read_object(repo->objects, &tag->oid, &type, &size);
309 316 if (!buf)
310 317 goto out;
311 318 line = memmem(buf, size, "\ntagger ", 8);
@@ -325,50 +332,52 @@ out:
325 332
326 333
327 334 /* Write the pack data to bundle_fd */
328 - static int write_pack_data(int bundle_fd, struct rev_info *revs, struct strvec *pack_options)
335 + static int write_pack_data(int bundle_fd, struct rev_info *revs, int progress)
329 336 {
330 - struct child_process pack_objects = CHILD_PROCESS_INIT;
337 + struct odb_generate_pack_options opts = ODB_GENERATE_PACK_OPTIONS_INIT;
338 + struct odb_pack_generator *generator;
339 + int ret = 0;
331 340 int i;
332 341
333 - strvec_pushl(&pack_objects.args,
334 - "pack-objects",
335 - "--stdout", "--thin", "--delta-base-offset",
336 - NULL);
337 - strvec_pushv(&pack_objects.args, pack_options->v);
342 + opts.thin = 1;
343 + opts.ofs_delta = 1;
344 + if (progress)
345 + opts.progress = ODB_GENERATE_PACK_PROGRESS_VERBOSE;
338 346 if (revs->filter.choice)
339 - strvec_pushf(&pack_objects.args, "--filter=%s",
340 - list_objects_filter_spec(&revs->filter));
341 - pack_objects.in = -1;
342 - pack_objects.out = bundle_fd;
343 - pack_objects.git_cmd = 1;
347 + opts.filter_spec = list_objects_filter_spec(&revs->filter);
344 348
345 349 /*
346 - * start_command() will close our descriptor if it's >1. Duplicate it
347 - * to avoid surprising the caller.
350 + * The pack generator will consume our descriptor if it's >1.
351 + * Duplicate it to avoid surprising the caller.
348 352 */
349 - if (pack_objects.out > 1) {
350 - pack_objects.out = dup(pack_objects.out);
351 - if (pack_objects.out < 0) {
352 - error_errno(_("unable to dup bundle descriptor"));
353 - child_process_clear(&pack_objects);
354 - return -1;
355 - }
353 + opts.pack_fd = bundle_fd;
354 + if (opts.pack_fd > 1) {
355 + opts.pack_fd = dup(bundle_fd);
356 + if (opts.pack_fd < 0)
357 + return error_errno(_("unable to dup bundle descriptor"));
356 358 }
357 359
358 - if (start_command(&pack_objects))
359 - return error(_("Could not spawn pack-objects"));
360 -
361 360 for (i = 0; i < revs->pending.nr; i++) {
362 361 struct object *object = revs->pending.objects[i].item;
363 362 if (object->flags & UNINTERESTING)
364 - write_or_die(pack_objects.in, "^", 1);
365 - write_or_die(pack_objects.in, oid_to_hex(&object->oid), the_hash_algo->hexsz);
366 - write_or_die(pack_objects.in, "\n", 1);
363 + oid_array_append(&opts.haves, &object->oid);
364 + else
365 + oid_array_append(&opts.wants, &object->oid);
367 366 }
368 - close(pack_objects.in);
369 - if (finish_command(&pack_objects))
370 - return error(_("pack-objects died"));
371 - return 0;
367 +
368 + if (odb_generate_pack(revs->repo->objects, &generator, &opts)) {
369 + ret = error(_("Could not spawn pack-objects"));
370 + goto out;
371 + }
372 +
373 + if (odb_pack_generator_finish(generator)) {
374 + ret = error(_("pack-objects died"));
375 + goto out;
376 + }
377 +
378 + out:
379 + odb_generate_pack_options_release(&opts);
380 + return ret;
372 381 }
373 382
374 383 /*
@@ -395,10 +404,10 @@ static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
395 404
396 405 if (e->item->flags & UNINTERESTING)
397 406 continue;
398 - if (repo_dwim_ref(the_repository, e->name, strlen(e->name),
407 + if (repo_dwim_ref(revs->repo, e->name, strlen(e->name),
399 408 &oid, &ref, 0) != 1)
400 409 goto skip_write_ref;
401 - if (refs_read_ref_full(get_main_ref_store(the_repository), e->name, RESOLVE_REF_READING, &oid, &flag))
410 + if (refs_read_ref_full(get_main_ref_store(revs->repo), e->name, RESOLVE_REF_READING, &oid, &flag))
402 411 flag = 0;
403 412 display_ref = (flag & REF_ISSYMREF) ? e->name : ref;
404 413
@@ -406,7 +415,7 @@ static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
406 415 goto skip_write_ref;
407 416
408 417 if (e->item->type == OBJ_TAG &&
409 - !is_tag_in_date_range(e->item, revs)) {
418 + !is_tag_in_date_range(revs->repo, e->item, revs)) {
410 419 e->item->flags |= UNINTERESTING;
411 420 goto skip_write_ref;
412 421 }
@@ -428,7 +437,8 @@ static int write_bundle_refs(int bundle_fd, struct rev_info *revs)
428 437
429 438 ref_count++;
430 439 strset_add(&objects, display_ref);
431 - write_or_die(bundle_fd, oid_to_hex(&e->item->oid), the_hash_algo->hexsz);
440 + write_or_die(bundle_fd, oid_to_hex(&e->item->oid),
441 + revs->repo->hash_algo->hexsz);
432 442 write_or_die(bundle_fd, " ", 1);
433 443 write_or_die(bundle_fd, display_ref, strlen(display_ref));
434 444 write_or_die(bundle_fd, "\n", 1);
@@ -476,7 +486,7 @@ static void write_bundle_prerequisites(struct commit *commit, void *data)
476 486 }
477 487
478 488 int create_bundle(struct repository *r, const char *path,
479 - int argc, const char **argv, struct strvec *pack_options, int version)
489 + int argc, const char **argv, int version, int progress)
480 490 {
481 491 struct lock_file lock = LOCK_INIT;
482 492 int bundle_fd = -1;
@@ -507,7 +517,7 @@ int create_bundle(struct repository *r, const char *path,
507 517 * SHA1.
508 518 * 2. @filter is required because we parsed an object filter.
509 519 */
510 - if (the_hash_algo != &hash_algos[GIT_HASH_SHA1_LEGACY] || revs.filter.choice)
520 + if (r->hash_algo != &hash_algos[GIT_HASH_SHA1_LEGACY] || revs.filter.choice)
511 521 min_version = 3;
512 522
513 523 if (argc > 1) {
@@ -528,14 +538,15 @@ int create_bundle(struct repository *r, const char *path,
528 538 if (version < 2 || version > 3) {
529 539 die(_("unsupported bundle version %d"), version);
530 540 } else if (version < min_version) {
531 - die(_("cannot write bundle version %d with algorithm %s"), version, the_hash_algo->name);
541 + die(_("cannot write bundle version %d with algorithm %s"), version,
542 + r->hash_algo->name);
532 543 } else if (version == 2) {
533 544 write_or_die(bundle_fd, v2_bundle_signature, strlen(v2_bundle_signature));
534 545 } else {
535 546 const char *capability = "@object-format=";
536 547 write_or_die(bundle_fd, v3_bundle_signature, strlen(v3_bundle_signature));
537 548 write_or_die(bundle_fd, capability, strlen(capability));
538 - write_or_die(bundle_fd, the_hash_algo->name, strlen(the_hash_algo->name));
549 + write_or_die(bundle_fd, r->hash_algo->name, strlen(r->hash_algo->name));
539 550 write_or_die(bundle_fd, "\n", 1);
540 551
541 552 if (revs.filter.choice) {
@@ -584,7 +595,7 @@ int create_bundle(struct repository *r, const char *path,
584 595 }
585 596
586 597 /* write pack */
587 - if (write_pack_data(bundle_fd, &revs_copy, pack_options)) {
598 + if (write_pack_data(bundle_fd, &revs_copy, progress)) {
588 599 ret = -1;
589 600 goto out;
590 601 }
modified bundle.h
@@ -27,8 +27,7 @@ int read_bundle_header(const char *path, struct bundle_header *header);
27 27 int read_bundle_header_fd(int fd, struct bundle_header *header,
28 28 const char *report_path);
29 29 int create_bundle(struct repository *r, const char *path,
30 - int argc, const char **argv, struct strvec *pack_options,
31 - int version);
30 + int argc, const char **argv, int version, int progress);
32 31
33 32 enum verify_bundle_flags {
34 33 VERIFY_BUNDLE_VERBOSE = (1 << 0),
modified commit-graph.c
@@ -651,8 +651,6 @@ struct commit_graph *load_commit_graph_chain_fd_st(struct object_database *odb,
651 651 count = st->st_size / (odb->repo->hash_algo->hexsz + 1);
652 652 CALLOC_ARRAY(oids, count);
653 653
654 - odb_prepare_alternates(odb);
655 -
656 654 for (i = 0; i < count; i++) {
657 655 struct odb_source *source;
658 656
@@ -768,7 +766,6 @@ static struct commit_graph *prepare_commit_graph(struct repository *r)
768 766 if (!commit_graph_compatible(r))
769 767 return NULL;
770 768
771 - odb_prepare_alternates(r->objects);
772 769 for (source = r->objects->sources; source; source = source->next) {
773 770 r->objects->commit_graph = read_commit_graph_one(source);
774 771 if (r->objects->commit_graph)
@@ -2018,7 +2015,6 @@ static void fill_oids_from_all_packs(struct write_commit_graph_context *ctx)
2018 2015 _("Finding commits for commit graph among packed objects"),
2019 2016 ctx->approx_nr_objects);
2020 2017
2021 - odb_prepare_alternates(ctx->r->objects);
2022 2018 for (source = ctx->r->objects->sources; source; source = source->next) {
2023 2019 struct odb_source_files *files = odb_source_files_downcast(source);
2024 2020 odb_source_for_each_object(&files->packed->base, &oi, add_packed_commits_oi,
modified commit-reach.c
@@ -11,6 +11,7 @@
11 11 #include "tag.h"
12 12 #include "commit-reach.h"
13 13 #include "ewah/ewok.h"
14 + #include "trace2.h"
14 15
15 16 /* Remember to update object flag allocation in object.h */
16 17 #define PARENT1 (1u<<16)
@@ -78,25 +79,111 @@ static void clear_nonstale_queue(struct nonstale_queue *queue)
78 79 queue->max_nonstale = NULL;
79 80 }
80 81
81 - static void nonstale_queue_put_dedup(struct nonstale_queue *queue,
82 - struct commit *c)
82 + /*
83 + * Priority queue with per-side commit counters for paint_down_to_common().
84 + * Each non-stale queued commit occupies exactly one bucket: PARENT1-only,
85 + * PARENT2-only, or both (a pending merge-base candidate).
86 + */
87 + struct paint_state {
88 + struct prio_queue queue;
89 + size_t parent1_count;
90 + size_t parent2_count;
91 + size_t mb_candidate_count;
92 + timestamp_t min_generation;
93 + timestamp_t last_gen;
94 + timestamp_t topo_ceiling;
95 + };
96 +
97 + static void paint_count_update(struct paint_state *state,
98 + unsigned flags, int delta)
83 99 {
84 - if (c->object.flags & ENQUEUED)
85 - return;
86 - c->object.flags |= ENQUEUED;
87 - nonstale_queue_put(queue, c);
100 + switch (flags & (PARENT1 | PARENT2 | STALE)) {
101 + case PARENT1:
102 + state->parent1_count += delta;
103 + break;
104 +
105 + case PARENT2:
106 + state->parent2_count += delta;
107 + break;
108 +
109 + case PARENT1 | PARENT2:
110 + state->mb_candidate_count += delta;
111 + break;
112 +
113 + case PARENT1 | PARENT2 | STALE:
114 + break;
115 +
116 + default:
117 + BUG("unexpected paint state");
118 + }
88 119 }
89 120
90 - static struct commit *nonstale_queue_get_dedup(struct nonstale_queue *queue)
121 + static void paint_queue_put(struct paint_state *state,
122 + struct commit *c, unsigned add_flags)
91 123 {
92 - struct commit *commit = nonstale_queue_get(queue);
124 + unsigned old_flags = c->object.flags;
125 + c->object.flags |= add_flags;
126 +
127 + if (old_flags & ENQUEUED) {
128 + paint_count_update(state, old_flags, -1);
129 + paint_count_update(state, c->object.flags, 1);
130 + } else {
131 + c->object.flags |= ENQUEUED;
132 + prio_queue_put(&state->queue, c);
133 + paint_count_update(state, c->object.flags, 1);
134 + }
135 + }
136 +
137 + /*
138 + * Dequeue the next commit for the paint walk, or return NULL when
139 + * no more merge bases can be discovered.
140 + */
141 + static struct commit *paint_queue_get(struct paint_state *state)
142 + {
143 + struct commit *commit = prio_queue_get(&state->queue);
144 + timestamp_t generation;
145 +
146 + if (!commit)
147 + return NULL;
148 +
149 + commit->object.flags &= ~ENQUEUED;
150 + generation = commit_graph_generation(commit);
93 151
94 - if (commit)
95 - commit->object.flags &= ~ENQUEUED;
152 + if (state->min_generation && generation > state->last_gen)
153 + BUG("bad generation skip %"PRItime" > %"PRItime" at %s",
154 + generation, state->last_gen,
155 + oid_to_hex(&commit->object.oid));
156 + state->last_gen = generation;
157 +
158 + /* generation cutoff */
159 + if (generation < state->min_generation)
160 + return NULL;
161 +
162 + /*
163 + * Check exit condition before decrementing: the counters
164 + * still include this commit, so the last non-stale commit
165 + * sees a non-zero count and is returned for processing.
166 + */
167 + if (!state->mb_candidate_count) {
168 + /* only stale entries remain */
169 + if (!state->parent1_count && !state->parent2_count)
170 + return NULL;
171 +
172 + /* one side is exhausted */
173 + if ((!state->parent1_count || !state->parent2_count) &&
174 + generation < state->topo_ceiling)
175 + return NULL;
176 + }
177 +
178 + paint_count_update(state, commit->object.flags, -1);
96 179 return commit;
97 180 }
98 181
99 - /* all input commits in one and twos[] must have been parsed! */
182 + /*
183 + * See Documentation/technical/paint-down-to-common.adoc
184 + *
185 + * All input commits in one and twos[] must have been parsed!
186 + */
100 187 static int paint_down_to_common(struct repository *r,
101 188 struct commit *one, int n,
102 189 struct commit **twos,
@@ -104,45 +191,40 @@ static int paint_down_to_common(struct repository *r,
104 191 enum merge_base_flags mb_flags,
105 192 struct commit_list **result)
106 193 {
107 - struct nonstale_queue queue = {
108 - { compare_commits_by_gen_then_commit_date }
194 + /*
195 + * Generation ordering is required for the side-exhaustion and
196 + * single-result early exits, which rely on topological traversal
197 + * order (children visited before parents) in the ordered region.
198 + */
199 + struct paint_state state = {
200 + .queue = { compare_commits_by_gen_then_commit_date }
109 201 };
202 + struct commit *commit;
110 203 int i;
111 - int gen_ordered = 1;
112 - timestamp_t last_gen = GENERATION_NUMBER_INFINITY;
204 + int steps = 0;
113 205 struct commit_list **tail = result;
114 206
115 - if (!min_generation && !corrected_commit_dates_enabled(r)) {
116 - queue.pq.compare = compare_commits_by_commit_date;
117 - gen_ordered = 0;
118 - }
207 + state.min_generation = min_generation;
208 + state.last_gen = GENERATION_NUMBER_INFINITY;
209 + state.topo_ceiling = corrected_commit_dates_enabled(r)
210 + ? GENERATION_NUMBER_INFINITY
211 + : GENERATION_NUMBER_V1_MAX;
212 +
119 213
120 214 one->object.flags |= PARENT1;
121 215 if (!n) {
122 216 commit_list_append(one, result);
123 217 return 0;
124 218 }
125 - nonstale_queue_put_dedup(&queue, one);
219 + paint_queue_put(&state, one, 0);
126 220
127 - for (i = 0; i < n; i++) {
128 - twos[i]->object.flags |= PARENT2;
129 - nonstale_queue_put_dedup(&queue, twos[i]);
130 - }
221 + for (i = 0; i < n; i++)
222 + paint_queue_put(&state, twos[i], PARENT2);
131 223
132 - while (queue.max_nonstale) {
133 - struct commit *commit = nonstale_queue_get_dedup(&queue);
224 + while ((commit = paint_queue_get(&state))) {
134 225 struct commit_list *parents;
135 226 int flags;
136 - timestamp_t generation = commit_graph_generation(commit);
137 -
138 - if (min_generation && generation > last_gen)
139 - BUG("bad generation skip %"PRItime" > %"PRItime" at %s",
140 - generation, last_gen,
141 - oid_to_hex(&commit->object.oid));
142 - last_gen = generation;
143 -
144 - if (generation < min_generation)
145 - break;
227 + steps++;
146 228
147 229 flags = commit->object.flags & (PARENT1 | PARENT2 | STALE);
148 230 if (flags == (PARENT1 | PARENT2)) {
@@ -155,8 +237,7 @@ static int paint_down_to_common(struct repository *r,
155 237 * descendant of this one.
156 238 */
157 239 if (!(mb_flags & MERGE_BASE_FIND_ALL) &&
158 - gen_ordered &&
159 - generation < GENERATION_NUMBER_INFINITY)
240 + state.last_gen < state.topo_ceiling)
160 241 break;
161 242 }
162 243 /* Mark parents of a found merge stale */
@@ -169,7 +250,7 @@ static int paint_down_to_common(struct repository *r,
169 250 if ((p->object.flags & flags) == flags)
170 251 continue;
171 252 if (repo_parse_commit(r, p)) {
172 - clear_nonstale_queue(&queue);
253 + clear_prio_queue(&state.queue);
173 254 commit_list_free(*result);
174 255 *result = NULL;
175 256 /*
@@ -184,12 +265,13 @@ static int paint_down_to_common(struct repository *r,
184 265 return error(_("could not parse commit %s"),
185 266 oid_to_hex(&p->object.oid));
186 267 }
187 - p->object.flags |= flags;
188 - nonstale_queue_put_dedup(&queue, p);
268 + paint_queue_put(&state, p, flags);
189 269 }
190 270 }
191 271
192 - clear_nonstale_queue(&queue);
272 + clear_prio_queue(&state.queue);
273 + trace2_data_intmax("paint_down_to_common", r,
274 + "steps", steps);
193 275 commit_list_sort_by_date(result);
194 276 return 0;
195 277 }
modified compat/pread.c
@@ -7,6 +7,8 @@ ssize_t git_pread(int fd, void *buf, size_t count, off_t offset)
7 7 ssize_t rc;
8 8
9 9 current_offset = lseek(fd, 0, SEEK_CUR);
10 + if (current_offset < 0)
11 + return -1;
10 12
11 13 if (lseek(fd, offset, SEEK_SET) < 0)
12 14 return -1;
modified config.c
@@ -1271,6 +1271,15 @@ ssize_t git_config_ssize_t(const char *name, const char *value,
1271 1271 return ret;
1272 1272 }
1273 1273
1274 + size_t git_config_size_t(const char *name, const char *value,
1275 + const struct key_value_info *kvi)
1276 + {
1277 + size_t ret;
1278 + if (!git_parse_size_t(value, &ret))
1279 + die_bad_number(name, value, kvi);
1280 + return ret;
1281 + }
1282 +
1274 1283 double git_config_double(const char *name, const char *value,
1275 1284 const struct key_value_info *kvi)
1276 1285 {
modified config.h
@@ -282,6 +282,9 @@ unsigned long git_config_ulong(const char *, const char *,
282 282 ssize_t git_config_ssize_t(const char *, const char *,
283 283 const struct key_value_info *);
284 284
285 + size_t git_config_size_t(const char *, const char *,
286 + const struct key_value_info *);
287 +
285 288 /**
286 289 * Identically to `git_config_double`, but for double-precision floating point
287 290 * values.
modified delta.h
@@ -14,7 +14,7 @@ struct delta_index;
14 14 * using free_delta_index().
15 15 */
16 16 struct delta_index *
17 - create_delta_index(const void *buf, unsigned long bufsize);
17 + create_delta_index(const void *buf, size_t bufsize);
18 18
19 19 /*
20 20 * free_delta_index: free the index created by create_delta_index()
@@ -28,7 +28,7 @@ void free_delta_index(struct delta_index *index);
28 28 *
29 29 * Given pointer must be what create_delta_index() returned, or NULL.
30 30 */
31 - unsigned long sizeof_delta_index(struct delta_index *index);
31 + size_t sizeof_delta_index(struct delta_index *index);
32 32
33 33 /*
34 34 * create_delta: create a delta from given index for the given buffer
@@ -42,8 +42,8 @@ unsigned long sizeof_delta_index(struct delta_index *index);
42 42 */
43 43 void *
44 44 create_delta(const struct delta_index *index,
45 - const void *buf, unsigned long bufsize,
46 - unsigned long *delta_size, unsigned long max_delta_size);
45 + const void *buf, size_t bufsize,
46 + size_t *delta_size, size_t max_delta_size);
47 47
48 48 /*
49 49 * diff_delta: create a delta from source buffer to target buffer
@@ -54,9 +54,9 @@ create_delta(const struct delta_index *index,
54 54 * updated with its size. The returned buffer must be freed by the caller.
55 55 */
56 56 static inline void *
57 - diff_delta(const void *src_buf, unsigned long src_bufsize,
58 - const void *trg_buf, unsigned long trg_bufsize,
59 - unsigned long *delta_size, unsigned long max_delta_size)
57 + diff_delta(const void *src_buf, size_t src_bufsize,
58 + const void *trg_buf, size_t trg_bufsize,
59 + size_t *delta_size, size_t max_delta_size)
60 60 {
61 61 struct delta_index *index = create_delta_index(src_buf, src_bufsize);
62 62 if (index) {
modified diff-delta.c
@@ -125,14 +125,14 @@ struct unpacked_index_entry {
125 125 };
126 126
127 127 struct delta_index {
128 - unsigned long memsize;
128 + size_t memsize;
129 129 const void *src_buf;
130 - unsigned long src_size;
130 + size_t src_size;
131 131 unsigned int hash_mask;
132 132 struct index_entry *hash[FLEX_ARRAY];
133 133 };
134 134
135 - struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
135 + struct delta_index * create_delta_index(const void *buf, size_t bufsize)
136 136 {
137 137 unsigned int i, hsize, hmask, entries, prev_val, *hash_count;
138 138 const unsigned char *data, *buffer = buf;
@@ -140,7 +140,7 @@ struct delta_index * create_delta_index(const void *buf, unsigned long bufsize)
140 140 struct unpacked_index_entry *entry, **hash;
141 141 struct index_entry *packed_entry, **packed_hash;
142 142 void *mem;
143 - unsigned long memsize;
143 + size_t memsize;
144 144
145 145 if (!buf || !bufsize)
146 146 return NULL;
@@ -302,7 +302,7 @@ void free_delta_index(struct delta_index *index)
302 302 free(index);
303 303 }
304 304
305 - unsigned long sizeof_delta_index(struct delta_index *index)
305 + size_t sizeof_delta_index(struct delta_index *index)
306 306 {
307 307 if (index)
308 308 return index->memsize;
@@ -318,8 +318,8 @@ unsigned long sizeof_delta_index(struct delta_index *index)
318 318
319 319 void *
320 320 create_delta(const struct delta_index *index,
321 - const void *trg_buf, unsigned long trg_size,
322 - unsigned long *delta_size, unsigned long max_size)
321 + const void *trg_buf, size_t trg_size,
322 + size_t *delta_size, size_t max_size)
323 323 {
324 324 unsigned int i, val;
325 325 off_t outpos, moff;
modified diff.c
@@ -3586,7 +3586,7 @@ static unsigned char *deflate_it(char *data,
3586 3586 unsigned long size,
3587 3587 unsigned long *result_size)
3588 3588 {
3589 - int bound;
3589 + size_t bound;
3590 3590 unsigned char *deflated;
3591 3591 git_zstream stream;
3592 3592 struct repo_config_values *cfg = repo_config_values(the_repository);
@@ -3624,9 +3624,11 @@ static void emit_binary_diff_body(struct diff_options *o,
3624 3624 delta = NULL;
3625 3625 deflated = deflate_it(two->ptr, two->size, &deflate_size);
3626 3626 if (one->size && two->size) {
3627 + size_t delta_size_st = 0;
3627 3628 delta = diff_delta(one->ptr, one->size,
3628 3629 two->ptr, two->size,
3629 - &delta_size, deflate_size);
3630 + &delta_size_st, deflate_size);
3631 + delta_size = cast_size_t_to_ulong(delta_size_st);
3630 3632 if (delta) {
3631 3633 void *to_free = delta;
3632 3634 orig_size = delta_size;
@@ -6163,7 +6165,7 @@ struct option *add_diff_options(const struct option *opts,
6163 6165 N_("continue listing the history of a file beyond renames"),
6164 6166 PARSE_OPT_NOARG, diff_opt_follow),
6165 6167 OPT_INTEGER('l', NULL, &options->rename_limit,
6166 - N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
6168 + N_("limit to cheap rename/copy detection if the number of rename/copy targets exceeds this value")),
6167 6169
6168 6170 OPT_GROUP(N_("Diff algorithm options")),
6169 6171 OPT_CALLBACK_F(0, "minimal", options, NULL,
modified fetch-object-info.c
@@ -1,6 +1,7 @@
1 1 #include "git-compat-util.h"
2 2 #include "gettext.h"
3 3 #include "hex.h"
4 + #include "object.h"
4 5 #include "pkt-line.h"
5 6 #include "connect.h"
6 7 #include "oid-array.h"
@@ -9,20 +10,26 @@
9 10 #include "string-list.h"
10 11
11 12 /* Sends object-info command and its arguments into the request buffer. */
12 - static void send_object_info_request(const int fd_out, struct object_info_args *args)
13 + static void send_object_info_request(const int fd_out,
14 + const struct string_list *server_options,
15 + const struct oid_array *oids,
16 + unsigned ask_size,
17 + unsigned ask_type)
13 18 {
14 19 struct strbuf req_buf = STRBUF_INIT;
15 20
16 - write_command_and_capabilities(&req_buf, "object-info", args->server_options);
21 + write_command_and_capabilities(&req_buf, "object-info", server_options);
17 22
18 - if (unsorted_string_list_has_string(args->object_info_options, "size"))
23 + if (ask_size)
19 24 packet_buf_write(&req_buf, "size");
20 - else if (args->object_info_options->nr)
21 - BUG("only size should be in object_info_options");
22 25
23 - if (args->oids)
24 - for (size_t i = 0; i < args->oids->nr; i++)
25 - packet_buf_write(&req_buf, "oid %s", oid_to_hex(&args->oids->oid[i]));
26 + if (ask_type)
27 + packet_buf_write(&req_buf, "type");
28 +
29 + if (oids)
30 + for (size_t i = 0; i < oids->nr; i++)
31 + packet_buf_write(&req_buf, "oid %s",
32 + oid_to_hex(&oids->oid[i]));
26 33
27 34 packet_buf_flush(&req_buf);
28 35 if (write_in_full(fd_out, req_buf.buf, req_buf.len) < 0)
@@ -45,35 +52,42 @@ static int parse_object_size(const char *s, size_t *res)
45 52 return 0;
46 53 }
47 54
48 - int fetch_object_info(const enum protocol_version version, struct object_info_args *args,
49 - struct packet_reader *reader, struct object_info *object_info_data,
50 - const int stateless_rpc, const int fd_out)
55 + void fetch_object_info(const enum protocol_version version,
56 + const struct string_list *server_options,
57 + const struct oid_array *oids,
58 + struct packet_reader *reader,
59 + struct fetch_object_info_results *results,
60 + const int stateless_rpc,
61 + const int fd_out)
51 62 {
63 + unsigned ask_size = 0;
64 + unsigned ask_type = 0;
52 65 int size_index = -1;
66 + int type_index = -1;
67 + size_t wanted;
68 +
69 + results->nr = oids->nr;
70 + CALLOC_ARRAY(results->unrecognized, results->nr);
53 71
54 72 switch (version) {
55 73 case protocol_v2:
56 74 if (!server_supports_v2("object-info"))
57 75 die(_("object-info capability is not enabled on the server"));
58 - /*
59 - * When removing an element from the list it gets swapped by the
60 - * last element, iterate backwards to prevent elements skipping
61 - * evaluation.
62 - *
63 - * object_info_options->nr can be safely casted without overflow
64 - * because the number of options is a small known number (the
65 - * supported placeholders which currently are size and type).
66 - */
67 - for (int i = (int)args->object_info_options->nr - 1; i >= 0; i--)
68 - if (!server_supports_feature("object-info",
69 - args->object_info_options->items[i].string, 0))
70 - unsorted_string_list_delete_item(args->object_info_options, i, 0);
76 +
77 + if (results->wants_size &&
78 + server_supports_feature("object-info", "size", 0))
79 + ask_size = 1;
80 +
81 + if (results->wants_type &&
82 + server_supports_feature("object-info", "type", 0))
83 + ask_type = 1;
71 84
72 85 /*
73 86 * Even if no options are left, we still send the oid so we get
74 87 * at least an existence check.
75 88 */
76 - send_object_info_request(fd_out, args);
89 + send_object_info_request(fd_out, server_options, oids, ask_size,
90 + ask_type);
77 91 break;
78 92 case protocol_v1:
79 93 case protocol_v0:
@@ -81,43 +95,49 @@ int fetch_object_info(const enum protocol_version version, struct object_info_ar
81 95 case protocol_unknown_version:
82 96 BUG("unknown protocol version");
83 97 }
98 + wanted = ask_size + ask_type;
84 99
85 - for (size_t i = 0; i < args->object_info_options->nr; i++) {
100 + for (size_t i = 0; i < wanted; i++) {
86 101 if (packet_reader_read(reader) != PACKET_READ_NORMAL) {
87 102 check_stateless_delimiter(stateless_rpc, reader,
88 103 "stateless delimiter expected");
89 - return -1;
104 + die(_("object-info: expected %" PRIuMAX " attributes, got %" PRIuMAX),
105 + (uintmax_t)wanted, (uintmax_t)i);
90 106 }
91 107
92 - if (!unsorted_string_list_has_string(args->object_info_options, reader->line))
93 - return -1;
94 -
95 108 if (!strcmp(reader->line, "size")) {
96 - /*
97 - * i is the number of supported options which currently
98 - * is only size. No risk of overflow.
99 - */
109 + if (!ask_size)
110 + die(_("object-info: unrequested 'size' attribute"));
111 + if (results->sizes)
112 + die(_("object-info: duplicate 'size' attribute"));
100 113 size_index = (int)i;
101 - for (size_t j = 0; j < args->oids->nr; j++)
102 - object_info_data[j].sizep =
103 - xcalloc(1, sizeof(*object_info_data[j].sizep));
114 + CALLOC_ARRAY(results->sizes, results->nr);
115 + } else if (!strcmp(reader->line, "type")) {
116 + if (!ask_type)
117 + die(_("object-info: unrequested 'type' attribute"));
118 + if (results->types)
119 + die(_("object-info: duplicate 'type' attribute"));
120 + type_index = (int)i;
121 + CALLOC_ARRAY(results->types, results->nr);
104 122 } else {
105 - BUG("only size is supported");
123 + die(_("object-info: unknown attribute '%s'"),
124 + reader->line);
106 125 }
107 126 }
108 127
109 - for (size_t i = 0;
110 - packet_reader_read(reader) == PACKET_READ_NORMAL &&
111 - i < args->oids->nr;
112 - i++) {
128 + for (size_t i = 0; i < oids->nr; i++) {
113 129 struct string_list object_info_values = STRING_LIST_INIT_DUP;
114 130
131 + if (packet_reader_read(reader) != PACKET_READ_NORMAL)
132 + die(_("object-info: expected %" PRIuMAX " objects, got %" PRIuMAX),
133 + (uintmax_t)oids->nr, (uintmax_t)i);
134 +
115 135 string_list_split(&object_info_values, reader->line, " ", -1);
116 136
117 137 if (strcmp(object_info_values.items[0].string,
118 - oid_to_hex(&args->oids->oid[i])))
138 + oid_to_hex(&oids->oid[i])))
119 139 die(_("object-info: expected OID: %s, got %s"),
120 - oid_to_hex(&args->oids->oid[i]),
140 + oid_to_hex(&oids->oid[i]),
121 141 object_info_values.items[0].string);
122 142
123 143 /*
@@ -127,30 +147,53 @@ int fetch_object_info(const enum protocol_version version, struct object_info_ar
127 147 */
128 148 if (object_info_values.nr >= 2 &&
129 149 !strcmp(object_info_values.items[1].string, "")) {
130 - object_info_data[i].unrecognized = 1;
150 + results->unrecognized[i] = 1;
131 151 string_list_clear(&object_info_values, 0);
132 152 continue;
133 153 }
134 154
135 155 /*
136 - * Because we filter the options to be only the supported by
137 - * the server we expect the server to answer with the same
138 - * number of attributes requested.
156 + * Because we only ask for attributes the server said it
157 + * supports, we expect the answer to have one value per
158 + * requested attribute, plus the OID.
139 159 */
140 - if (args->object_info_options->nr + 1 != object_info_values.nr)
160 + if (wanted + 1 != object_info_values.nr)
141 161 die("object-info: unexpected number of attributes: %s",
142 162 reader->line);
143 163
144 - if (size_index >= 0 &&
164 + if (results->sizes &&
145 165 parse_object_size(object_info_values.items[size_index + 1].string,
146 - object_info_data[i].sizep))
147 - die("object-info: ref %s has invalid size %s",
166 + &results->sizes[i]))
167 + die("object-info: object %s has invalid size %s",
148 168 object_info_values.items[0].string,
149 169 object_info_values.items[size_index + 1].string);
150 170
171 + if (results->types) {
172 + const char *type_str =
173 + object_info_values.items[type_index + 1].string;
174 + int type = type_from_string_gently(type_str, -1, 1);
175 +
176 + if (type < 0)
177 + die(_("object-info: object %s has invalid type '%s'"),
178 + object_info_values.items[0].string, type_str);
179 +
180 + results->types[i] = type;
181 + }
182 +
151 183 string_list_clear(&object_info_values, 0);
152 184 }
185 +
186 + if (packet_reader_read(reader) != PACKET_READ_FLUSH)
187 + die(_("object-info: expected flush after %" PRIuMAX " objects"),
188 + (uintmax_t)oids->nr);
189 +
153 190 check_stateless_delimiter(stateless_rpc, reader, "stateless delimiter expected");
191 + }
154 192
155 - return 0;
193 + void free_fetch_object_info_results(struct fetch_object_info_results *results)
194 + {
195 + free(results->sizes);
196 + free(results->types);
197 + free(results->unrecognized);
198 + memset(results, 0, sizeof(*results));
156 199 }
modified fetch-object-info.h
@@ -1,25 +1,40 @@
1 1 #ifndef FETCH_OBJECT_INFO_H
2 2 #define FETCH_OBJECT_INFO_H
3 3
4 + #include "object.h"
4 5 #include "pkt-line.h"
5 6 #include "protocol.h"
6 7
7 - struct object_info_args {
8 - struct string_list *object_info_options;
9 - const struct string_list *server_options;
10 - struct oid_array *oids;
8 + struct fetch_object_info_results {
9 + size_t *sizes;
10 + enum object_type *types;
11 + uint8_t *unrecognized;
12 + size_t nr;
13 + unsigned wants_size:1;
14 + unsigned wants_type:1;
11 15 };
12 16
13 - struct object_info;
17 + #define FETCH_OBJECT_INFO_RESULTS_INIT { 0 }
18 +
19 + struct oid_array;
14 20 /*
15 - * Sends git-cat-file object-info command into the request buf and read the
21 + * Sends git-cat-file object-info command into the request buf and reads the
16 22 * results from packets.
17 23 *
18 - * Modifies args->object_info_options, on return it contains only the supported
19 - * options by the server.
24 + * The caller sets the wants_* flags in "results" to indicate which attributes
25 + * it is interested in. On return, "results" holds one array per attribute that
26 + * the server both advertised and answered with. An array left NULL means the
27 + * attribute is not available.
28 + * Release them with free_fetch_object_info_results().
20 29 */
21 - int fetch_object_info(enum protocol_version version, struct object_info_args *args,
22 - struct packet_reader *reader, struct object_info *object_info_data,
23 - int stateless_rpc, int fd_out);
30 + void fetch_object_info(enum protocol_version version,
31 + const struct string_list *server_options,
32 + const struct oid_array *oids,
33 + struct packet_reader *reader,
34 + struct fetch_object_info_results *results,
35 + int stateless_rpc,
36 + int fd_out);
37 +
38 + void free_fetch_object_info_results(struct fetch_object_info_results *results);
24 39
25 40 #endif /* FETCH_OBJECT_INFO_H */
modified git-send-email.perl
@@ -863,7 +863,7 @@ sub get_patch_subject {
863 863 return "GIT: $1\n";
864 864 }
865 865 close $fh;
866 - die sprintf(__("No subject line in %s?"), $fn);
866 + die sprintf(__("No 'Subject:' line in '%s'\n"), $fn);
867 867 }
868 868
869 869 if ($compose) {
modified git-zlib.c
@@ -33,7 +33,7 @@ static const char *zerr_to_string(int status)
33 33
34 34 /* uLong is 32-bit on Windows, even on 64-bit systems */
35 35 #define ULONG_MAX_VALUE maximum_unsigned_value_of_type(uLong)
36 - static inline uInt zlib_buf_cap(unsigned long len)
36 + static inline uInt zlib_buf_cap(size_t len)
37 37 {
38 38 return (ZLIB_BUF_MAX < len) ? ZLIB_BUF_MAX : len;
39 39 }
@@ -167,9 +167,21 @@ int git_inflate(git_zstream *strm, int flush)
167 167 return status;
168 168 }
169 169
170 - unsigned long git_deflate_bound(git_zstream *strm, unsigned long size)
170 + size_t git_deflate_bound(git_zstream *strm, size_t size)
171 171 {
172 - return deflateBound(&strm->z, size);
172 + #if SIZE_MAX > ULONG_MAX
173 + if (size > maximum_unsigned_value_of_type(uLong))
174 + /*
175 + * deflateBound() takes uLong, which is 32-bit on
176 + * Windows. For inputs above that range, return zlib's
177 + * stored-block formula (the conservative path it would
178 + * itself use for an unknown stream state) plus the
179 + * worst-case wrapper overhead.
180 + */
181 + return size + (size >> 5) + (size >> 7) + (size >> 11)
182 + + 7 + 18;
183 + #endif
184 + return deflateBound(&strm->z, (uLong)size);
173 185 }
174 186
175 187 void git_deflate_init(git_zstream *strm, int level)
modified git-zlib.h
@@ -5,8 +5,8 @@
5 5
6 6 typedef struct git_zstream {
7 7 struct z_stream_s z;
8 - unsigned long avail_in;
9 - unsigned long avail_out;
8 + size_t avail_in;
9 + size_t avail_out;
10 10 size_t total_in;
11 11 size_t total_out;
12 12 unsigned char *next_in;
@@ -25,6 +25,6 @@ void git_deflate_end(git_zstream *);
25 25 int git_deflate_abort(git_zstream *);
26 26 int git_deflate_end_gently(git_zstream *);
27 27 int git_deflate(git_zstream *, int flush);
28 - unsigned long git_deflate_bound(git_zstream *, unsigned long);
28 + size_t git_deflate_bound(git_zstream *, size_t);
29 29
30 30 #endif /* GIT_ZLIB_H */
modified git.c
@@ -304,11 +304,15 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
304 304 if (envchanged)
305 305 *envchanged = 1;
306 306 } else if (!strcmp(cmd, "--shallow-file")) {
307 - (*argv)++;
308 - (*argc)--;
309 - setenv(GIT_SHALLOW_FILE_ENVIRONMENT, (*argv)[0], 1);
307 + if (*argc < 2) {
308 + fprintf(stderr, _("no file given for '%s' option\n" ), "--shallow-file");
309 + usage(git_usage_string);
310 + }
311 + setenv(GIT_SHALLOW_FILE_ENVIRONMENT, (*argv)[1], 1);
310 312 if (envchanged)
311 313 *envchanged = 1;
314 + (*argv)++;
315 + (*argc)--;
312 316 } else if (!strcmp(cmd, "-C")) {
313 317 if (*argc < 2) {
314 318 fprintf(stderr, _("no directory given for '%s' option\n" ), "-C");
modified http-push.c
@@ -367,7 +367,7 @@ static void start_put(struct transfer_request *request)
367 367 void *unpacked;
368 368 size_t len;
369 369 int hdrlen;
370 - ssize_t size;
370 + size_t size;
371 371 git_zstream stream;
372 372 struct repo_config_values *cfg = repo_config_values(the_repository);
373 373
modified http.c
@@ -1608,6 +1608,8 @@ struct active_request_slot *get_active_slot(void)
1608 1608
1609 1609 if (!slot->curl) {
1610 1610 slot->curl = curl_easy_duphandle(curl_default);
1611 + if (!slot->curl)
1612 + die("curl_easy_duphandle failed");
1611 1613 curl_session_count++;
1612 1614 }
1613 1615
modified loose.c
@@ -61,12 +61,17 @@ static int insert_loose_map(struct odb_source_loose *loose,
61 61 return inserted;
62 62 }
63 63
64 - static int load_one_loose_object_map(struct repository *repo, struct odb_source_loose *loose)
64 + int loose_object_map_load(struct odb_source_loose *loose)
65 65 {
66 - struct strbuf buf = STRBUF_INIT, path = STRBUF_INIT;
66 + struct repository *repo = loose->base.odb->repo;
67 + struct strbuf buf = STRBUF_INIT;
68 + char *path;
67 69 FILE *fp;
68 70 int ret = -1;
69 71
72 + if (!should_use_loose_object_map(repo))
73 + return 0;
74 +
70 75 if (!loose->map)
71 76 loose_object_map_init(&loose->map);
72 77 if (!loose->cache) {
@@ -78,10 +83,10 @@ static int load_one_loose_object_map(struct repository *repo, struct odb_source_
78 83 insert_loose_map(loose, repo->hash_algo->empty_blob, repo->compat_hash_algo->empty_blob);
79 84 insert_loose_map(loose, repo->hash_algo->null_oid, repo->compat_hash_algo->null_oid);
80 85
81 - repo_common_path_replace(repo, &path, "objects/loose-object-idx");
82 - fp = fopen(path.buf, "rb");
86 + path = xstrfmt("%s/loose-object-idx", loose->base.path);
87 + fp = fopen(path, "rb");
83 88 if (!fp) {
84 - strbuf_release(&path);
89 + free(path);
85 90 return 0;
86 91 }
87 92
@@ -102,7 +107,7 @@ static int load_one_loose_object_map(struct repository *repo, struct odb_source_
102 107 err:
103 108 fclose(fp);
104 109 strbuf_release(&buf);
105 - strbuf_release(&path);
110 + free(path);
106 111 return ret;
107 112 }
108 113
@@ -110,17 +115,12 @@ int repo_read_loose_object_map(struct repository *repo)
110 115 {
111 116 struct odb_source *source;
112 117
113 - if (!should_use_loose_object_map(repo))
114 - return 0;
115 -
116 - odb_prepare_alternates(repo->objects);
117 -
118 118 for (source = repo->objects->sources; source; source = source->next) {
119 119 struct odb_source_files *files = odb_source_files_downcast(source);
120 - if (load_one_loose_object_map(repo, files->loose) < 0) {
120 + if (loose_object_map_load(files->loose) < 0)
121 121 return -1;
122 - }
123 122 }
123 +
124 124 return 0;
125 125 }
126 126
modified loose.h
@@ -13,6 +13,7 @@ struct loose_object_map {
13 13
14 14 void loose_object_map_init(struct loose_object_map **map);
15 15 void loose_object_map_clear(struct loose_object_map **map);
16 + int loose_object_map_load(struct odb_source_loose *loose);
16 17 int repo_loose_object_map_oid(struct repository *repo,
17 18 const struct object_id *src,
18 19 const struct git_hash_algo *dest_algo,
modified midx.c
@@ -591,8 +591,7 @@ uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos)
591 591
592 592 int fill_midx_entry(struct multi_pack_index *m,
593 593 const struct object_id *oid,
594 - struct pack_entry *e,
595 - struct packed_git **bad_pack)
594 + struct pack_entry *e)
596 595 {
597 596 uint32_t pos;
598 597 uint32_t pack_int_id;
@@ -619,11 +618,8 @@ int fill_midx_entry(struct multi_pack_index *m,
619 618 return 0;
620 619
621 620 if (oidset_size(&p->bad_objects) &&
622 - oidset_contains(&p->bad_objects, oid)) {
623 - if (bad_pack && !*bad_pack)
624 - *bad_pack = p;
621 + oidset_contains(&p->bad_objects, oid))
625 622 return 0;
626 - }
627 623
628 624 e->offset = nth_midxed_offset(m, pos);
629 625 e->p = p;
@@ -1032,7 +1028,7 @@ int verify_midx_file(struct odb_source_packed *source, unsigned flags)
1032 1028
1033 1029 nth_midxed_object_oid(&oid, m, pairs[i].pos);
1034 1030
1035 - if (!fill_midx_entry(m, &oid, &e, NULL)) {
1031 + if (!fill_midx_entry(m, &oid, &e)) {
1036 1032 midx_report(_("failed to load pack entry for oid[%d] = %s"),
1037 1033 pairs[i].pos, oid_to_hex(&oid));
1038 1034 continue;
modified midx.h
@@ -117,8 +117,7 @@ uint32_t nth_midxed_pack_int_id(struct multi_pack_index *m, uint32_t pos);
117 117 struct object_id *nth_midxed_object_oid(struct object_id *oid,
118 118 struct multi_pack_index *m,
119 119 uint32_t n);
120 - int fill_midx_entry(struct multi_pack_index *m, const struct object_id *oid,
121 - struct pack_entry *e, struct packed_git **bad_pack);
120 + int fill_midx_entry(struct multi_pack_index *m, const struct object_id *oid, struct pack_entry *e);
122 121 int midx_contains_pack(struct multi_pack_index *m,
123 122 const char *idx_or_pack_name);
124 123 int midx_layer_contains_pack(struct multi_pack_index *m,
modified object-file.c
@@ -122,7 +122,7 @@ int check_object_signature(struct repository *r, const struct object_id *oid,
122 122 }
123 123
124 124 int stream_object_signature(struct repository *r,
125 - struct odb_read_stream *st,
125 + struct odb_stream *st,
126 126 const struct object_id *oid)
127 127 {
128 128 struct object_id real_oid;
@@ -138,7 +138,7 @@ int stream_object_signature(struct repository *r,
138 138 git_hash_update(&c, hdr, hdrlen);
139 139 for (;;) {
140 140 char buf[1024 * 16];
141 - ssize_t readlen = odb_read_stream_read(st, buf, sizeof(buf));
141 + ssize_t readlen = odb_stream_read(st, buf, sizeof(buf));
142 142 if (readlen < 0)
143 143 return -1;
144 144 if (!readlen)
@@ -702,9 +702,9 @@ static void prepare_packfile_transaction(struct odb_transaction_files *transacti
702 702 die_errno("unable to write pack header");
703 703 }
704 704
705 - static int hash_blob_stream(struct odb_write_stream *stream,
706 - const struct git_hash_algo *hash_algo,
707 - struct object_id *result_oid, size_t size)
705 + static int hash_stream(struct odb_stream *stream,
706 + const struct git_hash_algo *hash_algo,
707 + struct object_id *result_oid)
708 708 {
709 709 unsigned char buf[16384];
710 710 struct git_hash_ctx ctx;
@@ -712,22 +712,23 @@ static int hash_blob_stream(struct odb_write_stream *stream,
712 712 size_t bytes_hashed = 0;
713 713
714 714 header_len = format_object_header((char *)buf, sizeof(buf),
715 - OBJ_BLOB, size);
715 + stream->type, stream->size);
716 716 git_hash_init(&ctx, hash_algo);
717 717 git_hash_update(&ctx, buf, header_len);
718 718
719 - while (!stream->is_finished) {
720 - ssize_t read_result = odb_write_stream_read(stream, buf,
721 - sizeof(buf));
722 -
719 + while (1) {
720 + ssize_t read_result = odb_stream_read(stream, buf,
721 + sizeof(buf));
723 722 if (read_result < 0)
724 723 return -1;
724 + if (!read_result)
725 + break;
725 726
726 727 git_hash_update(&ctx, buf, read_result);
727 728 bytes_hashed += read_result;
728 729 }
729 730
730 - if (bytes_hashed != size)
731 + if (bytes_hashed != stream->size)
731 732 return -1;
732 733
733 734 git_hash_final_oid(result_oid, &ctx);
@@ -739,9 +740,9 @@ static int hash_blob_stream(struct odb_write_stream *stream,
739 740 * Read the contents from the stream provided, streaming it to the
740 741 * packfile in state while updating the hash in ctx.
741 742 */
742 - static void stream_blob_to_pack(struct transaction_packfile *state,
743 - struct git_hash_ctx *ctx, size_t size,
744 - struct odb_write_stream *stream)
743 + static void stream_to_pack(struct transaction_packfile *state,
744 + struct git_hash_ctx *ctx,
745 + struct odb_stream *stream)
745 746 {
746 747 git_zstream s;
747 748 unsigned char ibuf[16384];
@@ -749,21 +750,23 @@ static void stream_blob_to_pack(struct transaction_packfile *state,
749 750 unsigned hdrlen;
750 751 int status = Z_OK;
751 752 struct repo_config_values *cfg = repo_config_values(the_repository);
753 + bool is_finished = false;
752 754 size_t bytes_read = 0;
753 755
754 756 git_deflate_init(&s, cfg->pack_compression_level);
755 757
756 - hdrlen = encode_in_pack_object_header(obuf, sizeof(obuf), OBJ_BLOB, size);
758 + hdrlen = encode_in_pack_object_header(obuf, sizeof(obuf), stream->type, stream->size);
757 759 s.next_out = obuf + hdrlen;
758 760 s.avail_out = sizeof(obuf) - hdrlen;
759 761
760 762 while (status != Z_STREAM_END) {
761 - if (!stream->is_finished && !s.avail_in) {
762 - ssize_t rsize = odb_write_stream_read(stream, ibuf,
763 - sizeof(ibuf));
764 -
763 + if (!is_finished && !s.avail_in) {
764 + ssize_t rsize = odb_stream_read(stream, ibuf,
765 + sizeof(ibuf));
765 766 if (rsize < 0)
766 - die("failed to read blob data");
767 + die("failed to read object data");
768 + if (!rsize)
769 + is_finished = true;
767 770
768 771 git_hash_update(ctx, ibuf, rsize);
769 772
@@ -772,7 +775,7 @@ static void stream_blob_to_pack(struct transaction_packfile *state,
772 775 bytes_read += rsize;
773 776 }
774 777
775 - status = git_deflate(&s, stream->is_finished ? Z_FINISH : 0);
778 + status = git_deflate(&s, is_finished ? Z_FINISH : 0);
776 779
777 780 if (!s.avail_out || status == Z_STREAM_END) {
778 781 size_t written = s.next_out - obuf;
@@ -793,9 +796,9 @@ static void stream_blob_to_pack(struct transaction_packfile *state,
793 796 }
794 797 }
795 798
796 - if (bytes_read != size)
797 - die("read %" PRIuMAX " bytes of blob data, but expected %" PRIuMAX " bytes",
798 - (uintmax_t)bytes_read, (uintmax_t)size);
799 + if (bytes_read != stream->size)
800 + die("read %" PRIuMAX " bytes of object data, but expected %" PRIuMAX " bytes",
801 + (uintmax_t)bytes_read, (uintmax_t)stream->size);
799 802
800 803 git_deflate_end(&s);
801 804 }
@@ -865,12 +868,11 @@ clear_exit:
865 868 * result, which we need to know beforehand when writing a git object.
866 869 * Since the primary motivation for trying to stream from the working
867 870 * tree file and to avoid mmaping it in core is to deal with large
868 - * binary blobs, they generally do not want to get any conversion, and
871 + * objects, they generally do not want to get any conversion, and
869 872 * callers should avoid this code path when filters are requested.
870 873 */
871 874 static int odb_transaction_files_write_object_stream(struct odb_transaction *base,
872 - struct odb_write_stream *stream,
873 - size_t size,
875 + struct odb_stream *stream,
874 876 struct object_id *result_oid)
875 877 {
876 878 struct odb_transaction_files *transaction = container_of(base,
@@ -884,7 +886,7 @@ static int odb_transaction_files_write_object_stream(struct odb_transaction *bas
884 886 struct pack_idx_entry *idx;
885 887
886 888 header_len = format_object_header((char *)obuf, sizeof(obuf),
887 - OBJ_BLOB, size);
889 + stream->type, stream->size);
888 890 git_hash_init(&ctx, transaction->base.source->odb->repo->hash_algo);
889 891 git_hash_update(&ctx, obuf, header_len);
890 892
@@ -899,7 +901,7 @@ static int odb_transaction_files_write_object_stream(struct odb_transaction *bas
899 901 * to zlib compression and is sufficient for this check.
900 902 */
901 903 if (state->nr_written && pack_size_limit_cfg &&
902 - pack_size_limit_cfg < state->offset + size)
904 + pack_size_limit_cfg < state->offset + stream->size)
903 905 flush_packfile_transaction(transaction);
904 906
905 907 CALLOC_ARRAY(idx, 1);
@@ -909,7 +911,7 @@ static int odb_transaction_files_write_object_stream(struct odb_transaction *bas
909 911 hashfile_checkpoint(state->f, &checkpoint);
910 912 idx->offset = state->offset;
911 913 crc32_begin(state->f);
912 - stream_blob_to_pack(state, &ctx, size, stream);
914 + stream_to_pack(state, &ctx, stream);
913 915 git_hash_final_oid(result_oid, &ctx);
914 916
915 917 idx->crc32 = crc32_end(state->f);
@@ -950,8 +952,8 @@ int index_fd(struct index_state *istate, struct object_id *oid,
950 952 ret = index_core(istate, oid, fd, xsize_t(st->st_size),
951 953 type, path, flags);
952 954 } else {
953 - struct odb_write_stream stream;
954 - odb_write_stream_from_fd(&stream, fd, xsize_t(st->st_size));
955 + struct odb_stream *stream = odb_stream_from_fd(fd, xsize_t(st->st_size),
956 + OBJ_BLOB);
955 957
956 958 if (flags & INDEX_WRITE_OBJECT) {
957 959 struct object_database *odb = the_repository->objects;
@@ -961,18 +963,14 @@ int index_fd(struct index_state *istate, struct object_id *oid,
961 963 if (!inflight)
962 964 odb_transaction_begin_or_die(odb, &transaction, 0);
963 965 ret = odb_transaction_write_object_stream(transaction,
964 - &stream,
965 - xsize_t(st->st_size),
966 - oid);
966 + stream, oid);
967 967 if (!inflight)
968 968 odb_transaction_commit(transaction);
969 969 } else {
970 - ret = hash_blob_stream(&stream,
971 - the_repository->hash_algo, oid,
972 - xsize_t(st->st_size));
970 + ret = hash_stream(stream, the_repository->hash_algo, oid);
973 971 }
974 972
975 - odb_write_stream_release(&stream);
973 + odb_stream_close(stream);
976 974 }
977 975
978 976 close(fd);
@@ -1340,13 +1338,3 @@ int odb_transaction_files_begin(struct odb_source *source,
1340 1338
1341 1339 return 0;
1342 1340 }
1343 -
1344 - void free_object_info_contents(struct object_info *object_info)
1345 - {
1346 - if (!object_info)
1347 - return;
1348 - free(object_info->typep);
1349 - free(object_info->sizep);
1350 - free(object_info->disk_sizep);
1351 - free(object_info->delta_base_oid);
1352 - }
modified object-file.h
@@ -101,7 +101,7 @@ int check_object_signature(struct repository *r, const struct object_id *oid,
101 101 * the streaming interface and rehash it to do the same.
102 102 */
103 103 int stream_object_signature(struct repository *r,
104 - struct odb_read_stream *stream,
104 + struct odb_stream *stream,
105 105 const struct object_id *oid);
106 106
107 107 enum finalize_object_file_flags {
modified object-name.c
@@ -280,7 +280,6 @@ static int init_object_disambiguation(struct repository *r,
280 280
281 281 ds->len = len;
282 282 ds->repo = r;
283 - odb_prepare_alternates(r->objects);
284 283 return 0;
285 284 }
286 285
modified object.c
@@ -345,7 +345,7 @@ struct object *parse_object_with_flags(struct repository *r,
345 345 if ((!obj || obj->type == OBJ_NONE || obj->type == OBJ_BLOB) &&
346 346 odb_read_object_info(r->objects, oid, NULL) == OBJ_BLOB) {
347 347 if (!skip_hash) {
348 - struct odb_read_stream *stream = odb_read_stream_open(r->objects, oid, NULL);
348 + struct odb_stream *stream = odb_stream_from_object(r->objects, oid, NULL);
349 349
350 350 if (!stream) {
351 351 error(_("unable to open object stream for %s"), oid_to_hex(oid));
@@ -354,11 +354,11 @@ struct object *parse_object_with_flags(struct repository *r,
354 354
355 355 if (stream_object_signature(r, stream, repl) < 0) {
356 356 error(_("hash mismatch %s"), oid_to_hex(oid));
357 - odb_read_stream_close(stream);
357 + odb_stream_close(stream);
358 358 return NULL;
359 359 }
360 360
361 - odb_read_stream_close(stream);
361 + odb_stream_close(stream);
362 362 }
363 363 parse_blob_buffer(lookup_blob(r, oid));
364 364 return lookup_object(r, oid);
modified odb.c
@@ -2,11 +2,10 @@
2 2 #include "abspath.h"
3 3 #include "commit-graph.h"
4 4 #include "config.h"
5 - #include "dir.h"
6 5 #include "environment.h"
7 6 #include "gettext.h"
7 + #include "hashmap.h"
8 8 #include "hex.h"
9 - #include "khash.h"
10 9 #include "lockfile.h"
11 10 #include "loose.h"
12 11 #include "midx.h"
@@ -15,6 +14,7 @@
15 14 #include "object-name.h"
16 15 #include "odb.h"
17 16 #include "odb/source-inmemory.h"
17 + #include "packfile.h"
18 18 #include "path.h"
19 19 #include "promisor-remote.h"
20 20 #include "quote.h"
@@ -28,8 +28,47 @@
28 28 #include "trace2.h"
29 29 #include "write-or-die.h"
30 30
31 - KHASH_INIT(odb_path_map, const char * /* key: odb_path */,
32 - struct odb_source *, 1, fspathhash, fspatheq)
31 + /*
32 + * NEEDSWORK: we're using "core.ignoreCase" to deduplicate alternates that
33 + * _may_ be the same. This requires quite a bit of boilerplate for dubious
34 + * benefit:
35 + *
36 + * - Duplicating alternates should really only lead to regressed performance.
37 + *
38 + * - We don't properly resolve symlinks or mointpoints, so we may still end
39 + * up duplicating alternates.
40 + *
41 + * - The value may be lying, in which case we might deduplicate alternates
42 + * that are in fact not mapping to the same directory.
43 + *
44 + * We should investigate whether we can remove this whole mechanism outright.
45 + */
46 + static int odb_source_paths_cmp(struct object_database *o,
47 + const char *a, const char *b)
48 + {
49 + if (o->source_paths_icase < 0) {
50 + int icase = 0;
51 + repo_config_get_bool(o->repo, "core.ignorecase", &icase);
52 + o->source_paths_icase = icase;
53 + }
54 +
55 + return o->source_paths_icase ? strcasecmp(a, b) : strcmp(a, b);
56 + }
57 +
58 + static int odb_source_by_path_cmp(const void *cb_data,
59 + const struct hashmap_entry *entry,
60 + const struct hashmap_entry *entry_or_key,
61 + const void *keydata)
62 + {
63 + struct object_database *o = (struct object_database *)cb_data;
64 + const struct odb_source *source = container_of(entry, const struct odb_source, by_path_entry);
65 + const char *path = keydata;
66 +
67 + if (!path)
68 + path = container_of(entry_or_key, const struct odb_source, by_path_entry)->path;
69 +
70 + return odb_source_paths_cmp(o, source->path, path);
71 + }
33 72
34 73 int odb_mkstemp(struct object_database *odb,
35 74 struct strbuf *temp_filename, const char *pattern)
@@ -57,8 +96,8 @@ int odb_mkstemp(struct object_database *odb,
57 96 */
58 97 static bool odb_is_source_usable(struct object_database *o, const char *path)
59 98 {
60 - int r;
61 99 struct strbuf normalized_objdir = STRBUF_INIT;
100 + struct hashmap_entry key;
62 101 bool usable = false;
63 102
64 103 strbuf_realpath(&normalized_objdir, o->sources->path, 1);
@@ -75,20 +114,18 @@ static bool odb_is_source_usable(struct object_database *o, const char *path)
75 114 * Prevent the common mistake of listing the same
76 115 * thing twice, or object directory itself.
77 116 */
78 - if (!o->source_by_path) {
79 - khiter_t p;
80 -
81 - o->source_by_path = kh_init_odb_path_map();
117 + if (!hashmap_get_size(&o->source_by_path)) {
82 118 assert(!o->sources->next);
83 - p = kh_put_odb_path_map(o->source_by_path, o->sources->path, &r);
84 - assert(r == 1); /* never used */
85 - kh_value(o->source_by_path, p) = o->sources;
119 + hashmap_entry_init(&o->sources->by_path_entry,
120 + strihash(o->sources->path));
121 + hashmap_add(&o->source_by_path, &o->sources->by_path_entry);
86 122 }
87 123
88 - if (fspatheq(path, normalized_objdir.buf))
124 + if (!odb_source_paths_cmp(o, path, normalized_objdir.buf))
89 125 goto out;
90 126
91 - if (kh_get_odb_path_map(o->source_by_path, path) < kh_end(o->source_by_path))
127 + hashmap_entry_init(&key, strihash(path));
128 + if (hashmap_get(&o->source_by_path, &key, path))
92 129 goto out;
93 130
94 131 usable = true;
@@ -171,8 +208,6 @@ static struct odb_source *odb_add_alternate_recursively(struct object_database *
171 208 {
172 209 struct odb_source *alternate = NULL;
173 210 struct strvec sources = STRVEC_INIT;
174 - khiter_t pos;
175 - int ret;
176 211
177 212 if (!odb_is_source_usable(odb, source))
178 213 goto error;
@@ -183,10 +218,11 @@ static struct odb_source *odb_add_alternate_recursively(struct object_database *
183 218 *odb->sources_tail = alternate;
184 219 odb->sources_tail = &(alternate->next);
185 220
186 - pos = kh_put_odb_path_map(odb->source_by_path, alternate->path, &ret);
187 - if (!ret)
221 + hashmap_entry_init(&alternate->by_path_entry, strihash(alternate->path));
222 + if (hashmap_get(&odb->source_by_path, &alternate->by_path_entry,
223 + alternate->path))
188 224 BUG("source must not yet exist");
189 - kh_value(odb->source_by_path, pos) = alternate;
225 + hashmap_add(&odb->source_by_path, &alternate->by_path_entry);
190 226
191 227 /* recursively add alternates */
192 228 odb_source_read_alternates(alternate, &sources);
@@ -209,18 +245,12 @@ void odb_add_to_alternates_file(struct object_database *odb,
209 245 int ret = odb_source_write_alternate(odb->sources, dir);
210 246 if (ret < 0)
211 247 die(NULL);
212 - if (odb->loaded_alternates)
213 - odb_add_alternate_recursively(odb, dir, 0);
248 + odb_add_alternate_recursively(odb, dir, 0);
214 249 }
215 250
216 251 struct odb_source *odb_add_to_alternates_memory(struct object_database *odb,
217 252 const char *dir)
218 253 {
219 - /*
220 - * Make sure alternates are initialized, or else our entry may be
221 - * overwritten when they are.
222 - */
223 - odb_prepare_alternates(odb);
224 254 return odb_add_alternate_recursively(odb, dir, 0);
225 255 }
226 256
@@ -229,12 +259,6 @@ struct odb_source *odb_set_temporary_primary_source(struct object_database *odb,
229 259 {
230 260 struct odb_source *source;
231 261
232 - /*
233 - * Make sure alternates are initialized, or else our entry may be
234 - * overwritten when they are.
235 - */
236 - odb_prepare_alternates(odb);
237 -
238 262 /*
239 263 * Make a new primary odb and link the old primary ODB in as an
240 264 * alternate
@@ -340,7 +364,6 @@ struct odb_source *odb_find_source(struct object_database *odb, const char *obj_
340 364 char *obj_dir_real = real_pathdup(obj_dir, 1);
341 365 struct strbuf odb_path_real = STRBUF_INIT;
342 366
343 - odb_prepare_alternates(odb);
344 367 for (source = odb->sources; source; source = source->next) {
345 368 strbuf_realpath(&odb_path_real, source->path, 1);
346 369 if (!strcmp(obj_dir_real, odb_path_real.buf))
@@ -474,7 +497,6 @@ int odb_for_each_alternate(struct object_database *odb,
474 497 struct odb_source *alternate;
475 498 int r = 0;
476 499
477 - odb_prepare_alternates(odb);
478 500 for (alternate = odb->sources->next; alternate; alternate = alternate->next) {
479 501 r = cb(alternate, payload);
480 502 if (r)
@@ -483,26 +505,22 @@ int odb_for_each_alternate(struct object_database *odb,
483 505 return r;
484 506 }
485 507
486 - void odb_prepare_alternates(struct object_database *odb)
508 + static void odb_prepare_alternates(struct object_database *odb,
509 + const char *alternate_db)
487 510 {
488 511 struct strvec sources = STRVEC_INIT;
489 512
490 - if (odb->loaded_alternates)
491 - return;
492 -
493 - parse_alternates(odb->alternate_db, PATH_SEP, NULL, &sources);
513 + parse_alternates(alternate_db, PATH_SEP, NULL, &sources);
494 514 odb_source_read_alternates(odb->sources, &sources);
515 +
495 516 for (size_t i = 0; i < sources.nr; i++)
496 517 odb_add_alternate_recursively(odb, sources.v[i], 0);
497 518
498 - odb->loaded_alternates = 1;
499 -
500 519 strvec_clear(&sources);
501 520 }
502 521
503 522 int odb_has_alternates(struct object_database *odb)
504 523 {
505 - odb_prepare_alternates(odb);
506 524 return !!odb->sources->next;
507 525 }
508 526
@@ -546,15 +564,12 @@ static int register_all_submodule_sources(struct object_database *odb)
546 564 return ret;
547 565 }
548 566
549 - static enum odb_read_status do_oid_object_info_extended(struct object_database *odb,
550 - const struct object_id *oid,
551 - struct object_info *oi, unsigned flags)
567 + static int do_oid_object_info_extended(struct object_database *odb,
568 + const struct object_id *oid,
569 + struct object_info *oi, unsigned flags)
552 570 {
553 - struct strbuf corrupt_err = STRBUF_INIT;
554 571 const struct object_id *real = oid;
555 - enum odb_read_status ret;
556 572 int already_retried = 0;
557 - bool corrupt = false;
558 573
559 574 if (flags & OBJECT_INFO_LOOKUP_REPLACE)
560 575 real = lookup_replace_object(odb->repo, oid);
@@ -562,22 +577,15 @@ static enum odb_read_status do_oid_object_info_extended(struct object_database *
562 577 if (is_null_oid(real))
563 578 return -1;
564 579
565 - if (!odb_source_read_object_info(odb->inmemory_objects, oid, oi, flags, NULL))
580 + if (!odb_source_read_object_info(odb->inmemory_objects, oid, oi, flags))
566 581 return 0;
567 582
568 - odb_prepare_alternates(odb);
569 -
570 583 while (1) {
571 584 struct odb_source *source;
572 585
573 - for (source = odb->sources; source; source = source->next) {
574 - ret = odb_source_read_object_info(source, real, oi, flags,
575 - corrupt_err.len ? NULL : &corrupt_err);
576 - if (!ret)
577 - goto out;
578 - if (ret != ODB_READ_NOT_FOUND)
579 - corrupt = true;
580 - }
586 + for (source = odb->sources; source; source = source->next)
587 + if (!odb_source_read_object_info(source, real, oi, flags))
588 + return 0;
581 589
582 590 /*
583 591 * When the object hasn't been found we try a second read and
@@ -585,15 +593,10 @@ static enum odb_read_status do_oid_object_info_extended(struct object_database *
585 593 * caches or reload on-disk state.
586 594 */
587 595 if (!(flags & OBJECT_INFO_QUICK)) {
588 - for (source = odb->sources; source; source = source->next) {
589 - ret = odb_source_read_object_info(source, real, oi,
590 - flags | OBJECT_INFO_SECOND_READ,
591 - corrupt_err.len ? NULL : &corrupt_err);
592 - if (!ret)
593 - goto out;
594 - if (ret != ODB_READ_NOT_FOUND)
595 - corrupt = true;
596 - }
596 + for (source = odb->sources; source; source = source->next)
597 + if (!odb_source_read_object_info(source, real, oi,
598 + flags | OBJECT_INFO_SECOND_READ))
599 + return 0;
597 600 }
598 601
599 602 /*
@@ -616,23 +619,16 @@ static enum odb_read_status do_oid_object_info_extended(struct object_database *
616 619 }
617 620
618 621 if (flags & OBJECT_INFO_DIE_IF_CORRUPT) {
622 + const struct packed_git *p;
619 623 if ((flags & OBJECT_INFO_LOOKUP_REPLACE) && !oideq(real, oid))
620 624 die(_("replacement %s not found for %s"),
621 625 oid_to_hex(real), oid_to_hex(oid));
622 - if (corrupt) {
623 - if (corrupt_err.len)
624 - die("%s", corrupt_err.buf);
625 - die(_("object %s is corrupt"), oid_to_hex(real));
626 - }
626 + if ((p = has_packed_and_bad(odb->repo, real)))
627 + die(_("packed object %s (stored in %s) is corrupt"),
628 + oid_to_hex(real), p->pack_name);
627 629 }
628 -
629 - ret = corrupt ? ODB_READ_ERROR : ODB_READ_NOT_FOUND;
630 - goto out;
630 + return -1;
631 631 }
632 -
633 - out:
634 - strbuf_release(&corrupt_err);
635 - return ret;
636 632 }
637 633
638 634 static int oid_object_info_convert(struct repository *r,
@@ -715,12 +711,12 @@ static int oid_object_info_convert(struct repository *r,
715 711 return ret;
716 712 }
717 713
718 - enum odb_read_status odb_read_object_info_extended(struct object_database *odb,
719 - const struct object_id *oid,
720 - struct object_info *oi,
721 - enum object_info_flags flags)
714 + int odb_read_object_info_extended(struct object_database *odb,
715 + const struct object_id *oid,
716 + struct object_info *oi,
717 + enum object_info_flags flags)
722 718 {
723 - enum odb_read_status ret;
719 + int ret;
724 720
725 721 if (oid->algo && (hash_algo_by_ptr(odb->repo->hash_algo) != oid->algo))
726 722 return oid_object_info_convert(odb->repo, oid, oi, flags);
@@ -846,7 +842,6 @@ int odb_freshen_object(struct object_database *odb,
846 842 const struct object_id *oid)
847 843 {
848 844 struct odb_source *source;
849 - odb_prepare_alternates(odb);
850 845 for (source = odb->sources; source; source = source->next)
851 846 if (odb_source_freshen_object(source, oid, NULL))
852 847 return 1;
@@ -861,7 +856,6 @@ int odb_for_each_object_ext(struct object_database *odb,
861 856 {
862 857 int ret;
863 858
864 - odb_prepare_alternates(odb);
865 859 for (struct odb_source *source = odb->sources; source; source = source->next) {
866 860 if (opts->flags & ODB_FOR_EACH_OBJECT_LOCAL_ONLY && !source->local)
867 861 continue;
@@ -899,7 +893,6 @@ int odb_count_objects(struct object_database *odb,
899 893 return 0;
900 894 }
901 895
902 - odb_prepare_alternates(odb);
903 896 for (source = odb->sources; source; source = source->next) {
904 897 unsigned long c;
905 898
@@ -979,7 +972,6 @@ int odb_find_abbrev_len(struct object_database *odb,
979 972 goto out;
980 973 }
981 974
982 - odb_prepare_alternates(odb);
983 975 for (struct odb_source *source = odb->sources; source; source = source->next) {
984 976 ret = odb_source_find_abbrev_len(source, oid, len, &len);
985 977 if (ret)
@@ -1047,10 +1039,10 @@ int odb_write_object_ext(struct object_database *odb,
1047 1039 }
1048 1040
1049 1041 int odb_write_object_stream(struct object_database *odb,
1050 - struct odb_write_stream *stream, size_t len,
1042 + struct odb_stream *stream,
1051 1043 struct object_id *oid)
1052 1044 {
1053 - return odb_source_write_object_stream(odb->sources, stream, len, oid);
1045 + return odb_source_write_object_stream(odb->sources, stream, oid);
1054 1046 }
1055 1047
1056 1048 int odb_optimize(struct object_database *odb,
@@ -1065,27 +1057,55 @@ bool odb_optimize_required(struct object_database *odb,
1065 1057 return odb_source_optimize_required(odb->sources, opts);
1066 1058 }
1067 1059
1060 + void odb_generate_pack_options_release(struct odb_generate_pack_options *opts)
1061 + {
1062 + oid_array_clear(&opts->wants);
1063 + oid_array_clear(&opts->haves);
1064 + oid_array_clear(&opts->shallows);
1065 + }
1066 +
1067 + int odb_generate_pack(struct object_database *odb,
1068 + struct odb_pack_generator **out,
1069 + const struct odb_generate_pack_options *opts)
1070 + {
1071 + if (!odb->sources->generate_pack)
1072 + return error(_("primary object source does not support generating packfiles"));
1073 + return odb_source_generate_pack(odb->sources, out, opts);
1074 + }
1075 +
1076 + int odb_pack_generator_finish(struct odb_pack_generator *generator)
1077 + {
1078 + return generator->finish(generator);
1079 + }
1080 +
1068 1081 struct object_database *odb_new(struct repository *repo,
1069 - const char *primary_source,
1070 - const char *secondary_sources)
1082 + enum odb_new_flags flags)
1071 1083 {
1072 - struct object_database *o = xmalloc(sizeof(*o));
1073 - char *to_free = NULL;
1084 + char *primary_source = NULL, *secondary_sources = NULL;
1085 + struct object_database *o;
1074 1086
1075 - memset(o, 0, sizeof(*o));
1087 + CALLOC_ARRAY(o, 1);
1076 1088 o->repo = repo;
1077 1089 pthread_mutex_init(&o->replace_mutex, NULL);
1078 1090 string_list_init_dup(&o->submodule_source_paths);
1091 + hashmap_init(&o->source_by_path, odb_source_by_path_cmp, o, 0);
1092 + o->source_paths_icase = -1;
1079 1093
1094 + if (flags & ODB_NEW_HONOR_ENV) {
1095 + primary_source = xstrdup_or_null(getenv(DB_ENVIRONMENT));
1096 + secondary_sources = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));
1097 + }
1080 1098 if (!primary_source)
1081 - primary_source = to_free = xstrfmt("%s/objects", repo->commondir);
1099 + primary_source = xstrfmt("%s/objects", repo->commondir);
1100 +
1082 1101 o->sources = odb_source_new(o, primary_source, true);
1083 1102 o->sources_tail = &o->sources->next;
1084 - o->alternate_db = xstrdup_or_null(secondary_sources);
1085 1103 o->inmemory_objects = &odb_source_inmemory_new(o)->base;
1086 1104
1087 - free(to_free);
1105 + odb_prepare_alternates(o, secondary_sources);
1088 1106
1107 + free(secondary_sources);
1108 + free(primary_source);
1089 1109 return o;
1090 1110 }
1091 1111
@@ -1110,8 +1130,7 @@ static void odb_free_sources(struct object_database *o)
1110 1130 odb_source_free(o->inmemory_objects);
1111 1131 o->inmemory_objects = NULL;
1112 1132
1113 - kh_destroy_odb_path_map(o->source_by_path);
1114 - o->source_by_path = NULL;
1133 + hashmap_clear(&o->source_by_path);
1115 1134 }
1116 1135
1117 1136 void odb_free(struct object_database *o)
@@ -1119,8 +1138,6 @@ void odb_free(struct object_database *o)
1119 1138 if (!o)
1120 1139 return;
1121 1140
1122 - free(o->alternate_db);
1123 -
1124 1141 oidmap_clear(&o->replace_map, 1);
1125 1142 pthread_mutex_destroy(&o->replace_mutex);
1126 1143
@@ -1142,14 +1159,14 @@ void odb_prepare(struct object_database *o, enum odb_prepare_flags flags)
1142 1159 * Reprepare alt odbs, in case the alternates file was modified
1143 1160 * during the course of this process. This only _adds_ odbs to
1144 1161 * the linked list, so existing odbs will continue to exist for
1145 - * the lifetime of the process.
1162 + * the lifetime of the process. Consequently, we don't have to
1163 + * reprocess GIT_ALTERNATE_OBJECT_DIRECTORIES here.
1146 1164 */
1147 1165 if (flags & ODB_PREPARE_FLUSH_CACHES) {
1148 - o->loaded_alternates = 0;
1166 + odb_prepare_alternates(o, NULL);
1149 1167 o->object_count_valid = 0;
1150 1168 }
1151 1169
1152 - odb_prepare_alternates(o);
1153 1170 for (source = o->sources; source; source = source->next)
1154 1171 odb_source_prepare(source, flags);
1155 1172
@@ -1160,3 +1177,11 @@ void odb_reprepare(struct object_database *o)
1160 1177 {
1161 1178 odb_prepare(o, ODB_PREPARE_FLUSH_CACHES);
1162 1179 }
1180 +
1181 + int odb_fsck(struct object_database *odb, struct odb_fsck_options *options)
1182 + {
1183 + int ret = 0;
1184 + for (struct odb_source *source = odb->sources; source; source = source->next)
1185 + ret |= odb_source_fsck(source, options);
1186 + return ret;
1187 + }
modified odb.h
@@ -1,7 +1,9 @@
1 1 #ifndef ODB_H
2 2 #define ODB_H
3 3
4 + #include "hashmap.h"
4 5 #include "object.h"
6 + #include "oid-array.h"
5 7 #include "oidset.h"
6 8 #include "oidmap.h"
7 9 #include "string-list.h"
@@ -54,16 +56,19 @@ struct object_database {
54 56 */
55 57 struct odb_source *sources;
56 58 struct odb_source **sources_tail;
57 - struct kh_odb_path_map *source_by_path;
58 59
59 - int loaded_alternates;
60 + /*
61 + * Map of object database sources, keyed by their respective paths.
62 + * This map is used to detect the case where the same source is
63 + * registered multiple times.
64 + */
65 + struct hashmap source_by_path;
60 66
61 67 /*
62 - * A list of alternate object directories loaded from the environment;
63 - * this should not generally need to be accessed directly, but will
64 - * populate the "sources" list when odb_prepare_alternates() is run.
68 + * Whether source paths shall be compared case-insensitively, as
69 + * determined by "core.ignoreCase".
65 70 */
66 - char *alternate_db;
71 + int source_paths_icase;
67 72
68 73 /*
69 74 * Objects that should be substituted by other objects
@@ -100,6 +105,20 @@ struct object_database {
100 105 struct string_list submodule_source_paths;
101 106 };
102 107
108 + enum odb_new_flags {
109 + /*
110 + * Honor environment variables when constructing the object database
111 + * sources. This makes us respect the following environment variables:
112 + *
113 + * - GIT_OBJECT_DIRECTORY to override the primary object directory.
114 + *
115 + * - GIT_ALTERNATE_OBJECT_DIRECTORIES to override alternates.
116 + *
117 + * Environment variables may be backend-specific.
118 + */
119 + ODB_NEW_HONOR_ENV = (1 << 0),
120 + };
121 +
103 122 /*
104 123 * Create a new object database for the given repository.
105 124 *
@@ -112,8 +131,7 @@ struct object_database {
112 131 * Returns the newly created object database.
113 132 */
114 133 struct object_database *odb_new(struct repository *repo,
115 - const char *primary_source,
116 - const char *alternate_sources);
134 + enum odb_new_flags flags);
117 135
118 136 /* Free the object database and release all resources. */
119 137 void odb_free(struct object_database *o);
@@ -188,6 +206,29 @@ void odb_prepare(struct object_database *o, enum odb_prepare_flags flags);
188 206 /* Equivalent to `odb_prepare(o, ODB_PREPARE_FLUSH_CACHES)`. */
189 207 void odb_reprepare(struct object_database *o);
190 208
209 + enum odb_fsck_flags {
210 + /*
211 + * If set, perform a full consistency check for the full object
212 + * database, including all of its sources and the contents of their
213 + * optimized formats. Otherwise, only check the local source, and
214 + * restrict checks of its optimized formats to cheap structural
215 + * verification of their metadata.
216 + */
217 + ODB_FSCK_FULL = (1 << 0),
218 + };
219 +
220 + /* Options that shall be passed to `odb_fsck()`. */
221 + struct odb_fsck_options {
222 + enum odb_fsck_flags flags;
223 + };
224 +
225 + /*
226 + * Run backend-specific integrity checks on all object sources. Each source
227 + * performs the checks appropriate to its type. Returns 0 on success, a
228 + * negative error code otherwise.
229 + */
230 + int odb_fsck(struct object_database *odb, struct odb_fsck_options *opts);
231 +
191 232 /*
192 233 * Find source by its object directory path. Returns a `NULL` pointer in case
193 234 * the source could not be found.
@@ -247,12 +288,6 @@ void odb_for_each_alternate_ref(struct object_database *odb,
247 288 int odb_mkstemp(struct object_database *odb,
248 289 struct strbuf *temp_filename, const char *pattern);
249 290
250 - /*
251 - * Prepare alternate object sources for the given database by reading
252 - * "objects/info/alternates" and opening the respective sources.
253 - */
254 - void odb_prepare_alternates(struct object_database *odb);
255 -
256 291 /*
257 292 * Check whether the object database has any alternates. The primary object
258 293 * source does not count as alternate.
@@ -435,23 +470,14 @@ enum object_info_flags {
435 470 OBJECT_INFO_FOR_PREFETCH = (OBJECT_INFO_SKIP_FETCH_OBJECT | OBJECT_INFO_QUICK),
436 471 };
437 472
438 - enum odb_read_status {
439 - /* The read was successful. */
440 - ODB_READ_OK = 0,
441 - /* The read resulted in a generic error. */
442 - ODB_READ_ERROR = -1,
443 - /* The object could not be found. */
444 - ODB_READ_NOT_FOUND = -2,
445 - };
446 -
447 473 /*
448 474 * Read object info from the object database and populate the `object_info`
449 475 * structure. Returns 0 on success, a negative error code otherwise.
450 476 */
451 - enum odb_read_status odb_read_object_info_extended(struct object_database *odb,
452 - const struct object_id *oid,
453 - struct object_info *oi,
454 - enum object_info_flags flags);
477 + int odb_read_object_info_extended(struct object_database *odb,
478 + const struct object_id *oid,
479 + struct object_info *oi,
480 + enum object_info_flags flags);
455 481
456 482 /*
457 483 * Read a subset of object info for the given object ID. Returns an `enum
@@ -680,18 +706,166 @@ static inline int odb_write_object(struct object_database *odb,
680 706 return odb_write_object_ext(odb, buf, len, type, oid, NULL, 0);
681 707 }
682 708
683 - struct odb_write_stream;
709 + struct odb_stream;
684 710
685 711 int odb_write_object_stream(struct object_database *odb,
686 - struct odb_write_stream *stream, size_t len,
712 + struct odb_stream *stream,
687 713 struct object_id *oid);
688 714
715 + /*
716 + * Options for generating a packfile via `odb_generate_pack()`.
717 + */
718 + struct odb_generate_pack_options {
719 + /* Tips of the object graph that shall be packed. */
720 + struct oid_array wants;
721 +
722 + /*
723 + * Boundary of the object graph. Objects reachable from any of these
724 + * tips are expected to already be available to whoever consumes the
725 + * pack and shall thus not be packed.
726 + */
727 + struct oid_array haves;
728 +
729 + /*
730 + * The shallow boundary that shall be used when computing object
731 + * reachability. When set, any shallow information of the repository
732 + * itself shall be ignored in favor of these objects.
733 + */
734 + struct oid_array shallows;
735 +
736 + /*
737 + * Pre-expanded object filter specification that limits the set of
738 + * objects that shall be packed. May be `NULL` in case no filter shall
739 + * be applied.
740 + */
741 + const char *filter_spec;
742 +
743 + /*
744 + * Protocols that may be used to offload objects via packfile URIs.
745 + * May be `NULL` in case packfile URIs shall not be used.
746 + */
747 + const struct string_list *uri_protocols;
748 +
749 + /*
750 + * Hook command that shall be executed instead of the internal
751 + * machinery to generate the pack. It is up to the specific backend
752 + * whether or not this hook is supported. May be `NULL` in case no
753 + * hook shall be executed.
754 + */
755 + const char *pack_objects_hook;
756 +
757 + /*
758 + * File descriptor that the generated pack shall be written to. If set
759 + * to `-1`, a pipe will be created and exposed via the pack generator's
760 + * `out` field. If set to `0`, the pack will be written to the standard
761 + * output stream. Otherwise, the provided descriptor will be written to
762 + * and is consumed by the generator.
763 + */
764 + int pack_fd;
765 +
766 + /*
767 + * File descriptor that progress output shall be written to. The same
768 + * semantics as for `pack_fd` apply, except that `0` will cause the
769 + * generator to write to stderr instead of stdout.
770 + */
771 + int progress_fd;
772 +
773 + /* Whether to print progress or not. */
774 + enum {
775 + /* Don't print progress output. */
776 + ODB_GENERATE_PACK_PROGRESS_NONE,
777 +
778 + /*
779 + * Print progress while computing the packfile, but stop
780 + * printing progress once starting to write it.
781 + */
782 + ODB_GENERATE_PACK_PROGRESS_STANDARD,
783 +
784 + /*
785 + * Similar to STANDARD, but also print progress when writing
786 + * the packfile.
787 + */
788 + ODB_GENERATE_PACK_PROGRESS_VERBOSE,
789 + } progress;
790 +
791 + /* Allow the pack to contain deltas against unpacked objects. */
792 + unsigned thin:1;
793 +
794 + /* Use offset deltas instead of reference deltas. */
795 + unsigned ofs_delta:1;
796 +
797 + /* Include unasked-for annotated tags of packed objects. */
798 + unsigned include_tag:1;
799 +
800 + /* The generated pack is destined for a shallow consumer. */
801 + unsigned shallow:1;
802 +
803 + /* Allow objects that may be missing due to a promisor remote. */
804 + unsigned missing_allow_promisor:1;
805 +
806 + /* Do not use bitmap indices when computing reachability. */
807 + unsigned disable_bitmaps:1;
808 + };
809 +
810 + #define ODB_GENERATE_PACK_OPTIONS_INIT { \
811 + .wants = OID_ARRAY_INIT, \
812 + .haves = OID_ARRAY_INIT, \
813 + .shallows = OID_ARRAY_INIT, \
814 + .pack_fd = -1, \
815 + }
816 +
817 + /* Release resources associated with the options. */
818 + void odb_generate_pack_options_release(struct odb_generate_pack_options *opts);
819 +
820 + /*
821 + * A handle for an ongoing packfile generation as started via
822 + * `odb_generate_pack()`.
823 + */
824 + struct odb_pack_generator {
825 + /*
826 + * File descriptor from which the generated pack can be read. Only set
827 + * when the pack generation was started with `pack_fd == -1`. The
828 + * caller is responsible for closing the descriptor.
829 + */
830 + int out;
831 +
832 + /*
833 + * File descriptor from which progress output can be read. Only set
834 + * when the pack generation was started with `progress_fd == -1`. The
835 + * caller is responsible for closing the descriptor.
836 + */
837 + int err;
838 +
839 + /*
840 + * Callback function to finish this generator. This callback is
841 + * expected to wait for the packfile generation to complete and to then
842 + * free the generator itself.
843 + */
844 + int (*finish)(struct odb_pack_generator *);
845 + };
846 +
847 + /*
848 + * Start generating a packfile from the object database with the given
849 + * options. The pack is generated asynchronously; the caller is expected to
850 + * consume the file descriptors exposed via the pack generator and to then
851 + * wait for completion via `odb_pack_generator_finish()`.
852 + *
853 + * Returns 0 on success and populates the `out` pointer with the pack
854 + * generator. Returns a negative error code otherwise.
855 + */
856 + int odb_generate_pack(struct object_database *odb,
857 + struct odb_pack_generator **out,
858 + const struct odb_generate_pack_options *opts);
859 +
860 + /*
861 + * Wait for the packfile generation to complete and free the pack generator.
862 + * Returns 0 on success, a negative error code otherwise.
863 + */
864 + int odb_pack_generator_finish(struct odb_pack_generator *generator);
865 +
689 866 void parse_alternates(const char *string,
690 867 int sep,
691 868 const char *relative_base,
692 869 struct strvec *out);
693 870
694 - /* Free pointers inside of object_info, but not object_info itself */
695 - void free_object_info_contents(struct object_info *object_info);
696 -
697 871 #endif /* ODB_H */
modified odb/source-files.c
@@ -4,6 +4,7 @@
4 4 #include "chdir-notify.h"
5 5 #include "config.h"
6 6 #include "gettext.h"
7 + #include "hex.h"
7 8 #include "lockfile.h"
8 9 #include "object-file.h"
9 10 #include "odb.h"
@@ -51,6 +52,23 @@ static void odb_source_files_close(struct odb_source *source)
51 52 odb_source_close(&files->packed->base);
52 53 }
53 54
55 + static int odb_source_files_create_on_disk(struct odb_source *source)
56 + {
57 + struct strbuf path = STRBUF_INIT;
58 +
59 + safe_create_dir(source->odb->repo, source->path, 1);
60 +
61 + strbuf_addf(&path, "%s/pack", source->path);
62 + safe_create_dir(source->odb->repo, path.buf, 1);
63 +
64 + strbuf_reset(&path);
65 + strbuf_addf(&path, "%s/info", source->path);
66 + safe_create_dir(source->odb->repo, path.buf, 1);
67 +
68 + strbuf_release(&path);
69 + return 0;
70 + }
71 +
54 72 static void odb_source_files_prepare(struct odb_source *source,
55 73 enum odb_prepare_flags flags)
56 74 {
@@ -59,38 +77,21 @@ static void odb_source_files_prepare(struct odb_source *source,
59 77 odb_source_prepare(&files->packed->base, flags);
60 78 }
61 79
62 - static enum odb_read_status odb_source_files_read_object_info(struct odb_source *source,
63 - const struct object_id *oid,
64 - struct object_info *oi,
65 - enum object_info_flags flags,
66 - struct strbuf *errmsg)
80 + static int odb_source_files_read_object_info(struct odb_source *source,
81 + const struct object_id *oid,
82 + struct object_info *oi,
83 + enum object_info_flags flags)
67 84 {
68 85 struct odb_source_files *files = odb_source_files_downcast(source);
69 - enum odb_read_status ret_packed, ret_loose;
70 86
71 - ret_packed = odb_source_read_object_info(&files->packed->base, oid, oi,
72 - flags, errmsg);
73 - if (!ret_packed)
87 + if (!odb_source_read_object_info(&files->packed->base, oid, oi, flags) ||
88 + !odb_source_read_object_info(&files->loose->base, oid, oi, flags))
74 89 return 0;
75 90
76 - ret_loose = odb_source_read_object_info(&files->loose->base, oid, oi, flags,
77 - ret_packed == ODB_READ_NOT_FOUND ? errmsg : NULL);
78 - if (!ret_loose)
79 - return 0;
80 -
81 - /*
82 - * Reading the packed object may have failed even though the object
83 - * exists, for example because it is corrupt. Report this failure to
84 - * the caller in case neither of the sources was able to read the
85 - * object, and prefer the error of the packed source in case both
86 - * reads have failed.
87 - */
88 - if (ret_packed != ODB_READ_NOT_FOUND)
89 - return ret_packed;
90 - return ret_loose;
91 + return -1;
91 92 }
92 93
93 - static int odb_source_files_read_object_stream(struct odb_read_stream **out,
94 + static int odb_source_files_read_object_stream(struct odb_stream **out,
94 95 struct odb_source *source,
95 96 const struct object_id *oid)
96 97 {
@@ -201,12 +202,11 @@ static int odb_source_files_write_object(struct odb_source *source,
201 202 }
202 203
203 204 static int odb_source_files_write_object_stream(struct odb_source *source,
204 - struct odb_write_stream *stream,
205 - size_t len,
205 + struct odb_stream *stream,
206 206 struct object_id *oid)
207 207 {
208 208 struct odb_source_files *files = odb_source_files_downcast(source);
209 - return odb_source_write_object_stream(&files->loose->base, stream, len, oid);
209 + return odb_source_write_object_stream(&files->loose->base, stream, oid);
210 210 }
211 211
212 212 static int odb_source_files_begin_transaction(struct odb_source *source,
@@ -746,6 +746,168 @@ out:
746 746 return ret;
747 747 }
748 748
749 + struct odb_pack_generator_files {
750 + struct odb_pack_generator base;
751 + struct child_process cp;
752 + };
753 +
754 + static int odb_pack_generator_files_finish(struct odb_pack_generator *_generator)
755 + {
756 + struct odb_pack_generator_files *generator =
757 + (struct odb_pack_generator_files *)_generator;
758 + int ret;
759 +
760 + ret = finish_command(&generator->cp);
761 + free(generator);
762 +
763 + if (ret) {
764 + /*
765 + * On failure, pack-objects is expected to have written a
766 + * useful error message to its standard error stream already.
767 + * Death by signal is worth mentioning, though, with the
768 + * exception of SIGPIPE: that is a normal occurrence when the
769 + * consumer of the pack hangs up.
770 + */
771 + if (ret > 128 && ret - 128 == SIGPIPE)
772 + return -1;
773 + if (ret > 128)
774 + error(_("pack-objects died of signal %d"), ret - 128);
775 + return -1;
776 + }
777 +
778 + return 0;
779 + }
780 +
781 + static int odb_source_files_generate_pack(struct odb_source *source UNUSED,
782 + struct odb_pack_generator **out,
783 + const struct odb_generate_pack_options *opts)
784 + {
785 + struct odb_pack_generator_files *generator;
786 + struct child_process *cp;
787 + FILE *in;
788 +
789 + CALLOC_ARRAY(generator, 1);
790 + child_process_init(&generator->cp);
791 + cp = &generator->cp;
792 +
793 + /*
794 + * The hook is expected to spawn "$hook git pack-objects <args...>"
795 + * and to behave like git-pack-objects(1) would have. This can for
796 + * example be used to serve precomputed packfiles.
797 + */
798 + if (opts->pack_objects_hook) {
799 + strvec_push(&cp->args, opts->pack_objects_hook);
800 + strvec_push(&cp->args, "git");
801 + cp->use_shell = 1;
802 + } else {
803 + cp->git_cmd = 1;
804 + }
805 +
806 + /*
807 + * The caller-provided shallow boundary overrides any shallow state
808 + * that the repository itself may have, so the shallow file needs to
809 + * be neutralized.
810 + */
811 + if (opts->shallows.nr) {
812 + strvec_push(&cp->args, "--shallow-file");
813 + strvec_push(&cp->args, "");
814 + }
815 + strvec_push(&cp->args, "pack-objects");
816 + strvec_push(&cp->args, "--revs");
817 + strvec_push(&cp->args, "--stdout");
818 + if (opts->thin)
819 + strvec_push(&cp->args, "--thin");
820 + if (opts->shallow)
821 + strvec_push(&cp->args, "--shallow");
822 + if (opts->ofs_delta)
823 + strvec_push(&cp->args, "--delta-base-offset");
824 + if (opts->include_tag)
825 + strvec_push(&cp->args, "--include-tag");
826 + if (opts->missing_allow_promisor)
827 + strvec_push(&cp->args, "--missing=allow-promisor");
828 + if (opts->disable_bitmaps)
829 + strvec_push(&cp->args, "--no-use-bitmap-index");
830 + switch (opts->progress) {
831 + case ODB_GENERATE_PACK_PROGRESS_NONE:
832 + strvec_push(&cp->args, "--quiet");
833 + break;
834 + case ODB_GENERATE_PACK_PROGRESS_STANDARD:
835 + strvec_push(&cp->args, "--progress");
836 + break;
837 + case ODB_GENERATE_PACK_PROGRESS_VERBOSE:
838 + strvec_push(&cp->args, "--all-progress");
839 + break;
840 + default:
841 + BUG("unknown progress option %d", opts->progress);
842 + }
843 + if (opts->filter_spec)
844 + strvec_pushf(&cp->args, "--filter=%s", opts->filter_spec);
845 + if (opts->uri_protocols)
846 + for (size_t i = 0; i < opts->uri_protocols->nr; i++)
847 + strvec_pushf(&cp->args, "--uri-protocol=%s",
848 + opts->uri_protocols->items[i].string);
849 +
850 + cp->in = -1;
851 + cp->out = opts->pack_fd;
852 + cp->err = opts->progress_fd;
853 + cp->clean_on_exit = 1;
854 +
855 + if (start_command(cp)) {
856 + free(generator);
857 + return error(_("could not spawn pack-objects"));
858 + }
859 +
860 + /*
861 + * Feed the objects to pack-objects. This is safe to do synchronously
862 + * because pack-objects consumes all of its standard input before it
863 + * starts to generate the pack.
864 + */
865 + in = xfdopen(cp->in, "w");
866 + for (size_t i = 0; i < opts->shallows.nr; i++)
867 + fprintf(in, "--shallow %s\n", oid_to_hex(&opts->shallows.oid[i]));
868 + for (size_t i = 0; i < opts->wants.nr; i++)
869 + fprintf(in, "%s\n", oid_to_hex(&opts->wants.oid[i]));
870 + fprintf(in, "--not\n");
871 + for (size_t i = 0; i < opts->haves.nr; i++)
872 + fprintf(in, "%s\n", oid_to_hex(&opts->haves.oid[i]));
873 + fprintf(in, "\n");
874 + fflush(in);
875 + if (ferror(in)) {
876 + error(_("error writing to pack-objects"));
877 + fclose(in);
878 + if (opts->pack_fd < 0)
879 + close(cp->out);
880 + if (opts->progress_fd < 0)
881 + close(cp->err);
882 + finish_command(cp);
883 + free(generator);
884 + return -1;
885 + }
886 + fclose(in);
887 +
888 + generator->base.out = opts->pack_fd < 0 ? cp->out : -1;
889 + generator->base.err = opts->progress_fd < 0 ? cp->err : -1;
890 + generator->base.finish = odb_pack_generator_files_finish;
891 +
892 + *out = &generator->base;
893 + return 0;
894 + }
895 +
896 + static int odb_source_files_fsck(struct odb_source *source,
897 + struct odb_fsck_options *opts)
898 + {
899 + struct odb_source_files *files = odb_source_files_downcast(source);
900 + int ret = 0;
901 +
902 + if (!(opts->flags & ODB_FSCK_FULL) && !source->local)
903 + return 0;
904 +
905 + ret |= odb_source_fsck(&files->loose->base, opts);
906 + ret |= odb_source_fsck(&files->packed->base, opts);
907 +
908 + return ret;
909 + }
910 +
749 911 struct odb_source_files *odb_source_files_new(struct object_database *odb,
750 912 const char *path,
751 913 bool local)
@@ -759,7 +921,9 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
759 921
760 922 files->base.free = odb_source_files_free;
761 923 files->base.close = odb_source_files_close;
924 + files->base.create_on_disk = odb_source_files_create_on_disk;
762 925 files->base.prepare = odb_source_files_prepare;
926 + files->base.fsck = odb_source_files_fsck;
763 927 files->base.read_object_info = odb_source_files_read_object_info;
764 928 files->base.read_object_stream = odb_source_files_read_object_stream;
765 929 files->base.for_each_object = odb_source_files_for_each_object;
@@ -773,6 +937,7 @@ struct odb_source_files *odb_source_files_new(struct object_database *odb,
773 937 files->base.write_alternate = odb_source_files_write_alternate;
774 938 files->base.optimize = odb_source_files_optimize;
775 939 files->base.optimize_required = odb_source_files_optimize_required;
940 + files->base.generate_pack = odb_source_files_generate_pack;
776 941
777 942 /*
778 943 * Ideally, we would only ever store absolute paths in the source. This
modified odb/source-files.h
@@ -43,7 +43,9 @@ bool odb_source_files_optimize_required(struct odb_source *source,
43 43 static inline struct odb_source_files *odb_source_files_downcast(struct odb_source *source)
44 44 {
45 45 if (source->type != ODB_SOURCE_FILES)
46 - BUG("trying to downcast source of type '%d' to files", source->type);
46 + BUG("trying to downcast source of type '%s' to '%s'",
47 + odb_source_type_to_name(source->type),
48 + odb_source_type_to_name(ODB_SOURCE_FILES));
47 49 return container_of(source, struct odb_source_files, base);
48 50 }
49 51
modified odb/source-inmemory.c
@@ -1,6 +1,7 @@
1 1 #include "git-compat-util.h"
2 2 #include "object-file.h"
3 3 #include "odb.h"
4 + #include "fsck.h"
4 5 #include "odb/source-inmemory.h"
5 6 #include "odb/streaming.h"
6 7 #include "oidtree.h"
@@ -56,30 +57,29 @@ static void populate_object_info(struct odb_source_inmemory *source,
56 57 oi->source_infop->source = &source->base;
57 58 }
58 59
59 - static enum odb_read_status odb_source_inmemory_read_object_info(struct odb_source *source,
60 - const struct object_id *oid,
61 - struct object_info *oi,
62 - enum object_info_flags flags UNUSED,
63 - struct strbuf *errmsg UNUSED)
60 + static int odb_source_inmemory_read_object_info(struct odb_source *source,
61 + const struct object_id *oid,
62 + struct object_info *oi,
63 + enum object_info_flags flags UNUSED)
64 64 {
65 65 struct odb_source_inmemory *inmemory = odb_source_inmemory_downcast(source);
66 66 const struct inmemory_object *object;
67 67
68 68 object = find_cached_object(inmemory, oid);
69 69 if (!object)
70 - return ODB_READ_NOT_FOUND;
70 + return -1;
71 71
72 72 populate_object_info(inmemory, oi, object);
73 73 return 0;
74 74 }
75 75
76 76 struct odb_read_stream_inmemory {
77 - struct odb_read_stream base;
77 + struct odb_stream base;
78 78 const unsigned char *buf;
79 79 size_t offset;
80 80 };
81 81
82 - static ssize_t odb_read_stream_inmemory_read(struct odb_read_stream *stream,
82 + static ssize_t odb_read_stream_inmemory_read(struct odb_stream *stream,
83 83 char *buf, size_t buf_len)
84 84 {
85 85 struct odb_read_stream_inmemory *inmemory =
@@ -95,12 +95,12 @@ static ssize_t odb_read_stream_inmemory_read(struct odb_read_stream *stream,
95 95 return bytes;
96 96 }
97 97
98 - static int odb_read_stream_inmemory_close(struct odb_read_stream *stream UNUSED)
98 + static int odb_read_stream_inmemory_close(struct odb_stream *stream UNUSED)
99 99 {
100 100 return 0;
101 101 }
102 102
103 - static int odb_source_inmemory_read_object_stream(struct odb_read_stream **out,
103 + static int odb_source_inmemory_read_object_stream(struct odb_stream **out,
104 104 struct odb_source *source,
105 105 const struct object_id *oid)
106 106 {
@@ -257,8 +257,7 @@ static int odb_source_inmemory_write_object(struct odb_source *source,
257 257 }
258 258
259 259 static int odb_source_inmemory_write_object_stream(struct odb_source *source,
260 - struct odb_write_stream *stream,
261 - size_t len,
260 + struct odb_stream *stream,
262 261 struct object_id *oid)
263 262 {
264 263 char buf[16384];
@@ -266,12 +265,19 @@ static int odb_source_inmemory_write_object_stream(struct odb_source *source,
266 265 char *data;
267 266 int ret;
268 267
269 - CALLOC_ARRAY(data, len);
270 - while (!stream->is_finished) {
268 + CALLOC_ARRAY(data, stream->size);
269 + while (1) {
271 270 ssize_t bytes_read;
272 271
273 - bytes_read = odb_write_stream_read(stream, buf, sizeof(buf));
274 - if (total_read + bytes_read > len) {
272 + bytes_read = odb_stream_read(stream, buf, sizeof(buf));
273 + if (bytes_read < 0) {
274 + ret = error("failed to read object stream");
275 + goto out;
276 + }
277 + if (!bytes_read)
278 + break;
279 +
280 + if (total_read + bytes_read > stream->size) {
275 281 ret = error("object stream yielded more bytes than expected");
276 282 goto out;
277 283 }
@@ -280,15 +286,16 @@ static int odb_source_inmemory_write_object_stream(struct odb_source *source,
280 286 total_read += bytes_read;
281 287 }
282 288
283 - if (total_read != len) {
289 + if (total_read != stream->size) {
284 290 ret = error("object stream yielded less bytes than expected");
285 291 goto out;
286 292 }
287 293
288 - hash_object_file(source->odb->repo->hash_algo, data, total_read, OBJ_BLOB, oid);
294 + hash_object_file(source->odb->repo->hash_algo, data, total_read,
295 + stream->type, oid);
289 296
290 - ret = odb_source_inmemory_write_object(source, data, len, OBJ_BLOB, oid,
291 - NULL, NULL, 0);
297 + ret = odb_source_inmemory_write_object(source, data, stream->size,
298 + stream->type, oid, NULL, NULL, 0);
292 299 if (ret < 0)
293 300 goto out;
294 301
@@ -362,6 +369,12 @@ static void odb_source_inmemory_free(struct odb_source *source)
362 369 free(inmemory);
363 370 }
364 371
372 + static int odb_source_inmemory_fsck(struct odb_source *source UNUSED,
373 + struct odb_fsck_options *opts UNUSED)
374 + {
375 + return 0;
376 + }
377 +
365 378 struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
366 379 {
367 380 struct odb_source_inmemory *source;
@@ -372,6 +385,7 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
372 385 source->base.free = odb_source_inmemory_free;
373 386 source->base.close = odb_source_inmemory_close;
374 387 source->base.prepare = odb_source_inmemory_prepare;
388 + source->base.fsck = odb_source_inmemory_fsck;
375 389 source->base.read_object_info = odb_source_inmemory_read_object_info;
376 390 source->base.read_object_stream = odb_source_inmemory_read_object_stream;
377 391 source->base.for_each_object = odb_source_inmemory_for_each_object;
modified odb/source-inmemory.h
@@ -26,7 +26,9 @@ struct odb_source_inmemory *odb_source_inmemory_new(struct object_database *odb)
26 26 static inline struct odb_source_inmemory *odb_source_inmemory_downcast(struct odb_source *source)
27 27 {
28 28 if (source->type != ODB_SOURCE_INMEMORY)
29 - BUG("trying to downcast source of type '%d' to in-memory", source->type);
29 + BUG("trying to downcast source of type '%s' to '%s'",
30 + odb_source_type_to_name(source->type),
31 + odb_source_type_to_name(ODB_SOURCE_INMEMORY));
30 32 return container_of(source, struct odb_source_inmemory, base);
31 33 }
32 34
modified odb/source-loose.c
@@ -67,8 +67,7 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
67 67 const char *path,
68 68 const struct object_id *oid,
69 69 struct object_info *oi,
70 - enum object_info_flags flags,
71 - struct strbuf *errmsg)
70 + enum object_info_flags flags)
72 71 {
73 72 int ret;
74 73 int fd;
@@ -92,16 +91,11 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
92 91 struct stat st;
93 92
94 93 if ((!oi || (!oi->disk_sizep && !oi->mtimep)) && (flags & OBJECT_INFO_QUICK)) {
95 - ret = quick_has_loose(loose, oid) ? 0 : ODB_READ_NOT_FOUND;
94 + ret = quick_has_loose(loose, oid) ? 0 : -1;
96 95 goto out;
97 96 }
98 97
99 98 if (lstat(path, &st) < 0) {
100 - if (errno == ENOENT) {
101 - ret = ODB_READ_NOT_FOUND;
102 - goto out;
103 - }
104 -
105 99 ret = -1;
106 100 goto out;
107 101 }
@@ -119,12 +113,9 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
119 113
120 114 fd = git_open(path);
121 115 if (fd < 0) {
122 - if (errno == ENOENT) {
123 - ret = ODB_READ_NOT_FOUND;
124 - goto out;
125 - }
126 -
127 - ret = error_errno(_("unable to open loose object %s"), oid_to_hex(oid));
116 + if (errno != ENOENT)
117 + error_errno(_("unable to open loose object %s"), oid_to_hex(oid));
118 + ret = -1;
128 119 goto out;
129 120 }
130 121
@@ -164,7 +155,7 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
164 155
165 156 if (parse_loose_header(hdr, oi) < 0) {
166 157 ret = error(_("unable to parse %s header"), oid_to_hex(oid));
167 - goto out;
158 + goto corrupt;
168 159 }
169 160
170 161 if (*oi->typep < 0)
@@ -174,7 +165,7 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
174 165 *oi->contentp = unpack_loose_rest(&stream, hdr, *oi->sizep, oid);
175 166 if (!*oi->contentp) {
176 167 ret = -1;
177 - goto out;
168 + goto corrupt;
178 169 }
179 170 }
180 171
@@ -182,19 +173,21 @@ static int read_object_info_from_path(struct odb_source_loose *loose,
182 173 case ULHR_BAD:
183 174 ret = error(_("unable to unpack %s header"),
184 175 oid_to_hex(oid));
185 - goto out;
176 + goto corrupt;
186 177 case ULHR_TOO_LONG:
187 178 ret = error(_("header for %s too long, exceeds %d bytes"),
188 179 oid_to_hex(oid), MAX_HEADER_LEN);
189 - goto out;
180 + goto corrupt;
190 181 }
191 182
192 183 ret = 0;
193 184
185 + corrupt:
186 + if (ret && (flags & OBJECT_INFO_DIE_IF_CORRUPT))
187 + die(_("loose object %s (stored in %s) is corrupt"),
188 + oid_to_hex(oid), path);
189 +
194 190 out:
195 - if (ret && ret != ODB_READ_NOT_FOUND && errmsg)
196 - strbuf_addf(errmsg, _("loose object %s (stored in %s) is corrupt"),
197 - oid_to_hex(oid), path);
198 191 if (stream_to_end)
199 192 git_inflate_end(stream_to_end);
200 193 if (map)
@@ -213,11 +206,10 @@ out:
213 206 return ret;
214 207 }
215 208
216 - static enum odb_read_status odb_source_loose_read_object_info(struct odb_source *source,
217 - const struct object_id *oid,
218 - struct object_info *oi,
219 - enum object_info_flags flags,
220 - struct strbuf *errmsg)
209 + static int odb_source_loose_read_object_info(struct odb_source *source,
210 + const struct object_id *oid,
211 + struct object_info *oi,
212 + enum object_info_flags flags)
221 213 {
222 214 struct odb_source_loose *loose = odb_source_loose_downcast(source);
223 215 static struct strbuf buf = STRBUF_INIT;
@@ -229,10 +221,10 @@ static enum odb_read_status odb_source_loose_read_object_info(struct odb_source
229 221 * second time.
230 222 */
231 223 if (flags & OBJECT_INFO_SECOND_READ)
232 - return ODB_READ_NOT_FOUND;
224 + return -1;
233 225
234 226 odb_loose_path(loose, &buf, oid);
235 - return read_object_info_from_path(loose, buf.buf, oid, oi, flags, errmsg);
227 + return read_object_info_from_path(loose, buf.buf, oid, oi, flags);
236 228 }
237 229
238 230 /*
@@ -286,7 +278,7 @@ out:
286 278 }
287 279
288 280 struct odb_loose_read_stream {
289 - struct odb_read_stream base;
281 + struct odb_stream base;
290 282 git_zstream z;
291 283 enum {
292 284 ODB_LOOSE_READ_STREAM_INUSE,
@@ -300,7 +292,7 @@ struct odb_loose_read_stream {
300 292 int hdr_used;
301 293 };
302 294
303 - static ssize_t read_istream_loose(struct odb_read_stream *_st, char *buf, size_t sz)
295 + static ssize_t read_istream_loose(struct odb_stream *_st, char *buf, size_t sz)
304 296 {
305 297 struct odb_loose_read_stream *st =
306 298 container_of(_st, struct odb_loose_read_stream, base);
@@ -347,7 +339,7 @@ static ssize_t read_istream_loose(struct odb_read_stream *_st, char *buf, size_t
347 339 return total_read;
348 340 }
349 341
350 - static int close_istream_loose(struct odb_read_stream *_st)
342 + static int close_istream_loose(struct odb_stream *_st)
351 343 {
352 344 struct odb_loose_read_stream *st =
353 345 container_of(_st, struct odb_loose_read_stream, base);
@@ -358,7 +350,7 @@ static int close_istream_loose(struct odb_read_stream *_st)
358 350 return 0;
359 351 }
360 352
361 - static int odb_source_loose_read_object_stream(struct odb_read_stream **out,
353 + static int odb_source_loose_read_object_stream(struct odb_stream **out,
362 354 struct odb_source *source,
363 355 const struct object_id *oid)
364 356 {
@@ -429,7 +421,7 @@ static int for_each_object_wrapper_cb(const struct object_id *oid,
429 421 if (data->request) {
430 422 struct object_info oi = *data->request;
431 423
432 - if (read_object_info_from_path(data->loose, path, oid, &oi, 0, NULL) < 0)
424 + if (read_object_info_from_path(data->loose, path, oid, &oi, 0) < 0)
433 425 return -1;
434 426
435 427 return data->cb(oid, &oi, data->cb_data);
@@ -447,7 +439,7 @@ static int for_each_prefixed_object_wrapper_cb(const struct object_id *oid,
447 439 struct object_info oi = *data->request;
448 440
449 441 if (odb_source_read_object_info(&data->loose->base,
450 - oid, &oi, 0, NULL) < 0)
442 + oid, &oi, 0) < 0)
451 443 return -1;
452 444
453 445 return data->cb(oid, &oi, data->cb_data);
@@ -853,8 +845,7 @@ static int odb_source_loose_write_object(struct odb_source *source,
853 845 }
854 846
855 847 static int odb_source_loose_write_object_stream(struct odb_source *source,
856 - struct odb_write_stream *in_stream,
857 - size_t len,
848 + struct odb_stream *in_stream,
858 849 struct object_id *oid)
859 850 {
860 851 struct odb_source_loose *loose = odb_source_loose_downcast(source);
@@ -868,6 +859,7 @@ static int odb_source_loose_write_object_stream(struct odb_source *source,
868 859 struct strbuf filename = STRBUF_INIT;
869 860 unsigned char buf[8192];
870 861 int dirlen;
862 + bool is_finished = false;
871 863 char hdr[MAX_HEADER_LEN];
872 864 int hdrlen;
873 865
@@ -876,7 +868,7 @@ static int odb_source_loose_write_object_stream(struct odb_source *source,
876 868
877 869 /* Since oid is not determined, save tmp file to odb path. */
878 870 strbuf_addf(&filename, "%s/", loose->base.path);
879 - hdrlen = format_object_header(hdr, sizeof(hdr), OBJ_BLOB, len);
871 + hdrlen = format_object_header(hdr, sizeof(hdr), in_stream->type, in_stream->size);
880 872
881 873 /*
882 874 * Common steps for write_loose_object and stream_loose_object to
@@ -898,21 +890,24 @@ static int odb_source_loose_write_object_stream(struct odb_source *source,
898 890 do {
899 891 unsigned char *in0 = stream.next_in;
900 892
901 - if (!stream.avail_in && !in_stream->is_finished) {
902 - ssize_t read_len = odb_write_stream_read(in_stream, buf,
903 - sizeof(buf));
893 + if (!stream.avail_in && !is_finished) {
894 + ssize_t read_len = odb_stream_read(in_stream, buf,
895 + sizeof(buf));
904 896 if (read_len < 0) {
905 897 close(fd);
906 898 err = -1;
907 899 goto cleanup;
908 900 }
909 901
902 + /* All data has been read. */
903 + if (!read_len) {
904 + is_finished = true;
905 + flush = 1;
906 + }
907 +
910 908 stream.avail_in = read_len;
911 909 stream.next_in = buf;
912 910 in0 = buf;
913 - /* All data has been read. */
914 - if (in_stream->is_finished)
915 - flush = 1;
916 911 }
917 912 ret = write_loose_object_common(loose, &c, &compat_c, &stream, flush, in0, fd,
918 913 compressed, sizeof(compressed));
@@ -924,9 +919,9 @@ static int odb_source_loose_write_object_stream(struct odb_source *source,
924 919 */
925 920 } while (ret == Z_OK || ret == Z_BUF_ERROR);
926 921
927 - if (stream.total_in != len + hdrlen)
922 + if (stream.total_in != in_stream->size + hdrlen)
928 923 die(_("write stream object %"PRIuMAX" != %"PRIuMAX), (uintmax_t)stream.total_in,
929 - (uintmax_t)len + hdrlen);
924 + (uintmax_t)in_stream->size + hdrlen);
930 925
931 926 /*
932 927 * Common steps for write_loose_object and stream_loose_object to
@@ -1036,6 +1031,12 @@ static void odb_source_loose_free(struct odb_source *source)
1036 1031 free(loose);
1037 1032 }
1038 1033
1034 + static int odb_source_loose_fsck(struct odb_source *source UNUSED,
1035 + struct odb_fsck_options *opts UNUSED)
1036 + {
1037 + return 0;
1038 + }
1039 +
1039 1040 struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
1040 1041 const char *path,
1041 1042 bool local)
@@ -1048,6 +1049,7 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
1048 1049 loose->base.free = odb_source_loose_free;
1049 1050 loose->base.close = odb_source_loose_close;
1050 1051 loose->base.prepare = odb_source_loose_prepare;
1052 + loose->base.fsck = odb_source_loose_fsck;
1051 1053 loose->base.read_object_info = odb_source_loose_read_object_info;
1052 1054 loose->base.read_object_stream = odb_source_loose_read_object_stream;
1053 1055 loose->base.for_each_object = odb_source_loose_for_each_object;
@@ -1063,5 +1065,7 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
1063 1065 if (!is_absolute_path(loose->base.path))
1064 1066 chdir_notify_register(NULL, odb_source_loose_reparent, loose);
1065 1067
1068 + loose_object_map_load(loose);
1069 +
1066 1070 return loose;
1067 1071 }
modified odb/source-loose.h
@@ -41,7 +41,9 @@ struct odb_source_loose *odb_source_loose_new(struct object_database *odb,
41 41 static inline struct odb_source_loose *odb_source_loose_downcast(struct odb_source *source)
42 42 {
43 43 if (source->type != ODB_SOURCE_LOOSE)
44 - BUG("trying to downcast source of type '%d' to loose", source->type);
44 + BUG("trying to downcast source of type '%s' to '%s'",
45 + odb_source_type_to_name(source->type),
46 + odb_source_type_to_name(ODB_SOURCE_LOOSE));
45 47 return container_of(source, struct odb_source_loose, base);
46 48 }
47 49
modified odb/source-packed.c
@@ -2,9 +2,8 @@
2 2 #include "abspath.h"
3 3 #include "chdir-notify.h"
4 4 #include "dir.h"
5 - #include "gettext.h"
5 + #include "fsck.h"
6 6 #include "git-zlib.h"
7 - #include "hex.h"
8 7 #include "list-objects-filter-options.h"
9 8 #include "mergesort.h"
10 9 #include "midx.h"
@@ -12,23 +11,21 @@
12 11 #include "odb/streaming.h"
13 12 #include "packfile.h"
14 13 #include "pack-bitmap.h"
15 - #include "strbuf.h"
16 14
17 15 static int find_pack_entry(struct odb_source_packed *store,
18 16 const struct object_id *oid,
19 - struct pack_entry *e,
20 - struct packed_git **bad_pack)
17 + struct pack_entry *e)
21 18 {
22 19 struct packfile_list_entry *l;
23 20
24 21 odb_source_prepare(&store->base, 0);
25 - if (store->midx && fill_midx_entry(store->midx, oid, e, bad_pack))
22 + if (store->midx && fill_midx_entry(store->midx, oid, e))
26 23 return 1;
27 24
28 25 for (l = store->packs.head; l; l = l->next) {
29 26 struct packed_git *p = l->pack;
30 27
31 - if (!p->multi_pack_index && packfile_fill_entry(p, oid, e, bad_pack)) {
28 + if (!p->multi_pack_index && packfile_fill_entry(p, oid, e)) {
32 29 if (!store->skip_mru_updates)
33 30 packfile_list_prepend(&store->packs, p);
34 31 return 1;
@@ -38,14 +35,12 @@ static int find_pack_entry(struct odb_source_packed *store,
38 35 return 0;
39 36 }
40 37
41 - static enum odb_read_status odb_source_packed_read_object_info(struct odb_source *source,
42 - const struct object_id *oid,
43 - struct object_info *oi,
44 - enum object_info_flags flags,
45 - struct strbuf *errmsg)
38 + static int odb_source_packed_read_object_info(struct odb_source *source,
39 + const struct object_id *oid,
40 + struct object_info *oi,
41 + enum object_info_flags flags)
46 42 {
47 43 struct odb_source_packed *packed = odb_source_packed_downcast(source);
48 - struct packed_git *bad_pack = NULL;
49 44 struct pack_entry e;
50 45 int ret;
51 46
@@ -57,55 +52,33 @@ static enum odb_read_status odb_source_packed_read_object_info(struct odb_source
57 52 if (flags & OBJECT_INFO_SECOND_READ)
58 53 odb_source_prepare(source, ODB_PREPARE_FLUSH_CACHES);
59 54
60 - if (!find_pack_entry(packed, oid, &e, &bad_pack)) {
61 - /*
62 - * The lookup may have failed because the object is known to be
63 - * corrupt in one of the packfiles. Report the object as
64 - * corrupt instead of missing in that case.
65 - */
66 - if (bad_pack) {
67 - ret = -1;
68 - goto out;
69 - }
70 -
71 - ret = ODB_READ_NOT_FOUND;
72 - goto out;
73 - }
55 + if (!find_pack_entry(packed, oid, &e))
56 + return 1;
74 57
75 58 /*
76 59 * We know that the caller doesn't actually need the
77 60 * information below, so return early.
78 61 */
79 - if (!oi) {
80 - ret = 0;
81 - goto out;
82 - }
62 + if (!oi)
63 + return 0;
83 64
84 65 ret = packed_object_info(packed, e.p, e.offset, oi);
85 66 if (ret < 0) {
86 - bad_pack = e.p;
87 67 mark_bad_packed_object(e.p, oid);
88 - goto out;
68 + return -1;
89 69 }
90 70
91 - ret = 0;
92 -
93 - out:
94 - if (ret < 0 && bad_pack && errmsg)
95 - strbuf_addf(errmsg, _("packed object %s (stored in %s) is corrupt"),
96 - oid_to_hex(oid), bad_pack->pack_name);
97 -
98 - return ret;
71 + return 0;
99 72 }
100 73
101 - static int odb_source_packed_read_object_stream(struct odb_read_stream **out,
74 + static int odb_source_packed_read_object_stream(struct odb_stream **out,
102 75 struct odb_source *source,
103 76 const struct object_id *oid)
104 77 {
105 78 struct odb_source_packed *packed = odb_source_packed_downcast(source);
106 79 struct pack_entry e;
107 80
108 - if (!find_pack_entry(packed, oid, &e, NULL))
81 + if (!find_pack_entry(packed, oid, &e))
109 82 return -1;
110 83
111 84 return packfile_read_object_stream(out, oid, e.p, e.offset);
@@ -611,7 +584,7 @@ static int odb_source_packed_freshen_object(struct odb_source *source,
611 584 timesp = &times;
612 585 }
613 586
614 - if (!find_pack_entry(packed, oid, &e, NULL))
587 + if (!find_pack_entry(packed, oid, &e))
615 588 return 0;
616 589 if (e.p->is_cruft)
617 590 return 0;
@@ -637,8 +610,7 @@ static int odb_source_packed_write_object(struct odb_source *source UNUSED,
637 610 }
638 611
639 612 static int odb_source_packed_write_object_stream(struct odb_source *source UNUSED,
640 - struct odb_write_stream *stream UNUSED,
641 - size_t len UNUSED,
613 + struct odb_stream *stream UNUSED,
642 614 struct object_id *oid UNUSED)
643 615 {
644 616 return error("packed backend cannot write object streams");
@@ -855,6 +827,12 @@ static void odb_source_packed_free(struct odb_source *source)
855 827 free(packed);
856 828 }
857 829
830 + static int odb_source_packed_fsck(struct odb_source *source UNUSED,
831 + struct odb_fsck_options *opts UNUSED)
832 + {
833 + return 0;
834 + }
835 +
858 836 struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
859 837 const char *path,
860 838 bool local)
@@ -868,6 +846,7 @@ struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
868 846 packed->base.free = odb_source_packed_free;
869 847 packed->base.close = odb_source_packed_close;
870 848 packed->base.prepare = odb_source_packed_prepare;
849 + packed->base.fsck = odb_source_packed_fsck;
871 850 packed->base.read_object_info = odb_source_packed_read_object_info;
872 851 packed->base.read_object_stream = odb_source_packed_read_object_stream;
873 852 packed->base.for_each_object = odb_source_packed_for_each_object;
modified odb/source-packed.h
@@ -78,7 +78,9 @@ struct odb_source_packed *odb_source_packed_new(struct object_database *odb,
78 78 static inline struct odb_source_packed *odb_source_packed_downcast(struct odb_source *source)
79 79 {
80 80 if (source->type != ODB_SOURCE_PACKED)
81 - BUG("trying to downcast source of type '%d' to packed", source->type);
81 + BUG("trying to downcast source of type '%s' to '%s'",
82 + odb_source_type_to_name(source->type),
83 + odb_source_type_to_name(ODB_SOURCE_PACKED));
82 84 return container_of(source, struct odb_source_packed, base);
83 85 }
84 86
modified odb/source.c
@@ -4,6 +4,25 @@
4 4 #include "odb/source.h"
5 5 #include "packfile.h"
6 6
7 + static const char * const odb_source_names_by_type[] = {
8 + [ODB_SOURCE_UNKNOWN] = "unknown",
9 + [ODB_SOURCE_FILES] = "files",
10 + [ODB_SOURCE_LOOSE] = "loose",
11 + [ODB_SOURCE_PACKED] = "packed",
12 + [ODB_SOURCE_INMEMORY] = "in-memory",
13 + };
14 +
15 + const char *odb_source_type_to_name(enum odb_source_type type)
16 + {
17 + const char *name;
18 + if (type < 0 || type >= ARRAY_SIZE(odb_source_names_by_type))
19 + type = ODB_SOURCE_UNKNOWN;
20 + name = odb_source_names_by_type[type];
21 + if (!name)
22 + BUG("name missing in `odb_source_names_by_type` for '%d'", type);
23 + return name;
24 + }
25 +
7 26 struct odb_source *odb_source_new(struct object_database *odb,
8 27 const char *path,
9 28 bool local)
modified odb/source.h
@@ -1,6 +1,7 @@
1 1 #ifndef ODB_SOURCE_H
2 2 #define ODB_SOURCE_H
3 3
4 + #include "hashmap.h"
4 5 #include "object.h"
5 6 #include "odb.h"
6 7 #include "odb/transaction.h"
@@ -25,9 +26,14 @@ enum odb_source_type {
25 26 ODB_SOURCE_INMEMORY,
26 27 };
27 28
29 + /*
30 + * Convert between the enum and its name. Returns the equivalent of "unknown"
31 + * for unknown types.
32 + */
33 + const char *odb_source_type_to_name(enum odb_source_type type);
34 +
28 35 struct object_id;
29 - struct odb_read_stream;
30 - struct strbuf;
36 + struct odb_stream;
31 37 struct strvec;
32 38
33 39 /*
@@ -45,6 +51,12 @@ struct strvec;
45 51 struct odb_source {
46 52 struct odb_source *next;
47 53
54 + /*
55 + * Entry in the object database's map of sources, keyed by this
56 + * source's path.
57 + */
58 + struct hashmap_entry by_path_entry;
59 +
48 60 /* Object database that owns this object source. */
49 61 struct object_database *odb;
50 62
@@ -84,6 +96,18 @@ struct odb_source {
84 96 */
85 97 void (*close)(struct odb_source *source);
86 98
99 + /*
100 + * This callback is expected to create on-disk data structures that are
101 + * required for this source to operate.
102 + *
103 + * The callback is expected to return 0 on success, a negative error
104 + * code otherwise.
105 + *
106 + * This callback may be NULL in case the source does not need any
107 + * on-disk setup.
108 + */
109 + int (*create_on_disk)(struct odb_source *source);
110 +
87 111 /*
88 112 * This callback is expected to prepare the source so that it becomes
89 113 * ready for use. It optionally clears underlying caches of the object
@@ -111,17 +135,13 @@ struct odb_source {
111 135 * second read in case they know that the first read would have
112 136 * already surfaced the object without reloading any on-disk state.
113 137 *
114 - * The callback is expected to return an `enum odb_read_status`. Please
115 - * refer to the individual values that can be returned. In case reading
116 - * the object has failed with a generic error and `errmsg` is non-NULL,
117 - * the callback is expected to populate it with a human-readable
118 - * message that describes the failure.
138 + * The callback is expected to return a negative error code in case
139 + * reading the object has failed, 0 otherwise.
119 140 */
120 - enum odb_read_status (*read_object_info)(struct odb_source *source,
121 - const struct object_id *oid,
122 - struct object_info *oi,
123 - enum object_info_flags flags,
124 - struct strbuf *errmsg);
141 + int (*read_object_info)(struct odb_source *source,
142 + const struct object_id *oid,
143 + struct object_info *oi,
144 + enum object_info_flags flags);
125 145
126 146 /*
127 147 * This callback is expected to create a new read stream that can be
@@ -130,7 +150,7 @@ struct odb_source {
130 150 * The callback is expected to return a negative error code in case
131 151 * creating the object stream has failed, 0 otherwise.
132 152 */
133 - int (*read_object_stream)(struct odb_read_stream **out,
153 + int (*read_object_stream)(struct odb_stream **out,
134 154 struct odb_source *source,
135 155 const struct object_id *oid);
136 156
@@ -226,7 +246,7 @@ struct odb_source {
226 246 * otherwise.
227 247 */
228 248 int (*write_object_stream)(struct odb_source *source,
229 - struct odb_write_stream *stream, size_t len,
249 + struct odb_stream *stream,
230 250 struct object_id *oid);
231 251
232 252 /*
@@ -283,6 +303,34 @@ struct odb_source {
283 303 */
284 304 bool (*optimize_required)(struct odb_source *source,
285 305 const struct odb_optimize_options *opts);
306 +
307 + /*
308 + * This callback is expected to start generating a packfile with the
309 + * given options. The pack shall be generated asynchronously so that
310 + * the caller can consume the pack data and progress output while the
311 + * pack is being generated.
312 + *
313 + * This callback is optional. Sources that cannot generate packfiles
314 + * shall leave it unset.
315 + *
316 + * The callback is expected to return 0 on success and populate the
317 + * `out` pointer with the pack generator, a negative error code
318 + * otherwise.
319 + */
320 + int (*generate_pack)(struct odb_source *source,
321 + struct odb_pack_generator **out,
322 + const struct odb_generate_pack_options *opts);
323 +
324 + /*
325 + * This callback is expected to check the integrity of the object source
326 + * and report any errors found via the fsck options. The checks performed
327 + * are backend-specific.
328 + *
329 + * The callback is expected to return 0 on success, a negative error
330 + * code otherwise.
331 + */
332 + int (*fsck)(struct odb_source *source,
333 + struct odb_fsck_options *options);
286 334 };
287 335
288 336 /*
@@ -332,6 +380,17 @@ static inline void odb_source_close(struct odb_source *source)
332 380 source->close(source);
333 381 }
334 382
383 + /*
384 + * Create on-disk data structures that are required for this source to operate
385 + * correctly. Returns 0 on success, a negative error code otherwise.
386 + */
387 + static inline int odb_source_create_on_disk(struct odb_source *source)
388 + {
389 + if (!source->create_on_disk)
390 + return 0;
391 + return source->create_on_disk(source);
392 + }
393 +
335 394 /*
336 395 * Prepare the object database source and clear any caches. Depending on the
337 396 * backend used this may have the effect that concurrently-written objects
@@ -345,26 +404,21 @@ static inline void odb_source_prepare(struct odb_source *source,
345 404
346 405 /*
347 406 * Read an object from the object database source identified by its object ID.
348 - * Please refer to `enum odb_read_status` for the individual error codes.
349 - *
350 - * In case reading the object has failed with a generic error and `errmsg` is
351 - * non-NULL it will be populated with a human-readable message that describes
352 - * the failure.
407 + * Returns 0 on success, a negative error code otherwise.
353 408 */
354 - static inline enum odb_read_status odb_source_read_object_info(struct odb_source *source,
355 - const struct object_id *oid,
356 - struct object_info *oi,
357 - enum object_info_flags flags,
358 - struct strbuf *errmsg)
409 + static inline int odb_source_read_object_info(struct odb_source *source,
410 + const struct object_id *oid,
411 + struct object_info *oi,
412 + enum object_info_flags flags)
359 413 {
360 - return source->read_object_info(source, oid, oi, flags, errmsg);
414 + return source->read_object_info(source, oid, oi, flags);
361 415 }
362 416
363 417 /*
364 418 * Create a new read stream for the given object ID. Returns 0 on success, a
365 419 * negative error code otherwise.
366 420 */
367 - static inline int odb_source_read_object_stream(struct odb_read_stream **out,
421 + static inline int odb_source_read_object_stream(struct odb_stream **out,
368 422 struct odb_source *source,
369 423 const struct object_id *oid)
370 424 {
@@ -461,11 +515,10 @@ static inline int odb_source_write_object(struct odb_source *source,
461 515 * out pointer for the object ID.
462 516 */
463 517 static inline int odb_source_write_object_stream(struct odb_source *source,
464 - struct odb_write_stream *stream,
465 - size_t len,
518 + struct odb_stream *stream,
466 519 struct object_id *oid)
467 520 {
468 - return source->write_object_stream(source, stream, len, oid);
521 + return source->write_object_stream(source, stream, oid);
469 522 }
470 523
471 524 /*
@@ -530,4 +583,30 @@ static inline bool odb_source_optimize_required(struct odb_source *source,
530 583 return source->optimize_required(source, opts);
531 584 }
532 585
586 + /*
587 + * Start generating a packfile from the given source with the given options.
588 + * The pack is generated asynchronously; the caller is expected to consume the
589 + * file descriptors exposed via the pack generator and to then wait for
590 + * completion via `odb_pack_generator_finish()`.
591 + *
592 + * Returns 0 on success and populates the `out` pointer with the pack
593 + * generator, a negative error code otherwise.
594 + */
595 + static inline int odb_source_generate_pack(struct odb_source *source,
596 + struct odb_pack_generator **out,
597 + const struct odb_generate_pack_options *opts)
598 + {
599 + return source->generate_pack(source, out, opts);
600 + }
601 +
602 + /*
603 + * Check the integrity of the object database source. The checks performed
604 + * are backend-specific. Returns 0 on success, a negative error code otherwise.
605 + */
606 + static inline int odb_source_fsck(struct odb_source *source,
607 + struct odb_fsck_options *opts)
608 + {
609 + return source->fsck(source, opts);
610 + }
611 +
533 612 #endif
modified odb/streaming.c
@@ -20,8 +20,8 @@
20 20 *****************************************************************/
21 21
22 22 struct odb_filtered_read_stream {
23 - struct odb_read_stream base;
24 - struct odb_read_stream *upstream;
23 + struct odb_stream base;
24 + struct odb_stream *upstream;
25 25 struct stream_filter *filter;
26 26 char ibuf[FILTER_BUFFER];
27 27 char obuf[FILTER_BUFFER];
@@ -30,14 +30,14 @@ struct odb_filtered_read_stream {
30 30 int input_finished;
31 31 };
32 32
33 - static int close_istream_filtered(struct odb_read_stream *_fs)
33 + static int close_istream_filtered(struct odb_stream *_fs)
34 34 {
35 35 struct odb_filtered_read_stream *fs = (struct odb_filtered_read_stream *)_fs;
36 36 free_stream_filter(fs->filter);
37 - return odb_read_stream_close(fs->upstream);
37 + return odb_stream_close(fs->upstream);
38 38 }
39 39
40 - static ssize_t read_istream_filtered(struct odb_read_stream *_fs, char *buf,
40 + static ssize_t read_istream_filtered(struct odb_stream *_fs, char *buf,
41 41 size_t sz)
42 42 {
43 43 struct odb_filtered_read_stream *fs = (struct odb_filtered_read_stream *)_fs;
@@ -86,7 +86,7 @@ static ssize_t read_istream_filtered(struct odb_read_stream *_fs, char *buf,
86 86
87 87 /* refill the input from the upstream */
88 88 if (!fs->input_finished) {
89 - fs->i_end = odb_read_stream_read(fs->upstream, fs->ibuf, FILTER_BUFFER);
89 + fs->i_end = odb_stream_read(fs->upstream, fs->ibuf, FILTER_BUFFER);
90 90 if (fs->i_end < 0)
91 91 return -1;
92 92 if (fs->i_end)
@@ -97,8 +97,8 @@ static ssize_t read_istream_filtered(struct odb_read_stream *_fs, char *buf,
97 97 return filled;
98 98 }
99 99
100 - static struct odb_read_stream *attach_stream_filter(struct odb_read_stream *st,
101 - struct stream_filter *filter)
100 + static struct odb_stream *attach_stream_filter(struct odb_stream *st,
101 + struct stream_filter *filter)
102 102 {
103 103 struct odb_filtered_read_stream *fs;
104 104
@@ -120,19 +120,19 @@ static struct odb_read_stream *attach_stream_filter(struct odb_read_stream *st,
120 120 *****************************************************************/
121 121
122 122 struct odb_incore_read_stream {
123 - struct odb_read_stream base;
123 + struct odb_stream base;
124 124 char *buf; /* from odb_read_object_info_extended() */
125 125 unsigned long read_ptr;
126 126 };
127 127
128 - static int close_istream_incore(struct odb_read_stream *_st)
128 + static int close_istream_incore(struct odb_stream *_st)
129 129 {
130 130 struct odb_incore_read_stream *st = (struct odb_incore_read_stream *)_st;
131 131 free(st->buf);
132 132 return 0;
133 133 }
134 134
135 - static ssize_t read_istream_incore(struct odb_read_stream *_st, char *buf, size_t sz)
135 + static ssize_t read_istream_incore(struct odb_stream *_st, char *buf, size_t sz)
136 136 {
137 137 struct odb_incore_read_stream *st = (struct odb_incore_read_stream *)_st;
138 138 size_t read_size = sz;
@@ -147,7 +147,7 @@ static ssize_t read_istream_incore(struct odb_read_stream *_st, char *buf, size_
147 147 return read_size;
148 148 }
149 149
150 - static int open_istream_incore(struct odb_read_stream **out,
150 + static int open_istream_incore(struct odb_stream **out,
151 151 struct object_database *odb,
152 152 const struct object_id *oid)
153 153 {
@@ -178,13 +178,12 @@ static int open_istream_incore(struct odb_read_stream **out,
178 178 * static helpers variables and functions for users of streaming interface
179 179 *****************************************************************************/
180 180
181 - static int istream_source(struct odb_read_stream **out,
181 + static int istream_source(struct odb_stream **out,
182 182 struct object_database *odb,
183 183 const struct object_id *oid)
184 184 {
185 185 struct odb_source *source;
186 186
187 - odb_prepare_alternates(odb);
188 187 for (source = odb->sources; source; source = source->next)
189 188 if (!odb_source_read_object_stream(out, source, oid))
190 189 return 0;
@@ -196,23 +195,23 @@ static int istream_source(struct odb_read_stream **out,
196 195 * Users of streaming interface
197 196 ****************************************************************/
198 197
199 - int odb_read_stream_close(struct odb_read_stream *st)
198 + int odb_stream_close(struct odb_stream *st)
200 199 {
201 200 int r = st->close(st);
202 201 free(st);
203 202 return r;
204 203 }
205 204
206 - ssize_t odb_read_stream_read(struct odb_read_stream *st, void *buf, size_t sz)
205 + ssize_t odb_stream_read(struct odb_stream *st, void *buf, size_t sz)
207 206 {
208 207 return st->read(st, buf, sz);
209 208 }
210 209
211 - struct odb_read_stream *odb_read_stream_open(struct object_database *odb,
212 - const struct object_id *oid,
213 - struct stream_filter *filter)
210 + struct odb_stream *odb_stream_from_object(struct object_database *odb,
211 + const struct object_id *oid,
212 + struct stream_filter *filter)
214 213 {
215 - struct odb_read_stream *st;
214 + struct odb_stream *st;
216 215 const struct object_id *real = lookup_replace_object(odb->repo, oid);
217 216 int ret = istream_source(&st, odb, real);
218 217
@@ -221,9 +220,9 @@ struct odb_read_stream *odb_read_stream_open(struct object_database *odb,
221 220
222 221 if (filter) {
223 222 /* Add "&& !is_null_stream_filter(filter)" for performance */
224 - struct odb_read_stream *nst = attach_stream_filter(st, filter);
223 + struct odb_stream *nst = attach_stream_filter(st, filter);
225 224 if (!nst) {
226 - odb_read_stream_close(st);
225 + odb_stream_close(st);
227 226 return NULL;
228 227 }
229 228 st = nst;
@@ -232,27 +231,17 @@ struct odb_read_stream *odb_read_stream_open(struct object_database *odb,
232 231 return st;
233 232 }
234 233
235 - ssize_t odb_write_stream_read(struct odb_write_stream *st, void *buf, size_t sz)
236 - {
237 - return st->read(st, buf, sz);
238 - }
239 -
240 - void odb_write_stream_release(struct odb_write_stream *st)
241 - {
242 - free(st->data);
243 - }
244 -
245 234 int odb_stream_blob_to_fd(struct object_database *odb,
246 235 int fd,
247 236 const struct object_id *oid,
248 237 struct stream_filter *filter,
249 238 int can_seek)
250 239 {
251 - struct odb_read_stream *st;
240 + struct odb_stream *st;
252 241 ssize_t kept = 0;
253 242 int result = -1;
254 243
255 - st = odb_read_stream_open(odb, oid, filter);
244 + st = odb_stream_from_object(odb, oid, filter);
256 245 if (!st) {
257 246 if (filter)
258 247 free_stream_filter(filter);
@@ -263,7 +252,7 @@ int odb_stream_blob_to_fd(struct object_database *odb,
263 252 for (;;) {
264 253 char buf[1024 * 16];
265 254 ssize_t wrote, holeto;
266 - ssize_t readlen = odb_read_stream_read(st, buf, sizeof(buf));
255 + ssize_t readlen = odb_stream_read(st, buf, sizeof(buf));
267 256
268 257 if (readlen < 0)
269 258 goto close_and_exit;
@@ -294,47 +283,53 @@ int odb_stream_blob_to_fd(struct object_database *odb,
294 283 result = 0;
295 284
296 285 close_and_exit:
297 - odb_read_stream_close(st);
286 + odb_stream_close(st);
298 287 return result;
299 288 }
300 289
301 - struct read_object_fd_data {
290 + struct fd_stream {
291 + struct odb_stream base;
302 292 int fd;
303 293 size_t remaining;
304 294 };
305 295
306 - static ssize_t read_object_fd(struct odb_write_stream *stream,
307 - unsigned char *buf, size_t len)
296 + static ssize_t fd_stream_read(struct odb_stream *stream,
297 + char *buf, size_t len)
308 298 {
309 - struct read_object_fd_data *data = stream->data;
299 + struct fd_stream *fds = container_of(stream, struct fd_stream, base);
310 300 ssize_t read_result;
311 301 size_t count;
312 302
313 - if (stream->is_finished)
303 + if (!fds->remaining)
314 304 return 0;
315 305
316 - count = data->remaining < len ? data->remaining : len;
317 - read_result = read_in_full(data->fd, buf, count);
306 + count = fds->remaining < len ? fds->remaining : len;
307 + read_result = read_in_full(fds->fd, buf, count);
318 308 if (read_result < 0 || (size_t)read_result != count)
319 309 return -1;
320 310
321 - data->remaining -= count;
322 - if (!data->remaining)
323 - stream->is_finished = 1;
311 + fds->remaining -= count;
324 312
325 313 return read_result;
326 314 }
327 315
328 - void odb_write_stream_from_fd(struct odb_write_stream *stream, int fd,
329 - size_t size)
316 + static int fd_stream_close(struct odb_stream *stream UNUSED)
317 + {
318 + /* The file descriptor is owned by the caller for now. */
319 + return 0;
320 + }
321 +
322 + struct odb_stream *odb_stream_from_fd(int fd, size_t size, enum object_type type)
330 323 {
331 - struct read_object_fd_data *data;
324 + struct fd_stream *fds;
332 325
333 - CALLOC_ARRAY(data, 1);
334 - data->fd = fd;
335 - data->remaining = size;
326 + CALLOC_ARRAY(fds, 1);
327 + fds->base.read = fd_stream_read;
328 + fds->base.close = fd_stream_close;
329 + fds->base.size = size;
330 + fds->base.type = type;
331 + fds->fd = fd;
332 + fds->remaining = size;
336 333
337 - stream->data = data;
338 - stream->read = read_object_fd;
339 - stream->is_finished = 0;
334 + return &fds->base;
340 335 }
modified odb/streaming.h
@@ -8,68 +8,53 @@
8 8 #include "odb.h"
9 9
10 10 struct object_database;
11 - struct odb_read_stream;
11 + struct odb_stream;
12 12 struct stream_filter;
13 13
14 - typedef int (*odb_read_stream_close_fn)(struct odb_read_stream *);
15 - typedef ssize_t (*odb_read_stream_read_fn)(struct odb_read_stream *, char *, size_t);
14 + typedef int (*odb_stream_close_fn)(struct odb_stream *);
15 + typedef ssize_t (*odb_stream_read_fn)(struct odb_stream *, char *, size_t);
16 16
17 17 /*
18 - * A stream that can be used to read an object from the object database without
19 - * loading all of it into memory.
18 + * A stream that can be used to read an object from or write an object into the
19 + * object database without loading all of it into memory.
20 20 */
21 - struct odb_read_stream {
22 - odb_read_stream_close_fn close;
23 - odb_read_stream_read_fn read;
21 + struct odb_stream {
22 + odb_stream_close_fn close;
23 + odb_stream_read_fn read;
24 24 enum object_type type;
25 25 size_t size; /* inflated size of full object */
26 26 };
27 27
28 28 /*
29 - * Create a new object stream for the given object database. An optional filter
30 - * can be used to transform the object's content.
29 + * Create a new object stream for the given object. An optional filter can be
30 + * used to transform the object's content.
31 31 *
32 32 * Returns the stream on success, a `NULL` pointer otherwise.
33 33 */
34 - struct odb_read_stream *odb_read_stream_open(struct object_database *odb,
35 - const struct object_id *oid,
36 - struct stream_filter *filter);
34 + struct odb_stream *odb_stream_from_object(struct object_database *odb,
35 + const struct object_id *oid,
36 + struct stream_filter *filter);
37 37
38 38 /*
39 - * Close the given read stream and release all resources associated with it.
39 + * Create a new object stream for the given file descriptor. This can be used
40 + * to, for example, stream an object into the object database. This function
41 + * does _not_ take ownership of the file descriptor. It's the responsibility of
42 + * the caller to close it after the stream has been closed.
43 + */
44 + struct odb_stream *odb_stream_from_fd(int fd, size_t size, enum object_type type);
45 +
46 + /*
47 + * Close the given object stream and release all resources associated with it.
40 48 * Returns 0 on success, a negative error code otherwise.
41 49 */
42 - int odb_read_stream_close(struct odb_read_stream *stream);
50 + int odb_stream_close(struct odb_stream *stream);
43 51
44 52 /*
45 53 * Read data from the stream into the buffer. Returns 0 on EOF and the number
46 54 * of bytes read on success. Returns a negative error code in case reading from
47 55 * the stream fails.
48 56 */
49 - ssize_t odb_read_stream_read(struct odb_read_stream *stream, void *buf, size_t len);
50 -
51 - /*
52 - * A stream that provides an object to be written to the object database without
53 - * loading all of it into memory.
54 - */
55 - struct odb_write_stream {
56 - ssize_t (*read)(struct odb_write_stream *, unsigned char *, size_t);
57 - void *data;
58 - int is_finished;
59 - };
60 -
61 - /*
62 - * Read data from the stream into the buffer. Returns 0 when finished and the
63 - * number of bytes read on success. Returns a negative error code in case
64 - * reading from the stream fails.
65 - */
66 - ssize_t odb_write_stream_read(struct odb_write_stream *stream, void *buf,
67 - size_t len);
68 -
69 - /*
70 - * Releases memory allocated for underlying stream data.
71 - */
72 - void odb_write_stream_release(struct odb_write_stream *stream);
57 + ssize_t odb_stream_read(struct odb_stream *stream, void *buf, size_t len);
73 58
74 59 /*
75 60 * Look up the object by its ID and write the full contents to the file
@@ -88,10 +73,4 @@ int odb_stream_blob_to_fd(struct object_database *odb,
88 73 struct stream_filter *filter,
89 74 int can_seek);
90 75
91 - /*
92 - * Sets up an ODB write stream that reads from an fd.
93 - */
94 - void odb_write_stream_from_fd(struct odb_write_stream *stream, int fd,
95 - size_t size);
96 -
97 76 #endif /* STREAMING_H */
modified odb/transaction.c
@@ -39,10 +39,10 @@ int odb_transaction_commit(struct odb_transaction *transaction)
39 39 }
40 40
41 41 int odb_transaction_write_object_stream(struct odb_transaction *transaction,
42 - struct odb_write_stream *stream,
43 - size_t len, struct object_id *oid)
42 + struct odb_stream *stream,
43 + struct object_id *oid)
44 44 {
45 - return transaction->write_object_stream(transaction, stream, len, oid);
45 + return transaction->write_object_stream(transaction, stream, oid);
46 46 }
47 47
48 48 int odb_transaction_env(struct odb_transaction *transaction, struct strvec *env)
modified odb/transaction.h
@@ -24,14 +24,14 @@ struct odb_transaction {
24 24
25 25 /*
26 26 * This callback is expected to write the given object stream into
27 - * the ODB transaction. Note that for now, only blobs support streaming.
27 + * the ODB transaction.
28 28 *
29 29 * The resulting object ID shall be written into the out pointer. The
30 30 * callback is expected to return 0 on success, a negative error code
31 31 * otherwise.
32 32 */
33 33 int (*write_object_stream)(struct odb_transaction *transaction,
34 - struct odb_write_stream *stream, size_t len,
34 + struct odb_stream *stream,
35 35 struct object_id *oid);
36 36
37 37 /*
@@ -81,8 +81,8 @@ int odb_transaction_commit(struct odb_transaction *transaction);
81 81 * error code otherwise.
82 82 */
83 83 int odb_transaction_write_object_stream(struct odb_transaction *transaction,
84 - struct odb_write_stream *stream,
85 - size_t len, struct object_id *oid);
84 + struct odb_stream *stream,
85 + struct object_id *oid);
86 86
87 87 /*
88 88 * Populates the provided strvec with the environment variables that a child
modified oss-fuzz/fuzz-pack-headers.c
@@ -9,7 +9,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
9 9 size_t len;
10 10
11 11 unpack_object_header_buffer((const unsigned char *)data,
12 - (unsigned long)size, &type, &len);
12 + size, &type, &len);
13 13
14 14 return 0;
15 15 }
modified pack-bitmap.c
@@ -717,7 +717,6 @@ static int open_bitmap(struct repository *r,
717 717
718 718 assert(!bitmap_git->map);
719 719
720 - odb_prepare_alternates(r->objects);
721 720 for (source = r->objects->sources; source; source = source->next) {
722 721 struct odb_source_files *files = odb_source_files_downcast(source);
723 722
@@ -3417,7 +3416,6 @@ int verify_bitmap_files(struct repository *r)
3417 3416 struct packed_git *p;
3418 3417 int res = 0;
3419 3418
3420 - odb_prepare_alternates(r->objects);
3421 3419 for (source = r->objects->sources; source; source = source->next) {
3422 3420 struct odb_source_files *files = odb_source_files_downcast(source);
3423 3421 struct multi_pack_index *m = get_multi_pack_index(files->packed);
modified pack-check.c
@@ -34,7 +34,7 @@ int check_pack_crc(struct packed_git *p, struct pack_window **w_curs,
34 34 uint32_t data_crc = crc32(0, NULL, 0);
35 35
36 36 do {
37 - unsigned long avail;
37 + size_t avail;
38 38 void *data = use_pack(p, w_curs, offset, &avail);
39 39 if (avail > len)
40 40 avail = len;
@@ -71,7 +71,7 @@ static int verify_packfile(struct repository *r,
71 71
72 72 git_hash_init(&ctx, r->hash_algo);
73 73 do {
74 - unsigned long remaining;
74 + size_t remaining;
75 75 unsigned char *in = use_pack(p, w_curs, offset, &remaining);
76 76 offset += remaining;
77 77 if (!pack_sig_ofs)
@@ -106,7 +106,7 @@ static int verify_packfile(struct repository *r,
106 106 QSORT(entries, nr_objects, compare_entries);
107 107
108 108 for (i = 0; i < nr_objects; i++) {
109 - struct odb_read_stream *stream = NULL;
109 + struct odb_stream *stream = NULL;
110 110 void *data;
111 111 struct object_id oid;
112 112 enum object_type type;
@@ -171,7 +171,7 @@ static int verify_packfile(struct repository *r,
171 171 display_progress(progress, base_count + i);
172 172
173 173 if (stream)
174 - odb_read_stream_close(stream);
174 + odb_stream_close(stream);
175 175 free(data);
176 176 }
177 177
modified packfile.c
@@ -620,7 +620,7 @@ static int in_window(struct repository *r, struct pack_window *win,
620 620 unsigned char *use_pack(struct packed_git *p,
621 621 struct pack_window **w_cursor,
622 622 off_t offset,
623 - unsigned long *left)
623 + size_t *left)
624 624 {
625 625 struct pack_window *win = *w_cursor;
626 626
@@ -866,12 +866,11 @@ struct packfile_list_entry *packfile_store_get_packs(struct odb_source_packed *s
866 866 return store->packs.head;
867 867 }
868 868
869 - unsigned long unpack_object_header_buffer(const unsigned char *buf,
870 - unsigned long len, enum object_type *type, size_t *sizep)
869 + size_t unpack_object_header_buffer(const unsigned char *buf, size_t len,
870 + enum object_type *type, size_t *sizep)
871 871 {
872 872 unsigned shift;
873 - size_t size, c;
874 - unsigned long used = 0;
873 + size_t size, c, used = 0;
875 874
876 875 c = buf[used++];
877 876 *type = (c >> 4) & 7;
@@ -960,8 +959,7 @@ int unpack_object_header(struct packed_git *p,
960 959 size_t *sizep)
961 960 {
962 961 unsigned char *base;
963 - unsigned long left;
964 - unsigned long used;
962 + size_t left, used;
965 963 enum object_type type;
966 964
967 965 /* use_pack() assures us we have [base, base + 20) available
@@ -985,6 +983,23 @@ void mark_bad_packed_object(struct packed_git *p, const struct object_id *oid)
985 983 oidset_insert(&p->bad_objects, oid);
986 984 }
987 985
986 + const struct packed_git *has_packed_and_bad(struct repository *r,
987 + const struct object_id *oid)
988 + {
989 + struct odb_source *source;
990 +
991 + for (source = r->objects->sources; source; source = source->next) {
992 + struct odb_source_files *files = odb_source_files_downcast(source);
993 + struct packfile_list_entry *e;
994 +
995 + for (e = files->packed->packs.head; e; e = e->next)
996 + if (oidset_contains(&e->pack->bad_objects, oid))
997 + return e->pack;
998 + }
999 +
1000 + return NULL;
1001 + }
1002 +
988 1003 off_t get_delta_base(struct packed_git *p,
989 1004 struct pack_window **w_curs,
990 1005 off_t *curpos,
@@ -1842,17 +1857,13 @@ int is_pack_valid(struct packed_git *p)
1842 1857
1843 1858 int packfile_fill_entry(struct packed_git *p,
1844 1859 const struct object_id *oid,
1845 - struct pack_entry *e,
1846 - struct packed_git **bad_pack)
1860 + struct pack_entry *e)
1847 1861 {
1848 1862 off_t offset;
1849 1863
1850 1864 if (oidset_size(&p->bad_objects) &&
1851 - oidset_contains(&p->bad_objects, oid)) {
1852 - if (bad_pack && !*bad_pack)
1853 - *bad_pack = p;
1865 + oidset_contains(&p->bad_objects, oid))
1854 1866 return 0;
1855 - }
1856 1867
1857 1868 offset = find_pack_entry_one(oid, p);
1858 1869 if (!offset)
@@ -1925,10 +1936,9 @@ int has_object_pack(struct repository *r, const struct object_id *oid)
1925 1936 {
1926 1937 struct odb_source *source;
1927 1938
1928 - odb_prepare_alternates(r->objects);
1929 1939 for (source = r->objects->sources; source; source = source->next) {
1930 1940 struct odb_source_files *files = odb_source_files_downcast(source);
1931 - if (!odb_source_read_object_info(&files->packed->base, oid, NULL, 0, NULL))
1941 + if (!odb_source_read_object_info(&files->packed->base, oid, NULL, 0))
1932 1942 return 1;
1933 1943 }
1934 1944
@@ -1949,7 +1959,7 @@ int has_object_kept_pack(struct repository *r, const struct object_id *oid,
1949 1959
1950 1960 for (; *cache; cache++) {
1951 1961 struct packed_git *p = *cache;
1952 - if (packfile_fill_entry(p, oid, &e, NULL))
1962 + if (packfile_fill_entry(p, oid, &e))
1953 1963 return 1;
1954 1964 }
1955 1965 }
@@ -2102,7 +2112,7 @@ int parse_pack_header_option(const char *in, unsigned char *out, unsigned int *l
2102 2112 }
2103 2113
2104 2114 struct odb_packed_read_stream {
2105 - struct odb_read_stream base;
2115 + struct odb_stream base;
2106 2116 struct packed_git *pack;
2107 2117 git_zstream z;
2108 2118 enum {
@@ -2114,7 +2124,7 @@ struct odb_packed_read_stream {
2114 2124 off_t pos;
2115 2125 };
2116 2126
2117 - static ssize_t read_istream_pack_non_delta(struct odb_read_stream *_st, char *buf,
2127 + static ssize_t read_istream_pack_non_delta(struct odb_stream *_st, char *buf,
2118 2128 size_t sz)
2119 2129 {
2120 2130 struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st;
@@ -2174,7 +2184,7 @@ static ssize_t read_istream_pack_non_delta(struct odb_read_stream *_st, char *bu
2174 2184 return total_read;
2175 2185 }
2176 2186
2177 - static int close_istream_pack_non_delta(struct odb_read_stream *_st)
2187 + static int close_istream_pack_non_delta(struct odb_stream *_st)
2178 2188 {
2179 2189 struct odb_packed_read_stream *st = (struct odb_packed_read_stream *)_st;
2180 2190 if (st->z_state == ODB_PACKED_READ_STREAM_INUSE)
@@ -2182,7 +2192,7 @@ static int close_istream_pack_non_delta(struct odb_read_stream *_st)
2182 2192 return 0;
2183 2193 }
2184 2194
2185 - int packfile_read_object_stream(struct odb_read_stream **out,
2195 + int packfile_read_object_stream(struct odb_stream **out,
2186 2196 const struct object_id *oid,
2187 2197 struct packed_git *pack,
2188 2198 off_t offset)
modified packfile.h
@@ -12,7 +12,7 @@
12 12
13 13 /* in odb.h */
14 14 struct object_info;
15 - struct odb_read_stream;
15 + struct odb_stream;
16 16
17 17 struct packed_git {
18 18 struct pack_window *windows;
@@ -77,8 +77,6 @@ static inline struct repo_for_each_pack_data repo_for_eack_pack_data_init(struct
77 77 {
78 78 struct repo_for_each_pack_data data = { 0 };
79 79
80 - odb_prepare_alternates(repo->objects);
81 -
82 80 for (struct odb_source *source = repo->objects->sources; source; source = source->next) {
83 81 struct odb_source_files *files = odb_source_files_downcast(source);
84 82 struct packfile_list_entry *entry = packfile_store_get_packs(files->packed);
@@ -240,7 +238,8 @@ uint32_t get_pack_fanout(struct packed_git *p, uint32_t value);
240 238
241 239 struct object_database;
242 240
243 - unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t, unsigned long *);
241 + unsigned char *use_pack(struct packed_git *, struct pack_window **, off_t,
242 + size_t *);
244 243 void close_pack_windows(struct packed_git *);
245 244 void close_pack(struct packed_git *);
246 245 void unuse_pack(struct pack_window **);
@@ -294,20 +293,20 @@ off_t find_pack_entry_one(const struct object_id *oid, struct packed_git *);
294 293
295 294 int packfile_fill_entry(struct packed_git *p,
296 295 const struct object_id *oid,
297 - struct pack_entry *e,
298 - struct packed_git **bad_pack);
296 + struct pack_entry *e);
299 297
300 298 int is_pack_valid(struct packed_git *);
301 299 void *unpack_entry(struct repository *r, struct packed_git *, off_t,
302 300 enum object_type *, size_t *);
303 - unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, size_t *sizep);
301 + size_t unpack_object_header_buffer(const unsigned char *buf, size_t len,
302 + enum object_type *type, size_t *sizep);
304 303 size_t get_size_from_delta(struct packed_git *, struct pack_window **, off_t);
305 304 int unpack_object_header(struct packed_git *, struct pack_window **, off_t *, size_t *);
306 305 off_t get_delta_base(struct packed_git *p, struct pack_window **w_curs,
307 306 off_t *curpos, enum object_type type,
308 307 off_t delta_obj_offset);
309 308
310 - int packfile_read_object_stream(struct odb_read_stream **out,
309 + int packfile_read_object_stream(struct odb_stream **out,
311 310 const struct object_id *oid,
312 311 struct packed_git *pack,
313 312 off_t offset);
@@ -329,6 +328,7 @@ int packed_object_info_with_index_pos(struct odb_source_packed *source,
329 328 uint32_t *maybe_index_pos, struct object_info *oi);
330 329
331 330 void mark_bad_packed_object(struct packed_git *, const struct object_id *);
331 + const struct packed_git *has_packed_and_bad(struct repository *, const struct object_id *);
332 332
333 333 int has_object_pack(struct repository *r, const struct object_id *oid);
334 334 int has_object_kept_pack(struct repository *r, const struct object_id *oid,
modified parse-options.c
@@ -1414,6 +1414,8 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
1414 1414
1415 1415 if (opts->type == OPTION_SUBCOMMAND)
1416 1416 continue;
1417 + if (!full && (opts->flags & PARSE_OPT_HIDDEN))
1418 + continue;
1417 1419 if (opts->type == OPTION_GROUP) {
1418 1420 fputc('\n', outfile);
1419 1421 need_newline = 0;
@@ -1421,8 +1423,6 @@ static enum parse_opt_result usage_with_options_internal(struct parse_opt_ctx_t
1421 1423 fprintf(outfile, "%s\n", _(opts->help));
1422 1424 continue;
1423 1425 }
1424 - if (!full && (opts->flags & PARSE_OPT_HIDDEN))
1425 - continue;
1426 1426
1427 1427 if (need_newline) {
1428 1428 fputc('\n', outfile);
modified parse-options.h
@@ -237,6 +237,11 @@ struct option {
237 237 .type = OPTION_GROUP, \
238 238 .help = (h), \
239 239 }
240 + #define OPT_HIDDEN_GROUP(h) { \
241 + .type = OPTION_GROUP, \
242 + .help = (h), \
243 + .flags = PARSE_OPT_HIDDEN, \
244 + }
240 245 #define OPT_BIT(s, l, v, h, b) OPT_BIT_F(s, l, v, h, b, 0)
241 246 #define OPT_BITOP(s, l, v, h, set, clear) { \
242 247 .type = OPTION_BITOP, \
modified parse.c
@@ -134,6 +134,15 @@ int git_parse_ssize_t(const char *value, ssize_t *ret)
134 134 return 1;
135 135 }
136 136
137 + int git_parse_size_t(const char *value, size_t *ret)
138 + {
139 + uintmax_t tmp;
140 + if (!git_parse_unsigned(value, &tmp, maximum_signed_value_of_type(size_t)))
141 + return 0;
142 + *ret = tmp;
143 + return 1;
144 + }
145 +
137 146 int git_parse_double(const char *value, double *ret)
138 147 {
139 148 char *end;
modified parse.h
@@ -4,6 +4,7 @@
4 4 int git_parse_signed(const char *value, intmax_t *ret, intmax_t max);
5 5 int git_parse_unsigned(const char *value, uintmax_t *ret, uintmax_t max);
6 6 int git_parse_ssize_t(const char *, ssize_t *);
7 + int git_parse_size_t(const char *, size_t *);
7 8 int git_parse_ulong(const char *, unsigned long *);
8 9 int git_parse_uint(const char *value, unsigned int *ret);
9 10 int git_parse_int(const char *value, int *ret);
modified protocol-caps.c
@@ -11,7 +11,8 @@
11 11 #include "strbuf.h"
12 12
13 13 struct requested_info {
14 - unsigned size : 1;
14 + unsigned size:1;
15 + unsigned type:1;
15 16 };
16 17
17 18 /*
@@ -73,15 +74,20 @@ static void send_info(struct repository *r, struct packet_writer *writer,
73 74 if (info->size)
74 75 packet_writer_write(writer, "size");
75 76
77 + if (info->type)
78 + packet_writer_write(writer, "type");
79 +
76 80 for_each_string_list_item (item, oid_str_list) {
77 81 const char *oid_str = item->string;
82 + enum object_type object_type;
78 83 struct object_id oid;
79 84 size_t object_size;
80 85
81 86 if (get_oid_hex_algop(oid_str, &oid, r->hash_algo) < 0) {
82 87 packet_writer_error(
83 88 writer,
84 - "object-info: protocol error, expected to get oid, not '%s'",
89 + "object-info: protocol error, expected to get "
90 + "oid, not '%s'",
85 91 oid_str);
86 92 continue;
87 93 }
@@ -93,7 +99,8 @@ static void send_info(struct repository *r, struct packet_writer *writer,
93 99 * If an object is not recognized by the server append SP to
94 100 * the response.
95 101 */
96 - if (get_object_info(r->objects, &oid, &object_size) <= OBJ_NONE) {
102 + object_type = get_object_info(r->objects, &oid, &object_size);
103 + if (object_type <= OBJ_NONE) {
97 104 strbuf_addstr(&send_buffer, " ");
98 105 goto write;
99 106 }
@@ -103,6 +110,9 @@ static void send_info(struct repository *r, struct packet_writer *writer,
103 110 (uintmax_t)object_size);
104 111 }
105 112
113 + if (info->type)
114 + strbuf_addf(&send_buffer, " %s", type_name(object_type));
115 +
106 116 write:
107 117 packet_writer_write(writer, "%s", send_buffer.buf);
108 118 strbuf_reset(&send_buffer);
@@ -124,6 +134,11 @@ int cap_object_info(struct repository *r, struct packet_reader *request)
124 134 continue;
125 135 }
126 136
137 + if (!strcmp("type", request->line)) {
138 + info.type = 1;
139 + continue;
140 + }
141 +
127 142 if (parse_oid(request->line, &oid_str_list))
128 143 continue;
129 144
modified ref-filter.c
@@ -2744,6 +2744,72 @@ static int filter_exclude_match(struct ref_filter *filter, const char *refname)
2744 2744 return match_pattern(filter->exclude.v, refname, filter->ignore_case);
2745 2745 }
2746 2746
2747 + static const char *short_upstream_name(const char *full_ref)
2748 + {
2749 + const char *short_name = full_ref;
2750 +
2751 + if (!skip_prefix(short_name, "refs/heads/", &short_name))
2752 + skip_prefix(short_name, "refs/remotes/", &short_name);
2753 + return short_name;
2754 + }
2755 +
2756 + /*
2757 + * Match the configured upstream of a branch against the registered
2758 + * --forked patterns. Exact patterns are compared against the full
2759 + * upstream refname so they are unambiguous; glob patterns are matched
2760 + * against the abbreviated upstream so that a glob such as origin/...
2761 + * works as typed.
2762 + */
2763 + static int filter_forked_match(struct ref_filter *filter, const char *refname)
2764 + {
2765 + const char *short_name;
2766 + struct branch *branch;
2767 + const char *upstream;
2768 +
2769 + if (!skip_prefix(refname, "refs/heads/", &short_name))
2770 + return 0;
2771 + branch = branch_get(short_name);
2772 + if (!branch)
2773 + return 0;
2774 + upstream = branch_get_upstream(branch, NULL);
2775 + if (!upstream)
2776 + return 0;
2777 +
2778 + for (size_t i = 0; i < filter->forked.nr; i++) {
2779 + const char *pattern = filter->forked.v[i];
2780 + if (has_glob_specials(pattern)) {
2781 + if (!wildmatch(pattern, short_upstream_name(upstream),
2782 + WM_PATHNAME))
2783 + return 1;
2784 + } else if (!strcmp(pattern, upstream)) {
2785 + return 1;
2786 + }
2787 + }
2788 + return 0;
2789 + }
2790 +
2791 + int ref_filter_forked_add(struct ref_filter *filter, const char *arg)
2792 + {
2793 + struct object_id oid;
2794 + char *full_ref = NULL;
2795 +
2796 + if (has_glob_specials(arg)) {
2797 + strvec_push(&filter->forked, arg);
2798 + return 0;
2799 + }
2800 +
2801 + if (repo_dwim_ref(the_repository, arg, strlen(arg), &oid,
2802 + &full_ref, 0) == 1 &&
2803 + (starts_with(full_ref, "refs/heads/") ||
2804 + starts_with(full_ref, "refs/remotes/"))) {
2805 + strvec_push(&filter->forked, full_ref);
2806 + free(full_ref);
2807 + return 0;
2808 + }
2809 + free(full_ref);
2810 + return -1;
2811 + }
2812 +
2747 2813 /*
2748 2814 * We need to seek to the reference right after a given marker but excluding any
2749 2815 * matching references. So we seek to the lexicographically next reference.
@@ -2979,6 +3045,9 @@ static struct ref_array_item *apply_ref_filter(const struct reference *ref,
2979 3045 if (filter->points_at.nr && !match_points_at(&filter->points_at, ref->oid, ref->name))
2980 3046 return NULL;
2981 3047
3048 + if (filter->forked.nr && !filter_forked_match(filter, ref->name))
3049 + return NULL;
3050 +
2982 3051 /*
2983 3052 * A merge filter is applied on refs pointing to commits. Hence
2984 3053 * obtain the commit using the 'oid' available and discard all
@@ -3764,6 +3833,7 @@ void ref_filter_init(struct ref_filter *filter)
3764 3833 void ref_filter_clear(struct ref_filter *filter)
3765 3834 {
3766 3835 strvec_clear(&filter->exclude);
3836 + strvec_clear(&filter->forked);
3767 3837 oid_array_clear(&filter->points_at);
3768 3838 commit_list_free(filter->with_commit);
3769 3839 commit_list_free(filter->no_commit);
modified ref-filter.h
@@ -67,6 +67,7 @@ struct ref_filter {
67 67 const char **name_patterns;
68 68 const char *start_after;
69 69 struct strvec exclude;
70 + struct strvec forked;
70 71 struct oid_array points_at;
71 72 struct commit_list *with_commit;
72 73 struct commit_list *no_commit;
@@ -110,6 +111,7 @@ struct ref_format {
110 111 #define REF_FILTER_INIT { \
111 112 .points_at = OID_ARRAY_INIT, \
112 113 .exclude = STRVEC_INIT, \
114 + .forked = STRVEC_INIT, \
113 115 }
114 116 #define REF_FORMAT_INIT { \
115 117 .use_color = GIT_COLOR_UNKNOWN, \
@@ -172,6 +174,14 @@ void ref_sorting_release(struct ref_sorting *);
172 174 struct ref_sorting *ref_sorting_options(struct string_list *);
173 175 /* Function to parse --merged and --no-merged options */
174 176 int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
177 + /*
178 + * Register a --forked <branch> pattern on the filter. The argument is
179 + * either a ref, which is resolved to its full refname, or a shell-style
180 + * glob. Branches are kept only when their configured upstream matches
181 + * one of the registered patterns. Returns -1 if the argument is not a
182 + * valid ref or pattern.
183 + */
184 + int ref_filter_forked_add(struct ref_filter *filter, const char *arg);
175 185 /* Get the current HEAD's description */
176 186 char *get_head_description(void);
177 187 /* Set up translated strings in the output. */
modified reftable/block.c
@@ -87,7 +87,10 @@ int block_writer_init(struct block_writer *bw, uint8_t typ, uint8_t *block,
87 87 REFTABLE_CALLOC_ARRAY(bw->zstream, 1);
88 88 if (!bw->zstream)
89 89 return REFTABLE_OUT_OF_MEMORY_ERROR;
90 - deflateInit(bw->zstream, 9);
90 + if (deflateInit(bw->zstream, 9) != Z_OK) {
91 + REFTABLE_FREE_AND_NULL(bw->zstream);
92 + return REFTABLE_ZLIB_ERROR;
93 + }
91 94 }
92 95
93 96 return 0;
modified reftable/writer.c
@@ -150,6 +150,7 @@ int reftable_writer_new(struct reftable_writer **out,
150 150 {
151 151 struct reftable_write_options opts = {0};
152 152 struct reftable_writer *wp;
153 + int err;
153 154
154 155 if (_opts)
155 156 opts = *_opts;
@@ -177,7 +178,12 @@ int reftable_writer_new(struct reftable_writer **out,
177 178 wp->opts = opts;
178 179 wp->hash_id = hash_id;
179 180 wp->flush = flush_func;
180 - writer_reinit_block_writer(wp, REFTABLE_BLOCK_TYPE_REF);
181 + err = writer_reinit_block_writer(wp, REFTABLE_BLOCK_TYPE_REF);
182 + if (err < 0) {
183 + reftable_free(wp->block);
184 + reftable_free(wp);
185 + return err;
186 + }
181 187
182 188 *out = wp;
183 189
modified repository.c
@@ -201,8 +201,6 @@ void repo_set_compat_hash_algo(struct repository *repo MAYBE_UNUSED, uint32_t al
201 201 if (hash_algo_by_ptr(repo->hash_algo) == algo)
202 202 BUG("hash_algo and compat_hash_algo match");
203 203 repo->compat_hash_algo = algo ? &hash_algos[algo] : NULL;
204 - if (repo->compat_hash_algo)
205 - repo_read_loose_object_map(repo);
206 204 #else
207 205 if (algo)
208 206 die(_("compatibility hash algorithm support requires Rust"));
@@ -296,6 +294,7 @@ int repo_init(struct repository *repo,
296 294 warning("%s", err.buf);
297 295 goto error;
298 296 }
297 + repo->objects = odb_new(repo, 0);
299 298
300 299 if (worktree)
301 300 repo_set_worktree(repo, worktree);
modified send-pack.c
@@ -42,16 +42,17 @@ int option_parse_push_signed(const struct option *opt,
42 42 die("bad %s argument: %s", opt->long_name, arg);
43 43 }
44 44
45 - static void feed_object(struct repository *r,
46 - const struct object_id *oid, FILE *fh, int negative)
45 + static void append_negative_object(struct repository *r,
46 + struct oid_array *haves,
47 + const struct object_id *oid)
47 48 {
48 - if (negative && !odb_has_object(r->objects, oid, 0))
49 + /*
50 + * The remote end may have advertised objects that we do not have in
51 + * our object database. Skip those, as we cannot use them as boundary.
52 + */
53 + if (!odb_has_object(r->objects, oid, 0))
49 54 return;
50 -
51 - if (negative)
52 - putc('^', fh);
53 - fputs(oid_to_hex(oid), fh);
54 - putc('\n', fh);
55 + oid_array_append(haves, oid);
55 56 }
56 57
57 58 /*
@@ -62,92 +63,58 @@ static int pack_objects(struct repository *r,
62 63 struct oid_array *negotiated,
63 64 struct send_pack_args *args)
64 65 {
65 - /*
66 - * The child becomes pack-objects --revs; we feed
67 - * the revision parameters to it via its stdin and
68 - * let its stdout go back to the other end.
69 - */
70 - struct child_process po = CHILD_PROCESS_INIT;
71 - FILE *po_in;
66 + struct odb_generate_pack_options opts = ODB_GENERATE_PACK_OPTIONS_INIT;
67 + struct odb_pack_generator *generator;
72 68 int rc;
73 69
74 70 trace2_region_enter("send_pack", "pack_objects", r);
75 - strvec_push(&po.args, "pack-objects");
76 - strvec_push(&po.args, "--all-progress-implied");
77 - strvec_push(&po.args, "--revs");
78 - strvec_push(&po.args, "--stdout");
79 - if (args->use_thin_pack)
80 - strvec_push(&po.args, "--thin");
81 - if (args->use_ofs_delta)
82 - strvec_push(&po.args, "--delta-base-offset");
83 - if (args->quiet || !args->progress)
84 - strvec_push(&po.args, "-q");
71 +
72 + opts.thin = args->use_thin_pack;
73 + opts.ofs_delta = args->use_ofs_delta;
85 74 if (args->progress)
86 - strvec_push(&po.args, "--progress");
87 - if (is_repository_shallow(r))
88 - strvec_push(&po.args, "--shallow");
89 - if (args->disable_bitmaps)
90 - strvec_push(&po.args, "--no-use-bitmap-index");
91 - po.in = -1;
92 - po.out = args->stateless_rpc ? -1 : fd;
93 - po.git_cmd = 1;
94 - po.clean_on_exit = 1;
95 - if (start_command(&po))
96 - die_errno("git pack-objects failed");
75 + opts.progress = ODB_GENERATE_PACK_PROGRESS_VERBOSE;
76 + opts.shallow = is_repository_shallow(r);
77 + opts.disable_bitmaps = args->disable_bitmaps;
97 78
98 79 /*
99 - * We feed the pack-objects we just spawned with revision
100 - * parameters by writing to the pipe.
80 + * The pack is either written directly to the remote's descriptor, or,
81 + * in the case of a stateless RPC, read back from a pipe so that we
82 + * can wrap the pack data into pkt-lines.
101 83 */
102 - po_in = xfdopen(po.in, "w");
84 + opts.pack_fd = args->stateless_rpc ? -1 : fd;
85 +
103 86 for (size_t i = 0; i < advertised->nr; i++)
104 - feed_object(r, &advertised->oid[i], po_in, 1);
87 + append_negative_object(r, &opts.haves, &advertised->oid[i]);
105 88 for (size_t i = 0; i < negotiated->nr; i++)
106 - feed_object(r, &negotiated->oid[i], po_in, 1);
89 + append_negative_object(r, &opts.haves, &negotiated->oid[i]);
107 90
108 91 while (refs) {
109 92 if (!is_null_oid(&refs->old_oid))
110 - feed_object(r, &refs->old_oid, po_in, 1);
93 + append_negative_object(r, &opts.haves, &refs->old_oid);
111 94 if (!is_null_oid(&refs->new_oid))
112 - feed_object(r, &refs->new_oid, po_in, 0);
95 + oid_array_append(&opts.wants, &refs->new_oid);
113 96 refs = refs->next;
114 97 }
115 98
116 - fflush(po_in);
117 - if (ferror(po_in))
118 - die_errno("error writing to pack-objects");
119 - fclose(po_in);
99 + if (odb_generate_pack(r->objects, &generator, &opts))
100 + die("git pack-objects failed");
101 + odb_generate_pack_options_release(&opts);
120 102
121 103 if (args->stateless_rpc) {
122 104 char *buf = xmalloc(LARGE_PACKET_MAX);
123 105 while (1) {
124 - ssize_t n = xread(po.out, buf, LARGE_PACKET_MAX);
106 + ssize_t n = xread(generator->out, buf, LARGE_PACKET_MAX);
125 107 if (n <= 0)
126 108 break;
127 109 send_sideband(fd, -1, buf, n, LARGE_PACKET_MAX);
128 110 }
129 111 free(buf);
130 - close(po.out);
131 - po.out = -1;
112 + close(generator->out);
132 113 }
133 114
134 - rc = finish_command(&po);
135 - if (rc) {
136 - /*
137 - * For a normal non-zero exit, we assume pack-objects wrote
138 - * something useful to stderr. For death by signal, though,
139 - * we should mention it to the user. The exception is SIGPIPE
140 - * (141), because that's a normal occurrence if the remote end
141 - * hangs up (and we'll report that by trying to read the unpack
142 - * status).
143 - */
144 - if (rc > 128 && rc != 141)
145 - error("pack-objects died of signal %d", rc - 128);
146 - trace2_region_leave("send_pack", "pack_objects", r);
147 - return -1;
148 - }
115 + rc = odb_pack_generator_finish(generator);
149 116 trace2_region_leave("send_pack", "pack_objects", r);
150 - return 0;
117 + return rc;
151 118 }
152 119
153 120 static int receive_unpack_status(struct packet_reader *reader)
@@ -768,7 +735,7 @@ int send_pack(struct repository *r,
768 735 goto out;
769 736 }
770 737 if (!args->stateless_rpc)
771 - /* Closed by pack_objects() via start_command() */
738 + /* Consumed by the pack generator in pack_objects() */
772 739 fd[1] = -1;
773 740 }
774 741 if (args->stateless_rpc && cmds_sent)
modified sequencer.c
@@ -1127,6 +1127,7 @@ static int run_git_commit(const char *defmsg,
1127 1127 struct child_process cmd = CHILD_PROCESS_INIT;
1128 1128
1129 1129 cmd.git_cmd = 1;
1130 + cmd.odb_to_close = the_repository->objects;
1130 1131
1131 1132 if (is_rebase_i(opts) &&
1132 1133 ((opts->committer_date_is_author_date && !opts->ignore_date) ||
@@ -6277,7 +6278,6 @@ int sequencer_make_script(struct repository *r, struct strbuf *out,
6277 6278 revs.sort_order = REV_SORT_IN_GRAPH_ORDER;
6278 6279 revs.topo_order = 1;
6279 6280
6280 - revs.pretty_given = 1;
6281 6281 repo_config_get_string(the_repository, "rebase.instructionFormat", &format);
6282 6282 if (!format || !*format) {
6283 6283 free(format);
modified serve.c
@@ -46,6 +46,9 @@ static int promisor_remote_advertise(struct repository *r,
46 46 static void promisor_remote_receive(struct repository *r,
47 47 const char *remotes)
48 48 {
49 + if (!remotes)
50 + die("promisor-remote capability requires an argument");
51 +
49 52 mark_promisor_remotes_as_accepted(r, remotes);
50 53 }
51 54
@@ -97,9 +100,9 @@ static int object_info_advertise(struct repository *r, struct strbuf *value)
97 100 /* disabled by default */
98 101 advertise_object_info = 0;
99 102 }
100 - /* Currently only size is supported */
103 + /* Currently only size and type are supported */
101 104 if (value && advertise_object_info)
102 - strbuf_addstr(value, "size");
105 + strbuf_addstr(value, "size type");
103 106 return advertise_object_info;
104 107 }
105 108
modified setup.c
@@ -1765,8 +1765,6 @@ int apply_repository_format(struct repository *repo,
1765 1765 enum apply_repository_format_flags flags,
1766 1766 struct strbuf *err)
1767 1767 {
1768 - char *object_directory = NULL, *alternate_object_directories = NULL;
1769 -
1770 1768 if (verify_repository_format(format, err) < 0)
1771 1769 return -1;
1772 1770
@@ -1779,8 +1777,6 @@ int apply_repository_format(struct repository *repo,
1779 1777 if (flags & APPLY_REPOSITORY_FORMAT_HONOR_ENV) {
1780 1778 const char *shallow_file;
1781 1779
1782 - object_directory = xstrdup_or_null(getenv(DB_ENVIRONMENT));
1783 - alternate_object_directories = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));
1784 1780 shallow_file = getenv(GIT_SHALLOW_FILE_ENVIRONMENT);
1785 1781 if (shallow_file)
1786 1782 set_alternate_shallow_file(repo, shallow_file);
@@ -1788,8 +1784,6 @@ int apply_repository_format(struct repository *repo,
1788 1784
1789 1785 repo->bare_cfg = format->is_bare;
1790 1786 repo_set_hash_algo(repo, format->hash_algo);
1791 - repo->objects = odb_new(repo, object_directory,
1792 - alternate_object_directories);
1793 1787 repo_set_compat_hash_algo(repo, format->compat_hash_algo);
1794 1788 repo_set_ref_storage_format(repo,
1795 1789 format->ref_storage_format,
@@ -1805,8 +1799,6 @@ int apply_repository_format(struct repository *repo,
1805 1799 repo->repository_format_precious_objects =
1806 1800 format->precious_objects;
1807 1801
1808 - free(alternate_object_directories);
1809 - free(object_directory);
1810 1802 return 0;
1811 1803 }
1812 1804
@@ -1890,6 +1882,7 @@ const char *enter_repo(struct repository *repo, const char *path, unsigned flags
1890 1882 read_and_verify_repository_format(&fmt, ".", NULL);
1891 1883 if (apply_repository_format(repo, &fmt, APPLY_REPOSITORY_FORMAT_HONOR_ENV, &err) < 0)
1892 1884 die("%s", err.buf);
1885 + repo->objects = odb_new(repo, ODB_NEW_HONOR_ENV);
1893 1886 startup_info->have_repository = 1;
1894 1887
1895 1888 clear_repository_format(&fmt);
@@ -2092,6 +2085,7 @@ const char *setup_git_directory_gently(struct repository *repo, int *nongit_ok)
2092 2085 if (apply_repository_format(repo, &discovery.format,
2093 2086 APPLY_REPOSITORY_FORMAT_HONOR_ENV, &err) < 0)
2094 2087 die("%s", err.buf);
2088 + repo->objects = odb_new(repo, ODB_NEW_HONOR_ENV);
2095 2089
2096 2090 clear_repository_format(&discovery.format);
2097 2091 strbuf_release(&err);
@@ -2653,25 +2647,29 @@ static int create_default_files(struct repository *repo,
2653 2647 return reinit;
2654 2648 }
2655 2649
2656 - static void create_object_directory(struct repository *repo)
2650 + static void create_object_database(struct repository *repo)
2657 2651 {
2658 - struct strbuf path = STRBUF_INIT;
2659 - size_t baselen;
2660 -
2661 - strbuf_addstr(&path, repo_get_object_directory(repo));
2662 - baselen = path.len;
2663 -
2664 - safe_create_dir(repo, path.buf, 1);
2665 -
2666 - strbuf_setlen(&path, baselen);
2667 - strbuf_addstr(&path, "/pack");
2668 - safe_create_dir(repo, path.buf, 1);
2652 + /*
2653 + * Create the "objects" directory in the common directory. This is done
2654 + * so that the repository can be discovered regardless of the backend
2655 + * used.
2656 + *
2657 + * Note that we only do this in case the object directory wasn't
2658 + * overwritten via an environment variable. If it _is_ being overridden
2659 + * then we skip this step, as the repository won't be discoverable
2660 + * anyway without the environment variable.
2661 + */
2662 + if (!getenv(DB_ENVIRONMENT)) {
2663 + struct strbuf objects_dir = STRBUF_INIT;
2664 + repo_common_path_append(repo, &objects_dir, "objects");
2665 + safe_create_dir(repo, objects_dir.buf, 1);
2666 + strbuf_release(&objects_dir);
2667 + }
2669 2668
2670 - strbuf_setlen(&path, baselen);
2671 - strbuf_addstr(&path, "/info");
2672 - safe_create_dir(repo, path.buf, 1);
2669 + repo->objects = odb_new(repo, ODB_NEW_HONOR_ENV);
2673 2670
2674 - strbuf_release(&path);
2671 + if (odb_source_create_on_disk(repo->objects->sources) < 0)
2672 + die(_("failed creating object database"));
2675 2673 }
2676 2674
2677 2675 static void separate_git_dir(struct repository *repo,
@@ -2869,7 +2867,6 @@ int init_db(struct repository *repo,
2869 2867 repository_format_configure(&repo_fmt, hash, ref_storage_format);
2870 2868 if (apply_repository_format(repo, &repo_fmt, APPLY_REPOSITORY_FORMAT_HONOR_ENV, &err) < 0)
2871 2869 die("%s", err.buf);
2872 - startup_info->have_repository = 1;
2873 2870
2874 2871 /*
2875 2872 * Ensure `core.hidedotfiles` is processed. This must happen after we
@@ -2883,10 +2880,6 @@ int init_db(struct repository *repo,
2883 2880 reinit = create_default_files(repo, template_dir, original_git_dir,
2884 2881 &repo_fmt, init_shared_repository);
2885 2882
2886 - if (!(flags & INIT_DB_SKIP_REFDB))
2887 - create_reference_database(repo, initial_branch, flags & INIT_DB_QUIET);
2888 - create_object_directory(repo);
2889 -
2890 2883 if (repo_settings_get_shared_repository(repo)) {
2891 2884 char buf[10];
2892 2885 /* We do not spell "group" and such, so that
@@ -2908,6 +2901,12 @@ int init_db(struct repository *repo,
2908 2901 repo_config_set(repo, "receive.denyNonFastforwards", "true");
2909 2902 }
2910 2903
2904 + if (!(flags & INIT_DB_SKIP_REFDB))
2905 + create_reference_database(repo, initial_branch, flags & INIT_DB_QUIET);
2906 + create_object_database(repo);
2907 +
2908 + startup_info->have_repository = 1;
2909 +
2911 2910 if (!(flags & INIT_DB_QUIET)) {
2912 2911 int len = strlen(git_dir);
2913 2912
modified setup.h
@@ -245,8 +245,8 @@ enum apply_repository_format_flags {
245 245
246 246 /*
247 247 * Apply the given repository format to the repo. This initializes extensions
248 - * and basic data structures required for normal operation. Returns 0 on
249 - * success, a negative error code when the format is not valid as determined by
248 + * required for normal operation. Returns 0 on success, a negative error code
249 + * when the format is not valid as determined by
250 250 * `verify_repository_format()`.
251 251 */
252 252 int apply_repository_format(struct repository *repo,
modified t/helper/test-delta.c
@@ -32,7 +32,7 @@ int cmd__delta(int argc, const char **argv)
32 32 die_errno("unable to read '%s'", argv[3]);
33 33
34 34 if (argv[1][1] == 'd') {
35 - unsigned long delta_size;
35 + size_t delta_size;
36 36 out_buf = diff_delta(from.buf, from.len,
37 37 data.buf, data.len,
38 38 &delta_size, 0);
modified t/helper/test-pack-deltas.c
@@ -22,7 +22,7 @@ static unsigned long do_compress(void **pptr, unsigned long size)
22 22 {
23 23 git_zstream stream;
24 24 void *in, *out;
25 - unsigned long maxsize;
25 + size_t maxsize;
26 26
27 27 git_deflate_init(&stream, 1);
28 28 maxsize = git_deflate_bound(&stream, size);
@@ -49,7 +49,7 @@ static void write_ref_delta(struct hashfile *f,
49 49 {
50 50 unsigned char header[MAX_PACK_OBJECT_HEADER];
51 51 unsigned long delta_size, compressed_size, hdrlen;
52 - size_t size, base_size;
52 + size_t size, base_size, delta_size_st = 0;
53 53 enum object_type type;
54 54 void *base_buf, *delta_buf;
55 55 void *buf = odb_read_object(the_repository->objects,
@@ -65,7 +65,8 @@ static void write_ref_delta(struct hashfile *f,
65 65 die("unable to read %s", oid_to_hex(base));
66 66
67 67 delta_buf = diff_delta(base_buf, base_size,
68 - buf, size, &delta_size, 0);
68 + buf, size, &delta_size_st, 0);
69 + delta_size = cast_size_t_to_ulong(delta_size_st);
69 70
70 71 compressed_size = do_compress(&delta_buf, delta_size);
71 72
modified t/helper/test-parse-options.c
@@ -209,6 +209,10 @@ int cmd__parse_options(int argc, const char **argv)
209 209 OPT_GROUP("Alias"),
210 210 OPT_STRING('A', "alias-source", &string, "string", "get a string"),
211 211 OPT_ALIAS('Z', "alias-target", "alias-source"),
212 + OPT_HIDDEN_GROUP("Hidden options"),
213 + OPT_HIDDEN_BOOL(0, "hidden-bool", &boolean, "get a boolean"),
214 + OPT_INTEGER_F('k', "hidden-integer", &integer, "get a integer",
215 + PARSE_OPT_HIDDEN),
212 216 OPT_END(),
213 217 };
214 218 int ret = 0;
modified t/helper/test-read-midx.c
@@ -82,7 +82,7 @@ static int read_midx_file(const char *object_dir, const char *checksum,
82 82 for (i = 0; i < m->num_objects; i++) {
83 83 nth_midxed_object_oid(&oid, m,
84 84 i + m->num_objects_in_base);
85 - fill_midx_entry(m, &oid, &e, NULL);
85 + fill_midx_entry(m, &oid, &e);
86 86
87 87 printf("%s %"PRIu64"\t%s\n",
88 88 oid_to_hex(&oid), e.offset, e.p->pack_name);
modified t/meson.build
@@ -796,6 +796,7 @@ integration_tests = [
796 796 't6041-bisect-submodule.sh',
797 797 't6050-replace.sh',
798 798 't6060-merge-index.sh',
799 + 't6099-merge-base-side-exhaustion.sh',
799 800 't6100-rev-list-in-order.sh',
800 801 't6101-rev-parse-parents.sh',
801 802 't6102-rev-list-unexpected-objects.sh',
modified t/t0040-parse-options.sh
@@ -7,7 +7,7 @@ test_description='our own option parser'
7 7
8 8 . ./test-lib.sh
9 9
10 - cat >expect <<\EOF
10 + cat >expect-part1 <<\EOF
11 11 usage: test-tool parse-options <options>
12 12
13 13 A helper function for the parse-options API.
@@ -41,6 +41,9 @@ String options
41 41 --[no-]string2 <str> get another string
42 42 --[no-]st <st> get another string (pervert ordering)
43 43 -o <str> get another string
44 + EOF
45 +
46 + cat >expect-part2 <<\EOF
44 47 --longhelp help text of this entry
45 48 spans multiple lines
46 49 --[no-]list <str> add str to list
@@ -67,12 +70,32 @@ Alias
67 70
68 71 EOF
69 72
73 + cat >expect-noop <<\EOF
74 + --[no-]obsolete no-op (backward compatibility)
75 + EOF
76 +
77 + cat >expect-hidden <<\EOF
78 + Hidden options
79 + --[no-]hidden-bool get a boolean
80 + -k, --[no-]hidden-integer <n>
81 + get a integer
82 +
83 + EOF
84 +
70 85 test_expect_success 'test help' '
86 + cat expect-part1 expect-part2 >expect &&
71 87 test-tool parse-options -h >output 2>output.err &&
72 88 test_must_be_empty output.err &&
73 89 test_cmp expect output
74 90 '
75 91
92 + test_expect_success 'test --help-all shows hidden group and options' '
93 + cat expect-part1 expect-noop expect-part2 expect-hidden >expect-help-all &&
94 + test-tool parse-options --help-all >output 2>output.err &&
95 + test_must_be_empty output.err &&
96 + test_cmp expect-help-all output
97 + '
98 +
76 99 mv expect expect.err
77 100
78 101 check () {
modified t/t0041-usage.sh
@@ -107,4 +107,11 @@ test_expect_success 'for-each-ref usage error' '
107 107 test_grep "usage" actual.err
108 108 '
109 109
110 + test_expect_success 'git --shallow-file without a value' '
111 + test_must_fail git --shallow-file >actual 2>actual.err &&
112 + test_line_count = 0 actual &&
113 + test_grep "no file given for " actual.err &&
114 + test_grep "usage" actual.err
115 + '
116 +
110 117 test_done
modified t/t0450/adoc-help-mismatches
@@ -12,7 +12,6 @@ column
12 12 credential
13 13 credential-cache
14 14 credential-store
15 - fast-import
16 15 fetch-pack
17 16 fmt-merge-msg
18 17 format-patch
modified t/t1016-compatObjectFormat.sh
@@ -187,6 +187,24 @@ do
187 187 eval signedtag3_${hash}_oid=$(git hash-object -t tag -w ../${hash}_signedtag3) &&
188 188 eval signedtag4_${hash}_oid=$(git hash-object -t tag -w ../${hash}_signedtag4)
189 189 '
190 +
191 + test_expect_success 'rev-parse maps oid of object borrowed from alternate' '
192 + for repo in alt borrow
193 + do
194 + test_when_finished "rm -rf $repo" &&
195 + git init --object-format=$hash $repo &&
196 + git -C $repo config set core.repositoryformatversion 1 &&
197 + git -C $repo config set extensions.compatObjectFormat $(compat_hash $hash) || exit 1
198 + done &&
199 +
200 + git -C alt commit --allow-empty --message A &&
201 + echo "$(pwd)/alt/.git/objects" >borrow/.git/objects/info/alternates &&
202 +
203 + oid=$(git -C alt rev-parse HEAD) &&
204 + git -C alt rev-parse --output-object-format=$(compat_hash $hash) "$oid" >expect &&
205 + git -C borrow rev-parse --output-object-format=$(compat_hash $hash) "$oid" >actual &&
206 + test_cmp expect actual
207 + '
190 208 done
191 209 cd "$base"
192 210
modified t/t1017-cat-file-remote-object-info.sh
@@ -7,6 +7,7 @@ test_description='git cat-file --batch-command with remote-object-info command'
7 7
8 8 hello_content="Hello World"
9 9 hello_size=$(strlen "$hello_content")
10 + hello_type="blob"
10 11 hello_oid=$(echo_without_newline "$hello_content" | git hash-object --stdin)
11 12 hello_short_oid=$(git rev-parse --short "$hello_oid")
12 13
@@ -19,6 +20,7 @@ unstored_oid=$(echo_without_newline "$unstored_content" | git hash-object --stdi
19 20 # file name is hello, which is 5 characters
20 21 # a space is 1 character and a null is 1 character
21 22 tree_size=$(($(test_oid rawsz) + 13))
23 + tree_type="tree"
22 24
23 25 commit_message="Initial commit"
24 26
@@ -31,6 +33,7 @@ commit_message="Initial commit"
31 33 # An easier way to calculate is: 1. use `git cat-file commit <commit hash> | wc -c`,
32 34 # to get 177, 2. then deduct 40 hex characters to get 137
33 35 commit_size=$(($(test_oid hexsz) + 137))
36 + commit_type="commit"
34 37
35 38 tag_header_without_oid="type blob
36 39 tag hellotag
@@ -44,6 +47,7 @@ $tag_description"
44 47
45 48 tag_oid=$(echo_without_newline "$tag_content" | git hash-object -t tag --stdin -w)
46 49 tag_size=$(strlen "$tag_content")
50 + tag_type="tag"
47 51
48 52 set_transport_variables () {
49 53 hello_oid=$(echo_without_newline "$hello_content" | git hash-object --stdin)
@@ -135,10 +139,10 @@ test_expect_success 'batch-command remote-object-info git:// default filter' '
135 139 set_transport_variables "$daemon_parent" &&
136 140 cd "$daemon_parent/daemon_client_empty" &&
137 141
138 - echo "$hello_oid $hello_size" >expect &&
139 - echo "$tree_oid $tree_size" >>expect &&
140 - echo "$commit_oid $commit_size" >>expect &&
141 - echo "$tag_oid $tag_size" >>expect &&
142 + echo "$hello_oid $hello_type $hello_size" >expect &&
143 + echo "$tree_oid $tree_type $tree_size" >>expect &&
144 + echo "$commit_oid $commit_type $commit_size" >>expect &&
145 + echo "$tag_oid $tag_type $tag_size" >>expect &&
142 146
143 147 git cat-file --batch-command >actual <<-EOF &&
144 148 remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid $tree_oid
@@ -148,7 +152,7 @@ test_expect_success 'batch-command remote-object-info git:// default filter' '
148 152 )
149 153 '
150 154
151 - test_expect_success 'remote-object-info does not change the default format of info' '
155 + test_expect_success 'remote-object-info and info can be mixed using the unified default format' '
152 156 (
153 157 set_transport_variables "$daemon_parent" &&
154 158 cd "$daemon_parent/daemon_client_empty" &&
@@ -158,7 +162,7 @@ test_expect_success 'remote-object-info does not change the default format of in
158 162 local_size=$(strlen "$local_content") &&
159 163
160 164 echo "$local_oid blob $local_size" >expect &&
161 - echo "$hello_oid $hello_size" >>expect &&
165 + echo "$hello_oid blob $hello_size" >>expect &&
162 166 echo "$local_oid blob $local_size" >>expect &&
163 167
164 168 git cat-file --batch-command >actual <<-EOF &&
@@ -205,10 +209,10 @@ test_expect_success 'batch-command -Z remote-object-info git:// default filter'
205 209 set_transport_variables "$daemon_parent" &&
206 210 cd "$daemon_parent/daemon_client_empty" &&
207 211
208 - printf "%s\0" "$hello_oid $hello_size" >expect &&
209 - printf "%s\0" "$tree_oid $tree_size" >>expect &&
210 - printf "%s\0" "$commit_oid $commit_size" >>expect &&
211 - printf "%s\0" "$tag_oid $tag_size" >>expect &&
212 + printf "%s\0" "$hello_oid $hello_type $hello_size" >expect &&
213 + printf "%s\0" "$tree_oid $tree_type $tree_size" >>expect &&
214 + printf "%s\0" "$commit_oid $commit_type $commit_size" >>expect &&
215 + printf "%s\0" "$tag_oid $tag_type $tag_size" >>expect &&
212 216
213 217 printf "%s\0" "$hello_oid missing" >>expect &&
214 218 printf "%s\0" "$tree_oid missing" >>expect &&
@@ -256,14 +260,12 @@ test_expect_success 'remote-object-info does not die on missing oid like info' '
256 260 )
257 261 '
258 262
259 - # This tests depends on %(objecttype) not being supported yet, once supported
260 - # it needs to be updated.
261 - test_expect_success 'unsupported placeholder on remote returns empty string' '
263 + test_expect_success 'objecttype is supported by remote-object-info' '
262 264 (
263 265 set_transport_variables "$daemon_parent" &&
264 266 cd "$daemon_parent/daemon_client_empty" &&
265 267
266 - echo "" >expect &&
268 + echo "$hello_type" >expect &&
267 269 git cat-file --batch-command="%(objecttype)" >actual <<-EOF &&
268 270 remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid
269 271 EOF
@@ -271,6 +273,22 @@ test_expect_success 'unsupported placeholder on remote returns empty string' '
271 273 )
272 274 '
273 275
276 + test_expect_success 'unsupported placeholders on remote return empty string' '
277 + (
278 + set_transport_variables "$daemon_parent" &&
279 + cd "$daemon_parent/daemon_client_empty" &&
280 +
281 + fmt="%(objectmode) %(objectsize:disk) %(rest) %(deltabase)" &&
282 +
283 + # The hardcoded SPs between the atoms are respected.
284 + echo " " >expect &&
285 + git cat-file --batch-command="$fmt" >actual <<-EOF &&
286 + remote-object-info "$GIT_DAEMON_URL/parent" $hello_oid
287 + EOF
288 + test_cmp expect actual
289 + )
290 + '
291 +
274 292 test_expect_success 'requesting only objectname echoes back' '
275 293 (
276 294 set_transport_variables "$daemon_parent" &&
@@ -430,10 +448,10 @@ test_expect_success 'batch-command remote-object-info file:// default filter' '
430 448 server_path="$(pwd)/server" &&
431 449 cd file_client_empty &&
432 450
433 - echo "$hello_oid $hello_size" >expect &&
434 - echo "$tree_oid $tree_size" >>expect &&
435 - echo "$commit_oid $commit_size" >>expect &&
436 - echo "$tag_oid $tag_size" >>expect &&
451 + echo "$hello_oid $hello_type $hello_size" >expect &&
452 + echo "$tree_oid $tree_type $tree_size" >>expect &&
453 + echo "$commit_oid $commit_type $commit_size" >>expect &&
454 + echo "$tag_oid $tag_type $tag_size" >>expect &&
437 455
438 456 git cat-file --batch-command >actual <<-EOF &&
439 457 remote-object-info "file://${server_path}" $hello_oid $tree_oid
@@ -449,10 +467,10 @@ test_expect_success 'batch-command -Z remote-object-info file:// default filter'
449 467 server_path="$(pwd)/server" &&
450 468 cd file_client_empty &&
451 469
452 - printf "%s\0" "$hello_oid $hello_size" >expect &&
453 - printf "%s\0" "$tree_oid $tree_size" >>expect &&
454 - printf "%s\0" "$commit_oid $commit_size" >>expect &&
455 - printf "%s\0" "$tag_oid $tag_size" >>expect &&
470 + printf "%s\0" "$hello_oid $hello_type $hello_size" >expect &&
471 + printf "%s\0" "$tree_oid $tree_type $tree_size" >>expect &&
472 + printf "%s\0" "$commit_oid $commit_type $commit_size" >>expect &&
473 + printf "%s\0" "$tag_oid $tag_type $tag_size" >>expect &&
456 474
457 475 printf "%s\0" "$hello_oid missing" >>expect &&
458 476 printf "%s\0" "$tree_oid missing" >>expect &&
@@ -600,10 +618,10 @@ test_expect_success 'batch-command remote-object-info http:// default filter' '
600 618 set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
601 619 cd "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" &&
602 620
603 - echo "$hello_oid $hello_size" >expect &&
604 - echo "$tree_oid $tree_size" >>expect &&
605 - echo "$commit_oid $commit_size" >>expect &&
606 - echo "$tag_oid $tag_size" >>expect &&
621 + echo "$hello_oid $hello_type $hello_size" >expect &&
622 + echo "$tree_oid $tree_type $tree_size" >>expect &&
623 + echo "$commit_oid $commit_type $commit_size" >>expect &&
624 + echo "$tag_oid $tag_type $tag_size" >>expect &&
607 625
608 626 git cat-file --batch-command >actual <<-EOF &&
609 627 remote-object-info "$HTTPD_URL/smart/http_parent" $hello_oid $tree_oid
@@ -618,10 +636,10 @@ test_expect_success 'batch-command -Z remote-object-info http:// default filter'
618 636 set_transport_variables "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
619 637 cd "$HTTPD_DOCUMENT_ROOT_PATH/http_client_empty" &&
620 638
621 - printf "%s\0" "$hello_oid $hello_size" >expect &&
622 - printf "%s\0" "$tree_oid $tree_size" >>expect &&
623 - printf "%s\0" "$commit_oid $commit_size" >>expect &&
624 - printf "%s\0" "$tag_oid $tag_size" >>expect &&
639 + printf "%s\0" "$hello_oid $hello_type $hello_size" >expect &&
640 + printf "%s\0" "$tree_oid $tree_type $tree_size" >>expect &&
641 + printf "%s\0" "$commit_oid $commit_type $commit_size" >>expect &&
642 + printf "%s\0" "$tag_oid $tag_type $tag_size" >>expect &&
625 643
626 644 batch_input="remote-object-info $HTTPD_URL/smart/http_parent $hello_oid $tree_oid
627 645 remote-object-info $HTTPD_URL/smart/http_parent $commit_oid $tag_oid
modified t/t1060-object-corruption.sh
@@ -145,22 +145,4 @@ test_expect_success 'partial clone of corrupted repository' '
145 145 test_must_fail git -C corrupt-partial checkout --force
146 146 '
147 147
148 - test_expect_success 'corrupted loose commit can be read from alternate' '
149 - git init repo-a &&
150 - tree=$(git -C repo-a write-tree) &&
151 - commit=$(git -C repo-a commit-tree $tree </dev/null) &&
152 -
153 - cp -r repo-a repo-b &&
154 - (
155 - cd repo-b &&
156 - echo ../../../repo-a/.git/objects >.git/objects/info/alternates &&
157 - corrupt_byte "$commit" 1
158 - ) &&
159 -
160 - git -C repo-a cat-file -p "$commit" >expect &&
161 - git -C repo-b cat-file -p "$commit" >actual 2>err &&
162 - test_cmp expect actual &&
163 - test_grep "inflate: data stream error" err
164 - '
165 -
166 148 test_done
modified t/t1450-fsck.sh
@@ -844,6 +844,11 @@ test_expect_success 'alternate objects are correctly blamed' '
844 844 echo "../../alt.git/objects" >.git/objects/info/alternates &&
845 845 mkdir alt.git/objects/$(dirname $path) &&
846 846 >alt.git/objects/$(dirname $path)/$(basename $path) &&
847 +
848 + # Without "--full", only the local object source is checked.
849 + git fsck --no-full >out 2>&1 &&
850 + test_must_be_empty out &&
851 +
847 852 test_must_fail git fsck >out 2>&1 &&
848 853 test_grep alt.git out
849 854 '
modified t/t3200-branch.sh
@@ -1757,4 +1757,425 @@ test_expect_success 'errors if given a bad branch name' '
1757 1757 test_cmp expect actual
1758 1758 '
1759 1759
1760 + test_expect_success '--forked: setup' '
1761 + test_create_repo forked-upstream &&
1762 + (
1763 + cd forked-upstream &&
1764 + test_commit base &&
1765 + git branch one base &&
1766 + git branch two base
1767 + ) &&
1768 +
1769 + test_create_repo forked-other &&
1770 + (
1771 + cd forked-other &&
1772 + test_commit other-base &&
1773 + git branch foreign other-base
1774 + ) &&
1775 +
1776 + git clone forked-upstream forked &&
1777 + (
1778 + cd forked &&
1779 + git remote add -f other ../forked-other &&
1780 + git branch local-base &&
1781 + git branch --track local-one origin/one &&
1782 + git branch --track local-two origin/two &&
1783 + git branch --track local-foreign other/foreign &&
1784 + git branch --track local-onbase local-base &&
1785 +
1786 + git checkout local-one &&
1787 + test_commit --no-tag local-one-work local-one.t &&
1788 + git checkout local-foreign &&
1789 + test_commit --no-tag local-foreign-work local-foreign.t
1790 + )
1791 + '
1792 +
1793 + test_expect_success '--forked <upstream-tracking-branch> filters by upstream' '
1794 + git -C forked branch --forked origin/one \
1795 + --format="%(refname:short)" >actual &&
1796 + echo local-one >expect &&
1797 + test_cmp expect actual
1798 + '
1799 +
1800 + test_expect_success '--forked <glob> filters by wildmatch' '
1801 + git -C forked branch --forked "origin/*" \
1802 + --format="%(refname:short)" >actual &&
1803 + cat >expect <<-\EOF &&
1804 + local-one
1805 + local-two
1806 + main
1807 + EOF
1808 + test_cmp expect actual
1809 + '
1810 +
1811 + test_expect_success '--forked <local-branch> matches branches with local upstream' '
1812 + git -C forked branch --forked local-base \
1813 + --format="%(refname:short)" >actual &&
1814 + echo local-onbase >expect &&
1815 + test_cmp expect actual
1816 + '
1817 +
1818 + test_expect_success '--forked can be repeated to widen the filter' '
1819 + git -C forked branch --forked origin/one \
1820 + --forked other/foreign \
1821 + --format="%(refname:short)" >actual &&
1822 + cat >expect <<-\EOF &&
1823 + local-foreign
1824 + local-one
1825 + EOF
1826 + test_cmp expect actual
1827 + '
1828 +
1829 + test_expect_success '--forked combines literal and glob arguments' '
1830 + git -C forked branch --forked local-base \
1831 + --forked "other/*" \
1832 + --format="%(refname:short)" >actual &&
1833 + cat >expect <<-\EOF &&
1834 + local-foreign
1835 + local-onbase
1836 + EOF
1837 + test_cmp expect actual
1838 + '
1839 +
1840 + test_expect_success '--forked "*/*" covers every remote-tracking upstream' '
1841 + git -C forked branch --forked "*/*" \
1842 + --format="%(refname:short)" >actual &&
1843 + cat >expect <<-\EOF &&
1844 + local-foreign
1845 + local-one
1846 + local-two
1847 + main
1848 + EOF
1849 + test_cmp expect actual
1850 + '
1851 +
1852 + test_expect_success '--forked composes with --no-merged' '
1853 + git -C forked branch --forked "origin/*" \
1854 + --no-merged origin/one \
1855 + --format="%(refname:short)" >actual &&
1856 + echo local-one >expect &&
1857 + test_cmp expect actual
1858 + '
1859 +
1860 + test_expect_success '--forked <remote> uses the branch <remote>/HEAD points at' '
1861 + git -C forked branch --forked origin \
1862 + --format="%(refname:short)" >actual &&
1863 + echo main >expect &&
1864 + test_cmp expect actual
1865 + '
1866 +
1867 + test_expect_success '--forked narrows a <pattern> argument' '
1868 + git -C forked branch --forked "origin/*" "local-*" \
1869 + --format="%(refname:short)" >actual &&
1870 + cat >expect <<-\EOF &&
1871 + local-one
1872 + local-two
1873 + EOF
1874 + test_cmp expect actual
1875 + '
1876 +
1877 + test_expect_success '--forked rejects unknown branch/pattern' '
1878 + test_must_fail git -C forked branch --forked nope 2>err &&
1879 + test_grep "not a valid branch or pattern" err
1880 + '
1881 +
1882 + test_expect_success '--forked requires a value' '
1883 + test_must_fail git -C forked branch --forked 2>err &&
1884 + test_grep "requires a value" err
1885 + '
1886 +
1887 + test_expect_success '--delete-merged: setup' '
1888 + git init -b main upstream &&
1889 + (
1890 + cd upstream &&
1891 + test_commit base &&
1892 + git checkout -b next &&
1893 + test_commit next-work &&
1894 + git checkout main
1895 + ) &&
1896 + git init -b main other &&
1897 + test_commit -C other other-base &&
1898 + git init -b main fork
1899 + '
1900 +
1901 + setup_repo_for_delete_merged () {
1902 + rm -rf repo &&
1903 + git clone upstream repo &&
1904 + (
1905 + cd repo &&
1906 + git remote add fork ../fork &&
1907 + git remote add other ../other &&
1908 + git config push.default current &&
1909 + git fetch other
1910 + )
1911 + }
1912 +
1913 + create_merged_branch () {
1914 + (
1915 + cd repo &&
1916 + git checkout -b "$1" --track origin/next &&
1917 + git commit --allow-empty -m "$1 work" &&
1918 + git push origin "$1:next"
1919 + )
1920 + }
1921 +
1922 + check_branches () {
1923 + git for-each-ref --format="%(refname:short)" refs/heads/ >actual &&
1924 + cat >expect &&
1925 + test_cmp expect actual
1926 + }
1927 +
1928 + test_expect_success '--delete-merged keeps cloned main without explicit push configuration' '
1929 + setup_repo_for_delete_merged &&
1930 + (
1931 + cd repo &&
1932 + test_cmp_config origin branch.main.remote &&
1933 + test_cmp_config refs/heads/main branch.main.merge &&
1934 + git checkout --detach &&
1935 +
1936 + git branch --delete-merged */* &&
1937 +
1938 + check_branches <<-\EOF
1939 + main
1940 + EOF
1941 + )
1942 + '
1943 +
1944 + test_expect_success '--delete-merged deletes only selected merged branches' '
1945 + setup_repo_for_delete_merged &&
1946 + create_merged_branch also-merged &&
1947 + create_merged_branch merged &&
1948 + (
1949 + cd repo &&
1950 + git checkout -b unmerged --track origin/next &&
1951 + git commit --allow-empty -m "unmerged work" &&
1952 + git checkout -b tracks-other --track other/main &&
1953 + sha=$(git rev-parse --short merged) &&
1954 +
1955 + git branch --dry-run --delete-merged origin/next merged \
1956 + >actual 2>&1 &&
1957 + echo "Would delete branch merged (was $sha)." >expect &&
1958 + test_cmp expect actual &&
1959 + git rev-parse --verify refs/heads/merged &&
1960 +
1961 + check_branches <<-\EOF &&
1962 + also-merged
1963 + main
1964 + merged
1965 + tracks-other
1966 + unmerged
1967 + EOF
1968 +
1969 + git branch --delete-merged origin/next merged >actual 2>&1 &&
1970 + echo "Deleted branch merged (was $sha)." >expect &&
1971 + test_cmp expect actual &&
1972 +
1973 + check_branches <<-\EOF
1974 + also-merged
1975 + main
1976 + tracks-other
1977 + unmerged
1978 + EOF
1979 + )
1980 + '
1981 +
1982 + test_expect_success '--delete-merged keeps main despite a different default push remote' '
1983 + setup_repo_for_delete_merged &&
1984 + create_merged_branch on-next &&
1985 + create_merged_branch checked-out &&
1986 + create_merged_branch upstream-gone &&
1987 + (
1988 + cd repo &&
1989 + git config remote.pushDefault fork &&
1990 + git checkout -b local-to-delete --track main &&
1991 + git config branch.upstream-gone.merge refs/heads/topic &&
1992 + git checkout -b tracks-other --track other/main &&
1993 + git checkout checked-out &&
1994 +
1995 + git branch --delete-merged origin/* --delete-merged main &&
1996 +
1997 + check_branches <<-\EOF
1998 + checked-out
1999 + main
2000 + tracks-other
2001 + upstream-gone
2002 + EOF
2003 + )
2004 + '
2005 +
2006 + test_expect_success '--delete-merged maps push refspecs to upstreams' '
2007 + setup_repo_for_delete_merged &&
2008 + (
2009 + cd repo &&
2010 + git checkout -b topic &&
2011 + git config remote.origin.push \
2012 + "refs/heads/topic:refs/heads/published" &&
2013 + git push origin &&
2014 + git branch --set-upstream-to=origin/published topic &&
2015 + git checkout -b other-topic --track origin/published &&
2016 + git checkout --detach &&
2017 +
2018 + git branch --delete-merged origin/published &&
2019 +
2020 + check_branches <<-\EOF
2021 + main
2022 + topic
2023 + EOF
2024 + )
2025 + '
2026 +
2027 + test_expect_success '--delete-merged keeps the upstream of a surviving branch' '
2028 + setup_repo_for_delete_merged &&
2029 + create_merged_branch feature &&
2030 + (
2031 + cd repo &&
2032 + git checkout -b topic --track feature &&
2033 + git commit --allow-empty -m "topic work" &&
2034 +
2035 + git branch --dry-run --delete-merged origin/next >out &&
2036 + test_grep ! "feature" out &&
2037 +
2038 + git branch --delete-merged origin/next 2>err &&
2039 + test_must_be_empty err &&
2040 +
2041 + check_branches <<-\EOF &&
2042 + feature
2043 + main
2044 + topic
2045 + EOF
2046 +
2047 + pattern="branch\\.(feature|topic)\\.(merge|remote)" &&
2048 + git config --local --get-regexp "$pattern" >actual &&
2049 + cat >expect <<-\EOF &&
2050 + branch.feature.remote origin
2051 + branch.feature.merge refs/heads/next
2052 + branch.topic.remote .
2053 + branch.topic.merge refs/heads/feature
2054 + EOF
2055 + test_cmp expect actual
2056 + )
2057 + '
2058 +
2059 + test_expect_success '--delete-merged clears the deleted upstream of a protected branch' '
2060 + setup_repo_for_delete_merged &&
2061 + (
2062 + cd repo &&
2063 + git branch --track lower origin/next &&
2064 + git branch --track mid lower &&
2065 + git checkout -b tip --track mid &&
2066 + git commit --allow-empty -m "tip work" &&
2067 + sha=$(git rev-parse --short lower) &&
2068 +
2069 + git branch --dry-run --delete-merged origin/next \
2070 + --delete-merged lower >actual 2>&1 &&
2071 + echo "Would delete branch lower (was $sha)." >expect &&
2072 + test_cmp expect actual &&
2073 +
2074 + pattern="branch\\.(lower|mid|tip)\\.(merge|remote)" &&
2075 + git config --local --get-regexp "$pattern" >actual &&
2076 + cat >expect <<-\EOF &&
2077 + branch.lower.remote origin
2078 + branch.lower.merge refs/heads/next
2079 + branch.mid.remote .
2080 + branch.mid.merge refs/heads/lower
2081 + branch.tip.remote .
2082 + branch.tip.merge refs/heads/mid
2083 + EOF
2084 + test_cmp expect actual &&
2085 +
2086 + git branch --delete-merged origin/next \
2087 + --delete-merged lower >actual 2>&1 &&
2088 + echo "Deleted branch lower (was $sha)." >expect &&
2089 + test_cmp expect actual &&
2090 +
2091 + check_branches <<-\EOF &&
2092 + main
2093 + mid
2094 + tip
2095 + EOF
2096 +
2097 + pattern="branch\\.(mid|tip)\\.(merge|remote)" &&
2098 + git config --local --get-regexp "$pattern" >actual &&
2099 + cat >expect <<-\EOF &&
2100 + branch.tip.remote .
2101 + branch.tip.merge refs/heads/mid
2102 + EOF
2103 + test_cmp expect actual
2104 + )
2105 + '
2106 +
2107 + test_expect_success '--delete-merged result is independent of stacked branch names' '
2108 + setup_repo_for_delete_merged &&
2109 + (
2110 + cd repo &&
2111 + git branch --track c-lower origin/next &&
2112 + git branch --track b-mid c-lower &&
2113 + git checkout -b a-tip --track b-mid &&
2114 + git commit --allow-empty -m "tip work" &&
2115 +
2116 + git branch --delete-merged origin/next --delete-merged "c-*" &&
2117 +
2118 + check_branches <<-\EOF &&
2119 + a-tip
2120 + b-mid
2121 + main
2122 + EOF
2123 +
2124 + git branch --delete-merged origin/next \
2125 + --delete-merged "c-*" >actual 2>&1 &&
2126 + test_must_be_empty actual &&
2127 +
2128 + check_branches <<-\EOF
2129 + a-tip
2130 + b-mid
2131 + main
2132 + EOF
2133 + )
2134 + '
2135 +
2136 + test_expect_success '--delete-merged requires a value' '
2137 + test_must_fail git -C forked branch --delete-merged 2>err &&
2138 + test_grep "requires a value" err
2139 + '
2140 +
2141 + test_expect_success '--delete-merged honours branch.<name>.deleteMerged=false' '
2142 + setup_repo_for_delete_merged &&
2143 + create_merged_branch deleted &&
2144 + create_merged_branch kept &&
2145 + (
2146 + cd repo &&
2147 + git config branch.kept.deleteMerged false &&
2148 + git checkout --detach &&
2149 +
2150 + git branch --delete-merged origin/next 2>err &&
2151 +
2152 + test_grep "Skipping .kept." err &&
2153 + check_branches <<-\EOF
2154 + kept
2155 + main
2156 + EOF
2157 + )
2158 + '
2159 +
2160 + test_expect_success "branch -d still deletes a deleteMerged=false branch" '
2161 + setup_repo_for_delete_merged &&
2162 + create_merged_branch kept &&
2163 + (
2164 + cd repo &&
2165 + git config branch.kept.deleteMerged false &&
2166 + git checkout --detach &&
2167 +
2168 + git branch -d kept &&
2169 +
2170 + check_branches <<-\EOF
2171 + main
2172 + EOF
2173 + )
2174 + '
2175 +
2176 + test_expect_success '--dry-run without --delete-merged is rejected' '
2177 + test_must_fail git -C forked branch --dry-run 2>err &&
2178 + test_grep "requires --delete-merged" err
2179 + '
2180 +
1760 2181 test_done
modified t/t3404-rebase-interactive.sh
@@ -65,6 +65,24 @@ test_expect_success 'setup' '
65 65 test_commit P fileP
66 66 '
67 67
68 + test_expect_success MINGW 'rebase releases object database before committing' '
69 + test_when_finished "rm -f .git/hooks/post-commit repacked packs" &&
70 + git switch -C repack-rewrite primary &&
71 + git repack -ad &&
72 + write_script .git/hooks/post-commit <<-\EOF &&
73 + git repack -ad &&
74 + >repacked
75 + EOF
76 + (
77 + set_fake_editor &&
78 + FAKE_LINES="reword 1" GIT_TEST_LEGACY_DELETE=1 \
79 + git -c core.commitGraph=false rebase -i HEAD^
80 + ) &&
81 + test_path_is_file repacked &&
82 + ls .git/objects/pack/*.pack >packs &&
83 + test_line_count = 1 packs
84 + '
85 +
68 86 # "exec" commands are run with the user shell by default, but this may
69 87 # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
70 88 # to create a file. Unsetting SHELL avoids such non-portable behavior
modified t/t5516-fetch-push.sh
@@ -1903,20 +1903,20 @@ test_expect_success 'push with config push.useBitmaps' '
1903 1903 test_unconfig push.useBitmaps &&
1904 1904 GIT_TRACE2_EVENT="$PWD/default" \
1905 1905 git push --quiet testrepo main:test &&
1906 - test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1907 - --thin --delta-base-offset -q <default &&
1906 + test_subcommand git pack-objects --revs --stdout --thin \
1907 + --delta-base-offset --quiet <default &&
1908 1908
1909 1909 test_config push.useBitmaps true &&
1910 1910 GIT_TRACE2_EVENT="$PWD/true" \
1911 1911 git push --quiet testrepo main:test2 &&
1912 - test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1913 - --thin --delta-base-offset -q <true &&
1912 + test_subcommand git pack-objects --revs --stdout --thin \
1913 + --delta-base-offset --quiet <true &&
1914 1914
1915 1915 test_config push.useBitmaps false &&
1916 1916 GIT_TRACE2_EVENT="$PWD/false" \
1917 1917 git push --quiet testrepo main:test3 &&
1918 - test_subcommand git pack-objects --all-progress-implied --revs --stdout \
1919 - --thin --delta-base-offset -q --no-use-bitmap-index <false
1918 + test_subcommand git pack-objects --revs --stdout --thin \
1919 + --delta-base-offset --no-use-bitmap-index --quiet <false
1920 1920 '
1921 1921
1922 1922 test_expect_success 'push with config pack.usePathWalk=true' '
modified t/t5701-git-serve.sh
@@ -71,6 +71,17 @@ test_expect_success 'request invalid capability' '
71 71 test_grep "unknown capability" err
72 72 '
73 73
74 + test_expect_success 'promisor-remote capability requires an argument' '
75 + test-tool pkt-line pack >in <<-EOF &&
76 + command=ls-refs
77 + object-format=$(test_oid algo)
78 + promisor-remote
79 + 0000
80 + EOF
81 + test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
82 + test_grep "promisor-remote capability requires an argument" err
83 + '
84 +
74 85 test_expect_success 'request with no command' '
75 86 test-tool pkt-line pack >in <<-EOF &&
76 87 agent=git/test
@@ -344,20 +355,53 @@ test_expect_success 'unexpected lines are not allowed in fetch request' '
344 355 test_expect_success 'basics of object-info' '
345 356 test_config transfer.advertiseObjectInfo true &&
346 357
358 + two_oid=$(git rev-parse two:two.t) &&
359 + two_size=$(test_file_size two.t) &&
360 +
347 361 test-tool pkt-line pack >in <<-EOF &&
348 362 command=object-info
349 363 object-format=$(test_oid algo)
350 364 0001
351 365 size
352 - oid $(git rev-parse two:two.t)
353 - oid $(git rev-parse two:two.t)
366 + oid $two_oid
367 + oid $two_oid
368 + 0000
369 + EOF
370 +
371 + cat >expect <<-EOF &&
372 + size
373 + $two_oid $two_size
374 + $two_oid $two_size
375 + 0000
376 + EOF
377 +
378 + test-tool serve-v2 --stateless-rpc <in >out &&
379 + test-tool pkt-line unpack <out >actual &&
380 + test_cmp expect actual
381 + '
382 +
383 + test_expect_success 'object-info supports type' '
384 + test_config transfer.advertiseObjectInfo true &&
385 +
386 + two_oid=$(git rev-parse two:two.t) &&
387 + two_size=$(test_file_size two.t) &&
388 +
389 + test-tool pkt-line pack >in <<-EOF &&
390 + command=object-info
391 + object-format=$(test_oid algo)
392 + 0001
393 + size
394 + type
395 + oid $two_oid
396 + oid $two_oid
354 397 0000
355 398 EOF
356 399
357 400 cat >expect <<-EOF &&
358 401 size
359 - $(git rev-parse two:two.t) $(wc -c <two.t | xargs)
360 - $(git rev-parse two:two.t) $(wc -c <two.t | xargs)
402 + type
403 + $two_oid $two_size blob
404 + $two_oid $two_size blob
361 405 0000
362 406 EOF
363 407
added t/t6099-merge-base-side-exhaustion.sh mode 100755
@@ -0,0 +1,82 @@
1 + #!/bin/sh
2 +
3 + test_description='merge-base with ancestor among merge-base candidates
4 +
5 + Test that merge-base --all correctly handles cases where
6 + multiple merge-base candidates exist and one is an ancestor
7 + of another. The side-exhaustion optimization in
8 + paint_down_to_common may exit before STALE propagation
9 + removes the ancestor, but remove_redundant catches it.
10 +
11 + Graph shape (parents are below children):
12 +
13 + A ----- X
14 + |\ /|
15 + | B---/ |
16 + | \ |
17 + e2 \ f2
18 + | | |
19 + e1 d1 f1
20 + \ | /
21 + \ | /
22 + \|/
23 + C
24 +
25 + A and X are the two tips.
26 + B and C are both reachable from A and X.
27 + B reaches C through d1.
28 + Only B should appear in merge-base --all output.
29 + '
30 +
31 + GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
32 + export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
33 +
34 + TEST_PASSES_SANITIZE_LEAK=true
35 + . ./test-lib.sh
36 +
37 + test_expect_success 'setup ancestor merge-base candidate' '
38 + test_commit C &&
39 +
40 + git checkout -b d-chain HEAD &&
41 + test_commit d1 &&
42 + test_commit B &&
43 +
44 + git checkout -b e-path C &&
45 + test_commit e1 &&
46 + test_commit e2 &&
47 +
48 + git checkout -b f-path C &&
49 + test_commit f1 &&
50 + test_commit f2 &&
51 +
52 + git checkout -b branch-A e-path &&
53 + test_merge A B &&
54 +
55 + git checkout -b branch-X f-path &&
56 + test_merge X B &&
57 +
58 + git commit-graph write --reachable
59 + '
60 +
61 + test_expect_success 'merge-base --all excludes ancestor candidate' '
62 + git rev-parse B >expected &&
63 + git merge-base --all A X >actual &&
64 + test_cmp expected actual
65 + '
66 +
67 + test_expect_success 'merge-base (single) finds shallowest' '
68 + git rev-parse B >expected &&
69 + git merge-base A X >actual &&
70 + test_cmp expected actual
71 + '
72 +
73 + # Without commit-graph: generation numbers are INFINITY,
74 + # side-exhaustion optimization does not fire.
75 + test_expect_success 'merge-base --all without commit-graph' '
76 + rm -f .git/objects/info/commit-graph &&
77 + git rev-parse B >expected &&
78 + git merge-base --all A X >actual &&
79 + test_cmp expected actual
80 + '
81 +
82 + test_done
modified t/t6600-test-reach.sh
@@ -85,6 +85,103 @@ test_expect_success 'setup' '
85 85 git branch -f skew-P2 "$skew_P2" &&
86 86 git tag skew-M2 "$skew_M2" &&
87 87
88 + # Build a small side topology to exercise the (PARENT1|PARENT2) ->
89 + # (PARENT1|PARENT2|STALE) transition in paint_down_to_common(); the
90 + # 10x10 grid above does not exercise it because no merge-base candidate
91 + # there is a descendant of another, so STALE never reaches a
92 + # still-pending candidate.
93 + #
94 + # ps-X
95 + # /|\
96 + # / | \
97 + # ps-Z ps-B ps-W
98 + # | / \ |
99 + # | / \ |
100 + # |/ \|
101 + # ps-T1 ps-T2
102 + #
103 + # where ps-T1=merge(ps-Z,ps-B), ps-T2=merge(ps-W,ps-B), so
104 + # merge-base(ps-T1,ps-T2) = ps-B. During the walk, ps-X transitions
105 + # to (PARENT1|PARENT2) via ps-Z and ps-W before ps-B is dequeued;
106 + # then the STALE-walk from ps-B transitions ps-X to
107 + # (PARENT1|PARENT2|STALE).
108 + git checkout --orphan ps-orphan &&
109 + test_commit ps-X &&
110 + git checkout -b ps-B-br ps-X && test_commit ps-B &&
111 + git checkout -b ps-Z-br ps-X && test_commit ps-Z &&
112 + git checkout -b ps-W-br ps-X && test_commit ps-W &&
113 + git checkout -b ps-T1 ps-Z &&
114 + git merge --no-ff -m ps-T1 ps-B &&
115 + git checkout -b ps-T2 ps-W &&
116 + git merge --no-ff -m ps-T2 ps-B &&
117 +
118 + # Build a side topology that lives entirely outside the half
119 + # commit-graph and has non-monotonic commit dates, to exercise the
120 + # INFINITY-gate in paint_down_to_common. With both tips outside
121 + # the graph, generation is INFINITY and the queue falls back to
122 + # commit-date order, which here is non-monotonic.
123 + #
124 + # pi-X (date 500, PARENT1 tip) --> pi-P, pi-D
125 + # pi-D (date 480) --> pi-C
126 + # pi-C (date 200) --> pi-B
127 + # pi-B (date 100, PARENT2 tip) --> pi-P
128 + # pi-P (date 450, root)
129 + #
130 + # merge-base(pi-X, pi-B) = pi-B (it is an ancestor of pi-X and is
131 + # itself one of the queried tips).
132 + git checkout --orphan pi-orphan &&
133 + test_commit --date "@450 +0000" pi-P &&
134 + test_commit --date "@100 +0000" pi-B &&
135 + test_commit --date "@200 +0000" pi-C &&
136 + test_commit --date "@480 +0000" pi-D &&
137 + GIT_AUTHOR_DATE="@500 +0000" GIT_COMMITTER_DATE="@500 +0000" \
138 + git commit-tree -p pi-D -p pi-P -m pi-X pi-D^{tree} >pi-X-oid &&
139 + pi_x="$(cat pi-X-oid)" &&
140 + git branch -f pi-X-br "$pi_x" &&
141 + git tag pi-X "$pi_x" &&
142 +
143 + # Clock-skew topology for side-exhaustion testing.
144 + # D is the correct merge base but has a higher committer date
145 + # than C (its child). With date ordering, D would be dequeued
146 + # before C, causing side-exhaustion to fire too early.
147 + # Generation ordering prevents this by visiting children
148 + # before parents regardless of dates.
149 + #
150 + # se-A (date 7000) --> se-C (date 3000) --> se-D (date 5000) --> se-root (date 4000)
151 + # se-B (date 6000) --> se-D
152 + #
153 + se_root=$(skew_commit 4000 se-root) &&
154 + se_D=$(skew_commit 5000 se-D -p "$se_root") &&
155 + se_C=$(skew_commit 3000 se-C -p "$se_D") &&
156 + se_A=$(skew_commit 7000 se-A -p "$se_C") &&
157 + se_B=$(skew_commit 6000 se-B -p "$se_D") &&
158 + git branch -f se-A "$se_A" &&
159 + git branch -f se-B "$se_B" &&
160 + git tag se-D "$se_D" &&
161 +
162 + # Clock-skew topology with redundant ancestor for
163 + # side-exhaustion testing. MB1 is the correct merge base;
164 + # MB2 is its parent. A reaches MB2 via E (high date) and
165 + # MB1 via C (low date). B reaches MB1 via D. With date
166 + # ordering, side-exhaustion would fire before C is dequeued,
167 + # missing MB1. Generation ordering ensures both are found.
168 + #
169 + # se2-A (date 8000) --> se2-C (date 2000) --> se2-MB1 (date 5000) --> se2-MB2 (date 4000) --> se2-root (date 1000)
170 + # se2-A --> se2-E (date 6500) --> se2-MB2
171 + # se2-B (date 7000) --> se2-D (date 6000) --> se2-MB1
172 + #
173 + se2_root=$(skew_commit 1000 se2-root) &&
174 + se2_MB2=$(skew_commit 4000 se2-MB2 -p "$se2_root") &&
175 + se2_MB1=$(skew_commit 5000 se2-MB1 -p "$se2_MB2") &&
176 + se2_C=$(skew_commit 2000 se2-C -p "$se2_MB1") &&
177 + se2_D=$(skew_commit 6000 se2-D -p "$se2_MB1") &&
178 + se2_E=$(skew_commit 6500 se2-E -p "$se2_MB2") &&
179 + se2_A=$(skew_commit 8000 se2-A -p "$se2_C" -p "$se2_E") &&
180 + se2_B=$(skew_commit 7000 se2-B -p "$se2_D") &&
181 + git branch -f se2-A "$se2_A" &&
182 + git branch -f se2-B "$se2_B" &&
183 + git tag se2-MB1 "$se2_MB1" &&
184 +
88 185 git commit-graph write --reachable &&
89 186 mv .git/objects/info/commit-graph commit-graph-full &&
90 187 chmod u+w commit-graph-full &&
@@ -98,24 +195,34 @@ test_expect_success 'setup' '
98 195 '
99 196
100 197 run_all_modes () {
101 - test_when_finished rm -rf .git/objects/info/commit-graph &&
102 - "$@" <input >actual &&
103 - test_cmp expect actual &&
104 - cp commit-graph-full .git/objects/info/commit-graph &&
105 - "$@" <input >actual &&
106 - test_cmp expect actual &&
107 - cp commit-graph-half .git/objects/info/commit-graph &&
108 - "$@" <input >actual &&
109 - test_cmp expect actual &&
110 - cp commit-graph-no-gdat .git/objects/info/commit-graph &&
111 - "$@" <input >actual &&
112 - test_cmp expect actual
198 + graph=.git/objects/info/commit-graph &&
199 + test_when_finished rm -rf "$graph" "${graph}s" &&
200 + rm -f trace-mode-*.txt &&
201 +
202 + for mode in none full half no-gdat
203 + do
204 + rm -rf "$graph" "${graph}s" &&
205 + cp "commit-graph-${mode}" "$graph" 2>/dev/null ||
206 + true &&
207 + GIT_TRACE2_EVENT="$(pwd)/trace-mode-${mode}.txt" \
208 + "$@" <input >actual &&
209 + test_cmp expect actual || return 1
210 + done
113 211 }
114 212
115 213 test_all_modes () {
116 214 run_all_modes test-tool reach "$@"
117 215 }
118 216
217 + test_paint_down_steps () {
218 + for mode in none full half no-gdat
219 + do
220 + test_trace2_data_singular paint_down_to_common steps "$1" \
221 + "mode=$mode" <"trace-mode-${mode}.txt" || return 1
222 + shift
223 + done
224 + }
225 +
119 226 test_expect_success 'ref_newer:miss' '
120 227 cat >input <<-\EOF &&
121 228 A:commit-5-7
@@ -182,6 +289,17 @@ test_expect_success 'in_merge_bases_many:miss-heuristic' '
182 289 test_all_modes in_merge_bases_many
183 290 '
184 291
292 + test_expect_success 'in_merge_bases_many:self' '
293 + cat >input <<-\EOF &&
294 + A:commit-6-8
295 + X:commit-5-9
296 + X:commit-6-8
297 + EOF
298 + echo "in_merge_bases_many(A,X):1" >expect &&
299 + test_all_modes in_merge_bases_many &&
300 + test_paint_down_steps 45 1 25 1
301 + '
302 +
185 303 test_expect_success 'is_descendant_of:hit' '
186 304 cat >input <<-\EOF &&
187 305 A:commit-5-7
@@ -219,6 +337,105 @@ test_expect_success 'get_merge_bases_many' '
219 337 test_all_modes get_merge_bases_many
220 338 '
221 339
340 + test_expect_success 'get_merge_bases_many:duplicate-twos' '
341 + cat >input <<-\EOF &&
342 + A:commit-5-7
343 + X:commit-4-8
344 + X:commit-4-8
345 + X:commit-6-6
346 + X:commit-6-6
347 + X:commit-8-3
348 + EOF
349 + {
350 + echo "get_merge_bases_many(A,X):" &&
351 + git rev-parse commit-5-6 \
352 + commit-4-7 | sort
353 + } >expect &&
354 + test_all_modes get_merge_bases_many
355 + '
356 +
357 + test_expect_success 'get_merge_bases_many:pending-stale' '
358 + # Exercises the (PARENT1|PARENT2) -> (...|STALE) transition path in
359 + # paint_down_to_common(). See the topology comment in the setup test.
360 + cat >input <<-\EOF &&
361 + A:ps-T1
362 + X:ps-T2
363 + EOF
364 + {
365 + echo "get_merge_bases_many(A,X):" &&
366 + git rev-parse ps-B
367 + } >expect &&
368 + test_all_modes get_merge_bases_many &&
369 + test_paint_down_steps 5 5 5 5
370 + '
371 +
372 + test_expect_success 'get_merge_bases_many:infinity-both-sides' '
373 + # Exercises the push-time INFINITY-gate in paint_down_to_common(). See
374 + # the pi-* topology comment in the setup test.
375 + cat >input <<-\EOF &&
376 + A:pi-X
377 + X:pi-B
378 + EOF
379 + {
380 + echo "get_merge_bases_many(A,X):" &&
381 + git rev-parse pi-B
382 + } >expect &&
383 + test_all_modes get_merge_bases_many &&
384 + test_paint_down_steps 5 4 5 4
385 + '
386 +
387 + test_expect_success 'setup mixed finite/INFINITY topology' '
388 + # Create a commit outside all saved commit-graph files so it always
389 + # has INFINITY generation, while its parent (ps-X) is in the graph
390 + # with a finite generation. Use the ps-* orphan topology so we do
391 + # not pollute the grid-based rev-list tests.
392 + git checkout ps-X &&
393 + test_env GIT_TEST_COMMIT_GRAPH= test_commit pm-INF
394 + '
395 +
396 + test_expect_success 'get_merge_bases_many:mixed-finite-infinity' '
397 + # One tip (pm-INF) is outside the commit-graph with INFINITY
398 + # generation; the other (ps-B) is in the graph with finite
399 + # generation. The walk starts in the INFINITY region and crosses
400 + # into the finite region where side-exhaustion can fire.
401 + cat >input <<-\EOF &&
402 + A:pm-INF
403 + X:ps-B
404 + EOF
405 + {
406 + echo "get_merge_bases_many(A,X):" &&
407 + git rev-parse ps-X
408 + } >expect &&
409 + test_all_modes get_merge_bases_many &&
410 + test_paint_down_steps 3 3 3 3
411 + '
412 +
413 + test_expect_success 'merge-base --all commit-walk steps' '
414 + >input &&
415 + git rev-parse commit-9-1 >expect &&
416 + run_all_modes git merge-base --all commit-9-9 commit-9-1 &&
417 + test_paint_down_steps 81 9 57 37
418 + '
419 +
420 + test_expect_success 'merge-base --all with clock skew (side-exhaustion)' '
421 + # Verify that the merge base is computed correctly even
422 + # when commits have non-monotonic commit dates.
423 + >input &&
424 + git rev-parse se-D >expect &&
425 + run_all_modes git merge-base --all se-A se-B &&
426 + test_paint_down_steps 6 4 6 4
427 + '
428 +
429 + test_expect_success 'merge-base --all with clock skew and redundant ancestor (side-exhaustion)' '
430 + # Verify that the correct merge base is found even when
431 + # non-monotonic commit dates could cause a redundant
432 + # ancestor to be visited first.
433 + >input &&
434 + git rev-parse se2-MB1 >expect &&
435 + run_all_modes git merge-base --all se2-A se2-B &&
436 + test_paint_down_steps 8 6 8 6
437 + '
438 +
222 439 test_expect_success 'reduce_heads' '
223 440 cat >input <<-\EOF &&
224 441 X:commit-1-10
modified t/t7900-maintenance.sh
@@ -7,9 +7,6 @@ test_description='git maintenance builtin'
7 7 GIT_TEST_COMMIT_GRAPH=0
8 8 GIT_TEST_MULTI_PACK_INDEX=0
9 9
10 - # Ensure that auto-maintenance detaches as usual.
11 - sane_unset GIT_TEST_MAINT_AUTO_DETACH
12 -
13 10 test_lazy_prereq XMLLINT '
14 11 xmllint --version
15 12 '
@@ -67,41 +64,60 @@ test_expect_success 'run [--auto|--quiet] with gc strategy' '
67 64 '
68 65
69 66 test_expect_success 'maintenance.auto config option' '
70 - GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
71 - test_subcommand git maintenance run --auto --quiet --detach <default &&
72 - GIT_TRACE2_EVENT="$(pwd)/true" \
73 - git -c maintenance.auto=true \
74 - commit --quiet --allow-empty -m 2 &&
75 - test_subcommand git maintenance run --auto --quiet --detach <true &&
76 - GIT_TRACE2_EVENT="$(pwd)/false" \
77 - git -c maintenance.auto=false \
78 - commit --quiet --allow-empty -m 3 &&
79 - test_subcommand ! git maintenance run --auto --quiet --detach <false
67 + test_when_finished "rm -rf repo" &&
68 + git init repo &&
69 + (
70 + cd repo &&
71 + sane_unset GIT_TEST_MAINT_AUTO_DETACH &&
72 +
73 + GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
74 + test_subcommand git maintenance run --auto --quiet --detach <default &&
75 + GIT_TRACE2_EVENT="$(pwd)/true" \
76 + git -c maintenance.auto=true \
77 + commit --quiet --allow-empty -m 2 &&
78 + test_subcommand git maintenance run --auto --quiet --detach <true &&
79 + GIT_TRACE2_EVENT="$(pwd)/false" \
80 + git -c maintenance.auto=false \
81 + commit --quiet --allow-empty -m 3 &&
82 + test_subcommand ! git maintenance run --auto --quiet --detach <false
83 + )
80 84 '
81 85
82 86 test_expect_success 'gc.auto config option' '
83 - GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
84 - test_subcommand git maintenance run --auto --quiet --detach <default &&
85 - GIT_TRACE2_EVENT="$(pwd)/true" \
86 - git -c gc.auto=1 commit --quiet --allow-empty -m 2 &&
87 - test_subcommand git maintenance run --auto --quiet --detach <true &&
88 - GIT_TRACE2_EVENT="$(pwd)/false" \
89 - git -c gc.auto=0 commit --quiet --allow-empty -m 3 &&
90 - test_subcommand ! git maintenance run --auto --quiet --detach <false
87 + test_when_finished "rm -rf repo" &&
88 + git init repo &&
89 + (
90 + cd repo &&
91 + sane_unset GIT_TEST_MAINT_AUTO_DETACH &&
92 +
93 + GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
94 + test_subcommand git maintenance run --auto --quiet --detach <default &&
95 + GIT_TRACE2_EVENT="$(pwd)/true" \
96 + git -c gc.auto=1 commit --quiet --allow-empty -m 2 &&
97 + test_subcommand git maintenance run --auto --quiet --detach <true &&
98 + GIT_TRACE2_EVENT="$(pwd)/false" \
99 + git -c gc.auto=0 commit --quiet --allow-empty -m 3 &&
100 + test_subcommand ! git maintenance run --auto --quiet --detach <false
101 + )
91 102 '
92 103
93 104 test_expect_success 'maintenance.auto overrides gc.auto' '
94 - test_when_finished "rm -f trace" &&
105 + test_when_finished "rm -rf repo" &&
106 + git init repo &&
107 + (
108 + cd repo &&
109 + sane_unset GIT_TEST_MAINT_AUTO_DETACH &&
95 110
96 - test_config maintenance.auto false &&
97 - test_config gc.auto 1 &&
98 - GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
99 - test_subcommand ! git maintenance run --auto --quiet --detach <trace &&
111 + git config set maintenance.auto false &&
112 + git config set gc.auto 1 &&
113 + GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
114 + test_subcommand ! git maintenance run --auto --quiet --detach <trace &&
100 115
101 - test_config maintenance.auto true &&
102 - test_config gc.auto 0 &&
103 - GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
104 - test_subcommand git maintenance run --auto --quiet --detach <trace
116 + git config set maintenance.auto true &&
117 + git config set gc.auto 0 &&
118 + GIT_TRACE2_EVENT="$(pwd)/trace" git commit --quiet --allow-empty -m 1 &&
119 + test_subcommand git maintenance run --auto --quiet --detach <trace
120 + )
105 121 '
106 122
107 123 for cfg in maintenance.autoDetach gc.autoDetach
modified t/t9001-send-email.sh
@@ -1422,6 +1422,21 @@ test_expect_success $PREREQ 'detects ambiguous reference/file conflict' '
1422 1422 test_grep disambiguate errors
1423 1423 '
1424 1424
1425 + test_expect_success $PREREQ 'missing subject omits Perl location' '
1426 + cat >no-subject.patch <<-\EOF &&
1427 + This is the body.
1428 + EOF
1429 + test_must_fail git send-email \
1430 + --dry-run \
1431 + --from="Example <nobody@example.com>" \
1432 + --to=nobody@example.com \
1433 + no-subject.patch 2>actual &&
1434 + cat >expect <<-\EOF &&
1435 + No '\''Subject:'\'' line in '\''no-subject.patch'\''
1436 + EOF
1437 + test_cmp expect actual
1438 + '
1439 +
1425 1440 test_expect_success $PREREQ 'feed two files' '
1426 1441 rm -fr outdir &&
1427 1442 git format-patch -2 -o outdir &&
modified t/t9300-fast-import.sh
@@ -2827,6 +2827,13 @@ test_expect_success 'R: unknown commandline options are rejected' '\
2827 2827 test_must_fail git fast-import --non-existing-option < /dev/null
2828 2828 '
2829 2829
2830 + test_expect_success 'R: feature-only names are rejected on the command line' '
2831 + for opt in --alias --get-mark --ls --notes
2832 + do
2833 + test_must_fail git fast-import "$opt" </dev/null || return 1
2834 + done
2835 + '
2836 +
2830 2837 test_expect_success 'R: die on invalid option argument' '
2831 2838 echo "option git active-branches=-5" |
2832 2839 test_must_fail git fast-import &&
modified t/test-lib-functions.sh
@@ -2004,6 +2004,41 @@ test_trace2_data () {
2004 2004 grep -e '"category":"'"$1"'","key":"'"$2"'","value":"'"$3"'"'
2005 2005 }
2006 2006
2007 + # Check that the given trace2 data event has the expected value and
2008 + # appears exactly once. Produces a diagnostic on failure.
2009 + #
2010 + # test_trace2_data_singular <category> <key> <value> [<label>]
2011 + test_trace2_data_singular () {
2012 + local category="$1" key="$2" expect_val="$3"
2013 + local label_suffix="${4:+ [$4]}"
2014 + local kv_pattern='"category":"'"$category"'","key":"'"$key"'","value":"\([^"]*\)"'
2015 + local actual
2016 +
2017 + actual=$(sed -n "s|.*${kv_pattern}.*|\1|p") &&
2018 +
2019 + if test -z "$actual"
2020 + then
2021 + echo >&4 "error: trace2 data '$category/$key'$label_suffix not found"
2022 + return 1
2023 + fi &&
2024 +
2025 + case "$actual" in
2026 + *"$LF"*)
2027 + echo >&4 "error: trace2 data '$category/$key'$label_suffix has multiple entries, expected 1"
2028 + printf '%s\n' "$actual" | sed 's/^/ actual: /' >&4
2029 + return 1
2030 + ;;
2031 + esac &&
2032 +
2033 + if test "$actual" != "$expect_val"
2034 + then
2035 + echo >&4 "error: trace2 data '$category/$key'$label_suffix"
2036 + echo >&4 " expected: $expect_val"
2037 + echo >&4 " actual: $actual"
2038 + return 1
2039 + fi
2040 + }
2041 +
2007 2042 # Given a GIT_TRACE2_EVENT log over stdin, writes to stdout a list of URLs
2008 2043 # sent to git-remote-https child processes.
2009 2044 test_remote_https_urls() {
modified t/unit-tests/u-odb-inmemory.c
@@ -29,7 +29,7 @@ static void cl_assert_object_info(struct odb_source_inmemory *source,
29 29 .contentp = &actual_content,
30 30 };
31 31
32 - cl_must_pass(odb_source_read_object_info(&source->base, oid, &oi, 0, NULL));
32 + cl_must_pass(odb_source_read_object_info(&source->base, oid, &oi, 0));
33 33 cl_assert_equal_u(actual_size, strlen(expected_content));
34 34 cl_assert_equal_u(actual_type, expected_type);
35 35 cl_assert_equal_s((char *) actual_content, expected_content);
@@ -50,7 +50,7 @@ static void cl_assert_write_object(struct odb_source_inmemory *source,
50 50
51 51 void test_odb_inmemory__initialize(void)
52 52 {
53 - odb = odb_new(&repo, "", "");
53 + odb = odb_new(&repo, 0);
54 54 }
55 55
56 56 void test_odb_inmemory__cleanup(void)
@@ -72,8 +72,7 @@ void test_odb_inmemory__read_missing_object(void)
72 72 const char *end;
73 73
74 74 cl_must_pass(parse_oid_hex_algop(RANDOM_OID, &oid, &end, repo.hash_algo));
75 - cl_assert_equal_i(odb_source_read_object_info(&source->base, &oid, NULL, 0, NULL),
76 - ODB_READ_NOT_FOUND);
75 + cl_must_fail(odb_source_read_object_info(&source->base, &oid, NULL, 0));
77 76
78 77 odb_source_free(&source->base);
79 78 }
@@ -101,7 +100,7 @@ void test_odb_inmemory__read_written_object(void)
101 100 void test_odb_inmemory__read_stream_object(void)
102 101 {
103 102 struct odb_source_inmemory *source = odb_source_inmemory_new(odb);
104 - struct odb_read_stream *stream;
103 + struct odb_stream *stream;
105 104 struct object_id written_oid;
106 105 const char data[] = "foobar";
107 106 char buf[3] = { 0 };
@@ -113,15 +112,15 @@ void test_odb_inmemory__read_stream_object(void)
113 112 cl_assert_equal_i(stream->type, OBJ_BLOB);
114 113 cl_assert_equal_u(stream->size, 6);
115 114
116 - cl_assert_equal_i(odb_read_stream_read(stream, buf, 2), 2);
115 + cl_assert_equal_i(odb_stream_read(stream, buf, 2), 2);
117 116 cl_assert_equal_s(buf, "fo");
118 - cl_assert_equal_i(odb_read_stream_read(stream, buf, 2), 2);
117 + cl_assert_equal_i(odb_stream_read(stream, buf, 2), 2);
119 118 cl_assert_equal_s(buf, "ob");
120 - cl_assert_equal_i(odb_read_stream_read(stream, buf, 2), 2);
119 + cl_assert_equal_i(odb_stream_read(stream, buf, 2), 2);
121 120 cl_assert_equal_s(buf, "ar");
122 - cl_assert_equal_i(odb_read_stream_read(stream, buf, 2), 0);
121 + cl_assert_equal_i(odb_stream_read(stream, buf, 2), 0);
123 122
124 - odb_read_stream_close(stream);
123 + odb_stream_close(stream);
125 124 odb_source_free(&source->base);
126 125 }
127 126
@@ -267,28 +266,28 @@ void test_odb_inmemory__freshen_object(void)
267 266 }
268 267
269 268 struct membuf_write_stream {
270 - struct odb_write_stream base;
269 + struct odb_stream base;
271 270 const char *buf;
272 271 size_t offset;
273 - size_t size;
274 272 };
275 273
276 - static ssize_t membuf_write_stream_read(struct odb_write_stream *stream,
277 - unsigned char *buf, size_t len)
274 + static ssize_t membuf_write_stream_read(struct odb_stream *stream,
275 + char *buf, size_t len)
278 276 {
279 277 struct membuf_write_stream *s = container_of(stream, struct membuf_write_stream, base);
280 278 size_t chunk_size = 2;
281 279
280 + if (s->offset == s->base.size)
281 + return 0;
282 +
282 283 if (chunk_size > len)
283 284 chunk_size = len;
284 - if (chunk_size > s->size - s->offset)
285 - chunk_size = s->size - s->offset;
285 + if (chunk_size > s->base.size - s->offset)
286 + chunk_size = s->base.size - s->offset;
286 287
287 288 memcpy(buf, s->buf + s->offset, chunk_size);
288 289
289 290 s->offset += chunk_size;
290 - if (s->offset == s->size)
291 - s->base.is_finished = 1;
292 291
293 292 return chunk_size;
294 293 }
@@ -298,14 +297,17 @@ void test_odb_inmemory__write_object_stream(void)
298 297 struct odb_source_inmemory *source = odb_source_inmemory_new(odb);
299 298 const char data[] = "foobar";
300 299 struct membuf_write_stream stream = {
301 - .base.read = membuf_write_stream_read,
300 + .base = {
301 + .read = membuf_write_stream_read,
302 + .size = strlen(data),
303 + .type = OBJ_BLOB,
304 + },
302 305 .buf = data,
303 - .size = strlen(data),
304 306 };
305 307 struct object_id written_oid;
306 308
307 309 cl_must_pass(odb_source_write_object_stream(&source->base, &stream.base,
308 - strlen(data), &written_oid));
310 + &written_oid));
309 311 cl_assert_equal_s(oid_to_hex(&written_oid), FOOBAR_OID);
310 312 cl_assert_object_info(source, &written_oid, OBJ_BLOB, "foobar");
311 313
modified t/unit-tests/u-reftable-table.c
@@ -32,7 +32,8 @@ void test_reftable_table__seek_once(void)
32 32 ret = reftable_table_new(&table, &source, "name");
33 33 cl_assert(!ret);
34 34
35 - reftable_table_init_ref_iterator(table, &it);
35 + ret = reftable_table_init_ref_iterator(table, &it);
36 + cl_assert_equal_i(ret, 0);
36 37 ret = reftable_iterator_seek_ref(&it, "");
37 38 cl_assert(!ret);
38 39 ret = reftable_iterator_next_ref(&it, &ref);
@@ -74,7 +75,8 @@ void test_reftable_table__reseek(void)
74 75 ret = reftable_table_new(&table, &source, "name");
75 76 cl_assert(!ret);
76 77
77 - reftable_table_init_ref_iterator(table, &it);
78 + ret = reftable_table_init_ref_iterator(table, &it);
79 + cl_assert_equal_i(ret, 0);
78 80
79 81 for (size_t i = 0; i < 5; i++) {
80 82 ret = reftable_iterator_seek_ref(&it, "");
modified transport-helper.c
@@ -487,6 +487,8 @@ static int get_exporter(struct transport *transport,
487 487 /* we need to duplicate helper->in because we want to use it after
488 488 * fastexport is done with it. */
489 489 fastexport->out = dup(helper->in);
490 + if (fastexport->out < 0)
491 + return error_errno(_("could not dup helper output fd"));
490 492 strvec_push(&fastexport->args, "fast-export");
491 493 strvec_push(&fastexport->args, "--use-done-feature");
492 494 strvec_push(&fastexport->args, data->signed_tags ?
@@ -784,11 +786,14 @@ static int fetch_refs(struct transport *transport,
784 786 return -1;
785 787 }
786 788
787 - static int fetch_object_info_helper(struct transport *transport)
789 + static int fetch_object_info_helper(struct transport *transport,
790 + const struct oid_array *oids,
791 + struct fetch_object_info_results *results)
788 792 {
789 793 get_helper(transport);
790 794 if (process_connect(transport, 0))
791 - return transport->vtable->fetch_object_info(transport);
795 + return transport->vtable->fetch_object_info(transport, oids,
796 + results);
792 797
793 798 die(_("object-info requires protocol v2"));
794 799 }
@@ -1191,7 +1196,9 @@ static int push_refs_with_export(struct transport *transport,
1191 1196
1192 1197 if (data->export_marks) {
1193 1198 strbuf_addf(&buf, "%s.tmp", data->export_marks);
1194 - rename(buf.buf, data->export_marks);
1199 + if (rename(buf.buf, data->export_marks))
1200 + warning_errno(_("could not rename '%s' to '%s'"),
1201 + buf.buf, data->export_marks);
1195 1202 strbuf_release(&buf);
1196 1203 }
1197 1204
modified transport-internal.h
@@ -7,6 +7,8 @@ struct ref;
7 7 struct transport;
8 8 struct strvec;
9 9 struct transport_ls_refs_options;
10 + struct oid_array;
11 + struct fetch_object_info_results;
10 12
11 13 struct transport_vtable {
12 14 /**
@@ -51,7 +53,9 @@ struct transport_vtable {
51 53 *
52 54 * Uses object-info capability of v2 protocol.
53 55 */
54 - int (*fetch_object_info)(struct transport *transport);
56 + int (*fetch_object_info)(struct transport *transport,
57 + const struct oid_array *oids,
58 + struct fetch_object_info_results *results);
55 59
56 60 /**
57 61 * Push the objects and refs. Send the necessary objects, and
modified transport.c
@@ -433,16 +433,13 @@ static int get_bundle_uri(struct transport *transport)
433 433 transport->bundles, stateless_rpc);
434 434 }
435 435
436 - static int fetch_object_info_via_pack(struct transport *transport)
436 + static int fetch_object_info_via_pack(struct transport *transport,
437 + const struct oid_array *oids,
438 + struct fetch_object_info_results *results)
437 439 {
438 440 int ret = 0;
439 441 struct git_transport_data *data = transport->data;
440 442 struct packet_reader reader;
441 - struct object_info_args args = { 0 };
442 -
443 - args.server_options = transport->server_options;
444 - args.oids = transport->smart_options->object_info_oids;
445 - args.object_info_options = transport->smart_options->object_info_options;
446 443
447 444 connect_setup(transport, 0);
448 445 packet_reader_init(&reader, data->fd[0], NULL, 0,
@@ -453,9 +450,12 @@ static int fetch_object_info_via_pack(struct transport *transport)
453 450 data->version = discover_version(&reader);
454 451 transport->hash_algo = reader.hash_algo;
455 452
456 - ret = fetch_object_info(data->version, &args, &reader,
457 - data->options.object_info_data,
458 - transport->stateless_rpc, data->fd[1]);
453 + fetch_object_info(data->version,
454 + transport->server_options,
455 + oids,
456 + &reader,
457 + results,
458 + transport->stateless_rpc, data->fd[1]);
459 459
460 460 close(data->fd[0]);
461 461 if (data->fd[1] >= 0)
@@ -467,11 +467,13 @@ static int fetch_object_info_via_pack(struct transport *transport)
467 467 return ret;
468 468 }
469 469
470 - int transport_fetch_object_info(struct transport *transport)
470 + int transport_fetch_object_info(struct transport *transport,
471 + const struct oid_array *oids,
472 + struct fetch_object_info_results *results)
471 473 {
472 474 if (!transport->vtable->fetch_object_info)
473 475 die(_("remote does not support object-info"));
474 - return transport->vtable->fetch_object_info(transport);
476 + return transport->vtable->fetch_object_info(transport, oids, results);
475 477 }
476 478
477 479 static int fetch_refs_via_pack(struct transport *transport,
modified transport.h
@@ -7,6 +7,8 @@
7 7 #include "string-list.h"
8 8 #include "connect.h"
9 9
10 + struct fetch_object_info_results;
11 +
10 12 struct git_transport_options {
11 13 unsigned thin : 1;
12 14 unsigned keep : 1;
@@ -55,10 +57,6 @@ struct git_transport_options {
55 57 * common commits to this oidset instead of fetching any packfiles.
56 58 */
57 59 struct oidset *acked_commits;
58 -
59 - struct oid_array *object_info_oids;
60 - struct object_info *object_info_data;
61 - struct string_list *object_info_options;
62 60 };
63 61
64 62 enum transport_family {
@@ -316,7 +314,9 @@ int transport_fetch_refs(struct transport *transport, struct ref *refs);
316 314 /*
317 315 * Fetch the object info from remote
318 316 */
319 - int transport_fetch_object_info(struct transport *transport);
317 + int transport_fetch_object_info(struct transport *transport,
318 + const struct oid_array *oids,
319 + struct fetch_object_info_results *results);
320 320
321 321 /*
322 322 * If this flag is set, unlocking will avoid to call non-async-signal-safe
modified upload-pack.c
@@ -197,11 +197,11 @@ static void send_client_data(int fd, const char *data, ssize_t sz,
197 197 write_or_die(fd, data, sz);
198 198 }
199 199
200 - static int write_one_shallow(const struct commit_graft *graft, void *cb_data)
200 + static int append_one_shallow(const struct commit_graft *graft, void *cb_data)
201 201 {
202 - FILE *fp = cb_data;
202 + struct oid_array *shallows = cb_data;
203 203 if (graft->nr_parent == -1)
204 - fprintf(fp, "--shallow %s\n", oid_to_hex(&graft->oid));
204 + oid_array_append(shallows, &graft->oid);
205 205 return 0;
206 206 }
207 207
@@ -299,7 +299,8 @@ static int relay_pack_data(int pack_objects_out, struct output_state *os,
299 299 static void create_pack_file(struct upload_pack_data *pack_data,
300 300 const struct string_list *uri_protocols)
301 301 {
302 - struct child_process pack_objects = CHILD_PROCESS_INIT;
302 + struct odb_generate_pack_options opts = ODB_GENERATE_PACK_OPTIONS_INIT;
303 + struct odb_pack_generator *generator;
303 304 struct output_state *output_state = xcalloc(1, sizeof(struct output_state));
304 305 char progress[128];
305 306 char abort_msg[] = "aborting due to possible repository "
@@ -307,78 +308,42 @@ static void create_pack_file(struct upload_pack_data *pack_data,
307 308 uint64_t last_sent_ms = 0;
308 309 ssize_t sz;
309 310 int i;
310 - FILE *pipe_fd;
311 -
312 - if (!pack_data->pack_objects_hook)
313 - pack_objects.git_cmd = 1;
314 - else {
315 - strvec_push(&pack_objects.args, pack_data->pack_objects_hook);
316 - strvec_push(&pack_objects.args, "git");
317 - pack_objects.use_shell = 1;
318 - }
319 311
320 312 if (pack_data->shallow_nr) {
321 - strvec_push(&pack_objects.args, "--shallow-file");
322 - strvec_push(&pack_objects.args, "");
323 - }
324 - strvec_push(&pack_objects.args, "pack-objects");
325 - strvec_push(&pack_objects.args, "--revs");
326 - if (pack_data->use_thin_pack)
327 - strvec_push(&pack_objects.args, "--thin");
328 -
329 - strvec_push(&pack_objects.args, "--stdout");
330 - if (pack_data->shallow_nr)
331 - strvec_push(&pack_objects.args, "--shallow");
332 - if (!pack_data->no_progress)
333 - strvec_push(&pack_objects.args, "--progress");
334 - if (pack_data->use_ofs_delta)
335 - strvec_push(&pack_objects.args, "--delta-base-offset");
336 - if (pack_data->use_include_tag)
337 - strvec_push(&pack_objects.args, "--include-tag");
338 - if (repo_has_accepted_promisor_remote(the_repository))
339 - strvec_push(&pack_objects.args, "--missing=allow-promisor");
340 - if (pack_data->filter_options.choice) {
341 - const char *spec =
342 - expand_list_objects_filter_spec(&pack_data->filter_options);
343 - strvec_pushf(&pack_objects.args, "--filter=%s", spec);
344 - }
345 - if (uri_protocols) {
346 - for (i = 0; i < uri_protocols->nr; i++)
347 - strvec_pushf(&pack_objects.args, "--uri-protocol=%s",
348 - uri_protocols->items[i].string);
313 + for_each_commit_graft(append_one_shallow, &opts.shallows);
314 + opts.shallow = 1;
349 315 }
350 -
351 - pack_objects.in = -1;
352 - pack_objects.out = -1;
353 - pack_objects.err = -1;
354 - pack_objects.clean_on_exit = 1;
355 -
356 - if (start_command(&pack_objects))
357 - die("git upload-pack: unable to fork git-pack-objects");
358 -
359 - pipe_fd = xfdopen(pack_objects.in, "w");
360 -
361 - if (pack_data->shallow_nr)
362 - for_each_commit_graft(write_one_shallow, pipe_fd);
363 -
364 316 for (i = 0; i < pack_data->want_obj.nr; i++)
365 - fprintf(pipe_fd, "%s\n",
366 - oid_to_hex(&pack_data->want_obj.objects[i].item->oid));
367 - fprintf(pipe_fd, "--not\n");
317 + oid_array_append(&opts.wants,
318 + &pack_data->want_obj.objects[i].item->oid);
368 319 for (i = 0; i < pack_data->have_obj.nr; i++)
369 - fprintf(pipe_fd, "%s\n",
370 - oid_to_hex(&pack_data->have_obj.objects[i].item->oid));
320 + oid_array_append(&opts.haves,
321 + &pack_data->have_obj.objects[i].item->oid);
371 322 for (i = 0; i < pack_data->extra_edge_obj.nr; i++)
372 - fprintf(pipe_fd, "%s\n",
373 - oid_to_hex(&pack_data->extra_edge_obj.objects[i].item->oid));
374 - fprintf(pipe_fd, "\n");
375 - fflush(pipe_fd);
376 - fclose(pipe_fd);
377 -
378 - /* We read from pack_objects.err to capture stderr output for
379 - * progress bar, and pack_objects.out to capture the pack data.
380 - */
323 + oid_array_append(&opts.haves,
324 + &pack_data->extra_edge_obj.objects[i].item->oid);
381 325
326 + opts.thin = pack_data->use_thin_pack;
327 + if (!pack_data->no_progress)
328 + opts.progress = ODB_GENERATE_PACK_PROGRESS_STANDARD;
329 + opts.ofs_delta = pack_data->use_ofs_delta;
330 + opts.include_tag = pack_data->use_include_tag;
331 + opts.missing_allow_promisor = repo_has_accepted_promisor_remote(the_repository);
332 + if (pack_data->filter_options.choice)
333 + opts.filter_spec = expand_list_objects_filter_spec(&pack_data->filter_options);
334 + opts.uri_protocols = uri_protocols;
335 + opts.pack_objects_hook = pack_data->pack_objects_hook;
336 + opts.pack_fd = -1;
337 + opts.progress_fd = -1;
338 +
339 + if (odb_generate_pack(the_repository->objects, &generator, &opts))
340 + die("git upload-pack: unable to generate pack");
341 + odb_generate_pack_options_release(&opts);
342 +
343 + /*
344 + * We read from generator->err to capture stderr output for the
345 + * progress bar, and generator->out to capture the pack data.
346 + */
382 347 while (1) {
383 348 uint64_t now_ms = getnanotime() / 1000000;
384 349 struct pollfd pfd[2];
@@ -393,14 +358,14 @@ static void create_pack_file(struct upload_pack_data *pack_data,
393 358 pollsize = 0;
394 359 pe = pu = -1;
395 360
396 - if (0 <= pack_objects.out) {
397 - pfd[pollsize].fd = pack_objects.out;
361 + if (0 <= generator->out) {
362 + pfd[pollsize].fd = generator->out;
398 363 pfd[pollsize].events = POLLIN;
399 364 pu = pollsize;
400 365 pollsize++;
401 366 }
402 - if (0 <= pack_objects.err) {
403 - pfd[pollsize].fd = pack_objects.err;
367 + if (0 <= generator->err) {
368 + pfd[pollsize].fd = generator->err;
404 369 pfd[pollsize].events = POLLIN;
405 370 pe = pollsize;
406 371 pollsize++;
@@ -437,15 +402,15 @@ static void create_pack_file(struct upload_pack_data *pack_data,
437 402 /* Status ready; we ship that in the side-band
438 403 * or dump to the standard error.
439 404 */
440 - sz = xread(pack_objects.err, progress,
405 + sz = xread(generator->err, progress,
441 406 sizeof(progress));
442 407 if (0 < sz) {
443 408 send_client_data(2, progress, sz,
444 409 pack_data->use_sideband);
445 410 last_sent_ms = now_ms;
446 411 } else if (sz == 0) {
447 - close(pack_objects.err);
448 - pack_objects.err = -1;
412 + close(generator->err);
413 + generator->err = -1;
449 414 }
450 415 else
451 416 goto fail;
@@ -455,15 +420,15 @@ static void create_pack_file(struct upload_pack_data *pack_data,
455 420
456 421 if (0 <= pu && (pfd[pu].revents & (POLLIN|POLLHUP))) {
457 422 bool did_send_data;
458 - int result = relay_pack_data(pack_objects.out,
423 + int result = relay_pack_data(generator->out,
459 424 output_state,
460 425 pack_data->use_sideband,
461 426 !!uri_protocols,
462 427 &did_send_data);
463 428
464 429 if (result == 0) {
465 - close(pack_objects.out);
466 - pack_objects.out = -1;
430 + close(generator->out);
431 + generator->out = -1;
467 432 } else if (result < 0) {
468 433 goto fail;
469 434 }
@@ -498,7 +463,7 @@ static void create_pack_file(struct upload_pack_data *pack_data,
498 463 }
499 464 }
500 465
501 - if (finish_command(&pack_objects)) {
466 + if (odb_pack_generator_finish(generator)) {
502 467 error("git upload-pack: git-pack-objects died with error.");
503 468 goto fail;
504 469 }