diff options
274 files changed, 7542 insertions, 3365 deletions
diff --git a/.github/workflows/check-whitespace.yml b/.github/workflows/check-whitespace.yml index a241a63..d0a78fc 100644 --- a/.github/workflows/check-whitespace.yml +++ b/.github/workflows/check-whitespace.yml @@ -26,66 +26,7 @@ jobs: - name: git log --check id: check_out run: | - baseSha=${{github.event.pull_request.base.sha}} - problems=() - commit= - commitText= - commitTextmd= - goodparent= - while read dash sha etc - do - case "${dash}" in - "---") - if test -z "${commit}" - then - goodparent=${sha} - fi - commit="${sha}" - commitText="${sha} ${etc}" - commitTextmd="[${sha}](https://github.com/${{ github.repository }}/commit/${sha}) ${etc}" - ;; - "") - ;; - *) - if test -n "${commit}" - then - problems+=("1) --- ${commitTextmd}") - echo "" - echo "--- ${commitText}" - commit= - fi - case "${dash}" in - *:[1-9]*:) # contains file and line number information - dashend=${dash#*:} - problems+=("[${dash}](https://github.com/${{ github.repository }}/blob/${{github.event.pull_request.head.ref}}/${dash%%:*}#L${dashend%:}) ${sha} ${etc}") - ;; - *) - problems+=("\`${dash} ${sha} ${etc}\`") - ;; - esac - echo "${dash} ${sha} ${etc}" - ;; - esac - done <<< $(git log --check --pretty=format:"---% h% s" ${baseSha}..) - - if test ${#problems[*]} -gt 0 - then - if test -z "${commit}" - then - goodparent=${baseSha: 0:7} - fi - echo "🛑 Please review the Summary output for further information." - echo "### :x: A whitespace issue was found in one or more of the commits." >$GITHUB_STEP_SUMMARY - echo "" >>$GITHUB_STEP_SUMMARY - echo "Run these commands to correct the problem:" >>$GITHUB_STEP_SUMMARY - echo "1. \`git rebase --whitespace=fix ${goodparent}\`" >>$GITHUB_STEP_SUMMARY - echo "1. \`git push --force\`" >>$GITHUB_STEP_SUMMARY - echo " " >>$GITHUB_STEP_SUMMARY - echo "Errors:" >>$GITHUB_STEP_SUMMARY - for i in "${problems[@]}" - do - echo "${i}" >>$GITHUB_STEP_SUMMARY - done - - exit 2 - fi + ./ci/check-whitespace.sh \ + "${{github.event.pull_request.base.sha}}" \ + "$GITHUB_STEP_SUMMARY" \ + "https://github.com/${{github.repository}}" diff --git a/.github/workflows/coverity.yml b/.github/workflows/coverity.yml index 53cf12f..48341e8 100644 --- a/.github/workflows/coverity.yml +++ b/.github/workflows/coverity.yml @@ -45,7 +45,7 @@ jobs: - run: ci/install-dependencies.sh if: contains(matrix.os, 'ubuntu') || contains(matrix.os, 'macos') env: - runs_on_pool: ${{ matrix.os }} + distro: ${{ matrix.os }} # The Coverity site says the tool is usually updated twice yearly, so the # MD5 of download can be used to determine whether there's been an update. diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5838986..13cc0fe 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -284,8 +284,7 @@ jobs: cc: clang pool: macos-13 - jobname: osx-gcc - cc: gcc - cc_package: gcc-13 + cc: gcc-13 pool: macos-13 - jobname: linux-gcc-default cc: gcc diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ba65f50..f676959 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -95,6 +95,15 @@ test:osx: - t/failed-test-artifacts when: on_failure +test:fuzz-smoke-tests: + image: ubuntu:latest + variables: + CC: clang + before_script: + - ./ci/install-dependencies.sh + script: + - ./ci/run-build-and-minimal-fuzzers.sh + static-analysis: image: ubuntu:22.04 variables: @@ -104,3 +113,12 @@ static-analysis: script: - ./ci/run-static-analysis.sh - ./ci/check-directional-formatting.bash + +check-whitespace: + image: ubuntu:latest + before_script: + - ./ci/install-dependencies.sh + script: + - ./ci/check-whitespace.sh "$CI_MERGE_REQUEST_TARGET_BRANCH_SHA" + rules: + - if: $CI_PIPELINE_SOURCE == 'merge_request_event' diff --git a/Documentation/DecisionMaking.txt b/Documentation/DecisionMaking.txt new file mode 100644 index 0000000..dbb4c1f --- /dev/null +++ b/Documentation/DecisionMaking.txt @@ -0,0 +1,74 @@ +Decision-Making Process in the Git Project +========================================== + +Introduction +------------ +This document describes the current decision-making process in the Git +project. It is a descriptive rather than prescriptive doc; that is, we want to +describe how things work in practice rather than explicitly recommending any +particular process or changes to the current process. + +Here we document how the project makes decisions for discussions +(with or without patches), in scale larger than an individual patch +series (which is fully covered by the SubmittingPatches document). + + +Larger Discussions (with patches) +--------------------------------- +As with discussions on an individual patch series, starting a larger-scale +discussion often begins by sending a patch or series to the list. This might +take the form of an initial design doc, with implementation following in later +iterations of the series (for example, +link:https://lore.kernel.org/git/0169ce6fb9ccafc089b74ae406db0d1a8ff8ac65.1688165272.git.steadmon@google.com/[adding unit tests] or +link:https://lore.kernel.org/git/20200420235310.94493-1-emilyshaffer@google.com/[config-based hooks]), +or it might include a full implementation from the beginning. +In either case, discussion progresses the same way for an individual patch series, +until consensus is reached or the topic is dropped. + + +Larger Discussions (without patches) +------------------------------------ +Occasionally, larger discussions might occur without an associated patch series. +These may be very large-scale technical decisions that are beyond the scope of +even a single large patch series, or they may be more open-ended, +policy-oriented discussions (examples: +link:https://lore.kernel.org/git/ZZ77NQkSuiRxRDwt@nand.local/[introducing Rust] +or link:https://lore.kernel.org/git/YHofmWcIAidkvJiD@google.com/[improving submodule UX]). +In either case, discussion progresses as described above for general patch series. + +For larger discussions without a patch series or other concrete implementation, +it may be hard to judge when consensus has been reached, as there are not any +official guidelines. If discussion stalls at this point, it may be helpful to +restart discussion with an RFC patch series (such as a partial, unfinished +implementation or proof of concept) that can be more easily debated. + +When consensus is reached that it is a good idea, the original +proposer is expected to coordinate the effort to make it happen, +with help from others who were involved in the discussion, as +needed. + +For decisions that require code changes, it is often the case that the original +proposer will follow up with a patch series, although it is also common for +other interested parties to provide an implementation (or parts of the +implementation, for very large changes). + +For non-technical decisions such as community norms or processes, it is up to +the community as a whole to implement and sustain agreed-upon changes. +The project leadership committe (PLC) may help the implementation of +policy decisions. + + +Other Discussion Venues +----------------------- +Occasionally decision proposals are presented off-list, e.g. at the semi-regular +Contributors' Summit. While higher-bandwidth face-to-face discussion is often +useful for quickly reaching consensus among attendees, generally we expect to +summarize the discussion in notes that can later be presented on-list. For an +example, see the thread +link:https://lore.kernel.org/git/AC2EB721-2979-43FD-922D-C5076A57F24B@jramsay.com.au/[Notes +from Git Contributor Summit, Los Angeles (April 5, 2020)] by James Ramsay. + +We prefer that "official" discussion happens on the list so that the full +community has opportunity to engage in discussion. This also means that the +mailing list archives contain a more-or-less complete history of project +discussions and decisions. diff --git a/Documentation/Makefile b/Documentation/Makefile index 3f2383a..a04da67 100644 --- a/Documentation/Makefile +++ b/Documentation/Makefile @@ -103,6 +103,7 @@ SP_ARTICLES += howto/coordinate-embargoed-releases API_DOCS = $(patsubst %.txt,%,$(filter-out technical/api-index-skel.txt technical/api-index.txt, $(wildcard technical/api-*.txt))) SP_ARTICLES += $(API_DOCS) +TECH_DOCS += DecisionMaking TECH_DOCS += ReviewingGuidelines TECH_DOCS += MyFirstContribution TECH_DOCS += MyFirstObjectWalk diff --git a/Documentation/RelNotes/2.39.4.txt b/Documentation/RelNotes/2.39.4.txt new file mode 100644 index 0000000..7f54521 --- /dev/null +++ b/Documentation/RelNotes/2.39.4.txt @@ -0,0 +1,79 @@ +Git v2.39.4 Release Notes +========================= + +This addresses the security issues CVE-2024-32002, CVE-2024-32004, +CVE-2024-32020 and CVE-2024-32021. + +This release also backports fixes necessary to let the CI builds pass +successfully. + +Fixes since v2.39.3 +------------------- + + * CVE-2024-32002: + + Recursive clones on case-insensitive filesystems that support symbolic + links are susceptible to case confusion that can be exploited to + execute just-cloned code during the clone operation. + + * CVE-2024-32004: + + Repositories can be configured to execute arbitrary code during local + clones. To address this, the ownership checks introduced in v2.30.3 + are now extended to cover cloning local repositories. + + * CVE-2024-32020: + + Local clones may end up hardlinking files into the target repository's + object database when source and target repository reside on the same + disk. If the source repository is owned by a different user, then + those hardlinked files may be rewritten at any point in time by the + untrusted user. + + * CVE-2024-32021: + + When cloning a local source repository that contains symlinks via the + filesystem, Git may create hardlinks to arbitrary user-readable files + on the same filesystem as the target repository in the objects/ + directory. + + * CVE-2024-32465: + + It is supposed to be safe to clone untrusted repositories, even those + unpacked from zip archives or tarballs originating from untrusted + sources, but Git can be tricked to run arbitrary code as part of the + clone. + + * Defense-in-depth: submodule: require the submodule path to contain + directories only. + + * Defense-in-depth: clone: when symbolic links collide with directories, keep + the latter. + + * Defense-in-depth: clone: prevent hooks from running during a clone. + + * Defense-in-depth: core.hooksPath: add some protection while cloning. + + * Defense-in-depth: fsck: warn about symlink pointing inside a gitdir. + + * Various fix-ups on HTTP tests. + + * Test update. + + * HTTP Header redaction code has been adjusted for a newer version of + cURL library that shows its traces differently from earlier + versions. + + * Fix was added to work around a regression in libcURL 8.7.0 (which has + already been fixed in their tip of the tree). + + * Replace macos-12 used at GitHub CI with macos-13. + + * ci(linux-asan/linux-ubsan): let's save some time + + * Tests with LSan from time to time seem to emit harmless message that makes + our tests unnecessarily flakey; we work it around by filtering the + uninteresting output. + + * Update GitHub Actions jobs to avoid warnings against using deprecated + version of Node.js. diff --git a/Documentation/RelNotes/2.39.5.txt b/Documentation/RelNotes/2.39.5.txt new file mode 100644 index 0000000..97c0185 --- /dev/null +++ b/Documentation/RelNotes/2.39.5.txt @@ -0,0 +1,26 @@ +Git v2.39.5 Release Notes +========================= + +In preparing security fixes for four CVEs, we made overly aggressive +"defense in depth" changes that broke legitimate use cases like 'git +lfs' and 'git annex.' This release is to revert these misguided, if +well-intentioned, changes that were shipped in 2.39.4 and were not +direct security fixes. + +Jeff King (5): + send-email: drop FakeTerm hack + send-email: avoid creating more than one Term::ReadLine object + ci: drop mention of BREW_INSTALL_PACKAGES variable + ci: avoid bare "gcc" for osx-gcc job + ci: stop installing "gcc-13" for osx-gcc + +Johannes Schindelin (6): + hook: plug a new memory leak + init: use the correct path of the templates directory again + Revert "core.hooksPath: add some protection while cloning" + tests: verify that `clone -c core.hooksPath=/dev/null` works again + clone: drop the protections where hooks aren't run + Revert "Add a helper function to compare file contents" + +Junio C Hamano (1): + Revert "fsck: warn about symlink pointing inside a gitdir" diff --git a/Documentation/RelNotes/2.40.2.txt b/Documentation/RelNotes/2.40.2.txt new file mode 100644 index 0000000..646a2cc --- /dev/null +++ b/Documentation/RelNotes/2.40.2.txt @@ -0,0 +1,7 @@ +Git v2.40.2 Release Notes +========================= + +This release merges up the fix that appears in v2.39.4 to address +the security issues CVE-2024-32002, CVE-2024-32004, CVE-2024-32020, +CVE-2024-32021 and CVE-2024-32465; see the release notes for that +version for details. diff --git a/Documentation/RelNotes/2.40.3.txt b/Documentation/RelNotes/2.40.3.txt new file mode 100644 index 0000000..6ca088e --- /dev/null +++ b/Documentation/RelNotes/2.40.3.txt @@ -0,0 +1,26 @@ +Git v2.40.3 Release Notes +========================= + +In preparing security fixes for four CVEs, we made overly aggressive +"defense in depth" changes that broke legitimate use cases like 'git +lfs' and 'git annex.' This release is to revert these misguided, if +well-intentioned, changes that were shipped in 2.40.2 and were not +direct security fixes. + +Jeff King (5): + send-email: drop FakeTerm hack + send-email: avoid creating more than one Term::ReadLine object + ci: drop mention of BREW_INSTALL_PACKAGES variable + ci: avoid bare "gcc" for osx-gcc job + ci: stop installing "gcc-13" for osx-gcc + +Johannes Schindelin (6): + hook: plug a new memory leak + init: use the correct path of the templates directory again + Revert "core.hooksPath: add some protection while cloning" + tests: verify that `clone -c core.hooksPath=/dev/null` works again + clone: drop the protections where hooks aren't run + Revert "Add a helper function to compare file contents" + +Junio C Hamano (1): + Revert "fsck: warn about symlink pointing inside a gitdir" diff --git a/Documentation/RelNotes/2.41.1.txt b/Documentation/RelNotes/2.41.1.txt new file mode 100644 index 0000000..9fb4c21 --- /dev/null +++ b/Documentation/RelNotes/2.41.1.txt @@ -0,0 +1,7 @@ +Git v2.41.1 Release Notes +========================= + +This release merges up the fix that appears in v2.39.4 and v2.40.2 +to address the security issues CVE-2024-32002, CVE-2024-32004, +CVE-2024-32020, CVE-2024-32021 and CVE-2024-32465; see the release +notes for these versions for details. diff --git a/Documentation/RelNotes/2.41.2.txt b/Documentation/RelNotes/2.41.2.txt new file mode 100644 index 0000000..f94afde --- /dev/null +++ b/Documentation/RelNotes/2.41.2.txt @@ -0,0 +1,26 @@ +Git v2.41.2 Release Notes +========================= + +In preparing security fixes for four CVEs, we made overly aggressive +"defense in depth" changes that broke legitimate use cases like 'git +lfs' and 'git annex.' This release is to revert these misguided, if +well-intentioned, changes that were shipped in 2.41.1 and were not +direct security fixes. + +Jeff King (5): + send-email: drop FakeTerm hack + send-email: avoid creating more than one Term::ReadLine object + ci: drop mention of BREW_INSTALL_PACKAGES variable + ci: avoid bare "gcc" for osx-gcc job + ci: stop installing "gcc-13" for osx-gcc + +Johannes Schindelin (6): + hook: plug a new memory leak + init: use the correct path of the templates directory again + Revert "core.hooksPath: add some protection while cloning" + tests: verify that `clone -c core.hooksPath=/dev/null` works again + clone: drop the protections where hooks aren't run + Revert "Add a helper function to compare file contents" + +Junio C Hamano (1): + Revert "fsck: warn about symlink pointing inside a gitdir" diff --git a/Documentation/RelNotes/2.42.2.txt b/Documentation/RelNotes/2.42.2.txt new file mode 100644 index 0000000..dbf761a --- /dev/null +++ b/Documentation/RelNotes/2.42.2.txt @@ -0,0 +1,7 @@ +Git v2.42.2 Release Notes +========================= + +This release merges up the fix that appears in v2.39.4, v2.40.2 +and v2.41.1 to address the security issues CVE-2024-32002, +CVE-2024-32004, CVE-2024-32020, CVE-2024-32021 and CVE-2024-32465; +see the release notes for these versions for details. diff --git a/Documentation/RelNotes/2.42.3.txt b/Documentation/RelNotes/2.42.3.txt new file mode 100644 index 0000000..bfe3ba5 --- /dev/null +++ b/Documentation/RelNotes/2.42.3.txt @@ -0,0 +1,26 @@ +Git v2.42.3 Release Notes +========================= + +In preparing security fixes for four CVEs, we made overly aggressive +"defense in depth" changes that broke legitimate use cases like 'git +lfs' and 'git annex.' This release is to revert these misguided, if +well-intentioned, changes that were shipped in 2.42.2 and were not +direct security fixes. + +Jeff King (5): + send-email: drop FakeTerm hack + send-email: avoid creating more than one Term::ReadLine object + ci: drop mention of BREW_INSTALL_PACKAGES variable + ci: avoid bare "gcc" for osx-gcc job + ci: stop installing "gcc-13" for osx-gcc + +Johannes Schindelin (6): + hook: plug a new memory leak + init: use the correct path of the templates directory again + Revert "core.hooksPath: add some protection while cloning" + tests: verify that `clone -c core.hooksPath=/dev/null` works again + clone: drop the protections where hooks aren't run + Revert "Add a helper function to compare file contents" + +Junio C Hamano (1): + Revert "fsck: warn about symlink pointing inside a gitdir" diff --git a/Documentation/RelNotes/2.43.4.txt b/Documentation/RelNotes/2.43.4.txt new file mode 100644 index 0000000..0a84251 --- /dev/null +++ b/Documentation/RelNotes/2.43.4.txt @@ -0,0 +1,7 @@ +Git v2.43.4 Release Notes +========================= + +This release merges up the fix that appears in v2.39.4, v2.40.2, +v2.41.1 and v2.42.2 to address the security issues CVE-2024-32002, +CVE-2024-32004, CVE-2024-32020, CVE-2024-32021 and CVE-2024-32465; +see the release notes for these versions for details. diff --git a/Documentation/RelNotes/2.43.5.txt b/Documentation/RelNotes/2.43.5.txt new file mode 100644 index 0000000..236b234 --- /dev/null +++ b/Documentation/RelNotes/2.43.5.txt @@ -0,0 +1,26 @@ +Git v2.43.5 Release Notes +========================= + +In preparing security fixes for four CVEs, we made overly aggressive +"defense in depth" changes that broke legitimate use cases like 'git +lfs' and 'git annex.' This release is to revert these misguided, if +well-intentioned, changes that were shipped in 2.43.4 and were not +direct security fixes. + +Jeff King (5): + send-email: drop FakeTerm hack + send-email: avoid creating more than one Term::ReadLine object + ci: drop mention of BREW_INSTALL_PACKAGES variable + ci: avoid bare "gcc" for osx-gcc job + ci: stop installing "gcc-13" for osx-gcc + +Johannes Schindelin (6): + hook: plug a new memory leak + init: use the correct path of the templates directory again + Revert "core.hooksPath: add some protection while cloning" + tests: verify that `clone -c core.hooksPath=/dev/null` works again + clone: drop the protections where hooks aren't run + Revert "Add a helper function to compare file contents" + +Junio C Hamano (1): + Revert "fsck: warn about symlink pointing inside a gitdir" diff --git a/Documentation/RelNotes/2.44.1.txt b/Documentation/RelNotes/2.44.1.txt new file mode 100644 index 0000000..b5135c3 --- /dev/null +++ b/Documentation/RelNotes/2.44.1.txt @@ -0,0 +1,8 @@ +Git v2.44.1 Release Notes +========================= + +This release merges up the fix that appears in v2.39.4, v2.40.2, +v2.41.1, v2.42.2 and v2.43.4 to address the security issues +CVE-2024-32002, CVE-2024-32004, CVE-2024-32020, CVE-2024-32021 +and CVE-2024-32465; see the release notes for these versions +for details. diff --git a/Documentation/RelNotes/2.44.2.txt b/Documentation/RelNotes/2.44.2.txt new file mode 100644 index 0000000..76700f0 --- /dev/null +++ b/Documentation/RelNotes/2.44.2.txt @@ -0,0 +1,26 @@ +Git v2.44.2 Release Notes +========================= + +In preparing security fixes for four CVEs, we made overly aggressive +"defense in depth" changes that broke legitimate use cases like 'git +lfs' and 'git annex.' This release is to revert these misguided, if +well-intentioned, changes that were shipped in 2.44.1 and were not +direct security fixes. + +Jeff King (5): + send-email: drop FakeTerm hack + send-email: avoid creating more than one Term::ReadLine object + ci: drop mention of BREW_INSTALL_PACKAGES variable + ci: avoid bare "gcc" for osx-gcc job + ci: stop installing "gcc-13" for osx-gcc + +Johannes Schindelin (6): + hook: plug a new memory leak + init: use the correct path of the templates directory again + Revert "core.hooksPath: add some protection while cloning" + tests: verify that `clone -c core.hooksPath=/dev/null` works again + clone: drop the protections where hooks aren't run + Revert "Add a helper function to compare file contents" + +Junio C Hamano (1): + Revert "fsck: warn about symlink pointing inside a gitdir" diff --git a/Documentation/RelNotes/2.45.1.txt b/Documentation/RelNotes/2.45.1.txt new file mode 100644 index 0000000..3b0d60c --- /dev/null +++ b/Documentation/RelNotes/2.45.1.txt @@ -0,0 +1,8 @@ +Git v2.45.1 Release Notes +========================= + +This release merges up the fix that appears in v2.39.4, +v2.40.2, v2.41.1, v2.42.2, v2.43.4 and v2.44.1 to address the +security issues CVE-2024-32002, CVE-2024-32004, CVE-2024-32020, +CVE-2024-32021 and CVE-2024-32465; see the release notes for +these versions for details. diff --git a/Documentation/RelNotes/2.45.2.txt b/Documentation/RelNotes/2.45.2.txt new file mode 100644 index 0000000..13429e6 --- /dev/null +++ b/Documentation/RelNotes/2.45.2.txt @@ -0,0 +1,26 @@ +Git v2.45.2 Release Notes +========================= + +In preparing security fixes for four CVEs, we made overly aggressive +"defense in depth" changes that broke legitimate use cases like 'git +lfs' and 'git annex.' This release is to revert these misguided, if +well-intentioned, changes that were shipped in 2.45.1 and were not +direct security fixes. + +Jeff King (5): + send-email: drop FakeTerm hack + send-email: avoid creating more than one Term::ReadLine object + ci: drop mention of BREW_INSTALL_PACKAGES variable + ci: avoid bare "gcc" for osx-gcc job + ci: stop installing "gcc-13" for osx-gcc + +Johannes Schindelin (6): + hook: plug a new memory leak + init: use the correct path of the templates directory again + Revert "core.hooksPath: add some protection while cloning" + tests: verify that `clone -c core.hooksPath=/dev/null` works again + clone: drop the protections where hooks aren't run + Revert "Add a helper function to compare file contents" + +Junio C Hamano (1): + Revert "fsck: warn about symlink pointing inside a gitdir" diff --git a/Documentation/RelNotes/2.46.0.txt b/Documentation/RelNotes/2.46.0.txt index 5838476..d2843aa 100644 --- a/Documentation/RelNotes/2.46.0.txt +++ b/Documentation/RelNotes/2.46.0.txt @@ -10,7 +10,47 @@ UI, Workflows & Features * The "--rfc" option of "git format-patch" learned to take an optional string value to be used in place of "RFC" to tweak the "[PATCH]" on the subject header. - (merge ce36894509 jc/format-patch-rfc-more later to maint). + + * The credential helper protocol, together with the HTTP layer, have + been enhanced to support authentication schemes different from + username & password pair, like Bearer and NTLM. + + * Command line completion script (in contrib/) learned to complete + "git symbolic-ref" a bit better (you need to enable plumbing + commands to be completed with GIT_COMPLETION_SHOW_ALL_COMMANDS). + + * When the user responds to a prompt given by "git add -p" with an + unsupported command, list of available commands were given, which + was too much if the user knew what they wanted to type but merely + made a typo. Now the user gets a much shorter error message. + + * The color parsing code learned to handle 12-bit RGB colors, spelled + as "#RGB" (in addition to "#RRGGBB" that is already supported). + + * The operation mode options (like "--get") the "git config" command + uses have been deprecated and replaced with subcommands (like "git + config get"). + + * "git tag" learned the "--trailer" option to futz with the trailers + in the same way as "git commit" does. + + * A new global "--no-advice" option can be used to disable all advice + messages, which is meant to be used only in scripts. + + * Updates to symbolic refs can now be made as a part of ref + transaction. + + * The trailer API has been reshuffled a bit. + + * Terminology to call various ref-like things are getting + straightened out. + + * The command line completion script (in contrib/) has been adjusted + to the recent update to "git config" that adopted subcommand based + UI. + + * The knobs to tweak how reftable files are written have been made + available as configuration variables. Performance, Internal Implementation, Development Support etc. @@ -23,6 +63,52 @@ Performance, Internal Implementation, Development Support etc. that are used in fuzzer tests, to make sure at least they build without bitrot, in Linux CI runs. + * Code to write out reftable has seen some optimization and + simplification. + + * Tests to ensure interoperability between reftable written by jgit + and our code have been added and enabled in CI. + + * The singleton index_state instance "the_index" has been eliminated + by always instantiating "the_repository" and replacing references + to "the_index" with references to its .index member. + + * Git-GUI has a new maintainer, Johannes Sixt. + (merge e18ad8eb26 jc/git-gui-maintainer-update later to maint). + + * The "test-tool" has been taught to run testsuite tests in parallel, + bypassing the need to use the "prove" tool. + + * The "whitespace check" task that was enabled for GitHub Actions CI + has been ported to GitLab CI. + + * The refs API lost functions that implicitly assumes to work on the + primary ref_store by forcing the callers to pass a ref_store as an + argument. + + * Code clean-up to reduce inter-function communication inside + builtin/config.c done via the use of global variables. + + * The pack bitmap code saw some clean-up to prepare for a follow-up topic. + + * Preliminary code clean-up for "git send-email". + + * The default "creation-factor" used by "git format-patch" has been + raised to make it more aggressively find matching commits. + + * Before discovering the repository details, We used to assume SHA-1 + as the "default" hash function, which has been corrected. Hopefully + this will smoke out codepaths that rely on such an unwarranted + assumptions. + + * The project decision making policy has been documented. + + * The strcmp-offset tests have been rewritten using the unit test + framework. + + * "git add -p" learned to complain when an answer with more than one + letter is given to a prompt that expects a single letter answer. + Fixes since v2.45 ----------------- @@ -52,4 +138,79 @@ Fixes since v2.45 errored out. Now it keeps going. (merge c75662bfc9 js/for-each-repo-keep-going later to maint). + * zsh can pretend to be a normal shell pretty well except for some + glitches that we tickle in some of our scripts. Work them around + so that "vimdiff" and our test suite works well enough with it. + (merge fedd5c79ff bc/zsh-compatibility later to maint). + + * Command line completion support for zsh (in contrib/) has been + updated to stop exposing internal state to end-user shell + interaction. + (merge 3c20acdf46 dk/zsh-git-repo-path-fix later to maint). + + * Tests that try to corrupt in-repository files in chunked format did + not work well on macOS due to its broken "mv", which has been + worked around. + (merge 861dc19ba8 jc/test-workaround-broken-mv later to maint). + + * The maximum size of attribute files is enforced more consistently. + (merge c793f9cb08 tb/attr-limits later to maint). + + * Unbreak CI jobs so that we do not attempt to use Python 2 that has + been removed from the platform. + (merge 5ca0c455f1 ps/ci-python-2-deprecation later to maint). + + * Git 2.43 started using the tree of HEAD as the source of attributes + in a bare repository, which has severe performance implications. + For now, revert the change, without ripping out a more explicit + support for the attr.tree configuration variable. + (merge 51441e6460 jc/no-default-attr-tree-in-bare later to maint). + + * The "--exit-code" option of "git diff" command learned to work with + the "--ext-diff" option. + (merge 11be65cfa4 rs/external-diff-with-exit-code later to maint). + + * Windows CI running in GitHub Actions started complaining about the + order of arguments given to calloc(); the imported regex code uses + the wrong order almost consistently, which has been corrected. + (merge f01301aabe jc/compat-regex-calloc-fix later to maint). + + * Expose "name conflict" error when a ref creation fails due to D/F + conflict in the ref namespace, to improve an error message given by + "git fetch". + (merge 9339fca23e it/refs-name-conflict later to maint). + + * The SubmittingPatches document now refers folks to manpages + translation project. + (merge 558a5b8cd0 jc/doc-manpages-l10n later to maint). + + * The documentation for "git diff --name-only" has been clarified + that it is about showing the names in the post-image tree. + (merge 4986662cbc jc/doc-diff-name-only later to maint). + + * The credential helper that talks with osx keychain learned to avoid + storing back the authentication material it just got received from + the keychain. + (merge e1ab45b2da kn/osxkeychain-skip-idempotent-store later to maint). + + * The chainlint script (invoked during "make test") did nothing when + it failed to detect the number of available CPUs. It now falls + back to 1 CPU to avoid the problem. + (merge 2e7e9205be es/chainlint-ncores-fix later to maint). + + * Revert overly aggressive "layered defence" that went into 2.45.1 + and friends, which broke "git-lfs", "git-annex", and other use + cases, so that we can rebuild necessary counterparts in the open. + * Other code cleanup, docfix, build fix, etc. + (merge 4cf6e7bf5e jt/doc-submitting-rerolled-series later to maint). + (merge a5a4cb7b27 rs/diff-parseopts-cleanup later to maint). + (merge 395c130fd8 ma/win32-unix-domain-socket later to maint). + (merge 7df2405b38 jk/ci-macos-gcc13-fix later to maint). + (merge 55702c543e fa/p4-error later to maint). + (merge 2566a77774 vd/doc-merge-tree-x-option later to maint). + (merge b64b0df9da ds/scalar-reconfigure-all-fix later to maint). + (merge c81ffcff83 dm/update-index-doc-fix later to maint). + (merge fc0202b0e9 dg/fetch-pack-code-cleanup later to maint). + (merge 7150f140f9 mt/t0211-typofix later to maint). + (merge d424488901 jc/rev-parse-fatal-doc later to maint). diff --git a/Documentation/SubmittingPatches b/Documentation/SubmittingPatches index 384893b..d8a8caa 100644 --- a/Documentation/SubmittingPatches +++ b/Documentation/SubmittingPatches @@ -7,6 +7,73 @@ Here are some guidelines for contributing back to this project. There is also a link:MyFirstContribution.html[step-by-step tutorial] available which covers many of these same guidelines. +[[patch-flow]] +=== A typical life cycle of a patch series + +To help us understand the reason behind various guidelines given later +in the document, first let's understand how the life cycle of a +typical patch series for this project goes. + +. You come up with an itch. You code it up. You do not need any + pre-authorization from the project to do so. ++ +Your patches will be reviewed by other contributors on the mailing +list, and the reviews will be done to assess the merit of various +things, like the general idea behind your patch (including "is it +solving a problem worth solving in the first place?"), the reason +behind the design of the solution, and the actual implementation. +The guidelines given here are there to help your patches by making +them easier to understand by the reviewers. + +. You send the patches to the list and cc people who may need to know + about the change. Your goal is *not* necessarily to convince others + that what you are building is good. Your goal is to get help in + coming up with a solution for the "itch" that is better than what + you can build alone. ++ +The people who may need to know are the ones who worked on the code +you are touching. These people happen to be the ones who are +most likely to be knowledgeable enough to help you, but +they have no obligation to help you (i.e. you ask them for help, +you don't demand). +git log -p {litdd} _$area_you_are_modifying_+ would +help you find out who they are. + +. You get comments and suggestions for improvements. You may even get + them in an "on top of your change" patch form. You are expected to + respond to them with "Reply-All" on the mailing list, while taking + them into account while preparing an updated set of patches. + +. Polish, refine, and re-send your patches to the list and to the people + who spent their time to improve your patch. Go back to step (2). + +. While the above iterations improve your patches, the maintainer may + pick the patches up from the list and queue them to the `seen` + branch, in order to make it easier for people to play with it + without having to pick up and apply the patches to their trees + themselves. Being in `seen` has no other meaning. Specifically, it + does not mean the patch was "accepted" in any way. + +. When the discussion reaches a consensus that the latest iteration of + the patches are in good enough shape, the maintainer includes the + topic in the "What's cooking" report that are sent out a few times a + week to the mailing list, marked as "Will merge to 'next'." This + decision is primarily made by the maintainer with help from those + who participated in the review discussion. + +. After the patches are merged to the 'next' branch, the discussion + can still continue to further improve them by adding more patches on + top, but by the time a topic gets merged to 'next', it is expected + that everybody agrees that the scope and the basic direction of the + topic are appropriate, so such an incremental updates are limited to + small corrections and polishing. After a topic cooks for some time + (like 7 calendar days) in 'next' without needing further tweaks on + top, it gets merged to the 'master' branch and wait to become part + of the next major release. + +In the following sections, many techniques and conventions are listed +to help your patches get reviewed effectively in such a life cycle. + + [[choose-starting-point]] === Choose a starting point. @@ -192,8 +259,9 @@ reasons: which case, they can explain why they extend your code to cover files, too). -The goal of your log message is to convey the _why_ behind your -change to help future developers. +The goal of your log message is to convey the _why_ behind your change +to help future developers. The reviewers will also make sure that +your proposed log message will serve this purpose well. The first line of the commit message should be a short description (50 characters is the soft limit, see DISCUSSION in linkgit:git-commit[1]), @@ -540,14 +608,93 @@ patch, format it as "multipart/signed", not a text/plain message that starts with `-----BEGIN PGP SIGNED MESSAGE-----`. That is not a text/plain, it's something else. +=== Handling Conflicts and Iterating Patches + +When revising changes made to your patches, it's important to +acknowledge the possibility of conflicts with other ongoing topics. To +navigate these potential conflicts effectively, follow the recommended +steps outlined below: + +. Build on a suitable base branch, see the <<choose-starting-point, section above>>, +and format-patch the series. If you are doing "rebase -i" in-place to +update from the previous round, this will reuse the previous base so +(2) and (3) may become trivial. + +. Find the base of where the last round was queued ++ + $ mine='kn/ref-transaction-symref' + $ git checkout "origin/seen^{/^Merge branch '$mine'}...master" + +. Apply your format-patch result. There are two cases +.. Things apply cleanly and tests fine. Go to (4). +.. Things apply cleanly but does not build or test fails, or things do +not apply cleanly. ++ +In the latter case, you have textual or semantic conflicts coming from +the difference between the old base and the base you used to build in +(1). Identify what caused the breakages (e.g., a topic or two may have +merged since the base used by (2) until the base used by (1)). ++ +Check out the latest 'origin/master' (which may be newer than the base +used by (2)), "merge --no-ff" the topics you newly depend on in there, +and use the result of the merge(s) as the base, rebuild the series and +test again. Run format-patch from the last such merges to the tip of +your topic. If you did ++ + $ git checkout origin/master + $ git merge --no-ff --into-name kn/ref-transaction-symref fo/obar + $ git merge --no-ff --into-name kn/ref-transaction-symref ba/zqux + ... rebuild the topic ... ++ +Then you'd just format your topic above these "preparing the ground" +merges, e.g. ++ + $ git format-patch "HEAD^{/^Merge branch 'ba/zqux'}"..HEAD ++ +Do not forget to write in the cover letter you did this, including the +topics you have in your base on top of 'master'. Then go to (4). + +. Make a trial merge of your topic into 'next' and 'seen', e.g. ++ + $ git checkout --detach 'origin/seen' + $ git revert -m 1 <the merge of the previous iteration into seen> + $ git merge kn/ref-transaction-symref ++ +The "revert" is needed if the previous iteration of your topic is +already in 'seen' (like in this case). You could choose to rebuild +master..origin/seen from scratch while excluding your previous +iteration, which may emulate what happens on the maintainers end more +closely. ++ +This trial merge may conflict. It is primarily to see what conflicts +_other_ topics may have with your topic. In other words, you do not +have to depend on it to make your topic work on 'master'. It may +become the job of the other topic owners to resolve conflicts if your +topic goes to 'next' before theirs. ++ +Make a note on what conflict you saw in the cover letter. You do not +necessarily have to resolve them, but it would be a good opportunity to +learn what others are doing in related areas. ++ + $ git checkout --detach 'origin/next' + $ git merge kn/ref-transaction-symref ++ +This is to see what conflicts your topic has with other topics that are +already cooking. This should not conflict if (3)-2 prepared a base on +top of updated master plus dependent topics taken from 'next'. Unless +the context is severe (one way to tell is try the same trial merge with +your old iteration, which may conflict in a similar way), expect that it +will be handled on maintainers end (if it gets unmanageable, I'll ask to +rebase when I receive your patches). + == Subsystems with dedicated maintainers Some parts of the system have dedicated maintainers with their own repositories. -- `git-gui/` comes from git-gui project, maintained by Pratyush Yadav: +- `git-gui/` comes from git-gui project, maintained by Johannes Sixt: - https://github.com/prati0100/git-gui.git + https://github.com/j6t/git-gui - `gitk-git/` comes from Paul Mackerras's gitk project: @@ -562,54 +709,12 @@ repositories. Patches to these parts should be based on their trees. -[[patch-flow]] -== An ideal patch flow - -Here is an ideal patch flow for this project the current maintainer -suggests to the contributors: - -. You come up with an itch. You code it up. - -. Send it to the list and cc people who may need to know about - the change. -+ -The people who may need to know are the ones whose code you -are butchering. These people happen to be the ones who are -most likely to be knowledgeable enough to help you, but -they have no obligation to help you (i.e. you ask for help, -don't demand). +git log -p {litdd} _$area_you_are_modifying_+ would -help you find out who they are. - -. You get comments and suggestions for improvements. You may - even get them in an "on top of your change" patch form. - -. Polish, refine, and re-send to the list and the people who - spend their time to improve your patch. Go back to step (2). - -. The list forms consensus that the last round of your patch is - good. Send it to the maintainer and cc the list. - -. A topic branch is created with the patch and is merged to `next`, - and cooked further and eventually graduates to `master`. - -In any time between the (2)-(3) cycle, the maintainer may pick it up -from the list and queue it to `seen`, in order to make it easier for -people to play with it without having to pick up and apply the patch to -their trees themselves. - -[[patch-status]] -== Know the status of your patch after submission +- The "Git documentation translations" project, led by Jean-Noël + Avila, translates our documentation pages. Their work products are + maintained separately from this project, not as part of our tree: -* You can use Git itself to find out when your patch is merged in - master. `git pull --rebase` will automatically skip already-applied - patches, and will let you know. This works only if you rebase on top - of the branch in which your patch has been merged (i.e. it will not - tell you if your patch is merged in `seen` if you rebase on top of - master). + https://github.com/jnavila/git-manpages-l10n/ -* Read the Git mailing list, the maintainer regularly posts messages - entitled "What's cooking in git.git" giving - the status of various proposed changes. == GitHub CI[[GHCI]] diff --git a/Documentation/config.txt b/Documentation/config.txt index 6f649c9..cbf0b99 100644 --- a/Documentation/config.txt +++ b/Documentation/config.txt @@ -498,6 +498,8 @@ include::config/rebase.txt[] include::config/receive.txt[] +include::config/reftable.txt[] + include::config/remote.txt[] include::config/remotes.txt[] diff --git a/Documentation/config/reftable.txt b/Documentation/config/reftable.txt new file mode 100644 index 0000000..0515727 --- /dev/null +++ b/Documentation/config/reftable.txt @@ -0,0 +1,48 @@ +reftable.blockSize:: + The size in bytes used by the reftable backend when writing blocks. + The block size is determined by the writer, and does not have to be a + power of 2. The block size must be larger than the longest reference + name or log entry used in the repository, as references cannot span + blocks. ++ +Powers of two that are friendly to the virtual memory system or +filesystem (such as 4kB or 8kB) are recommended. Larger sizes (64kB) can +yield better compression, with a possible increased cost incurred by +readers during access. ++ +The largest block size is `16777215` bytes (15.99 MiB). The default value is +`4096` bytes (4kB). A value of `0` will use the default value. + +reftable.restartInterval:: + The interval at which to create restart points. The reftable backend + determines the restart points at file creation. Every 16 may be + more suitable for smaller block sizes (4k or 8k), every 64 for larger + block sizes (64k). ++ +More frequent restart points reduces prefix compression and increases +space consumed by the restart table, both of which increase file size. ++ +Less frequent restart points makes prefix compression more effective, +decreasing overall file size, with increased penalties for readers +walking through more records after the binary search step. ++ +A maximum of `65535` restart points per block is supported. ++ +The default value is to create restart points every 16 records. A value of `0` +will use the default value. + +reftable.indexObjects:: + Whether the reftable backend shall write object blocks. Object blocks + are a reverse mapping of object ID to the references pointing to them. ++ +The default value is `true`. + +reftable.geometricFactor:: + Whenever the reftable backend appends a new table to the stack, it + performs auto compaction to ensure that there is only a handful of + tables. The backend does this by ensuring that tables form a geometric + sequence regarding the respective sizes of each table. ++ +By default, the geometric sequence uses a factor of 2, meaning that for any +table, the next-biggest table must at least be twice as big. A maximum factor +of 256 is supported. diff --git a/Documentation/diff-options.txt b/Documentation/diff-options.txt index 0e94569..c7df20e 100644 --- a/Documentation/diff-options.txt +++ b/Documentation/diff-options.txt @@ -329,12 +329,13 @@ explained for the configuration variable `core.quotePath` (see linkgit:git-config[1]). --name-only:: - Show only names of changed files. The file names are often encoded in UTF-8. + Show only the name of each changed file in the post-image tree. + The file names are often encoded in UTF-8. For more information see the discussion about encoding in the linkgit:git-log[1] manual page. --name-status:: - Show only names and status of changed files. See the description + Show only the name(s) and status of each changed file. See the description of the `--diff-filter` option on what the status letters mean. Just like `--name-only` the file names are often encoded in UTF-8. diff --git a/Documentation/git-config.txt b/Documentation/git-config.txt index ac61113..65c645d 100644 --- a/Documentation/git-config.txt +++ b/Documentation/git-config.txt @@ -9,21 +9,14 @@ git-config - Get and set repository or global options SYNOPSIS -------- [verse] -'git config' [<file-option>] [--type=<type>] [--comment=<message>] [--fixed-value] [--show-origin] [--show-scope] [-z|--null] <name> [<value> [<value-pattern>]] -'git config' [<file-option>] [--type=<type>] [--comment=<message>] --add <name> <value> -'git config' [<file-option>] [--type=<type>] [--comment=<message>] [--fixed-value] --replace-all <name> <value> [<value-pattern>] -'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] --get <name> [<value-pattern>] -'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] --get-all <name> [<value-pattern>] -'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] [--name-only] --get-regexp <name-regex> [<value-pattern>] -'git config' [<file-option>] [--type=<type>] [-z|--null] --get-urlmatch <name> <URL> -'git config' [<file-option>] [--fixed-value] --unset <name> [<value-pattern>] -'git config' [<file-option>] [--fixed-value] --unset-all <name> [<value-pattern>] -'git config' [<file-option>] --rename-section <old-name> <new-name> -'git config' [<file-option>] --remove-section <name> -'git config' [<file-option>] [--show-origin] [--show-scope] [-z|--null] [--name-only] -l | --list -'git config' [<file-option>] --get-color <name> [<default>] +'git config list' [<file-option>] [<display-option>] [--includes] +'git config get' [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name> +'git config set' [<file-option>] [--type=<type>] [--all] [--value=<value>] [--fixed-value] <name> <value> +'git config unset' [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value> +'git config rename-section' [<file-option>] <old-name> <new-name> +'git config remove-section' [<file-option>] <name> +'git config edit' [<file-option>] 'git config' [<file-option>] --get-colorbool <name> [<stdout-is-tty>] -'git config' [<file-option>] -e | --edit DESCRIPTION ----------- @@ -31,7 +24,7 @@ You can query/set/replace/unset options with this command. The name is actually the section and the key separated by a dot, and the value will be escaped. -Multiple lines can be added to an option by using the `--add` option. +Multiple lines can be added to an option by using the `--append` option. If you want to update or unset an option which can occur on multiple lines, a `value-pattern` (which is an extended regular expression, unless the `--fixed-value` option is given) needs to be given. Only the @@ -74,6 +67,42 @@ On success, the command returns the exit code 0. A list of all available configuration variables can be obtained using the `git help --config` command. +COMMANDS +-------- + +list:: + List all variables set in config file, along with their values. + +get:: + Emits the value of the specified key. If key is present multiple times + in the configuration, emits the last value. If `--all` is specified, + emits all values associated with key. Returns error code 1 if key is + not present. + +set:: + Set value for one or more config options. By default, this command + refuses to write multi-valued config options. Passing `--all` will + replace all multi-valued config options with the new value, whereas + `--value=` will replace all config options whose values match the given + pattern. + +unset:: + Unset value for one or more config options. By default, this command + refuses to unset multi-valued keys. Passing `--all` will unset all + multi-valued config options, whereas `--value` will unset all config + options whose values match the given pattern. + +rename-section:: + Rename the given section to a new name. + +remove-section:: + Remove the given section from the configuration file. + +edit:: + Opens an editor to modify the specified config file; either + `--system`, `--global`, `--local` (default), `--worktree`, or + `--file <config-file>`. + [[OPTIONS]] OPTIONS ------- @@ -82,10 +111,9 @@ OPTIONS Default behavior is to replace at most one line. This replaces all lines matching the key (and optionally the `value-pattern`). ---add:: +--append:: Adds a new line to the option without altering any existing - values. This is the same as providing '^$' as the `value-pattern` - in `--replace-all`. + values. This is the same as providing '--value=^$' in `set`. --comment <message>:: Append a comment at the end of new or modified lines. @@ -99,22 +127,16 @@ OPTIONS not contain linefeed characters (no multi-line comments are permitted). ---get:: - Get the value for a given key (optionally filtered by a regex - matching the value). Returns error code 1 if the key was not - found and the last value if multiple key values were found. +--all:: + With `get`, return all values for a multi-valued key. ---get-all:: - Like get, but returns all values for a multi-valued key. +---regexp:: + With `get`, interpret the name as a regular expression. Regular + expression matching is currently case-sensitive and done against a + canonicalized version of the key in which section and variable names + are lowercased, but subsection names are not. ---get-regexp:: - Like --get-all, but interprets the name as a regular expression and - writes out the key names. Regular expression matching is currently - case-sensitive and done against a canonicalized version of the key - in which section and variable names are lowercased, but subsection - names are not. - ---get-urlmatch <name> <URL>:: +--url=<URL>:: When given a two-part <name> as <section>.<key>, the value for <section>.<URL>.<key> whose <URL> part matches the best to the given URL is returned (if no such key exists, the value for @@ -178,22 +200,6 @@ See also <<FILES>>. section in linkgit:gitrevisions[7] for a more complete list of ways to spell blob names. ---remove-section:: - Remove the given section from the configuration file. - ---rename-section:: - Rename the given section to a new name. - ---unset:: - Remove the line matching the key from config file. - ---unset-all:: - Remove all lines matching the key from config file. - --l:: ---list:: - List all variables set in config file, along with their values. - --fixed-value:: When used with the `value-pattern` argument, treat `value-pattern` as an exact string instead of a regular expression. This will restrict @@ -248,8 +254,8 @@ Valid `<type>`'s include: contain line breaks. --name-only:: - Output only the names of config variables for `--list` or - `--get-regexp`. + Output only the names of config variables for `list` or + `get`. --show-origin:: Augment the output of all queried config options with the @@ -273,23 +279,6 @@ Valid `<type>`'s include: When the color setting for `name` is undefined, the command uses `color.ui` as fallback. ---get-color <name> [<default>]:: - - Find the color configured for `name` (e.g. `color.diff.new`) and - output it as the ANSI color escape sequence to the standard - output. The optional `default` parameter is used instead, if - there is no color configured for `name`. -+ -`--type=color [--default=<default>]` is preferred over `--get-color` -(but note that `--get-color` will omit the trailing newline printed by -`--type=color`). - --e:: ---edit:: - Opens an editor to modify the specified config file; either - `--system`, `--global`, `--local` (default), `--worktree`, or - `--file <config-file>`. - --[no-]includes:: Respect `include.*` directives in config files when looking up values. Defaults to `off` when a specific file is given (e.g., @@ -297,14 +286,64 @@ Valid `<type>`'s include: config files. --default <value>:: - When using `--get`, and the requested variable is not found, behave as if + When using `get`, and the requested variable is not found, behave as if <value> were the value assigned to that variable. +DEPRECATED MODES +---------------- + +The following modes have been deprecated in favor of subcommands. It is +recommended to migrate to the new syntax. + +'git config <name>':: + Replaced by `git config get <name>`. + +'git config <name> <value> [<value-pattern>]':: + Replaced by `git config set [--value=<pattern>] <name> <value>`. + +-l:: +--list:: + Replaced by `git config list`. + +--get <name> [<value-pattern>]:: + Replaced by `git config get [--value=<pattern>] <name>`. + +--get-all <name> [<value-pattern>]:: + Replaced by `git config get [--value=<pattern>] --all --show-names <name>`. + +--get-regexp <name-regexp>:: + Replaced by `git config get --all --show-names --regexp <name-regexp>`. + +--get-urlmatch <name> <URL>:: + Replaced by `git config get --all --show-names --url=<URL> <name>`. + +--get-color <name> [<default>]:: + Replaced by `git config get --type=color [--default=<default>] <name>`. + +--add <name> <value>:: + Replaced by `git config set --append <name> <value>`. + +--unset <name> [<value-pattern>]:: + Replaced by `git config unset [--value=<pattern>] <name>`. + +--unset-all <name> [<value-pattern>]:: + Replaced by `git config unset [--value=<pattern>] --all <name>`. + +--rename-section <old-name> <new-name>:: + Replaced by `git config rename-section <old-name> <new-name>`. + +--remove-section <name>:: + Replaced by `git config remove-section <name>`. + +-e:: +--edit:: + Replaced by `git config edit`. + CONFIGURATION ------------- `pager.config` is only respected when listing configuration, i.e., when -using `--list` or any of the `--get-*` which may return multiple results. -The default is to use a pager. +using `list` or `get` which may return multiple results. The default is to use +a pager. [[FILES]] FILES @@ -346,8 +385,8 @@ precedence over values read earlier. When multiple values are taken then all values of a key from all files will be used. By default, options are only written to the repository specific -configuration file. Note that this also affects options like `--replace-all` -and `--unset`. *'git config' will only ever change one file at a time*. +configuration file. Note that this also affects options like `set` +and `unset`. *'git config' will only ever change one file at a time*. You can limit which configuration sources are read from or written to by specifying the path of a file with the `--file` option, or by specifying a @@ -482,7 +521,7 @@ Given a .git/config like this: you can set the filemode to true with ------------ -% git config core.filemode true +% git config set core.filemode true ------------ The hypothetical proxy command entries actually have a postfix to discern @@ -490,7 +529,7 @@ what URL they apply to. Here is how to change the entry for kernel.org to "ssh". ------------ -% git config core.gitproxy '"ssh" for kernel.org' 'for kernel.org$' +% git config set --value='for kernel.org$' core.gitproxy '"ssh" for kernel.org' ------------ This makes sure that only the key/value pair for kernel.org is replaced. @@ -498,7 +537,7 @@ This makes sure that only the key/value pair for kernel.org is replaced. To delete the entry for renames, do ------------ -% git config --unset diff.renames +% git config unset diff.renames ------------ If you want to delete an entry for a multivar (like core.gitproxy above), @@ -507,51 +546,45 @@ you have to provide a regex matching the value of exactly one line. To query the value for a given key, do ------------ -% git config --get core.filemode ------------- - -or - ------------- -% git config core.filemode +% git config get core.filemode ------------ or, to query a multivar: ------------ -% git config --get core.gitproxy "for kernel.org$" +% git config get --value="for kernel.org$" core.gitproxy ------------ If you want to know all the values for a multivar, do: ------------ -% git config --get-all core.gitproxy +% git config get --all --show-names core.gitproxy ------------ If you like to live dangerously, you can replace *all* core.gitproxy by a new one with ------------ -% git config --replace-all core.gitproxy ssh +% git config set --all core.gitproxy ssh ------------ However, if you really only want to replace the line for the default proxy, i.e. the one without a "for ..." postfix, do something like this: ------------ -% git config core.gitproxy ssh '! for ' +% git config set --value='! for ' core.gitproxy ssh ------------ To actually match only values with an exclamation mark, you have to ------------ -% git config section.key value '[!]' +% git config set --value='[!]' section.key value ------------ To add a new proxy, without altering any of the existing ones, use ------------ -% git config --add core.gitproxy '"proxy-command" for example.com' +% git config set --append core.gitproxy '"proxy-command" for example.com' ------------ An example to use customized color from the configuration in your @@ -559,8 +592,8 @@ script: ------------ #!/bin/sh -WS=$(git config --get-color color.diff.whitespace "blue reverse") -RESET=$(git config --get-color "" "reset") +WS=$(git config get --type=color --default="blue reverse" color.diff.whitespace) +RESET=$(git config get --type=color --default="reset" "") echo "${WS}your whitespace color or blue reverse${RESET}" ------------ @@ -568,11 +601,11 @@ For URLs in `https://weak.example.com`, `http.sslVerify` is set to false, while it is set to `true` for all others: ------------ -% git config --type=bool --get-urlmatch http.sslverify https://good.example.com +% git config get --type=bool --url=https://good.example.com http.sslverify true -% git config --type=bool --get-urlmatch http.sslverify https://weak.example.com +% git config get --type=bool --url=https://weak.example.com http.sslverify false -% git config --get-urlmatch http https://weak.example.com +% git config get --url=https://weak.example.com http http.cookieFile /tmp/cookie.txt http.sslverify false ------------ diff --git a/Documentation/git-format-patch.txt b/Documentation/git-format-patch.txt index 369af2c..8708b31 100644 --- a/Documentation/git-format-patch.txt +++ b/Documentation/git-format-patch.txt @@ -357,6 +357,11 @@ material (this may change in the future). between the previous and current series of patches by adjusting the creation/deletion cost fudge factor. See linkgit:git-range-diff[1]) for details. ++ +Defaults to 999 (the linkgit:git-range-diff[1] uses 60), as the use +case is to show comparison with an older iteration of the same +topic and the tool should find more correspondence between the two +sets of patches. --notes[=<ref>]:: --no-notes:: diff --git a/Documentation/git-gui.txt b/Documentation/git-gui.txt index e8f3ccb..f5b02ef 100644 --- a/Documentation/git-gui.txt +++ b/Documentation/git-gui.txt @@ -114,7 +114,7 @@ of end users. The official repository of the 'git gui' project can be found at: - https://github.com/prati0100/git-gui.git/ + https://github.com/j6t/git-gui GIT --- diff --git a/Documentation/git-merge-tree.txt b/Documentation/git-merge-tree.txt index dd388fa..84cb2ed 100644 --- a/Documentation/git-merge-tree.txt +++ b/Documentation/git-merge-tree.txt @@ -72,6 +72,11 @@ OPTIONS As the merge-base is provided directly, <branch1> and <branch2> do not need to specify commits; trees are enough. +-X<option>:: +--strategy-option=<option>:: + Pass the merge strategy-specific option through to the merge strategy. + See linkgit:git-merge[1] for details. + [[OUTPUT]] OUTPUT ------ diff --git a/Documentation/git-rev-parse.txt b/Documentation/git-rev-parse.txt index f9d5a35..dc12d38 100644 --- a/Documentation/git-rev-parse.txt +++ b/Documentation/git-rev-parse.txt @@ -18,8 +18,15 @@ Many Git porcelainish commands take a mixture of flags (i.e. parameters that begin with a dash '-') and parameters meant for the underlying 'git rev-list' command they use internally and flags and parameters for the other commands they use -downstream of 'git rev-list'. This command is used to -distinguish between them. +downstream of 'git rev-list'. The primary purpose of this command +is to allow calling programs to distinguish between them. There are +a few other operation modes that have nothing to do with the above +"help parse command line options". + +Unless otherwise specified, most of the options and operation modes +require you to run this command inside a git repository or a working +tree that is under the control of a git repository, and will give you +a fatal error otherwise. OPTIONS @@ -32,11 +39,15 @@ Each of these options must appear first on the command line. --parseopt:: Use 'git rev-parse' in option parsing mode (see PARSEOPT section below). + The command in this mode can be used outside a repository or + a working tree controlled by a repository. --sq-quote:: Use 'git rev-parse' in shell quoting mode (see SQ-QUOTE section below). In contrast to the `--sq` option below, this mode only does quoting. Nothing else is done to command input. + The command in this mode can be used outside a repository or + a working tree controlled by a repository. Options for --parseopt ~~~~~~~~~~~~~~~~~~~~~~ diff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt index 5fe519c..4494729 100644 --- a/Documentation/git-tag.txt +++ b/Documentation/git-tag.txt @@ -10,6 +10,7 @@ SYNOPSIS -------- [verse] 'git tag' [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] [-e] + [(--trailer <token>[(=|:)<value>])...] <tagname> [<commit> | <object>] 'git tag' -d <tagname>... 'git tag' [-n[<num>]] -l [--contains <commit>] [--no-contains <commit>] @@ -31,8 +32,8 @@ creates a 'tag' object, and requires a tag message. Unless `-m <msg>` or `-F <file>` is given, an editor is started for the user to type in the tag message. -If `-m <msg>` or `-F <file>` is given and `-a`, `-s`, and `-u <key-id>` -are absent, `-a` is implied. +If `-m <msg>` or `-F <file>` or `--trailer <token>[=<value>]` is given +and `-a`, `-s`, and `-u <key-id>` are absent, `-a` is implied. Otherwise, a tag reference that points directly at the given object (i.e., a lightweight tag) is created. @@ -178,6 +179,17 @@ This option is only applicable when listing tags without annotation lines. Implies `-a` if none of `-a`, `-s`, or `-u <key-id>` is given. +--trailer <token>[(=|:)<value>]:: + Specify a (<token>, <value>) pair that should be applied as a + trailer. (e.g. `git tag --trailer "Custom-Key: value"` + will add a "Custom-Key" trailer to the tag message.) + The `trailer.*` configuration variables + (linkgit:git-interpret-trailers[1]) can be used to define if + a duplicated trailer is omitted, where in the run of trailers + each trailer would appear, and other details. + The trailers can be extracted in `git tag --list`, using + `--format="%(trailers)"` placeholder. + -e:: --edit:: The message taken from file with `-F` and command line with diff --git a/Documentation/git-update-index.txt b/Documentation/git-update-index.txt index 8c47890..7128aed 100644 --- a/Documentation/git-update-index.txt +++ b/Documentation/git-update-index.txt @@ -25,6 +25,7 @@ SYNOPSIS [--really-refresh] [--unresolve] [--again | -g] [--info-only] [--index-info] [-z] [--stdin] [--index-version <n>] + [--show-index-version] [--verbose] [--] [<file>...] diff --git a/Documentation/git-upload-pack.txt b/Documentation/git-upload-pack.txt index 7ad60bc..516d163 100644 --- a/Documentation/git-upload-pack.txt +++ b/Documentation/git-upload-pack.txt @@ -55,6 +55,37 @@ ENVIRONMENT admins may need to configure some transports to allow this variable to be passed. See the discussion in linkgit:git[1]. +`GIT_NO_LAZY_FETCH`:: + When cloning or fetching from a partial repository (i.e., one + itself cloned with `--filter`), the server-side `upload-pack` + may need to fetch extra objects from its upstream in order to + complete the request. By default, `upload-pack` will refuse to + perform such a lazy fetch, because `git fetch` may run arbitrary + commands specified in configuration and hooks of the source + repository (and `upload-pack` tries to be safe to run even in + untrusted `.git` directories). ++ +This is implemented by having `upload-pack` internally set the +`GIT_NO_LAZY_FETCH` variable to `1`. If you want to override it +(because you are fetching from a partial clone, and you are sure +you trust it), you can explicitly set `GIT_NO_LAZY_FETCH` to +`0`. + +SECURITY +-------- + +Most Git commands should not be run in an untrusted `.git` directory +(see the section `SECURITY` in linkgit:git[1]). `upload-pack` tries to +avoid any dangerous configuration options or hooks from the repository +it's serving, making it safe to clone an untrusted directory and run +commands on the resulting clone. + +For an extra level of safety, you may be able to run `upload-pack` as an +alternate user. The details will be platform dependent, but on many +systems you can run: + + git clone --no-local --upload-pack='sudo -u nobody git-upload-pack' ... + SEE ALSO -------- linkgit:gitnamespaces[7] diff --git a/Documentation/git.txt b/Documentation/git.txt index 7a1b112..a31a70a 100644 --- a/Documentation/git.txt +++ b/Documentation/git.txt @@ -11,9 +11,10 @@ SYNOPSIS [verse] 'git' [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>] [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path] - [-p|--paginate|-P|--no-pager] [--no-replace-objects] [--bare] - [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>] - [--config-env=<name>=<envvar>] <command> [<args>] + [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--no-lazy-fetch] + [--no-optional-locks] [--no-advice] [--bare] [--git-dir=<path>] + [--work-tree=<path>] [--namespace=<name>] [--config-env=<name>=<envvar>] + <command> [<args>] DESCRIPTION ----------- @@ -186,6 +187,13 @@ If you just want to run git as if it was started in `<path>` then use This is equivalent to setting the `GIT_NO_LAZY_FETCH` environment variable to `1`. +--no-optional-locks:: + Do not perform optional operations that require locks. This is + equivalent to setting the `GIT_OPTIONAL_LOCKS` to `0`. + +--no-advice:: + Disable all advice hints from being printed. + --literal-pathspecs:: Treat pathspecs literally (i.e. no globbing, no pathspec magic). This is equivalent to setting the `GIT_LITERAL_PATHSPECS` environment @@ -207,10 +215,6 @@ If you just want to run git as if it was started in `<path>` then use Add "icase" magic to all pathspec. This is equivalent to setting the `GIT_ICASE_PATHSPECS` environment variable to `1`. ---no-optional-locks:: - Do not perform optional operations that require locks. This is - equivalent to setting the `GIT_OPTIONAL_LOCKS` to `0`. - --list-cmds=<group>[,<group>...]:: List commands by group. This is an internal/experimental option and may change or be removed in the future. Supported @@ -1067,6 +1071,37 @@ The index is also capable of storing multiple entries (called "stages") for a given pathname. These stages are used to hold the various unmerged version of a file when a merge is in progress. +SECURITY +-------- + +Some configuration options and hook files may cause Git to run arbitrary +shell commands. Because configuration and hooks are not copied using +`git clone`, it is generally safe to clone remote repositories with +untrusted content, inspect them with `git log`, and so on. + +However, it is not safe to run Git commands in a `.git` directory (or +the working tree that surrounds it) when that `.git` directory itself +comes from an untrusted source. The commands in its config and hooks +are executed in the usual way. + +By default, Git will refuse to run when the repository is owned by +someone other than the user running the command. See the entry for +`safe.directory` in linkgit:git-config[1]. While this can help protect +you in a multi-user environment, note that you can also acquire +untrusted repositories that are owned by you (for example, if you +extract a zip file or tarball from an untrusted source). In such cases, +you'd need to "sanitize" the untrusted repository first. + +If you have an untrusted `.git` directory, you should first clone it +with `git clone --no-local` to obtain a clean copy. Git does restrict +the set of options and hooks that will be run by `upload-pack`, which +handles the server side of a clone or fetch, but beware that the +surface area for attack against `upload-pack` is large, so this does +carry some risk. The safest thing is to serve the repository as an +unprivileged user (either via linkgit:git-daemon[1], ssh, or using +other tools to change user ids). See the discussion in the `SECURITY` +section of linkgit:git-upload-pack[1]. + FURTHER DOCUMENTATION --------------------- diff --git a/Documentation/githooks.txt b/Documentation/githooks.txt index ee9b92c..06e9971 100644 --- a/Documentation/githooks.txt +++ b/Documentation/githooks.txt @@ -486,7 +486,7 @@ reference-transaction This hook is invoked by any Git command that performs reference updates. It executes whenever a reference transaction is prepared, committed or aborted and may thus get called multiple times. The hook -does not cover symbolic references (but that may change in the future). +also supports symbolic reference updates. The hook takes exactly one argument, which is the current state the given reference transaction is in: @@ -503,16 +503,20 @@ given reference transaction is in: For each reference update that was added to the transaction, the hook receives on standard input a line of the format: - <old-oid> SP <new-oid> SP <ref-name> LF + <old-value> SP <new-value> SP <ref-name> LF -where `<old-oid>` is the old object name passed into the reference -transaction, `<new-oid>` is the new object name to be stored in the +where `<old-value>` is the old object name passed into the reference +transaction, `<new-value>` is the new object name to be stored in the ref and `<ref-name>` is the full name of the ref. When force updating the reference regardless of its current value or when the reference is -to be created anew, `<old-oid>` is the all-zeroes object name. To +to be created anew, `<old-value>` is the all-zeroes object name. To distinguish these cases, you can inspect the current value of `<ref-name>` via `git rev-parse`. +For symbolic reference updates the `<old_value>` and `<new-value>` +fields could denote references instead of objects. A reference will be +denoted with a 'ref:' prefix, like `ref:<ref-target>`. + The exit status of the hook is ignored for any state except for the "prepared" state. In the "prepared" state, a non-zero exit status will cause the transaction to be aborted. The hook will not be called with diff --git a/Documentation/glossary-content.txt b/Documentation/glossary-content.txt index d71b199..30b394a 100644 --- a/Documentation/glossary-content.txt +++ b/Documentation/glossary-content.txt @@ -497,20 +497,18 @@ exclude;; unusual refs. [[def_pseudoref]]pseudoref:: - Pseudorefs are a class of files under `$GIT_DIR` which behave - like refs for the purposes of rev-parse, but which are treated - specially by git. Pseudorefs both have names that are all-caps, - and always start with a line consisting of a - <<def_SHA1,SHA-1>> followed by whitespace. So, HEAD is not a - pseudoref, because it is sometimes a symbolic ref. They might - optionally contain some additional data. `MERGE_HEAD` and - `CHERRY_PICK_HEAD` are examples. Unlike - <<def_per_worktree_ref,per-worktree refs>>, these files cannot - be symbolic refs, and never have reflogs. They also cannot be - updated through the normal ref update machinery. Instead, - they are updated by directly writing to the files. However, - they can be read as if they were refs, so `git rev-parse - MERGE_HEAD` will work. + A ref that has different semantics than normal refs. These refs can be + read via normal Git commands, but cannot be written to by commands like + linkgit:git-update-ref[1]. ++ +The following pseudorefs are known to Git: + + - `FETCH_HEAD` is written by linkgit:git-fetch[1] or linkgit:git-pull[1]. It + may refer to multiple object IDs. Each object ID is annotated with metadata + indicating where it was fetched from and its fetch status. + + - `MERGE_HEAD` is written by linkgit:git-merge[1] when resolving merge + conflicts. It contains all commit IDs which are being merged. [[def_pull]]pull:: Pulling a <<def_branch,branch>> means to <<def_fetch,fetch>> it and @@ -552,20 +550,38 @@ exclude;; to the result. [[def_ref]]ref:: - A name that begins with `refs/` (e.g. `refs/heads/master`) - that points to an <<def_object_name,object name>> or another - ref (the latter is called a <<def_symref,symbolic ref>>). + A name that that points to an <<def_object_name,object name>> or + another ref (the latter is called a <<def_symref,symbolic ref>>). For convenience, a ref can sometimes be abbreviated when used as an argument to a Git command; see linkgit:gitrevisions[7] for details. Refs are stored in the <<def_repository,repository>>. + The ref namespace is hierarchical. -Different subhierarchies are used for different purposes (e.g. the -`refs/heads/` hierarchy is used to represent local branches). +Ref names must either start with `refs/` or be located in the root of +the hierarchy. For the latter, their name must follow these rules: ++ + - The name consists of only upper-case characters or underscores. + + - The name ends with "`_HEAD`" or is equal to "`HEAD`". + -There are a few special-purpose refs that do not begin with `refs/`. -The most notable example is `HEAD`. +There are some irregular refs in the root of the hierarchy that do not +match these rules. The following list is exhaustive and shall not be +extended in the future: ++ + - `AUTO_MERGE` + + - `BISECT_EXPECTED_REV` + + - `NOTES_MERGE_PARTIAL` + + - `NOTES_MERGE_REF` + + - `MERGE_AUTOSTASH` ++ +Different subhierarchies are used for different purposes. For example, +the `refs/heads/` hierarchy is used to represent local branches whereas +the `refs/tags/` hierarchy is used to represent local tags.. [[def_reflog]]reflog:: A reflog shows the local "history" of a ref. In other words, @@ -576,7 +592,8 @@ The most notable example is `HEAD`. [[def_refspec]]refspec:: A "refspec" is used by <<def_fetch,fetch>> and <<def_push,push>> to describe the mapping between remote - <<def_ref,ref>> and local ref. + <<def_ref,ref>> and local ref. See linkgit:git-fetch[1] or + linkgit:git-push[1] for details. [[def_remote]]remote repository:: A <<def_repository,repository>> which is used to track the same @@ -638,20 +655,6 @@ The most notable example is `HEAD`. An <<def_object,object>> used to temporarily store the contents of a <<def_dirty,dirty>> working directory and the index for future reuse. -[[def_special_ref]]special ref:: - A ref that has different semantics than normal refs. These refs can be - accessed via normal Git commands but may not behave the same as a - normal ref in some cases. -+ -The following special refs are known to Git: - - - "`FETCH_HEAD`" is written by linkgit:git-fetch[1] or linkgit:git-pull[1]. It - may refer to multiple object IDs. Each object ID is annotated with metadata - indicating where it was fetched from and its fetch status. - - - "`MERGE_HEAD`" is written by linkgit:git-merge[1] when resolving merge - conflicts. It contains all commit IDs which are being merged. - [[def_submodule]]submodule:: A <<def_repository,repository>> that holds the history of a separate project inside another repository (the latter of @@ -794,6 +794,7 @@ TEST_BUILTINS_OBJS += test-dump-split-index.o TEST_BUILTINS_OBJS += test-dump-untracked-cache.o TEST_BUILTINS_OBJS += test-env-helper.o TEST_BUILTINS_OBJS += test-example-decorate.o +TEST_BUILTINS_OBJS += test-example-tap.o TEST_BUILTINS_OBJS += test-find-pack.o TEST_BUILTINS_OBJS += test-fsmonitor-client.o TEST_BUILTINS_OBJS += test-genrandom.o @@ -838,7 +839,6 @@ TEST_BUILTINS_OBJS += test-sha1.o TEST_BUILTINS_OBJS += test-sha256.o TEST_BUILTINS_OBJS += test-sigchain.o TEST_BUILTINS_OBJS += test-simple-ipc.o -TEST_BUILTINS_OBJS += test-strcmp-offset.o TEST_BUILTINS_OBJS += test-string-list.o TEST_BUILTINS_OBJS += test-submodule-config.o TEST_BUILTINS_OBJS += test-submodule-nested-repo-config.o @@ -1333,11 +1333,12 @@ THIRD_PARTY_SOURCES += compat/regex/% THIRD_PARTY_SOURCES += sha1collisiondetection/% THIRD_PARTY_SOURCES += sha1dc/% -UNIT_TEST_PROGRAMS += t-basic -UNIT_TEST_PROGRAMS += t-mem-pool -UNIT_TEST_PROGRAMS += t-strbuf UNIT_TEST_PROGRAMS += t-ctype +UNIT_TEST_PROGRAMS += t-mem-pool UNIT_TEST_PROGRAMS += t-prio-queue +UNIT_TEST_PROGRAMS += t-strbuf +UNIT_TEST_PROGRAMS += t-strcmp-offset +UNIT_TEST_PROGRAMS += t-trailer UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS)) UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS)) UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o @@ -3235,7 +3236,7 @@ perf: all .PRECIOUS: $(TEST_OBJS) -t/helper/test-tool$X: $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS)) +t/helper/test-tool$X: $(patsubst %,t/helper/%,$(TEST_BUILTINS_OBJS)) $(UNIT_TEST_DIR)/test-lib.o t/helper/test-%$X: t/helper/test-%.o GIT-LDFLAGS $(GITLIBS) $(REFTABLE_TEST_LIB) $(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(filter %.a,$^) $(LIBS) @@ -3888,5 +3889,5 @@ $(UNIT_TEST_PROGS): $(UNIT_TEST_BIN)/%$X: $(UNIT_TEST_DIR)/%.o $(UNIT_TEST_DIR)/ .PHONY: build-unit-tests unit-tests build-unit-tests: $(UNIT_TEST_PROGS) -unit-tests: $(UNIT_TEST_PROGS) +unit-tests: $(UNIT_TEST_PROGS) t/helper/test-tool$X $(MAKE) -C t/ unit-tests diff --git a/add-interactive.c b/add-interactive.c index e17602b..b5d6cd6 100644 --- a/add-interactive.c +++ b/add-interactive.c @@ -532,8 +532,9 @@ static int get_modified_files(struct repository *r, size_t *binary_count) { struct object_id head_oid; - int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, - &head_oid, NULL); + int is_initial = !refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + "HEAD", RESOLVE_REF_READING, + &head_oid, NULL); struct collection_status s = { 0 }; int i; @@ -761,8 +762,10 @@ static int run_revert(struct add_i_state *s, const struct pathspec *ps, size_t count, i, j; struct object_id oid; - int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &oid, - NULL); + int is_initial = !refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + "HEAD", RESOLVE_REF_READING, + &oid, + NULL); struct lock_file index_lock; const char **paths; struct tree *tree; @@ -990,8 +993,10 @@ static int run_diff(struct add_i_state *s, const struct pathspec *ps, ssize_t count, i; struct object_id oid; - int is_initial = !resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &oid, - NULL); + int is_initial = !refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + "HEAD", RESOLVE_REF_READING, + &oid, + NULL); if (get_modified_files(s->r, INDEX_ONLY, files, ps, NULL, NULL) < 0) return -1; diff --git a/add-patch.c b/add-patch.c index 2252895..814de57 100644 --- a/add-patch.c +++ b/add-patch.c @@ -1227,6 +1227,7 @@ static int prompt_yesno(struct add_p_state *s, const char *prompt) fflush(stdout); if (read_single_character(s) == EOF) return -1; + /* do not limit to 1-byte input to allow 'no' etc. */ switch (tolower(s->answer.buf[0])) { case 'n': return 0; case 'y': return 1; @@ -1510,6 +1511,12 @@ static int patch_update_file(struct add_p_state *s, if (!s->answer.len) continue; ch = tolower(s->answer.buf[0]); + + /* 'g' takes a hunk number and '/' takes a regexp */ + if (s->answer.len != 1 && (ch != 'g' && ch != '/')) { + err(s, _("Only one letter is expected, got '%s'"), s->answer.buf); + continue; + } if (ch == 'y') { hunk->use = USE_HUNK; soft_increment: @@ -2,6 +2,7 @@ #include "advice.h" #include "config.h" #include "color.h" +#include "environment.h" #include "gettext.h" #include "help.h" #include "string-list.h" @@ -127,6 +128,12 @@ void advise(const char *advice, ...) int advice_enabled(enum advice_type type) { int enabled = advice_setting[type].level != ADVICE_LEVEL_DISABLED; + static int globally_enabled = -1; + + if (globally_enabled < 0) + globally_enabled = git_env_bool(GIT_ADVICE_ENVIRONMENT, 1); + if (!globally_enabled) + return 0; if (type == ADVICE_PUSH_UPDATE_REJECTED) return enabled && @@ -765,8 +765,8 @@ static struct attr_stack *read_attr_from_file(const char *path, unsigned flags) return res; } -static struct attr_stack *read_attr_from_buf(char *buf, const char *path, - unsigned flags) +static struct attr_stack *read_attr_from_buf(char *buf, size_t length, + const char *path, unsigned flags) { struct attr_stack *res; char *sp; @@ -774,6 +774,11 @@ static struct attr_stack *read_attr_from_buf(char *buf, const char *path, if (!buf) return NULL; + if (length >= ATTR_MAX_FILE_SIZE) { + warning(_("ignoring overly large gitattributes blob '%s'"), path); + free(buf); + return NULL; + } CALLOC_ARRAY(res, 1); for (sp = buf; *sp;) { @@ -813,7 +818,7 @@ static struct attr_stack *read_attr_from_blob(struct index_state *istate, return NULL; } - return read_attr_from_buf(buf, path, flags); + return read_attr_from_buf(buf, sz, path, flags); } static struct attr_stack *read_attr_from_index(struct index_state *istate, @@ -860,13 +865,7 @@ static struct attr_stack *read_attr_from_index(struct index_state *istate, stack = read_attr_from_blob(istate, &istate->cache[sparse_dir_pos]->oid, relative_path, flags); } else { buf = read_blob_data_from_index(istate, path, &size); - if (!buf) - return NULL; - if (size >= ATTR_MAX_FILE_SIZE) { - warning(_("ignoring overly large gitattributes blob '%s'"), path); - return NULL; - } - stack = read_attr_from_buf(buf, path, flags); + stack = read_attr_from_buf(buf, size, path, flags); } return stack; } @@ -1206,15 +1205,16 @@ static void collect_some_attrs(struct index_state *istate, } static const char *default_attr_source_tree_object_name; -static int ignore_bad_attr_tree; void set_git_attr_source(const char *tree_object_name) { default_attr_source_tree_object_name = xstrdup(tree_object_name); } -static void compute_default_attr_source(struct object_id *attr_source) +static int compute_default_attr_source(struct object_id *attr_source) { + int ignore_bad_attr_tree = 0; + if (!default_attr_source_tree_object_name) default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT); @@ -1223,29 +1223,34 @@ static void compute_default_attr_source(struct object_id *attr_source) ignore_bad_attr_tree = 1; } - if (!default_attr_source_tree_object_name && - startup_info->have_repository && - is_bare_repository()) { - default_attr_source_tree_object_name = "HEAD"; - ignore_bad_attr_tree = 1; - } + if (!default_attr_source_tree_object_name) + return 0; - if (!default_attr_source_tree_object_name || !is_null_oid(attr_source)) - return; + if (!startup_info->have_repository) { + if (!ignore_bad_attr_tree) + die(_("cannot use --attr-source or GIT_ATTR_SOURCE without repo")); + return 0; + } if (repo_get_oid_treeish(the_repository, default_attr_source_tree_object_name, - attr_source) && !ignore_bad_attr_tree) - die(_("bad --attr-source or GIT_ATTR_SOURCE")); + attr_source)) { + if (!ignore_bad_attr_tree) + die(_("bad --attr-source or GIT_ATTR_SOURCE")); + return 0; + } + + return 1; } static struct object_id *default_attr_source(void) { static struct object_id attr_source; + static int has_attr_source = -1; - if (is_null_oid(&attr_source)) - compute_default_attr_source(&attr_source); - if (is_null_oid(&attr_source)) + if (has_attr_source < 0) + has_attr_source = compute_default_attr_source(&attr_source); + if (!has_attr_source) return NULL; return &attr_source; } @@ -1296,7 +1301,8 @@ static const char *builtin_object_mode_attr(struct index_state *istate, const ch if (pos >= 0) { if (S_ISGITLINK(istate->cache[pos]->ce_mode)) mode = istate->cache[pos]->ce_mode; - } else if (resolve_gitlink_ref(path, "HEAD", &oid) == 0) { + } else if (repo_resolve_gitlink_ref(the_repository, path, + "HEAD", &oid) == 0) { mode = S_IFGITLINK; } } @@ -469,7 +469,8 @@ static int register_ref(const char *refname, const struct object_id *oid, static int read_bisect_refs(void) { - return for_each_ref_in("refs/bisect/", register_ref, NULL); + return refs_for_each_ref_in(get_main_ref_store(the_repository), + "refs/bisect/", register_ref, NULL); } static GIT_PATH_FUNC(git_path_bisect_names, "BISECT_NAMES") @@ -709,7 +710,7 @@ static enum bisect_error error_if_skipped_commits(struct commit_list *tried, static int is_expected_rev(const struct object_id *oid) { struct object_id expected_oid; - if (read_ref("BISECT_EXPECTED_REV", &expected_oid)) + if (refs_read_ref(get_main_ref_store(the_repository), "BISECT_EXPECTED_REV", &expected_oid)) return 0; return oideq(oid, &expected_oid); } @@ -721,11 +722,14 @@ enum bisect_error bisect_checkout(const struct object_id *bisect_rev, struct pretty_print_context pp = {0}; struct strbuf commit_msg = STRBUF_INIT; - update_ref(NULL, "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), NULL, + "BISECT_EXPECTED_REV", bisect_rev, NULL, 0, + UPDATE_REFS_DIE_ON_ERR); if (no_checkout) { - update_ref(NULL, "BISECT_HEAD", bisect_rev, NULL, 0, - UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), NULL, + "BISECT_HEAD", bisect_rev, NULL, 0, + UPDATE_REFS_DIE_ON_ERR); } else { struct child_process cmd = CHILD_PROCESS_INIT; @@ -1027,7 +1031,8 @@ enum bisect_error bisect_next_all(struct repository *r, const char *prefix) * If no_checkout is non-zero, the bisection process does not * checkout the trial commit but instead simply updates BISECT_HEAD. */ - int no_checkout = ref_exists("BISECT_HEAD"); + int no_checkout = refs_ref_exists(get_main_ref_store(the_repository), + "BISECT_HEAD"); unsigned bisect_flags = 0; read_bisect_terms(&term_bad, &term_good); @@ -1178,10 +1183,14 @@ int bisect_clean_state(void) /* There may be some refs packed during bisection */ struct string_list refs_for_removal = STRING_LIST_INIT_NODUP; - for_each_ref_in("refs/bisect", mark_for_removal, (void *) &refs_for_removal); + refs_for_each_ref_in(get_main_ref_store(the_repository), + "refs/bisect", mark_for_removal, + (void *) &refs_for_removal); string_list_append(&refs_for_removal, xstrdup("BISECT_HEAD")); string_list_append(&refs_for_removal, xstrdup("BISECT_EXPECTED_REV")); - result = delete_refs("bisect: remove", &refs_for_removal, REF_NO_DEREF); + result = refs_delete_refs(get_main_ref_store(the_repository), + "bisect: remove", &refs_for_removal, + REF_NO_DEREF); refs_for_removal.strdup_strings = 1; string_list_clear(&refs_for_removal, 0); unlink_or_warn(git_path_bisect_ancestors_ok()); @@ -2700,7 +2700,7 @@ static struct commit *dwim_reverse_initial(struct rev_info *revs, return NULL; /* Do we have HEAD? */ - if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) + if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING, &head_oid, NULL)) return NULL; head_commit = lookup_commit_reference_gently(revs->repo, &head_oid, 1); @@ -2803,7 +2803,7 @@ void setup_scoreboard(struct blame_scoreboard *sb, if (sb->final) { parent_oid = &sb->final->object.oid; } else { - if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) + if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING, &head_oid, NULL)) die("no such ref: HEAD"); parent_oid = &head_oid; } @@ -377,7 +377,7 @@ int validate_branchname(const char *name, struct strbuf *ref) exit(code); } - return ref_exists(ref->buf); + return refs_ref_exists(get_main_ref_store(the_repository), ref->buf); } static int initialized_checked_out_branches; @@ -623,11 +623,12 @@ void create_branch(struct repository *r, msg = xstrfmt("branch: Reset to %s", start_name); else msg = xstrfmt("branch: Created from %s", start_name); - transaction = ref_transaction_begin(&err); + transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!transaction || ref_transaction_update(transaction, ref.buf, &oid, forcing ? NULL : null_oid(), - 0, msg, &err) || + NULL, NULL, 0, msg, &err) || ref_transaction_commit(transaction, &err)) die("%s", err.buf); ref_transaction_free(transaction); diff --git a/builtin/am.c b/builtin/am.c index 4db2bc3..3683902 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -1001,7 +1001,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format, if (mkdir(state->dir, 0777) < 0 && errno != EEXIST) die_errno(_("failed to create directory '%s'"), state->dir); - delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); + refs_delete_ref(get_main_ref_store(the_repository), NULL, + "REBASE_HEAD", NULL, REF_NO_DEREF); if (split_mail(state, patch_format, paths, keep_cr) < 0) { am_destroy(state); @@ -1081,12 +1082,15 @@ static void am_setup(struct am_state *state, enum patch_format patch_format, if (!repo_get_oid(the_repository, "HEAD", &curr_head)) { write_state_text(state, "abort-safety", oid_to_hex(&curr_head)); if (!state->rebasing) - update_ref("am", "ORIG_HEAD", &curr_head, NULL, 0, - UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), + "am", "ORIG_HEAD", &curr_head, NULL, + 0, + UPDATE_REFS_DIE_ON_ERR); } else { write_state_text(state, "abort-safety", ""); if (!state->rebasing) - delete_ref(NULL, "ORIG_HEAD", NULL, 0); + refs_delete_ref(get_main_ref_store(the_repository), + NULL, "ORIG_HEAD", NULL, 0); } /* @@ -1119,7 +1123,8 @@ static void am_next(struct am_state *state) oidclr(&state->orig_commit); unlink(am_path(state, "original-commit")); - delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); + refs_delete_ref(get_main_ref_store(the_repository), NULL, + "REBASE_HEAD", NULL, REF_NO_DEREF); if (!repo_get_oid(the_repository, "HEAD", &head)) write_state_text(state, "abort-safety", oid_to_hex(&head)); @@ -1466,8 +1471,9 @@ static int parse_mail_rebase(struct am_state *state, const char *mail) oidcpy(&state->orig_commit, &commit_oid); write_state_text(state, "original-commit", oid_to_hex(&commit_oid)); - update_ref("am", "REBASE_HEAD", &commit_oid, - NULL, REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), "am", + "REBASE_HEAD", &commit_oid, + NULL, REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR); return 0; } @@ -1697,8 +1703,9 @@ static void do_commit(const struct am_state *state) strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg), state->msg); - update_ref(sb.buf, "HEAD", &commit, old_oid, 0, - UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), sb.buf, "HEAD", + &commit, old_oid, 0, + UPDATE_REFS_DIE_ON_ERR); if (state->rebasing) { FILE *fp = xfopen(am_path(state, "rewritten"), "a"); @@ -2175,7 +2182,8 @@ static void am_abort(struct am_state *state) am_rerere_clear(); - curr_branch = resolve_refdup("HEAD", 0, &curr_head, NULL); + curr_branch = refs_resolve_refdup(get_main_ref_store(the_repository), + "HEAD", 0, &curr_head, NULL); has_curr_head = curr_branch && !is_null_oid(&curr_head); if (!has_curr_head) oidcpy(&curr_head, the_hash_algo->empty_tree); @@ -2188,11 +2196,13 @@ static void am_abort(struct am_state *state) die(_("failed to clean index")); if (has_orig_head) - update_ref("am --abort", "HEAD", &orig_head, - has_curr_head ? &curr_head : NULL, 0, - UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), + "am --abort", "HEAD", &orig_head, + has_curr_head ? &curr_head : NULL, 0, + UPDATE_REFS_DIE_ON_ERR); else if (curr_branch) - delete_ref(NULL, curr_branch, NULL, REF_NO_DEREF); + refs_delete_ref(get_main_ref_store(the_repository), NULL, + curr_branch, NULL, REF_NO_DEREF); free(curr_branch); am_destroy(state); diff --git a/builtin/apply.c b/builtin/apply.c index 861a019..d623c52 100644 --- a/builtin/apply.c +++ b/builtin/apply.c @@ -1,6 +1,7 @@ #include "builtin.h" #include "gettext.h" #include "repository.h" +#include "hash.h" #include "apply.h" static const char * const apply_usage[] = { @@ -18,6 +19,15 @@ int cmd_apply(int argc, const char **argv, const char *prefix) if (init_apply_state(&state, the_repository, prefix)) exit(128); + /* + * We could to redo the "apply.c" machinery to make this + * arbitrary fallback unnecessary, but it is dubious that it + * is worth the effort. + * cf. https://lore.kernel.org/git/xmqqcypfcmn4.fsf@gitster.g/ + */ + if (!the_hash_algo) + repo_set_hash_algo(the_repository, GIT_HASH_SHA1); + argc = apply_parse_options(argc, argv, &state, &force_apply, &options, apply_usage); diff --git a/builtin/bisect.c b/builtin/bisect.c index f69c3f7..a58432b 100644 --- a/builtin/bisect.c +++ b/builtin/bisect.c @@ -243,7 +243,7 @@ static int bisect_reset(const char *commit) strbuf_addstr(&branch, commit); } - if (branch.len && !ref_exists("BISECT_HEAD")) { + if (branch.len && !refs_ref_exists(get_main_ref_store(the_repository), "BISECT_HEAD")) { struct child_process cmd = CHILD_PROCESS_INIT; cmd.git_cmd = 1; @@ -302,8 +302,8 @@ static int bisect_write(const char *state, const char *rev, goto finish; } - if (update_ref(NULL, tag.buf, &oid, NULL, 0, - UPDATE_REFS_MSG_ON_ERR)) { + if (refs_update_ref(get_main_ref_store(the_repository), NULL, tag.buf, &oid, NULL, 0, + UPDATE_REFS_MSG_ON_ERR)) { res = -1; goto finish; } @@ -416,11 +416,12 @@ static void bisect_status(struct bisect_state *state, char *bad_ref = xstrfmt("refs/bisect/%s", terms->term_bad); char *good_glob = xstrfmt("%s-*", terms->term_good); - if (ref_exists(bad_ref)) + if (refs_ref_exists(get_main_ref_store(the_repository), bad_ref)) state->nr_bad = 1; - for_each_glob_ref_in(inc_nr, good_glob, "refs/bisect/", - (void *) &state->nr_good); + refs_for_each_glob_ref_in(get_main_ref_store(the_repository), inc_nr, + good_glob, "refs/bisect/", + (void *) &state->nr_good); free(good_glob); free(bad_ref); @@ -574,9 +575,11 @@ static int prepare_revs(struct bisect_terms *terms, struct rev_info *revs) reset_revision_walk(); repo_init_revisions(the_repository, revs, NULL); setup_revisions(0, NULL, revs, NULL); - for_each_glob_ref_in(add_bisect_ref, bad, "refs/bisect/", &cb); + refs_for_each_glob_ref_in(get_main_ref_store(the_repository), + add_bisect_ref, bad, "refs/bisect/", &cb); cb.object_flags = UNINTERESTING; - for_each_glob_ref_in(add_bisect_ref, good, "refs/bisect/", &cb); + refs_for_each_glob_ref_in(get_main_ref_store(the_repository), + add_bisect_ref, good, "refs/bisect/", &cb); if (prepare_revision_walk(revs)) res = error(_("revision walk setup failed\n")); @@ -636,7 +639,7 @@ static int bisect_successful(struct bisect_terms *terms) char *bad_ref = xstrfmt("refs/bisect/%s",terms->term_bad); int res; - read_ref(bad_ref, &oid); + refs_read_ref(get_main_ref_store(the_repository), bad_ref, &oid); commit = lookup_commit_reference_by_name(bad_ref); repo_format_commit_message(the_repository, commit, "%s", &commit_name, &pp); @@ -779,7 +782,8 @@ static enum bisect_error bisect_start(struct bisect_terms *terms, int argc, /* * Verify HEAD */ - head = resolve_ref_unsafe("HEAD", 0, &head_oid, &flags); + head = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + "HEAD", 0, &head_oid, &flags); if (!head) if (repo_get_oid(the_repository, "HEAD", &head_oid)) return error(_("bad HEAD - I need a HEAD")); @@ -838,8 +842,8 @@ static enum bisect_error bisect_start(struct bisect_terms *terms, int argc, res = error(_("invalid ref: '%s'"), start_head.buf); goto finish; } - if (update_ref(NULL, "BISECT_HEAD", &oid, NULL, 0, - UPDATE_REFS_MSG_ON_ERR)) { + if (refs_update_ref(get_main_ref_store(the_repository), NULL, "BISECT_HEAD", &oid, NULL, 0, + UPDATE_REFS_MSG_ON_ERR)) { res = BISECT_FAILED; goto finish; } @@ -972,7 +976,7 @@ static enum bisect_error bisect_state(struct bisect_terms *terms, int argc, oid_array_append(&revs, &commit->object.oid); } - if (read_ref("BISECT_EXPECTED_REV", &expected)) + if (refs_read_ref(get_main_ref_store(the_repository), "BISECT_EXPECTED_REV", &expected)) verify_expected = 0; /* Ignore invalid file contents */ for (i = 0; i < revs.nr; i++) { @@ -982,7 +986,9 @@ static enum bisect_error bisect_state(struct bisect_terms *terms, int argc, } if (verify_expected && !oideq(&revs.oid[i], &expected)) { unlink_or_warn(git_path_bisect_ancestors_ok()); - delete_ref(NULL, "BISECT_EXPECTED_REV", NULL, REF_NO_DEREF); + refs_delete_ref(get_main_ref_store(the_repository), + NULL, "BISECT_EXPECTED_REV", NULL, + REF_NO_DEREF); verify_expected = 0; } } @@ -1179,13 +1185,15 @@ static int verify_good(const struct bisect_terms *terms, const char *command) struct object_id good_rev; struct object_id current_rev; char *good_glob = xstrfmt("%s-*", terms->term_good); - int no_checkout = ref_exists("BISECT_HEAD"); + int no_checkout = refs_ref_exists(get_main_ref_store(the_repository), + "BISECT_HEAD"); - for_each_glob_ref_in(get_first_good, good_glob, "refs/bisect/", - &good_rev); + refs_for_each_glob_ref_in(get_main_ref_store(the_repository), + get_first_good, good_glob, "refs/bisect/", + &good_rev); free(good_glob); - if (read_ref(no_checkout ? "BISECT_HEAD" : "HEAD", ¤t_rev)) + if (refs_read_ref(get_main_ref_store(the_repository), no_checkout ? "BISECT_HEAD" : "HEAD", ¤t_rev)) return -1; res = bisect_checkout(&good_rev, no_checkout); diff --git a/builtin/blame.c b/builtin/blame.c index 9aa7468..fadba1a 100644 --- a/builtin/blame.c +++ b/builtin/blame.c @@ -915,7 +915,6 @@ int cmd_blame(int argc, const char **argv, const char *prefix) struct range_set ranges; unsigned int range_i; long anchor; - const int hexsz = the_hash_algo->hexsz; long num_lines = 0; const char *str_usage = cmd_is_annotate ? annotate_usage : blame_usage; const char **opt_usage = cmd_is_annotate ? annotate_opt_usage : blame_opt_usage; @@ -973,11 +972,11 @@ parse_done: } else if (show_progress < 0) show_progress = isatty(2); - if (0 < abbrev && abbrev < hexsz) + if (0 < abbrev && abbrev < (int)the_hash_algo->hexsz) /* one more abbrev length is needed for the boundary commit */ abbrev++; else if (!abbrev) - abbrev = hexsz; + abbrev = the_hash_algo->hexsz; if (revs_file && read_ancestry(revs_file)) die_errno("reading graft file '%s' failed", revs_file); @@ -1093,8 +1092,8 @@ parse_done: struct commit *head_commit; struct object_id head_oid; - if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, - &head_oid, NULL) || + if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING, + &head_oid, NULL) || !(head_commit = lookup_commit_reference_gently(revs.repo, &head_oid, 1))) die("no such ref: HEAD"); diff --git a/builtin/branch.c b/builtin/branch.c index dd3e3a7..48cac74 100644 --- a/builtin/branch.c +++ b/builtin/branch.c @@ -148,8 +148,8 @@ static int branch_merged(int kind, const char *name, if (upstream && (reference_name = reference_name_to_free = - resolve_refdup(upstream, RESOLVE_REF_READING, - &oid, NULL)) != NULL) + refs_resolve_refdup(get_main_ref_store(the_repository), upstream, RESOLVE_REF_READING, + &oid, NULL)) != NULL) reference_rev = lookup_commit_reference(the_repository, &oid); } @@ -272,21 +272,24 @@ static int delete_branches(int argc, const char **argv, int force, int kinds, } } - target = resolve_refdup(name, - RESOLVE_REF_READING - | RESOLVE_REF_NO_RECURSE - | RESOLVE_REF_ALLOW_BAD_NAME, - &oid, &flags); + target = refs_resolve_refdup(get_main_ref_store(the_repository), + name, + RESOLVE_REF_READING + | RESOLVE_REF_NO_RECURSE + | RESOLVE_REF_ALLOW_BAD_NAME, + &oid, &flags); if (!target) { if (remote_branch) { error(_("remote-tracking branch '%s' not found"), bname.buf); } else { char *virtual_name = mkpathdup(fmt_remotes, bname.buf); - char *virtual_target = resolve_refdup(virtual_name, - RESOLVE_REF_READING - | RESOLVE_REF_NO_RECURSE - | RESOLVE_REF_ALLOW_BAD_NAME, - &oid, &flags); + char *virtual_target = refs_resolve_refdup(get_main_ref_store(the_repository), + virtual_name, + RESOLVE_REF_READING + | RESOLVE_REF_NO_RECURSE + | RESOLVE_REF_ALLOW_BAD_NAME, + &oid, + &flags); FREE_AND_NULL(virtual_name); if (virtual_target) @@ -317,13 +320,13 @@ static int delete_branches(int argc, const char **argv, int force, int kinds, free(target); } - if (delete_refs(NULL, &refs_to_delete, REF_NO_DEREF)) + if (refs_delete_refs(get_main_ref_store(the_repository), NULL, &refs_to_delete, REF_NO_DEREF)) ret = 1; for_each_string_list_item(item, &refs_to_delete) { char *describe_ref = item->util; char *name = item->string; - if (!ref_exists(name)) { + if (!refs_ref_exists(get_main_ref_store(the_repository), name)) { char *refname = name + branch_name_pos; if (!quiet) printf(remote_branch @@ -499,7 +502,8 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin static void print_current_branch_name(void) { int flags; - const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, &flags); + const char *refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + "HEAD", 0, NULL, &flags); const char *shortname; if (!refname) die(_("could not resolve HEAD")); @@ -555,7 +559,7 @@ static int replace_each_worktree_head_symref(struct worktree **worktrees, continue; refs = get_worktree_ref_store(worktrees[i]); - if (refs_create_symref(refs, "HEAD", newref, logmsg)) + if (refs_update_symref(refs, "HEAD", newref, logmsg)) ret = error(_("HEAD of working tree %s is not updated"), worktrees[i]->path); } @@ -580,7 +584,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int * Bad name --- this could be an attempt to rename a * ref that we used to allow to be created by accident. */ - if (ref_exists(oldref.buf)) + if (refs_ref_exists(get_main_ref_store(the_repository), oldref.buf)) recovery = 1; else { int code = die_message(_("invalid branch name: '%s'"), oldname); @@ -601,7 +605,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int } } - if ((copy || !(oldref_usage & IS_HEAD)) && !ref_exists(oldref.buf)) { + if ((copy || !(oldref_usage & IS_HEAD)) && !refs_ref_exists(get_main_ref_store(the_repository), oldref.buf)) { if (oldref_usage & IS_HEAD) die(_("no commit on branch '%s' yet"), oldname); else @@ -632,9 +636,9 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int oldref.buf, newref.buf); if (!copy && !(oldref_usage & IS_ORPHAN) && - rename_ref(oldref.buf, newref.buf, logmsg.buf)) + refs_rename_ref(get_main_ref_store(the_repository), oldref.buf, newref.buf, logmsg.buf)) die(_("branch rename failed")); - if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf)) + if (copy && refs_copy_existing_ref(get_main_ref_store(the_repository), oldref.buf, newref.buf, logmsg.buf)) die(_("branch copy failed")); if (recovery) { @@ -786,7 +790,8 @@ int cmd_branch(int argc, const char **argv, const char *prefix) track = git_branch_track; - head = resolve_refdup("HEAD", 0, &head_oid, NULL); + head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD", + 0, &head_oid, NULL); if (!head) die(_("failed to resolve HEAD as a valid ref")); if (!strcmp(head, "HEAD")) @@ -891,7 +896,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) } strbuf_addf(&branch_ref, "refs/heads/%s", branch_name); - if (!ref_exists(branch_ref.buf)) + if (!refs_ref_exists(get_main_ref_store(the_repository), branch_ref.buf)) error((!argc || branch_checked_out(branch_ref.buf)) ? _("no commit on branch '%s' yet") : _("no branch named '%s'"), @@ -936,7 +941,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) die(_("no such branch '%s'"), argv[0]); } - if (!ref_exists(branch->refname)) { + if (!refs_ref_exists(get_main_ref_store(the_repository), branch->refname)) { if (!argc || branch_checked_out(branch->refname)) die(_("no commit on branch '%s' yet"), branch->name); die(_("branch '%s' does not exist"), branch->name); diff --git a/builtin/bundle.c b/builtin/bundle.c index 3ad11dc..d5d41a8 100644 --- a/builtin/bundle.c +++ b/builtin/bundle.c @@ -140,6 +140,11 @@ static int cmd_bundle_verify(int argc, const char **argv, const char *prefix) { builtin_bundle_verify_usage, options, &bundle_file); /* bundle internals use argv[1] as further parameters */ + if (!startup_info->have_repository) { + ret = error(_("need a repository to verify a bundle")); + goto cleanup; + } + if ((bundle_fd = open_bundle(bundle_file, &header, &name)) < 0) { ret = 1; goto cleanup; diff --git a/builtin/checkout.c b/builtin/checkout.c index 8a1d13b..f90a4ca 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -645,7 +645,8 @@ static int checkout_paths(const struct checkout_opts *opts, rollback_lock_file(&lock_file); } - read_ref_full("HEAD", 0, &rev, NULL); + refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", 0, + &rev, NULL); head = lookup_commit_reference_gently(the_repository, &rev, 1); errs |= post_checkout_hook(head, head, 0); @@ -957,7 +958,8 @@ static void update_refs_for_switch(const struct checkout_opts *opts, int ret; struct strbuf err = STRBUF_INIT; - ret = safe_create_reflog(refname, &err); + ret = refs_create_reflog(get_main_ref_store(the_repository), + refname, &err); if (ret) { fprintf(stderr, _("Can not do reflog for '%s': %s\n"), opts->new_orphan_branch, err.buf); @@ -998,8 +1000,10 @@ static void update_refs_for_switch(const struct checkout_opts *opts, if (!strcmp(new_branch_info->name, "HEAD") && !new_branch_info->path && !opts->force_detach) { /* Nothing to do. */ } else if (opts->force_detach || !new_branch_info->path) { /* No longer on any branch. */ - update_ref(msg.buf, "HEAD", &new_branch_info->commit->object.oid, NULL, - REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), msg.buf, + "HEAD", &new_branch_info->commit->object.oid, + NULL, + REF_NO_DEREF, UPDATE_REFS_DIE_ON_ERR); if (!opts->quiet) { if (old_branch_info->path && advice_enabled(ADVICE_DETACHED_HEAD) && !opts->force_detach) @@ -1007,7 +1011,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts, describe_detached_head(_("HEAD is now at"), new_branch_info->commit); } } else if (new_branch_info->path) { /* Switch branches. */ - if (create_symref("HEAD", new_branch_info->path, msg.buf) < 0) + if (refs_update_symref(get_main_ref_store(the_repository), "HEAD", new_branch_info->path, msg.buf) < 0) die(_("unable to update HEAD")); if (!opts->quiet) { if (old_branch_info->path && !strcmp(new_branch_info->path, old_branch_info->path)) { @@ -1028,8 +1032,9 @@ static void update_refs_for_switch(const struct checkout_opts *opts, } } if (old_branch_info->path && old_branch_info->name) { - if (!ref_exists(old_branch_info->path) && reflog_exists(old_branch_info->path)) - delete_reflog(old_branch_info->path); + if (!refs_ref_exists(get_main_ref_store(the_repository), old_branch_info->path) && refs_reflog_exists(get_main_ref_store(the_repository), old_branch_info->path)) + refs_delete_reflog(get_main_ref_store(the_repository), + old_branch_info->path); } } remove_branch_state(the_repository, !opts->quiet); @@ -1128,7 +1133,8 @@ static void orphaned_commit_warning(struct commit *old_commit, struct commit *ne object->flags &= ~UNINTERESTING; add_pending_object(&revs, object, oid_to_hex(&object->oid)); - for_each_ref(add_pending_uninteresting_ref, &revs); + refs_for_each_ref(get_main_ref_store(the_repository), + add_pending_uninteresting_ref, &revs); if (new_commit) add_pending_oid(&revs, "HEAD", &new_commit->object.oid, @@ -1158,7 +1164,8 @@ static int switch_branches(const struct checkout_opts *opts, trace2_cmd_mode("branch"); memset(&old_branch_info, 0, sizeof(old_branch_info)); - old_branch_info.path = resolve_refdup("HEAD", 0, &rev, &flag); + old_branch_info.path = refs_resolve_refdup(get_main_ref_store(the_repository), + "HEAD", 0, &rev, &flag); if (old_branch_info.path) old_branch_info.commit = lookup_commit_reference_gently(the_repository, &rev, 1); if (!(flag & REF_ISSYMREF)) @@ -1246,7 +1253,7 @@ static void setup_new_branch_info_and_source_tree( setup_branch_path(new_branch_info); if (!check_refname_format(new_branch_info->path, 0) && - !read_ref(new_branch_info->path, &branch_rev)) + !refs_read_ref(get_main_ref_store(the_repository), new_branch_info->path, &branch_rev)) oidcpy(rev, &branch_rev); else /* not an existing branch */ @@ -1465,7 +1472,8 @@ static int switch_unborn_to_new_branch(const struct checkout_opts *opts) if (!opts->new_branch) die(_("You are on a branch yet to be born")); strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch); - status = create_symref("HEAD", branch_ref.buf, "checkout -b"); + status = refs_update_symref(get_main_ref_store(the_repository), + "HEAD", branch_ref.buf, "checkout -b"); strbuf_release(&branch_ref); if (!opts->quiet) fprintf(stderr, _("Switched to a new branch '%s'\n"), @@ -1552,7 +1560,8 @@ static void die_if_switching_to_a_branch_in_use(struct checkout_opts *opts, if (opts->ignore_other_worktrees) return; - head_ref = resolve_refdup("HEAD", 0, NULL, &flags); + head_ref = refs_resolve_refdup(get_main_ref_store(the_repository), + "HEAD", 0, NULL, &flags); if (head_ref && (!(flags & REF_ISSYMREF) || strcmp(head_ref, full_ref))) die_if_checked_out(full_ref, 1); free(head_ref); @@ -1633,7 +1642,7 @@ static int checkout_branch(struct checkout_opts *opts, struct object_id rev; int flag; - if (!read_ref_full("HEAD", 0, &rev, &flag) && + if (!refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", 0, &rev, &flag) && (flag & REF_ISSYMREF) && is_null_oid(&rev)) return switch_unborn_to_new_branch(opts); } diff --git a/builtin/clone.c b/builtin/clone.c index 93fdfc9..730b3ef 100644 --- a/builtin/clone.c +++ b/builtin/clone.c @@ -328,7 +328,20 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest, int src_len, dest_len; struct dir_iterator *iter; int iter_status; - struct strbuf realpath = STRBUF_INIT; + + /* + * Refuse copying directories by default which aren't owned by us. The + * code that performs either the copying or hardlinking is not prepared + * to handle various edge cases where an adversary may for example + * racily swap out files for symlinks. This can cause us to + * inadvertently use the wrong source file. + * + * Furthermore, even if we were prepared to handle such races safely, + * creating hardlinks across user boundaries is an inherently unsafe + * operation as the hardlinked files can be rewritten at will by the + * potentially-untrusted user. We thus refuse to do so by default. + */ + die_upon_dubious_ownership(NULL, NULL, src_repo); mkdir_if_missing(dest->buf, 0777); @@ -376,9 +389,27 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest, if (unlink(dest->buf) && errno != ENOENT) die_errno(_("failed to unlink '%s'"), dest->buf); if (!option_no_hardlinks) { - strbuf_realpath(&realpath, src->buf, 1); - if (!link(realpath.buf, dest->buf)) + if (!link(src->buf, dest->buf)) { + struct stat st; + + /* + * Sanity-check whether the created hardlink + * actually links to the expected file now. This + * catches time-of-check-time-of-use bugs in + * case the source file was meanwhile swapped. + */ + if (lstat(dest->buf, &st)) + die(_("hardlink cannot be checked at '%s'"), dest->buf); + if (st.st_mode != iter->st.st_mode || + st.st_ino != iter->st.st_ino || + st.st_dev != iter->st.st_dev || + st.st_size != iter->st.st_size || + st.st_uid != iter->st.st_uid || + st.st_gid != iter->st.st_gid) + die(_("hardlink different from source at '%s'"), dest->buf); + continue; + } if (option_local > 0) die_errno(_("failed to create link '%s'"), dest->buf); option_no_hardlinks = 1; @@ -391,8 +422,6 @@ static void copy_or_link_directory(struct strbuf *src, struct strbuf *dest, strbuf_setlen(src, src_len); die(_("failed to iterate over '%s'"), src->buf); } - - strbuf_release(&realpath); } static void clone_local(const char *src_repo, const char *dest_repo) @@ -538,7 +567,8 @@ static void write_remote_refs(const struct ref *local_refs) struct ref_transaction *t; struct strbuf err = STRBUF_INIT; - t = ref_transaction_begin(&err); + t = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!t) die("%s", err.buf); @@ -569,8 +599,9 @@ static void write_followtags(const struct ref *refs, const char *msg) OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT)) continue; - update_ref(msg, ref->name, &ref->old_oid, NULL, 0, - UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), msg, + ref->name, &ref->old_oid, NULL, 0, + UPDATE_REFS_DIE_ON_ERR); } } @@ -622,9 +653,9 @@ static void update_remote_refs(const struct ref *refs, struct strbuf head_ref = STRBUF_INIT; strbuf_addstr(&head_ref, branch_top); strbuf_addstr(&head_ref, "HEAD"); - if (create_symref(head_ref.buf, - remote_head_points_at->peer_ref->name, - msg) < 0) + if (refs_update_symref(get_main_ref_store(the_repository), head_ref.buf, + remote_head_points_at->peer_ref->name, + msg) < 0) die(_("unable to update %s"), head_ref.buf); strbuf_release(&head_ref); } @@ -636,33 +667,36 @@ static void update_head(const struct ref *our, const struct ref *remote, const char *head; if (our && skip_prefix(our->name, "refs/heads/", &head)) { /* Local default branch link */ - if (create_symref("HEAD", our->name, NULL) < 0) + if (refs_update_symref(get_main_ref_store(the_repository), "HEAD", our->name, NULL) < 0) die(_("unable to update HEAD")); if (!option_bare) { - update_ref(msg, "HEAD", &our->old_oid, NULL, 0, - UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), + msg, "HEAD", &our->old_oid, NULL, 0, + UPDATE_REFS_DIE_ON_ERR); install_branch_config(0, head, remote_name, our->name); } } else if (our) { struct commit *c = lookup_commit_reference(the_repository, &our->old_oid); /* --branch specifies a non-branch (i.e. tags), detach HEAD */ - update_ref(msg, "HEAD", &c->object.oid, NULL, REF_NO_DEREF, - UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), msg, + "HEAD", &c->object.oid, NULL, REF_NO_DEREF, + UPDATE_REFS_DIE_ON_ERR); } else if (remote) { /* * We know remote HEAD points to a non-branch, or * HEAD points to a branch but we don't know which one. * Detach HEAD in all these cases. */ - update_ref(msg, "HEAD", &remote->old_oid, NULL, REF_NO_DEREF, - UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), msg, + "HEAD", &remote->old_oid, NULL, REF_NO_DEREF, + UPDATE_REFS_DIE_ON_ERR); } else if (unborn && skip_prefix(unborn, "refs/heads/", &head)) { /* * Unborn head from remote; same as "our" case above except * that we have no ref to update. */ - if (create_symref("HEAD", unborn, NULL) < 0) + if (refs_update_symref(get_main_ref_store(the_repository), "HEAD", unborn, NULL) < 0) die(_("unable to update HEAD")); if (!option_bare) install_branch_config(0, head, remote_name, unborn); @@ -703,7 +737,8 @@ static int checkout(int submodule_progress, int filter_submodules) if (option_no_checkout) return 0; - head = resolve_refdup("HEAD", RESOLVE_REF_READING, &oid, NULL); + head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD", + RESOLVE_REF_READING, &oid, NULL); if (!head) { warning(_("remote HEAD refers to nonexistent ref, " "unable to checkout")); @@ -1424,6 +1459,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix) } else if (remote_head) { our_head_points_at = NULL; } else { + char *to_free = NULL; const char *branch; if (!mapped_refs) { @@ -1436,7 +1472,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix) "refs/heads/", &branch)) { unborn_head = xstrdup(transport_ls_refs_options.unborn_head_target); } else { - branch = git_default_branch_name(0); + branch = to_free = repo_default_branch_name(the_repository, 0); unborn_head = xstrfmt("refs/heads/%s", branch); } @@ -1452,6 +1488,8 @@ int cmd_clone(int argc, const char **argv, const char *prefix) * a match. */ our_head_points_at = find_remote_branch(mapped_refs, branch); + + free(to_free); } write_refspec_config(src_ref_prefix, our_head_points_at, @@ -1506,6 +1544,7 @@ int cmd_clone(int argc, const char **argv, const char *prefix) free(dir); free(path); free(repo_to_free); + UNLEAK(repo); junk_mode = JUNK_LEAVE_ALL; transport_ls_refs_options_release(&transport_ls_refs_options); diff --git a/builtin/commit.c b/builtin/commit.c index c294305..78bfae2 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -37,6 +37,7 @@ #include "commit-reach.h" #include "commit-graph.h" #include "pretty.h" +#include "trailer.h" static const char * const builtin_commit_usage[] = { N_("git commit [-a | --interactive | --patch] [-s] [-v] [-u<mode>] [--amend]\n" @@ -141,14 +142,6 @@ static struct strbuf message = STRBUF_INIT; static enum wt_status_format status_format = STATUS_FORMAT_UNSPECIFIED; -static int opt_pass_trailer(const struct option *opt, const char *arg, int unset) -{ - BUG_ON_OPT_NEG(unset); - - strvec_pushl(opt->value, "--trailer", arg, NULL); - return 0; -} - static int opt_parse_porcelain(const struct option *opt, const char *arg, int unset) { enum wt_status_format *value = (enum wt_status_format *)opt->value; @@ -1037,14 +1030,7 @@ static int prepare_to_commit(const char *index_file, const char *prefix, fclose(s->fp); if (trailer_args.nr) { - struct child_process run_trailer = CHILD_PROCESS_INIT; - - strvec_pushl(&run_trailer.args, "interpret-trailers", - "--in-place", "--no-divider", - git_path_commit_editmsg(), NULL); - strvec_pushv(&run_trailer.args, trailer_args.v); - run_trailer.git_cmd = 1; - if (run_command(&run_trailer)) + if (amend_file_with_trailers(git_path_commit_editmsg(), &trailer_args)) die(_("unable to pass trailers to --trailers")); strvec_clear(&trailer_args); } @@ -1672,7 +1658,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix) OPT_STRING(0, "fixup", &fixup_message, N_("[(amend|reword):]commit"), N_("use autosquash formatted message to fixup or amend/reword specified commit")), OPT_STRING(0, "squash", &squash_message, N_("commit"), N_("use autosquash formatted message to squash specified commit")), OPT_BOOL(0, "reset-author", &renew_authorship, N_("the commit is authored by me now (used with -C/-c/--amend)")), - OPT_CALLBACK_F(0, "trailer", &trailer_args, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG, opt_pass_trailer), + OPT_PASSTHRU_ARGV(0, "trailer", &trailer_args, N_("trailer"), N_("add custom trailer(s)"), PARSE_OPT_NONEG), OPT_BOOL('s', "signoff", &signoff, N_("add a Signed-off-by trailer")), OPT_FILENAME('t', "template", &template_file, N_("use specified template file")), OPT_BOOL('e', "edit", &edit_flag, N_("force edit of commit")), diff --git a/builtin/config.c b/builtin/config.c index 0015620..c38264c 100644 --- a/builtin/config.c +++ b/builtin/config.c @@ -16,59 +16,112 @@ #include "worktree.h" static const char *const builtin_config_usage[] = { - N_("git config [<options>]"), + N_("git config list [<file-option>] [<display-option>] [--includes]"), + N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>"), + N_("git config set [<file-option>] [--type=<type>] [--all] [--value=<value>] [--fixed-value] <name> <value>"), + N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>"), + N_("git config rename-section [<file-option>] <old-name> <new-name>"), + N_("git config remove-section [<file-option>] <name>"), + N_("git config edit [<file-option>]"), + N_("git config [<file-option>] --get-colorbool <name> [<stdout-is-tty>]"), NULL }; -static char *key; -static regex_t *key_regexp; -static const char *value_pattern; -static regex_t *regexp; -static int show_keys; -static int omit_values; -static int use_key_regexp; -static int do_all; -static int do_not_match; -static char delim = '='; -static char key_delim = ' '; -static char term = '\n'; - -static int use_global_config, use_system_config, use_local_config; -static int use_worktree_config; -static struct git_config_source given_config_source; -static int actions, type; -static char *default_value; -static int end_nul; -static int respect_includes_opt = -1; -static struct config_options config_options; -static int show_origin; -static int show_scope; -static int fixed_value; -static const char *comment; - -#define ACTION_GET (1<<0) -#define ACTION_GET_ALL (1<<1) -#define ACTION_GET_REGEXP (1<<2) -#define ACTION_REPLACE_ALL (1<<3) -#define ACTION_ADD (1<<4) -#define ACTION_UNSET (1<<5) -#define ACTION_UNSET_ALL (1<<6) -#define ACTION_RENAME_SECTION (1<<7) -#define ACTION_REMOVE_SECTION (1<<8) -#define ACTION_LIST (1<<9) -#define ACTION_EDIT (1<<10) -#define ACTION_SET (1<<11) -#define ACTION_SET_ALL (1<<12) -#define ACTION_GET_COLOR (1<<13) -#define ACTION_GET_COLORBOOL (1<<14) -#define ACTION_GET_URLMATCH (1<<15) - -/* - * The actions "ACTION_LIST | ACTION_GET_*" which may produce more than - * one line of output and which should therefore be paged. - */ -#define PAGING_ACTIONS (ACTION_LIST | ACTION_GET_ALL | \ - ACTION_GET_REGEXP | ACTION_GET_URLMATCH) +static const char *const builtin_config_list_usage[] = { + N_("git config list [<file-option>] [<display-option>] [--includes]"), + NULL +}; + +static const char *const builtin_config_get_usage[] = { + N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>"), + NULL +}; + +static const char *const builtin_config_set_usage[] = { + N_("git config set [<file-option>] [--type=<type>] [--comment=<message>] [--all] [--value=<value>] [--fixed-value] <name> <value>"), + NULL +}; + +static const char *const builtin_config_unset_usage[] = { + N_("git config unset [<file-option>] [--all] [--value=<value>] [--fixed-value] <name> <value>"), + NULL +}; + +static const char *const builtin_config_rename_section_usage[] = { + N_("git config rename-section [<file-option>] <old-name> <new-name>"), + NULL +}; + +static const char *const builtin_config_remove_section_usage[] = { + N_("git config remove-section [<file-option>] <name>"), + NULL +}; + +static const char *const builtin_config_edit_usage[] = { + N_("git config edit [<file-option>]"), + NULL +}; + +#define CONFIG_LOCATION_OPTIONS(opts) \ + OPT_GROUP(N_("Config file location")), \ + OPT_BOOL(0, "global", &opts.use_global_config, N_("use global config file")), \ + OPT_BOOL(0, "system", &opts.use_system_config, N_("use system config file")), \ + OPT_BOOL(0, "local", &opts.use_local_config, N_("use repository config file")), \ + OPT_BOOL(0, "worktree", &opts.use_worktree_config, N_("use per-worktree config file")), \ + OPT_STRING('f', "file", &opts.source.file, N_("file"), N_("use given config file")), \ + OPT_STRING(0, "blob", &opts.source.blob, N_("blob-id"), N_("read config from given blob object")) + +struct config_location_options { + struct git_config_source source; + struct config_options options; + char *file_to_free; + int use_global_config; + int use_system_config; + int use_local_config; + int use_worktree_config; + int respect_includes_opt; +}; +#define CONFIG_LOCATION_OPTIONS_INIT { \ + .respect_includes_opt = -1, \ +} + +#define CONFIG_TYPE_OPTIONS(type) \ + OPT_GROUP(N_("Type")), \ + OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type), \ + OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL), \ + OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT), \ + OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), \ + OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR), \ + OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH), \ + OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE) + +#define CONFIG_DISPLAY_OPTIONS(opts) \ + OPT_GROUP(N_("Display options")), \ + OPT_BOOL('z', "null", &opts.end_nul, N_("terminate values with NUL byte")), \ + OPT_BOOL(0, "name-only", &opts.omit_values, N_("show variable names only")), \ + OPT_BOOL(0, "show-origin", &opts.show_origin, N_("show origin of config (file, standard input, blob, command line)")), \ + OPT_BOOL(0, "show-scope", &opts.show_scope, N_("show scope of config (worktree, local, global, system, command)")), \ + OPT_BOOL(0, "show-names", &opts.show_keys, N_("show config keys in addition to their values")), \ + CONFIG_TYPE_OPTIONS(opts.type) + +struct config_display_options { + int end_nul; + int omit_values; + int show_origin; + int show_scope; + int show_keys; + int type; + char *default_value; + /* Populated via `display_options_init()`. */ + int term; + int delim; + int key_delim; +}; +#define CONFIG_DISPLAY_OPTIONS_INIT { \ + .term = '\n', \ + .delim = '=', \ + .key_delim = ' ', \ +} #define TYPE_BOOL 1 #define TYPE_INT 2 @@ -82,8 +135,6 @@ static const char *comment; { OPTION_CALLBACK, (s), (l), (v), NULL, (h), PARSE_OPT_NOARG | \ PARSE_OPT_NONEG, option_parse_type, (i) } -static NORETURN void usage_builtin_config(void); - static int option_parse_type(const struct option *opt, const char *arg, int unset) { @@ -128,61 +179,13 @@ static int option_parse_type(const struct option *opt, const char *arg, * --type=int'. */ error(_("only one type at a time")); - usage_builtin_config(); + exit(129); } *to_type = new_type; return 0; } -static struct option builtin_config_options[] = { - OPT_GROUP(N_("Config file location")), - OPT_BOOL(0, "global", &use_global_config, N_("use global config file")), - OPT_BOOL(0, "system", &use_system_config, N_("use system config file")), - OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")), - OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")), - OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")), - OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")), - OPT_GROUP(N_("Action")), - OPT_BIT(0, "get", &actions, N_("get value: name [value-pattern]"), ACTION_GET), - OPT_BIT(0, "get-all", &actions, N_("get all values: key [value-pattern]"), ACTION_GET_ALL), - OPT_BIT(0, "get-regexp", &actions, N_("get values for regexp: name-regex [value-pattern]"), ACTION_GET_REGEXP), - OPT_BIT(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH), - OPT_BIT(0, "replace-all", &actions, N_("replace all matching variables: name value [value-pattern]"), ACTION_REPLACE_ALL), - OPT_BIT(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD), - OPT_BIT(0, "unset", &actions, N_("remove a variable: name [value-pattern]"), ACTION_UNSET), - OPT_BIT(0, "unset-all", &actions, N_("remove all matches: name [value-pattern]"), ACTION_UNSET_ALL), - OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION), - OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION), - OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST), - OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")), - OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT), - OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR), - OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL), - OPT_GROUP(N_("Type")), - OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type), - OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL), - OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT), - OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), - OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR), - OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH), - OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE), - OPT_GROUP(N_("Other")), - OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")), - OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")), - OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")), - OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")), - OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")), - OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")), - OPT_STRING(0, "comment", &comment, N_("value"), N_("human-readable comment string (# will be prepended as needed)")), - OPT_END(), -}; - -static NORETURN void usage_builtin_config(void) -{ - usage_with_options(builtin_config_usage, builtin_config_options); -} - static void check_argc(int argc, int min, int max) { if (argc >= min && argc <= max) @@ -192,27 +195,29 @@ static void check_argc(int argc, int min, int max) else error(_("wrong number of arguments, should be from %d to %d"), min, max); - usage_builtin_config(); + exit(129); } -static void show_config_origin(const struct key_value_info *kvi, +static void show_config_origin(const struct config_display_options *opts, + const struct key_value_info *kvi, struct strbuf *buf) { - const char term = end_nul ? '\0' : '\t'; + const char term = opts->end_nul ? '\0' : '\t'; strbuf_addstr(buf, config_origin_type_name(kvi->origin_type)); strbuf_addch(buf, ':'); - if (end_nul) + if (opts->end_nul) strbuf_addstr(buf, kvi->filename ? kvi->filename : ""); else quote_c_style(kvi->filename ? kvi->filename : "", buf, NULL, 0); strbuf_addch(buf, term); } -static void show_config_scope(const struct key_value_info *kvi, +static void show_config_scope(const struct config_display_options *opts, + const struct key_value_info *kvi, struct strbuf *buf) { - const char term = end_nul ? '\0' : '\t'; + const char term = opts->end_nul ? '\0' : '\t'; const char *scope = config_scope_name(kvi->scope); strbuf_addstr(buf, N_(scope)); @@ -221,24 +226,25 @@ static void show_config_scope(const struct key_value_info *kvi, static int show_all_config(const char *key_, const char *value_, const struct config_context *ctx, - void *cb UNUSED) + void *cb) { + const struct config_display_options *opts = cb; const struct key_value_info *kvi = ctx->kvi; - if (show_origin || show_scope) { + if (opts->show_origin || opts->show_scope) { struct strbuf buf = STRBUF_INIT; - if (show_scope) - show_config_scope(kvi, &buf); - if (show_origin) - show_config_origin(kvi, &buf); + if (opts->show_scope) + show_config_scope(opts, kvi, &buf); + if (opts->show_origin) + show_config_origin(opts, kvi, &buf); /* Use fwrite as "buf" can contain \0's if "end_null" is set. */ fwrite(buf.buf, 1, buf.len, stdout); strbuf_release(&buf); } - if (!omit_values && value_) - printf("%s%c%s%c", key_, delim, value_, term); + if (!opts->omit_values && value_) + printf("%s%c%s%c", key_, opts->delim, value_, opts->term); else - printf("%s%c", key_, term); + printf("%s%c", key_, opts->term); return 0; } @@ -248,26 +254,27 @@ struct strbuf_list { int alloc; }; -static int format_config(struct strbuf *buf, const char *key_, +static int format_config(const struct config_display_options *opts, + struct strbuf *buf, const char *key_, const char *value_, const struct key_value_info *kvi) { - if (show_scope) - show_config_scope(kvi, buf); - if (show_origin) - show_config_origin(kvi, buf); - if (show_keys) + if (opts->show_scope) + show_config_scope(opts, kvi, buf); + if (opts->show_origin) + show_config_origin(opts, kvi, buf); + if (opts->show_keys) strbuf_addstr(buf, key_); - if (!omit_values) { - if (show_keys) - strbuf_addch(buf, key_delim); + if (!opts->omit_values) { + if (opts->show_keys) + strbuf_addch(buf, opts->key_delim); - if (type == TYPE_INT) + if (opts->type == TYPE_INT) strbuf_addf(buf, "%"PRId64, git_config_int64(key_, value_ ? value_ : "", kvi)); - else if (type == TYPE_BOOL) + else if (opts->type == TYPE_BOOL) strbuf_addstr(buf, git_config_bool(key_, value_) ? "true" : "false"); - else if (type == TYPE_BOOL_OR_INT) { + else if (opts->type == TYPE_BOOL_OR_INT) { int is_bool, v; v = git_config_bool_or_int(key_, value_, kvi, &is_bool); @@ -275,24 +282,24 @@ static int format_config(struct strbuf *buf, const char *key_, strbuf_addstr(buf, v ? "true" : "false"); else strbuf_addf(buf, "%d", v); - } else if (type == TYPE_BOOL_OR_STR) { + } else if (opts->type == TYPE_BOOL_OR_STR) { int v = git_parse_maybe_bool(value_); if (v < 0) strbuf_addstr(buf, value_); else strbuf_addstr(buf, v ? "true" : "false"); - } else if (type == TYPE_PATH) { + } else if (opts->type == TYPE_PATH) { const char *v; if (git_config_pathname(&v, key_, value_) < 0) return -1; strbuf_addstr(buf, v); free((char *)v); - } else if (type == TYPE_EXPIRY_DATE) { + } else if (opts->type == TYPE_EXPIRY_DATE) { timestamp_t t; if (git_config_expiry_date(&t, key_, value_) < 0) return -1; strbuf_addf(buf, "%"PRItime, t); - } else if (type == TYPE_COLOR) { + } else if (opts->type == TYPE_COLOR) { char v[COLOR_MAXLEN]; if (git_config_color(v, key_, value_) < 0) return -1; @@ -301,43 +308,73 @@ static int format_config(struct strbuf *buf, const char *key_, strbuf_addstr(buf, value_); } else { /* Just show the key name; back out delimiter */ - if (show_keys) + if (opts->show_keys) strbuf_setlen(buf, buf->len - 1); } } - strbuf_addch(buf, term); + strbuf_addch(buf, opts->term); return 0; } +#define GET_VALUE_ALL (1 << 0) +#define GET_VALUE_KEY_REGEXP (1 << 1) + +struct collect_config_data { + const struct config_display_options *display_opts; + struct strbuf_list *values; + const char *value_pattern; + const char *key; + regex_t *regexp; + regex_t *key_regexp; + int do_not_match; + unsigned get_value_flags; + unsigned flags; +}; + static int collect_config(const char *key_, const char *value_, const struct config_context *ctx, void *cb) { - struct strbuf_list *values = cb; + struct collect_config_data *data = cb; + struct strbuf_list *values = data->values; const struct key_value_info *kvi = ctx->kvi; - if (!use_key_regexp && strcmp(key_, key)) + if (!(data->get_value_flags & GET_VALUE_KEY_REGEXP) && + strcmp(key_, data->key)) return 0; - if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0)) + if ((data->get_value_flags & GET_VALUE_KEY_REGEXP) && + regexec(data->key_regexp, key_, 0, NULL, 0)) return 0; - if (fixed_value && strcmp(value_pattern, (value_?value_:""))) + if ((data->flags & CONFIG_FLAGS_FIXED_VALUE) && + strcmp(data->value_pattern, (value_?value_:""))) return 0; - if (regexp != NULL && - (do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0))) + if (data->regexp && + (data->do_not_match ^ !!regexec(data->regexp, (value_?value_:""), 0, NULL, 0))) return 0; ALLOC_GROW(values->items, values->nr + 1, values->alloc); strbuf_init(&values->items[values->nr], 0); - return format_config(&values->items[values->nr++], key_, value_, kvi); + return format_config(data->display_opts, &values->items[values->nr++], + key_, value_, kvi); } -static int get_value(const char *key_, const char *regex_, unsigned flags) +static int get_value(const struct config_location_options *opts, + const struct config_display_options *display_opts, + const char *key_, const char *regex_, + unsigned get_value_flags, unsigned flags) { int ret = CONFIG_GENERIC_ERROR; struct strbuf_list values = {NULL}; + struct collect_config_data data = { + .display_opts = display_opts, + .values = &values, + .get_value_flags = get_value_flags, + .flags = flags, + }; + char *key = NULL; int i; - if (use_key_regexp) { + if (get_value_flags & GET_VALUE_KEY_REGEXP) { char *tl; /* @@ -354,10 +391,10 @@ static int get_value(const char *key_, const char *regex_, unsigned flags) for (tl = key; *tl && *tl != '.'; tl++) *tl = tolower(*tl); - key_regexp = (regex_t*)xmalloc(sizeof(regex_t)); - if (regcomp(key_regexp, key, REG_EXTENDED)) { + data.key_regexp = (regex_t*)xmalloc(sizeof(regex_t)); + if (regcomp(data.key_regexp, key, REG_EXTENDED)) { error(_("invalid key pattern: %s"), key_); - FREE_AND_NULL(key_regexp); + FREE_AND_NULL(data.key_regexp); ret = CONFIG_INVALID_PATTERN; goto free_strings; } @@ -366,30 +403,32 @@ static int get_value(const char *key_, const char *regex_, unsigned flags) ret = CONFIG_INVALID_KEY; goto free_strings; } + + data.key = key; } if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE)) - value_pattern = regex_; + data.value_pattern = regex_; else if (regex_) { if (regex_[0] == '!') { - do_not_match = 1; + data.do_not_match = 1; regex_++; } - regexp = (regex_t*)xmalloc(sizeof(regex_t)); - if (regcomp(regexp, regex_, REG_EXTENDED)) { + data.regexp = (regex_t*)xmalloc(sizeof(regex_t)); + if (regcomp(data.regexp, regex_, REG_EXTENDED)) { error(_("invalid pattern: %s"), regex_); - FREE_AND_NULL(regexp); + FREE_AND_NULL(data.regexp); ret = CONFIG_INVALID_PATTERN; goto free_strings; } } - config_with_options(collect_config, &values, - &given_config_source, the_repository, - &config_options); + config_with_options(collect_config, &data, + &opts->source, the_repository, + &opts->options); - if (!values.nr && default_value) { + if (!values.nr && display_opts->default_value) { struct key_value_info kvi = KVI_INIT; struct strbuf *item; @@ -397,16 +436,17 @@ static int get_value(const char *key_, const char *regex_, unsigned flags) ALLOC_GROW(values.items, values.nr + 1, values.alloc); item = &values.items[values.nr++]; strbuf_init(item, 0); - if (format_config(item, key_, default_value, &kvi) < 0) + if (format_config(display_opts, item, key_, + display_opts->default_value, &kvi) < 0) die(_("failed to format default config value: %s"), - default_value); + display_opts->default_value); } ret = !values.nr; for (i = 0; i < values.nr; i++) { struct strbuf *buf = values.items + i; - if (do_all || i == values.nr - 1) + if ((get_value_flags & GET_VALUE_ALL) || i == values.nr - 1) fwrite(buf->buf, 1, buf->len, stdout); strbuf_release(buf); } @@ -414,20 +454,20 @@ static int get_value(const char *key_, const char *regex_, unsigned flags) free_strings: free(key); - if (key_regexp) { - regfree(key_regexp); - free(key_regexp); + if (data.key_regexp) { + regfree(data.key_regexp); + free(data.key_regexp); } - if (regexp) { - regfree(regexp); - free(regexp); + if (data.regexp) { + regfree(data.regexp); + free(data.regexp); } return ret; } static char *normalize_value(const char *key, const char *value, - struct key_value_info *kvi) + int type, struct key_value_info *kvi) { if (!value) return NULL; @@ -478,97 +518,113 @@ static char *normalize_value(const char *key, const char *value, BUG("cannot normalize type %d", type); } -static int get_color_found; -static const char *get_color_slot; -static const char *get_colorbool_slot; -static char parsed_color[COLOR_MAXLEN]; +struct get_color_config_data { + int get_color_found; + const char *get_color_slot; + char parsed_color[COLOR_MAXLEN]; +}; static int git_get_color_config(const char *var, const char *value, const struct config_context *ctx UNUSED, - void *cb UNUSED) + void *cb) { - if (!strcmp(var, get_color_slot)) { + struct get_color_config_data *data = cb; + + if (!strcmp(var, data->get_color_slot)) { if (!value) config_error_nonbool(var); - if (color_parse(value, parsed_color) < 0) + if (color_parse(value, data->parsed_color) < 0) return -1; - get_color_found = 1; + data->get_color_found = 1; } return 0; } -static void get_color(const char *var, const char *def_color) +static void get_color(const struct config_location_options *opts, + const char *var, const char *def_color) { - get_color_slot = var; - get_color_found = 0; - parsed_color[0] = '\0'; - config_with_options(git_get_color_config, NULL, - &given_config_source, the_repository, - &config_options); - - if (!get_color_found && def_color) { - if (color_parse(def_color, parsed_color) < 0) + struct get_color_config_data data = { + .get_color_slot = var, + .parsed_color[0] = '\0', + }; + + config_with_options(git_get_color_config, &data, + &opts->source, the_repository, + &opts->options); + + if (!data.get_color_found && def_color) { + if (color_parse(def_color, data.parsed_color) < 0) die(_("unable to parse default color value")); } - fputs(parsed_color, stdout); + fputs(data.parsed_color, stdout); } -static int get_colorbool_found; -static int get_diff_color_found; -static int get_color_ui_found; +struct get_colorbool_config_data { + int get_colorbool_found; + int get_diff_color_found; + int get_color_ui_found; + const char *get_colorbool_slot; +}; + static int git_get_colorbool_config(const char *var, const char *value, const struct config_context *ctx UNUSED, - void *data UNUSED) + void *cb) { - if (!strcmp(var, get_colorbool_slot)) - get_colorbool_found = git_config_colorbool(var, value); + struct get_colorbool_config_data *data = cb; + + if (!strcmp(var, data->get_colorbool_slot)) + data->get_colorbool_found = git_config_colorbool(var, value); else if (!strcmp(var, "diff.color")) - get_diff_color_found = git_config_colorbool(var, value); + data->get_diff_color_found = git_config_colorbool(var, value); else if (!strcmp(var, "color.ui")) - get_color_ui_found = git_config_colorbool(var, value); + data->get_color_ui_found = git_config_colorbool(var, value); return 0; } -static int get_colorbool(const char *var, int print) +static int get_colorbool(const struct config_location_options *opts, + const char *var, int print) { - get_colorbool_slot = var; - get_colorbool_found = -1; - get_diff_color_found = -1; - get_color_ui_found = -1; - config_with_options(git_get_colorbool_config, NULL, - &given_config_source, the_repository, - &config_options); - - if (get_colorbool_found < 0) { - if (!strcmp(get_colorbool_slot, "color.diff")) - get_colorbool_found = get_diff_color_found; - if (get_colorbool_found < 0) - get_colorbool_found = get_color_ui_found; - } - - if (get_colorbool_found < 0) + struct get_colorbool_config_data data = { + .get_colorbool_slot = var, + .get_colorbool_found = -1, + .get_diff_color_found = -1, + .get_color_ui_found = -1, + }; + + config_with_options(git_get_colorbool_config, &data, + &opts->source, the_repository, + &opts->options); + + if (data.get_colorbool_found < 0) { + if (!strcmp(data.get_colorbool_slot, "color.diff")) + data.get_colorbool_found = data.get_diff_color_found; + if (data.get_colorbool_found < 0) + data.get_colorbool_found = data.get_color_ui_found; + } + + if (data.get_colorbool_found < 0) /* default value if none found in config */ - get_colorbool_found = GIT_COLOR_AUTO; + data.get_colorbool_found = GIT_COLOR_AUTO; - get_colorbool_found = want_color(get_colorbool_found); + data.get_colorbool_found = want_color(data.get_colorbool_found); if (print) { - printf("%s\n", get_colorbool_found ? "true" : "false"); + printf("%s\n", data.get_colorbool_found ? "true" : "false"); return 0; } else - return get_colorbool_found ? 0 : 1; + return data.get_colorbool_found ? 0 : 1; } -static void check_write(void) +static void check_write(const struct git_config_source *source) { - if (!given_config_source.file && !startup_info->have_repository) + if (!source->file && !startup_info->have_repository) die(_("not in a git directory")); - if (given_config_source.use_stdin) + if (source->use_stdin) die(_("writing to stdin is not supported")); - if (given_config_source.blob) + if (source->blob) die(_("writing config blobs is not supported")); } @@ -605,10 +661,13 @@ static int urlmatch_collect_fn(const char *var, const char *value, return 0; } -static int get_urlmatch(const char *var, const char *url) +static int get_urlmatch(const struct config_location_options *opts, + const struct config_display_options *_display_opts, + const char *var, const char *url) { int ret; char *section_tail; + struct config_display_options display_opts = *_display_opts; struct string_list_item *item; struct urlmatch_config config = URLMATCH_CONFIG_INIT; struct string_list values = STRING_LIST_INIT_DUP; @@ -625,15 +684,15 @@ static int get_urlmatch(const char *var, const char *url) if (section_tail) { *section_tail = '\0'; config.key = section_tail + 1; - show_keys = 0; + display_opts.show_keys = 0; } else { config.key = NULL; - show_keys = 1; + display_opts.show_keys = 1; } config_with_options(urlmatch_config_entry, &config, - &given_config_source, the_repository, - &config_options); + &opts->source, the_repository, + &opts->options); ret = !values.nr; @@ -641,7 +700,7 @@ static int get_urlmatch(const char *var, const char *url) struct urlmatch_current_candidate_value *matched = item->util; struct strbuf buf = STRBUF_INIT; - format_config(&buf, item->string, + format_config(&display_opts, &buf, item->string, matched->value_is_null ? NULL : matched->value.buf, &matched->kvi); fwrite(buf.buf, 1, buf.len, stdout); @@ -671,47 +730,39 @@ static char *default_user_config(void) return strbuf_detach(&buf, NULL); } -int cmd_config(int argc, const char **argv, const char *prefix) +static void location_options_init(struct config_location_options *opts, + const char *prefix) { - int nongit = !startup_info->have_repository; - char *value = NULL; - int flags = 0; - int ret = 0; - struct key_value_info default_kvi = KVI_INIT; - - given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT)); + if (!opts->source.file) + opts->source.file = opts->file_to_free = + xstrdup_or_null(getenv(CONFIG_ENVIRONMENT)); - argc = parse_options(argc, argv, prefix, builtin_config_options, - builtin_config_usage, - PARSE_OPT_STOP_AT_NON_OPTION); - - if (use_global_config + use_system_config + use_local_config + - use_worktree_config + - !!given_config_source.file + !!given_config_source.blob > 1) { + if (opts->use_global_config + opts->use_system_config + + opts->use_local_config + opts->use_worktree_config + + !!opts->source.file + !!opts->source.blob > 1) { error(_("only one config file at a time")); - usage_builtin_config(); + exit(129); } - if (nongit) { - if (use_local_config) + if (!startup_info->have_repository) { + if (opts->use_local_config) die(_("--local can only be used inside a git repository")); - if (given_config_source.blob) + if (opts->source.blob) die(_("--blob can only be used inside a git repository")); - if (use_worktree_config) + if (opts->use_worktree_config) die(_("--worktree can only be used inside a git repository")); - } - if (given_config_source.file && - !strcmp(given_config_source.file, "-")) { - given_config_source.file = NULL; - given_config_source.use_stdin = 1; - given_config_source.scope = CONFIG_SCOPE_COMMAND; + if (opts->source.file && + !strcmp(opts->source.file, "-")) { + opts->source.file = NULL; + opts->source.use_stdin = 1; + opts->source.scope = CONFIG_SCOPE_COMMAND; } - if (use_global_config) { - given_config_source.file = git_global_config(); - if (!given_config_source.file) + if (opts->use_global_config) { + opts->source.file = opts->file_to_free = git_global_config(); + if (!opts->source.file) /* * It is unknown if HOME/.gitconfig exists, so * we do not know if we should write to XDG @@ -719,17 +770,18 @@ int cmd_config(int argc, const char **argv, const char *prefix) * is set and points at a sane location. */ die(_("$HOME not set")); - given_config_source.scope = CONFIG_SCOPE_GLOBAL; - } else if (use_system_config) { - given_config_source.file = git_system_config(); - given_config_source.scope = CONFIG_SCOPE_SYSTEM; - } else if (use_local_config) { - given_config_source.file = git_pathdup("config"); - given_config_source.scope = CONFIG_SCOPE_LOCAL; - } else if (use_worktree_config) { + opts->source.scope = CONFIG_SCOPE_GLOBAL; + } else if (opts->use_system_config) { + opts->source.file = opts->file_to_free = git_system_config(); + opts->source.scope = CONFIG_SCOPE_SYSTEM; + } else if (opts->use_local_config) { + opts->source.file = opts->file_to_free = git_pathdup("config"); + opts->source.scope = CONFIG_SCOPE_LOCAL; + } else if (opts->use_worktree_config) { struct worktree **worktrees = get_worktrees(); if (the_repository->repository_format_worktree_config) - given_config_source.file = git_pathdup("config.worktree"); + opts->source.file = opts->file_to_free = + git_pathdup("config.worktree"); else if (worktrees[0] && worktrees[1]) die(_("--worktree cannot be used with multiple " "working trees unless the config\n" @@ -737,76 +789,439 @@ int cmd_config(int argc, const char **argv, const char *prefix) "Please read \"CONFIGURATION FILE\"\n" "section in \"git help worktree\" for details")); else - given_config_source.file = git_pathdup("config"); - given_config_source.scope = CONFIG_SCOPE_LOCAL; + opts->source.file = opts->file_to_free = + git_pathdup("config"); + opts->source.scope = CONFIG_SCOPE_LOCAL; free_worktrees(worktrees); - } else if (given_config_source.file) { - if (!is_absolute_path(given_config_source.file) && prefix) - given_config_source.file = - prefix_filename(prefix, given_config_source.file); - given_config_source.scope = CONFIG_SCOPE_COMMAND; - } else if (given_config_source.blob) { - given_config_source.scope = CONFIG_SCOPE_COMMAND; + } else if (opts->source.file) { + if (!is_absolute_path(opts->source.file) && prefix) + opts->source.file = opts->file_to_free = + prefix_filename(prefix, opts->source.file); + opts->source.scope = CONFIG_SCOPE_COMMAND; + } else if (opts->source.blob) { + opts->source.scope = CONFIG_SCOPE_COMMAND; } - if (respect_includes_opt == -1) - config_options.respect_includes = !given_config_source.file; + if (opts->respect_includes_opt == -1) + opts->options.respect_includes = !opts->source.file; else - config_options.respect_includes = respect_includes_opt; - if (!nongit) { - config_options.commondir = get_git_common_dir(); - config_options.git_dir = get_git_dir(); + opts->options.respect_includes = opts->respect_includes_opt; + if (startup_info->have_repository) { + opts->options.commondir = get_git_common_dir(); + opts->options.git_dir = get_git_dir(); + } +} + +static void location_options_release(struct config_location_options *opts) +{ + free(opts->file_to_free); +} + +static void display_options_init(struct config_display_options *opts) +{ + if (opts->end_nul) { + opts->term = '\0'; + opts->delim = '\n'; + opts->key_delim = '\n'; } +} - if (end_nul) { - term = '\0'; - delim = '\n'; - key_delim = '\n'; +static int cmd_config_list(int argc, const char **argv, const char *prefix) +{ + struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; + struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT; + struct option opts[] = { + CONFIG_LOCATION_OPTIONS(location_opts), + CONFIG_DISPLAY_OPTIONS(display_opts), + OPT_GROUP(N_("Other")), + OPT_BOOL(0, "includes", &location_opts.respect_includes_opt, + N_("respect include directives on lookup")), + OPT_END(), + }; + + argc = parse_options(argc, argv, prefix, opts, builtin_config_list_usage, 0); + check_argc(argc, 0, 0); + + location_options_init(&location_opts, prefix); + display_options_init(&display_opts); + + setup_auto_pager("config", 1); + + if (config_with_options(show_all_config, &display_opts, + &location_opts.source, the_repository, + &location_opts.options) < 0) { + if (location_opts.source.file) + die_errno(_("unable to read config file '%s'"), + location_opts.source.file); + else + die(_("error processing config file(s)")); } - if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && type) { - error(_("--get-color and variable type are incoherent")); - usage_builtin_config(); + location_options_release(&location_opts); + return 0; +} + +static int cmd_config_get(int argc, const char **argv, const char *prefix) +{ + struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; + struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT; + const char *value_pattern = NULL, *url = NULL; + int flags = 0; + unsigned get_value_flags = 0; + struct option opts[] = { + CONFIG_LOCATION_OPTIONS(location_opts), + OPT_GROUP(N_("Filter options")), + OPT_BIT(0, "all", &get_value_flags, N_("return all values for multi-valued config options"), GET_VALUE_ALL), + OPT_BIT(0, "regexp", &get_value_flags, N_("interpret the name as a regular expression"), GET_VALUE_KEY_REGEXP), + OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")), + OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), + OPT_STRING(0, "url", &url, N_("URL"), N_("show config matching the given URL")), + CONFIG_DISPLAY_OPTIONS(display_opts), + OPT_GROUP(N_("Other")), + OPT_BOOL(0, "includes", &location_opts.respect_includes_opt, + N_("respect include directives on lookup")), + OPT_STRING(0, "default", &display_opts.default_value, + N_("value"), N_("use default value when missing entry")), + OPT_END(), + }; + int ret; + + argc = parse_options(argc, argv, prefix, opts, builtin_config_get_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + check_argc(argc, 1, 1); + + if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern) + die(_("--fixed-value only applies with 'value-pattern'")); + if (display_opts.default_value && + ((get_value_flags & GET_VALUE_ALL) || url)) + die(_("--default= cannot be used with --all or --url=")); + if (url && ((get_value_flags & GET_VALUE_ALL) || + (get_value_flags & GET_VALUE_KEY_REGEXP) || + value_pattern)) + die(_("--url= cannot be used with --all, --regexp or --value")); + + location_options_init(&location_opts, prefix); + display_options_init(&display_opts); + + setup_auto_pager("config", 1); + + if (url) + ret = get_urlmatch(&location_opts, &display_opts, argv[0], url); + else + ret = get_value(&location_opts, &display_opts, argv[0], value_pattern, + get_value_flags, flags); + + location_options_release(&location_opts); + return ret; +} + +static int cmd_config_set(int argc, const char **argv, const char *prefix) +{ + struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; + const char *value_pattern = NULL, *comment_arg = NULL; + char *comment = NULL; + int flags = 0, append = 0, type = 0; + struct option opts[] = { + CONFIG_LOCATION_OPTIONS(location_opts), + CONFIG_TYPE_OPTIONS(type), + OPT_GROUP(N_("Filter")), + OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE), + OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")), + OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), + OPT_GROUP(N_("Other")), + OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")), + OPT_BOOL(0, "append", &append, N_("add a new line without altering any existing values")), + OPT_END(), + }; + struct key_value_info default_kvi = KVI_INIT; + char *value; + int ret; + + argc = parse_options(argc, argv, prefix, opts, builtin_config_set_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + check_argc(argc, 2, 2); + + if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern) + die(_("--fixed-value only applies with --value=<pattern>")); + if (append && value_pattern) + die(_("--append cannot be used with --value=<pattern>")); + if (append) + value_pattern = CONFIG_REGEX_NONE; + + comment = git_config_prepare_comment_string(comment_arg); + + location_options_init(&location_opts, prefix); + check_write(&location_opts.source); + + value = normalize_value(argv[0], argv[1], type, &default_kvi); + + if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) { + ret = git_config_set_multivar_in_file_gently(location_opts.source.file, + argv[0], value, value_pattern, + comment, flags); + } else { + ret = git_config_set_in_file_gently(location_opts.source.file, + argv[0], comment, value); + if (ret == CONFIG_NOTHING_SET) + error(_("cannot overwrite multiple values with a single value\n" + " Use a regexp, --add or --replace-all to change %s."), argv[0]); } - if (HAS_MULTI_BITS(actions)) { - error(_("only one action at a time")); - usage_builtin_config(); + location_options_release(&location_opts); + free(comment); + free(value); + return ret; +} + +static int cmd_config_unset(int argc, const char **argv, const char *prefix) +{ + struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; + const char *value_pattern = NULL; + int flags = 0; + struct option opts[] = { + CONFIG_LOCATION_OPTIONS(location_opts), + OPT_GROUP(N_("Filter")), + OPT_BIT(0, "all", &flags, N_("replace multi-valued config option with new value"), CONFIG_FLAGS_MULTI_REPLACE), + OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")), + OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), + OPT_END(), + }; + int ret; + + argc = parse_options(argc, argv, prefix, opts, builtin_config_unset_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + check_argc(argc, 1, 1); + + if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern) + die(_("--fixed-value only applies with 'value-pattern'")); + + location_options_init(&location_opts, prefix); + check_write(&location_opts.source); + + if ((flags & CONFIG_FLAGS_MULTI_REPLACE) || value_pattern) + ret = git_config_set_multivar_in_file_gently(location_opts.source.file, + argv[0], NULL, value_pattern, + NULL, flags); + else + ret = git_config_set_in_file_gently(location_opts.source.file, argv[0], + NULL, NULL); + + location_options_release(&location_opts); + return ret; +} + +static int cmd_config_rename_section(int argc, const char **argv, const char *prefix) +{ + struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; + struct option opts[] = { + CONFIG_LOCATION_OPTIONS(location_opts), + OPT_END(), + }; + int ret; + + argc = parse_options(argc, argv, prefix, opts, builtin_config_rename_section_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + check_argc(argc, 2, 2); + + location_options_init(&location_opts, prefix); + check_write(&location_opts.source); + + ret = git_config_rename_section_in_file(location_opts.source.file, + argv[0], argv[1]); + if (ret < 0) + goto out; + else if (!ret) + die(_("no such section: %s"), argv[0]); + ret = 0; + +out: + location_options_release(&location_opts); + return ret; +} + +static int cmd_config_remove_section(int argc, const char **argv, const char *prefix) +{ + struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; + struct option opts[] = { + CONFIG_LOCATION_OPTIONS(location_opts), + OPT_END(), + }; + int ret; + + argc = parse_options(argc, argv, prefix, opts, builtin_config_remove_section_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + check_argc(argc, 1, 1); + + location_options_init(&location_opts, prefix); + check_write(&location_opts.source); + + ret = git_config_rename_section_in_file(location_opts.source.file, + argv[0], NULL); + if (ret < 0) + goto out; + else if (!ret) + die(_("no such section: %s"), argv[0]); + ret = 0; + +out: + location_options_release(&location_opts); + return ret; +} + +static int show_editor(struct config_location_options *opts) +{ + char *config_file; + + if (!opts->source.file && !startup_info->have_repository) + die(_("not in a git directory")); + if (opts->source.use_stdin) + die(_("editing stdin is not supported")); + if (opts->source.blob) + die(_("editing blobs is not supported")); + git_config(git_default_config, NULL); + config_file = opts->source.file ? + xstrdup(opts->source.file) : + git_pathdup("config"); + if (opts->use_global_config) { + int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666); + if (fd >= 0) { + char *content = default_user_config(); + write_str_in_full(fd, content); + free(content); + close(fd); + } + else if (errno != EEXIST) + die_errno(_("cannot create configuration file %s"), config_file); + } + launch_editor(config_file, NULL, NULL); + free(config_file); + + return 0; +} + +static int cmd_config_edit(int argc, const char **argv, const char *prefix) +{ + struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; + struct option opts[] = { + CONFIG_LOCATION_OPTIONS(location_opts), + OPT_END(), + }; + int ret; + + argc = parse_options(argc, argv, prefix, opts, builtin_config_edit_usage, 0); + check_argc(argc, 0, 0); + + location_options_init(&location_opts, prefix); + check_write(&location_opts.source); + + ret = show_editor(&location_opts); + location_options_release(&location_opts); + return ret; +} + +static int cmd_config_actions(int argc, const char **argv, const char *prefix) +{ + enum { + ACTION_GET = (1<<0), + ACTION_GET_ALL = (1<<1), + ACTION_GET_REGEXP = (1<<2), + ACTION_REPLACE_ALL = (1<<3), + ACTION_ADD = (1<<4), + ACTION_UNSET = (1<<5), + ACTION_UNSET_ALL = (1<<6), + ACTION_RENAME_SECTION = (1<<7), + ACTION_REMOVE_SECTION = (1<<8), + ACTION_LIST = (1<<9), + ACTION_EDIT = (1<<10), + ACTION_SET = (1<<11), + ACTION_SET_ALL = (1<<12), + ACTION_GET_COLOR = (1<<13), + ACTION_GET_COLORBOOL = (1<<14), + ACTION_GET_URLMATCH = (1<<15), + }; + struct config_location_options location_opts = CONFIG_LOCATION_OPTIONS_INIT; + struct config_display_options display_opts = CONFIG_DISPLAY_OPTIONS_INIT; + const char *comment_arg = NULL; + int actions = 0; + unsigned flags = 0; + struct option opts[] = { + CONFIG_LOCATION_OPTIONS(location_opts), + OPT_GROUP(N_("Action")), + OPT_CMDMODE(0, "get", &actions, N_("get value: name [<value-pattern>]"), ACTION_GET), + OPT_CMDMODE(0, "get-all", &actions, N_("get all values: key [<value-pattern>]"), ACTION_GET_ALL), + OPT_CMDMODE(0, "get-regexp", &actions, N_("get values for regexp: name-regex [<value-pattern>]"), ACTION_GET_REGEXP), + OPT_CMDMODE(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH), + OPT_CMDMODE(0, "replace-all", &actions, N_("replace all matching variables: name value [<value-pattern>]"), ACTION_REPLACE_ALL), + OPT_CMDMODE(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD), + OPT_CMDMODE(0, "unset", &actions, N_("remove a variable: name [<value-pattern>]"), ACTION_UNSET), + OPT_CMDMODE(0, "unset-all", &actions, N_("remove all matches: name [<value-pattern>]"), ACTION_UNSET_ALL), + OPT_CMDMODE(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION), + OPT_CMDMODE(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION), + OPT_CMDMODE('l', "list", &actions, N_("list all"), ACTION_LIST), + OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT), + OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR), + OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL), + CONFIG_DISPLAY_OPTIONS(display_opts), + OPT_GROUP(N_("Other")), + OPT_STRING(0, "default", &display_opts.default_value, + N_("value"), N_("with --get, use default value when missing entry")), + OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")), + OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE), + OPT_BOOL(0, "includes", &location_opts.respect_includes_opt, + N_("respect include directives on lookup")), + OPT_END(), + }; + char *value = NULL, *comment = NULL; + int ret = 0; + struct key_value_info default_kvi = KVI_INIT; + + argc = parse_options(argc, argv, prefix, opts, + builtin_config_usage, + PARSE_OPT_STOP_AT_NON_OPTION); + + location_options_init(&location_opts, prefix); + display_options_init(&display_opts); + + if ((actions & (ACTION_GET_COLOR|ACTION_GET_COLORBOOL)) && display_opts.type) { + error(_("--get-color and variable type are incoherent")); + exit(129); } + if (actions == 0) switch (argc) { case 1: actions = ACTION_GET; break; case 2: actions = ACTION_SET; break; case 3: actions = ACTION_SET_ALL; break; default: - usage_builtin_config(); + error(_("no action specified")); + exit(129); } - if (omit_values && + if (display_opts.omit_values && !(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) { error(_("--name-only is only applicable to --list or --get-regexp")); - usage_builtin_config(); + exit(129); } - if (show_origin && !(actions & + if (display_opts.show_origin && !(actions & (ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) { error(_("--show-origin is only applicable to --get, --get-all, " "--get-regexp, and --list")); - usage_builtin_config(); + exit(129); } - if (default_value && !(actions & ACTION_GET)) { + if (display_opts.default_value && !(actions & ACTION_GET)) { error(_("--default is only applicable to --get")); - usage_builtin_config(); + exit(129); } - if (comment && + if (comment_arg && !(actions & (ACTION_ADD|ACTION_SET|ACTION_SET_ALL|ACTION_REPLACE_ALL))) { error(_("--comment is only applicable to add/set/replace operations")); - usage_builtin_config(); + exit(129); } /* check usage of --fixed-value */ - if (fixed_value) { + if (flags & CONFIG_FLAGS_FIXED_VALUE) { int allowed_usage = 0; switch (actions) { @@ -835,148 +1250,125 @@ int cmd_config(int argc, const char **argv, const char *prefix) if (!allowed_usage) { error(_("--fixed-value only applies with 'value-pattern'")); - usage_builtin_config(); + exit(129); } - - flags |= CONFIG_FLAGS_FIXED_VALUE; } - comment = git_config_prepare_comment_string(comment); + comment = git_config_prepare_comment_string(comment_arg); - if (actions & PAGING_ACTIONS) + /* + * The following actions may produce more than one line of output and + * should therefore be paged. + */ + if (actions & (ACTION_LIST | ACTION_GET_ALL | ACTION_GET_REGEXP | ACTION_GET_URLMATCH)) setup_auto_pager("config", 1); if (actions == ACTION_LIST) { check_argc(argc, 0, 0); - if (config_with_options(show_all_config, NULL, - &given_config_source, the_repository, - &config_options) < 0) { - if (given_config_source.file) + if (config_with_options(show_all_config, &display_opts, + &location_opts.source, the_repository, + &location_opts.options) < 0) { + if (location_opts.source.file) die_errno(_("unable to read config file '%s'"), - given_config_source.file); + location_opts.source.file); else die(_("error processing config file(s)")); } } else if (actions == ACTION_EDIT) { - char *config_file; - - check_argc(argc, 0, 0); - if (!given_config_source.file && nongit) - die(_("not in a git directory")); - if (given_config_source.use_stdin) - die(_("editing stdin is not supported")); - if (given_config_source.blob) - die(_("editing blobs is not supported")); - git_config(git_default_config, NULL); - config_file = given_config_source.file ? - xstrdup(given_config_source.file) : - git_pathdup("config"); - if (use_global_config) { - int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666); - if (fd >= 0) { - char *content = default_user_config(); - write_str_in_full(fd, content); - free(content); - close(fd); - } - else if (errno != EEXIST) - die_errno(_("cannot create configuration file %s"), config_file); - } - launch_editor(config_file, NULL, NULL); - free(config_file); + ret = show_editor(&location_opts); } else if (actions == ACTION_SET) { - check_write(); + check_write(&location_opts.source); check_argc(argc, 2, 2); - value = normalize_value(argv[0], argv[1], &default_kvi); - ret = git_config_set_in_file_gently(given_config_source.file, argv[0], comment, value); + value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); + ret = git_config_set_in_file_gently(location_opts.source.file, argv[0], comment, value); if (ret == CONFIG_NOTHING_SET) error(_("cannot overwrite multiple values with a single value\n" " Use a regexp, --add or --replace-all to change %s."), argv[0]); } else if (actions == ACTION_SET_ALL) { - check_write(); + check_write(&location_opts.source); check_argc(argc, 2, 3); - value = normalize_value(argv[0], argv[1], &default_kvi); - ret = git_config_set_multivar_in_file_gently(given_config_source.file, + value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); + ret = git_config_set_multivar_in_file_gently(location_opts.source.file, argv[0], value, argv[2], comment, flags); } else if (actions == ACTION_ADD) { - check_write(); + check_write(&location_opts.source); check_argc(argc, 2, 2); - value = normalize_value(argv[0], argv[1], &default_kvi); - ret = git_config_set_multivar_in_file_gently(given_config_source.file, + value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); + ret = git_config_set_multivar_in_file_gently(location_opts.source.file, argv[0], value, CONFIG_REGEX_NONE, comment, flags); } else if (actions == ACTION_REPLACE_ALL) { - check_write(); + check_write(&location_opts.source); check_argc(argc, 2, 3); - value = normalize_value(argv[0], argv[1], &default_kvi); - ret = git_config_set_multivar_in_file_gently(given_config_source.file, + value = normalize_value(argv[0], argv[1], display_opts.type, &default_kvi); + ret = git_config_set_multivar_in_file_gently(location_opts.source.file, argv[0], value, argv[2], comment, flags | CONFIG_FLAGS_MULTI_REPLACE); } else if (actions == ACTION_GET) { check_argc(argc, 1, 2); - return get_value(argv[0], argv[1], flags); + ret = get_value(&location_opts, &display_opts, argv[0], argv[1], + 0, flags); } else if (actions == ACTION_GET_ALL) { - do_all = 1; check_argc(argc, 1, 2); - return get_value(argv[0], argv[1], flags); + ret = get_value(&location_opts, &display_opts, argv[0], argv[1], + GET_VALUE_ALL, flags); } else if (actions == ACTION_GET_REGEXP) { - show_keys = 1; - use_key_regexp = 1; - do_all = 1; + display_opts.show_keys = 1; check_argc(argc, 1, 2); - return get_value(argv[0], argv[1], flags); + ret = get_value(&location_opts, &display_opts, argv[0], argv[1], + GET_VALUE_ALL|GET_VALUE_KEY_REGEXP, flags); } else if (actions == ACTION_GET_URLMATCH) { check_argc(argc, 2, 2); - return get_urlmatch(argv[0], argv[1]); + ret = get_urlmatch(&location_opts, &display_opts, argv[0], argv[1]); } else if (actions == ACTION_UNSET) { - check_write(); + check_write(&location_opts.source); check_argc(argc, 1, 2); if (argc == 2) - return git_config_set_multivar_in_file_gently(given_config_source.file, - argv[0], NULL, argv[1], - NULL, flags); + ret = git_config_set_multivar_in_file_gently(location_opts.source.file, + argv[0], NULL, argv[1], + NULL, flags); else - return git_config_set_in_file_gently(given_config_source.file, - argv[0], NULL, NULL); + ret = git_config_set_in_file_gently(location_opts.source.file, + argv[0], NULL, NULL); } else if (actions == ACTION_UNSET_ALL) { - check_write(); + check_write(&location_opts.source); check_argc(argc, 1, 2); - return git_config_set_multivar_in_file_gently(given_config_source.file, - argv[0], NULL, argv[1], - NULL, flags | CONFIG_FLAGS_MULTI_REPLACE); + ret = git_config_set_multivar_in_file_gently(location_opts.source.file, + argv[0], NULL, argv[1], + NULL, flags | CONFIG_FLAGS_MULTI_REPLACE); } else if (actions == ACTION_RENAME_SECTION) { - check_write(); + check_write(&location_opts.source); check_argc(argc, 2, 2); - ret = git_config_rename_section_in_file(given_config_source.file, + ret = git_config_rename_section_in_file(location_opts.source.file, argv[0], argv[1]); if (ret < 0) - return ret; + goto out; else if (!ret) die(_("no such section: %s"), argv[0]); else ret = 0; } else if (actions == ACTION_REMOVE_SECTION) { - check_write(); + check_write(&location_opts.source); check_argc(argc, 1, 1); - ret = git_config_rename_section_in_file(given_config_source.file, + ret = git_config_rename_section_in_file(location_opts.source.file, argv[0], NULL); if (ret < 0) - return ret; + goto out; else if (!ret) die(_("no such section: %s"), argv[0]); else @@ -984,15 +1376,51 @@ int cmd_config(int argc, const char **argv, const char *prefix) } else if (actions == ACTION_GET_COLOR) { check_argc(argc, 1, 2); - get_color(argv[0], argv[1]); + get_color(&location_opts, argv[0], argv[1]); } else if (actions == ACTION_GET_COLORBOOL) { check_argc(argc, 1, 2); if (argc == 2) color_stdout_is_tty = git_config_bool("command line", argv[1]); - return get_colorbool(argv[0], argc == 2); + ret = get_colorbool(&location_opts, argv[0], argc == 2); } +out: + location_options_release(&location_opts); + free(comment); free(value); return ret; } + +int cmd_config(int argc, const char **argv, const char *prefix) +{ + parse_opt_subcommand_fn *subcommand = NULL; + struct option subcommand_opts[] = { + OPT_SUBCOMMAND("list", &subcommand, cmd_config_list), + OPT_SUBCOMMAND("get", &subcommand, cmd_config_get), + OPT_SUBCOMMAND("set", &subcommand, cmd_config_set), + OPT_SUBCOMMAND("unset", &subcommand, cmd_config_unset), + OPT_SUBCOMMAND("rename-section", &subcommand, cmd_config_rename_section), + OPT_SUBCOMMAND("remove-section", &subcommand, cmd_config_remove_section), + OPT_SUBCOMMAND("edit", &subcommand, cmd_config_edit), + OPT_END(), + }; + + /* + * This is somewhat hacky: we first parse the command line while + * keeping all args intact in order to determine whether a subcommand + * has been specified. If so, we re-parse it a second time, but this + * time we drop KEEP_ARGV0. This is so that we don't munge the command + * line in case no subcommand was given, which would otherwise confuse + * us when parsing the legacy-style modes that don't use subcommands. + */ + argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage, + PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_ARGV0|PARSE_OPT_KEEP_UNKNOWN_OPT); + if (subcommand) { + argc = parse_options(argc, argv, prefix, subcommand_opts, builtin_config_usage, + PARSE_OPT_SUBCOMMAND_OPTIONAL|PARSE_OPT_KEEP_UNKNOWN_OPT); + return subcommand(argc, argv, prefix); + } + + return cmd_config_actions(argc, argv, prefix); +} diff --git a/builtin/describe.c b/builtin/describe.c index c0e3301..e5287ed 100644 --- a/builtin/describe.c +++ b/builtin/describe.c @@ -200,7 +200,7 @@ static int get_name(const char *path, const struct object_id *oid, } /* Is it annotated? */ - if (!peel_iterated_oid(oid, &peeled)) { + if (!peel_iterated_oid(the_repository, oid, &peeled)) { is_annotated = !oideq(oid, &peeled); } else { oidcpy(&peeled, oid); @@ -637,7 +637,8 @@ int cmd_describe(int argc, const char **argv, const char *prefix) } hashmap_init(&names, commit_name_neq, NULL, 0); - for_each_rawref(get_name, NULL); + refs_for_each_rawref(get_main_ref_store(the_repository), get_name, + NULL); if (!hashmap_get_size(&names) && !always) die(_("No names found, cannot describe anything.")); diff --git a/builtin/diff.c b/builtin/diff.c index efc3748..9b6cdab 100644 --- a/builtin/diff.c +++ b/builtin/diff.c @@ -465,6 +465,15 @@ int cmd_diff(int argc, const char **argv, const char *prefix) no_index = DIFF_NO_INDEX_IMPLICIT; } + /* + * When operating outside of a Git repository we need to have a hash + * algorithm at hand so that we can generate the blob hashes. We + * default to SHA1 here, but may eventually want to change this to be + * configurable via a command line option. + */ + if (nongit) + repo_set_hash_algo(the_repository, GIT_HASH_SHA1); + init_diff_ui_defaults(); git_config(git_diff_ui_config, NULL); prefix = precompose_argv_prefix(argc, argv, prefix); diff --git a/builtin/fast-import.c b/builtin/fast-import.c index dc5a9d3..d1c0243 100644 --- a/builtin/fast-import.c +++ b/builtin/fast-import.c @@ -1604,10 +1604,11 @@ static int update_branch(struct branch *b) if (is_null_oid(&b->oid)) { if (b->delete) - delete_ref(NULL, b->name, NULL, 0); + refs_delete_ref(get_main_ref_store(the_repository), + NULL, b->name, NULL, 0); return 0; } - if (read_ref(b->name, &old_oid)) + if (refs_read_ref(get_main_ref_store(the_repository), b->name, &old_oid)) oidclr(&old_oid); if (!force_update && !is_null_oid(&old_oid)) { struct commit *old_cmit, *new_cmit; @@ -1631,10 +1632,11 @@ static int update_branch(struct branch *b) return -1; } } - transaction = ref_transaction_begin(&err); + transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!transaction || ref_transaction_update(transaction, b->name, &b->oid, &old_oid, - 0, msg, &err) || + NULL, NULL, 0, msg, &err) || ref_transaction_commit(transaction, &err)) { ref_transaction_free(transaction); error("%s", err.buf); @@ -1665,7 +1667,8 @@ static void dump_tags(void) struct strbuf err = STRBUF_INIT; struct ref_transaction *transaction; - transaction = ref_transaction_begin(&err); + transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!transaction) { failure |= error("%s", err.buf); goto cleanup; @@ -1675,7 +1678,8 @@ static void dump_tags(void) strbuf_addf(&ref_name, "refs/tags/%s", t->name); if (ref_transaction_update(transaction, ref_name.buf, - &t->oid, NULL, 0, msg, &err)) { + &t->oid, NULL, NULL, NULL, + 0, msg, &err)) { failure |= error("%s", err.buf); goto cleanup; } diff --git a/builtin/fetch.c b/builtin/fetch.c index 5857d86..a319954 100644 --- a/builtin/fetch.c +++ b/builtin/fetch.c @@ -340,7 +340,8 @@ static void find_non_local_tags(const struct ref *refs, refname_hash_init(&remote_refs); create_fetch_oidset(head, &fetch_oids); - for_each_ref(add_one_refname, &existing_refs); + refs_for_each_ref(get_main_ref_store(the_repository), add_one_refname, + &existing_refs); /* * If we already have a transaction, then we need to filter out all @@ -614,7 +615,9 @@ static struct ref *get_ref_map(struct remote *remote, if (!existing_refs_populated) { refname_hash_init(&existing_refs); - for_each_ref(add_one_refname, &existing_refs); + refs_for_each_ref(get_main_ref_store(the_repository), + add_one_refname, + &existing_refs); existing_refs_populated = 1; } @@ -659,7 +662,8 @@ static int s_update_ref(const char *action, * lifecycle. */ if (!transaction) { - transaction = our_transaction = ref_transaction_begin(&err); + transaction = our_transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!transaction) { ret = STORE_REF_ERROR_OTHER; goto out; @@ -668,7 +672,7 @@ static int s_update_ref(const char *action, ret = ref_transaction_update(transaction, ref->name, &ref->new_oid, check_old ? &ref->old_oid : NULL, - 0, msg, &err); + NULL, NULL, 0, msg, &err); if (ret) { ret = STORE_REF_ERROR_OTHER; goto out; @@ -1393,7 +1397,9 @@ static int prune_refs(struct display_state *display_state, for (ref = stale_refs; ref; ref = ref->next) string_list_append(&refnames, ref->name); - result = delete_refs("fetch: prune", &refnames, 0); + result = refs_delete_refs(get_main_ref_store(the_repository), + "fetch: prune", &refnames, + 0); string_list_clear(&refnames, 0); } } @@ -1406,7 +1412,8 @@ static int prune_refs(struct display_state *display_state, _("(none)"), ref->name, &ref->new_oid, &ref->old_oid, summary_width); - warn_dangling_symref(stderr, dangling_msg, ref->name); + refs_warn_dangling_symref(get_main_ref_store(the_repository), + stderr, dangling_msg, ref->name); } } @@ -1479,7 +1486,8 @@ static void add_negotiation_tips(struct git_transport_options *smart_options) continue; } old_nr = oids->nr; - for_each_glob_ref(add_oid, s, oids); + refs_for_each_glob_ref(get_main_ref_store(the_repository), + add_oid, s, oids); if (old_nr == oids->nr) warning("ignoring --negotiation-tip=%s because it does not match any refs", s); @@ -1655,7 +1663,8 @@ static int do_fetch(struct transport *transport, config->display_format); if (atomic_fetch) { - transaction = ref_transaction_begin(&err); + transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!transaction) { retcode = -1; goto cleanup; diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 919282e..5517a4a 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -98,7 +98,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) } if (include_root_refs) - flags |= FILTER_REFS_ROOT_REFS; + flags |= FILTER_REFS_ROOT_REFS | FILTER_REFS_DETACHED_HEAD; filter.match_as_path = 1; filter_and_format_refs(&filter, flags, sorting, &format); diff --git a/builtin/fsck.c b/builtin/fsck.c index f892487..d13a226 100644 --- a/builtin/fsck.c +++ b/builtin/fsck.c @@ -514,7 +514,9 @@ static int fsck_handle_reflog(const char *logname, void *cb_data) struct strbuf refname = STRBUF_INIT; strbuf_worktree_ref(cb_data, &refname, logname); - for_each_reflog_ent(refname.buf, fsck_handle_reflog_ent, refname.buf); + refs_for_each_reflog_ent(get_main_ref_store(the_repository), + refname.buf, fsck_handle_reflog_ent, + refname.buf); strbuf_release(&refname); return 0; } @@ -563,7 +565,8 @@ static void get_default_heads(void) const char *head_points_at; struct object_id head_oid; - for_each_rawref(fsck_handle_ref, NULL); + refs_for_each_rawref(get_main_ref_store(the_repository), + fsck_handle_ref, NULL); worktrees = get_worktrees(); for (p = worktrees; *p; p++) { @@ -712,7 +715,9 @@ static int fsck_head_link(const char *head_ref_name, if (verbose) fprintf_ln(stderr, _("Checking %s link"), head_ref_name); - *head_points_at = resolve_ref_unsafe(head_ref_name, 0, head_oid, NULL); + *head_points_at = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + head_ref_name, 0, head_oid, + NULL); if (!*head_points_at) { errors_found |= ERROR_REFS; return error(_("invalid %s"), head_ref_name); diff --git a/builtin/gc.c b/builtin/gc.c index d3b5ca9..72bac25 100644 --- a/builtin/gc.c +++ b/builtin/gc.c @@ -846,7 +846,7 @@ static int dfs_on_ref(const char *refname UNUSED, struct commit_list *stack = NULL; struct commit *commit; - if (!peel_iterated_oid(oid, &peeled)) + if (!peel_iterated_oid(the_repository, oid, &peeled)) oid = &peeled; if (oid_object_info(the_repository, oid, NULL) != OBJ_COMMIT) return 0; @@ -907,7 +907,8 @@ static int should_write_commit_graph(void) if (data.limit < 0) return 1; - result = for_each_ref(dfs_on_ref, &data); + result = refs_for_each_ref(get_main_ref_store(the_repository), + dfs_on_ref, &data); repo_clear_commit_marks(the_repository, SEEN); diff --git a/builtin/hash-object.c b/builtin/hash-object.c index 82ca6d2..c767414 100644 --- a/builtin/hash-object.c +++ b/builtin/hash-object.c @@ -123,6 +123,9 @@ int cmd_hash_object(int argc, const char **argv, const char *prefix) else prefix = setup_git_directory_gently(&nongit); + if (nongit && !the_hash_algo) + repo_set_hash_algo(the_repository, GIT_HASH_SHA1); + if (vpath && prefix) { vpath_free = prefix_filename(prefix, vpath); vpath = vpath_free; diff --git a/builtin/interpret-trailers.c b/builtin/interpret-trailers.c index 8768bfe..1d96949 100644 --- a/builtin/interpret-trailers.c +++ b/builtin/interpret-trailers.c @@ -141,7 +141,7 @@ static void interpret_trailers(const struct process_trailer_options *opts, LIST_HEAD(head); struct strbuf sb = STRBUF_INIT; struct strbuf trailer_block = STRBUF_INIT; - struct trailer_info info; + struct trailer_info *info; FILE *outfile = stdout; trailer_config_init(); @@ -151,13 +151,13 @@ static void interpret_trailers(const struct process_trailer_options *opts, if (opts->in_place) outfile = create_in_place_tempfile(file); - parse_trailers(opts, &info, sb.buf, &head); + info = parse_trailers(opts, sb.buf, &head); /* Print the lines before the trailers */ if (!opts->only_trailers) - fwrite(sb.buf, 1, info.trailer_block_start, outfile); + fwrite(sb.buf, 1, trailer_block_start(info), outfile); - if (!opts->only_trailers && !info.blank_line_before_trailer) + if (!opts->only_trailers && !blank_line_before_trailer_block(info)) fprintf(outfile, "\n"); @@ -178,8 +178,8 @@ static void interpret_trailers(const struct process_trailer_options *opts, /* Print the lines after the trailers as is */ if (!opts->only_trailers) - fwrite(sb.buf + info.trailer_block_end, 1, sb.len - info.trailer_block_end, outfile); - trailer_info_release(&info); + fwrite(sb.buf + trailer_block_end(info), 1, sb.len - trailer_block_end(info), outfile); + trailer_info_release(info); if (opts->in_place) if (rename_tempfile(&trailers_tempfile, file)) diff --git a/builtin/log.c b/builtin/log.c index 4da7399..c8ce0c0 100644 --- a/builtin/log.c +++ b/builtin/log.c @@ -2226,8 +2226,10 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix) if (check_head) { const char *ref, *v; - ref = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, - NULL, NULL); + ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + "HEAD", + RESOLVE_REF_READING, + NULL, NULL); if (ref && skip_prefix(ref, "refs/heads/", &v)) branch_name = xstrdup(v); else @@ -2294,7 +2296,7 @@ int cmd_format_patch(int argc, const char **argv, const char *prefix) } if (creation_factor < 0) - creation_factor = RANGE_DIFF_CREATION_FACTOR_DEFAULT; + creation_factor = CREATION_FACTOR_FOR_THE_SAME_SERIES; else if (!rdiff_prev) die(_("the option '%s' requires '%s'"), "--creation-factor", "--range-diff"); diff --git a/builtin/merge.c b/builtin/merge.c index 6a6d379..e4bd65e 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -448,8 +448,10 @@ static void finish(struct commit *head_commit, if (verbosity >= 0 && !merge_msg.len) printf(_("No merge message -- not updating HEAD\n")); else { - update_ref(reflog_message.buf, "HEAD", new_head, head, - 0, UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), + reflog_message.buf, "HEAD", new_head, + head, + 0, UPDATE_REFS_DIE_ON_ERR); /* * We ignore errors in 'gc --auto', since the * user should see them. @@ -546,7 +548,7 @@ static void merge_name(const char *remote, struct strbuf *msg) struct strbuf truname = STRBUF_INIT; strbuf_addf(&truname, "refs/heads/%s", remote); strbuf_setlen(&truname, truname.len - len); - if (ref_exists(truname.buf)) { + if (refs_ref_exists(get_main_ref_store(the_repository), truname.buf)) { strbuf_addf(msg, "%s\t\tbranch '%s'%s of .\n", oid_to_hex(&remote_head->object.oid), @@ -1251,7 +1253,7 @@ static int merging_a_throwaway_tag(struct commit *commit) */ tag_ref = xstrfmt("refs/tags/%s", ((struct tag *)merge_remote_util(commit)->obj)->tag); - if (!read_ref(tag_ref, &oid) && + if (!refs_read_ref(get_main_ref_store(the_repository), tag_ref, &oid) && oideq(&oid, &merge_remote_util(commit)->obj->oid)) is_throwaway_tag = 0; else @@ -1283,7 +1285,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix) * Check if we are _not_ on a detached HEAD, i.e. if there is a * current branch. */ - branch = branch_to_free = resolve_refdup("HEAD", 0, &head_oid, NULL); + branch = branch_to_free = refs_resolve_refdup(get_main_ref_store(the_repository), + "HEAD", 0, &head_oid, + NULL); if (branch) skip_prefix(branch, "refs/heads/", &branch); @@ -1324,8 +1328,10 @@ int cmd_merge(int argc, const char **argv, const char *prefix) if (!file_exists(git_path_merge_head(the_repository))) die(_("There is no merge to abort (MERGE_HEAD missing).")); - if (!read_ref("MERGE_AUTOSTASH", &stash_oid)) - delete_ref("", "MERGE_AUTOSTASH", &stash_oid, REF_NO_DEREF); + if (!refs_read_ref(get_main_ref_store(the_repository), "MERGE_AUTOSTASH", &stash_oid)) + refs_delete_ref(get_main_ref_store(the_repository), + "", "MERGE_AUTOSTASH", &stash_oid, + REF_NO_DEREF); /* Invoke 'git reset --merge' */ ret = cmd_reset(nargc, nargv, prefix); @@ -1378,7 +1384,7 @@ int cmd_merge(int argc, const char **argv, const char *prefix) else die(_("You have not concluded your merge (MERGE_HEAD exists).")); } - if (ref_exists("CHERRY_PICK_HEAD")) { + if (refs_ref_exists(get_main_ref_store(the_repository), "CHERRY_PICK_HEAD")) { if (advice_enabled(ADVICE_RESOLVE_CONFLICT)) die(_("You have not concluded your cherry-pick (CHERRY_PICK_HEAD exists).\n" "Please, commit your changes before you merge.")); @@ -1449,8 +1455,10 @@ int cmd_merge(int argc, const char **argv, const char *prefix) remote_head_oid = &remoteheads->item->object.oid; read_empty(remote_head_oid); - update_ref("initial pull", "HEAD", remote_head_oid, NULL, 0, - UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), + "initial pull", "HEAD", remote_head_oid, NULL, + 0, + UPDATE_REFS_DIE_ON_ERR); goto done; } @@ -1530,8 +1538,10 @@ int cmd_merge(int argc, const char **argv, const char *prefix) free(list); } - update_ref("updating ORIG_HEAD", "ORIG_HEAD", - &head_commit->object.oid, NULL, 0, UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), + "updating ORIG_HEAD", "ORIG_HEAD", + &head_commit->object.oid, NULL, 0, + UPDATE_REFS_DIE_ON_ERR); if (remoteheads && !common) { /* No common ancestors found. */ diff --git a/builtin/name-rev.c b/builtin/name-rev.c index ad9930c..70e9ec4 100644 --- a/builtin/name-rev.c +++ b/builtin/name-rev.c @@ -296,7 +296,8 @@ static void add_to_tip_table(const struct object_id *oid, const char *refname, char *short_refname = NULL; if (shorten_unambiguous) - short_refname = shorten_unambiguous_ref(refname, 0); + short_refname = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), + refname, 0); else if (skip_prefix(refname, "refs/heads/", &refname)) ; /* refname already advanced */ else @@ -647,7 +648,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix) adjust_cutoff_timestamp_for_slop(); - for_each_ref(name_ref, &data); + refs_for_each_ref(get_main_ref_store(the_repository), name_ref, &data); name_tips(&string_pool); if (annotate_stdin) { diff --git a/builtin/notes.c b/builtin/notes.c index cb01130..7f80b34 100644 --- a/builtin/notes.c +++ b/builtin/notes.c @@ -794,9 +794,9 @@ static int merge_abort(struct notes_merge_options *o) * notes_merge_abort() to remove .git/NOTES_MERGE_WORKTREE. */ - if (delete_ref(NULL, "NOTES_MERGE_PARTIAL", NULL, 0)) + if (refs_delete_ref(get_main_ref_store(the_repository), NULL, "NOTES_MERGE_PARTIAL", NULL, 0)) ret += error(_("failed to delete ref NOTES_MERGE_PARTIAL")); - if (delete_ref(NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF)) + if (refs_delete_ref(get_main_ref_store(the_repository), NULL, "NOTES_MERGE_REF", NULL, REF_NO_DEREF)) ret += error(_("failed to delete ref NOTES_MERGE_REF")); if (notes_merge_abort(o)) ret += error(_("failed to remove 'git notes merge' worktree")); @@ -834,7 +834,8 @@ static int merge_commit(struct notes_merge_options *o) init_notes(t, "NOTES_MERGE_PARTIAL", combine_notes_overwrite, 0); o->local_ref = local_ref_to_free = - resolve_refdup("NOTES_MERGE_REF", 0, &oid, NULL); + refs_resolve_refdup(get_main_ref_store(the_repository), + "NOTES_MERGE_REF", 0, &oid, NULL); if (!o->local_ref) die(_("failed to resolve NOTES_MERGE_REF")); @@ -847,9 +848,10 @@ static int merge_commit(struct notes_merge_options *o) &pretty_ctx); strbuf_trim(&msg); strbuf_insertstr(&msg, 0, "notes: "); - update_ref(msg.buf, o->local_ref, &oid, - is_null_oid(&parent_oid) ? NULL : &parent_oid, - 0, UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), msg.buf, + o->local_ref, &oid, + is_null_oid(&parent_oid) ? NULL : &parent_oid, + 0, UPDATE_REFS_DIE_ON_ERR); free_notes(t); strbuf_release(&msg); @@ -961,14 +963,16 @@ static int merge(int argc, const char **argv, const char *prefix) if (result >= 0) /* Merge resulted (trivially) in result_oid */ /* Update default notes ref with new commit */ - update_ref(msg.buf, default_notes_ref(), &result_oid, NULL, 0, - UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), msg.buf, + default_notes_ref(), &result_oid, NULL, 0, + UPDATE_REFS_DIE_ON_ERR); else { /* Merge has unresolved conflicts */ struct worktree **worktrees; const struct worktree *wt; /* Update .git/NOTES_MERGE_PARTIAL with partial merge result */ - update_ref(msg.buf, "NOTES_MERGE_PARTIAL", &result_oid, NULL, - 0, UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), msg.buf, + "NOTES_MERGE_PARTIAL", &result_oid, NULL, + 0, UPDATE_REFS_DIE_ON_ERR); /* Store ref-to-be-updated into .git/NOTES_MERGE_REF */ worktrees = get_worktrees(); wt = find_shared_symref(worktrees, "NOTES_MERGE_REF", @@ -977,7 +981,7 @@ static int merge(int argc, const char **argv, const char *prefix) die(_("a notes merge into %s is already in-progress at %s"), default_notes_ref(), wt->path); free_worktrees(worktrees); - if (create_symref("NOTES_MERGE_REF", default_notes_ref(), NULL)) + if (refs_update_symref(get_main_ref_store(the_repository), "NOTES_MERGE_REF", default_notes_ref(), NULL)) die(_("failed to store link to current notes ref (%s)"), default_notes_ref()); fprintf(stderr, _("Automatic notes merge failed. Fix conflicts in %s " diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c index baf0090..638f5c5 100644 --- a/builtin/pack-objects.c +++ b/builtin/pack-objects.c @@ -779,7 +779,7 @@ static int mark_tagged(const char *path UNUSED, const struct object_id *oid, if (entry) entry->tagged = 1; - if (!peel_iterated_oid(oid, &peeled)) { + if (!peel_iterated_oid(the_repository, oid, &peeled)) { entry = packlist_find(&to_pack, &peeled); if (entry) entry->tagged = 1; @@ -939,7 +939,8 @@ static struct object_entry **compute_write_order(void) /* * Mark objects that are at the tip of tags. */ - for_each_tag_ref(mark_tagged, NULL); + refs_for_each_tag_ref(get_main_ref_store(the_repository), mark_tagged, + NULL); if (use_delta_islands) { max_layers = compute_pack_layers(&to_pack); @@ -1314,6 +1315,7 @@ static void write_pack_file(void) if (!pack_to_stdout) { struct stat st; struct strbuf tmpname = STRBUF_INIT; + struct bitmap_writer bitmap_writer; char *idx_tmp_name = NULL; /* @@ -1339,8 +1341,9 @@ static void write_pack_file(void) hash_to_hex(hash)); if (write_bitmap_index) { - bitmap_writer_set_checksum(hash); - bitmap_writer_build_type_index( + bitmap_writer_init(&bitmap_writer); + bitmap_writer_set_checksum(&bitmap_writer, hash); + bitmap_writer_build_type_index(&bitmap_writer, &to_pack, written_list, nr_written); } @@ -1358,12 +1361,17 @@ static void write_pack_file(void) strbuf_addstr(&tmpname, "bitmap"); stop_progress(&progress_state); - bitmap_writer_show_progress(progress); - bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); - if (bitmap_writer_build(&to_pack) < 0) + bitmap_writer_show_progress(&bitmap_writer, + progress); + bitmap_writer_select_commits(&bitmap_writer, + indexed_commits, + indexed_commits_nr); + if (bitmap_writer_build(&bitmap_writer, &to_pack) < 0) die(_("failed to write bitmap index")); - bitmap_writer_finish(written_list, nr_written, + bitmap_writer_finish(&bitmap_writer, + written_list, nr_written, tmpname.buf, write_bitmap_options); + bitmap_writer_free(&bitmap_writer); write_bitmap_index = 0; strbuf_setlen(&tmpname, tmpname_len); } @@ -3124,7 +3132,7 @@ static int add_ref_tag(const char *tag UNUSED, const struct object_id *oid, { struct object_id peeled; - if (!peel_iterated_oid(oid, &peeled) && obj_is_packed(&peeled)) + if (!peel_iterated_oid(the_repository, oid, &peeled) && obj_is_packed(&peeled)) add_tag_chain(oid); return 0; } @@ -4073,7 +4081,7 @@ static int mark_bitmap_preferred_tip(const char *refname, struct object_id peeled; struct object *object; - if (!peel_iterated_oid(oid, &peeled)) + if (!peel_iterated_oid(the_repository, oid, &peeled)) oid = &peeled; object = parse_object_or_die(oid, refname); @@ -4093,7 +4101,9 @@ static void mark_bitmap_preferred_tips(void) return; for_each_string_list_item(item, preferred_tips) { - for_each_ref_in(item->string, mark_bitmap_preferred_tip, NULL); + refs_for_each_ref_in(get_main_ref_store(the_repository), + item->string, mark_bitmap_preferred_tip, + NULL); } } @@ -4588,7 +4598,8 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix) } cleanup_preferred_base(); if (include_tag && nr_result) - for_each_tag_ref(add_ref_tag, NULL); + refs_for_each_tag_ref(get_main_ref_store(the_repository), + add_ref_tag, NULL); stop_progress(&progress_state); trace2_region_leave("pack-objects", "enumerate-objects", the_repository); diff --git a/builtin/patch-id.c b/builtin/patch-id.c index 3894d2b..583099c 100644 --- a/builtin/patch-id.c +++ b/builtin/patch-id.c @@ -5,6 +5,7 @@ #include "hash.h" #include "hex.h" #include "parse-options.h" +#include "setup.h" static void flush_current_id(int patchlen, struct object_id *id, struct object_id *result) { @@ -237,6 +238,18 @@ int cmd_patch_id(int argc, const char **argv, const char *prefix) argc = parse_options(argc, argv, prefix, builtin_patch_id_options, patch_id_usage, 0); + /* + * We rely on `the_hash_algo` to compute patch IDs. This is dubious as + * it means that the hash algorithm now depends on the object hash of + * the repository, even though git-patch-id(1) clearly defines that + * patch IDs always use SHA1. + * + * NEEDSWORK: This hack should be removed in favor of converting + * the code that computes patch IDs to always use SHA1. + */ + if (!the_hash_algo) + repo_set_hash_algo(the_repository, GIT_HASH_SHA1); + generate_id_list(opts ? opts > 1 : config.stable, opts ? opts == 3 : config.verbatim); return 0; diff --git a/builtin/pull.c b/builtin/pull.c index 6686921..d622202 100644 --- a/builtin/pull.c +++ b/builtin/pull.c @@ -611,7 +611,7 @@ static int pull_into_void(const struct object_id *merge_head, merge_head, 0)) return 1; - if (update_ref("initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR)) + if (refs_update_ref(get_main_ref_store(the_repository), "initial pull", "HEAD", merge_head, curr_head, 0, UPDATE_REFS_DIE_ON_ERR)) return 1; return 0; diff --git a/builtin/rebase.c b/builtin/rebase.c index fe17d56..0466d94 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -251,7 +251,7 @@ static int init_basic_state(struct replay_opts *opts, const char *head_name, if (!is_directory(merge_dir()) && mkdir_in_gitdir(merge_dir())) return error_errno(_("could not create temporary %s"), merge_dir()); - delete_reflog("REBASE_HEAD"); + refs_delete_reflog(get_main_ref_store(the_repository), "REBASE_HEAD"); interactive = fopen(path_interactive(), "w"); if (!interactive) @@ -513,8 +513,10 @@ static int finish_rebase(struct rebase_options *opts) struct strbuf dir = STRBUF_INIT; int ret = 0; - delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); - delete_ref(NULL, "AUTO_MERGE", NULL, REF_NO_DEREF); + refs_delete_ref(get_main_ref_store(the_repository), NULL, + "REBASE_HEAD", NULL, REF_NO_DEREF); + refs_delete_ref(get_main_ref_store(the_repository), NULL, + "AUTO_MERGE", NULL, REF_NO_DEREF); apply_autostash(state_dir_path("autostash", opts)); /* * We ignore errors in 'git maintenance run --auto', since the @@ -1622,7 +1624,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) /* Is it a local branch? */ strbuf_reset(&buf); strbuf_addf(&buf, "refs/heads/%s", branch_name); - if (!read_ref(buf.buf, &branch_oid)) { + if (!refs_read_ref(get_main_ref_store(the_repository), buf.buf, &branch_oid)) { die_if_checked_out(buf.buf, 1); options.head_name = xstrdup(buf.buf); options.orig_head = @@ -1639,8 +1641,8 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) } else if (argc == 0) { /* Do not need to switch branches, we are already on it. */ options.head_name = - xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL, - &flags)); + xstrdup_or_null(refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, + &flags)); if (!options.head_name) die(_("No such ref: %s"), "HEAD"); if (flags & REF_ISSYMREF) { @@ -1734,7 +1736,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) if (!(options.flags & REBASE_NO_QUIET)) ; /* be quiet */ else if (!strcmp(branch_name, "HEAD") && - resolve_ref_unsafe("HEAD", 0, NULL, &flag)) + refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, &flag)) puts(_("HEAD is up to date.")); else printf(_("Current branch %s is up to date.\n"), @@ -1744,7 +1746,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) } else if (!(options.flags & REBASE_NO_QUIET)) ; /* be quiet */ else if (!strcmp(branch_name, "HEAD") && - resolve_ref_unsafe("HEAD", 0, NULL, &flag)) + refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, &flag)) puts(_("HEAD is up to date, rebase forced.")); else printf(_("Current branch %s is up to date, rebase " diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c index e8d7df1..be8969a 100644 --- a/builtin/receive-pack.c +++ b/builtin/receive-pack.c @@ -1566,7 +1566,7 @@ static const char *update(struct command *cmd, struct shallow_info *si) struct strbuf err = STRBUF_INIT; if (!parse_object(the_repository, old_oid)) { old_oid = NULL; - if (ref_exists(name)) { + if (refs_ref_exists(get_main_ref_store(the_repository), name)) { rp_warning("allowing deletion of corrupt ref"); } else { rp_warning("deleting a non-existent ref"); @@ -1595,6 +1595,7 @@ static const char *update(struct command *cmd, struct shallow_info *si) if (ref_transaction_update(transaction, namespaced_name, new_oid, old_oid, + NULL, NULL, 0, "push", &err)) { rp_error("%s", err.buf); @@ -1693,7 +1694,8 @@ static void check_aliased_update(struct command *cmd, struct string_list *list) int flag; strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name); - dst_name = resolve_ref_unsafe(buf.buf, 0, NULL, &flag); + dst_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + buf.buf, 0, NULL, &flag); check_aliased_update_internal(cmd, list, dst_name, flag); strbuf_release(&buf); } @@ -1829,7 +1831,8 @@ static void execute_commands_non_atomic(struct command *commands, if (!should_process_cmd(cmd) || cmd->run_proc_receive) continue; - transaction = ref_transaction_begin(&err); + transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!transaction) { rp_error("%s", err.buf); strbuf_reset(&err); @@ -1857,7 +1860,8 @@ static void execute_commands_atomic(struct command *commands, struct strbuf err = STRBUF_INIT; const char *reported_error = "atomic push failure"; - transaction = ref_transaction_begin(&err); + transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!transaction) { rp_error("%s", err.buf); strbuf_reset(&err); @@ -1983,7 +1987,9 @@ static void execute_commands(struct command *commands, check_aliased_updates(commands); free(head_name_to_free); - head_name = head_name_to_free = resolve_refdup("HEAD", 0, NULL, NULL); + head_name = head_name_to_free = refs_resolve_refdup(get_main_ref_store(the_repository), + "HEAD", 0, NULL, + NULL); if (run_proc_receive && run_proc_receive_hook(commands, push_options)) diff --git a/builtin/reflog.c b/builtin/reflog.c index 060eb33..0d2ff95 100644 --- a/builtin/reflog.c +++ b/builtin/reflog.c @@ -364,11 +364,12 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix) }; set_reflog_expiry_param(&cb.cmd, item->string); - status |= reflog_expire(item->string, flags, - reflog_expiry_prepare, - should_prune_fn, - reflog_expiry_cleanup, - &cb); + status |= refs_reflog_expire(get_main_ref_store(the_repository), + item->string, flags, + reflog_expiry_prepare, + should_prune_fn, + reflog_expiry_cleanup, + &cb); } string_list_clear(&collected.reflogs, 0); } @@ -377,16 +378,17 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix) char *ref; struct expire_reflog_policy_cb cb = { .cmd = cmd }; - if (!dwim_log(argv[i], strlen(argv[i]), NULL, &ref)) { + if (!repo_dwim_log(the_repository, argv[i], strlen(argv[i]), NULL, &ref)) { status |= error(_("%s points nowhere!"), argv[i]); continue; } set_reflog_expiry_param(&cb.cmd, ref); - status |= reflog_expire(ref, flags, - reflog_expiry_prepare, - should_prune_fn, - reflog_expiry_cleanup, - &cb); + status |= refs_reflog_expire(get_main_ref_store(the_repository), + ref, flags, + reflog_expiry_prepare, + should_prune_fn, + reflog_expiry_cleanup, + &cb); free(ref); } return status; @@ -437,7 +439,8 @@ static int cmd_reflog_exists(int argc, const char **argv, const char *prefix) refname = argv[0]; if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) die(_("invalid ref format: %s"), refname); - return !reflog_exists(refname); + return !refs_reflog_exists(get_main_ref_store(the_repository), + refname); } /* diff --git a/builtin/remote.c b/builtin/remote.c index 8412d12..447ef1d 100644 --- a/builtin/remote.c +++ b/builtin/remote.c @@ -240,7 +240,7 @@ static int add(int argc, const char **argv, const char *prefix) strbuf_reset(&buf2); strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master); - if (create_symref(buf.buf, buf2.buf, "remote add")) + if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote add")) return error(_("Could not setup master '%s'"), master); } @@ -376,7 +376,7 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat for (ref = fetch_map; ref; ref = ref->next) { if (omit_name_by_refspec(ref->name, &states->remote->fetch)) string_list_append(&states->skipped, abbrev_branch(ref->name)); - else if (!ref->peer_ref || !ref_exists(ref->peer_ref->name)) + else if (!ref->peer_ref || !refs_ref_exists(get_main_ref_store(the_repository), ref->peer_ref->name)) string_list_append(&states->new_refs, abbrev_branch(ref->name)); else string_list_append(&states->tracked, abbrev_branch(ref->name)); @@ -598,8 +598,9 @@ static int read_remote_branches(const char *refname, strbuf_addf(&buf, "refs/remotes/%s/", rename->old_name); if (starts_with(refname, buf.buf)) { item = string_list_append(rename->remote_branches, refname); - symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING, - NULL, &flag); + symref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + refname, RESOLVE_REF_READING, + NULL, &flag); if (symref && (flag & REF_ISSYMREF)) { item->util = xstrdup(symref); rename->symrefs_nr++; @@ -789,7 +790,8 @@ static int mv(int argc, const char **argv, const char *prefix) * First remove symrefs, then rename the rest, finally create * the new symrefs. */ - for_each_ref(read_remote_branches, &rename); + refs_for_each_ref(get_main_ref_store(the_repository), + read_remote_branches, &rename); if (show_progress) { /* * Count symrefs twice, since "renaming" them is done by @@ -805,7 +807,7 @@ static int mv(int argc, const char **argv, const char *prefix) if (refs_read_symbolic_ref(get_main_ref_store(the_repository), item->string, &referent)) continue; - if (delete_ref(NULL, item->string, NULL, REF_NO_DEREF)) + if (refs_delete_ref(get_main_ref_store(the_repository), NULL, item->string, NULL, REF_NO_DEREF)) die(_("deleting '%s' failed"), item->string); strbuf_release(&referent); @@ -823,7 +825,7 @@ static int mv(int argc, const char **argv, const char *prefix) strbuf_reset(&buf2); strbuf_addf(&buf2, "remote: renamed %s to %s", item->string, buf.buf); - if (rename_ref(item->string, buf.buf, buf2.buf)) + if (refs_rename_ref(get_main_ref_store(the_repository), item->string, buf.buf, buf2.buf)) die(_("renaming '%s' failed"), item->string); display_progress(progress, ++refs_renamed_nr); } @@ -843,7 +845,7 @@ static int mv(int argc, const char **argv, const char *prefix) strbuf_reset(&buf3); strbuf_addf(&buf3, "remote: renamed %s to %s", item->string, buf.buf); - if (create_symref(buf.buf, buf2.buf, buf3.buf)) + if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, buf3.buf)) die(_("creating '%s' failed"), buf.buf); display_progress(progress, ++refs_renamed_nr); } @@ -917,11 +919,14 @@ static int rm(int argc, const char **argv, const char *prefix) * refs, which are invalidated when deleting a branch. */ cb_data.remote = remote; - result = for_each_ref(add_branch_for_removal, &cb_data); + result = refs_for_each_ref(get_main_ref_store(the_repository), + add_branch_for_removal, &cb_data); strbuf_release(&buf); if (!result) - result = delete_refs("remote: remove", &branches, REF_NO_DEREF); + result = refs_delete_refs(get_main_ref_store(the_repository), + "remote: remove", &branches, + REF_NO_DEREF); string_list_clear(&branches, 0); if (skipped.nr) { @@ -1010,7 +1015,8 @@ static int get_remote_ref_states(const char *name, get_push_ref_states(remote_refs, states); transport_disconnect(transport); } else { - for_each_ref(append_ref_to_tracked_list, states); + refs_for_each_ref(get_main_ref_store(the_repository), + append_ref_to_tracked_list, states); string_list_sort(&states->tracked); get_push_ref_states_noquery(states); } @@ -1407,7 +1413,7 @@ static int set_head(int argc, const char **argv, const char *prefix) head_name = xstrdup(states.heads.items[0].string); free_remote_ref_states(&states); } else if (opt_d && !opt_a && argc == 1) { - if (delete_ref(NULL, buf.buf, NULL, REF_NO_DEREF)) + if (refs_delete_ref(get_main_ref_store(the_repository), NULL, buf.buf, NULL, REF_NO_DEREF)) result |= error(_("Could not delete %s"), buf.buf); } else usage_with_options(builtin_remote_sethead_usage, options); @@ -1415,9 +1421,9 @@ static int set_head(int argc, const char **argv, const char *prefix) if (head_name) { strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name); /* make sure it's valid */ - if (!ref_exists(buf2.buf)) + if (!refs_ref_exists(get_main_ref_store(the_repository), buf2.buf)) result |= error(_("Not a valid ref: %s"), buf2.buf); - else if (create_symref(buf.buf, buf2.buf, "remote set-head")) + else if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote set-head")) result |= error(_("Could not setup %s"), buf.buf); else if (opt_a) printf("%s/HEAD set to %s\n", argv[0], head_name); @@ -1457,7 +1463,8 @@ static int prune_remote(const char *remote, int dry_run) string_list_sort(&refs_to_prune); if (!dry_run) - result |= delete_refs("remote: prune", &refs_to_prune, 0); + result |= refs_delete_refs(get_main_ref_store(the_repository), + "remote: prune", &refs_to_prune, 0); for_each_string_list_item(item, &states.stale) { const char *refname = item->util; @@ -1470,7 +1477,8 @@ static int prune_remote(const char *remote, int dry_run) abbrev_ref(refname, "refs/remotes/")); } - warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune); + refs_warn_dangling_symrefs(get_main_ref_store(the_repository), + stdout, dangling_msg, &refs_to_prune); string_list_clear(&refs_to_prune, 0); free_remote_ref_states(&states); diff --git a/builtin/repack.c b/builtin/repack.c index 15e4ccc..58ad82d 100644 --- a/builtin/repack.c +++ b/builtin/repack.c @@ -673,7 +673,7 @@ static int midx_snapshot_ref_one(const char *refname UNUSED, struct midx_snapshot_ref_data *data = _data; struct object_id peeled; - if (!peel_iterated_oid(oid, &peeled)) + if (!peel_iterated_oid(the_repository, oid, &peeled)) oid = &peeled; if (oidset_insert(&data->seen, oid)) @@ -706,11 +706,14 @@ static void midx_snapshot_refs(struct tempfile *f) data.preferred = 1; for_each_string_list_item(item, preferred) - for_each_ref_in(item->string, midx_snapshot_ref_one, &data); + refs_for_each_ref_in(get_main_ref_store(the_repository), + item->string, + midx_snapshot_ref_one, &data); data.preferred = 0; } - for_each_ref(midx_snapshot_ref_one, &data); + refs_for_each_ref(get_main_ref_store(the_repository), + midx_snapshot_ref_one, &data); if (close_tempfile_gently(f)) { int save_errno = errno; diff --git a/builtin/replace.c b/builtin/replace.c index da59600..ce9f697 100644 --- a/builtin/replace.c +++ b/builtin/replace.c @@ -43,11 +43,12 @@ enum replace_format { }; struct show_data { + struct repository *repo; const char *pattern; enum replace_format format; }; -static int show_reference(struct repository *r, const char *refname, +static int show_reference(const char *refname, const struct object_id *oid, int flag UNUSED, void *cb_data) { @@ -62,11 +63,11 @@ static int show_reference(struct repository *r, const char *refname, struct object_id object; enum object_type obj_type, repl_type; - if (repo_get_oid(r, refname, &object)) + if (repo_get_oid(data->repo, refname, &object)) return error(_("failed to resolve '%s' as a valid ref"), refname); - obj_type = oid_object_info(r, &object, NULL); - repl_type = oid_object_info(r, oid, NULL); + obj_type = oid_object_info(data->repo, &object, NULL); + repl_type = oid_object_info(data->repo, oid, NULL); printf("%s (%s) -> %s (%s)\n", refname, type_name(obj_type), oid_to_hex(oid), type_name(repl_type)); @@ -80,6 +81,7 @@ static int list_replace_refs(const char *pattern, const char *format) { struct show_data data; + data.repo = the_repository; if (!pattern) pattern = "*"; data.pattern = pattern; @@ -99,7 +101,8 @@ static int list_replace_refs(const char *pattern, const char *format) "valid formats are 'short', 'medium' and 'long'"), format); - for_each_replace_ref(the_repository, show_reference, (void *)&data); + refs_for_each_replace_ref(get_main_ref_store(the_repository), + show_reference, (void *)&data); return 0; } @@ -130,7 +133,7 @@ static int for_each_replace_name(const char **argv, each_replace_name_fn fn) strbuf_addstr(&ref, oid_to_hex(&oid)); full_hex = ref.buf + base_len; - if (read_ref(ref.buf, &oid)) { + if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &oid)) { error(_("replace ref '%s' not found"), full_hex); had_error = 1; continue; @@ -145,7 +148,7 @@ static int for_each_replace_name(const char **argv, each_replace_name_fn fn) static int delete_replace_ref(const char *name, const char *ref, const struct object_id *oid) { - if (delete_ref(NULL, ref, oid, 0)) + if (refs_delete_ref(get_main_ref_store(the_repository), NULL, ref, oid, 0)) return 1; printf_ln(_("Deleted replace ref '%s'"), name); return 0; @@ -163,7 +166,7 @@ static int check_ref_valid(struct object_id *object, if (check_refname_format(ref->buf, 0)) return error(_("'%s' is not a valid ref name"), ref->buf); - if (read_ref(ref->buf, prev)) + if (refs_read_ref(get_main_ref_store(the_repository), ref->buf, prev)) oidclr(prev); else if (!force) return error(_("replace ref '%s' already exists"), ref->buf); @@ -198,10 +201,11 @@ static int replace_object_oid(const char *object_ref, return -1; } - transaction = ref_transaction_begin(&err); + transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!transaction || ref_transaction_update(transaction, ref.buf, repl, &prev, - 0, NULL, &err) || + NULL, NULL, 0, NULL, &err) || ref_transaction_commit(transaction, &err)) res = error("%s", err.buf); diff --git a/builtin/reset.c b/builtin/reset.c index b6dacf9..5f941fb 100644 --- a/builtin/reset.c +++ b/builtin/reset.c @@ -307,13 +307,16 @@ static int reset_refs(const char *rev, const struct object_id *oid) if (!repo_get_oid(the_repository, "HEAD", &oid_orig)) { orig = &oid_orig; set_reflog_message(&msg, "updating ORIG_HEAD", NULL); - update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, - UPDATE_REFS_MSG_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), msg.buf, + "ORIG_HEAD", orig, old_orig, 0, + UPDATE_REFS_MSG_ON_ERR); } else if (old_orig) - delete_ref(NULL, "ORIG_HEAD", old_orig, 0); + refs_delete_ref(get_main_ref_store(the_repository), NULL, + "ORIG_HEAD", old_orig, 0); set_reflog_message(&msg, "updating HEAD", rev); - update_ref_status = update_ref(msg.buf, "HEAD", oid, orig, 0, - UPDATE_REFS_MSG_ON_ERR); + update_ref_status = refs_update_ref(get_main_ref_store(the_repository), + msg.buf, "HEAD", oid, orig, 0, + UPDATE_REFS_MSG_ON_ERR); strbuf_release(&msg); return update_ref_status; } diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c index af79538..1e2919f 100644 --- a/builtin/rev-parse.c +++ b/builtin/rev-parse.c @@ -160,8 +160,9 @@ static void show_rev(int type, const struct object_id *oid, const char *name) case 1: /* happy */ if (abbrev_ref) { char *old = full; - full = shorten_unambiguous_ref(full, - abbrev_ref_strict); + full = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), + full, + abbrev_ref_strict); free(old); } show_with_type(type, full); @@ -599,9 +600,12 @@ static int opt_with_value(const char *arg, const char *opt, const char **value) static void handle_ref_opt(const char *pattern, const char *prefix) { if (pattern) - for_each_glob_ref_in(show_reference, pattern, prefix, NULL); + refs_for_each_glob_ref_in(get_main_ref_store(the_repository), + show_reference, pattern, prefix, + NULL); else - for_each_ref_in(prefix, show_reference, NULL); + refs_for_each_ref_in(get_main_ref_store(the_repository), + prefix, show_reference, NULL); clear_ref_exclusions(&ref_excludes); } @@ -687,7 +691,6 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) const char *name = NULL; struct object_context unused; struct strbuf buf = STRBUF_INIT; - const int hexsz = the_hash_algo->hexsz; int seen_end_of_options = 0; enum format_type format = FORMAT_DEFAULT; @@ -863,8 +866,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) abbrev = strtoul(arg, NULL, 10); if (abbrev < MINIMUM_ABBREV) abbrev = MINIMUM_ABBREV; - else if (hexsz <= abbrev) - abbrev = hexsz; + else if ((int)the_hash_algo->hexsz <= abbrev) + abbrev = the_hash_algo->hexsz; continue; } if (!strcmp(arg, "--sq")) { @@ -898,7 +901,8 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) continue; } if (!strcmp(arg, "--all")) { - for_each_ref(show_reference, NULL); + refs_for_each_ref(get_main_ref_store(the_repository), + show_reference, NULL); clear_ref_exclusions(&ref_excludes); continue; } @@ -908,8 +912,14 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) continue; } if (!strcmp(arg, "--bisect")) { - for_each_fullref_in("refs/bisect/bad", show_reference, NULL); - for_each_fullref_in("refs/bisect/good", anti_reference, NULL); + refs_for_each_fullref_in(get_main_ref_store(the_repository), + "refs/bisect/bad", + NULL, show_reference, + NULL); + refs_for_each_fullref_in(get_main_ref_store(the_repository), + "refs/bisect/good", + NULL, anti_reference, + NULL); continue; } if (opt_with_value(arg, "--branches", &arg)) { diff --git a/builtin/shortlog.c b/builtin/shortlog.c index 3c7cd2d..d4daf31 100644 --- a/builtin/shortlog.c +++ b/builtin/shortlog.c @@ -435,7 +435,7 @@ parse_done: usage_with_options(shortlog_usage, options); } - if (setup_revisions(argc, argv, &rev, NULL) != 1) { + if (!nongit && setup_revisions(argc, argv, &rev, NULL) != 1) { error(_("unrecognized argument: %s"), argv[1]); usage_with_options(shortlog_usage, options); } diff --git a/builtin/show-branch.c b/builtin/show-branch.c index b01ec76..d72f4cb 100644 --- a/builtin/show-branch.c +++ b/builtin/show-branch.c @@ -479,13 +479,15 @@ static void snarf_refs(int head, int remotes) if (head) { int orig_cnt = ref_name_cnt; - for_each_ref(append_head_ref, NULL); + refs_for_each_ref(get_main_ref_store(the_repository), + append_head_ref, NULL); sort_ref_range(orig_cnt, ref_name_cnt); } if (remotes) { int orig_cnt = ref_name_cnt; - for_each_ref(append_remote_ref, NULL); + refs_for_each_ref(get_main_ref_store(the_repository), + append_remote_ref, NULL); sort_ref_range(orig_cnt, ref_name_cnt); } } @@ -549,7 +551,8 @@ static void append_one_rev(const char *av) match_ref_pattern = av; match_ref_slash = count_slashes(av); - for_each_ref(append_matching_ref, NULL); + refs_for_each_ref(get_main_ref_store(the_repository), + append_matching_ref, NULL); if (saved_matches == ref_name_cnt && ref_name_cnt < MAX_REVS) error(_("no matching refs with %s"), av); @@ -740,9 +743,11 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) if (ac == 0) { static const char *fake_av[2]; - fake_av[0] = resolve_refdup("HEAD", - RESOLVE_REF_READING, &oid, - NULL); + fake_av[0] = refs_resolve_refdup(get_main_ref_store(the_repository), + "HEAD", + RESOLVE_REF_READING, + &oid, + NULL); fake_av[1] = NULL; av = fake_av; ac = 1; @@ -815,8 +820,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix) snarf_refs(all_heads, all_remotes); } - head = resolve_refdup("HEAD", RESOLVE_REF_READING, - &head_oid, NULL); + head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD", + RESOLVE_REF_READING, + &head_oid, NULL); if (with_current_branch && head) { int has_head = 0; diff --git a/builtin/show-ref.c b/builtin/show-ref.c index 1c15421..3114bdc 100644 --- a/builtin/show-ref.c +++ b/builtin/show-ref.c @@ -50,7 +50,7 @@ static void show_one(const struct show_one_options *opts, if (!opts->deref_tags) return; - if (!peel_iterated_oid(oid, &peeled)) { + if (!peel_iterated_oid(the_repository, oid, &peeled)) { hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev); printf("%s %s^{}\n", hex, refname); } @@ -129,7 +129,8 @@ static int cmd_show_ref__exclude_existing(const struct exclude_existing_options char buf[1024]; int patternlen = opts->pattern ? strlen(opts->pattern) : 0; - for_each_ref(add_existing, &existing_refs); + refs_for_each_ref(get_main_ref_store(the_repository), add_existing, + &existing_refs); while (fgets(buf, sizeof(buf), stdin)) { char *ref; int len = strlen(buf); @@ -173,7 +174,7 @@ static int cmd_show_ref__verify(const struct show_one_options *show_one_opts, struct object_id oid; if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) && - !read_ref(*refs, &oid)) { + !refs_read_ref(get_main_ref_store(the_repository), *refs, &oid)) { show_one(show_one_opts, *refs, &oid); } else if (!show_one_opts->quiet) @@ -205,14 +206,20 @@ static int cmd_show_ref__patterns(const struct patterns_options *opts, show_ref_data.patterns = patterns; if (opts->show_head) - head_ref(show_ref, &show_ref_data); + refs_head_ref(get_main_ref_store(the_repository), show_ref, + &show_ref_data); if (opts->heads_only || opts->tags_only) { if (opts->heads_only) - for_each_fullref_in("refs/heads/", show_ref, &show_ref_data); + refs_for_each_fullref_in(get_main_ref_store(the_repository), + "refs/heads/", NULL, + show_ref, &show_ref_data); if (opts->tags_only) - for_each_fullref_in("refs/tags/", show_ref, &show_ref_data); + refs_for_each_fullref_in(get_main_ref_store(the_repository), + "refs/tags/", NULL, show_ref, + &show_ref_data); } else { - for_each_ref(show_ref, &show_ref_data); + refs_for_each_ref(get_main_ref_store(the_repository), + show_ref, &show_ref_data); } if (!show_ref_data.found_match) return 1; diff --git a/builtin/stash.c b/builtin/stash.c index bf2834f..7859bc0 100644 --- a/builtin/stash.c +++ b/builtin/stash.c @@ -195,7 +195,7 @@ static int get_stash_info(struct stash_info *info, int argc, const char **argv) commit = argv[0]; if (!commit) { - if (!ref_exists(ref_stash)) { + if (!refs_ref_exists(get_main_ref_store(the_repository), ref_stash)) { fprintf_ln(stderr, _("No stash entries found.")); return -1; } @@ -243,7 +243,8 @@ static int do_clear_stash(void) if (repo_get_oid(the_repository, ref_stash, &obj)) return 0; - return delete_ref(NULL, ref_stash, &obj, 0); + return refs_delete_ref(get_main_ref_store(the_repository), NULL, + ref_stash, &obj, 0); } static int clear_stash(int argc, const char **argv, const char *prefix) @@ -686,7 +687,8 @@ static int reject_reflog_ent(struct object_id *ooid UNUSED, static int reflog_is_empty(const char *refname) { - return !for_each_reflog_ent(refname, reject_reflog_ent, NULL); + return !refs_for_each_reflog_ent(get_main_ref_store(the_repository), + refname, reject_reflog_ent, NULL); } static int do_drop_stash(struct stash_info *info, int quiet) @@ -823,7 +825,7 @@ static int list_stash(int argc, const char **argv, const char *prefix) git_stash_list_usage, PARSE_OPT_KEEP_UNKNOWN_OPT); - if (!ref_exists(ref_stash)) + if (!refs_ref_exists(get_main_ref_store(the_repository), ref_stash)) return 0; cp.git_cmd = 1; @@ -997,10 +999,10 @@ static int do_store_stash(const struct object_id *w_commit, const char *stash_ms if (!stash_msg) stash_msg = "Created via \"git stash store\"."; - if (update_ref(stash_msg, ref_stash, w_commit, NULL, - REF_FORCE_CREATE_REFLOG, - quiet ? UPDATE_REFS_QUIET_ON_ERR : - UPDATE_REFS_MSG_ON_ERR)) { + if (refs_update_ref(get_main_ref_store(the_repository), stash_msg, ref_stash, w_commit, NULL, + REF_FORCE_CREATE_REFLOG, + quiet ? UPDATE_REFS_QUIET_ON_ERR : + UPDATE_REFS_MSG_ON_ERR)) { if (!quiet) { fprintf_ln(stderr, _("Cannot update %s with %s"), ref_stash, oid_to_hex(w_commit)); @@ -1383,7 +1385,8 @@ static int do_create_stash(const struct pathspec *ps, struct strbuf *stash_msg_b goto done; } - branch_ref = resolve_ref_unsafe("HEAD", 0, NULL, &flags); + branch_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + "HEAD", 0, NULL, &flags); if (flags & REF_ISSYMREF) skip_prefix(branch_ref, "refs/heads/", &branch_name); head_short_sha1 = repo_find_unique_abbrev(the_repository, @@ -1565,7 +1568,7 @@ static int do_push_stash(const struct pathspec *ps, const char *stash_msg, int q goto done; } - if (!reflog_exists(ref_stash) && do_clear_stash()) { + if (!refs_reflog_exists(get_main_ref_store(the_repository), ref_stash) && do_clear_stash()) { ret = -1; if (!quiet) fprintf_ln(stderr, _("Cannot initialize stash")); diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 93f4b9d..897f198 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -302,6 +302,9 @@ static void runcommand_in_submodule_cb(const struct cache_entry *list_item, struct child_process cp = CHILD_PROCESS_INIT; char *displaypath; + if (validate_submodule_path(path) < 0) + exit(128); + displaypath = get_submodule_displaypath(path, info->prefix, info->super_prefix); @@ -633,6 +636,9 @@ static void status_submodule(const char *path, const struct object_id *ce_oid, .free_removed_argv_elements = 1, }; + if (validate_submodule_path(path) < 0) + exit(128); + if (!submodule_from_path(the_repository, null_oid(), path)) die(_("no submodule mapping found in .gitmodules for path '%s'"), path); @@ -673,7 +679,8 @@ static void status_submodule(const char *path, const struct object_id *ce_oid, displaypath); } else if (!(flags & OPT_CACHED)) { struct object_id oid; - struct ref_store *refs = get_submodule_ref_store(path); + struct ref_store *refs = repo_get_submodule_ref_store(the_repository, + path); if (!refs) { print_status(flags, '-', path, ce_oid, displaypath); @@ -897,7 +904,8 @@ static void generate_submodule_summary(struct summary_cb *info, if (!info->cached && oideq(&p->oid_dst, null_oid())) { if (S_ISGITLINK(p->mod_dst)) { - struct ref_store *refs = get_submodule_ref_store(p->sm_path); + struct ref_store *refs = repo_get_submodule_ref_store(the_repository, + p->sm_path); if (refs) refs_head_ref(refs, handle_submodule_head_ref, &p->oid_dst); @@ -1237,6 +1245,9 @@ static void sync_submodule(const char *path, const char *prefix, if (!is_submodule_active(the_repository, path)) return; + if (validate_submodule_path(path) < 0) + exit(128); + sub = submodule_from_path(the_repository, null_oid(), path); if (sub && sub->url) { @@ -1380,6 +1391,9 @@ static void deinit_submodule(const char *path, const char *prefix, struct strbuf sb_config = STRBUF_INIT; char *sub_git_dir = xstrfmt("%s/.git", path); + if (validate_submodule_path(path) < 0) + exit(128); + sub = submodule_from_path(the_repository, null_oid(), path); if (!sub || !sub->name) @@ -1661,16 +1675,42 @@ static char *clone_submodule_sm_gitdir(const char *name) return sm_gitdir; } +static int dir_contains_only_dotgit(const char *path) +{ + DIR *dir = opendir(path); + struct dirent *e; + int ret = 1; + + if (!dir) + return 0; + + e = readdir_skip_dot_and_dotdot(dir); + if (!e) + ret = 0; + else if (strcmp(DEFAULT_GIT_DIR_ENVIRONMENT, e->d_name) || + (e = readdir_skip_dot_and_dotdot(dir))) { + error("unexpected item '%s' in '%s'", e->d_name, path); + ret = 0; + } + + closedir(dir); + return ret; +} + static int clone_submodule(const struct module_clone_data *clone_data, struct string_list *reference) { char *p; char *sm_gitdir = clone_submodule_sm_gitdir(clone_data->name); char *sm_alternate = NULL, *error_strategy = NULL; + struct stat st; struct child_process cp = CHILD_PROCESS_INIT; const char *clone_data_path = clone_data->path; char *to_free = NULL; + if (validate_submodule_path(clone_data_path) < 0) + exit(128); + if (!is_absolute_path(clone_data->path)) clone_data_path = to_free = xstrfmt("%s/%s", get_git_work_tree(), clone_data->path); @@ -1680,6 +1720,10 @@ static int clone_submodule(const struct module_clone_data *clone_data, "git dir"), sm_gitdir); if (!file_exists(sm_gitdir)) { + if (clone_data->require_init && !stat(clone_data_path, &st) && + !is_empty_dir(clone_data_path)) + die(_("directory not empty: '%s'"), clone_data_path); + if (safe_create_leading_directories_const(sm_gitdir) < 0) die(_("could not create directory '%s'"), sm_gitdir); @@ -1724,10 +1768,18 @@ static int clone_submodule(const struct module_clone_data *clone_data, if(run_command(&cp)) die(_("clone of '%s' into submodule path '%s' failed"), clone_data->url, clone_data_path); + + if (clone_data->require_init && !stat(clone_data_path, &st) && + !dir_contains_only_dotgit(clone_data_path)) { + char *dot_git = xstrfmt("%s/.git", clone_data_path); + unlink(dot_git); + free(dot_git); + die(_("directory not empty: '%s'"), clone_data_path); + } } else { char *path; - if (clone_data->require_init && !access(clone_data_path, X_OK) && + if (clone_data->require_init && !stat(clone_data_path, &st) && !is_empty_dir(clone_data_path)) die(_("directory not empty: '%s'"), clone_data_path); if (safe_create_leading_directories_const(clone_data_path) < 0) @@ -1737,6 +1789,23 @@ static int clone_submodule(const struct module_clone_data *clone_data, free(path); } + /* + * We already performed this check at the beginning of this function, + * before cloning the objects. This tries to detect racy behavior e.g. + * in parallel clones, where another process could easily have made the + * gitdir nested _after_ it was created. + * + * To prevent further harm coming from this unintentionally-nested + * gitdir, let's disable it by deleting the `HEAD` file. + */ + if (validate_submodule_git_dir(sm_gitdir, clone_data->name) < 0) { + char *head = xstrfmt("%s/HEAD", sm_gitdir); + unlink(head); + free(head); + die(_("refusing to create/use '%s' in another submodule's " + "git dir"), sm_gitdir); + } + connect_work_tree_and_git_dir(clone_data_path, sm_gitdir, 0); p = git_pathdup_submodule(clone_data_path, "config"); @@ -2389,7 +2458,9 @@ static int remote_submodule_branch(const char *path, const char **branch) } if (!strcmp(*branch, ".")) { - const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, NULL); + const char *refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + "HEAD", 0, NULL, + NULL); if (!refname) return die_message(_("No such ref: %s"), "HEAD"); @@ -2516,6 +2587,9 @@ static int update_submodule(struct update_data *update_data) { int ret; + if (validate_submodule_path(update_data->sm_path) < 0) + return -1; + ret = determine_submodule_update_strategy(the_repository, update_data->just_cloned, update_data->sm_path, @@ -2526,7 +2600,8 @@ static int update_submodule(struct update_data *update_data) if (update_data->just_cloned) oidcpy(&update_data->suboid, null_oid()); - else if (resolve_gitlink_ref(update_data->sm_path, "HEAD", &update_data->suboid)) + else if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path, + "HEAD", &update_data->suboid)) return die_message(_("Unable to find current revision in submodule path '%s'"), update_data->displaypath); @@ -2553,7 +2628,8 @@ static int update_submodule(struct update_data *update_data) update_data->sm_path); } - if (resolve_gitlink_ref(update_data->sm_path, remote_ref, &update_data->oid)) + if (repo_resolve_gitlink_ref(the_repository, update_data->sm_path, + remote_ref, &update_data->oid)) return die_message(_("Unable to find %s revision in submodule path '%s'"), remote_ref, update_data->sm_path); @@ -2623,12 +2699,21 @@ static int update_submodules(struct update_data *update_data) for (i = 0; i < suc.update_clone_nr; i++) { struct update_clone_data ucd = suc.update_clone[i]; - int code; + int code = 128; oidcpy(&update_data->oid, &ucd.oid); update_data->just_cloned = ucd.just_cloned; update_data->sm_path = ucd.sub->path; + /* + * Verify that the submodule path does not contain any + * symlinks; if it does, it might have been tampered with. + * TODO: allow exempting it via + * `safe.submodule.path` or something + */ + if (validate_submodule_path(update_data->sm_path) < 0) + goto fail; + code = ensure_core_worktree(update_data->sm_path); if (code) goto fail; @@ -2795,7 +2880,8 @@ static int push_check(int argc, const char **argv, const char *prefix UNUSED) argv++; argc--; /* Get the submodule's head ref and determine if it is detached */ - head = resolve_refdup("HEAD", 0, &head_oid, NULL); + head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD", + 0, &head_oid, NULL); if (!head) die(_("Failed to resolve HEAD as a valid ref.")); if (!strcmp(head, "HEAD")) @@ -3273,7 +3359,7 @@ static void die_on_repo_without_commits(const char *path) strbuf_addstr(&sb, path); if (is_nonbare_repository_dir(&sb)) { struct object_id oid; - if (resolve_gitlink_ref(path, "HEAD", &oid) < 0) + if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0) die(_("'%s' does not have a commit checked out"), path); } strbuf_release(&sb); @@ -3355,6 +3441,9 @@ static int module_add(int argc, const char **argv, const char *prefix) normalize_path_copy(add_data.sm_path, add_data.sm_path); strip_dir_trailing_slashes(add_data.sm_path); + if (validate_submodule_path(add_data.sm_path) < 0) + exit(128); + die_on_index_match(add_data.sm_path, force); die_on_repo_without_commits(add_data.sm_path); diff --git a/builtin/symbolic-ref.c b/builtin/symbolic-ref.c index c9defe4..81abdd1 100644 --- a/builtin/symbolic-ref.c +++ b/builtin/symbolic-ref.c @@ -18,7 +18,8 @@ static int check_symref(const char *HEAD, int quiet, int shorten, int recurse, i const char *refname; resolve_flags = (recurse ? 0 : RESOLVE_REF_NO_RECURSE); - refname = resolve_ref_unsafe(HEAD, resolve_flags, NULL, &flag); + refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + HEAD, resolve_flags, NULL, &flag); if (!refname) die("No such ref: %s", HEAD); @@ -31,7 +32,9 @@ static int check_symref(const char *HEAD, int quiet, int shorten, int recurse, i if (print) { char *to_free = NULL; if (shorten) - refname = to_free = shorten_unambiguous_ref(refname, 0); + refname = to_free = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), + refname, + 0); puts(refname); free(to_free); } @@ -66,7 +69,8 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix) die("Cannot delete %s, not a symbolic ref", argv[0]); if (!strcmp(argv[0], "HEAD")) die("deleting '%s' is not allowed", argv[0]); - return delete_ref(NULL, argv[0], NULL, REF_NO_DEREF); + return refs_delete_ref(get_main_ref_store(the_repository), + NULL, argv[0], NULL, REF_NO_DEREF); } switch (argc) { @@ -79,7 +83,8 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix) die("Refusing to point HEAD outside of refs/"); if (check_refname_format(argv[1], REFNAME_ALLOW_ONELEVEL) < 0) die("Refusing to set '%s' to invalid ref '%s'", argv[0], argv[1]); - ret = !!create_symref(argv[0], argv[1], msg); + ret = !!refs_update_symref(get_main_ref_store(the_repository), + argv[0], argv[1], msg); break; default: usage_with_options(git_symbolic_ref_usage, options); diff --git a/builtin/tag.c b/builtin/tag.c index 9a33cb5..6e2c0cf 100644 --- a/builtin/tag.c +++ b/builtin/tag.c @@ -28,9 +28,11 @@ #include "date.h" #include "write-or-die.h" #include "object-file-convert.h" +#include "trailer.h" static const char * const git_tag_usage[] = { N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] [-e]\n" + " [(--trailer <token>[(=|:)<value>])...]\n" " <tagname> [<commit> | <object>]"), N_("git tag -d <tagname>..."), N_("git tag [-n[<num>]] -l [--contains <commit>] [--no-contains <commit>]\n" @@ -87,7 +89,7 @@ static int for_each_tag_name(const char **argv, each_tag_name_fn fn, for (p = argv; *p; p++) { strbuf_reset(&ref); strbuf_addf(&ref, "refs/tags/%s", *p); - if (read_ref(ref.buf, &oid)) { + if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &oid)) { error(_("tag '%s' not found."), *p); had_error = 1; continue; @@ -116,13 +118,13 @@ static int delete_tags(const char **argv) struct string_list_item *item; result = for_each_tag_name(argv, collect_tags, (void *)&refs_to_delete); - if (delete_refs(NULL, &refs_to_delete, REF_NO_DEREF)) + if (refs_delete_refs(get_main_ref_store(the_repository), NULL, &refs_to_delete, REF_NO_DEREF)) result = 1; for_each_string_list_item(item, &refs_to_delete) { const char *name = item->string; struct object_id *oid = item->util; - if (!ref_exists(name)) + if (!refs_ref_exists(get_main_ref_store(the_repository), name)) printf(_("Deleted tag '%s' (was %s)\n"), item->string + 10, repo_find_unique_abbrev(the_repository, oid, DEFAULT_ABBREV)); @@ -290,10 +292,12 @@ static const char message_advice_nested_tag[] = static void create_tag(const struct object_id *object, const char *object_ref, const char *tag, struct strbuf *buf, struct create_tag_options *opt, - struct object_id *prev, struct object_id *result, char *path) + struct object_id *prev, struct object_id *result, + struct strvec *trailer_args, char *path) { enum object_type type; struct strbuf header = STRBUF_INIT; + int should_edit; type = oid_object_info(the_repository, object, NULL); if (type <= OBJ_NONE) @@ -313,13 +317,15 @@ static void create_tag(const struct object_id *object, const char *object_ref, tag, git_committer_info(IDENT_STRICT)); - if (!opt->message_given || opt->use_editor) { + should_edit = opt->use_editor || !opt->message_given; + if (should_edit || trailer_args->nr) { int fd; /* write the template message before editing: */ fd = xopen(path, O_CREAT | O_TRUNC | O_WRONLY, 0600); - if (opt->message_given) { + if (opt->message_given && buf->len) { + strbuf_complete(buf, '\n'); write_or_die(fd, buf->buf, buf->len); strbuf_reset(buf); } else if (!is_null_oid(prev)) { @@ -338,10 +344,19 @@ static void create_tag(const struct object_id *object, const char *object_ref, } close(fd); - if (launch_editor(path, buf, NULL)) { - fprintf(stderr, - _("Please supply the message using either -m or -F option.\n")); - exit(1); + if (trailer_args->nr && amend_file_with_trailers(path, trailer_args)) + die(_("unable to pass trailers to --trailers")); + + if (should_edit) { + if (launch_editor(path, buf, NULL)) { + fprintf(stderr, + _("Please supply the message using either -m or -F option.\n")); + exit(1); + } + } else if (trailer_args->nr) { + strbuf_reset(buf); + if (strbuf_read_file(buf, path, 0) < 0) + die_errno(_("failed to read '%s'"), path); } } @@ -463,6 +478,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix) struct ref_sorting *sorting; struct string_list sorting_options = STRING_LIST_INIT_DUP; struct ref_format format = REF_FORMAT_INIT; + struct strvec trailer_args = STRVEC_INIT; int icase = 0; int edit_flag = 0; struct option options[] = { @@ -479,6 +495,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix) OPT_CALLBACK_F('m', "message", &msg, N_("message"), N_("tag message"), PARSE_OPT_NONEG, parse_msg_arg), OPT_FILENAME('F', "file", &msgfile, N_("read message from file")), + OPT_PASSTHRU_ARGV(0, "trailer", &trailer_args, N_("trailer"), + N_("add custom trailer(s)"), PARSE_OPT_NONEG), OPT_BOOL('e', "edit", &edit_flag, N_("force edit of tag message")), OPT_BOOL('s', "sign", &opt.sign, N_("annotated and GPG-signed tag")), OPT_CLEANUP(&cleanup_arg), @@ -548,7 +566,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix) opt.sign = 1; set_signing_key(keyid); } - create_tag_object = (opt.sign || annotate || msg.given || msgfile); + create_tag_object = (opt.sign || annotate || msg.given || msgfile || + edit_flag || trailer_args.nr); if ((create_tag_object || force) && (cmdmode != 0)) usage_with_options(git_tag_usage, options); @@ -630,7 +649,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix) if (strbuf_check_tag_ref(&ref, tag)) die(_("'%s' is not a valid tag name."), tag); - if (read_ref(ref.buf, &prev)) + if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &prev)) oidclr(&prev); else if (!force) die(_("tag '%s' already exists"), tag); @@ -654,12 +673,14 @@ int cmd_tag(int argc, const char **argv, const char *prefix) opt.sign = 1; path = git_pathdup("TAG_EDITMSG"); create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object, - path); + &trailer_args, path); } - transaction = ref_transaction_begin(&err); + transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!transaction || ref_transaction_update(transaction, ref.buf, &object, &prev, + NULL, NULL, create_reflog ? REF_FORCE_CREATE_REFLOG : 0, reflog_msg.buf, &err) || ref_transaction_commit(transaction, &err)) { @@ -686,6 +707,7 @@ cleanup: strbuf_release(&reflog_msg); strbuf_release(&msg.buf); strbuf_release(&err); + strvec_clear(&trailer_args); free(msgfile); return ret; } diff --git a/builtin/update-index.c b/builtin/update-index.c index 6321810..d343416 100644 --- a/builtin/update-index.c +++ b/builtin/update-index.c @@ -349,7 +349,8 @@ static int process_directory(const char *path, int len, struct stat *st) if (S_ISGITLINK(ce->ce_mode)) { /* Do nothing to the index if there is no HEAD! */ - if (resolve_gitlink_ref(path, "HEAD", &oid) < 0) + if (repo_resolve_gitlink_ref(the_repository, path, + "HEAD", &oid) < 0) return 0; return add_one_path(ce, path, len, st); @@ -375,7 +376,7 @@ static int process_directory(const char *path, int len, struct stat *st) } /* No match - should we add it as a gitlink? */ - if (!resolve_gitlink_ref(path, "HEAD", &oid)) + if (!repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid)) return add_one_path(NULL, path, len, st); /* Error out. */ @@ -682,7 +683,7 @@ static int do_reupdate(const char **paths, PATHSPEC_PREFER_CWD, prefix, paths); - if (read_ref("HEAD", &head_oid)) + if (refs_read_ref(get_main_ref_store(the_repository), "HEAD", &head_oid)) /* If there is no HEAD, that means it is an initial * commit. Update everything in the index. */ diff --git a/builtin/update-ref.c b/builtin/update-ref.c index e46afbc..6cda1c0 100644 --- a/builtin/update-ref.c +++ b/builtin/update-ref.c @@ -204,6 +204,7 @@ static void parse_cmd_update(struct ref_transaction *transaction, if (ref_transaction_update(transaction, refname, &new_oid, have_old ? &old_oid : NULL, + NULL, NULL, update_flags | create_reflog_flag, msg, &err)) die("%s", err.buf); @@ -397,7 +398,8 @@ static void update_refs_stdin(void) struct ref_transaction *transaction; int i, j; - transaction = ref_transaction_begin(&err); + transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!transaction) die("%s", err.buf); @@ -464,7 +466,8 @@ static void update_refs_stdin(void) * get a "start". */ state = cmd->state; - transaction = ref_transaction_begin(&err); + transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!transaction) die("%s", err.buf); @@ -571,11 +574,14 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix) * For purposes of backwards compatibility, we treat * NULL_SHA1 as "don't care" here: */ - return delete_ref(msg, refname, - (oldval && !is_null_oid(&oldoid)) ? &oldoid : NULL, - default_flags); + return refs_delete_ref(get_main_ref_store(the_repository), + msg, refname, + (oldval && !is_null_oid(&oldoid)) ? &oldoid : NULL, + default_flags); else - return update_ref(msg, refname, &oid, oldval ? &oldoid : NULL, - default_flags | create_reflog_flag, - UPDATE_REFS_DIE_ON_ERR); + return refs_update_ref(get_main_ref_store(the_repository), + msg, refname, &oid, + oldval ? &oldoid : NULL, + default_flags | create_reflog_flag, + UPDATE_REFS_DIE_ON_ERR); } diff --git a/builtin/upload-pack.c b/builtin/upload-pack.c index 15afb97..46d9327 100644 --- a/builtin/upload-pack.c +++ b/builtin/upload-pack.c @@ -9,6 +9,7 @@ #include "upload-pack.h" #include "serve.h" #include "commit.h" +#include "environment.h" static const char * const upload_pack_usage[] = { N_("git-upload-pack [--[no-]strict] [--timeout=<n>] [--stateless-rpc]\n" @@ -39,6 +40,7 @@ int cmd_upload_pack(int argc, const char **argv, const char *prefix) packet_trace_identity("upload-pack"); disable_replace_refs(); save_commit_buffer = 0; + xsetenv(NO_LAZY_FETCH_ENVIRONMENT, "1", 0); argc = parse_options(argc, argv, prefix, options, upload_pack_usage, 0); diff --git a/builtin/var.c b/builtin/var.c index cf55672..5dc3848 100644 --- a/builtin/var.c +++ b/builtin/var.c @@ -46,7 +46,7 @@ static char *pager(int ident_flag UNUSED) static char *default_branch(int ident_flag UNUSED) { - return xstrdup_or_null(git_default_branch_name(1)); + return repo_default_branch_name(the_repository, 1); } static char *shell_path(int ident_flag UNUSED) diff --git a/builtin/worktree.c b/builtin/worktree.c index 7c6c725..893e973 100644 --- a/builtin/worktree.c +++ b/builtin/worktree.c @@ -433,7 +433,7 @@ static int add_worktree(const char *path, const char *refname, /* is 'refname' a branch or commit? */ if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) && - ref_exists(symref.buf)) { + refs_ref_exists(get_main_ref_store(the_repository), symref.buf)) { is_branch = 1; if (!opts->force) die_if_checked_out(symref.buf, 0); @@ -509,7 +509,7 @@ static int add_worktree(const char *path, const char *refname, } wt_refs = get_worktree_ref_store(wt); - ret = refs_init_db(wt_refs, REFS_INIT_DB_IS_WORKTREE, &sb); + ret = ref_store_create_on_disk(wt_refs, REF_STORE_CREATE_ON_DISK_IS_WORKTREE, &sb); if (ret) goto done; @@ -517,7 +517,7 @@ static int add_worktree(const char *path, const char *refname, ret = refs_update_ref(wt_refs, NULL, "HEAD", &commit->object.oid, NULL, 0, UPDATE_REFS_MSG_ON_ERR); else - ret = refs_create_symref(wt_refs, "HEAD", symref.buf, NULL); + ret = refs_update_symref(wt_refs, "HEAD", symref.buf, NULL); if (ret) goto done; @@ -605,7 +605,7 @@ static void print_preparing_worktree_line(int detach, } else { struct strbuf s = STRBUF_INIT; if (!detach && !strbuf_check_branch_ref(&s, branch) && - ref_exists(s.buf)) + refs_ref_exists(get_main_ref_store(the_repository), s.buf)) fprintf_ln(stderr, _("Preparing worktree (checking out '%s')"), branch); else { @@ -647,9 +647,9 @@ static int first_valid_ref(const char *refname UNUSED, */ static int can_use_local_refs(const struct add_opts *opts) { - if (head_ref(first_valid_ref, NULL)) { + if (refs_head_ref(get_main_ref_store(the_repository), first_valid_ref, NULL)) { return 1; - } else if (for_each_branch_ref(first_valid_ref, NULL)) { + } else if (refs_for_each_branch_ref(get_main_ref_store(the_repository), first_valid_ref, NULL)) { if (!opts->quiet) { struct strbuf path = STRBUF_INIT; struct strbuf contents = STRBUF_INIT; @@ -689,7 +689,7 @@ static int can_use_remote_refs(const struct add_opts *opts) { if (!guess_remote) { return 0; - } else if (for_each_remote_ref(first_valid_ref, NULL)) { + } else if (refs_for_each_remote_ref(get_main_ref_store(the_repository), first_valid_ref, NULL)) { return 1; } else if (!opts->force && remote_get(NULL)) { die(_("No local or remote refs exist despite at least one remote\n" @@ -747,7 +747,8 @@ static const char *dwim_branch(const char *path, const char **new_branch) UNLEAK(branchname); branch_exists = !strbuf_check_branch_ref(&ref, branchname) && - ref_exists(ref.buf); + refs_ref_exists(get_main_ref_store(the_repository), + ref.buf); strbuf_release(&ref); if (branch_exists) return branchname; @@ -838,7 +839,7 @@ static int add(int ac, const char **av, const char *prefix) if (!opts.force && !strbuf_check_branch_ref(&symref, new_branch) && - ref_exists(symref.buf)) + refs_ref_exists(get_main_ref_store(the_repository), symref.buf)) die_if_checked_out(symref.buf, 0); strbuf_release(&symref); } @@ -974,7 +975,9 @@ static void show_worktree(struct worktree *wt, int path_maxlen, int abbrev_len) if (wt->is_detached) strbuf_addstr(&sb, "(detached HEAD)"); else if (wt->head_ref) { - char *ref = shorten_unambiguous_ref(wt->head_ref, 0); + char *ref = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), + wt->head_ref, + 0); strbuf_addf(&sb, "[%s]", ref); free(ref); } else diff --git a/bundle-uri.c b/bundle-uri.c index ca32050..91b3319 100644 --- a/bundle-uri.c +++ b/bundle-uri.c @@ -395,11 +395,13 @@ static int unbundle_from_file(struct repository *r, const char *file) strbuf_setlen(&bundle_ref, bundle_prefix_len); strbuf_addstr(&bundle_ref, branch_name); - has_old = !read_ref(bundle_ref.buf, &old_oid); - update_ref("fetched bundle", bundle_ref.buf, oid, - has_old ? &old_oid : NULL, - REF_SKIP_OID_VERIFICATION, - UPDATE_REFS_MSG_ON_ERR); + has_old = !refs_read_ref(get_main_ref_store(the_repository), + bundle_ref.buf, &old_oid); + refs_update_ref(get_main_ref_store(the_repository), + "fetched bundle", bundle_ref.buf, oid, + has_old ? &old_oid : NULL, + REF_SKIP_OID_VERIFICATION, + UPDATE_REFS_MSG_ON_ERR); } bundle_header_release(&header); @@ -389,7 +389,7 @@ static int write_bundle_refs(int bundle_fd, struct rev_info *revs) if (repo_dwim_ref(the_repository, e->name, strlen(e->name), &oid, &ref, 0) != 1) goto skip_write_ref; - if (read_ref_full(e->name, RESOLVE_REF_READING, &oid, &flag)) + if (refs_read_ref_full(get_main_ref_store(the_repository), e->name, RESOLVE_REF_READING, &oid, &flag)) flag = 0; display_ref = (flag & REF_ISSYMREF) ? e->name : ref; diff --git a/ci/check-whitespace.sh b/ci/check-whitespace.sh new file mode 100755 index 0000000..db39909 --- /dev/null +++ b/ci/check-whitespace.sh @@ -0,0 +1,95 @@ +#!/usr/bin/env bash +# +# Check that commits after a specified point do not contain new or modified +# lines with whitespace errors. An optional formatted summary can be generated +# by providing an output file path and url as additional arguments. +# + +baseCommit=$1 +outputFile=$2 +url=$3 + +if test "$#" -ne 1 && test "$#" -ne 3 +then + echo "USAGE: $0 <BASE_COMMIT> [<OUTPUT_FILE> <URL>]" + exit 1 +fi + +problems=() +commit= +commitText= +commitTextmd= +goodParent= + +while read dash sha etc +do + case "${dash}" in + "---") # Line contains commit information. + if test -z "${goodParent}" + then + # Assume the commit has no whitespace errors until detected otherwise. + goodParent=${sha} + fi + + commit="${sha}" + commitText="${sha} ${etc}" + commitTextmd="[${sha}](${url}/commit/${sha}) ${etc}" + ;; + "") + ;; + *) # Line contains whitespace error information for current commit. + if test -n "${goodParent}" + then + problems+=("1) --- ${commitTextmd}") + echo "" + echo "--- ${commitText}" + goodParent= + fi + + case "${dash}" in + *:[1-9]*:) # contains file and line number information + dashend=${dash#*:} + problems+=("[${dash}](${url}/blob/${commit}/${dash%%:*}#L${dashend%:}) ${sha} ${etc}") + ;; + *) + problems+=("\`${dash} ${sha} ${etc}\`") + ;; + esac + echo "${dash} ${sha} ${etc}" + ;; + esac +done <<< "$(git log --check --pretty=format:"---% h% s" "${baseCommit}"..)" + +if test ${#problems[*]} -gt 0 +then + if test -z "${goodParent}" + then + goodParent=${baseCommit: 0:7} + fi + + echo "A whitespace issue was found in onen of more of the commits." + echo "Run the following command to resolve whitespace issues:" + echo "git rebase --whitespace=fix ${goodParent}" + + # If target output file is provided, write formatted output. + if test -n "$outputFile" + then + echo "🛑 Please review the Summary output for further information." + ( + echo "### :x: A whitespace issue was found in one or more of the commits." + echo "" + echo "Run these commands to correct the problem:" + echo "1. \`git rebase --whitespace=fix ${goodParent}\`" + echo "1. \`git push --force\`" + echo "" + echo "Errors:" + + for i in "${problems[@]}" + do + echo "${i}" + done + ) >"$outputFile" + fi + + exit 2 +fi diff --git a/ci/install-dependencies.sh b/ci/install-dependencies.sh index c196e56..6ec0f85 100755 --- a/ci/install-dependencies.sh +++ b/ci/install-dependencies.sh @@ -27,7 +27,7 @@ alpine-*) apk add --update shadow sudo build-base curl-dev openssl-dev expat-dev gettext \ pcre2-dev python3 musl-libintl perl-utils ncurses \ apache2 apache2-http2 apache2-proxy apache2-ssl apache2-webdav apr-util-dbd_sqlite3 \ - bash cvs gnupg perl-cgi perl-dbd-sqlite >/dev/null + bash cvs gnupg perl-cgi perl-dbd-sqlite perl-io-tty >/dev/null ;; fedora-*) dnf -yq update >/dev/null && @@ -42,7 +42,7 @@ ubuntu-*) language-pack-is libsvn-perl apache2 cvs cvsps git gnupg subversion \ make libssl-dev libcurl4-openssl-dev libexpat-dev wget sudo default-jre \ tcl tk gettext zlib1g-dev perl-modules liberror-perl libauthen-sasl-perl \ - libemail-valid-perl libio-socket-ssl-perl libnet-smtp-ssl-perl libdbd-sqlite3-perl libcgi-pm-perl \ + libemail-valid-perl libio-pty-perl libio-socket-ssl-perl libnet-smtp-ssl-perl libdbd-sqlite3-perl libcgi-pm-perl \ ${CC_PACKAGE:-${CC:-gcc}} $PYTHON_PACKAGE mkdir --parents "$CUSTOM_PATH" @@ -69,8 +69,6 @@ macos-*) export HOMEBREW_NO_AUTO_UPDATE=1 HOMEBREW_NO_INSTALL_CLEANUP=1 # Uncomment this if you want to run perf tests: # brew install gnu-time - test -z "$BREW_INSTALL_PACKAGES" || - brew install $BREW_INSTALL_PACKAGES brew link --force gettext mkdir -p "$CUSTOM_PATH" @@ -18,7 +18,7 @@ elif test true = "$GITLAB_CI" then begin_group () { need_to_end_group=t - printf "\e[0Ksection_start:$(date +%s):$(echo "$1" | tr ' ' _)\r\e[0K$1\n" + printf "\e[0Ksection_start:$(date +%s):$(echo "$1" | tr ' ' _)[collapsed=true]\r\e[0K$1\n" trap "end_group '$1'" EXIT set -x } @@ -325,9 +325,13 @@ ubuntu-*) break fi - PYTHON_PACKAGE=python2 - if test "$jobname" = linux-gcc + # Python 2 is end of life, and Ubuntu 23.04 and newer don't actually + # have it anymore. We thus only test with Python 2 on older LTS + # releases. + if "$distro" = "ubuntu-20.04" then + PYTHON_PACKAGE=python2 + else PYTHON_PACKAGE=python3 fi MAKEFLAGS="$MAKEFLAGS PYTHON_PATH=/usr/bin/$PYTHON_PACKAGE" diff --git a/ci/run-build-and-tests.sh b/ci/run-build-and-tests.sh index c192bd6..98dda42 100755 --- a/ci/run-build-and-tests.sh +++ b/ci/run-build-and-tests.sh @@ -53,8 +53,6 @@ if test -n "$run_tests" then group "Run tests" make test || handle_failed_tests - group "Run unit tests" \ - make DEFAULT_UNIT_TEST_TARGET=unit-tests-prove unit-tests fi check_unignored_build_artifacts diff --git a/ci/run-test-slice.sh b/ci/run-test-slice.sh index ae80943..e167e64 100755 --- a/ci/run-test-slice.sh +++ b/ci/run-test-slice.sh @@ -17,7 +17,7 @@ handle_failed_tests # We only have one unit test at the moment, so run it in the first slice if [ "$1" == "0" ] ; then - group "Run unit tests" make --quiet -C t unit-tests-prove + group "Run unit tests" make --quiet -C t unit-tests-test-tool fi check_unignored_build_artifacts diff --git a/combine-diff.c b/combine-diff.c index d6d6fa1..4960d90 100644 --- a/combine-diff.c +++ b/combine-diff.c @@ -1066,7 +1066,8 @@ static void show_patch_diff(struct combine_diff_path *elem, int num_parent, elem->mode = canon_mode(st.st_mode); } else if (S_ISDIR(st.st_mode)) { struct object_id oid; - if (resolve_gitlink_ref(elem->path, "HEAD", &oid) < 0) + if (repo_resolve_gitlink_ref(the_repository, elem->path, + "HEAD", &oid) < 0) result = grab_blob(opt->repo, &elem->oid, elem->mode, &result_size, NULL, NULL); diff --git a/commit-graph.c b/commit-graph.c index 45417d7..e5dd355 100644 --- a/commit-graph.c +++ b/commit-graph.c @@ -1821,7 +1821,7 @@ static int add_ref_to_set(const char *refname UNUSED, struct object_id peeled; struct refs_cb_data *data = (struct refs_cb_data *)cb_data; - if (!peel_iterated_oid(oid, &peeled)) + if (!peel_iterated_oid(the_repository, oid, &peeled)) oid = &peeled; if (oid_object_info(the_repository, oid, NULL) == OBJ_COMMIT) oidset_insert(data->commits, oid); @@ -1845,7 +1845,8 @@ int write_commit_graph_reachable(struct object_directory *odb, data.progress = start_delayed_progress( _("Collecting referenced commits"), 0); - for_each_ref(add_ref_to_set, &data); + refs_for_each_ref(get_main_ref_store(the_repository), add_ref_to_set, + &data); stop_progress(&data.progress); @@ -1070,7 +1070,8 @@ struct commit *get_fork_point(const char *refname, struct commit *commit) memset(&revs, 0, sizeof(revs)); revs.initial = 1; - for_each_reflog_ent(full_refname, collect_one_reflog_ent, &revs); + refs_for_each_reflog_ent(get_main_ref_store(the_repository), + full_refname, collect_one_reflog_ent, &revs); if (!revs.nr) add_one_commit(&oid, &revs); diff --git a/compat/mingw.c b/compat/mingw.c index 4876344..6b06ea5 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -3159,6 +3159,7 @@ int uname(struct utsname *buf) return 0; } +#ifndef NO_UNIX_SOCKETS int mingw_have_unix_sockets(void) { SC_HANDLE scm, srvc; @@ -3177,3 +3178,4 @@ int mingw_have_unix_sockets(void) } return ret; } +#endif diff --git a/compat/regex/regcomp.c b/compat/regex/regcomp.c index d1bc09e..2bc0f11 100644 --- a/compat/regex/regcomp.c +++ b/compat/regex/regcomp.c @@ -868,7 +868,7 @@ init_dfa (re_dfa_t *dfa, size_t pat_len) if (table_size > pat_len) break; - dfa->state_table = calloc (sizeof (struct re_state_table_entry), table_size); + dfa->state_table = calloc (table_size, sizeof (struct re_state_table_entry)); dfa->state_hash_mask = table_size - 1; dfa->mb_cur_max = MB_CUR_MAX; @@ -936,7 +936,7 @@ init_dfa (re_dfa_t *dfa, size_t pat_len) { int i, j, ch; - dfa->sb_char = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1); + dfa->sb_char = (re_bitset_ptr_t) calloc (1, sizeof (bitset_t)); if (BE (dfa->sb_char == NULL, 0)) return REG_ESPACE; @@ -3079,9 +3079,9 @@ parse_bracket_exp (re_string_t *regexp, re_dfa_t *dfa, re_token_t *token, _NL_COLLATE_SYMB_EXTRAMB); } #endif - sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1); + sbcset = (re_bitset_ptr_t) calloc (1, sizeof (bitset_t)); #ifdef RE_ENABLE_I18N - mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1); + mbcset = (re_charset_t *) calloc (1, sizeof (re_charset_t)); #endif /* RE_ENABLE_I18N */ #ifdef RE_ENABLE_I18N if (BE (sbcset == NULL || mbcset == NULL, 0)) @@ -3626,9 +3626,9 @@ build_charclass_op (re_dfa_t *dfa, RE_TRANSLATE_TYPE trans, re_token_t br_token; bin_tree_t *tree; - sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1); + sbcset = (re_bitset_ptr_t) calloc (1, sizeof (bitset_t)); #ifdef RE_ENABLE_I18N - mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1); + mbcset = (re_charset_t *) calloc (1, sizeof (re_charset_t)); #endif /* RE_ENABLE_I18N */ #ifdef RE_ENABLE_I18N diff --git a/compat/regex/regex_internal.c b/compat/regex/regex_internal.c index ec51cf3..ec5cc5d 100644 --- a/compat/regex/regex_internal.c +++ b/compat/regex/regex_internal.c @@ -1628,7 +1628,7 @@ create_ci_newstate (const re_dfa_t *dfa, const re_node_set *nodes, reg_errcode_t err; re_dfastate_t *newstate; - newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1); + newstate = (re_dfastate_t *) calloc (1, sizeof (re_dfastate_t)); if (BE (newstate == NULL, 0)) return NULL; err = re_node_set_init_copy (&newstate->nodes, nodes); @@ -1678,7 +1678,7 @@ create_cd_newstate (const re_dfa_t *dfa, const re_node_set *nodes, reg_errcode_t err; re_dfastate_t *newstate; - newstate = (re_dfastate_t *) calloc (sizeof (re_dfastate_t), 1); + newstate = (re_dfastate_t *) calloc (1, sizeof (re_dfastate_t)); if (BE (newstate == NULL, 0)) return NULL; err = re_node_set_init_copy (&newstate->nodes, nodes); diff --git a/compat/regex/regexec.c b/compat/regex/regexec.c index 49358ae..e92be57 100644 --- a/compat/regex/regexec.c +++ b/compat/regex/regexec.c @@ -2796,8 +2796,8 @@ get_subexp (re_match_context_t *mctx, int bkref_node, int bkref_str_idx) continue; /* No. */ if (sub_top->path == NULL) { - sub_top->path = calloc (sizeof (state_array_t), - sl_str - sub_top->str_idx + 1); + sub_top->path = calloc (sl_str - sub_top->str_idx + 1, + sizeof (state_array_t)); if (sub_top->path == NULL) return REG_ESPACE; } @@ -3361,7 +3361,7 @@ build_trtable (const re_dfa_t *dfa, re_dfastate_t *state) if (ndests == 0) { state->trtable = (re_dfastate_t **) - calloc (sizeof (re_dfastate_t *), SBC_MAX); + calloc (SBC_MAX, sizeof (re_dfastate_t *)); return 1; } return 0; @@ -3457,7 +3457,7 @@ out_free: discern by looking at the character code: allocate a 256-entry transition table. */ trtable = state->trtable = - (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), SBC_MAX); + (re_dfastate_t **) calloc (SBC_MAX, sizeof (re_dfastate_t *)); if (BE (trtable == NULL, 0)) goto out_free; @@ -3488,7 +3488,7 @@ out_free: transition tables, one starting at trtable[0] and one starting at trtable[SBC_MAX]. */ trtable = state->word_trtable = - (re_dfastate_t **) calloc (sizeof (re_dfastate_t *), 2 * SBC_MAX); + (re_dfastate_t **) calloc (2 * SBC_MAX, sizeof (re_dfastate_t *)); if (BE (trtable == NULL, 0)) goto out_free; @@ -125,7 +125,7 @@ struct config_include_data { config_fn_t fn; void *data; const struct config_options *opts; - struct git_config_source *config_source; + const struct git_config_source *config_source; struct repository *repo; /* @@ -303,7 +303,8 @@ static int include_by_branch(const char *cond, size_t cond_len) int ret; struct strbuf pattern = STRBUF_INIT; const char *refname = !the_repository->gitdir ? - NULL : resolve_ref_unsafe("HEAD", 0, NULL, &flags); + NULL : refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + "HEAD", 0, NULL, &flags); const char *shortname; if (!refname || !(flags & REF_ISSYMREF) || @@ -2105,7 +2106,7 @@ static int do_git_config_sequence(const struct config_options *opts, } int config_with_options(config_fn_t fn, void *data, - struct git_config_source *config_source, + const struct git_config_source *config_source, struct repository *repo, const struct config_options *opts) { @@ -3182,14 +3183,10 @@ void git_config_set(const char *key, const char *value) trace2_cmd_set_config(key, value); } -/* - * The ownership rule is that the caller will own the string - * if it receives a piece of memory different from what it passed - * as the parameter. - */ -const char *git_config_prepare_comment_string(const char *comment) +char *git_config_prepare_comment_string(const char *comment) { size_t leading_blanks; + char *prepared; if (!comment) return NULL; @@ -3210,13 +3207,13 @@ const char *git_config_prepare_comment_string(const char *comment) leading_blanks = strspn(comment, " \t"); if (leading_blanks && comment[leading_blanks] == '#') - ; /* use it as-is */ + prepared = xstrdup(comment); /* use it as-is */ else if (comment[0] == '#') - comment = xstrfmt(" %s", comment); + prepared = xstrfmt(" %s", comment); else - comment = xstrfmt(" # %s", comment); + prepared = xstrfmt(" # %s", comment); - return comment; + return prepared; } static void validate_comment_string(const char *comment) @@ -232,7 +232,7 @@ void git_config(config_fn_t fn, void *); * sets `opts.respect_includes` to `1` by default. */ int config_with_options(config_fn_t fn, void *, - struct git_config_source *config_source, + const struct git_config_source *config_source, struct repository *repo, const struct config_options *opts); @@ -338,7 +338,7 @@ void git_config_set_multivar(const char *, const char *, const char *, unsigned) int repo_config_set_multivar_gently(struct repository *, const char *, const char *, const char *, unsigned); int git_config_set_multivar_in_file_gently(const char *, const char *, const char *, const char *, const char *, unsigned); -const char *git_config_prepare_comment_string(const char *); +char *git_config_prepare_comment_string(const char *); /** * takes four parameters: diff --git a/contrib/buildsystems/CMakeLists.txt b/contrib/buildsystems/CMakeLists.txt index 804629c..2f9c335 100644 --- a/contrib/buildsystems/CMakeLists.txt +++ b/contrib/buildsystems/CMakeLists.txt @@ -1005,10 +1005,11 @@ endforeach() #test-tool parse_makefile_for_sources(test-tool_SOURCES "TEST_BUILTINS_OBJS") +add_library(test-lib OBJECT ${CMAKE_SOURCE_DIR}/t/unit-tests/test-lib.c) list(TRANSFORM test-tool_SOURCES PREPEND "${CMAKE_SOURCE_DIR}/t/helper/") add_executable(test-tool ${CMAKE_SOURCE_DIR}/t/helper/test-tool.c ${test-tool_SOURCES} ${test-reftable_SOURCES}) -target_link_libraries(test-tool common-main) +target_link_libraries(test-tool test-lib common-main) set_target_properties(test-fake-ssh test-tool PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/t/helper) diff --git a/contrib/coccinelle/refs.cocci b/contrib/coccinelle/refs.cocci new file mode 100644 index 0000000..31d9cad --- /dev/null +++ b/contrib/coccinelle/refs.cocci @@ -0,0 +1,103 @@ +// Migrate "refs.h" to not rely on `the_repository` implicitly anymore. +@@ +@@ +( +- resolve_ref_unsafe ++ refs_resolve_ref_unsafe +| +- resolve_refdup ++ refs_resolve_refdup +| +- read_ref_full ++ refs_read_ref_full +| +- read_ref ++ refs_read_ref +| +- ref_exists ++ refs_ref_exists +| +- head_ref ++ refs_head_ref +| +- for_each_ref ++ refs_for_each_ref +| +- for_each_ref_in ++ refs_for_each_ref_in +| +- for_each_fullref_in ++ refs_for_each_fullref_in +| +- for_each_tag_ref ++ refs_for_each_tag_ref +| +- for_each_branch_ref ++ refs_for_each_branch_ref +| +- for_each_remote_ref ++ refs_for_each_remote_ref +| +- for_each_glob_ref ++ refs_for_each_glob_ref +| +- for_each_glob_ref_in ++ refs_for_each_glob_ref_in +| +- head_ref_namespaced ++ refs_head_ref_namespaced +| +- for_each_namespaced_ref ++ refs_for_each_namespaced_ref +| +- for_each_rawref ++ refs_for_each_rawref +| +- safe_create_reflog ++ refs_create_reflog +| +- reflog_exists ++ refs_reflog_exists +| +- delete_ref ++ refs_delete_ref +| +- delete_refs ++ refs_delete_refs +| +- delete_reflog ++ refs_delete_reflog +| +- for_each_reflog_ent ++ refs_for_each_reflog_ent +| +- for_each_reflog_ent_reverse ++ refs_for_each_reflog_ent_reverse +| +- for_each_reflog ++ refs_for_each_reflog +| +- shorten_unambiguous_ref ++ refs_shorten_unambiguous_ref +| +- rename_ref ++ refs_rename_ref +| +- copy_existing_ref ++ refs_copy_existing_ref +| +- create_symref ++ refs_create_symref +| +- ref_transaction_begin ++ ref_store_transaction_begin +| +- update_ref ++ refs_update_ref +| +- reflog_expire ++ refs_reflog_expire +) + ( ++ get_main_ref_store(the_repository), + ...) diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash index 5c0ddeb..60a22d6 100644 --- a/contrib/completion/git-completion.bash +++ b/contrib/completion/git-completion.bash @@ -2989,22 +2989,42 @@ __git_complete_config_variable_name_and_value () _git_config () { - case "$prev" in - --get|--get-all|--unset|--unset-all) - __gitcomp_nl "$(__git_config_get_set_variables)" + local subcommands subcommand + + __git_resolve_builtins "config" + + subcommands="$___git_resolved_builtins" + subcommand="$(__git_find_subcommand "$subcommands")" + + if [ -z "$subcommand" ] + then + __gitcomp "$subcommands" return - ;; - *.*) - __git_complete_config_variable_value + fi + + case "$cur" in + --*) + __gitcomp_builtin "config_$subcommand" return ;; esac - case "$cur" in - --*) - __gitcomp_builtin config + + case "$subcommand" in + get) + __gitcomp_nl "$(__git_config_get_set_variables)" ;; - *) - __git_complete_config_variable_name + set) + case "$prev" in + *.*) + __git_complete_config_variable_value + ;; + *) + __git_complete_config_variable_name + ;; + esac + ;; + unset) + __gitcomp_nl "$(__git_config_get_set_variables)" ;; esac } diff --git a/contrib/credential/osxkeychain/git-credential-osxkeychain.c b/contrib/credential/osxkeychain/git-credential-osxkeychain.c index 6a40917..6ce22a2 100644 --- a/contrib/credential/osxkeychain/git-credential-osxkeychain.c +++ b/contrib/credential/osxkeychain/git-credential-osxkeychain.c @@ -12,6 +12,7 @@ static CFStringRef username; static CFDataRef password; static CFDataRef password_expiry_utc; static CFDataRef oauth_refresh_token; +static int state_seen; static void clear_credential(void) { @@ -171,6 +172,9 @@ static OSStatus find_internet_password(void) CFRelease(item); + write_item("capability[]", "state", strlen("state")); + write_item("state[]", "osxkeychain:seen=1", strlen("osxkeychain:seen=1")); + out: CFRelease(attrs); @@ -284,6 +288,9 @@ static OSStatus add_internet_password(void) CFDictionaryRef attrs; OSStatus result; + if (state_seen) + return errSecSuccess; + /* Only store complete credentials */ if (!protocol || !host || !username || !password) return -1; @@ -395,6 +402,10 @@ static void read_credential(void) oauth_refresh_token = CFDataCreate(kCFAllocatorDefault, (UInt8 *)v, strlen(v)); + else if (!strcmp(buf, "state[]")) { + if (!strcmp(v, "osxkeychain:seen=1")) + state_seen = 1; + } /* * Ignore other lines; we don't know what they mean, but * this future-proofs us when later versions of git do @@ -414,6 +425,9 @@ int main(int argc, const char **argv) if (!argv[1]) die("%s", usage); + if (open(argv[0], O_RDONLY | O_EXLOCK) == -1) + die("failed to lock %s", argv[0]); + read_credential(); if (!strcmp(argv[1], "get")) @@ -1,6 +1,9 @@ #include "git-compat-util.h" #include "copy.h" #include "path.h" +#include "gettext.h" +#include "strbuf.h" +#include "abspath.h" int copy_fd(int ifd, int ofd) { diff --git a/delta-islands.c b/delta-islands.c index f7e0794..4ac3c10 100644 --- a/delta-islands.c +++ b/delta-islands.c @@ -488,7 +488,8 @@ void load_delta_islands(struct repository *r, int progress) git_config(island_config_callback, &ild); ild.remote_islands = kh_init_str(); - for_each_ref(find_island_for_ref, &ild); + refs_for_each_ref(get_main_ref_store(the_repository), + find_island_for_ref, &ild); free_config_regexes(&ild); deduplicate_islands(ild.remote_islands, r); free_remote_islands(ild.remote_islands); @@ -66,7 +66,8 @@ static int check_removed(const struct cache_entry *ce, struct stat *st) * a directory --- the blob was removed! */ if (!S_ISGITLINK(ce->ce_mode) && - resolve_gitlink_ref(ce->name, "HEAD", &sub)) + repo_resolve_gitlink_ref(the_repository, ce->name, + "HEAD", &sub)) return 1; } return 0; @@ -4555,6 +4555,7 @@ static void run_diff_cmd(const char *pgm, o, complete_rewrite); } else { fprintf(o->file, "* Unmerged path %s\n", name); + o->found_changes = 1; } } @@ -100,6 +100,18 @@ int fspathncmp(const char *a, const char *b, size_t count) return ignore_case ? strncasecmp(a, b, count) : strncmp(a, b, count); } +int paths_collide(const char *a, const char *b) +{ + size_t len_a = strlen(a), len_b = strlen(b); + + if (len_a == len_b) + return fspatheq(a, b); + + if (len_a < len_b) + return is_dir_sep(b[len_a]) && !fspathncmp(a, b, len_a); + return is_dir_sep(a[len_b]) && !fspathncmp(a, b, len_b); +} + unsigned int fspathhash(const char *str) { return ignore_case ? strihash(str) : strhash(str); @@ -3306,7 +3318,8 @@ static int remove_dir_recurse(struct strbuf *path, int flag, int *kept_up) struct object_id submodule_head; if ((flag & REMOVE_DIR_KEEP_NESTED_GIT) && - !resolve_gitlink_ref(path->buf, "HEAD", &submodule_head)) { + !repo_resolve_gitlink_ref(the_repository, path->buf, + "HEAD", &submodule_head)) { /* Do not descend and nuke a nested git work tree. */ if (kept_up) *kept_up = 1; @@ -549,6 +549,13 @@ int fspathncmp(const char *a, const char *b, size_t count); unsigned int fspathhash(const char *str); /* + * Reports whether paths collide. This may be because the paths differ only in + * case on a case-sensitive filesystem, or that one path refers to a symlink + * that collides with one of the parent directories of the other. + */ +int paths_collide(const char *a, const char *b); + +/* * The prefix part of pattern must not contains wildcards. */ struct pathspec_item; @@ -460,7 +460,7 @@ static void mark_colliding_entries(const struct checkout *state, continue; if ((trust_ino && !match_stat_data(&dup->ce_stat_data, st)) || - (!trust_ino && !fspathcmp(ce->name, dup->name))) { + paths_collide(ce->name, dup->name)) { dup->ce_flags |= CE_MATCHED; break; } @@ -547,6 +547,20 @@ int checkout_entry_ca(struct cache_entry *ce, struct conv_attrs *ca, /* If it is a gitlink, leave it alone! */ if (S_ISGITLINK(ce->ce_mode)) return 0; + /* + * We must avoid replacing submodules' leading + * directories with symbolic links, lest recursive + * clones can write into arbitrary locations. + * + * Technically, this logic is not limited + * to recursive clones, or for that matter to + * submodules' paths colliding with symbolic links' + * paths. Yet it strikes a balance in favor of + * simplicity, and if paths are colliding, we might + * just as well keep the directories during a clone. + */ + if (state->clone && S_ISLNK(ce->ce_mode)) + return 0; remove_subtree(&path); } else if (unlink(path.buf)) return error_errno("unable to unlink old '%s'", path.buf); diff --git a/environment.h b/environment.h index 05fd94d..0b2d457 100644 --- a/environment.h +++ b/environment.h @@ -58,6 +58,13 @@ const char *getenv_safe(struct strvec *argv, const char *name); #define GIT_ATTR_SOURCE_ENVIRONMENT "GIT_ATTR_SOURCE" /* + * Environment variable used to propagate the --no-advice global option to the + * advice_enabled() helper, even when run in a subprocess. + * This is an internal variable that should not be set by the user. + */ +#define GIT_ADVICE_ENVIRONMENT "GIT_ADVICE" + +/* * Environment variable used in handshaking the wire protocol. * Contains a colon ':' separated list of keys with optional values * 'key[=value]'. Presence of unknown keys and values must be diff --git a/fetch-pack.c b/fetch-pack.c index 091f9a8..7d2aef2 100644 --- a/fetch-pack.c +++ b/fetch-pack.c @@ -290,7 +290,8 @@ static void mark_tips(struct fetch_negotiator *negotiator, int i; if (!negotiation_tips) { - for_each_rawref(rev_list_insert_ref_oid, negotiator); + refs_for_each_rawref(get_main_ref_store(the_repository), + rev_list_insert_ref_oid, negotiator); return; } @@ -732,11 +733,6 @@ static void mark_alternate_complete(struct fetch_negotiator *negotiator UNUSED, mark_complete(&obj->oid); } -struct loose_object_iter { - struct oidset *loose_object_set; - struct ref *refs; -}; - /* * Mark recent commits available locally and reachable from a local ref as * COMPLETE. @@ -793,7 +789,8 @@ static void mark_complete_and_common_ref(struct fetch_negotiator *negotiator, */ trace2_region_enter("fetch-pack", "mark_complete_local_refs", NULL); if (!args->deepen) { - for_each_rawref(mark_complete_oid, NULL); + refs_for_each_rawref(get_main_ref_store(the_repository), + mark_complete_oid, NULL); for_each_cached_alternate(NULL, mark_alternate_complete); commit_list_sort_by_date(&complete); if (cutoff) diff --git a/fmt-merge-msg.c b/fmt-merge-msg.c index ae201e2..7d144b8 100644 --- a/fmt-merge-msg.c +++ b/fmt-merge-msg.c @@ -661,7 +661,9 @@ int fmt_merge_msg(struct strbuf *in, struct strbuf *out, /* learn the commit that we merge into and the current branch name */ current_branch = current_branch_to_free = - resolve_refdup("HEAD", RESOLVE_REF_READING, &head_oid, NULL); + refs_resolve_refdup(get_main_ref_store(the_repository), + "HEAD", RESOLVE_REF_READING, &head_oid, + NULL); if (!current_branch) die("No current branch"); @@ -3253,17 +3253,19 @@ class P4Sync(Command, P4UserMap): if self.stream_have_file_info: if "depotFile" in self.stream_file: f = self.stream_file["depotFile"] - # force a failure in fast-import, else an empty - # commit will be made - self.gitStream.write("\n") - self.gitStream.write("die-now\n") - self.gitStream.close() - # ignore errors, but make sure it exits first - self.importProcess.wait() - if f: - die("Error from p4 print for %s: %s" % (f, err)) - else: - die("Error from p4 print: %s" % err) + try: + # force a failure in fast-import, else an empty + # commit will be made + self.gitStream.write("\n") + self.gitStream.write("die-now\n") + self.gitStream.close() + # ignore errors, but make sure it exits first + self.importProcess.wait() + finally: + if f: + die("Error from p4 print for %s: %s" % (f, err)) + else: + die("Error from p4 print: %s" % err) if 'depotFile' in marshalled and self.stream_have_file_info: # start of a new file - output the old one first diff --git a/git-send-email.perl b/git-send-email.perl index 821b2b3..f0be4b4 100755 --- a/git-send-email.perl +++ b/git-send-email.perl @@ -1664,9 +1664,11 @@ EOF $smtp->code =~ /250|200/ or die sprintf(__("Failed to send %s\n"), $subject).$smtp->message; } if ($quiet) { - printf($dry_run ? __("Dry-Sent %s\n") : __("Sent %s\n"), $subject); + printf($dry_run ? __("Dry-Sent %s") : __("Sent %s"), $subject); + print "\n"; } else { - print($dry_run ? __("Dry-OK. Log says:\n") : __("OK. Log says:\n")); + print($dry_run ? __("Dry-OK. Log says:") : __("OK. Log says:")); + print "\n"; if (!defined $sendmail_cmd && !file_name_is_absolute($smtp_server)) { print "Server: $smtp_server\n"; print "MAIL FROM:<$raw_from>\n"; @@ -1686,10 +1688,11 @@ EOF print $header, "\n"; if ($smtp) { print __("Result: "), $smtp->code, ' ', - ($smtp->message =~ /\n([^\n]+\n)$/s), "\n"; + ($smtp->message =~ /\n([^\n]+\n)$/s); } else { - print __("Result: OK\n"); + print __("Result: OK"); } + print "\n"; } return 1; @@ -36,9 +36,10 @@ struct cmd_struct { const char git_usage_string[] = N_("git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]\n" " [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]\n" - " [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]\n" - " [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]\n" - " [--config-env=<name>=<envvar>] <command> [<args>]"); + " [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--no-lazy-fetch]\n" + " [--no-optional-locks] [--no-advice] [--bare] [--git-dir=<path>]\n" + " [--work-tree=<path>] [--namespace=<name>] [--config-env=<name>=<envvar>]\n" + " <command> [<args>]"); const char git_more_info_string[] = N_("'git help -a' and 'git help -g' list available subcommands and some\n" @@ -337,6 +338,10 @@ static int handle_options(const char ***argv, int *argc, int *envchanged) setenv(GIT_ATTR_SOURCE_ENVIRONMENT, cmd, 1); if (envchanged) *envchanged = 1; + } else if (!strcmp(cmd, "--no-advice")) { + setenv(GIT_ADVICE_ENVIRONMENT, "0", 1); + if (envchanged) + *envchanged = 1; } else { fprintf(stderr, _("unknown option: %s\n"), cmd); usage(git_usage_string); @@ -800,7 +800,7 @@ static int append_similar_ref(const char *refname, if (starts_with(refname, "refs/remotes/") && !strcmp(branch, cb->base_ref)) string_list_append_nodup(cb->similar_refs, - shorten_unambiguous_ref(refname, 1)); + refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), refname, 1)); return 0; } @@ -811,7 +811,8 @@ static struct string_list guess_refs(const char *ref) ref_cb.base_ref = ref; ref_cb.similar_refs = &similar_refs; - for_each_ref(append_similar_ref, &ref_cb); + refs_for_each_ref(get_main_ref_store(the_repository), + append_similar_ref, &ref_cb); return similar_refs; } @@ -7,25 +7,31 @@ #include "run-command.h" #include "config.h" #include "strbuf.h" +#include "environment.h" +#include "setup.h" const char *find_hook(const char *name) { static struct strbuf path = STRBUF_INIT; + int found_hook; + strbuf_reset(&path); strbuf_git_path(&path, "hooks/%s", name); - if (access(path.buf, X_OK) < 0) { + found_hook = access(path.buf, X_OK) >= 0; +#ifdef STRIP_EXTENSION + if (!found_hook) { int err = errno; -#ifdef STRIP_EXTENSION strbuf_addstr(&path, STRIP_EXTENSION); - if (access(path.buf, X_OK) >= 0) - return path.buf; - if (errno == EACCES) - err = errno; + found_hook = access(path.buf, X_OK) >= 0; + if (!found_hook) + errno = err; + } #endif - if (err == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) { + if (!found_hook) { + if (errno == EACCES && advice_enabled(ADVICE_IGNORED_HOOK)) { static struct string_list advise_given = STRING_LIST_INIT_DUP; if (!string_list_lookup(&advise_given, name)) { diff --git a/http-backend.c b/http-backend.c index 1ed1e29..5b65287 100644 --- a/http-backend.c +++ b/http-backend.c @@ -559,7 +559,8 @@ static void get_info_refs(struct strbuf *hdr, char *arg UNUSED) } else { select_getanyfile(hdr); - for_each_namespaced_ref(NULL, show_text_ref, &buf); + refs_for_each_namespaced_ref(get_main_ref_store(the_repository), + NULL, show_text_ref, &buf); send_strbuf(hdr, "text/plain", &buf); } strbuf_release(&buf); @@ -571,9 +572,10 @@ static int show_head_ref(const char *refname, const struct object_id *oid, struct strbuf *buf = cb_data; if (flag & REF_ISSYMREF) { - const char *target = resolve_ref_unsafe(refname, - RESOLVE_REF_READING, - NULL, NULL); + const char *target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + refname, + RESOLVE_REF_READING, + NULL, NULL); if (target) strbuf_addf(buf, "ref: %s\n", strip_namespace(target)); @@ -589,7 +591,8 @@ static void get_head(struct strbuf *hdr, char *arg UNUSED) struct strbuf buf = STRBUF_INIT; select_getanyfile(hdr); - head_ref_namespaced(show_head_ref, &buf); + refs_head_ref_namespaced(get_main_ref_store(the_repository), + show_head_ref, &buf); send_strbuf(hdr, "text/plain", &buf); strbuf_release(&buf); } @@ -232,8 +232,10 @@ void load_ref_decorations(struct decoration_filter *filter, int flags) } decoration_loaded = 1; decoration_flags = flags; - for_each_ref(add_ref_decoration, filter); - head_ref(add_ref_decoration, filter); + refs_for_each_ref(get_main_ref_store(the_repository), + add_ref_decoration, filter); + refs_head_ref(get_main_ref_store(the_repository), + add_ref_decoration, filter); for_each_commit_graft(add_graft_decoration, filter); } } @@ -277,7 +279,8 @@ static const struct name_decoration *current_pointed_by_HEAD(const struct name_d return NULL; /* Now resolve and find the matching current branch */ - branch_name = resolve_ref_unsafe("HEAD", 0, NULL, &rru_flags); + branch_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + "HEAD", 0, NULL, &rru_flags); if (!branch_name || !(rru_flags & REF_ISSYMREF)) return NULL; @@ -95,9 +95,11 @@ static int send_ref(const char *refname, const struct object_id *oid, strbuf_addf(&data->buf, "unborn %s", refname_nons); if (data->symrefs && flag & REF_ISSYMREF) { struct object_id unused; - const char *symref_target = resolve_ref_unsafe(refname, 0, - &unused, - &flag); + const char *symref_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + refname, + 0, + &unused, + &flag); if (!symref_target) die("'%s' is a symref but it is not?", refname); @@ -108,7 +110,7 @@ static int send_ref(const char *refname, const struct object_id *oid, if (data->peel && oid) { struct object_id peeled; - if (!peel_iterated_oid(oid, &peeled)) + if (!peel_iterated_oid(the_repository, oid, &peeled)) strbuf_addf(&data->buf, " peeled:%s", oid_to_hex(&peeled)); } @@ -126,7 +128,7 @@ static void send_possibly_unborn_head(struct ls_refs_data *data) int oid_is_null; strbuf_addf(&namespaced, "%sHEAD", get_git_namespace()); - if (!resolve_ref_unsafe(namespaced.buf, 0, &oid, &flag)) + if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository), namespaced.buf, 0, &oid, &flag)) return; /* bad ref */ oid_is_null = is_null_oid(&oid); if (!oid_is_null || diff --git a/mergetools/vimdiff b/mergetools/vimdiff index 734d15a..f8ad6b3 100644 --- a/mergetools/vimdiff +++ b/mergetools/vimdiff @@ -325,7 +325,7 @@ gen_cmd () { fi # If this is a single window diff with all the buffers - if ! echo "$tab" | grep ",\|/" >/dev/null + if ! echo "$tab" | grep -E ",|/" >/dev/null then CMD="$CMD | silent execute 'bufdo diffthis'" fi diff --git a/midx-write.c b/midx-write.c index 65e69d2..4998707 100644 --- a/midx-write.c +++ b/midx-write.c @@ -664,7 +664,7 @@ static int add_ref_to_pending(const char *refname, return 0; } - if (!peel_iterated_oid(oid, &peeled)) + if (!peel_iterated_oid(the_repository, oid, &peeled)) oid = &peeled; object = parse_object_or_die(oid, refname); @@ -755,7 +755,8 @@ static struct commit **find_commits_for_midx_bitmap(uint32_t *indexed_commits_nr read_refs_snapshot(refs_snapshot, &revs); } else { setup_revisions(0, NULL, &revs, NULL); - for_each_ref(add_ref_to_pending, &revs); + refs_for_each_ref(get_main_ref_store(the_repository), + add_ref_to_pending, &revs); } /* @@ -798,6 +799,7 @@ static int write_midx_bitmap(const char *midx_name, { int ret, i; uint16_t options = 0; + struct bitmap_writer writer; struct pack_idx_entry **index; char *bitmap_name = xstrfmt("%s-%s.bitmap", midx_name, hash_to_hex(midx_hash)); @@ -819,8 +821,10 @@ static int write_midx_bitmap(const char *midx_name, for (i = 0; i < pdata->nr_objects; i++) index[i] = &pdata->objects[i].idx; - bitmap_writer_show_progress(flags & MIDX_PROGRESS); - bitmap_writer_build_type_index(pdata, index, pdata->nr_objects); + bitmap_writer_init(&writer); + bitmap_writer_show_progress(&writer, flags & MIDX_PROGRESS); + bitmap_writer_build_type_index(&writer, pdata, index, + pdata->nr_objects); /* * bitmap_writer_finish expects objects in lex order, but pack_order @@ -838,17 +842,19 @@ static int write_midx_bitmap(const char *midx_name, for (i = 0; i < pdata->nr_objects; i++) index[pack_order[i]] = &pdata->objects[i].idx; - bitmap_writer_select_commits(commits, commits_nr, -1); - ret = bitmap_writer_build(pdata); + bitmap_writer_select_commits(&writer, commits, commits_nr); + ret = bitmap_writer_build(&writer, pdata); if (ret < 0) goto cleanup; - bitmap_writer_set_checksum(midx_hash); - bitmap_writer_finish(index, pdata->nr_objects, bitmap_name, options); + bitmap_writer_set_checksum(&writer, midx_hash); + bitmap_writer_finish(&writer, index, pdata->nr_objects, bitmap_name, + options); cleanup: free(index); free(bitmap_name); + bitmap_writer_free(&writer); trace2_region_leave("midx", "write_midx_bitmap", the_repository); diff --git a/negotiator/default.c b/negotiator/default.c index 9a5b696..518b3c4 100644 --- a/negotiator/default.c +++ b/negotiator/default.c @@ -192,6 +192,7 @@ void default_negotiator_init(struct fetch_negotiator *negotiator) ns->rev_list.compare = compare_commits_by_commit_date; if (marked) - for_each_ref(clear_marks, NULL); + refs_for_each_ref(get_main_ref_store(the_repository), + clear_marks, NULL); marked = 1; } diff --git a/negotiator/skipping.c b/negotiator/skipping.c index 5b91520..b7e008c 100644 --- a/negotiator/skipping.c +++ b/negotiator/skipping.c @@ -261,6 +261,7 @@ void skipping_negotiator_init(struct fetch_negotiator *negotiator) data->rev_list.compare = compare; if (marked) - for_each_ref(clear_marks, NULL); + refs_for_each_ref(get_main_ref_store(the_repository), + clear_marks, NULL); marked = 1; } diff --git a/notes-cache.c b/notes-cache.c index 0e1d5b1..038db01 100644 --- a/notes-cache.c +++ b/notes-cache.c @@ -17,7 +17,7 @@ static int notes_cache_match_validity(struct repository *r, struct strbuf msg = STRBUF_INIT; int ret; - if (read_ref(ref, &oid) < 0) + if (refs_read_ref(get_main_ref_store(the_repository), ref, &oid) < 0) return 0; commit = lookup_commit_reference_gently(r, &oid, 1); @@ -66,8 +66,8 @@ int notes_cache_write(struct notes_cache *c) if (commit_tree(c->validity, strlen(c->validity), &tree_oid, NULL, &commit_oid, NULL, NULL) < 0) return -1; - if (update_ref("update notes cache", c->tree.update_ref, &commit_oid, - NULL, 0, UPDATE_REFS_QUIET_ON_ERR) < 0) + if (refs_update_ref(get_main_ref_store(the_repository), "update notes cache", c->tree.update_ref, &commit_oid, + NULL, 0, UPDATE_REFS_QUIET_ON_ERR) < 0) return -1; return 0; diff --git a/notes-merge.c b/notes-merge.c index 5128293..6a9a139 100644 --- a/notes-merge.c +++ b/notes-merge.c @@ -562,7 +562,7 @@ int notes_merge(struct notes_merge_options *o, o->local_ref, o->remote_ref); /* Dereference o->local_ref into local_sha1 */ - if (read_ref_full(o->local_ref, 0, &local_oid, NULL)) + if (refs_read_ref_full(get_main_ref_store(the_repository), o->local_ref, 0, &local_oid, NULL)) die("Failed to resolve local notes ref '%s'", o->local_ref); else if (!check_refname_format(o->local_ref, 0) && is_null_oid(&local_oid)) diff --git a/notes-utils.c b/notes-utils.c index 6197a5a..e33aa86 100644 --- a/notes-utils.c +++ b/notes-utils.c @@ -23,7 +23,7 @@ void create_notes_commit(struct repository *r, if (!parents) { /* Deduce parent commit from t->ref */ struct object_id parent_oid; - if (!read_ref(t->ref, &parent_oid)) { + if (!refs_read_ref(get_main_ref_store(the_repository), t->ref, &parent_oid)) { struct commit *parent = lookup_commit(r, &parent_oid); if (repo_parse_commit(r, parent)) die("Failed to find/parse commit %s", t->ref); @@ -55,8 +55,9 @@ void commit_notes(struct repository *r, struct notes_tree *t, const char *msg) create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid); strbuf_insertstr(&buf, 0, "notes: "); - update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0, - UPDATE_REFS_DIE_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), buf.buf, + t->update_ref, &commit_oid, NULL, 0, + UPDATE_REFS_DIE_ON_ERR); strbuf_release(&buf); } @@ -945,7 +945,8 @@ void string_list_add_refs_by_glob(struct string_list *list, const char *glob) { assert(list->strdup_strings); if (has_glob_specials(glob)) { - for_each_glob_ref(string_list_add_one_ref, glob, list); + refs_for_each_glob_ref(get_main_ref_store(the_repository), + string_list_add_one_ref, glob, list); } else { struct object_id oid; if (repo_get_oid(the_repository, glob, &oid)) @@ -1029,7 +1030,7 @@ void init_notes(struct notes_tree *t, const char *notes_ref, if (flags & NOTES_INIT_EMPTY || repo_get_oid_treeish(the_repository, notes_ref, &object_oid)) return; - if (flags & NOTES_INIT_WRITABLE && read_ref(notes_ref, &object_oid)) + if (flags & NOTES_INIT_WRITABLE && refs_read_ref(get_main_ref_store(the_repository), notes_ref, &object_oid)) die("Cannot use notes ref %s", notes_ref); if (get_tree_entry(the_repository, &object_oid, "", &oid, &mode)) die("Failed to read notes tree referenced by %s (%s)", diff --git a/object-file.c b/object-file.c index 610b1f4..a40300c 100644 --- a/object-file.c +++ b/object-file.c @@ -2669,7 +2669,7 @@ int index_path(struct index_state *istate, struct object_id *oid, strbuf_release(&sb); break; case S_IFDIR: - return resolve_gitlink_ref(path, "HEAD", oid); + return repo_resolve_gitlink_ref(the_repository, path, "HEAD", oid); default: return error(_("%s: unsupported file type"), path); } @@ -207,6 +207,29 @@ struct object *lookup_object_by_type(struct repository *r, } } +enum peel_status peel_object(struct repository *r, + const struct object_id *name, + struct object_id *oid) +{ + struct object *o = lookup_unknown_object(r, name); + + if (o->type == OBJ_NONE) { + int type = oid_object_info(r, name, NULL); + if (type < 0 || !object_as_type(o, type, 0)) + return PEEL_INVALID; + } + + if (o->type != OBJ_TAG) + return PEEL_NON_TAG; + + o = deref_tag_noverify(r, o); + if (!o) + return PEEL_INVALID; + + oidcpy(oid, &o->oid); + return PEEL_PEELED; +} + struct object *parse_object_buffer(struct repository *r, const struct object_id *oid, enum object_type type, unsigned long size, void *buffer, int *eaten_p) { struct object *obj; @@ -81,6 +81,7 @@ void object_array_init(struct object_array *array); * reflog.c: 10--12 * builtin/show-branch.c: 0-------------------------------------------26 * builtin/unpack-objects.c: 2021 + * pack-bitmap.h: 22 */ #define FLAG_BITS 28 @@ -256,6 +257,41 @@ struct object *lookup_unknown_object(struct repository *r, const struct object_i struct object *lookup_object_by_type(struct repository *r, const struct object_id *oid, enum object_type type); +enum peel_status { + /* object was peeled successfully: */ + PEEL_PEELED = 0, + + /* + * object cannot be peeled because the named object (or an + * object referred to by a tag in the peel chain), does not + * exist. + */ + PEEL_INVALID = -1, + + /* object cannot be peeled because it is not a tag: */ + PEEL_NON_TAG = -2, + + /* ref_entry contains no peeled value because it is a symref: */ + PEEL_IS_SYMREF = -3, + + /* + * ref_entry cannot be peeled because it is broken (i.e., the + * symbolic reference cannot even be resolved to an object + * name): + */ + PEEL_BROKEN = -4 +}; + +/* + * Peel the named object; i.e., if the object is a tag, resolve the + * tag recursively until a non-tag is found. If successful, store the + * result to oid and return PEEL_PEELED. If the object is not a tag + * or is not valid, return PEEL_NON_TAG or PEEL_INVALID, respectively, + * and leave oid unchanged. + */ +enum peel_status peel_object(struct repository *r, + const struct object_id *name, struct object_id *oid); + struct object_list *object_list_insert(struct object *item, struct object_list **list_p); diff --git a/oss-fuzz/fuzz-commit-graph.c b/oss-fuzz/fuzz-commit-graph.c index fe15e2c..75e668a 100644 --- a/oss-fuzz/fuzz-commit-graph.c +++ b/oss-fuzz/fuzz-commit-graph.c @@ -19,6 +19,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) * touching the disk to keep the individual fuzz-test cases as fast as * possible. */ + repo_set_hash_algo(the_repository, GIT_HASH_SHA1); the_repository->settings.commit_graph_generation_version = 2; the_repository->settings.commit_graph_read_changed_paths = 1; g = parse_commit_graph(&the_repository->settings, (void *)data, size); diff --git a/pack-bitmap-write.c b/pack-bitmap-write.c index c6c8f94..6cae670 100644 --- a/pack-bitmap-write.c +++ b/pack-bitmap-write.c @@ -27,43 +27,53 @@ struct bitmapped_commit { uint32_t commit_pos; }; -struct bitmap_writer { - struct ewah_bitmap *commits; - struct ewah_bitmap *trees; - struct ewah_bitmap *blobs; - struct ewah_bitmap *tags; +void bitmap_writer_init(struct bitmap_writer *writer) +{ + memset(writer, 0, sizeof(struct bitmap_writer)); +} + +void bitmap_writer_free(struct bitmap_writer *writer) +{ + uint32_t i; - kh_oid_map_t *bitmaps; - struct packing_data *to_pack; + if (!writer) + return; - struct bitmapped_commit *selected; - unsigned int selected_nr, selected_alloc; + ewah_free(writer->commits); + ewah_free(writer->trees); + ewah_free(writer->blobs); + ewah_free(writer->tags); - struct progress *progress; - int show_progress; - unsigned char pack_checksum[GIT_MAX_RAWSZ]; -}; + kh_destroy_oid_map(writer->bitmaps); -static struct bitmap_writer writer; + for (i = 0; i < writer->selected_nr; i++) { + struct bitmapped_commit *bc = &writer->selected[i]; + if (bc->write_as != bc->bitmap) + ewah_free(bc->write_as); + ewah_free(bc->bitmap); + } + free(writer->selected); +} -void bitmap_writer_show_progress(int show) +void bitmap_writer_show_progress(struct bitmap_writer *writer, int show) { - writer.show_progress = show; + writer->show_progress = show; } /** * Build the initial type index for the packfile or multi-pack-index */ -void bitmap_writer_build_type_index(struct packing_data *to_pack, +void bitmap_writer_build_type_index(struct bitmap_writer *writer, + struct packing_data *to_pack, struct pack_idx_entry **index, uint32_t index_nr) { uint32_t i; - writer.commits = ewah_new(); - writer.trees = ewah_new(); - writer.blobs = ewah_new(); - writer.tags = ewah_new(); + writer->commits = ewah_new(); + writer->trees = ewah_new(); + writer->blobs = ewah_new(); + writer->tags = ewah_new(); ALLOC_ARRAY(to_pack->in_pack_pos, to_pack->nr_objects); for (i = 0; i < index_nr; ++i) { @@ -88,19 +98,19 @@ void bitmap_writer_build_type_index(struct packing_data *to_pack, switch (real_type) { case OBJ_COMMIT: - ewah_set(writer.commits, i); + ewah_set(writer->commits, i); break; case OBJ_TREE: - ewah_set(writer.trees, i); + ewah_set(writer->trees, i); break; case OBJ_BLOB: - ewah_set(writer.blobs, i); + ewah_set(writer->blobs, i); break; case OBJ_TAG: - ewah_set(writer.tags, i); + ewah_set(writer->tags, i); break; default: @@ -115,23 +125,26 @@ void bitmap_writer_build_type_index(struct packing_data *to_pack, * Compute the actual bitmaps */ -static inline void push_bitmapped_commit(struct commit *commit) +static inline void push_bitmapped_commit(struct bitmap_writer *writer, + struct commit *commit) { - if (writer.selected_nr >= writer.selected_alloc) { - writer.selected_alloc = (writer.selected_alloc + 32) * 2; - REALLOC_ARRAY(writer.selected, writer.selected_alloc); + if (writer->selected_nr >= writer->selected_alloc) { + writer->selected_alloc = (writer->selected_alloc + 32) * 2; + REALLOC_ARRAY(writer->selected, writer->selected_alloc); } - writer.selected[writer.selected_nr].commit = commit; - writer.selected[writer.selected_nr].bitmap = NULL; - writer.selected[writer.selected_nr].flags = 0; + writer->selected[writer->selected_nr].commit = commit; + writer->selected[writer->selected_nr].bitmap = NULL; + writer->selected[writer->selected_nr].write_as = NULL; + writer->selected[writer->selected_nr].flags = 0; - writer.selected_nr++; + writer->selected_nr++; } -static uint32_t find_object_pos(const struct object_id *oid, int *found) +static uint32_t find_object_pos(struct bitmap_writer *writer, + const struct object_id *oid, int *found) { - struct object_entry *entry = packlist_find(writer.to_pack, oid); + struct object_entry *entry = packlist_find(writer->to_pack, oid); if (!entry) { if (found) @@ -143,17 +156,17 @@ static uint32_t find_object_pos(const struct object_id *oid, int *found) if (found) *found = 1; - return oe_in_pack_pos(writer.to_pack, entry); + return oe_in_pack_pos(writer->to_pack, entry); } -static void compute_xor_offsets(void) +static void compute_xor_offsets(struct bitmap_writer *writer) { static const int MAX_XOR_OFFSET_SEARCH = 10; int i, next = 0; - while (next < writer.selected_nr) { - struct bitmapped_commit *stored = &writer.selected[next]; + while (next < writer->selected_nr) { + struct bitmapped_commit *stored = &writer->selected[next]; int best_offset = 0; struct ewah_bitmap *best_bitmap = stored->bitmap; @@ -166,7 +179,7 @@ static void compute_xor_offsets(void) break; test_xor = ewah_pool_new(); - ewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor); + ewah_xor(writer->selected[curr].bitmap, stored->bitmap, test_xor); if (test_xor->buffer_size < best_bitmap->buffer_size) { if (best_bitmap != stored->bitmap) @@ -348,7 +361,8 @@ static void bitmap_builder_clear(struct bitmap_builder *bb) bb->commits_nr = bb->commits_alloc = 0; } -static int fill_bitmap_tree(struct bitmap *bitmap, +static int fill_bitmap_tree(struct bitmap_writer *writer, + struct bitmap *bitmap, struct tree *tree) { int found; @@ -360,7 +374,7 @@ static int fill_bitmap_tree(struct bitmap *bitmap, * If our bit is already set, then there is nothing to do. Both this * tree and all of its children will be set. */ - pos = find_object_pos(&tree->object.oid, &found); + pos = find_object_pos(writer, &tree->object.oid, &found); if (!found) return -1; if (bitmap_get(bitmap, pos)) @@ -375,12 +389,12 @@ static int fill_bitmap_tree(struct bitmap *bitmap, while (tree_entry(&desc, &entry)) { switch (object_type(entry.mode)) { case OBJ_TREE: - if (fill_bitmap_tree(bitmap, + if (fill_bitmap_tree(writer, bitmap, lookup_tree(the_repository, &entry.oid)) < 0) return -1; break; case OBJ_BLOB: - pos = find_object_pos(&entry.oid, &found); + pos = find_object_pos(writer, &entry.oid, &found); if (!found) return -1; bitmap_set(bitmap, pos); @@ -397,7 +411,8 @@ static int fill_bitmap_tree(struct bitmap *bitmap, static int reused_bitmaps_nr; -static int fill_bitmap_commit(struct bb_commit *ent, +static int fill_bitmap_commit(struct bitmap_writer *writer, + struct bb_commit *ent, struct commit *commit, struct prio_queue *queue, struct prio_queue *tree_queue, @@ -436,7 +451,7 @@ static int fill_bitmap_commit(struct bb_commit *ent, * Mark ourselves and queue our tree. The commit * walk ensures we cover all parents. */ - pos = find_object_pos(&c->object.oid, &found); + pos = find_object_pos(writer, &c->object.oid, &found); if (!found) return -1; bitmap_set(ent->bitmap, pos); @@ -444,7 +459,8 @@ static int fill_bitmap_commit(struct bb_commit *ent, repo_get_commit_tree(the_repository, c)); for (p = c->parents; p; p = p->next) { - pos = find_object_pos(&p->item->object.oid, &found); + pos = find_object_pos(writer, &p->item->object.oid, + &found); if (!found) return -1; if (!bitmap_get(ent->bitmap, pos)) { @@ -455,29 +471,31 @@ static int fill_bitmap_commit(struct bb_commit *ent, } while (tree_queue->nr) { - if (fill_bitmap_tree(ent->bitmap, + if (fill_bitmap_tree(writer, ent->bitmap, prio_queue_get(tree_queue)) < 0) return -1; } return 0; } -static void store_selected(struct bb_commit *ent, struct commit *commit) +static void store_selected(struct bitmap_writer *writer, + struct bb_commit *ent, struct commit *commit) { - struct bitmapped_commit *stored = &writer.selected[ent->idx]; + struct bitmapped_commit *stored = &writer->selected[ent->idx]; khiter_t hash_pos; int hash_ret; stored->bitmap = bitmap_to_ewah(ent->bitmap); - hash_pos = kh_put_oid_map(writer.bitmaps, commit->object.oid, &hash_ret); + hash_pos = kh_put_oid_map(writer->bitmaps, commit->object.oid, &hash_ret); if (hash_ret == 0) die("Duplicate entry when writing index: %s", oid_to_hex(&commit->object.oid)); - kh_value(writer.bitmaps, hash_pos) = stored; + kh_value(writer->bitmaps, hash_pos) = stored; } -int bitmap_writer_build(struct packing_data *to_pack) +int bitmap_writer_build(struct bitmap_writer *writer, + struct packing_data *to_pack) { struct bitmap_builder bb; size_t i; @@ -488,11 +506,12 @@ int bitmap_writer_build(struct packing_data *to_pack) uint32_t *mapping; int closed = 1; /* until proven otherwise */ - writer.bitmaps = kh_init_oid_map(); - writer.to_pack = to_pack; + writer->bitmaps = kh_init_oid_map(); + writer->to_pack = to_pack; - if (writer.show_progress) - writer.progress = start_progress("Building bitmaps", writer.selected_nr); + if (writer->show_progress) + writer->progress = start_progress("Building bitmaps", + writer->selected_nr); trace2_region_enter("pack-bitmap-write", "building_bitmaps_total", the_repository); @@ -502,23 +521,23 @@ int bitmap_writer_build(struct packing_data *to_pack) else mapping = NULL; - bitmap_builder_init(&bb, &writer, old_bitmap); + bitmap_builder_init(&bb, writer, old_bitmap); for (i = bb.commits_nr; i > 0; i--) { struct commit *commit = bb.commits[i-1]; struct bb_commit *ent = bb_data_at(&bb.data, commit); struct commit *child; int reused = 0; - if (fill_bitmap_commit(ent, commit, &queue, &tree_queue, + if (fill_bitmap_commit(writer, ent, commit, &queue, &tree_queue, old_bitmap, mapping) < 0) { closed = 0; break; } if (ent->selected) { - store_selected(ent, commit); + store_selected(writer, ent, commit); nr_stored++; - display_progress(writer.progress, nr_stored); + display_progress(writer->progress, nr_stored); } while ((child = pop_commit(&ent->reverse_edges))) { @@ -549,10 +568,10 @@ int bitmap_writer_build(struct packing_data *to_pack) trace2_data_intmax("pack-bitmap-write", the_repository, "building_bitmaps_reused", reused_bitmaps_nr); - stop_progress(&writer.progress); + stop_progress(&writer->progress); if (closed) - compute_xor_offsets(); + compute_xor_offsets(writer); return closed ? 0 : -1; } @@ -590,9 +609,9 @@ static int date_compare(const void *_a, const void *_b) return (long)b->date - (long)a->date; } -void bitmap_writer_select_commits(struct commit **indexed_commits, - unsigned int indexed_commits_nr, - int max_bitmaps) +void bitmap_writer_select_commits(struct bitmap_writer *writer, + struct commit **indexed_commits, + unsigned int indexed_commits_nr) { unsigned int i = 0, j, next; @@ -600,12 +619,12 @@ void bitmap_writer_select_commits(struct commit **indexed_commits, if (indexed_commits_nr < 100) { for (i = 0; i < indexed_commits_nr; ++i) - push_bitmapped_commit(indexed_commits[i]); + push_bitmapped_commit(writer, indexed_commits[i]); return; } - if (writer.show_progress) - writer.progress = start_progress("Selecting bitmap commits", 0); + if (writer->show_progress) + writer->progress = start_progress("Selecting bitmap commits", 0); for (;;) { struct commit *chosen = NULL; @@ -615,11 +634,6 @@ void bitmap_writer_select_commits(struct commit **indexed_commits, if (i + next >= indexed_commits_nr) break; - if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) { - writer.selected_nr = max_bitmaps; - break; - } - if (next == 0) { chosen = indexed_commits[i]; } else { @@ -638,13 +652,13 @@ void bitmap_writer_select_commits(struct commit **indexed_commits, } } - push_bitmapped_commit(chosen); + push_bitmapped_commit(writer, chosen); i += next + 1; - display_progress(writer.progress, i); + display_progress(writer->progress, i); } - stop_progress(&writer.progress); + stop_progress(&writer->progress); } @@ -670,19 +684,18 @@ static const struct object_id *oid_access(size_t pos, const void *table) return &index[pos]->oid; } -static void write_selected_commits_v1(struct hashfile *f, - uint32_t *commit_positions, - off_t *offsets) +static void write_selected_commits_v1(struct bitmap_writer *writer, + struct hashfile *f, off_t *offsets) { int i; - for (i = 0; i < writer.selected_nr; ++i) { - struct bitmapped_commit *stored = &writer.selected[i]; + for (i = 0; i < writer->selected_nr; ++i) { + struct bitmapped_commit *stored = &writer->selected[i]; if (offsets) offsets[i] = hashfile_total(f); - hashwrite_be32(f, commit_positions[i]); + hashwrite_be32(f, stored->commit_pos); hashwrite_u8(f, stored->xor_offset); hashwrite_u8(f, stored->flags); @@ -692,29 +705,28 @@ static void write_selected_commits_v1(struct hashfile *f, static int table_cmp(const void *_va, const void *_vb, void *_data) { - uint32_t *commit_positions = _data; - uint32_t a = commit_positions[*(uint32_t *)_va]; - uint32_t b = commit_positions[*(uint32_t *)_vb]; + struct bitmap_writer *writer = _data; + struct bitmapped_commit *a = &writer->selected[*(uint32_t *)_va]; + struct bitmapped_commit *b = &writer->selected[*(uint32_t *)_vb]; - if (a > b) - return 1; - else if (a < b) + if (a->commit_pos < b->commit_pos) return -1; + else if (a->commit_pos > b->commit_pos) + return 1; return 0; } -static void write_lookup_table(struct hashfile *f, - uint32_t *commit_positions, +static void write_lookup_table(struct bitmap_writer *writer, struct hashfile *f, off_t *offsets) { uint32_t i; uint32_t *table, *table_inv; - ALLOC_ARRAY(table, writer.selected_nr); - ALLOC_ARRAY(table_inv, writer.selected_nr); + ALLOC_ARRAY(table, writer->selected_nr); + ALLOC_ARRAY(table_inv, writer->selected_nr); - for (i = 0; i < writer.selected_nr; i++) + for (i = 0; i < writer->selected_nr; i++) table[i] = i; /* @@ -722,17 +734,17 @@ static void write_lookup_table(struct hashfile *f, * bitmap corresponds to j'th bitmapped commit (among the selected * commits) in lex order of OIDs. */ - QSORT_S(table, writer.selected_nr, table_cmp, commit_positions); + QSORT_S(table, writer->selected_nr, table_cmp, writer); /* table_inv helps us discover that relationship (i'th bitmap * to j'th commit by j = table_inv[i]) */ - for (i = 0; i < writer.selected_nr; i++) + for (i = 0; i < writer->selected_nr; i++) table_inv[table[i]] = i; trace2_region_enter("pack-bitmap-write", "writing_lookup_table", the_repository); - for (i = 0; i < writer.selected_nr; i++) { - struct bitmapped_commit *selected = &writer.selected[table[i]]; + for (i = 0; i < writer->selected_nr; i++) { + struct bitmapped_commit *selected = &writer->selected[table[i]]; uint32_t xor_offset = selected->xor_offset; uint32_t xor_row; @@ -753,7 +765,7 @@ static void write_lookup_table(struct hashfile *f, xor_row = 0xffffffff; } - hashwrite_be32(f, commit_positions[table[i]]); + hashwrite_be32(f, writer->selected[table[i]].commit_pos); hashwrite_be64(f, (uint64_t)offsets[table[i]]); hashwrite_be32(f, xor_row); } @@ -775,12 +787,14 @@ static void write_hash_cache(struct hashfile *f, } } -void bitmap_writer_set_checksum(const unsigned char *sha1) +void bitmap_writer_set_checksum(struct bitmap_writer *writer, + const unsigned char *sha1) { - hashcpy(writer.pack_checksum, sha1); + hashcpy(writer->pack_checksum, sha1); } -void bitmap_writer_finish(struct pack_idx_entry **index, +void bitmap_writer_finish(struct bitmap_writer *writer, + struct pack_idx_entry **index, uint32_t index_nr, const char *filename, uint16_t options) @@ -789,7 +803,6 @@ void bitmap_writer_finish(struct pack_idx_entry **index, static uint16_t flags = BITMAP_OPT_FULL_DAG; struct strbuf tmp_file = STRBUF_INIT; struct hashfile *f; - uint32_t *commit_positions = NULL; off_t *offsets = NULL; uint32_t i; @@ -802,34 +815,32 @@ void bitmap_writer_finish(struct pack_idx_entry **index, memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE)); header.version = htons(default_version); header.options = htons(flags | options); - header.entry_count = htonl(writer.selected_nr); - hashcpy(header.checksum, writer.pack_checksum); + header.entry_count = htonl(writer->selected_nr); + hashcpy(header.checksum, writer->pack_checksum); hashwrite(f, &header, sizeof(header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz); - dump_bitmap(f, writer.commits); - dump_bitmap(f, writer.trees); - dump_bitmap(f, writer.blobs); - dump_bitmap(f, writer.tags); + dump_bitmap(f, writer->commits); + dump_bitmap(f, writer->trees); + dump_bitmap(f, writer->blobs); + dump_bitmap(f, writer->tags); if (options & BITMAP_OPT_LOOKUP_TABLE) CALLOC_ARRAY(offsets, index_nr); - ALLOC_ARRAY(commit_positions, writer.selected_nr); - - for (i = 0; i < writer.selected_nr; i++) { - struct bitmapped_commit *stored = &writer.selected[i]; - int commit_pos = oid_pos(&stored->commit->object.oid, index, index_nr, oid_access); + for (i = 0; i < writer->selected_nr; i++) { + struct bitmapped_commit *stored = &writer->selected[i]; + int commit_pos = oid_pos(&stored->commit->object.oid, index, + index_nr, oid_access); if (commit_pos < 0) BUG(_("trying to write commit not in index")); - - commit_positions[i] = commit_pos; + stored->commit_pos = commit_pos; } - write_selected_commits_v1(f, commit_positions, offsets); + write_selected_commits_v1(writer, f, offsets); if (options & BITMAP_OPT_LOOKUP_TABLE) - write_lookup_table(f, commit_positions, offsets); + write_lookup_table(writer, f, offsets); if (options & BITMAP_OPT_HASH_CACHE) write_hash_cache(f, index, index_nr); @@ -844,6 +855,5 @@ void bitmap_writer_finish(struct pack_idx_entry **index, die_errno("unable to rename temporary bitmap file to '%s'", filename); strbuf_release(&tmp_file); - free(commit_positions); free(offsets); } diff --git a/pack-bitmap.h b/pack-bitmap.h index c7dea13..3091095 100644 --- a/pack-bitmap.h +++ b/pack-bitmap.h @@ -97,9 +97,29 @@ int bitmap_has_oid_in_uninteresting(struct bitmap_index *, const struct object_i off_t get_disk_usage_from_bitmap(struct bitmap_index *, struct rev_info *); -void bitmap_writer_show_progress(int show); -void bitmap_writer_set_checksum(const unsigned char *sha1); -void bitmap_writer_build_type_index(struct packing_data *to_pack, +struct bitmap_writer { + struct ewah_bitmap *commits; + struct ewah_bitmap *trees; + struct ewah_bitmap *blobs; + struct ewah_bitmap *tags; + + kh_oid_map_t *bitmaps; + struct packing_data *to_pack; + + struct bitmapped_commit *selected; + unsigned int selected_nr, selected_alloc; + + struct progress *progress; + int show_progress; + unsigned char pack_checksum[GIT_MAX_RAWSZ]; +}; + +void bitmap_writer_init(struct bitmap_writer *writer); +void bitmap_writer_show_progress(struct bitmap_writer *writer, int show); +void bitmap_writer_set_checksum(struct bitmap_writer *writer, + const unsigned char *sha1); +void bitmap_writer_build_type_index(struct bitmap_writer *writer, + struct packing_data *to_pack, struct pack_idx_entry **index, uint32_t index_nr); uint32_t *create_bitmap_mapping(struct bitmap_index *bitmap_git, @@ -109,13 +129,17 @@ int rebuild_bitmap(const uint32_t *reposition, struct bitmap *dest); struct ewah_bitmap *bitmap_for_commit(struct bitmap_index *bitmap_git, struct commit *commit); -void bitmap_writer_select_commits(struct commit **indexed_commits, - unsigned int indexed_commits_nr, int max_bitmaps); -int bitmap_writer_build(struct packing_data *to_pack); -void bitmap_writer_finish(struct pack_idx_entry **index, +void bitmap_writer_select_commits(struct bitmap_writer *writer, + struct commit **indexed_commits, + unsigned int indexed_commits_nr); +int bitmap_writer_build(struct bitmap_writer *writer, + struct packing_data *to_pack); +void bitmap_writer_finish(struct bitmap_writer *writer, + struct pack_idx_entry **index, uint32_t index_nr, const char *filename, uint16_t options); +void bitmap_writer_free(struct bitmap_writer *writer); char *midx_bitmap_filename(struct multi_pack_index *midx); char *pack_bitmap_filename(struct packed_git *p); diff --git a/parse-options-cb.c b/parse-options-cb.c index bdc7fae..d99d688 100644 --- a/parse-options-cb.c +++ b/parse-options-cb.c @@ -7,6 +7,7 @@ #include "environment.h" #include "gettext.h" #include "object-name.h" +#include "setup.h" #include "string-list.h" #include "strvec.h" #include "oid-array.h" @@ -29,7 +30,7 @@ int parse_opt_abbrev_cb(const struct option *opt, const char *arg, int unset) opt->long_name); if (v && v < MINIMUM_ABBREV) v = MINIMUM_ABBREV; - else if (v > the_hash_algo->hexsz) + else if (startup_info->have_repository && v > the_hash_algo->hexsz) v = the_hash_algo->hexsz; } *(int *)(opt->value) = v; @@ -5,7 +5,6 @@ #include "abspath.h" #include "environment.h" #include "gettext.h" -#include "hex.h" #include "repository.h" #include "strbuf.h" #include "string-list.h" @@ -647,58 +646,6 @@ void strbuf_git_common_path(struct strbuf *sb, va_end(args); } -int validate_headref(const char *path) -{ - struct stat st; - char buffer[256]; - const char *refname; - struct object_id oid; - int fd; - ssize_t len; - - if (lstat(path, &st) < 0) - return -1; - - /* Make sure it is a "refs/.." symlink */ - if (S_ISLNK(st.st_mode)) { - len = readlink(path, buffer, sizeof(buffer)-1); - if (len >= 5 && !memcmp("refs/", buffer, 5)) - return 0; - return -1; - } - - /* - * Anything else, just open it and try to see if it is a symbolic ref. - */ - fd = open(path, O_RDONLY); - if (fd < 0) - return -1; - len = read_in_full(fd, buffer, sizeof(buffer)-1); - close(fd); - - if (len < 0) - return -1; - buffer[len] = '\0'; - - /* - * Is it a symbolic ref? - */ - if (skip_prefix(buffer, "ref:", &refname)) { - while (isspace(*refname)) - refname++; - if (starts_with(refname, "refs/")) - return 0; - } - - /* - * Is this a detached HEAD? - */ - if (!get_oid_hex(buffer, &oid)) - return 0; - - return -1; -} - static struct passwd *getpw_str(const char *username, size_t len) { struct passwd *pw; @@ -829,6 +776,7 @@ const char *enter_repo(const char *path, int strict) if (!suffix[i]) return NULL; gitfile = read_gitfile(used_path.buf); + die_upon_dubious_ownership(gitfile, NULL, used_path.buf); if (gitfile) { strbuf_reset(&used_path); strbuf_addstr(&used_path, gitfile); @@ -839,6 +787,7 @@ const char *enter_repo(const char *path, int strict) } else { const char *gitfile = read_gitfile(path); + die_upon_dubious_ownership(gitfile, NULL, path); if (gitfile) path = gitfile; if (chdir(path)) @@ -173,7 +173,6 @@ const char *git_path_fetch_head(struct repository *r); const char *git_path_shallow(struct repository *r); int ends_with_path_components(const char *path, const char *components); -int validate_headref(const char *ref); int calc_shared_perm(int mode); int adjust_shared_perm(const char *path); diff --git a/promisor-remote.c b/promisor-remote.c index ac3aa1e..b414922 100644 --- a/promisor-remote.c +++ b/promisor-remote.c @@ -8,6 +8,7 @@ #include "transport.h" #include "strvec.h" #include "packfile.h" +#include "environment.h" struct promisor_remote_config { struct promisor_remote *promisors; @@ -23,6 +24,15 @@ static int fetch_objects(struct repository *repo, int i; FILE *child_in; + if (git_env_bool(NO_LAZY_FETCH_ENVIRONMENT, 0)) { + static int warning_shown; + if (!warning_shown) { + warning_shown = 1; + warning(_("lazy fetching disabled; some objects may not be available")); + } + return -1; + } + child.git_cmd = 1; child.in = -1; if (repo != the_repository) diff --git a/range-diff.h b/range-diff.h index 04ffe21..2f69f6a 100644 --- a/range-diff.h +++ b/range-diff.h @@ -6,6 +6,12 @@ #define RANGE_DIFF_CREATION_FACTOR_DEFAULT 60 +/* + * A much higher value than the default, when we KNOW we are comparing + * the same series (e.g., used when format-patch calls range-diff). + */ +#define CREATION_FACTOR_FOR_THE_SAME_SERIES 999 + struct range_diff_options { int creation_factor; unsigned dual_color:1; diff --git a/reachable.c b/reachable.c index 3b85add..1224b30 100644 --- a/reachable.c +++ b/reachable.c @@ -363,10 +363,11 @@ void mark_reachable_objects(struct rev_info *revs, int mark_reflog, add_index_objects_to_pending(revs, 0); /* Add all external refs */ - for_each_ref(add_one_ref, revs); + refs_for_each_ref(get_main_ref_store(the_repository), add_one_ref, + revs); /* detached HEAD is not included in the list above */ - head_ref(add_one_ref, revs); + refs_head_ref(get_main_ref_store(the_repository), add_one_ref, revs); other_head_refs(add_one_ref, revs); /* rebase autostash and orig-head */ diff --git a/read-cache.c b/read-cache.c index e1723ad..447109a 100644 --- a/read-cache.c +++ b/read-cache.c @@ -271,7 +271,8 @@ static int ce_compare_gitlink(const struct cache_entry *ce) * * If so, we consider it always to match. */ - if (resolve_gitlink_ref(ce->name, "HEAD", &oid) < 0) + if (repo_resolve_gitlink_ref(the_repository, ce->name, + "HEAD", &oid) < 0) return 0; return !oideq(&oid, &ce->oid); } @@ -711,7 +712,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st, namelen = strlen(path); if (S_ISDIR(st_mode)) { - if (resolve_gitlink_ref(path, "HEAD", &oid) < 0) + if (repo_resolve_gitlink_ref(the_repository, path, "HEAD", &oid) < 0) return error(_("'%s' does not have a commit checked out"), path); while (namelen && path[namelen-1] == '/') namelen--; @@ -1116,19 +1117,32 @@ static int has_dir_name(struct index_state *istate, istate->cache[istate->cache_nr - 1]->name, &len_eq_last); if (cmp_last > 0) { - if (len_eq_last == 0) { + if (name[len_eq_last] != '/') { /* * The entry sorts AFTER the last one in the - * index and their paths have no common prefix, - * so there cannot be a F/D conflict. + * index. + * + * If there were a conflict with "file", then our + * name would start with "file/" and the last index + * entry would start with "file" but not "file/". + * + * The next character after common prefix is + * not '/', so there can be no conflict. */ return retval; } else { /* * The entry sorts AFTER the last one in the - * index, but has a common prefix. Fall through - * to the loop below to disect the entry's path - * and see where the difference is. + * index, and the next character after common + * prefix is '/'. + * + * Either the last index entry is a file in + * conflict with this entry, or it has a name + * which sorts between this entry and the + * potential conflicting file. + * + * In both cases, we fall through to the loop + * below and let the regular search code handle it. */ } } else if (cmp_last == 0) { @@ -1152,53 +1166,6 @@ static int has_dir_name(struct index_state *istate, } len = slash - name; - if (cmp_last > 0) { - /* - * (len + 1) is a directory boundary (including - * the trailing slash). And since the loop is - * decrementing "slash", the first iteration is - * the longest directory prefix; subsequent - * iterations consider parent directories. - */ - - if (len + 1 <= len_eq_last) { - /* - * The directory prefix (including the trailing - * slash) also appears as a prefix in the last - * entry, so the remainder cannot collide (because - * strcmp said the whole path was greater). - * - * EQ: last: xxx/A - * this: xxx/B - * - * LT: last: xxx/file_A - * this: xxx/file_B - */ - return retval; - } - - if (len > len_eq_last) { - /* - * This part of the directory prefix (excluding - * the trailing slash) is longer than the known - * equal portions, so this sub-directory cannot - * collide with a file. - * - * GT: last: xxxA - * this: xxxB/file - */ - return retval; - } - - /* - * This is a possible collision. Fall through and - * let the regular search code handle it. - * - * last: xxx - * this: xxx/file - */ - } - pos = index_name_stage_pos(istate, name, len, stage, EXPAND_SPARSE); if (pos >= 0) { /* diff --git a/ref-filter.c b/ref-filter.c index 59ad6f5..f7fb0c7 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -895,7 +895,9 @@ static int head_atom_parser(struct ref_format *format UNUSED, { if (arg) return err_no_arg(err, "HEAD"); - atom->u.head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL); + atom->u.head = refs_resolve_refdup(get_main_ref_store(the_repository), + "HEAD", RESOLVE_REF_READING, NULL, + NULL); return 0; } @@ -2135,7 +2137,9 @@ static const char *rstrip_ref_components(const char *refname, int len) static const char *show_ref(struct refname_atom *atom, const char *refname) { if (atom->option == R_SHORT) - return shorten_unambiguous_ref(refname, warn_ambiguous_refs); + return refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), + refname, + warn_ambiguous_refs); else if (atom->option == R_LSTRIP) return lstrip_ref_components(refname, atom->lstrip); else if (atom->option == R_RSTRIP) @@ -2338,8 +2342,10 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) CALLOC_ARRAY(ref->value, used_atom_cnt); if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) { - ref->symref = resolve_refdup(ref->refname, RESOLVE_REF_READING, - NULL, NULL); + ref->symref = refs_resolve_refdup(get_main_ref_store(the_repository), + ref->refname, + RESOLVE_REF_READING, + NULL, NULL); if (!ref->symref) ref->symref = xstrdup(""); } @@ -2514,7 +2520,7 @@ static int populate_value(struct ref_array_item *ref, struct strbuf *err) * If it is a tag object, see if we use the peeled value. If we do, * grab the peeled OID. */ - if (need_tagged && peel_iterated_oid(&obj->oid, &oi_deref.oid)) + if (need_tagged && peel_iterated_oid(the_repository, &obj->oid, &oi_deref.oid)) die("bad tag"); return get_object(ref, 1, &obj, &oi_deref, err); @@ -2628,7 +2634,7 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter, each_ref_fn cb, void *cb_data) { - if (filter->kind == FILTER_REFS_KIND_MASK) { + if (filter->kind & FILTER_REFS_ROOT_REFS) { /* In this case, we want to print all refs including root refs. */ return refs_for_each_include_root_refs(get_main_ref_store(the_repository), cb, cb_data); @@ -2640,7 +2646,8 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter, * prefixes like "refs/heads/" etc. are stripped off, * so we have to look at everything: */ - return for_each_fullref_in("", cb, cb_data); + return refs_for_each_fullref_in(get_main_ref_store(the_repository), + "", NULL, cb, cb_data); } if (filter->ignore_case) { @@ -2649,7 +2656,8 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter, * so just return everything and let the caller * sort it out. */ - return for_each_fullref_in("", cb, cb_data); + return refs_for_each_fullref_in(get_main_ref_store(the_repository), + "", NULL, cb, cb_data); } if (!filter->name_patterns[0]) { @@ -2756,8 +2764,10 @@ static int ref_kind_from_refname(const char *refname) return ref_kind[i].kind; } - if (is_pseudoref(get_main_ref_store(the_repository), refname)) + if (is_pseudo_ref(refname)) return FILTER_REFS_PSEUDOREFS; + if (is_root_ref(refname)) + return FILTER_REFS_ROOT_REFS; return FILTER_REFS_OTHERS; } @@ -2794,11 +2804,11 @@ static struct ref_array_item *apply_ref_filter(const char *refname, const struct /* * Generally HEAD refs are printed with special description denoting a rebase, * detached state and so forth. This is useful when only printing the HEAD ref - * But when it is being printed along with other pseudorefs, it makes sense to - * keep the formatting consistent. So we mask the type to act like a pseudoref. + * But when it is being printed along with other root refs, it makes sense to + * keep the formatting consistent. So we mask the type to act like a root ref. */ - if (filter->kind == FILTER_REFS_KIND_MASK && kind == FILTER_REFS_DETACHED_HEAD) - kind = FILTER_REFS_PSEUDOREFS; + if (filter->kind & FILTER_REFS_ROOT_REFS && kind == FILTER_REFS_DETACHED_HEAD) + kind = FILTER_REFS_ROOT_REFS; else if (!(kind & filter->kind)) return NULL; @@ -3060,11 +3070,17 @@ static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref * of filter_ref_kind(). */ if (filter->kind == FILTER_REFS_BRANCHES) - ret = for_each_fullref_in("refs/heads/", fn, cb_data); + ret = refs_for_each_fullref_in(get_main_ref_store(the_repository), + "refs/heads/", NULL, + fn, cb_data); else if (filter->kind == FILTER_REFS_REMOTES) - ret = for_each_fullref_in("refs/remotes/", fn, cb_data); + ret = refs_for_each_fullref_in(get_main_ref_store(the_repository), + "refs/remotes/", NULL, + fn, cb_data); else if (filter->kind == FILTER_REFS_TAGS) - ret = for_each_fullref_in("refs/tags/", fn, cb_data); + ret = refs_for_each_fullref_in(get_main_ref_store(the_repository), + "refs/tags/", NULL, fn, + cb_data); else if (filter->kind & FILTER_REFS_REGULAR) ret = for_each_fullref_in_pattern(filter, fn, cb_data); @@ -3072,9 +3088,10 @@ static int do_filter_refs(struct ref_filter *filter, unsigned int type, each_ref * When printing all ref types, HEAD is already included, * so we don't want to print HEAD again. */ - if (!ret && (filter->kind != FILTER_REFS_KIND_MASK) && + if (!ret && !(filter->kind & FILTER_REFS_ROOT_REFS) && (filter->kind & FILTER_REFS_DETACHED_HEAD)) - head_ref(fn, cb_data); + refs_head_ref(get_main_ref_store(the_repository), fn, + cb_data); } clear_contains_cache(&filter->internal.contains_cache); diff --git a/ref-filter.h b/ref-filter.h index 0ca28d2..27ae1aa 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -23,9 +23,9 @@ FILTER_REFS_REMOTES | FILTER_REFS_OTHERS) #define FILTER_REFS_DETACHED_HEAD 0x0020 #define FILTER_REFS_PSEUDOREFS 0x0040 -#define FILTER_REFS_ROOT_REFS (FILTER_REFS_DETACHED_HEAD | FILTER_REFS_PSEUDOREFS) +#define FILTER_REFS_ROOT_REFS 0x0080 #define FILTER_REFS_KIND_MASK (FILTER_REFS_REGULAR | FILTER_REFS_DETACHED_HEAD | \ - FILTER_REFS_PSEUDOREFS) + FILTER_REFS_PSEUDOREFS | FILTER_REFS_ROOT_REFS) struct atom_value; struct ref_sorting; diff --git a/reflog-walk.c b/reflog-walk.c index 66484f4..5f09552 100644 --- a/reflog-walk.c +++ b/reflog-walk.c @@ -67,24 +67,32 @@ static struct complete_reflogs *read_complete_reflog(const char *ref) struct complete_reflogs *reflogs = xcalloc(1, sizeof(struct complete_reflogs)); reflogs->ref = xstrdup(ref); - for_each_reflog_ent(ref, read_one_reflog, reflogs); + refs_for_each_reflog_ent(get_main_ref_store(the_repository), ref, + read_one_reflog, reflogs); if (reflogs->nr == 0) { const char *name; void *name_to_free; - name = name_to_free = resolve_refdup(ref, RESOLVE_REF_READING, - NULL, NULL); + name = name_to_free = refs_resolve_refdup(get_main_ref_store(the_repository), + ref, + RESOLVE_REF_READING, + NULL, NULL); if (name) { - for_each_reflog_ent(name, read_one_reflog, reflogs); + refs_for_each_reflog_ent(get_main_ref_store(the_repository), + name, read_one_reflog, + reflogs); free(name_to_free); } } if (reflogs->nr == 0) { char *refname = xstrfmt("refs/%s", ref); - for_each_reflog_ent(refname, read_one_reflog, reflogs); + refs_for_each_reflog_ent(get_main_ref_store(the_repository), + refname, read_one_reflog, reflogs); if (reflogs->nr == 0) { free(refname); refname = xstrfmt("refs/heads/%s", ref); - for_each_reflog_ent(refname, read_one_reflog, reflogs); + refs_for_each_reflog_ent(get_main_ref_store(the_repository), + refname, read_one_reflog, + reflogs); } free(refname); } @@ -174,7 +182,8 @@ int add_reflog_for_walk(struct reflog_walk_info *info, else { if (*branch == '\0') { free(branch); - branch = resolve_refdup("HEAD", 0, NULL, NULL); + branch = refs_resolve_refdup(get_main_ref_store(the_repository), + "HEAD", 0, NULL, NULL); if (!branch) die("no current branch"); @@ -182,8 +191,8 @@ int add_reflog_for_walk(struct reflog_walk_info *info, reflogs = read_complete_reflog(branch); if (!reflogs || reflogs->nr == 0) { char *b; - int ret = dwim_log(branch, strlen(branch), - NULL, &b); + int ret = repo_dwim_log(the_repository, branch, strlen(branch), + NULL, &b); if (ret > 1) free(b); else if (ret == 1) { @@ -236,7 +245,9 @@ void get_reflog_selector(struct strbuf *sb, if (shorten) { if (!commit_reflog->reflogs->short_ref) commit_reflog->reflogs->short_ref - = shorten_unambiguous_ref(commit_reflog->reflogs->ref, 0); + = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), + commit_reflog->reflogs->ref, + 0); printed_ref = commit_reflog->reflogs->short_ref; } else { printed_ref = commit_reflog->reflogs->ref; @@ -343,7 +343,8 @@ void reflog_expiry_prepare(const char *refname, case UE_ALWAYS: return; case UE_HEAD: - for_each_ref(push_tip_to_list, &cb->tips); + refs_for_each_ref(get_main_ref_store(the_repository), + push_tip_to_list, &cb->tips); for (elem = cb->tips; elem; elem = elem->next) commit_list_insert(elem->item, &cb->mark_list); break; @@ -408,7 +409,7 @@ int reflog_delete(const char *rev, enum expire_reflog_flags flags, int verbose) if (!spec) return error(_("not a reflog: %s"), rev); - if (!dwim_log(rev, spec - rev, NULL, &ref)) { + if (!repo_dwim_log(the_repository, rev, spec - rev, NULL, &ref)) { status |= error(_("no reflog for '%s'"), rev); goto cleanup; } @@ -416,19 +417,22 @@ int reflog_delete(const char *rev, enum expire_reflog_flags flags, int verbose) recno = strtoul(spec + 2, &ep, 10); if (*ep == '}') { cmd.recno = -recno; - for_each_reflog_ent(ref, count_reflog_ent, &cmd); + refs_for_each_reflog_ent(get_main_ref_store(the_repository), + ref, count_reflog_ent, &cmd); } else { cmd.expire_total = approxidate(spec + 2); - for_each_reflog_ent(ref, count_reflog_ent, &cmd); + refs_for_each_reflog_ent(get_main_ref_store(the_repository), + ref, count_reflog_ent, &cmd); cmd.expire_total = 0; } cb.cmd = cmd; - status |= reflog_expire(ref, flags, - reflog_expiry_prepare, - should_prune_fn, - reflog_expiry_cleanup, - &cb); + status |= refs_reflog_expire(get_main_ref_store(the_repository), ref, + flags, + reflog_expiry_prepare, + should_prune_fn, + reflog_expiry_cleanup, + &cb); cleanup: free(ref); @@ -6,7 +6,7 @@ #include "advice.h" #include "config.h" #include "environment.h" -#include "hashmap.h" +#include "strmap.h" #include "gettext.h" #include "hex.h" #include "lockfile.h" @@ -19,7 +19,6 @@ #include "object-store-ll.h" #include "object.h" #include "path.h" -#include "tag.h" #include "submodule.h" #include "worktree.h" #include "strvec.h" @@ -384,14 +383,6 @@ char *refs_resolve_refdup(struct ref_store *refs, return xstrdup_or_null(result); } -char *resolve_refdup(const char *refname, int resolve_flags, - struct object_id *oid, int *flags) -{ - return refs_resolve_refdup(get_main_ref_store(the_repository), - refname, resolve_flags, - oid, flags); -} - /* The argument to for_each_filter_refs */ struct for_each_ref_filter { const char *pattern; @@ -400,19 +391,18 @@ struct for_each_ref_filter { void *cb_data; }; -int read_ref_full(const char *refname, int resolve_flags, struct object_id *oid, int *flags) +int refs_read_ref_full(struct ref_store *refs, const char *refname, + int resolve_flags, struct object_id *oid, int *flags) { - struct ref_store *refs = get_main_ref_store(the_repository); - if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, oid, flags)) return 0; return -1; } -int read_ref(const char *refname, struct object_id *oid) +int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid) { - return read_ref_full(refname, RESOLVE_REF_READING, oid, NULL); + return refs_read_ref_full(refs, refname, RESOLVE_REF_READING, oid, NULL); } int refs_ref_exists(struct ref_store *refs, const char *refname) @@ -421,11 +411,6 @@ int refs_ref_exists(struct ref_store *refs, const char *refname) NULL, NULL); } -int ref_exists(const char *refname) -{ - return refs_ref_exists(get_main_ref_store(the_repository), refname); -} - static int for_each_filter_refs(const char *refname, const struct object_id *oid, int flags, void *data) @@ -439,28 +424,8 @@ static int for_each_filter_refs(const char *refname, return filter->fn(refname, oid, flags, filter->cb_data); } -enum peel_status peel_object(const struct object_id *name, struct object_id *oid) -{ - struct object *o = lookup_unknown_object(the_repository, name); - - if (o->type == OBJ_NONE) { - int type = oid_object_info(the_repository, name, NULL); - if (type < 0 || !object_as_type(o, type, 0)) - return PEEL_INVALID; - } - - if (o->type != OBJ_TAG) - return PEEL_NON_TAG; - - o = deref_tag_noverify(o); - if (!o) - return PEEL_INVALID; - - oidcpy(oid, &o->oid); - return PEEL_PEELED; -} - struct warn_if_dangling_data { + struct ref_store *refs; FILE *fp; const char *refname; const struct string_list *refnames; @@ -477,7 +442,7 @@ static int warn_if_dangling_symref(const char *refname, if (!(flags & REF_ISSYMREF)) return 0; - resolves_to = resolve_ref_unsafe(refname, 0, NULL, NULL); + resolves_to = refs_resolve_ref_unsafe(d->refs, refname, 0, NULL, NULL); if (!resolves_to || (d->refname ? strcmp(resolves_to, d->refname) @@ -490,26 +455,28 @@ static int warn_if_dangling_symref(const char *refname, return 0; } -void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname) +void refs_warn_dangling_symref(struct ref_store *refs, FILE *fp, + const char *msg_fmt, const char *refname) { - struct warn_if_dangling_data data; - - data.fp = fp; - data.refname = refname; - data.refnames = NULL; - data.msg_fmt = msg_fmt; - for_each_rawref(warn_if_dangling_symref, &data); + struct warn_if_dangling_data data = { + .refs = refs, + .fp = fp, + .refname = refname, + .msg_fmt = msg_fmt, + }; + refs_for_each_rawref(refs, warn_if_dangling_symref, &data); } -void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, const struct string_list *refnames) +void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp, + const char *msg_fmt, const struct string_list *refnames) { - struct warn_if_dangling_data data; - - data.fp = fp; - data.refname = NULL; - data.refnames = refnames; - data.msg_fmt = msg_fmt; - for_each_rawref(warn_if_dangling_symref, &data); + struct warn_if_dangling_data data = { + .refs = refs, + .fp = fp, + .refnames = refnames, + .msg_fmt = msg_fmt, + }; + refs_for_each_rawref(refs, warn_if_dangling_symref, &data); } int refs_for_each_tag_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) @@ -517,32 +484,17 @@ int refs_for_each_tag_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) return refs_for_each_ref_in(refs, "refs/tags/", fn, cb_data); } -int for_each_tag_ref(each_ref_fn fn, void *cb_data) -{ - return refs_for_each_tag_ref(get_main_ref_store(the_repository), fn, cb_data); -} - int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) { return refs_for_each_ref_in(refs, "refs/heads/", fn, cb_data); } -int for_each_branch_ref(each_ref_fn fn, void *cb_data) -{ - return refs_for_each_branch_ref(get_main_ref_store(the_repository), fn, cb_data); -} - int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) { return refs_for_each_ref_in(refs, "refs/remotes/", fn, cb_data); } -int for_each_remote_ref(each_ref_fn fn, void *cb_data) -{ - return refs_for_each_remote_ref(get_main_ref_store(the_repository), fn, cb_data); -} - -int head_ref_namespaced(each_ref_fn fn, void *cb_data) +int refs_head_ref_namespaced(struct ref_store *refs, each_ref_fn fn, void *cb_data) { struct strbuf buf = STRBUF_INIT; int ret = 0; @@ -550,7 +502,7 @@ int head_ref_namespaced(each_ref_fn fn, void *cb_data) int flag; strbuf_addf(&buf, "%sHEAD", get_git_namespace()); - if (!read_ref_full(buf.buf, RESOLVE_REF_READING, &oid, &flag)) + if (!refs_read_ref_full(refs, buf.buf, RESOLVE_REF_READING, &oid, &flag)) ret = fn(buf.buf, &oid, flag, cb_data); strbuf_release(&buf); @@ -583,8 +535,8 @@ void normalize_glob_ref(struct string_list_item *item, const char *prefix, strbuf_release(&normalized_pattern); } -int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, - const char *prefix, void *cb_data) +int refs_for_each_glob_ref_in(struct ref_store *refs, each_ref_fn fn, + const char *pattern, const char *prefix, void *cb_data) { struct strbuf real_pattern = STRBUF_INIT; struct for_each_ref_filter filter; @@ -607,15 +559,16 @@ int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, filter.prefix = prefix; filter.fn = fn; filter.cb_data = cb_data; - ret = for_each_ref(for_each_filter_refs, &filter); + ret = refs_for_each_ref(refs, for_each_filter_refs, &filter); strbuf_release(&real_pattern); return ret; } -int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data) +int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn, + const char *pattern, void *cb_data) { - return for_each_glob_ref_in(fn, pattern, NULL, cb_data); + return refs_for_each_glob_ref_in(refs, fn, pattern, NULL, cb_data); } const char *prettify_refname(const char *name) @@ -711,16 +664,6 @@ char *repo_default_branch_name(struct repository *r, int quiet) return ret; } -const char *git_default_branch_name(int quiet) -{ - static char *ret; - - if (!ret) - ret = repo_default_branch_name(the_repository, quiet); - - return ret; -} - /* * *string and *len will only be substituted, and *string returned (for * later free()ing) if the string passed in is a magic short-hand form @@ -832,11 +775,6 @@ int repo_dwim_log(struct repository *r, const char *str, int len, return logs_found; } -int dwim_log(const char *str, int len, struct object_id *oid, char **log) -{ - return repo_dwim_log(the_repository, str, len, oid, log); -} - int is_per_worktree_ref(const char *refname) { return starts_with(refname, "refs/worktree/") || @@ -844,7 +782,22 @@ int is_per_worktree_ref(const char *refname) starts_with(refname, "refs/rewritten/"); } -static int is_pseudoref_syntax(const char *refname) +int is_pseudo_ref(const char *refname) +{ + static const char * const pseudo_refs[] = { + "FETCH_HEAD", + "MERGE_HEAD", + }; + size_t i; + + for (i = 0; i < ARRAY_SIZE(pseudo_refs); i++) + if (!strcmp(refname, pseudo_refs[i])) + return 1; + + return 0; +} + +static int is_root_ref_syntax(const char *refname) { const char *c; @@ -853,56 +806,37 @@ static int is_pseudoref_syntax(const char *refname) return 0; } - /* - * HEAD is not a pseudoref, but it certainly uses the - * pseudoref syntax. - */ return 1; } -int is_pseudoref(struct ref_store *refs, const char *refname) +int is_root_ref(const char *refname) { - static const char *const irregular_pseudorefs[] = { + static const char *const irregular_root_refs[] = { + "HEAD", "AUTO_MERGE", "BISECT_EXPECTED_REV", "NOTES_MERGE_PARTIAL", "NOTES_MERGE_REF", "MERGE_AUTOSTASH", }; - struct object_id oid; size_t i; - if (!is_pseudoref_syntax(refname)) + if (!is_root_ref_syntax(refname) || + is_pseudo_ref(refname)) return 0; - if (ends_with(refname, "_HEAD")) { - refs_resolve_ref_unsafe(refs, refname, - RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, - &oid, NULL); - return !is_null_oid(&oid); - } - - for (i = 0; i < ARRAY_SIZE(irregular_pseudorefs); i++) - if (!strcmp(refname, irregular_pseudorefs[i])) { - refs_resolve_ref_unsafe(refs, refname, - RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, - &oid, NULL); - return !is_null_oid(&oid); - } - - return 0; -} + if (ends_with(refname, "_HEAD")) + return 1; -int is_headref(struct ref_store *refs, const char *refname) -{ - if (!strcmp(refname, "HEAD")) - return refs_ref_exists(refs, refname); + for (i = 0; i < ARRAY_SIZE(irregular_root_refs); i++) + if (!strcmp(refname, irregular_root_refs[i])) + return 1; return 0; } static int is_current_worktree_ref(const char *ref) { - return is_pseudoref_syntax(ref) || is_per_worktree_ref(ref); + return is_root_ref_syntax(ref) || is_per_worktree_ref(ref); } enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref, @@ -991,13 +925,6 @@ int refs_delete_ref(struct ref_store *refs, const char *msg, return 0; } -int delete_ref(const char *msg, const char *refname, - const struct object_id *old_oid, unsigned int flags) -{ - return refs_delete_ref(get_main_ref_store(the_repository), msg, refname, - old_oid, flags); -} - static void copy_reflog_msg(struct strbuf *sb, const char *msg) { char c; @@ -1190,11 +1117,6 @@ struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs, return tr; } -struct ref_transaction *ref_transaction_begin(struct strbuf *err) -{ - return ref_store_transaction_begin(get_main_ref_store(the_repository), err); -} - void ref_transaction_free(struct ref_transaction *transaction) { size_t i; @@ -1217,6 +1139,8 @@ void ref_transaction_free(struct ref_transaction *transaction) for (i = 0; i < transaction->nr; i++) { free(transaction->updates[i]->msg); + free((char *)transaction->updates[i]->new_target); + free((char *)transaction->updates[i]->old_target); free(transaction->updates[i]); } free(transaction->updates); @@ -1228,6 +1152,7 @@ struct ref_update *ref_transaction_add_update( const char *refname, unsigned int flags, const struct object_id *new_oid, const struct object_id *old_oid, + const char *new_target, const char *old_target, const char *msg) { struct ref_update *update; @@ -1235,16 +1160,24 @@ struct ref_update *ref_transaction_add_update( if (transaction->state != REF_TRANSACTION_OPEN) BUG("update called for transaction that is not open"); + if (old_oid && old_target) + BUG("only one of old_oid and old_target should be non NULL"); + if (new_oid && new_target) + BUG("only one of new_oid and new_target should be non NULL"); + FLEX_ALLOC_STR(update, refname, refname); ALLOC_GROW(transaction->updates, transaction->nr + 1, transaction->alloc); transaction->updates[transaction->nr++] = update; update->flags = flags; - if (flags & REF_HAVE_NEW) + update->new_target = xstrdup_or_null(new_target); + update->old_target = xstrdup_or_null(old_target); + if ((flags & REF_HAVE_NEW) && new_oid) oidcpy(&update->new_oid, new_oid); - if (flags & REF_HAVE_OLD) + if ((flags & REF_HAVE_OLD) && old_oid) oidcpy(&update->old_oid, old_oid); + update->msg = normalize_reflog_message(msg); return update; } @@ -1253,6 +1186,8 @@ int ref_transaction_update(struct ref_transaction *transaction, const char *refname, const struct object_id *new_oid, const struct object_id *old_oid, + const char *new_target, + const char *old_target, unsigned int flags, const char *msg, struct strbuf *err) { @@ -1267,6 +1202,13 @@ int ref_transaction_update(struct ref_transaction *transaction, return -1; } + if (!(flags & REF_SKIP_REFNAME_VERIFICATION) && + is_pseudo_ref(refname)) { + strbuf_addf(err, _("refusing to update pseudoref '%s'"), + refname); + return -1; + } + if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS) BUG("illegal flags 0x%x passed to ref_transaction_update()", flags); @@ -1278,9 +1220,11 @@ int ref_transaction_update(struct ref_transaction *transaction, flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS; flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0); + flags |= (new_target ? REF_HAVE_NEW : 0) | (old_target ? REF_HAVE_OLD : 0); ref_transaction_add_update(transaction, refname, flags, - new_oid, old_oid, msg); + new_oid, old_oid, new_target, + old_target, msg); return 0; } @@ -1295,7 +1239,8 @@ int ref_transaction_create(struct ref_transaction *transaction, return 1; } return ref_transaction_update(transaction, refname, new_oid, - null_oid(), flags, msg, err); + null_oid(), NULL, NULL, flags, + msg, err); } int ref_transaction_delete(struct ref_transaction *transaction, @@ -1308,7 +1253,8 @@ int ref_transaction_delete(struct ref_transaction *transaction, BUG("delete called with old_oid set to zeros"); return ref_transaction_update(transaction, refname, null_oid(), old_oid, - flags, msg, err); + NULL, NULL, flags, + msg, err); } int ref_transaction_verify(struct ref_transaction *transaction, @@ -1321,6 +1267,7 @@ int ref_transaction_verify(struct ref_transaction *transaction, BUG("verify called with old_oid set to NULL"); return ref_transaction_update(transaction, refname, NULL, old_oid, + NULL, NULL, flags, NULL, err); } @@ -1335,8 +1282,8 @@ int refs_update_ref(struct ref_store *refs, const char *msg, t = ref_store_transaction_begin(refs, &err); if (!t || - ref_transaction_update(t, refname, new_oid, old_oid, flags, msg, - &err) || + ref_transaction_update(t, refname, new_oid, old_oid, NULL, NULL, + flags, msg, &err) || ref_transaction_commit(t, &err)) { ret = 1; ref_transaction_free(t); @@ -1363,15 +1310,6 @@ int refs_update_ref(struct ref_store *refs, const char *msg, return 0; } -int update_ref(const char *msg, const char *refname, - const struct object_id *new_oid, - const struct object_id *old_oid, - unsigned int flags, enum action_on_err onerr) -{ - return refs_update_ref(get_main_ref_store(the_repository), msg, refname, new_oid, - old_oid, flags, onerr); -} - /* * Check that the string refname matches a rule of the form * "{prefix}%.*s{suffix}". So "foo/bar/baz" would match the rule @@ -1473,12 +1411,6 @@ char *refs_shorten_unambiguous_ref(struct ref_store *refs, return xstrdup(refname); } -char *shorten_unambiguous_ref(const char *refname, int strict) -{ - return refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), - refname, strict); -} - int parse_hide_refs_config(const char *var, const char *value, const char *section, struct strvec *hide_refs) { @@ -1597,11 +1529,6 @@ int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) return 0; } -int head_ref(each_ref_fn fn, void *cb_data) -{ - return refs_head_ref(get_main_ref_store(the_repository), fn, cb_data); -} - struct ref_iterator *refs_ref_iterator_begin( struct ref_store *refs, const char *prefix, @@ -1633,53 +1560,12 @@ struct ref_iterator *refs_ref_iterator_begin( return iter; } -/* - * Call fn for each reference in the specified submodule for which the - * refname begins with prefix. If trim is non-zero, then trim that - * many characters off the beginning of each refname before passing - * the refname to fn. flags can be DO_FOR_EACH_INCLUDE_BROKEN to - * include broken references in the iteration. If fn ever returns a - * non-zero value, stop the iteration and return that value; - * otherwise, return 0. - */ -static int do_for_each_repo_ref(struct repository *r, const char *prefix, - each_repo_ref_fn fn, int trim, int flags, - void *cb_data) -{ - struct ref_iterator *iter; - struct ref_store *refs = get_main_ref_store(r); - - if (!refs) - return 0; - - iter = refs_ref_iterator_begin(refs, prefix, NULL, trim, flags); - - return do_for_each_repo_ref_iterator(r, iter, fn, cb_data); -} - -struct do_for_each_ref_help { - each_ref_fn *fn; - void *cb_data; -}; - -static int do_for_each_ref_helper(struct repository *r UNUSED, - const char *refname, - const struct object_id *oid, - int flags, - void *cb_data) -{ - struct do_for_each_ref_help *hp = cb_data; - - return hp->fn(refname, oid, flags, hp->cb_data); -} - static int do_for_each_ref(struct ref_store *refs, const char *prefix, const char **exclude_patterns, each_ref_fn fn, int trim, enum do_for_each_ref_flags flags, void *cb_data) { struct ref_iterator *iter; - struct do_for_each_ref_help hp = { fn, cb_data }; if (!refs) return 0; @@ -1687,8 +1573,7 @@ static int do_for_each_ref(struct ref_store *refs, const char *prefix, iter = refs_ref_iterator_begin(refs, prefix, exclude_patterns, trim, flags); - return do_for_each_repo_ref_iterator(the_repository, iter, - do_for_each_ref_helper, &hp); + return do_for_each_ref_iterator(iter, fn, cb_data); } int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) @@ -1696,28 +1581,12 @@ int refs_for_each_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) return do_for_each_ref(refs, "", NULL, fn, 0, 0, cb_data); } -int for_each_ref(each_ref_fn fn, void *cb_data) -{ - return refs_for_each_ref(get_main_ref_store(the_repository), fn, cb_data); -} - int refs_for_each_ref_in(struct ref_store *refs, const char *prefix, each_ref_fn fn, void *cb_data) { return do_for_each_ref(refs, prefix, NULL, fn, strlen(prefix), 0, cb_data); } -int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data) -{ - return refs_for_each_ref_in(get_main_ref_store(the_repository), prefix, fn, cb_data); -} - -int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data) -{ - return do_for_each_ref(get_main_ref_store(the_repository), - prefix, NULL, fn, 0, 0, cb_data); -} - int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix, const char **exclude_patterns, each_ref_fn fn, void *cb_data) @@ -1725,22 +1594,22 @@ int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix, return do_for_each_ref(refs, prefix, exclude_patterns, fn, 0, 0, cb_data); } -int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data) +int refs_for_each_replace_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data) { const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref; - return do_for_each_repo_ref(r, git_replace_ref_base, fn, - strlen(git_replace_ref_base), - DO_FOR_EACH_INCLUDE_BROKEN, cb_data); + return do_for_each_ref(refs, git_replace_ref_base, NULL, fn, + strlen(git_replace_ref_base), + DO_FOR_EACH_INCLUDE_BROKEN, cb_data); } -int for_each_namespaced_ref(const char **exclude_patterns, - each_ref_fn fn, void *cb_data) +int refs_for_each_namespaced_ref(struct ref_store *refs, + const char **exclude_patterns, + each_ref_fn fn, void *cb_data) { struct strbuf buf = STRBUF_INIT; int ret; strbuf_addf(&buf, "%srefs/", get_git_namespace()); - ret = do_for_each_ref(get_main_ref_store(the_repository), - buf.buf, exclude_patterns, fn, 0, 0, cb_data); + ret = do_for_each_ref(refs, buf.buf, exclude_patterns, fn, 0, 0, cb_data); strbuf_release(&buf); return ret; } @@ -1751,11 +1620,6 @@ int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data) DO_FOR_EACH_INCLUDE_BROKEN, cb_data); } -int for_each_rawref(each_ref_fn fn, void *cb_data) -{ - return refs_for_each_rawref(get_main_ref_store(the_repository), fn, cb_data); -} - int refs_for_each_include_root_refs(struct ref_store *refs, each_ref_fn fn, void *cb_data) { @@ -1876,43 +1740,12 @@ done: return result; } -static int is_special_ref(const char *refname) -{ - /* - * Special references are refs that have different semantics compared - * to "normal" refs. These refs can thus not be stored in the ref - * backend, but must always be accessed via the filesystem. The - * following refs are special: - * - * - FETCH_HEAD may contain multiple object IDs, and each one of them - * carries additional metadata like where it came from. - * - * - MERGE_HEAD may contain multiple object IDs when merging multiple - * heads. - * - * Reading, writing or deleting references must consistently go either - * through the filesystem (special refs) or through the reference - * backend (normal ones). - */ - static const char * const special_refs[] = { - "FETCH_HEAD", - "MERGE_HEAD", - }; - size_t i; - - for (i = 0; i < ARRAY_SIZE(special_refs); i++) - if (!strcmp(refname, special_refs[i])) - return 1; - - return 0; -} - int refs_read_raw_ref(struct ref_store *ref_store, const char *refname, struct object_id *oid, struct strbuf *referent, unsigned int *type, int *failure_errno) { assert(failure_errno); - if (is_special_ref(refname)) + if (is_pseudo_ref(refname)) return refs_read_special_head(ref_store, refname, oid, referent, type, failure_errno); @@ -2016,26 +1849,19 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs, } /* backend functions */ -int refs_init_db(struct ref_store *refs, int flags, struct strbuf *err) +int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err) { - return refs->be->init_db(refs, flags, err); + return refs->be->create_on_disk(refs, flags, err); } -const char *resolve_ref_unsafe(const char *refname, int resolve_flags, - struct object_id *oid, int *flags) -{ - return refs_resolve_ref_unsafe(get_main_ref_store(the_repository), refname, - resolve_flags, oid, flags); -} - -int resolve_gitlink_ref(const char *submodule, const char *refname, - struct object_id *oid) +int repo_resolve_gitlink_ref(struct repository *r, + const char *submodule, const char *refname, + struct object_id *oid) { struct ref_store *refs; int flags; - refs = get_submodule_ref_store(submodule); - + refs = repo_get_submodule_ref_store(r, submodule); if (!refs) return -1; @@ -2045,66 +1871,21 @@ int resolve_gitlink_ref(const char *submodule, const char *refname, return 0; } -struct ref_store_hash_entry -{ - struct hashmap_entry ent; - - struct ref_store *refs; - - /* NUL-terminated identifier of the ref store: */ - char name[FLEX_ARRAY]; -}; - -static int ref_store_hash_cmp(const void *cmp_data UNUSED, - const struct hashmap_entry *eptr, - const struct hashmap_entry *entry_or_key, - const void *keydata) -{ - const struct ref_store_hash_entry *e1, *e2; - const char *name; - - e1 = container_of(eptr, const struct ref_store_hash_entry, ent); - e2 = container_of(entry_or_key, const struct ref_store_hash_entry, ent); - name = keydata ? keydata : e2->name; - - return strcmp(e1->name, name); -} - -static struct ref_store_hash_entry *alloc_ref_store_hash_entry( - const char *name, struct ref_store *refs) -{ - struct ref_store_hash_entry *entry; - - FLEX_ALLOC_STR(entry, name, name); - hashmap_entry_init(&entry->ent, strhash(name)); - entry->refs = refs; - return entry; -} - -/* A hashmap of ref_stores, stored by submodule name: */ -static struct hashmap submodule_ref_stores; - -/* A hashmap of ref_stores, stored by worktree id: */ -static struct hashmap worktree_ref_stores; - /* * Look up a ref store by name. If that ref_store hasn't been * registered yet, return NULL. */ -static struct ref_store *lookup_ref_store_map(struct hashmap *map, +static struct ref_store *lookup_ref_store_map(struct strmap *map, const char *name) { - struct ref_store_hash_entry *entry; - unsigned int hash; + struct strmap_entry *entry; - if (!map->tablesize) + if (!map->map.tablesize) /* It's initialized on demand in register_ref_store(). */ return NULL; - hash = strhash(name); - entry = hashmap_get_entry_from_hash(map, hash, name, - struct ref_store_hash_entry, ent); - return entry ? entry->refs : NULL; + entry = strmap_get_entry(map, name); + return entry ? entry->value : NULL; } /* @@ -2126,6 +1907,12 @@ static struct ref_store *ref_store_init(struct repository *repo, return refs; } +void ref_store_release(struct ref_store *ref_store) +{ + ref_store->be->release(ref_store); + free(ref_store->gitdir); +} + struct ref_store *get_main_ref_store(struct repository *r) { if (r->refs_private) @@ -2143,22 +1930,19 @@ struct ref_store *get_main_ref_store(struct repository *r) * Associate a ref store with a name. It is a fatal error to call this * function twice for the same name. */ -static void register_ref_store_map(struct hashmap *map, +static void register_ref_store_map(struct strmap *map, const char *type, struct ref_store *refs, const char *name) { - struct ref_store_hash_entry *entry; - - if (!map->tablesize) - hashmap_init(map, ref_store_hash_cmp, NULL, 0); - - entry = alloc_ref_store_hash_entry(name, refs); - if (hashmap_put(map, &entry->ent)) + if (!map->map.tablesize) + strmap_init(map); + if (strmap_put(map, name, refs)) BUG("%s ref_store '%s' initialized twice", type, name); } -struct ref_store *get_submodule_ref_store(const char *submodule) +struct ref_store *repo_get_submodule_ref_store(struct repository *repo, + const char *submodule) { struct strbuf submodule_sb = STRBUF_INIT; struct ref_store *refs; @@ -2179,7 +1963,7 @@ struct ref_store *get_submodule_ref_store(const char *submodule) /* We need to strip off one or more trailing slashes */ submodule = to_free = xmemdupz(submodule, len); - refs = lookup_ref_store_map(&submodule_ref_stores, submodule); + refs = lookup_ref_store_map(&repo->submodule_ref_stores, submodule); if (refs) goto done; @@ -2191,20 +1975,15 @@ struct ref_store *get_submodule_ref_store(const char *submodule) goto done; subrepo = xmalloc(sizeof(*subrepo)); - /* - * NEEDSWORK: Make get_submodule_ref_store() work with arbitrary - * superprojects other than the_repository. This probably should be - * done by making it take a struct repository * parameter instead of a - * submodule path. - */ - if (repo_submodule_init(subrepo, the_repository, submodule, + + if (repo_submodule_init(subrepo, repo, submodule, null_oid())) { free(subrepo); goto done; } refs = ref_store_init(subrepo, submodule_sb.buf, REF_STORE_READ | REF_STORE_ODB); - register_ref_store_map(&submodule_ref_stores, "submodule", + register_ref_store_map(&repo->submodule_ref_stores, "submodule", refs, submodule); done: @@ -2220,25 +1999,29 @@ struct ref_store *get_worktree_ref_store(const struct worktree *wt) const char *id; if (wt->is_current) - return get_main_ref_store(the_repository); + return get_main_ref_store(wt->repo); id = wt->id ? wt->id : "/"; - refs = lookup_ref_store_map(&worktree_ref_stores, id); + refs = lookup_ref_store_map(&wt->repo->worktree_ref_stores, id); if (refs) return refs; - if (wt->id) - refs = ref_store_init(the_repository, - git_common_path("worktrees/%s", wt->id), + if (wt->id) { + struct strbuf common_path = STRBUF_INIT; + strbuf_git_common_path(&common_path, wt->repo, + "worktrees/%s", wt->id); + refs = ref_store_init(wt->repo, common_path.buf, REF_STORE_ALL_CAPS); - else - refs = ref_store_init(the_repository, - get_git_common_dir(), + strbuf_release(&common_path); + } else { + refs = ref_store_init(wt->repo, wt->repo->commondir, REF_STORE_ALL_CAPS); + } if (refs) - register_ref_store_map(&worktree_ref_stores, "worktree", - refs, id); + register_ref_store_map(&wt->repo->worktree_ref_stores, + "worktree", refs, id); + return refs; } @@ -2256,36 +2039,37 @@ int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts) return refs->be->pack_refs(refs, opts); } -int peel_iterated_oid(const struct object_id *base, struct object_id *peeled) +int peel_iterated_oid(struct repository *r, const struct object_id *base, struct object_id *peeled) { if (current_ref_iter && (current_ref_iter->oid == base || oideq(current_ref_iter->oid, base))) return ref_iterator_peel(current_ref_iter, peeled); - return peel_object(base, peeled) ? -1 : 0; + return peel_object(r, base, peeled) ? -1 : 0; } -int refs_create_symref(struct ref_store *refs, - const char *ref_target, - const char *refs_heads_master, - const char *logmsg) +int refs_update_symref(struct ref_store *refs, const char *ref, + const char *target, const char *logmsg) { - char *msg; - int retval; + struct ref_transaction *transaction; + struct strbuf err = STRBUF_INIT; + int ret = 0; - msg = normalize_reflog_message(logmsg); - retval = refs->be->create_symref(refs, ref_target, refs_heads_master, - msg); - free(msg); - return retval; -} + transaction = ref_store_transaction_begin(refs, &err); + if (!transaction || + ref_transaction_update(transaction, ref, NULL, NULL, + target, NULL, REF_NO_DEREF, + logmsg, &err) || + ref_transaction_commit(transaction, &err)) { + ret = error("%s", err.buf); + } -int create_symref(const char *ref_target, const char *refs_heads_master, - const char *logmsg) -{ - return refs_create_symref(get_main_ref_store(the_repository), ref_target, - refs_heads_master, logmsg); + strbuf_release(&err); + if (transaction) + ref_transaction_free(transaction); + + return ret; } int ref_update_reject_duplicates(struct string_list *refnames, @@ -2338,10 +2122,22 @@ static int run_transaction_hook(struct ref_transaction *transaction, struct ref_update *update = transaction->updates[i]; strbuf_reset(&buf); - strbuf_addf(&buf, "%s %s %s\n", - oid_to_hex(&update->old_oid), - oid_to_hex(&update->new_oid), - update->refname); + + if (!(update->flags & REF_HAVE_OLD)) + strbuf_addf(&buf, "%s ", oid_to_hex(null_oid())); + else if (update->old_target) + strbuf_addf(&buf, "ref:%s ", update->old_target); + else + strbuf_addf(&buf, "%s ", oid_to_hex(&update->old_oid)); + + if (!(update->flags & REF_HAVE_NEW)) + strbuf_addf(&buf, "%s ", oid_to_hex(null_oid())); + else if (update->new_target) + strbuf_addf(&buf, "ref:%s ", update->new_target); + else + strbuf_addf(&buf, "%s ", oid_to_hex(&update->new_oid)); + + strbuf_addf(&buf, "%s\n", update->refname); if (write_in_full(proc.in, buf.buf, buf.len) < 0) { if (errno != EPIPE) { @@ -2560,8 +2356,7 @@ struct do_for_each_reflog_help { void *cb_data; }; -static int do_for_each_reflog_helper(struct repository *r UNUSED, - const char *refname, +static int do_for_each_reflog_helper(const char *refname, const struct object_id *oid UNUSED, int flags, void *cb_data) @@ -2577,13 +2372,7 @@ int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_dat iter = refs->be->reflog_iterator_begin(refs); - return do_for_each_repo_ref_iterator(the_repository, iter, - do_for_each_reflog_helper, &hp); -} - -int for_each_reflog(each_reflog_fn fn, void *cb_data) -{ - return refs_for_each_reflog(get_main_ref_store(the_repository), fn, cb_data); + return do_for_each_ref_iterator(iter, do_for_each_reflog_helper, &hp); } int refs_for_each_reflog_ent_reverse(struct ref_store *refs, @@ -2595,58 +2384,28 @@ int refs_for_each_reflog_ent_reverse(struct ref_store *refs, fn, cb_data); } -int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, - void *cb_data) -{ - return refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), - refname, fn, cb_data); -} - int refs_for_each_reflog_ent(struct ref_store *refs, const char *refname, each_reflog_ent_fn fn, void *cb_data) { return refs->be->for_each_reflog_ent(refs, refname, fn, cb_data); } -int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, - void *cb_data) -{ - return refs_for_each_reflog_ent(get_main_ref_store(the_repository), refname, - fn, cb_data); -} - int refs_reflog_exists(struct ref_store *refs, const char *refname) { return refs->be->reflog_exists(refs, refname); } -int reflog_exists(const char *refname) -{ - return refs_reflog_exists(get_main_ref_store(the_repository), refname); -} - int refs_create_reflog(struct ref_store *refs, const char *refname, struct strbuf *err) { return refs->be->create_reflog(refs, refname, err); } -int safe_create_reflog(const char *refname, struct strbuf *err) -{ - return refs_create_reflog(get_main_ref_store(the_repository), refname, - err); -} - int refs_delete_reflog(struct ref_store *refs, const char *refname) { return refs->be->delete_reflog(refs, refname); } -int delete_reflog(const char *refname) -{ - return refs_delete_reflog(get_main_ref_store(the_repository), refname); -} - int refs_reflog_expire(struct ref_store *refs, const char *refname, unsigned int flags, @@ -2660,19 +2419,6 @@ int refs_reflog_expire(struct ref_store *refs, cleanup_fn, policy_cb_data); } -int reflog_expire(const char *refname, - unsigned int flags, - reflog_expiry_prepare_fn prepare_fn, - reflog_expiry_should_prune_fn should_prune_fn, - reflog_expiry_cleanup_fn cleanup_fn, - void *policy_cb_data) -{ - return refs_reflog_expire(get_main_ref_store(the_repository), - refname, flags, - prepare_fn, should_prune_fn, - cleanup_fn, policy_cb_data); -} - int initial_ref_transaction_commit(struct ref_transaction *transaction, struct strbuf *err) { @@ -2751,12 +2497,6 @@ out: return ret; } -int delete_refs(const char *msg, struct string_list *refnames, - unsigned int flags) -{ - return refs_delete_refs(get_main_ref_store(the_repository), msg, refnames, flags); -} - int refs_rename_ref(struct ref_store *refs, const char *oldref, const char *newref, const char *logmsg) { @@ -2769,11 +2509,6 @@ int refs_rename_ref(struct ref_store *refs, const char *oldref, return retval; } -int rename_ref(const char *oldref, const char *newref, const char *logmsg) -{ - return refs_rename_ref(get_main_ref_store(the_repository), oldref, newref, logmsg); -} - int refs_copy_existing_ref(struct ref_store *refs, const char *oldref, const char *newref, const char *logmsg) { @@ -2786,7 +2521,37 @@ int refs_copy_existing_ref(struct ref_store *refs, const char *oldref, return retval; } -int copy_existing_ref(const char *oldref, const char *newref, const char *logmsg) +const char *ref_update_original_update_refname(struct ref_update *update) +{ + while (update->parent_update) + update = update->parent_update; + + return update->refname; +} + +int ref_update_has_null_new_value(struct ref_update *update) +{ + return !update->new_target && is_null_oid(&update->new_oid); +} + +int ref_update_check_old_target(const char *referent, struct ref_update *update, + struct strbuf *err) { - return refs_copy_existing_ref(get_main_ref_store(the_repository), oldref, newref, logmsg); + if (!update->old_target) + BUG("called without old_target set"); + + if (!strcmp(referent, update->old_target)) + return 0; + + if (!strcmp(referent, "")) + strbuf_addf(err, "verifying symref target: '%s': " + "reference is missing but expected %s", + ref_update_original_update_refname(update), + update->old_target); + else + strbuf_addf(err, "verifying symref target: '%s': " + "is at %s but expected %s", + ref_update_original_update_refname(update), + referent, update->old_target); + return -1; } @@ -72,18 +72,14 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs, struct object_id *oid, int *flags); -const char *resolve_ref_unsafe(const char *refname, int resolve_flags, - struct object_id *oid, int *flags); - char *refs_resolve_refdup(struct ref_store *refs, const char *refname, int resolve_flags, struct object_id *oid, int *flags); -char *resolve_refdup(const char *refname, int resolve_flags, - struct object_id *oid, int *flags); -int read_ref_full(const char *refname, int resolve_flags, - struct object_id *oid, int *flags); -int read_ref(const char *refname, struct object_id *oid); +int refs_read_ref_full(struct ref_store *refs, const char *refname, + int resolve_flags, struct object_id *oid, int *flags); + +int refs_read_ref(struct ref_store *refs, const char *refname, struct object_id *oid); int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname, struct strbuf *referent); @@ -114,27 +110,31 @@ int refs_verify_refname_available(struct ref_store *refs, int refs_ref_exists(struct ref_store *refs, const char *refname); -int ref_exists(const char *refname); - int should_autocreate_reflog(const char *refname); int is_branch(const char *refname); -#define REFS_INIT_DB_IS_WORKTREE (1 << 0) +#define REF_STORE_CREATE_ON_DISK_IS_WORKTREE (1 << 0) -int refs_init_db(struct ref_store *refs, int flags, struct strbuf *err); +int ref_store_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err); + +/* + * Release all memory and resources associated with the ref store. + */ +void ref_store_release(struct ref_store *ref_store); /* * Return the peeled value of the oid currently being iterated via * for_each_ref(), etc. This is equivalent to calling: * - * peel_object(oid, &peeled); + * peel_object(r, oid, &peeled); * * with the "oid" value given to the each_ref_fn callback, except * that some ref storage may be able to answer the query without * actually loading the object in memory. */ -int peel_iterated_oid(const struct object_id *base, struct object_id *peeled); +int peel_iterated_oid(struct repository *r, + const struct object_id *base, struct object_id *peeled); /** * Resolve refname in the nested "gitlink" repository in the specified @@ -142,8 +142,9 @@ int peel_iterated_oid(const struct object_id *base, struct object_id *peeled); * successful, return 0 and set oid to the name of the object; * otherwise, return a non-zero value. */ -int resolve_gitlink_ref(const char *submodule, const char *refname, - struct object_id *oid); +int repo_resolve_gitlink_ref(struct repository *r, + const char *submodule, const char *refname, + struct object_id *oid); /* * Return true iff abbrev_name is a possible abbreviation for @@ -163,15 +164,12 @@ int expand_ref(struct repository *r, const char *str, int len, struct object_id int repo_dwim_ref(struct repository *r, const char *str, int len, struct object_id *oid, char **ref, int nonfatal_dangling_mark); int repo_dwim_log(struct repository *r, const char *str, int len, struct object_id *oid, char **ref); -int dwim_log(const char *str, int len, struct object_id *oid, char **ref); /* * Retrieves the default branch name for newly-initialized repositories. * - * The return value of `repo_default_branch_name()` is an allocated string. The - * return value of `git_default_branch_name()` is a singleton. + * The return value is an allocated string. */ -const char *git_default_branch_name(int quiet); char *repo_default_branch_name(struct repository *r, int quiet); /* @@ -299,16 +297,6 @@ typedef int each_ref_fn(const char *refname, const struct object_id *oid, int flags, void *cb_data); /* - * The same as each_ref_fn, but also with a repository argument that - * contains the repository associated with the callback. - */ -typedef int each_repo_ref_fn(struct repository *r, - const char *refname, - const struct object_id *oid, - int flags, - void *cb_data); - -/* * The following functions invoke the specified callback function for * each reference indicated. If the function ever returns a nonzero * value, stop the iteration and return that value. Please note that @@ -329,18 +317,8 @@ int refs_for_each_branch_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data); int refs_for_each_remote_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data); - -/* just iterates the head ref. */ -int head_ref(each_ref_fn fn, void *cb_data); - -/* iterates all refs. */ -int for_each_ref(each_ref_fn fn, void *cb_data); - -/** - * iterates all refs which have a defined prefix and strips that prefix from - * the passed variable refname. - */ -int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data); +int refs_for_each_replace_ref(struct ref_store *refs, + each_ref_fn fn, void *cb_data); /* * references matching any pattern in "exclude_patterns" are omitted from the @@ -349,7 +327,6 @@ int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data); int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix, const char **exclude_patterns, each_ref_fn fn, void *cb_data); -int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data); /** * iterate all refs in "patterns" by partitioning patterns into disjoint sets @@ -366,31 +343,25 @@ int refs_for_each_fullref_in_prefixes(struct ref_store *refs, const char **exclude_patterns, each_ref_fn fn, void *cb_data); -/** - * iterate refs from the respective area. - */ -int for_each_tag_ref(each_ref_fn fn, void *cb_data); -int for_each_branch_ref(each_ref_fn fn, void *cb_data); -int for_each_remote_ref(each_ref_fn fn, void *cb_data); -int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data); - /* iterates all refs that match the specified glob pattern. */ -int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data); +int refs_for_each_glob_ref(struct ref_store *refs, each_ref_fn fn, + const char *pattern, void *cb_data); + +int refs_for_each_glob_ref_in(struct ref_store *refs, each_ref_fn fn, + const char *pattern, const char *prefix, void *cb_data); -int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, - const char *prefix, void *cb_data); +int refs_head_ref_namespaced(struct ref_store *refs, each_ref_fn fn, void *cb_data); -int head_ref_namespaced(each_ref_fn fn, void *cb_data); /* * references matching any pattern in "exclude_patterns" are omitted from the * result set on a best-effort basis. */ -int for_each_namespaced_ref(const char **exclude_patterns, - each_ref_fn fn, void *cb_data); +int refs_for_each_namespaced_ref(struct ref_store *refs, + const char **exclude_patterns, + each_ref_fn fn, void *cb_data); /* can be used to learn about broken ref and symref */ int refs_for_each_rawref(struct ref_store *refs, each_ref_fn fn, void *cb_data); -int for_each_rawref(each_ref_fn fn, void *cb_data); /* * Iterates over all refs including root refs, i.e. pseudorefs and HEAD. @@ -415,9 +386,10 @@ static inline const char *has_glob_specials(const char *pattern) return strpbrk(pattern, "?*["); } -void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refname); -void warn_dangling_symrefs(FILE *fp, const char *msg_fmt, - const struct string_list *refnames); +void refs_warn_dangling_symref(struct ref_store *refs, FILE *fp, + const char *msg_fmt, const char *refname); +void refs_warn_dangling_symrefs(struct ref_store *refs, FILE *fp, + const char *msg_fmt, const struct string_list *refnames); /* * Flags for controlling behaviour of pack_refs() @@ -446,7 +418,6 @@ int refs_pack_refs(struct ref_store *refs, struct pack_refs_opts *opts); */ int refs_create_reflog(struct ref_store *refs, const char *refname, struct strbuf *err); -int safe_create_reflog(const char *refname, struct strbuf *err); /** * Reads log for the value of ref during at_time (in which case "cnt" should be @@ -470,7 +441,6 @@ int read_ref_at(struct ref_store *refs, /** Check if a particular reflog exists */ int refs_reflog_exists(struct ref_store *refs, const char *refname); -int reflog_exists(const char *refname); /* * Delete the specified reference. If old_oid is non-NULL, then @@ -484,8 +454,6 @@ int refs_delete_ref(struct ref_store *refs, const char *msg, const char *refname, const struct object_id *old_oid, unsigned int flags); -int delete_ref(const char *msg, const char *refname, - const struct object_id *old_oid, unsigned int flags); /* * Delete the specified references. If there are any problems, emit @@ -495,12 +463,9 @@ int delete_ref(const char *msg, const char *refname, */ int refs_delete_refs(struct ref_store *refs, const char *msg, struct string_list *refnames, unsigned int flags); -int delete_refs(const char *msg, struct string_list *refnames, - unsigned int flags); /** Delete a reflog */ int refs_delete_reflog(struct ref_store *refs, const char *refname); -int delete_reflog(const char *refname); /* * Callback to process a reflog entry found by the iteration functions (see @@ -546,17 +511,7 @@ int refs_for_each_reflog_ent_reverse(struct ref_store *refs, void *cb_data); /* - * Iterate over reflog entries in the log for `refname` in the main ref store. - */ - -/* oldest entry first */ -int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data); - -/* youngest entry first */ -int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data); - -/* - * The signature for the callback function for the {refs_,}for_each_reflog() + * The signature for the callback function for the refs_for_each_reflog() * functions below. The memory pointed to by the refname argument is only * guaranteed to be valid for the duration of a single callback invocation. */ @@ -567,7 +522,6 @@ typedef int each_reflog_fn(const char *refname, void *cb_data); * and returns the value. Reflog file order is unspecified. */ int refs_for_each_reflog(struct ref_store *refs, each_reflog_fn fn, void *cb_data); -int for_each_reflog(each_reflog_fn fn, void *cb_data); #define REFNAME_ALLOW_ONELEVEL 1 #define REFNAME_REFSPEC_PATTERN 2 @@ -592,23 +546,17 @@ const char *prettify_refname(const char *refname); char *refs_shorten_unambiguous_ref(struct ref_store *refs, const char *refname, int strict); -char *shorten_unambiguous_ref(const char *refname, int strict); /** rename ref, return 0 on success **/ int refs_rename_ref(struct ref_store *refs, const char *oldref, const char *newref, const char *logmsg); -int rename_ref(const char *oldref, const char *newref, - const char *logmsg); /** copy ref, return 0 on success **/ int refs_copy_existing_ref(struct ref_store *refs, const char *oldref, const char *newref, const char *logmsg); -int copy_existing_ref(const char *oldref, const char *newref, - const char *logmsg); -int refs_create_symref(struct ref_store *refs, const char *refname, +int refs_update_symref(struct ref_store *refs, const char *refname, const char *target, const char *logmsg); -int create_symref(const char *refname, const char *target, const char *logmsg); enum action_on_err { UPDATE_REFS_MSG_ON_ERR, @@ -622,7 +570,6 @@ enum action_on_err { */ struct ref_transaction *ref_store_transaction_begin(struct ref_store *refs, struct strbuf *err); -struct ref_transaction *ref_transaction_begin(struct strbuf *err); /* * Reference transaction updates @@ -648,6 +595,16 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err); * before the update. A copy of this value is made in the * transaction. * + * new_target -- the target reference that the reference will be + * updated to point to. If the reference is a regular reference, + * it will be converted to a symbolic reference. Cannot be set + * together with `new_oid`. A copy of this value is made in the + * transaction. + * + * old_target -- the reference that the reference must be pointing to. + * Canont be set together with `old_oid`. A copy of this value is + * made in the transaction. + * * flags -- flags affecting the update, passed to * update_ref_lock(). Possible flags: REF_NO_DEREF, * REF_FORCE_CREATE_REFLOG. See those constants for more @@ -713,7 +670,11 @@ struct ref_transaction *ref_transaction_begin(struct strbuf *err); * beforehand. The old value is checked after the lock is taken to * prevent races. If the old value doesn't agree with old_oid, the * whole transaction fails. If old_oid is NULL, then the previous - * value is not checked. + * value is not checked. If `old_target` is not NULL, treat the reference + * as a symbolic ref and validate that its target before the update is + * `old_target`. If the `new_target` is not NULL, then the reference + * will be updated to a symbolic ref which targets `new_target`. + * Together, these allow us to update between regular refs and symrefs. * * See the above comment "Reference transaction updates" for more * information. @@ -722,6 +683,8 @@ int ref_transaction_update(struct ref_transaction *transaction, const char *refname, const struct object_id *new_oid, const struct object_id *old_oid, + const char *new_target, + const char *old_target, unsigned int flags, const char *msg, struct strbuf *err); @@ -853,9 +816,6 @@ void ref_transaction_free(struct ref_transaction *transaction); int refs_update_ref(struct ref_store *refs, const char *msg, const char *refname, const struct object_id *new_oid, const struct object_id *old_oid, unsigned int flags, enum action_on_err onerr); -int update_ref(const char *msg, const char *refname, - const struct object_id *new_oid, const struct object_id *old_oid, - unsigned int flags, enum action_on_err onerr); int parse_hide_refs_config(const char *var, const char *value, const char *, struct strvec *); @@ -913,7 +873,7 @@ enum expire_reflog_flags { /* * The following interface is used for reflog expiration. The caller - * calls reflog_expire(), supplying it with three callback functions, + * calls refs_reflog_expire(), supplying it with three callback functions, * of the following types. The callback functions define the * expiration policy that is desired. * @@ -950,12 +910,6 @@ int refs_reflog_expire(struct ref_store *refs, reflog_expiry_should_prune_fn should_prune_fn, reflog_expiry_cleanup_fn cleanup_fn, void *policy_cb_data); -int reflog_expire(const char *refname, - unsigned int flags, - reflog_expiry_prepare_fn prepare_fn, - reflog_expiry_should_prune_fn should_prune_fn, - reflog_expiry_cleanup_fn cleanup_fn, - void *policy_cb_data); struct ref_store *get_main_ref_store(struct repository *r); @@ -1003,7 +957,8 @@ struct ref_store *get_main_ref_store(struct repository *r); * For backwards compatibility, submodule=="" is treated the same as * submodule==NULL. */ -struct ref_store *get_submodule_ref_store(const char *submodule); +struct ref_store *repo_get_submodule_ref_store(struct repository *repo, + const char *submodule); struct ref_store *get_worktree_ref_store(const struct worktree *wt); /* @@ -1051,7 +1006,258 @@ extern struct ref_namespace_info ref_namespace[NAMESPACE__COUNT]; */ void update_ref_namespace(enum ref_namespace namespace, char *ref); -int is_pseudoref(struct ref_store *refs, const char *refname); -int is_headref(struct ref_store *refs, const char *refname); +/* + * Check whether the provided name names a root reference. This function only + * performs a syntactic check. + * + * A root ref is a reference that lives in the root of the reference hierarchy. + * These references must conform to special syntax: + * + * - Their name must be all-uppercase or underscores ("_"). + * + * - Their name must end with "_HEAD". As a special rule, "HEAD" is a root + * ref, as well. + * + * - Their name may not contain a slash. + * + * There is a special set of irregular root refs that exist due to historic + * reasons, only. This list shall not be expanded in the future: + * + * - AUTO_MERGE + * + * - BISECT_EXPECTED_REV + * + * - NOTES_MERGE_PARTIAL + * + * - NOTES_MERGE_REF + * + * - MERGE_AUTOSTASH + */ +int is_root_ref(const char *refname); + +/* + * Pseudorefs are refs that have different semantics compared to + * "normal" refs. These refs can thus not be stored in the ref backend, + * but must always be accessed via the filesystem. The following refs + * are pseudorefs: + * + * - FETCH_HEAD may contain multiple object IDs, and each one of them + * carries additional metadata like where it came from. + * + * - MERGE_HEAD may contain multiple object IDs when merging multiple + * heads. + * + * Reading, writing or deleting references must consistently go either + * through the filesystem (pseudorefs) or through the reference + * backend (normal ones). + */ +int is_pseudo_ref(const char *refname); + +/* + * The following functions have been removed in Git v2.45 in favor of functions + * that receive a `ref_store` as parameter. The intent of this section is + * merely to help patch authors of in-flight series to have a reference what + * they should be migrating to. The section will be removed in Git v2.46. + */ +#if 0 +static char *resolve_refdup(const char *refname, int resolve_flags, + struct object_id *oid, int *flags) +{ + return refs_resolve_refdup(get_main_ref_store(the_repository), + refname, resolve_flags, + oid, flags); +} + +static int read_ref_full(const char *refname, int resolve_flags, + struct object_id *oid, int *flags) +{ + return refs_read_ref_full(get_main_ref_store(the_repository), refname, + resolve_flags, oid, flags); +} + +static int read_ref(const char *refname, struct object_id *oid) +{ + return refs_read_ref(get_main_ref_store(the_repository), refname, oid); +} + +static int ref_exists(const char *refname) +{ + return refs_ref_exists(get_main_ref_store(the_repository), refname); +} + +static int for_each_tag_ref(each_ref_fn fn, void *cb_data) +{ + return refs_for_each_tag_ref(get_main_ref_store(the_repository), fn, cb_data); +} + +static int for_each_branch_ref(each_ref_fn fn, void *cb_data) +{ + return refs_for_each_branch_ref(get_main_ref_store(the_repository), fn, cb_data); +} + +static int for_each_remote_ref(each_ref_fn fn, void *cb_data) +{ + return refs_for_each_remote_ref(get_main_ref_store(the_repository), fn, cb_data); +} + +static int head_ref_namespaced(each_ref_fn fn, void *cb_data) +{ + return refs_head_ref_namespaced(get_main_ref_store(the_repository), + fn, cb_data); +} + +static int for_each_glob_ref_in(each_ref_fn fn, const char *pattern, + const char *prefix, void *cb_data) +{ + return refs_for_each_glob_ref_in(get_main_ref_store(the_repository), + fn, pattern, prefix, cb_data); +} + +static int for_each_glob_ref(each_ref_fn fn, const char *pattern, void *cb_data) +{ + return refs_for_each_glob_ref(get_main_ref_store(the_repository), + fn, pattern, cb_data); +} + +static int delete_ref(const char *msg, const char *refname, + const struct object_id *old_oid, unsigned int flags) +{ + return refs_delete_ref(get_main_ref_store(the_repository), msg, refname, + old_oid, flags); +} + +static struct ref_transaction *ref_transaction_begin(struct strbuf *err) +{ + return ref_store_transaction_begin(get_main_ref_store(the_repository), err); +} + +static int update_ref(const char *msg, const char *refname, + const struct object_id *new_oid, + const struct object_id *old_oid, + unsigned int flags, enum action_on_err onerr) +{ + return refs_update_ref(get_main_ref_store(the_repository), msg, refname, new_oid, + old_oid, flags, onerr); +} + +static char *shorten_unambiguous_ref(const char *refname, int strict) +{ + return refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), + refname, strict); +} + +static int head_ref(each_ref_fn fn, void *cb_data) +{ + return refs_head_ref(get_main_ref_store(the_repository), fn, cb_data); +} + +static int for_each_ref(each_ref_fn fn, void *cb_data) +{ + return refs_for_each_ref(get_main_ref_store(the_repository), fn, cb_data); +} + +static int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data) +{ + return refs_for_each_ref_in(get_main_ref_store(the_repository), prefix, fn, cb_data); +} + +static int for_each_fullref_in(const char *prefix, + const char **exclude_patterns, + each_ref_fn fn, void *cb_data) +{ + return refs_for_each_fullref_in(get_main_ref_store(the_repository), + prefix, exclude_patterns, fn, cb_data); +} + +static int for_each_namespaced_ref(const char **exclude_patterns, + each_ref_fn fn, void *cb_data) +{ + return refs_for_each_namespaced_ref(get_main_ref_store(the_repository), + exclude_patterns, fn, cb_data); +} + +static int for_each_rawref(each_ref_fn fn, void *cb_data) +{ + return refs_for_each_rawref(get_main_ref_store(the_repository), fn, cb_data); +} + +static const char *resolve_ref_unsafe(const char *refname, int resolve_flags, + struct object_id *oid, int *flags) +{ + return refs_resolve_ref_unsafe(get_main_ref_store(the_repository), refname, + resolve_flags, oid, flags); +} + +static int create_symref(const char *ref_target, const char *refs_heads_master, + const char *logmsg) +{ + return refs_create_symref(get_main_ref_store(the_repository), ref_target, + refs_heads_master, logmsg); +} + +static int for_each_reflog(each_reflog_fn fn, void *cb_data) +{ + return refs_for_each_reflog(get_main_ref_store(the_repository), fn, cb_data); +} + +static int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, + void *cb_data) +{ + return refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), + refname, fn, cb_data); +} + +static int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, + void *cb_data) +{ + return refs_for_each_reflog_ent(get_main_ref_store(the_repository), refname, + fn, cb_data); +} + +static int reflog_exists(const char *refname) +{ + return refs_reflog_exists(get_main_ref_store(the_repository), refname); +} + +static int safe_create_reflog(const char *refname, struct strbuf *err) +{ + return refs_create_reflog(get_main_ref_store(the_repository), refname, + err); +} + +static int delete_reflog(const char *refname) +{ + return refs_delete_reflog(get_main_ref_store(the_repository), refname); +} + +static int reflog_expire(const char *refname, + unsigned int flags, + reflog_expiry_prepare_fn prepare_fn, + reflog_expiry_should_prune_fn should_prune_fn, + reflog_expiry_cleanup_fn cleanup_fn, + void *policy_cb_data) +{ + return refs_reflog_expire(get_main_ref_store(the_repository), + refname, flags, + prepare_fn, should_prune_fn, + cleanup_fn, policy_cb_data); +} + +static int delete_refs(const char *msg, struct string_list *refnames, + unsigned int flags) +{ + return refs_delete_refs(get_main_ref_store(the_repository), msg, refnames, flags); +} + +static int rename_ref(const char *oldref, const char *newref, const char *logmsg) +{ + return refs_rename_ref(get_main_ref_store(the_repository), oldref, newref, logmsg); +} + +static int copy_existing_ref(const char *oldref, const char *newref, const char *logmsg) +{ + return refs_copy_existing_ref(get_main_ref_store(the_repository), oldref, newref, logmsg); +} +#endif #endif /* REFS_H */ diff --git a/refs/debug.c b/refs/debug.c index c7531b1..547d924 100644 --- a/refs/debug.c +++ b/refs/debug.c @@ -33,11 +33,18 @@ struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_stor return (struct ref_store *)res; } -static int debug_init_db(struct ref_store *refs, int flags, struct strbuf *err) +static void debug_release(struct ref_store *refs) { struct debug_ref_store *drefs = (struct debug_ref_store *)refs; - int res = drefs->refs->be->init_db(drefs->refs, flags, err); - trace_printf_key(&trace_refs, "init_db: %d\n", res); + drefs->refs->be->release(drefs->refs); + trace_printf_key(&trace_refs, "release\n"); +} + +static int debug_create_on_disk(struct ref_store *refs, int flags, struct strbuf *err) +{ + struct debug_ref_store *drefs = (struct debug_ref_store *)refs; + int res = drefs->refs->be->create_on_disk(drefs->refs, flags, err); + trace_printf_key(&trace_refs, "create_on_disk: %d\n", res); return res; } @@ -131,18 +138,6 @@ static int debug_pack_refs(struct ref_store *ref_store, struct pack_refs_opts *o return res; } -static int debug_create_symref(struct ref_store *ref_store, - const char *ref_name, const char *target, - const char *logmsg) -{ - struct debug_ref_store *drefs = (struct debug_ref_store *)ref_store; - int res = drefs->refs->be->create_symref(drefs->refs, ref_name, target, - logmsg); - trace_printf_key(&trace_refs, "create_symref: %s -> %s \"%s\": %d\n", ref_name, - target, logmsg, res); - return res; -} - static int debug_rename_ref(struct ref_store *ref_store, const char *oldref, const char *newref, const char *logmsg) { @@ -427,7 +422,8 @@ static int debug_reflog_expire(struct ref_store *ref_store, const char *refname, struct ref_storage_be refs_be_debug = { .name = "debug", .init = NULL, - .init_db = debug_init_db, + .release = debug_release, + .create_on_disk = debug_create_on_disk, /* * None of these should be NULL. If the "files" backend (in @@ -441,7 +437,6 @@ struct ref_storage_be refs_be_debug = { .initial_transaction_commit = debug_initial_transaction_commit, .pack_refs = debug_pack_refs, - .create_symref = debug_create_symref, .rename_ref = debug_rename_ref, .copy_ref = debug_copy_ref, diff --git a/refs/files-backend.c b/refs/files-backend.c index a098d14..324c59b 100644 --- a/refs/files-backend.c +++ b/refs/files-backend.c @@ -89,9 +89,9 @@ static void clear_loose_ref_cache(struct files_ref_store *refs) * Create a new submodule ref cache and add it to the internal * set of caches. */ -static struct ref_store *files_ref_store_create(struct repository *repo, - const char *gitdir, - unsigned int flags) +static struct ref_store *files_ref_store_init(struct repository *repo, + const char *gitdir, + unsigned int flags) { struct files_ref_store *refs = xcalloc(1, sizeof(*refs)); struct ref_store *ref_store = (struct ref_store *)refs; @@ -102,7 +102,7 @@ static struct ref_store *files_ref_store_create(struct repository *repo, get_common_dir_noenv(&sb, gitdir); refs->gitcommondir = strbuf_detach(&sb, NULL); refs->packed_ref_store = - packed_ref_store_create(repo, refs->gitcommondir, flags); + packed_ref_store_init(repo, refs->gitcommondir, flags); chdir_notify_reparent("files-backend $GIT_DIR", &refs->base.gitdir); chdir_notify_reparent("files-backend $GIT_COMMONDIR", @@ -149,6 +149,14 @@ static struct files_ref_store *files_downcast(struct ref_store *ref_store, return refs; } +static void files_ref_store_release(struct ref_store *ref_store) +{ + struct files_ref_store *refs = files_downcast(ref_store, 0, "release"); + free_ref_cache(refs->loose); + free(refs->gitcommondir); + ref_store_release(refs->packed_ref_store); +} + static void files_reflog_path(struct files_ref_store *refs, struct strbuf *sb, const char *refname) @@ -351,8 +359,7 @@ static void add_pseudoref_and_head_entries(struct ref_store *ref_store, strbuf_addstr(&refname, de->d_name); dtype = get_dtype(de, &path, 1); - if (dtype == DT_REG && (is_pseudoref(ref_store, de->d_name) || - is_headref(ref_store, de->d_name))) + if (dtype == DT_REG && is_root_ref(de->d_name)) loose_fill_ref_dir_regular_file(refs, refname.buf, dir); strbuf_setlen(&refname, dirnamelen); @@ -794,8 +801,10 @@ retry: */ if (refs_verify_refname_available( refs->packed_ref_store, refname, - extras, NULL, err)) + extras, NULL, err)) { + ret = TRANSACTION_NAME_CONFLICT; goto error_return; + } } ret = 0; @@ -1198,7 +1207,7 @@ static void prune_ref(struct files_ref_store *refs, struct ref_to_prune *r) ref_transaction_add_update( transaction, r->name, REF_NO_DEREF | REF_HAVE_NEW | REF_HAVE_OLD | REF_IS_PRUNING, - null_oid(), &r->oid, NULL); + null_oid(), &r->oid, NULL, NULL, NULL); if (ref_transaction_commit(transaction, &err)) goto cleanup; @@ -1229,7 +1238,8 @@ static void prune_refs(struct files_ref_store *refs, struct ref_to_prune **refs_ /* * Return true if the specified reference should be packed. */ -static int should_pack_ref(const char *refname, +static int should_pack_ref(struct files_ref_store *refs, + const char *refname, const struct object_id *oid, unsigned int ref_flags, struct pack_refs_opts *opts) { @@ -1245,7 +1255,7 @@ static int should_pack_ref(const char *refname, return 0; /* Do not pack broken refs: */ - if (!ref_resolves_to_object(refname, the_repository, oid, ref_flags)) + if (!ref_resolves_to_object(refname, refs->base.repo, oid, ref_flags)) return 0; if (ref_excluded(opts->exclusions, refname)) @@ -1277,14 +1287,14 @@ static int files_pack_refs(struct ref_store *ref_store, packed_refs_lock(refs->packed_ref_store, LOCK_DIE_ON_ERROR, &err); iter = cache_ref_iterator_begin(get_loose_ref_cache(refs, 0), NULL, - the_repository, 0); + refs->base.repo, 0); while ((ok = ref_iterator_advance(iter)) == ITER_OK) { /* * If the loose reference can be packed, add an entry * in the packed ref cache. If the reference should be * pruned, also add it to refs_to_prune. */ - if (!should_pack_ref(iter->refname, iter->oid, iter->flags, opts)) + if (!should_pack_ref(refs, iter->refname, iter->oid, iter->flags, opts)) continue; /* @@ -1292,7 +1302,7 @@ static int files_pack_refs(struct ref_store *ref_store, * packed-refs transaction: */ if (ref_transaction_update(transaction, iter->refname, - iter->oid, NULL, + iter->oid, NULL, NULL, NULL, REF_NO_DEREF, NULL, &err)) die("failure preparing to create packed reference %s: %s", iter->refname, err.buf); @@ -1381,7 +1391,8 @@ static int rename_tmp_log(struct files_ref_store *refs, const char *newrefname) return ret; } -static int write_ref_to_lockfile(struct ref_lock *lock, +static int write_ref_to_lockfile(struct files_ref_store *refs, + struct ref_lock *lock, const struct object_id *oid, int skip_oid_verification, struct strbuf *err); static int commit_ref_update(struct files_ref_store *refs, @@ -1529,7 +1540,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store, } oidcpy(&lock->old_oid, &orig_oid); - if (write_ref_to_lockfile(lock, &orig_oid, 0, &err) || + if (write_ref_to_lockfile(refs, lock, &orig_oid, 0, &err) || commit_ref_update(refs, lock, &orig_oid, logmsg, &err)) { error("unable to write current sha1 into %s: %s", newrefname, err.buf); strbuf_release(&err); @@ -1549,7 +1560,7 @@ static int files_copy_or_rename_ref(struct ref_store *ref_store, flag = log_all_ref_updates; log_all_ref_updates = LOG_REFS_NONE; - if (write_ref_to_lockfile(lock, &orig_oid, 0, &err) || + if (write_ref_to_lockfile(refs, lock, &orig_oid, 0, &err) || commit_ref_update(refs, lock, &orig_oid, NULL, &err)) { error("unable to write current sha1 into %s: %s", oldrefname, err.buf); strbuf_release(&err); @@ -1783,7 +1794,8 @@ static int files_log_ref_write(struct files_ref_store *refs, * Write oid into the open lockfile, then close the lockfile. On * errors, rollback the lockfile, fill in *err and return -1. */ -static int write_ref_to_lockfile(struct ref_lock *lock, +static int write_ref_to_lockfile(struct files_ref_store *refs, + struct ref_lock *lock, const struct object_id *oid, int skip_oid_verification, struct strbuf *err) { @@ -1792,7 +1804,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock, int fd; if (!skip_oid_verification) { - o = parse_object(the_repository, oid); + o = parse_object(refs->base.repo, oid); if (!o) { strbuf_addf( err, @@ -1811,7 +1823,7 @@ static int write_ref_to_lockfile(struct ref_lock *lock, } } fd = get_lock_file_fd(&lock->lk); - if (write_in_full(fd, oid_to_hex(oid), the_hash_algo->hexsz) < 0 || + if (write_in_full(fd, oid_to_hex(oid), refs->base.repo->hash_algo->hexsz) < 0 || write_in_full(fd, &term, 1) < 0 || fsync_component(FSYNC_COMPONENT_REFERENCE, get_lock_file_fd(&lock->lk)) < 0 || close_ref_gently(lock) < 0) { @@ -1903,66 +1915,23 @@ static int create_ref_symlink(struct ref_lock *lock, const char *target) return ret; } -static void update_symref_reflog(struct files_ref_store *refs, - struct ref_lock *lock, const char *refname, - const char *target, const char *logmsg) +static int create_symref_lock(struct files_ref_store *refs, + struct ref_lock *lock, const char *refname, + const char *target, struct strbuf *err) { - struct strbuf err = STRBUF_INIT; - struct object_id new_oid; - - if (logmsg && - refs_resolve_ref_unsafe(&refs->base, target, - RESOLVE_REF_READING, &new_oid, NULL) && - files_log_ref_write(refs, refname, &lock->old_oid, - &new_oid, logmsg, 0, &err)) { - error("%s", err.buf); - strbuf_release(&err); - } -} - -static int create_symref_locked(struct files_ref_store *refs, - struct ref_lock *lock, const char *refname, - const char *target, const char *logmsg) -{ - if (prefer_symlink_refs && !create_ref_symlink(lock, target)) { - update_symref_reflog(refs, lock, refname, target, logmsg); - return 0; + if (!fdopen_lock_file(&lock->lk, "w")) { + strbuf_addf(err, "unable to fdopen %s: %s", + get_lock_file_path(&lock->lk), strerror(errno)); + return -1; } - if (!fdopen_lock_file(&lock->lk, "w")) - return error("unable to fdopen %s: %s", + if (fprintf(get_lock_file_fp(&lock->lk), "ref: %s\n", target) < 0) { + strbuf_addf(err, "unable to write to %s: %s", get_lock_file_path(&lock->lk), strerror(errno)); - - update_symref_reflog(refs, lock, refname, target, logmsg); - - /* no error check; commit_ref will check ferror */ - fprintf(get_lock_file_fp(&lock->lk), "ref: %s\n", target); - if (commit_ref(lock) < 0) - return error("unable to write symref for %s: %s", refname, - strerror(errno)); - return 0; -} - -static int files_create_symref(struct ref_store *ref_store, - const char *refname, const char *target, - const char *logmsg) -{ - struct files_ref_store *refs = - files_downcast(ref_store, REF_STORE_WRITE, "create_symref"); - struct strbuf err = STRBUF_INIT; - struct ref_lock *lock; - int ret; - - lock = lock_ref_oid_basic(refs, refname, &err); - if (!lock) { - error("%s", err.buf); - strbuf_release(&err); return -1; } - ret = create_symref_locked(refs, lock, refname, target, logmsg); - unlock_ref(lock); - return ret; + return 0; } static int files_reflog_exists(struct ref_store *ref_store, @@ -2309,7 +2278,7 @@ static int split_head_update(struct ref_update *update, transaction, "HEAD", update->flags | REF_LOG_ONLY | REF_NO_DEREF, &update->new_oid, &update->old_oid, - update->msg); + NULL, NULL, update->msg); /* * Add "HEAD". This insertion is O(N) in the transaction @@ -2371,8 +2340,9 @@ static int split_symref_update(struct ref_update *update, new_update = ref_transaction_add_update( transaction, referent, new_flags, - &update->new_oid, &update->old_oid, - update->msg); + update->new_target ? NULL : &update->new_oid, + update->old_target ? NULL : &update->old_oid, + update->new_target, update->old_target, update->msg); new_update->parent_update = update; @@ -2401,17 +2371,6 @@ static int split_symref_update(struct ref_update *update, } /* - * Return the refname under which update was originally requested. - */ -static const char *original_update_refname(struct ref_update *update) -{ - while (update->parent_update) - update = update->parent_update; - - return update->refname; -} - -/* * Check whether the REF_HAVE_OLD and old_oid values stored in update * are consistent with oid, which is the reference's current value. If * everything is OK, return 0; otherwise, write an error message to @@ -2427,16 +2386,16 @@ static int check_old_oid(struct ref_update *update, struct object_id *oid, if (is_null_oid(&update->old_oid)) strbuf_addf(err, "cannot lock ref '%s': " "reference already exists", - original_update_refname(update)); + ref_update_original_update_refname(update)); else if (is_null_oid(oid)) strbuf_addf(err, "cannot lock ref '%s': " "reference is missing but expected %s", - original_update_refname(update), + ref_update_original_update_refname(update), oid_to_hex(&update->old_oid)); else strbuf_addf(err, "cannot lock ref '%s': " "is at %s but expected %s", - original_update_refname(update), + ref_update_original_update_refname(update), oid_to_hex(oid), oid_to_hex(&update->old_oid)); @@ -2471,7 +2430,7 @@ static int lock_ref_for_update(struct files_ref_store *refs, files_assert_main_repository(refs, "lock_ref_for_update"); - if ((update->flags & REF_HAVE_NEW) && is_null_oid(&update->new_oid)) + if ((update->flags & REF_HAVE_NEW) && ref_update_has_null_new_value(update)) update->flags |= REF_DELETING; if (head_ref) { @@ -2490,7 +2449,7 @@ static int lock_ref_for_update(struct files_ref_store *refs, reason = strbuf_detach(err, NULL); strbuf_addf(err, "cannot lock ref '%s': %s", - original_update_refname(update), reason); + ref_update_original_update_refname(update), reason); free(reason); goto out; } @@ -2510,11 +2469,18 @@ static int lock_ref_for_update(struct files_ref_store *refs, if (update->flags & REF_HAVE_OLD) { strbuf_addf(err, "cannot lock ref '%s': " "error reading reference", - original_update_refname(update)); + ref_update_original_update_refname(update)); + ret = TRANSACTION_GENERIC_ERROR; + goto out; + } + } + + if (update->old_target) { + if (ref_update_check_old_target(referent.buf, update, err)) { ret = TRANSACTION_GENERIC_ERROR; goto out; } - } else if (check_old_oid(update, &lock->old_oid, err)) { + } else if (check_old_oid(update, &lock->old_oid, err)) { ret = TRANSACTION_GENERIC_ERROR; goto out; } @@ -2535,7 +2501,17 @@ static int lock_ref_for_update(struct files_ref_store *refs, } else { struct ref_update *parent_update; - if (check_old_oid(update, &lock->old_oid, err)) { + /* + * Even if the ref is a regular ref, if `old_target` is set, we + * check the referent value. Ideally `old_target` should only + * be set for symrefs, but we're strict about its usage. + */ + if (update->old_target) { + if (ref_update_check_old_target(referent.buf, update, err)) { + ret = TRANSACTION_GENERIC_ERROR; + goto out; + } + } else if (check_old_oid(update, &lock->old_oid, err)) { ret = TRANSACTION_GENERIC_ERROR; goto out; } @@ -2553,9 +2529,28 @@ static int lock_ref_for_update(struct files_ref_store *refs, } } - if ((update->flags & REF_HAVE_NEW) && - !(update->flags & REF_DELETING) && - !(update->flags & REF_LOG_ONLY)) { + if (update->new_target && !(update->flags & REF_LOG_ONLY)) { + if (create_symref_lock(refs, lock, update->refname, + update->new_target, err)) { + ret = TRANSACTION_GENERIC_ERROR; + goto out; + } + + if (close_ref_gently(lock)) { + strbuf_addf(err, "couldn't close '%s.lock'", + update->refname); + ret = TRANSACTION_GENERIC_ERROR; + goto out; + } + + /* + * Once we have created the symref lock, the commit + * phase of the transaction only needs to commit the lock. + */ + update->flags |= REF_NEEDS_COMMIT; + } else if ((update->flags & REF_HAVE_NEW) && + !(update->flags & REF_DELETING) && + !(update->flags & REF_LOG_ONLY)) { if (!(update->type & REF_ISSYMREF) && oideq(&lock->old_oid, &update->new_oid)) { /* @@ -2563,7 +2558,7 @@ static int lock_ref_for_update(struct files_ref_store *refs, * value, so we don't need to write it. */ } else if (write_ref_to_lockfile( - lock, &update->new_oid, + refs, lock, &update->new_oid, update->flags & REF_SKIP_OID_VERIFICATION, err)) { char *write_err = strbuf_detach(err, NULL); @@ -2763,7 +2758,7 @@ static int files_transaction_prepare(struct ref_store *ref_store, packed_transaction, update->refname, REF_HAVE_NEW | REF_NO_DEREF, &update->new_oid, NULL, - NULL); + NULL, NULL, NULL); } } @@ -2818,6 +2813,43 @@ cleanup: return ret; } +static int parse_and_write_reflog(struct files_ref_store *refs, + struct ref_update *update, + struct ref_lock *lock, + struct strbuf *err) +{ + if (update->new_target) { + /* + * We want to get the resolved OID for the target, to ensure + * that the correct value is added to the reflog. + */ + if (!refs_resolve_ref_unsafe(&refs->base, update->new_target, + RESOLVE_REF_READING, + &update->new_oid, NULL)) { + /* + * TODO: currently we skip creating reflogs for dangling + * symref updates. It would be nice to capture this as + * zero oid updates however. + */ + return 0; + } + } + + if (files_log_ref_write(refs, lock->ref_name, &lock->old_oid, + &update->new_oid, update->msg, update->flags, err)) { + char *old_msg = strbuf_detach(err, NULL); + + strbuf_addf(err, "cannot update the ref '%s': %s", + lock->ref_name, old_msg); + free(old_msg); + unlock_ref(lock); + update->backend_data = NULL; + return -1; + } + + return 0; +} + static int files_transaction_finish(struct ref_store *ref_store, struct ref_transaction *transaction, struct strbuf *err) @@ -2848,23 +2880,20 @@ static int files_transaction_finish(struct ref_store *ref_store, if (update->flags & REF_NEEDS_COMMIT || update->flags & REF_LOG_ONLY) { - if (files_log_ref_write(refs, - lock->ref_name, - &lock->old_oid, - &update->new_oid, - update->msg, update->flags, - err)) { - char *old_msg = strbuf_detach(err, NULL); - - strbuf_addf(err, "cannot update the ref '%s': %s", - lock->ref_name, old_msg); - free(old_msg); - unlock_ref(lock); - update->backend_data = NULL; + if (parse_and_write_reflog(refs, update, lock, err)) { ret = TRANSACTION_GENERIC_ERROR; goto cleanup; } } + + /* + * We try creating a symlink, if that succeeds we continue to the + * next update. If not, we try and create a regular symref. + */ + if (update->new_target && prefer_symlink_refs) + if (!create_ref_symlink(lock, update->new_target)) + continue; + if (update->flags & REF_NEEDS_COMMIT) { clear_loose_ref_cache(refs); if (commit_ref(lock)) { @@ -3048,7 +3077,7 @@ static int files_initial_transaction_commit(struct ref_store *ref_store, ref_transaction_add_update(packed_transaction, update->refname, update->flags & ~REF_HAVE_OLD, &update->new_oid, &update->old_oid, - NULL); + NULL, NULL, NULL); } if (packed_refs_lock(refs->packed_ref_store, 0, err)) { @@ -3212,7 +3241,7 @@ static int files_reflog_expire(struct ref_store *ref_store, rollback_lock_file(&reflog_lock); } else if (update && (write_in_full(get_lock_file_fd(&lock->lk), - oid_to_hex(&cb.last_kept_oid), the_hash_algo->hexsz) < 0 || + oid_to_hex(&cb.last_kept_oid), refs->base.repo->hash_algo->hexsz) < 0 || write_str_in_full(get_lock_file_fd(&lock->lk), "\n") < 0 || close_ref_gently(lock) < 0)) { status |= error("couldn't write %s", @@ -3236,12 +3265,12 @@ static int files_reflog_expire(struct ref_store *ref_store, return -1; } -static int files_init_db(struct ref_store *ref_store, - int flags, - struct strbuf *err UNUSED) +static int files_ref_store_create_on_disk(struct ref_store *ref_store, + int flags, + struct strbuf *err UNUSED) { struct files_ref_store *refs = - files_downcast(ref_store, REF_STORE_WRITE, "init_db"); + files_downcast(ref_store, REF_STORE_WRITE, "create"); struct strbuf sb = STRBUF_INIT; /* @@ -3264,7 +3293,7 @@ static int files_init_db(struct ref_store *ref_store, * There is no need to create directories for common refs when creating * a worktree ref store. */ - if (!(flags & REFS_INIT_DB_IS_WORKTREE)) { + if (!(flags & REF_STORE_CREATE_ON_DISK_IS_WORKTREE)) { /* * Create .git/refs/{heads,tags} */ @@ -3283,15 +3312,16 @@ static int files_init_db(struct ref_store *ref_store, struct ref_storage_be refs_be_files = { .name = "files", - .init = files_ref_store_create, - .init_db = files_init_db, + .init = files_ref_store_init, + .release = files_ref_store_release, + .create_on_disk = files_ref_store_create_on_disk, + .transaction_prepare = files_transaction_prepare, .transaction_finish = files_transaction_finish, .transaction_abort = files_transaction_abort, .initial_transaction_commit = files_initial_transaction_commit, .pack_refs = files_pack_refs, - .create_symref = files_create_symref, .rename_ref = files_rename_ref, .copy_ref = files_copy_ref, diff --git a/refs/iterator.c b/refs/iterator.c index 9db8b05..d355ebf 100644 --- a/refs/iterator.c +++ b/refs/iterator.c @@ -440,15 +440,15 @@ struct ref_iterator *prefix_ref_iterator_begin(struct ref_iterator *iter0, struct ref_iterator *current_ref_iter = NULL; -int do_for_each_repo_ref_iterator(struct repository *r, struct ref_iterator *iter, - each_repo_ref_fn fn, void *cb_data) +int do_for_each_ref_iterator(struct ref_iterator *iter, + each_ref_fn fn, void *cb_data) { int retval = 0, ok; struct ref_iterator *old_ref_iter = current_ref_iter; current_ref_iter = iter; while ((ok = ref_iterator_advance(iter)) == ITER_OK) { - retval = fn(r, iter->refname, iter->oid, iter->flags, cb_data); + retval = fn(iter->refname, iter->oid, iter->flags, cb_data); if (retval) { /* * If ref_iterator_abort() returns ITER_ERROR, diff --git a/refs/packed-backend.c b/refs/packed-backend.c index 4e826c0..2789fd9 100644 --- a/refs/packed-backend.c +++ b/refs/packed-backend.c @@ -200,9 +200,14 @@ static int release_snapshot(struct snapshot *snapshot) } } -struct ref_store *packed_ref_store_create(struct repository *repo, - const char *gitdir, - unsigned int store_flags) +static size_t snapshot_hexsz(const struct snapshot *snapshot) +{ + return snapshot->refs->base.repo->hash_algo->hexsz; +} + +struct ref_store *packed_ref_store_init(struct repository *repo, + const char *gitdir, + unsigned int store_flags) { struct packed_ref_store *refs = xcalloc(1, sizeof(*refs)); struct ref_store *ref_store = (struct ref_store *)refs; @@ -252,6 +257,15 @@ static void clear_snapshot(struct packed_ref_store *refs) } } +static void packed_ref_store_release(struct ref_store *ref_store) +{ + struct packed_ref_store *refs = packed_downcast(ref_store, 0, "release"); + clear_snapshot(refs); + rollback_lock_file(&refs->lock); + delete_tempfile(&refs->tempfile); + free(refs->path); +} + static NORETURN void die_unterminated_line(const char *path, const char *p, size_t len) { @@ -280,11 +294,13 @@ struct snapshot_record { size_t len; }; -static int cmp_packed_ref_records(const void *v1, const void *v2) +static int cmp_packed_ref_records(const void *v1, const void *v2, + void *cb_data) { + const struct snapshot *snapshot = cb_data; const struct snapshot_record *e1 = v1, *e2 = v2; - const char *r1 = e1->start + the_hash_algo->hexsz + 1; - const char *r2 = e2->start + the_hash_algo->hexsz + 1; + const char *r1 = e1->start + snapshot_hexsz(snapshot) + 1; + const char *r2 = e2->start + snapshot_hexsz(snapshot) + 1; while (1) { if (*r1 == '\n') @@ -305,9 +321,9 @@ static int cmp_packed_ref_records(const void *v1, const void *v2) * refname. */ static int cmp_record_to_refname(const char *rec, const char *refname, - int start) + int start, const struct snapshot *snapshot) { - const char *r1 = rec + the_hash_algo->hexsz + 1; + const char *r1 = rec + snapshot_hexsz(snapshot) + 1; const char *r2 = refname; while (1) { @@ -354,7 +370,7 @@ static void sort_snapshot(struct snapshot *snapshot) if (!eol) /* The safety check should prevent this. */ BUG("unterminated line found in packed-refs"); - if (eol - pos < the_hash_algo->hexsz + 2) + if (eol - pos < snapshot_hexsz(snapshot) + 2) die_invalid_line(snapshot->refs->path, pos, eof - pos); eol++; @@ -380,7 +396,7 @@ static void sort_snapshot(struct snapshot *snapshot) if (sorted && nr > 1 && cmp_packed_ref_records(&records[nr - 2], - &records[nr - 1]) >= 0) + &records[nr - 1], snapshot) >= 0) sorted = 0; pos = eol; @@ -390,7 +406,7 @@ static void sort_snapshot(struct snapshot *snapshot) goto cleanup; /* We need to sort the memory. First we sort the records array: */ - QSORT(records, nr, cmp_packed_ref_records); + QSORT_S(records, nr, cmp_packed_ref_records, snapshot); /* * Allocate a new chunk of memory, and copy the old memory to @@ -466,7 +482,8 @@ static void verify_buffer_safe(struct snapshot *snapshot) return; last_line = find_start_of_record(start, eof - 1); - if (*(eof - 1) != '\n' || eof - last_line < the_hash_algo->hexsz + 2) + if (*(eof - 1) != '\n' || + eof - last_line < snapshot_hexsz(snapshot) + 2) die_invalid_line(snapshot->refs->path, last_line, eof - last_line); } @@ -561,7 +578,7 @@ static const char *find_reference_location_1(struct snapshot *snapshot, mid = lo + (hi - lo) / 2; rec = find_start_of_record(lo, mid); - cmp = cmp_record_to_refname(rec, refname, start); + cmp = cmp_record_to_refname(rec, refname, start, snapshot); if (cmp < 0) { lo = find_end_of_record(mid, hi); } else if (cmp > 0) { @@ -858,7 +875,7 @@ static int next_record(struct packed_ref_iterator *iter) iter->base.flags = REF_ISPACKED; p = iter->pos; - if (iter->eof - p < the_hash_algo->hexsz + 2 || + if (iter->eof - p < snapshot_hexsz(iter->snapshot) + 2 || parse_oid_hex(p, &iter->oid, &p) || !isspace(*p++)) die_invalid_line(iter->snapshot->refs->path, @@ -888,7 +905,7 @@ static int next_record(struct packed_ref_iterator *iter) if (iter->pos < iter->eof && *iter->pos == '^') { p = iter->pos + 1; - if (iter->eof - p < the_hash_algo->hexsz + 1 || + if (iter->eof - p < snapshot_hexsz(iter->snapshot) + 1 || parse_oid_hex(p, &iter->peeled, &p) || *p++ != '\n') die_invalid_line(iter->snapshot->refs->path, @@ -944,16 +961,13 @@ static int packed_ref_iterator_peel(struct ref_iterator *ref_iterator, struct packed_ref_iterator *iter = (struct packed_ref_iterator *)ref_iterator; - if (iter->repo != the_repository) - BUG("peeling for non-the_repository is not supported"); - if ((iter->base.flags & REF_KNOWS_PEELED)) { oidcpy(peeled, &iter->peeled); return is_null_oid(&iter->peeled) ? -1 : 0; } else if ((iter->base.flags & (REF_ISBROKEN | REF_ISSYMREF))) { return -1; } else { - return peel_object(&iter->oid, peeled) ? -1 : 0; + return peel_object(iter->repo, &iter->oid, peeled) ? -1 : 0; } } @@ -1244,9 +1258,9 @@ int packed_refs_is_locked(struct ref_store *ref_store) static const char PACKED_REFS_HEADER[] = "# pack-refs with: peeled fully-peeled sorted \n"; -static int packed_init_db(struct ref_store *ref_store UNUSED, - int flags UNUSED, - struct strbuf *err UNUSED) +static int packed_ref_store_create_on_disk(struct ref_store *ref_store UNUSED, + int flags UNUSED, + struct strbuf *err UNUSED) { /* Nothing to do. */ return 0; @@ -1412,7 +1426,8 @@ static int write_with_updates(struct packed_ref_store *refs, i++; } else { struct object_id peeled; - int peel_error = peel_object(&update->new_oid, + int peel_error = peel_object(refs->base.repo, + &update->new_oid, &peeled); if (write_packed_entry(out, update->refname, @@ -1706,15 +1721,16 @@ static struct ref_iterator *packed_reflog_iterator_begin(struct ref_store *ref_s struct ref_storage_be refs_be_packed = { .name = "packed", - .init = packed_ref_store_create, - .init_db = packed_init_db, + .init = packed_ref_store_init, + .release = packed_ref_store_release, + .create_on_disk = packed_ref_store_create_on_disk, + .transaction_prepare = packed_transaction_prepare, .transaction_finish = packed_transaction_finish, .transaction_abort = packed_transaction_abort, .initial_transaction_commit = packed_initial_transaction_commit, .pack_refs = packed_pack_refs, - .create_symref = NULL, .rename_ref = NULL, .copy_ref = NULL, diff --git a/refs/packed-backend.h b/refs/packed-backend.h index 9dd8a34..09437ad 100644 --- a/refs/packed-backend.h +++ b/refs/packed-backend.h @@ -13,9 +13,9 @@ struct ref_transaction; * even among packed refs. */ -struct ref_store *packed_ref_store_create(struct repository *repo, - const char *gitdir, - unsigned int store_flags); +struct ref_store *packed_ref_store_init(struct repository *repo, + const char *gitdir, + unsigned int store_flags); /* * Lock the packed-refs file for writing. Flags is passed to diff --git a/refs/ref-cache.c b/refs/ref-cache.c index 9f97972..b6c53fc 100644 --- a/refs/ref-cache.c +++ b/refs/ref-cache.c @@ -441,10 +441,7 @@ static int cache_ref_iterator_peel(struct ref_iterator *ref_iterator, { struct cache_ref_iterator *iter = (struct cache_ref_iterator *)ref_iterator; - - if (iter->repo != the_repository) - BUG("peeling for non-the_repository is not supported"); - return peel_object(ref_iterator->oid, peeled) ? -1 : 0; + return peel_object(iter->repo, ref_iterator->oid, peeled) ? -1 : 0; } static int cache_ref_iterator_abort(struct ref_iterator *ref_iterator) diff --git a/refs/refs-internal.h b/refs/refs-internal.h index 56641aa..33749fb 100644 --- a/refs/refs-internal.h +++ b/refs/refs-internal.h @@ -69,40 +69,6 @@ int ref_resolves_to_object(const char *refname, const struct object_id *oid, unsigned int flags); -enum peel_status { - /* object was peeled successfully: */ - PEEL_PEELED = 0, - - /* - * object cannot be peeled because the named object (or an - * object referred to by a tag in the peel chain), does not - * exist. - */ - PEEL_INVALID = -1, - - /* object cannot be peeled because it is not a tag: */ - PEEL_NON_TAG = -2, - - /* ref_entry contains no peeled value because it is a symref: */ - PEEL_IS_SYMREF = -3, - - /* - * ref_entry cannot be peeled because it is broken (i.e., the - * symbolic reference cannot even be resolved to an object - * name): - */ - PEEL_BROKEN = -4 -}; - -/* - * Peel the named object; i.e., if the object is a tag, resolve the - * tag recursively until a non-tag is found. If successful, store the - * result to oid and return PEEL_PEELED. If the object is not a tag - * or is not valid, return PEEL_NON_TAG or PEEL_INVALID, respectively, - * and leave oid unchanged. - */ -enum peel_status peel_object(const struct object_id *name, struct object_id *oid); - /** * Information needed for a single ref update. Set new_oid to the new * value or to null_oid to delete the ref. To check the old value @@ -125,6 +91,19 @@ struct ref_update { struct object_id old_oid; /* + * If set, point the reference to this value. This can also be + * used to convert regular references to become symbolic refs. + * Cannot be set together with `new_oid`. + */ + const char *new_target; + + /* + * If set, check that the reference previously pointed to this + * value. Cannot be set together with `old_oid`. + */ + const char *old_target; + + /* * One or more of REF_NO_DEREF, REF_FORCE_CREATE_REFLOG, * REF_HAVE_NEW, REF_HAVE_OLD, or backend-specific flags. */ @@ -173,6 +152,7 @@ struct ref_update *ref_transaction_add_update( const char *refname, unsigned int flags, const struct object_id *new_oid, const struct object_id *old_oid, + const char *new_target, const char *old_target, const char *msg); /* @@ -503,9 +483,8 @@ extern struct ref_iterator *current_ref_iter; * adapter between the callback style of reference iteration and the * iterator style. */ -int do_for_each_repo_ref_iterator(struct repository *r, - struct ref_iterator *iter, - each_repo_ref_fn fn, void *cb_data); +int do_for_each_ref_iterator(struct ref_iterator *iter, + each_ref_fn fn, void *cb_data); struct ref_store; @@ -529,10 +508,14 @@ struct ref_store; typedef struct ref_store *ref_store_init_fn(struct repository *repo, const char *gitdir, unsigned int flags); +/* + * Release all memory and resources associated with the ref store. + */ +typedef void ref_store_release_fn(struct ref_store *refs); -typedef int ref_init_db_fn(struct ref_store *refs, - int flags, - struct strbuf *err); +typedef int ref_store_create_on_disk_fn(struct ref_store *refs, + int flags, + struct strbuf *err); typedef int ref_transaction_prepare_fn(struct ref_store *refs, struct ref_transaction *transaction, @@ -552,10 +535,6 @@ typedef int ref_transaction_commit_fn(struct ref_store *refs, typedef int pack_refs_fn(struct ref_store *ref_store, struct pack_refs_opts *opts); -typedef int create_symref_fn(struct ref_store *ref_store, - const char *ref_target, - const char *refs_heads_master, - const char *logmsg); typedef int rename_ref_fn(struct ref_store *ref_store, const char *oldref, const char *newref, const char *logmsg); @@ -668,7 +647,8 @@ typedef int read_symbolic_ref_fn(struct ref_store *ref_store, const char *refnam struct ref_storage_be { const char *name; ref_store_init_fn *init; - ref_init_db_fn *init_db; + ref_store_release_fn *release; + ref_store_create_on_disk_fn *create_on_disk; ref_transaction_prepare_fn *transaction_prepare; ref_transaction_finish_fn *transaction_finish; @@ -676,7 +656,6 @@ struct ref_storage_be { ref_transaction_commit_fn *initial_transaction_commit; pack_refs_fn *pack_refs; - create_symref_fn *create_symref; rename_ref_fn *rename_ref; copy_ref_fn *copy_ref; @@ -700,7 +679,7 @@ extern struct ref_storage_be refs_be_packed; /* * A representation of the reference store for the main repository or * a submodule. The ref_store instances for submodules are kept in a - * hash map; see get_submodule_ref_store() for more info. + * hash map; see repo_get_submodule_ref_store() for more info. */ struct ref_store { /* The backend describing this ref_store's storage scheme: */ @@ -735,4 +714,25 @@ void base_ref_store_init(struct ref_store *refs, struct repository *repo, */ struct ref_store *maybe_debug_wrap_ref_store(const char *gitdir, struct ref_store *store); +/* + * Return the refname under which update was originally requested. + */ +const char *ref_update_original_update_refname(struct ref_update *update); + +/* + * Helper function to check if the new value is null, this + * takes into consideration that the update could be a regular + * ref or a symbolic ref. + */ +int ref_update_has_null_new_value(struct ref_update *update); + +/* + * Check whether the old_target values stored in update are consistent + * with the referent, which is the symbolic reference's current value. + * If everything is OK, return 0; otherwise, write an error message to + * err and return -1. + */ +int ref_update_check_old_target(const char *referent, struct ref_update *update, + struct strbuf *err); + #endif /* REFS_REFS_INTERNAL_H */ diff --git a/refs/reftable-backend.c b/refs/reftable-backend.c index 010ef81..438b5c4 100644 --- a/refs/reftable-backend.c +++ b/refs/reftable-backend.c @@ -1,6 +1,7 @@ #include "../git-compat-util.h" #include "../abspath.h" #include "../chdir-notify.h" +#include "../config.h" #include "../environment.h" #include "../gettext.h" #include "../hash.h" @@ -15,7 +16,6 @@ #include "../reftable/reftable-record.h" #include "../reftable/reftable-error.h" #include "../reftable/reftable-iterator.h" -#include "../reftable/reftable-merged.h" #include "../setup.h" #include "../strmap.h" #include "parse.h" @@ -129,7 +129,7 @@ static struct reftable_stack *stack_for(struct reftable_ref_store *store, store->base.repo->commondir, wtname_buf.buf); store->err = reftable_new_stack(&stack, wt_dir.buf, - store->write_options); + &store->write_options); assert(store->err != REFTABLE_API_ERROR); strmap_put(&store->worktree_stacks, wtname_buf.buf, stack); } @@ -228,6 +228,34 @@ done: return ret; } +static int reftable_be_config(const char *var, const char *value, + const struct config_context *ctx, + void *_opts) +{ + struct reftable_write_options *opts = _opts; + + if (!strcmp(var, "reftable.blocksize")) { + unsigned long block_size = git_config_ulong(var, value, ctx->kvi); + if (block_size > 16777215) + die("reftable block size cannot exceed 16MB"); + opts->block_size = block_size; + } else if (!strcmp(var, "reftable.restartinterval")) { + unsigned long restart_interval = git_config_ulong(var, value, ctx->kvi); + if (restart_interval > UINT16_MAX) + die("reftable block size cannot exceed %u", (unsigned)UINT16_MAX); + opts->restart_interval = restart_interval; + } else if (!strcmp(var, "reftable.indexobjects")) { + opts->skip_index_objects = !git_config_bool(var, value); + } else if (!strcmp(var, "reftable.geometricfactor")) { + unsigned long factor = git_config_ulong(var, value, ctx->kvi); + if (factor > UINT8_MAX) + die("reftable geometric factor cannot exceed %u", (unsigned)UINT8_MAX); + opts->auto_compaction_factor = factor; + } + + return 0; +} + static struct ref_store *reftable_be_init(struct repository *repo, const char *gitdir, unsigned int store_flags) @@ -243,12 +271,24 @@ static struct ref_store *reftable_be_init(struct repository *repo, base_ref_store_init(&refs->base, repo, gitdir, &refs_be_reftable); strmap_init(&refs->worktree_stacks); refs->store_flags = store_flags; - refs->write_options.block_size = 4096; + refs->write_options.hash_id = repo->hash_algo->format_id; refs->write_options.default_permissions = calc_shared_perm(0666 & ~mask); refs->write_options.disable_auto_compact = !git_env_bool("GIT_TEST_REFTABLE_AUTOCOMPACTION", 1); + git_config(reftable_be_config, &refs->write_options); + + /* + * It is somewhat unfortunate that we have to mirror the default block + * size of the reftable library here. But given that the write options + * wouldn't be updated by the library here, and given that we require + * the proper block size to trim reflog message so that they fit, we + * must set up a proper value here. + */ + if (!refs->write_options.block_size) + refs->write_options.block_size = 4096; + /* * Set up the main reftable stack that is hosted in GIT_COMMON_DIR. * This stack contains both the shared and the main worktree refs. @@ -263,7 +303,7 @@ static struct ref_store *reftable_be_init(struct repository *repo, } strbuf_addstr(&path, "/reftable"); refs->err = reftable_new_stack(&refs->main_stack, path.buf, - refs->write_options); + &refs->write_options); if (refs->err) goto done; @@ -280,7 +320,7 @@ static struct ref_store *reftable_be_init(struct repository *repo, strbuf_addf(&path, "%s/reftable", gitdir); refs->err = reftable_new_stack(&refs->worktree_stack, path.buf, - refs->write_options); + &refs->write_options); if (refs->err) goto done; } @@ -293,12 +333,33 @@ done: return &refs->base; } -static int reftable_be_init_db(struct ref_store *ref_store, - int flags UNUSED, - struct strbuf *err UNUSED) +static void reftable_be_release(struct ref_store *ref_store) +{ + struct reftable_ref_store *refs = reftable_be_downcast(ref_store, 0, "release"); + struct strmap_entry *entry; + struct hashmap_iter iter; + + if (refs->main_stack) { + reftable_stack_destroy(refs->main_stack); + refs->main_stack = NULL; + } + + if (refs->worktree_stack) { + reftable_stack_destroy(refs->worktree_stack); + refs->worktree_stack = NULL; + } + + strmap_for_each_entry(&refs->worktree_stacks, &iter, entry) + reftable_stack_destroy(entry->value); + strmap_clear(&refs->worktree_stacks, 0); +} + +static int reftable_be_create_on_disk(struct ref_store *ref_store, + int flags UNUSED, + struct strbuf *err UNUSED) { struct reftable_ref_store *refs = - reftable_be_downcast(ref_store, REF_STORE_WRITE, "init_db"); + reftable_be_downcast(ref_store, REF_STORE_WRITE, "create"); struct strbuf sb = STRBUF_INIT; strbuf_addf(&sb, "%s/reftable", refs->base.gitdir); @@ -354,8 +415,7 @@ static int reftable_ref_iterator_advance(struct ref_iterator *ref_iterator) */ if (!starts_with(iter->ref.refname, "refs/") && !(iter->flags & DO_FOR_EACH_INCLUDE_ROOT_REFS && - (is_pseudoref(&iter->refs->base, iter->ref.refname) || - is_headref(&iter->refs->base, iter->ref.refname)))) { + is_root_ref(iter->ref.refname))) { continue; } @@ -462,7 +522,6 @@ static struct reftable_ref_iterator *ref_iterator_for_stack(struct reftable_ref_ const char *prefix, int flags) { - struct reftable_merged_table *merged_table; struct reftable_ref_iterator *iter; int ret; @@ -482,9 +541,8 @@ static struct reftable_ref_iterator *ref_iterator_for_stack(struct reftable_ref_ if (ret) goto done; - merged_table = reftable_stack_merged_table(stack); - - ret = reftable_merged_table_seek_ref(merged_table, &iter->iter, prefix); + reftable_stack_init_ref_iterator(stack, &iter->iter); + ret = reftable_iterator_seek_ref(&iter->iter, prefix); if (ret) goto done; @@ -579,16 +637,6 @@ static int reftable_be_read_symbolic_ref(struct ref_store *ref_store, return ret; } -/* - * Return the refname under which update was originally requested. - */ -static const char *original_update_refname(struct ref_update *update) -{ - while (update->parent_update) - update = update->parent_update; - return update->refname; -} - struct reftable_transaction_update { struct ref_update *update; struct object_id current_oid; @@ -827,7 +875,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, new_update = ref_transaction_add_update( transaction, "HEAD", u->flags | REF_LOG_ONLY | REF_NO_DEREF, - &u->new_oid, &u->old_oid, u->msg); + &u->new_oid, &u->old_oid, NULL, NULL, u->msg); string_list_insert(&affected_refnames, new_update->refname); } @@ -854,7 +902,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, * There is no need to write the reference deletion * when the reference in question doesn't exist. */ - if (u->flags & REF_HAVE_NEW && !is_null_oid(&u->new_oid)) { + if ((u->flags & REF_HAVE_NEW) && !ref_update_has_null_new_value(u)) { ret = queue_transaction_update(refs, tx_data, u, ¤t_oid, err); if (ret) @@ -867,7 +915,7 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, /* The reference does not exist, but we expected it to. */ strbuf_addf(err, _("cannot lock ref '%s': " "unable to resolve reference '%s'"), - original_update_refname(u), u->refname); + ref_update_original_update_refname(u), u->refname); ret = -1; goto done; } @@ -905,8 +953,10 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, * intertwined with the locking in files-backend.c. */ new_update = ref_transaction_add_update( - transaction, referent.buf, new_flags, - &u->new_oid, &u->old_oid, u->msg); + transaction, referent.buf, new_flags, + &u->new_oid, &u->old_oid, u->new_target, + u->old_target, u->msg); + new_update->parent_update = u; /* @@ -936,20 +986,25 @@ static int reftable_be_transaction_prepare(struct ref_store *ref_store, * individual refs. But the error messages match what the files * backend returns, which keeps our tests happy. */ - if (u->flags & REF_HAVE_OLD && !oideq(¤t_oid, &u->old_oid)) { + if (u->old_target) { + if (ref_update_check_old_target(referent.buf, u, err)) { + ret = -1; + goto done; + } + } else if ((u->flags & REF_HAVE_OLD) && !oideq(¤t_oid, &u->old_oid)) { if (is_null_oid(&u->old_oid)) strbuf_addf(err, _("cannot lock ref '%s': " - "reference already exists"), - original_update_refname(u)); + "reference already exists"), + ref_update_original_update_refname(u)); else if (is_null_oid(¤t_oid)) strbuf_addf(err, _("cannot lock ref '%s': " - "reference is missing but expected %s"), - original_update_refname(u), + "reference is missing but expected %s"), + ref_update_original_update_refname(u), oid_to_hex(&u->old_oid)); else strbuf_addf(err, _("cannot lock ref '%s': " - "is at %s but expected %s"), - original_update_refname(u), + "is at %s but expected %s"), + ref_update_original_update_refname(u), oid_to_hex(¤t_oid), oid_to_hex(&u->old_oid)); ret = -1; @@ -1015,8 +1070,6 @@ static int transaction_update_cmp(const void *a, const void *b) static int write_transaction_table(struct reftable_writer *writer, void *cb_data) { struct write_transaction_table_arg *arg = cb_data; - struct reftable_merged_table *mt = - reftable_stack_merged_table(arg->stack); uint64_t ts = reftable_stack_next_update_index(arg->stack); struct reftable_log_record *logs = NULL; struct ident_split committer_ident = {0}; @@ -1047,10 +1100,14 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data * - `core.logAllRefUpdates` tells us to create the reflog for * the given ref. */ - if (u->flags & REF_HAVE_NEW && !(u->type & REF_ISSYMREF) && is_null_oid(&u->new_oid)) { + if ((u->flags & REF_HAVE_NEW) && + !(u->type & REF_ISSYMREF) && + ref_update_has_null_new_value(u)) { struct reftable_log_record log = {0}; struct reftable_iterator it = {0}; + reftable_stack_init_log_iterator(arg->stack, &it); + /* * When deleting refs we also delete all reflog entries * with them. While it is not strictly required to @@ -1060,7 +1117,7 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data * Unfortunately, we have no better way than to delete * all reflog entries one by one. */ - ret = reftable_merged_table_seek_log(mt, &it, u->refname); + ret = reftable_iterator_seek_log(&it, u->refname); while (ret == 0) { struct reftable_log_record *tombstone; @@ -1088,24 +1145,52 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data (u->flags & REF_FORCE_CREATE_REFLOG || should_write_log(&arg->refs->base, u->refname))) { struct reftable_log_record *log; + int create_reflog = 1; + + if (u->new_target) { + if (!refs_resolve_ref_unsafe(&arg->refs->base, u->new_target, + RESOLVE_REF_READING, &u->new_oid, NULL)) { + /* + * TODO: currently we skip creating reflogs for dangling + * symref updates. It would be nice to capture this as + * zero oid updates however. + */ + create_reflog = 0; + } + } - ALLOC_GROW(logs, logs_nr + 1, logs_alloc); - log = &logs[logs_nr++]; - memset(log, 0, sizeof(*log)); - - fill_reftable_log_record(log, &committer_ident); - log->update_index = ts; - log->refname = xstrdup(u->refname); - memcpy(log->value.update.new_hash, u->new_oid.hash, GIT_MAX_RAWSZ); - memcpy(log->value.update.old_hash, tx_update->current_oid.hash, GIT_MAX_RAWSZ); - log->value.update.message = - xstrndup(u->msg, arg->refs->write_options.block_size / 2); + if (create_reflog) { + ALLOC_GROW(logs, logs_nr + 1, logs_alloc); + log = &logs[logs_nr++]; + memset(log, 0, sizeof(*log)); + + fill_reftable_log_record(log, &committer_ident); + log->update_index = ts; + log->refname = xstrdup(u->refname); + memcpy(log->value.update.new_hash, + u->new_oid.hash, GIT_MAX_RAWSZ); + memcpy(log->value.update.old_hash, + tx_update->current_oid.hash, GIT_MAX_RAWSZ); + log->value.update.message = + xstrndup(u->msg, arg->refs->write_options.block_size / 2); + } } if (u->flags & REF_LOG_ONLY) continue; - if (u->flags & REF_HAVE_NEW && is_null_oid(&u->new_oid)) { + if (u->new_target) { + struct reftable_ref_record ref = { + .refname = (char *)u->refname, + .value_type = REFTABLE_REF_SYMREF, + .value.symref = (char *)u->new_target, + .update_index = ts, + }; + + ret = reftable_writer_add_ref(writer, &ref); + if (ret < 0) + goto done; + } else if ((u->flags & REF_HAVE_NEW) && ref_update_has_null_new_value(u)) { struct reftable_ref_record ref = { .refname = (char *)u->refname, .update_index = ts, @@ -1123,7 +1208,7 @@ static int write_transaction_table(struct reftable_writer *writer, void *cb_data ref.refname = (char *)u->refname; ref.update_index = ts; - peel_error = peel_object(&u->new_oid, &peeled); + peel_error = peel_object(arg->refs->base.repo, &u->new_oid, &peeled); if (!peel_error) { ref.value_type = REFTABLE_REF_VAL2; memcpy(ref.value.val2.target_value, peeled.hash, GIT_MAX_RAWSZ); @@ -1237,110 +1322,6 @@ struct write_create_symref_arg { const char *logmsg; }; -static int write_create_symref_table(struct reftable_writer *writer, void *cb_data) -{ - struct write_create_symref_arg *create = cb_data; - uint64_t ts = reftable_stack_next_update_index(create->stack); - struct reftable_ref_record ref = { - .refname = (char *)create->refname, - .value_type = REFTABLE_REF_SYMREF, - .value.symref = (char *)create->target, - .update_index = ts, - }; - struct ident_split committer_ident = {0}; - struct reftable_log_record log = {0}; - struct object_id new_oid; - struct object_id old_oid; - const char *committer_info; - int ret; - - reftable_writer_set_limits(writer, ts, ts); - - ret = refs_verify_refname_available(&create->refs->base, create->refname, - NULL, NULL, create->err); - if (ret < 0) - return ret; - - ret = reftable_writer_add_ref(writer, &ref); - if (ret) - return ret; - - /* - * Note that it is important to try and resolve the reference before we - * write the log entry. This is because `should_write_log()` will munge - * `core.logAllRefUpdates`, which is undesirable when we create a new - * repository because it would be written into the config. As HEAD will - * not resolve for new repositories this ordering will ensure that this - * never happens. - */ - if (!create->logmsg || - !refs_resolve_ref_unsafe(&create->refs->base, create->target, - RESOLVE_REF_READING, &new_oid, NULL) || - !should_write_log(&create->refs->base, create->refname)) - return 0; - - committer_info = git_committer_info(0); - if (split_ident_line(&committer_ident, committer_info, strlen(committer_info))) - BUG("failed splitting committer info"); - - fill_reftable_log_record(&log, &committer_ident); - log.refname = xstrdup(create->refname); - log.update_index = ts; - log.value.update.message = xstrndup(create->logmsg, - create->refs->write_options.block_size / 2); - memcpy(log.value.update.new_hash, new_oid.hash, GIT_MAX_RAWSZ); - if (refs_resolve_ref_unsafe(&create->refs->base, create->refname, - RESOLVE_REF_READING, &old_oid, NULL)) - memcpy(log.value.update.old_hash, old_oid.hash, GIT_MAX_RAWSZ); - - ret = reftable_writer_add_log(writer, &log); - reftable_log_record_release(&log); - return ret; -} - -static int reftable_be_create_symref(struct ref_store *ref_store, - const char *refname, - const char *target, - const char *logmsg) -{ - struct reftable_ref_store *refs = - reftable_be_downcast(ref_store, REF_STORE_WRITE, "create_symref"); - struct reftable_stack *stack = stack_for(refs, refname, &refname); - struct strbuf err = STRBUF_INIT; - struct write_create_symref_arg arg = { - .refs = refs, - .stack = stack, - .refname = refname, - .target = target, - .logmsg = logmsg, - .err = &err, - }; - int ret; - - ret = refs->err; - if (ret < 0) - goto done; - - ret = reftable_stack_reload(stack); - if (ret) - goto done; - - ret = reftable_stack_add(stack, &write_create_symref_table, &arg); - -done: - assert(ret != REFTABLE_API_ERROR); - if (ret) { - if (err.len) - error("%s", err.buf); - else - error("unable to write symref for %s: %s", refname, - reftable_error_str(ret)); - } - - strbuf_release(&err); - return ret; -} - struct write_copy_arg { struct reftable_ref_store *refs; struct reftable_stack *stack; @@ -1354,7 +1335,6 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data) { struct write_copy_arg *arg = cb_data; uint64_t deletion_ts, creation_ts; - struct reftable_merged_table *mt = reftable_stack_merged_table(arg->stack); struct reftable_ref_record old_ref = {0}, refs[2] = {0}; struct reftable_log_record old_log = {0}, *logs = NULL; struct reftable_iterator it = {0}; @@ -1488,7 +1468,8 @@ static int write_copy_table(struct reftable_writer *writer, void *cb_data) * copy over all log entries from the old reflog. Last but not least, * when renaming we also have to delete all the old reflog entries. */ - ret = reftable_merged_table_seek_log(mt, &it, arg->oldname); + reftable_stack_init_log_iterator(arg->stack, &it); + ret = reftable_iterator_seek_log(&it, arg->oldname); if (ret < 0) goto done; @@ -1694,7 +1675,6 @@ static struct ref_iterator_vtable reftable_reflog_iterator_vtable = { static struct reftable_reflog_iterator *reflog_iterator_for_stack(struct reftable_ref_store *refs, struct reftable_stack *stack) { - struct reftable_merged_table *merged_table; struct reftable_reflog_iterator *iter; int ret; @@ -1711,9 +1691,8 @@ static struct reftable_reflog_iterator *reflog_iterator_for_stack(struct reftabl if (ret < 0) goto done; - merged_table = reftable_stack_merged_table(stack); - - ret = reftable_merged_table_seek_log(merged_table, &iter->iter, ""); + reftable_stack_init_log_iterator(stack, &iter->iter); + ret = reftable_iterator_seek_log(&iter->iter, ""); if (ret < 0) goto done; @@ -1771,7 +1750,6 @@ static int reftable_be_for_each_reflog_ent_reverse(struct ref_store *ref_store, struct reftable_ref_store *refs = reftable_be_downcast(ref_store, REF_STORE_READ, "for_each_reflog_ent_reverse"); struct reftable_stack *stack = stack_for(refs, refname, &refname); - struct reftable_merged_table *mt = NULL; struct reftable_log_record log = {0}; struct reftable_iterator it = {0}; int ret; @@ -1779,8 +1757,8 @@ static int reftable_be_for_each_reflog_ent_reverse(struct ref_store *ref_store, if (refs->err < 0) return refs->err; - mt = reftable_stack_merged_table(stack); - ret = reftable_merged_table_seek_log(mt, &it, refname); + reftable_stack_init_log_iterator(stack, &it); + ret = reftable_iterator_seek_log(&it, refname); while (!ret) { ret = reftable_iterator_next_log(&it, &log); if (ret < 0) @@ -1808,7 +1786,6 @@ static int reftable_be_for_each_reflog_ent(struct ref_store *ref_store, struct reftable_ref_store *refs = reftable_be_downcast(ref_store, REF_STORE_READ, "for_each_reflog_ent"); struct reftable_stack *stack = stack_for(refs, refname, &refname); - struct reftable_merged_table *mt = NULL; struct reftable_log_record *logs = NULL; struct reftable_iterator it = {0}; size_t logs_alloc = 0, logs_nr = 0, i; @@ -1817,8 +1794,8 @@ static int reftable_be_for_each_reflog_ent(struct ref_store *ref_store, if (refs->err < 0) return refs->err; - mt = reftable_stack_merged_table(stack); - ret = reftable_merged_table_seek_log(mt, &it, refname); + reftable_stack_init_log_iterator(stack, &it); + ret = reftable_iterator_seek_log(&it, refname); while (!ret) { struct reftable_log_record log = {0}; @@ -1855,7 +1832,6 @@ static int reftable_be_reflog_exists(struct ref_store *ref_store, struct reftable_ref_store *refs = reftable_be_downcast(ref_store, REF_STORE_READ, "reflog_exists"); struct reftable_stack *stack = stack_for(refs, refname, &refname); - struct reftable_merged_table *mt = reftable_stack_merged_table(stack); struct reftable_log_record log = {0}; struct reftable_iterator it = {0}; int ret; @@ -1868,7 +1844,8 @@ static int reftable_be_reflog_exists(struct ref_store *ref_store, if (ret < 0) goto done; - ret = reftable_merged_table_seek_log(mt, &it, refname); + reftable_stack_init_log_iterator(stack, &it); + ret = reftable_iterator_seek_log(&it, refname); if (ret < 0) goto done; @@ -1966,8 +1943,6 @@ struct write_reflog_delete_arg { static int write_reflog_delete_table(struct reftable_writer *writer, void *cb_data) { struct write_reflog_delete_arg *arg = cb_data; - struct reftable_merged_table *mt = - reftable_stack_merged_table(arg->stack); struct reftable_log_record log = {0}, tombstone = {0}; struct reftable_iterator it = {0}; uint64_t ts = reftable_stack_next_update_index(arg->stack); @@ -1975,12 +1950,14 @@ static int write_reflog_delete_table(struct reftable_writer *writer, void *cb_da reftable_writer_set_limits(writer, ts, ts); + reftable_stack_init_log_iterator(arg->stack, &it); + /* * In order to delete a table we need to delete all reflog entries one * by one. This is inefficient, but the reftable format does not have a * better marker right now. */ - ret = reftable_merged_table_seek_log(mt, &it, arg->refname); + ret = reftable_iterator_seek_log(&it, arg->refname); while (ret == 0) { ret = reftable_iterator_next_log(&it, &log); if (ret < 0) @@ -2024,6 +2001,7 @@ static int reftable_be_delete_reflog(struct ref_store *ref_store, } struct reflog_expiry_arg { + struct reftable_ref_store *refs; struct reftable_stack *stack; struct reftable_log_record *records; struct object_id update_oid; @@ -2052,7 +2030,7 @@ static int write_reflog_expiry_table(struct reftable_writer *writer, void *cb_da ref.refname = (char *)arg->refname; ref.update_index = ts; - if (!peel_object(&arg->update_oid, &peeled)) { + if (!peel_object(arg->refs->base.repo, &arg->update_oid, &peeled)) { ref.value_type = REFTABLE_REF_VAL2; memcpy(ref.value.val2.target_value, peeled.hash, GIT_MAX_RAWSZ); memcpy(ref.value.val2.value, arg->update_oid.hash, GIT_MAX_RAWSZ); @@ -2116,7 +2094,6 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store, struct reftable_ref_store *refs = reftable_be_downcast(ref_store, REF_STORE_WRITE, "reflog_expire"); struct reftable_stack *stack = stack_for(refs, refname, &refname); - struct reftable_merged_table *mt = reftable_stack_merged_table(stack); struct reftable_log_record *logs = NULL; struct reftable_log_record *rewritten = NULL; struct reftable_ref_record ref_record = {0}; @@ -2135,7 +2112,9 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store, if (ret < 0) goto done; - ret = reftable_merged_table_seek_log(mt, &it, refname); + reftable_stack_init_log_iterator(stack, &it); + + ret = reftable_iterator_seek_log(&it, refname); if (ret < 0) goto done; @@ -2214,6 +2193,7 @@ static int reftable_be_reflog_expire(struct ref_store *ref_store, reftable_ref_record_val1(&ref_record)) oidread(&arg.update_oid, last_hash); + arg.refs = refs; arg.records = rewritten; arg.len = logs_nr; arg.stack = stack, @@ -2248,14 +2228,15 @@ done: struct ref_storage_be refs_be_reftable = { .name = "reftable", .init = reftable_be_init, - .init_db = reftable_be_init_db, + .release = reftable_be_release, + .create_on_disk = reftable_be_create_on_disk, + .transaction_prepare = reftable_be_transaction_prepare, .transaction_finish = reftable_be_transaction_finish, .transaction_abort = reftable_be_transaction_abort, .initial_transaction_commit = reftable_be_initial_transaction_commit, .pack_refs = reftable_be_pack_refs, - .create_symref = reftable_be_create_symref, .rename_ref = reftable_be_rename_ref, .copy_ref = reftable_be_copy_ref, diff --git a/reftable/block.c b/reftable/block.c index 5942cb4..00030ee 100644 --- a/reftable/block.c +++ b/reftable/block.c @@ -326,9 +326,9 @@ int block_reader_first_key(const struct block_reader *br, struct strbuf *key) return 0; } -static uint32_t block_reader_restart_offset(const struct block_reader *br, int i) +static uint32_t block_reader_restart_offset(const struct block_reader *br, size_t idx) { - return get_be24(br->restart_bytes + 3 * i); + return get_be24(br->restart_bytes + 3 * idx); } void block_iter_seek_start(struct block_iter *it, const struct block_reader *br) diff --git a/reftable/block.h b/reftable/block.h index e91f3d2..1c8f25e 100644 --- a/reftable/block.h +++ b/reftable/block.h @@ -29,7 +29,7 @@ struct block_writer { uint32_t header_off; /* How often to restart keys. */ - int restart_interval; + uint16_t restart_interval; int hash_size; /* Offset of next uint8_t to write. */ diff --git a/reftable/constants.h b/reftable/constants.h index 5eee72c..f6beb84 100644 --- a/reftable/constants.h +++ b/reftable/constants.h @@ -17,5 +17,6 @@ https://developers.google.com/open-source/licenses/bsd #define MAX_RESTARTS ((1 << 16) - 1) #define DEFAULT_BLOCK_SIZE 4096 +#define DEFAULT_GEOMETRIC_FACTOR 2 #endif diff --git a/reftable/dump.c b/reftable/dump.c index 26e0393..41abbb8 100644 --- a/reftable/dump.c +++ b/reftable/dump.c @@ -27,9 +27,9 @@ https://developers.google.com/open-source/licenses/bsd static int compact_stack(const char *stackdir) { struct reftable_stack *stack = NULL; - struct reftable_write_options cfg = { 0 }; + struct reftable_write_options opts = { 0 }; - int err = reftable_new_stack(&stack, stackdir, cfg); + int err = reftable_new_stack(&stack, stackdir, &opts); if (err < 0) goto done; @@ -48,6 +48,7 @@ static void print_help(void) printf("usage: dump [-cst] arg\n\n" "options: \n" " -c compact\n" + " -b dump blocks\n" " -t dump table\n" " -s dump stack\n" " -6 sha256 hash format\n" @@ -58,6 +59,7 @@ static void print_help(void) int reftable_dump_main(int argc, char *const *argv) { int err = 0; + int opt_dump_blocks = 0; int opt_dump_table = 0; int opt_dump_stack = 0; int opt_compact = 0; @@ -67,6 +69,8 @@ int reftable_dump_main(int argc, char *const *argv) for (; argc > 1; argv++, argc--) if (*argv[1] != '-') break; + else if (!strcmp("-b", argv[1])) + opt_dump_blocks = 1; else if (!strcmp("-t", argv[1])) opt_dump_table = 1; else if (!strcmp("-6", argv[1])) @@ -88,7 +92,9 @@ int reftable_dump_main(int argc, char *const *argv) arg = argv[1]; - if (opt_dump_table) { + if (opt_dump_blocks) { + err = reftable_reader_print_blocks(arg); + } else if (opt_dump_table) { err = reftable_reader_print_file(arg); } else if (opt_dump_stack) { err = reftable_stack_print_directory(arg, opt_hash_id); diff --git a/reftable/generic.c b/reftable/generic.c index b9f1c7c..28ae261 100644 --- a/reftable/generic.c +++ b/reftable/generic.c @@ -12,32 +12,66 @@ https://developers.google.com/open-source/licenses/bsd #include "reftable-iterator.h" #include "reftable-generic.h" -int reftable_table_seek_ref(struct reftable_table *tab, - struct reftable_iterator *it, const char *name) +void table_init_iter(struct reftable_table *tab, + struct reftable_iterator *it, + uint8_t typ) { - struct reftable_record rec = { .type = BLOCK_TYPE_REF, - .u.ref = { - .refname = (char *)name, - } }; - return tab->ops->seek_record(tab->table_arg, it, &rec); + + tab->ops->init_iter(tab->table_arg, it, typ); +} + +void reftable_table_init_ref_iter(struct reftable_table *tab, + struct reftable_iterator *it) +{ + table_init_iter(tab, it, BLOCK_TYPE_REF); +} + +void reftable_table_init_log_iter(struct reftable_table *tab, + struct reftable_iterator *it) +{ + table_init_iter(tab, it, BLOCK_TYPE_LOG); +} + +int reftable_iterator_seek_ref(struct reftable_iterator *it, + const char *name) +{ + struct reftable_record want = { + .type = BLOCK_TYPE_REF, + .u.ref = { + .refname = (char *)name, + }, + }; + return it->ops->seek(it->iter_arg, &want); +} + +int reftable_iterator_seek_log_at(struct reftable_iterator *it, + const char *name, uint64_t update_index) +{ + struct reftable_record want = { + .type = BLOCK_TYPE_LOG, + .u.log = { + .refname = (char *)name, + .update_index = update_index, + }, + }; + return it->ops->seek(it->iter_arg, &want); } -int reftable_table_seek_log(struct reftable_table *tab, - struct reftable_iterator *it, const char *name) +int reftable_iterator_seek_log(struct reftable_iterator *it, + const char *name) { - struct reftable_record rec = { .type = BLOCK_TYPE_LOG, - .u.log = { - .refname = (char *)name, - .update_index = ~((uint64_t)0), - } }; - return tab->ops->seek_record(tab->table_arg, it, &rec); + return reftable_iterator_seek_log_at(it, name, ~((uint64_t) 0)); } int reftable_table_read_ref(struct reftable_table *tab, const char *name, struct reftable_ref_record *ref) { struct reftable_iterator it = { NULL }; - int err = reftable_table_seek_ref(tab, &it, name); + int err; + + reftable_table_init_ref_iter(tab, &it); + + err = reftable_iterator_seek_ref(&it, name); if (err) goto done; @@ -62,10 +96,13 @@ int reftable_table_print(struct reftable_table *tab) { struct reftable_ref_record ref = { NULL }; struct reftable_log_record log = { NULL }; uint32_t hash_id = reftable_table_hash_id(tab); - int err = reftable_table_seek_ref(tab, &it, ""); - if (err < 0) { + int err; + + reftable_table_init_ref_iter(tab, &it); + + err = reftable_iterator_seek_ref(&it, ""); + if (err < 0) return err; - } while (1) { err = reftable_iterator_next_ref(&it, &ref); @@ -80,10 +117,12 @@ int reftable_table_print(struct reftable_table *tab) { reftable_iterator_destroy(&it); reftable_ref_record_release(&ref); - err = reftable_table_seek_log(tab, &it, ""); - if (err < 0) { + reftable_table_init_log_iter(tab, &it); + + err = reftable_iterator_seek_log(&it, ""); + if (err < 0) return err; - } + while (1) { err = reftable_iterator_next_log(&it, &log); if (err > 0) { @@ -152,11 +191,21 @@ int reftable_iterator_next_log(struct reftable_iterator *it, return err; } +int iterator_seek(struct reftable_iterator *it, struct reftable_record *want) +{ + return it->ops->seek(it->iter_arg, want); +} + int iterator_next(struct reftable_iterator *it, struct reftable_record *rec) { return it->ops->next(it->iter_arg, rec); } +static int empty_iterator_seek(void *arg, struct reftable_record *want) +{ + return 0; +} + static int empty_iterator_next(void *arg, struct reftable_record *rec) { return 1; @@ -167,6 +216,7 @@ static void empty_iterator_close(void *arg) } static struct reftable_iterator_vtable empty_vtable = { + .seek = &empty_iterator_seek, .next = &empty_iterator_next, .close = &empty_iterator_close, }; diff --git a/reftable/generic.h b/reftable/generic.h index 98886a0..8341fa5 100644 --- a/reftable/generic.h +++ b/reftable/generic.h @@ -14,19 +14,24 @@ https://developers.google.com/open-source/licenses/bsd /* generic interface to reftables */ struct reftable_table_vtable { - int (*seek_record)(void *tab, struct reftable_iterator *it, - struct reftable_record *); + void (*init_iter)(void *tab, struct reftable_iterator *it, uint8_t typ); uint32_t (*hash_id)(void *tab); uint64_t (*min_update_index)(void *tab); uint64_t (*max_update_index)(void *tab); }; +void table_init_iter(struct reftable_table *tab, + struct reftable_iterator *it, + uint8_t typ); + struct reftable_iterator_vtable { + int (*seek)(void *iter_arg, struct reftable_record *want); int (*next)(void *iter_arg, struct reftable_record *rec); void (*close)(void *iter_arg); }; void iterator_set_empty(struct reftable_iterator *it); +int iterator_seek(struct reftable_iterator *it, struct reftable_record *want); int iterator_next(struct reftable_iterator *it, struct reftable_record *rec); #endif diff --git a/reftable/iter.c b/reftable/iter.c index aa9ac19..fddea31 100644 --- a/reftable/iter.c +++ b/reftable/iter.c @@ -23,6 +23,13 @@ static void filtering_ref_iterator_close(void *iter_arg) reftable_iterator_destroy(&fri->it); } +static int filtering_ref_iterator_seek(void *iter_arg, + struct reftable_record *want) +{ + struct filtering_ref_iterator *fri = iter_arg; + return iterator_seek(&fri->it, want); +} + static int filtering_ref_iterator_next(void *iter_arg, struct reftable_record *rec) { @@ -38,11 +45,11 @@ static int filtering_ref_iterator_next(void *iter_arg, if (fri->double_check) { struct reftable_iterator it = { NULL }; - err = reftable_table_seek_ref(&fri->tab, &it, - ref->refname); - if (err == 0) { + reftable_table_init_ref_iter(&fri->tab, &it); + + err = reftable_iterator_seek_ref(&it, ref->refname); + if (err == 0) err = reftable_iterator_next_ref(&it, ref); - } reftable_iterator_destroy(&it); @@ -73,6 +80,7 @@ static int filtering_ref_iterator_next(void *iter_arg, } static struct reftable_iterator_vtable filtering_ref_iterator_vtable = { + .seek = &filtering_ref_iterator_seek, .next = &filtering_ref_iterator_next, .close = &filtering_ref_iterator_close, }; @@ -119,6 +127,12 @@ static int indexed_table_ref_iter_next_block(struct indexed_table_ref_iter *it) return 0; } +static int indexed_table_ref_iter_seek(void *p, struct reftable_record *want) +{ + BUG("seeking indexed table is not supported"); + return -1; +} + static int indexed_table_ref_iter_next(void *p, struct reftable_record *rec) { struct indexed_table_ref_iter *it = p; @@ -175,6 +189,7 @@ int new_indexed_table_ref_iter(struct indexed_table_ref_iter **dest, } static struct reftable_iterator_vtable indexed_table_ref_iter_vtable = { + .seek = &indexed_table_ref_iter_seek, .next = &indexed_table_ref_iter_next, .close = &indexed_table_ref_iter_close, }; diff --git a/reftable/merged.c b/reftable/merged.c index f85a24c..0da9dba 100644 --- a/reftable/merged.c +++ b/reftable/merged.c @@ -25,34 +25,25 @@ struct merged_subiter { struct merged_iter { struct merged_subiter *subiters; struct merged_iter_pqueue pq; - uint32_t hash_id; size_t stack_len; - uint8_t typ; int suppress_deletions; ssize_t advance_index; }; -static int merged_iter_init(struct merged_iter *mi) +static void merged_iter_init(struct merged_iter *mi, + struct reftable_merged_table *mt, + uint8_t typ) { - for (size_t i = 0; i < mi->stack_len; i++) { - struct pq_entry e = { - .index = i, - .rec = &mi->subiters[i].rec, - }; - int err; - - reftable_record_init(&mi->subiters[i].rec, mi->typ); - err = iterator_next(&mi->subiters[i].iter, - &mi->subiters[i].rec); - if (err < 0) - return err; - if (err > 0) - continue; + memset(mi, 0, sizeof(*mi)); + mi->advance_index = -1; + mi->suppress_deletions = mt->suppress_deletions; - merged_iter_pqueue_add(&mi->pq, &e); + REFTABLE_CALLOC_ARRAY(mi->subiters, mt->stack_len); + for (size_t i = 0; i < mt->stack_len; i++) { + reftable_record_init(&mi->subiters[i].rec, typ); + table_init_iter(&mt->stack[i], &mi->subiters[i].iter, typ); } - - return 0; + mi->stack_len = mt->stack_len; } static void merged_iter_close(void *p) @@ -83,6 +74,27 @@ static int merged_iter_advance_subiter(struct merged_iter *mi, size_t idx) return 0; } +static int merged_iter_seek(struct merged_iter *mi, struct reftable_record *want) +{ + int err; + + mi->advance_index = -1; + + for (size_t i = 0; i < mi->stack_len; i++) { + err = iterator_seek(&mi->subiters[i].iter, want); + if (err < 0) + return err; + if (err > 0) + continue; + + err = merged_iter_advance_subiter(mi, i); + if (err < 0) + return err; + } + + return 0; +} + static int merged_iter_next_entry(struct merged_iter *mi, struct reftable_record *rec) { @@ -148,6 +160,11 @@ static int merged_iter_next_entry(struct merged_iter *mi, return 0; } +static int merged_iter_seek_void(void *it, struct reftable_record *want) +{ + return merged_iter_seek(it, want); +} + static int merged_iter_next_void(void *p, struct reftable_record *rec) { struct merged_iter *mi = p; @@ -162,6 +179,7 @@ static int merged_iter_next_void(void *p, struct reftable_record *rec) } static struct reftable_iterator_vtable merged_iter_vtable = { + .seek = merged_iter_seek_void, .next = &merged_iter_next_void, .close = &merged_iter_close, }; @@ -235,81 +253,13 @@ reftable_merged_table_min_update_index(struct reftable_merged_table *mt) return mt->min; } -static int reftable_table_seek_record(struct reftable_table *tab, - struct reftable_iterator *it, - struct reftable_record *rec) -{ - return tab->ops->seek_record(tab->table_arg, it, rec); -} - -static int merged_table_seek_record(struct reftable_merged_table *mt, - struct reftable_iterator *it, - struct reftable_record *rec) -{ - struct merged_iter merged = { - .typ = reftable_record_type(rec), - .hash_id = mt->hash_id, - .suppress_deletions = mt->suppress_deletions, - .advance_index = -1, - }; - struct merged_iter *p; - int err; - - REFTABLE_CALLOC_ARRAY(merged.subiters, mt->stack_len); - for (size_t i = 0; i < mt->stack_len; i++) { - err = reftable_table_seek_record(&mt->stack[i], - &merged.subiters[merged.stack_len].iter, rec); - if (err < 0) - goto out; - if (!err) - merged.stack_len++; - } - - err = merged_iter_init(&merged); - if (err < 0) - goto out; - - p = reftable_malloc(sizeof(struct merged_iter)); - *p = merged; - iterator_from_merged_iter(it, p); - -out: - if (err < 0) - merged_iter_close(&merged); - return err; -} - -int reftable_merged_table_seek_ref(struct reftable_merged_table *mt, - struct reftable_iterator *it, - const char *name) -{ - struct reftable_record rec = { - .type = BLOCK_TYPE_REF, - .u.ref = { - .refname = (char *)name, - }, - }; - return merged_table_seek_record(mt, it, &rec); -} - -int reftable_merged_table_seek_log_at(struct reftable_merged_table *mt, - struct reftable_iterator *it, - const char *name, uint64_t update_index) -{ - struct reftable_record rec = { .type = BLOCK_TYPE_LOG, - .u.log = { - .refname = (char *)name, - .update_index = update_index, - } }; - return merged_table_seek_record(mt, it, &rec); -} - -int reftable_merged_table_seek_log(struct reftable_merged_table *mt, - struct reftable_iterator *it, - const char *name) +void merged_table_init_iter(struct reftable_merged_table *mt, + struct reftable_iterator *it, + uint8_t typ) { - uint64_t max = ~((uint64_t)0); - return reftable_merged_table_seek_log_at(mt, it, name, max); + struct merged_iter *mi = reftable_malloc(sizeof(*mi)); + merged_iter_init(mi, mt, typ); + iterator_from_merged_iter(it, mi); } uint32_t reftable_merged_table_hash_id(struct reftable_merged_table *mt) @@ -317,11 +267,11 @@ uint32_t reftable_merged_table_hash_id(struct reftable_merged_table *mt) return mt->hash_id; } -static int reftable_merged_table_seek_void(void *tab, - struct reftable_iterator *it, - struct reftable_record *rec) +static void reftable_merged_table_init_iter_void(void *tab, + struct reftable_iterator *it, + uint8_t typ) { - return merged_table_seek_record(tab, it, rec); + merged_table_init_iter(tab, it, typ); } static uint32_t reftable_merged_table_hash_id_void(void *tab) @@ -340,7 +290,7 @@ static uint64_t reftable_merged_table_max_update_index_void(void *tab) } static struct reftable_table_vtable merged_table_vtable = { - .seek_record = reftable_merged_table_seek_void, + .init_iter = reftable_merged_table_init_iter_void, .hash_id = reftable_merged_table_hash_id_void, .min_update_index = reftable_merged_table_min_update_index_void, .max_update_index = reftable_merged_table_max_update_index_void, diff --git a/reftable/merged.h b/reftable/merged.h index a2571db..a10469f 100644 --- a/reftable/merged.h +++ b/reftable/merged.h @@ -26,4 +26,10 @@ struct reftable_merged_table { void merged_table_release(struct reftable_merged_table *mt); +struct reftable_iterator; + +void merged_table_init_iter(struct reftable_merged_table *mt, + struct reftable_iterator *it, + uint8_t typ); + #endif diff --git a/reftable/merged_test.c b/reftable/merged_test.c index 530fc82..33a17ef 100644 --- a/reftable/merged_test.c +++ b/reftable/merged_test.c @@ -12,6 +12,7 @@ https://developers.google.com/open-source/licenses/bsd #include "basics.h" #include "blocksource.h" +#include "constants.h" #include "reader.h" #include "record.h" #include "test_framework.h" @@ -145,7 +146,10 @@ static void test_merged_between(void) int i; struct reftable_ref_record ref = { NULL }; struct reftable_iterator it = { NULL }; - int err = reftable_merged_table_seek_ref(mt, &it, "a"); + int err; + + merged_table_init_iter(mt, &it, BLOCK_TYPE_REF); + err = reftable_iterator_seek_ref(&it, "a"); EXPECT_ERR(err); err = reftable_iterator_next_ref(&it, &ref); @@ -217,14 +221,15 @@ static void test_merged(void) struct reftable_reader **readers = NULL; struct reftable_merged_table *mt = merged_table_from_records(refs, &bs, &readers, sizes, bufs, 3); - struct reftable_iterator it = { NULL }; - int err = reftable_merged_table_seek_ref(mt, &it, "a"); + int err; struct reftable_ref_record *out = NULL; size_t len = 0; size_t cap = 0; int i = 0; + merged_table_init_iter(mt, &it, BLOCK_TYPE_REF); + err = reftable_iterator_seek_ref(&it, "a"); EXPECT_ERR(err); EXPECT(reftable_merged_table_hash_id(mt) == GIT_SHA1_FORMAT_ID); EXPECT(reftable_merged_table_min_update_index(mt) == 1); @@ -348,14 +353,15 @@ static void test_merged_logs(void) struct reftable_reader **readers = NULL; struct reftable_merged_table *mt = merged_table_from_log_records( logs, &bs, &readers, sizes, bufs, 3); - struct reftable_iterator it = { NULL }; - int err = reftable_merged_table_seek_log(mt, &it, "a"); + int err; struct reftable_log_record *out = NULL; size_t len = 0; size_t cap = 0; int i = 0; + merged_table_init_iter(mt, &it, BLOCK_TYPE_LOG); + err = reftable_iterator_seek_log(&it, "a"); EXPECT_ERR(err); EXPECT(reftable_merged_table_hash_id(mt) == GIT_SHA1_FORMAT_ID); EXPECT(reftable_merged_table_min_update_index(mt) == 1); @@ -377,7 +383,8 @@ static void test_merged_logs(void) GIT_SHA1_RAWSZ)); } - err = reftable_merged_table_seek_log_at(mt, &it, "a", 2); + merged_table_init_iter(mt, &it, BLOCK_TYPE_LOG); + err = reftable_iterator_seek_log_at(&it, "a", 2); EXPECT_ERR(err); reftable_log_record_release(&out[0]); err = reftable_iterator_next_log(&it, &out[0]); diff --git a/reftable/reader.c b/reftable/reader.c index 481dff1..29c99e2 100644 --- a/reftable/reader.c +++ b/reftable/reader.c @@ -224,8 +224,14 @@ struct table_iter { struct block_iter bi; int is_finished; }; -#define TABLE_ITER_INIT { \ - .bi = BLOCK_ITER_INIT \ + +static int table_iter_init(struct table_iter *ti, struct reftable_reader *r) +{ + struct block_iter bi = BLOCK_ITER_INIT; + memset(ti, 0, sizeof(*ti)); + ti->r = r; + ti->bi = bi; + return 0; } static int table_iter_next_in_block(struct table_iter *ti, @@ -363,50 +369,23 @@ static int table_iter_next(struct table_iter *ti, struct reftable_record *rec) } } -static int table_iter_next_void(void *ti, struct reftable_record *rec) -{ - return table_iter_next(ti, rec); -} - -static void table_iter_close_void(void *ti) -{ - table_iter_close(ti); -} - -static struct reftable_iterator_vtable table_iter_vtable = { - .next = &table_iter_next_void, - .close = &table_iter_close_void, -}; - -static void iterator_from_table_iter(struct reftable_iterator *it, - struct table_iter *ti) -{ - assert(!it->ops); - it->iter_arg = ti; - it->ops = &table_iter_vtable; -} - -static int reader_table_iter_at(struct reftable_reader *r, - struct table_iter *ti, uint64_t off, - uint8_t typ) +static int table_iter_seek_to(struct table_iter *ti, uint64_t off, uint8_t typ) { int err; - err = reader_init_block_reader(r, &ti->br, off, typ); + err = reader_init_block_reader(ti->r, &ti->br, off, typ); if (err != 0) return err; - ti->r = r; ti->typ = block_reader_type(&ti->br); ti->block_off = off; block_iter_seek_start(&ti->bi, &ti->br); return 0; } -static int reader_start(struct reftable_reader *r, struct table_iter *ti, - uint8_t typ, int index) +static int table_iter_seek_start(struct table_iter *ti, uint8_t typ, int index) { - struct reftable_reader_offsets *offs = reader_offsets_for(r, typ); + struct reftable_reader_offsets *offs = reader_offsets_for(ti->r, typ); uint64_t off = offs->offset; if (index) { off = offs->index_offset; @@ -416,16 +395,16 @@ static int reader_start(struct reftable_reader *r, struct table_iter *ti, typ = BLOCK_TYPE_INDEX; } - return reader_table_iter_at(r, ti, off, typ); + return table_iter_seek_to(ti, off, typ); } -static int reader_seek_linear(struct table_iter *ti, - struct reftable_record *want) +static int table_iter_seek_linear(struct table_iter *ti, + struct reftable_record *want) { struct strbuf want_key = STRBUF_INIT; struct strbuf got_key = STRBUF_INIT; struct reftable_record rec; - int err = -1; + int err; reftable_record_init(&rec, reftable_record_type(want)); reftable_record_key(want, &want_key); @@ -499,9 +478,8 @@ done: return err; } -static int reader_seek_indexed(struct reftable_reader *r, - struct reftable_iterator *it, - struct reftable_record *rec) +static int table_iter_seek_indexed(struct table_iter *ti, + struct reftable_record *rec) { struct reftable_record want_index = { .type = BLOCK_TYPE_INDEX, .u.idx = { .last_key = STRBUF_INIT } @@ -510,15 +488,9 @@ static int reader_seek_indexed(struct reftable_reader *r, .type = BLOCK_TYPE_INDEX, .u.idx = { .last_key = STRBUF_INIT }, }; - struct table_iter index_iter = TABLE_ITER_INIT; - struct table_iter empty = TABLE_ITER_INIT; - struct table_iter next = TABLE_ITER_INIT; - int err = 0; + int err; reftable_record_key(rec, &want_index.u.idx.last_key); - err = reader_start(r, &index_iter, reftable_record_type(rec), 1); - if (err < 0) - goto done; /* * The index may consist of multiple levels, where each level may have @@ -526,7 +498,7 @@ static int reader_seek_indexed(struct reftable_reader *r, * highest layer that identifies the relevant index block as well as * the record inside that block that corresponds to our wanted key. */ - err = reader_seek_linear(&index_iter, &want_index); + err = table_iter_seek_linear(ti, &want_index); if (err < 0) goto done; @@ -552,123 +524,113 @@ static int reader_seek_indexed(struct reftable_reader *r, * all levels of the index only to find out that the key does * not exist. */ - err = table_iter_next(&index_iter, &index_result); + err = table_iter_next(ti, &index_result); if (err != 0) goto done; - err = reader_table_iter_at(r, &next, index_result.u.idx.offset, - 0); + err = table_iter_seek_to(ti, index_result.u.idx.offset, 0); if (err != 0) goto done; - err = block_iter_seek_key(&next.bi, &next.br, &want_index.u.idx.last_key); + err = block_iter_seek_key(&ti->bi, &ti->br, &want_index.u.idx.last_key); if (err < 0) goto done; - if (next.typ == reftable_record_type(rec)) { + if (ti->typ == reftable_record_type(rec)) { err = 0; break; } - if (next.typ != BLOCK_TYPE_INDEX) { + if (ti->typ != BLOCK_TYPE_INDEX) { err = REFTABLE_FORMAT_ERROR; - break; + goto done; } - - table_iter_close(&index_iter); - index_iter = next; - next = empty; - } - - if (err == 0) { - struct table_iter *malloced = reftable_calloc(1, sizeof(*malloced)); - *malloced = next; - next = empty; - iterator_from_table_iter(it, malloced); } done: - table_iter_close(&next); - table_iter_close(&index_iter); reftable_record_release(&want_index); reftable_record_release(&index_result); return err; } -static int reader_seek_internal(struct reftable_reader *r, - struct reftable_iterator *it, - struct reftable_record *rec) +static int table_iter_seek(struct table_iter *ti, + struct reftable_record *want) { - struct reftable_reader_offsets *offs = - reader_offsets_for(r, reftable_record_type(rec)); - uint64_t idx = offs->index_offset; - struct table_iter ti = TABLE_ITER_INIT, *p; + uint8_t typ = reftable_record_type(want); + struct reftable_reader_offsets *offs = reader_offsets_for(ti->r, typ); int err; - if (idx > 0) - return reader_seek_indexed(r, it, rec); - - err = reader_start(r, &ti, reftable_record_type(rec), 0); + err = table_iter_seek_start(ti, reftable_record_type(want), + !!offs->index_offset); if (err < 0) goto out; - err = reader_seek_linear(&ti, rec); - if (err < 0) + if (offs->index_offset) + err = table_iter_seek_indexed(ti, want); + else + err = table_iter_seek_linear(ti, want); + if (err) goto out; - REFTABLE_ALLOC_ARRAY(p, 1); - *p = ti; - iterator_from_table_iter(it, p); - out: - if (err) - table_iter_close(&ti); return err; } -static int reader_seek(struct reftable_reader *r, struct reftable_iterator *it, - struct reftable_record *rec) +static int table_iter_seek_void(void *ti, struct reftable_record *want) { - uint8_t typ = reftable_record_type(rec); + return table_iter_seek(ti, want); +} - struct reftable_reader_offsets *offs = reader_offsets_for(r, typ); - if (!offs->is_present) { - iterator_set_empty(it); - return 0; - } +static int table_iter_next_void(void *ti, struct reftable_record *rec) +{ + return table_iter_next(ti, rec); +} - return reader_seek_internal(r, it, rec); +static void table_iter_close_void(void *ti) +{ + table_iter_close(ti); } -int reftable_reader_seek_ref(struct reftable_reader *r, - struct reftable_iterator *it, const char *name) +static struct reftable_iterator_vtable table_iter_vtable = { + .seek = &table_iter_seek_void, + .next = &table_iter_next_void, + .close = &table_iter_close_void, +}; + +static void iterator_from_table_iter(struct reftable_iterator *it, + struct table_iter *ti) { - struct reftable_record rec = { - .type = BLOCK_TYPE_REF, - .u.ref = { - .refname = (char *)name, - }, - }; - return reader_seek(r, it, &rec); + assert(!it->ops); + it->iter_arg = ti; + it->ops = &table_iter_vtable; } -int reftable_reader_seek_log_at(struct reftable_reader *r, - struct reftable_iterator *it, const char *name, - uint64_t update_index) +static void reader_init_iter(struct reftable_reader *r, + struct reftable_iterator *it, + uint8_t typ) { - struct reftable_record rec = { .type = BLOCK_TYPE_LOG, - .u.log = { - .refname = (char *)name, - .update_index = update_index, - } }; - return reader_seek(r, it, &rec); + struct reftable_reader_offsets *offs = reader_offsets_for(r, typ); + + if (offs->is_present) { + struct table_iter *ti; + REFTABLE_ALLOC_ARRAY(ti, 1); + table_iter_init(ti, r); + iterator_from_table_iter(it, ti); + } else { + iterator_set_empty(it); + } } -int reftable_reader_seek_log(struct reftable_reader *r, - struct reftable_iterator *it, const char *name) +void reftable_reader_init_ref_iterator(struct reftable_reader *r, + struct reftable_iterator *it) { - uint64_t max = ~((uint64_t)0); - return reftable_reader_seek_log_at(r, it, name, max); + reader_init_iter(r, it, BLOCK_TYPE_REF); +} + +void reftable_reader_init_log_iterator(struct reftable_reader *r, + struct reftable_iterator *it) +{ + reader_init_iter(r, it, BLOCK_TYPE_LOG); } void reader_close(struct reftable_reader *r) @@ -719,7 +681,8 @@ static int reftable_reader_refs_for_indexed(struct reftable_reader *r, struct indexed_table_ref_iter *itr = NULL; /* Look through the reverse index. */ - err = reader_seek(r, &oit, &want); + reader_init_iter(r, &oit, BLOCK_TYPE_OBJ); + err = iterator_seek(&oit, &want); if (err != 0) goto done; @@ -754,15 +717,15 @@ static int reftable_reader_refs_for_unindexed(struct reftable_reader *r, struct reftable_iterator *it, uint8_t *oid) { - struct table_iter ti_empty = TABLE_ITER_INIT; - struct table_iter *ti = reftable_calloc(1, sizeof(*ti)); + struct table_iter *ti; struct filtering_ref_iterator *filter = NULL; struct filtering_ref_iterator empty = FILTERING_REF_ITERATOR_INIT; int oid_len = hash_size(r->hash_id); int err; - *ti = ti_empty; - err = reader_start(r, ti, BLOCK_TYPE_REF, 0); + REFTABLE_ALLOC_ARRAY(ti, 1); + table_iter_init(ti, r); + err = table_iter_seek_start(ti, BLOCK_TYPE_REF, 0); if (err < 0) { reftable_free(ti); return err; @@ -800,10 +763,11 @@ uint64_t reftable_reader_min_update_index(struct reftable_reader *r) /* generic table interface. */ -static int reftable_reader_seek_void(void *tab, struct reftable_iterator *it, - struct reftable_record *rec) +static void reftable_reader_init_iter_void(void *tab, + struct reftable_iterator *it, + uint8_t typ) { - return reader_seek(tab, it, rec); + reader_init_iter(tab, it, typ); } static uint32_t reftable_reader_hash_id_void(void *tab) @@ -822,7 +786,7 @@ static uint64_t reftable_reader_max_update_index_void(void *tab) } static struct reftable_table_vtable reader_vtable = { - .seek_record = reftable_reader_seek_void, + .init_iter = reftable_reader_init_iter_void, .hash_id = reftable_reader_hash_id_void, .min_update_index = reftable_reader_min_update_index_void, .max_update_index = reftable_reader_max_update_index_void, @@ -856,3 +820,68 @@ done: reftable_reader_free(r); return err; } + +int reftable_reader_print_blocks(const char *tablename) +{ + struct { + const char *name; + int type; + } sections[] = { + { + .name = "ref", + .type = BLOCK_TYPE_REF, + }, + { + .name = "obj", + .type = BLOCK_TYPE_OBJ, + }, + { + .name = "log", + .type = BLOCK_TYPE_LOG, + }, + }; + struct reftable_block_source src = { 0 }; + struct reftable_reader *r = NULL; + struct table_iter ti = { 0 }; + size_t i; + int err; + + err = reftable_block_source_from_file(&src, tablename); + if (err < 0) + goto done; + + err = reftable_new_reader(&r, &src, tablename); + if (err < 0) + goto done; + + table_iter_init(&ti, r); + + printf("header:\n"); + printf(" block_size: %d\n", r->block_size); + + for (i = 0; i < ARRAY_SIZE(sections); i++) { + err = table_iter_seek_start(&ti, sections[i].type, 0); + if (err < 0) + goto done; + if (err > 0) + continue; + + printf("%s:\n", sections[i].name); + + while (1) { + printf(" - length: %u\n", ti.br.block_len); + printf(" restarts: %u\n", ti.br.restart_count); + + err = table_iter_next_block(&ti); + if (err < 0) + goto done; + if (err > 0) + break; + } + } + +done: + reftable_reader_free(r); + table_iter_close(&ti); + return err; +} diff --git a/reftable/readwrite_test.c b/reftable/readwrite_test.c index a6dbd21..d99543b 100644 --- a/reftable/readwrite_test.c +++ b/reftable/readwrite_test.c @@ -239,7 +239,9 @@ static void test_log_write_read(void) err = init_reader(&rd, &source, "file.log"); EXPECT_ERR(err); - err = reftable_reader_seek_ref(&rd, &it, names[N - 1]); + reftable_reader_init_ref_iterator(&rd, &it); + + err = reftable_iterator_seek_ref(&it, names[N - 1]); EXPECT_ERR(err); err = reftable_iterator_next_ref(&it, &ref); @@ -252,7 +254,9 @@ static void test_log_write_read(void) reftable_iterator_destroy(&it); reftable_ref_record_release(&ref); - err = reftable_reader_seek_log(&rd, &it, ""); + reftable_reader_init_log_iterator(&rd, &it); + + err = reftable_iterator_seek_log(&it, ""); EXPECT_ERR(err); i = 0; @@ -330,7 +334,8 @@ static void test_log_zlib_corruption(void) err = init_reader(&rd, &source, "file.log"); EXPECT_ERR(err); - err = reftable_reader_seek_log(&rd, &it, "refname"); + reftable_reader_init_log_iterator(&rd, &it); + err = reftable_iterator_seek_log(&it, "refname"); EXPECT(err == REFTABLE_ZLIB_ERROR); reftable_iterator_destroy(&it); @@ -358,7 +363,8 @@ static void test_table_read_write_sequential(void) err = init_reader(&rd, &source, "file.ref"); EXPECT_ERR(err); - err = reftable_reader_seek_ref(&rd, &it, ""); + reftable_reader_init_ref_iterator(&rd, &it); + err = reftable_iterator_seek_ref(&it, ""); EXPECT_ERR(err); while (1) { @@ -412,7 +418,8 @@ static void test_table_read_api(void) err = init_reader(&rd, &source, "file.ref"); EXPECT_ERR(err); - err = reftable_reader_seek_ref(&rd, &it, names[0]); + reftable_reader_init_ref_iterator(&rd, &it); + err = reftable_iterator_seek_ref(&it, names[0]); EXPECT_ERR(err); err = reftable_iterator_next_log(&it, &log); @@ -457,7 +464,8 @@ static void test_table_read_write_seek(int index, int hash_id) } for (i = 1; i < N; i++) { - int err = reftable_reader_seek_ref(&rd, &it, names[i]); + reftable_reader_init_ref_iterator(&rd, &it); + err = reftable_iterator_seek_ref(&it, names[i]); EXPECT_ERR(err); err = reftable_iterator_next_ref(&it, &ref); EXPECT_ERR(err); @@ -472,7 +480,8 @@ static void test_table_read_write_seek(int index, int hash_id) strbuf_addstr(&pastLast, names[N - 1]); strbuf_addstr(&pastLast, "/"); - err = reftable_reader_seek_ref(&rd, &it, pastLast.buf); + reftable_reader_init_ref_iterator(&rd, &it); + err = reftable_iterator_seek_ref(&it, pastLast.buf); if (err == 0) { struct reftable_ref_record ref = { NULL }; int err = reftable_iterator_next_ref(&it, &ref); @@ -576,7 +585,8 @@ static void test_table_refs_for(int indexed) rd.obj_offsets.is_present = 0; } - err = reftable_reader_seek_ref(&rd, &it, ""); + reftable_reader_init_ref_iterator(&rd, &it); + err = reftable_iterator_seek_ref(&it, ""); EXPECT_ERR(err); reftable_iterator_destroy(&it); @@ -639,7 +649,8 @@ static void test_write_empty_table(void) err = reftable_new_reader(&rd, &source, "filename"); EXPECT_ERR(err); - err = reftable_reader_seek_ref(rd, &it, ""); + reftable_reader_init_ref_iterator(rd, &it); + err = reftable_iterator_seek_ref(&it, ""); EXPECT_ERR(err); err = reftable_iterator_next_ref(&it, &rec); @@ -846,7 +857,8 @@ static void test_write_multiple_indices(void) * Seeking the log uses the log index now. In case there is any * confusion regarding indices we would notice here. */ - err = reftable_reader_seek_log(reader, &it, ""); + reftable_reader_init_log_iterator(reader, &it); + err = reftable_iterator_seek_log(&it, ""); EXPECT_ERR(err); reftable_iterator_destroy(&it); @@ -901,7 +913,8 @@ static void test_write_multi_level_index(void) /* * Seeking the last ref should work as expected. */ - err = reftable_reader_seek_ref(reader, &it, "refs/heads/199"); + reftable_reader_init_ref_iterator(reader, &it); + err = reftable_iterator_seek_ref(&it, "refs/heads/199"); EXPECT_ERR(err); reftable_iterator_destroy(&it); diff --git a/reftable/reftable-generic.h b/reftable/reftable-generic.h index d239751..65670ea 100644 --- a/reftable/reftable-generic.h +++ b/reftable/reftable-generic.h @@ -21,11 +21,11 @@ struct reftable_table { void *table_arg; }; -int reftable_table_seek_log(struct reftable_table *tab, - struct reftable_iterator *it, const char *name); +void reftable_table_init_ref_iter(struct reftable_table *tab, + struct reftable_iterator *it); -int reftable_table_seek_ref(struct reftable_table *tab, - struct reftable_iterator *it, const char *name); +void reftable_table_init_log_iter(struct reftable_table *tab, + struct reftable_iterator *it); /* returns the hash ID from a generic reftable_table */ uint32_t reftable_table_hash_id(struct reftable_table *tab); diff --git a/reftable/reftable-iterator.h b/reftable/reftable-iterator.h index d3eee7a..e3bf688 100644 --- a/reftable/reftable-iterator.h +++ b/reftable/reftable-iterator.h @@ -21,12 +21,33 @@ struct reftable_iterator { void *iter_arg; }; +/* + * Position the iterator at the ref record with given name such that the next + * call to `next_ref()` would yield the record. + */ +int reftable_iterator_seek_ref(struct reftable_iterator *it, + const char *name); + /* reads the next reftable_ref_record. Returns < 0 for error, 0 for OK and > 0: * end of iteration. */ int reftable_iterator_next_ref(struct reftable_iterator *it, struct reftable_ref_record *ref); +/* + * Position the iterator at the log record with given name and update index + * such that the next call to `next_log()` would yield the record. + */ +int reftable_iterator_seek_log_at(struct reftable_iterator *it, + const char *name, uint64_t update_index); + +/* + * Position the iterator at the newest log record with given name such that the + * next call to `next_log()` would yield the record. + */ +int reftable_iterator_seek_log(struct reftable_iterator *it, + const char *name); + /* reads the next reftable_log_record. Returns < 0 for error, 0 for OK and > 0: * end of iteration. */ diff --git a/reftable/reftable-merged.h b/reftable/reftable-merged.h index c91a2d8..14d5fc9 100644 --- a/reftable/reftable-merged.h +++ b/reftable/reftable-merged.h @@ -36,21 +36,6 @@ int reftable_new_merged_table(struct reftable_merged_table **dest, struct reftable_table *stack, size_t n, uint32_t hash_id); -/* returns an iterator positioned just before 'name' */ -int reftable_merged_table_seek_ref(struct reftable_merged_table *mt, - struct reftable_iterator *it, - const char *name); - -/* returns an iterator for log entry, at given update_index */ -int reftable_merged_table_seek_log_at(struct reftable_merged_table *mt, - struct reftable_iterator *it, - const char *name, uint64_t update_index); - -/* like reftable_merged_table_seek_log_at but look for the newest entry. */ -int reftable_merged_table_seek_log(struct reftable_merged_table *mt, - struct reftable_iterator *it, - const char *name); - /* returns the max update_index covered by this merged table. */ uint64_t reftable_merged_table_max_update_index(struct reftable_merged_table *mt); diff --git a/reftable/reftable-reader.h b/reftable/reftable-reader.h index 4a4bc2f..a32f31d 100644 --- a/reftable/reftable-reader.h +++ b/reftable/reftable-reader.h @@ -36,48 +36,17 @@ struct reftable_table; int reftable_new_reader(struct reftable_reader **pp, struct reftable_block_source *src, const char *name); -/* reftable_reader_seek_ref returns an iterator where 'name' would be inserted - in the table. To seek to the start of the table, use name = "". - - example: - - struct reftable_reader *r = NULL; - int err = reftable_new_reader(&r, &src, "filename"); - if (err < 0) { ... } - struct reftable_iterator it = {0}; - err = reftable_reader_seek_ref(r, &it, "refs/heads/master"); - if (err < 0) { ... } - struct reftable_ref_record ref = {0}; - while (1) { - err = reftable_iterator_next_ref(&it, &ref); - if (err > 0) { - break; - } - if (err < 0) { - ..error handling.. - } - ..found.. - } - reftable_iterator_destroy(&it); - reftable_ref_record_release(&ref); -*/ -int reftable_reader_seek_ref(struct reftable_reader *r, - struct reftable_iterator *it, const char *name); +/* Initialize a reftable iterator for reading refs. */ +void reftable_reader_init_ref_iterator(struct reftable_reader *r, + struct reftable_iterator *it); + +/* Initialize a reftable iterator for reading logs. */ +void reftable_reader_init_log_iterator(struct reftable_reader *r, + struct reftable_iterator *it); /* returns the hash ID used in this table. */ uint32_t reftable_reader_hash_id(struct reftable_reader *r); -/* seek to logs for the given name, older than update_index. To seek to the - start of the table, use name = "". -*/ -int reftable_reader_seek_log_at(struct reftable_reader *r, - struct reftable_iterator *it, const char *name, - uint64_t update_index); - -/* seek to newest log entry for given name. */ -int reftable_reader_seek_log(struct reftable_reader *r, - struct reftable_iterator *it, const char *name); - /* closes and deallocates a reader. */ void reftable_reader_free(struct reftable_reader *); @@ -97,5 +66,7 @@ void reftable_table_from_reader(struct reftable_table *tab, /* print table onto stdout for debugging. */ int reftable_reader_print_file(const char *tablename); +/* print blocks onto stdout for debugging. */ +int reftable_reader_print_blocks(const char *tablename); #endif diff --git a/reftable/reftable-stack.h b/reftable/reftable-stack.h index 1b602dd..09e97c9 100644 --- a/reftable/reftable-stack.h +++ b/reftable/reftable-stack.h @@ -29,7 +29,7 @@ struct reftable_stack; * stored in 'dir'. Typically, this should be .git/reftables. */ int reftable_new_stack(struct reftable_stack **dest, const char *dir, - struct reftable_write_options config); + const struct reftable_write_options *opts); /* returns the update_index at which a next table should be written. */ uint64_t reftable_stack_next_update_index(struct reftable_stack *st); @@ -66,6 +66,24 @@ int reftable_stack_add(struct reftable_stack *st, void *write_arg), void *write_arg); +struct reftable_iterator; + +/* + * Initialize an iterator for the merged tables contained in the stack that can + * be used to iterate through refs. The iterator is valid until the next reload + * or write. + */ +void reftable_stack_init_ref_iterator(struct reftable_stack *st, + struct reftable_iterator *it); + +/* + * Initialize an iterator for the merged tables contained in the stack that can + * be used to iterate through logs. The iterator is valid until the next reload + * or write. + */ +void reftable_stack_init_log_iterator(struct reftable_stack *st, + struct reftable_iterator *it); + /* returns the merged_table for seeking. This table is valid until the * next write or reload, and should not be closed or deleted. */ diff --git a/reftable/reftable-writer.h b/reftable/reftable-writer.h index b601a69..189b1f4 100644 --- a/reftable/reftable-writer.h +++ b/reftable/reftable-writer.h @@ -28,7 +28,7 @@ struct reftable_write_options { unsigned skip_index_objects : 1; /* how often to write complete keys in each block. */ - int restart_interval; + uint16_t restart_interval; /* 4-byte identifier ("sha1", "s256") of the hash. * Defaults to SHA1 if unset @@ -45,6 +45,12 @@ struct reftable_write_options { /* boolean: Prevent auto-compaction of tables. */ unsigned disable_auto_compact : 1; + + /* + * Geometric sequence factor used by auto-compaction to decide which + * tables to compact. Defaults to 2 if unset. + */ + uint8_t auto_compaction_factor; }; /* reftable_block_stats holds statistics for a single block type */ @@ -88,7 +94,7 @@ struct reftable_stats { struct reftable_writer * reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t), int (*flush_func)(void *), - void *writer_arg, struct reftable_write_options *opts); + void *writer_arg, const struct reftable_write_options *opts); /* Set the range of update indices for the records we will add. When writing a table into a stack, the min should be at least diff --git a/reftable/stack.c b/reftable/stack.c index a59ebe0..98ac9cf 100644 --- a/reftable/stack.c +++ b/reftable/stack.c @@ -10,6 +10,7 @@ https://developers.google.com/open-source/licenses/bsd #include "../write-or-die.h" #include "system.h" +#include "constants.h" #include "merged.h" #include "reader.h" #include "reftable-error.h" @@ -54,15 +55,17 @@ static int reftable_fd_flush(void *arg) } int reftable_new_stack(struct reftable_stack **dest, const char *dir, - struct reftable_write_options config) + const struct reftable_write_options *_opts) { struct reftable_stack *p = reftable_calloc(1, sizeof(*p)); struct strbuf list_file_name = STRBUF_INIT; + struct reftable_write_options opts = {0}; int err = 0; - if (config.hash_id == 0) { - config.hash_id = GIT_SHA1_FORMAT_ID; - } + if (_opts) + opts = *_opts; + if (opts.hash_id == 0) + opts.hash_id = GIT_SHA1_FORMAT_ID; *dest = NULL; @@ -73,7 +76,7 @@ int reftable_new_stack(struct reftable_stack **dest, const char *dir, p->list_file = strbuf_detach(&list_file_name, NULL); p->list_fd = -1; p->reftable_dir = xstrdup(dir); - p->config = config; + p->opts = opts; err = reftable_stack_reload_maybe_reuse(p, 1); if (err < 0) { @@ -130,6 +133,20 @@ int read_lines(const char *filename, char ***namesp) return err; } +void reftable_stack_init_ref_iterator(struct reftable_stack *st, + struct reftable_iterator *it) +{ + merged_table_init_iter(reftable_stack_merged_table(st), + it, BLOCK_TYPE_REF); +} + +void reftable_stack_init_log_iterator(struct reftable_stack *st, + struct reftable_iterator *it) +{ + merged_table_init_iter(reftable_stack_merged_table(st), + it, BLOCK_TYPE_LOG); +} + struct reftable_merged_table * reftable_stack_merged_table(struct reftable_stack *st) { @@ -255,7 +272,7 @@ static int reftable_stack_reload_once(struct reftable_stack *st, char **names, /* success! */ err = reftable_new_merged_table(&new_merged, new_tables, - new_readers_len, st->config.hash_id); + new_readers_len, st->opts.hash_id); if (err < 0) goto done; @@ -578,8 +595,8 @@ static int reftable_stack_init_addition(struct reftable_addition *add, } goto done; } - if (st->config.default_permissions) { - if (chmod(add->lock_file->filename.buf, st->config.default_permissions) < 0) { + if (st->opts.default_permissions) { + if (chmod(add->lock_file->filename.buf, st->opts.default_permissions) < 0) { err = REFTABLE_IO_ERROR; goto done; } @@ -678,7 +695,7 @@ int reftable_addition_commit(struct reftable_addition *add) if (err) goto done; - if (!add->stack->config.disable_auto_compact) { + if (!add->stack->opts.disable_auto_compact) { /* * Auto-compact the stack to keep the number of tables in * control. It is possible that a concurrent writer is already @@ -756,9 +773,9 @@ int reftable_addition_add(struct reftable_addition *add, err = REFTABLE_IO_ERROR; goto done; } - if (add->stack->config.default_permissions) { + if (add->stack->opts.default_permissions) { if (chmod(get_tempfile_path(tab_file), - add->stack->config.default_permissions)) { + add->stack->opts.default_permissions)) { err = REFTABLE_IO_ERROR; goto done; } @@ -766,7 +783,7 @@ int reftable_addition_add(struct reftable_addition *add, tab_fd = get_tempfile_fd(tab_file); wr = reftable_new_writer(reftable_fd_write, reftable_fd_flush, &tab_fd, - &add->stack->config); + &add->stack->opts); err = write_table(wr, arg); if (err < 0) goto done; @@ -849,14 +866,14 @@ static int stack_compact_locked(struct reftable_stack *st, } tab_fd = get_tempfile_fd(tab_file); - if (st->config.default_permissions && - chmod(get_tempfile_path(tab_file), st->config.default_permissions) < 0) { + if (st->opts.default_permissions && + chmod(get_tempfile_path(tab_file), st->opts.default_permissions) < 0) { err = REFTABLE_IO_ERROR; goto done; } wr = reftable_new_writer(reftable_fd_write, reftable_fd_flush, - &tab_fd, &st->config); + &tab_fd, &st->opts); err = stack_write_compact(st, wr, first, last, config); if (err < 0) goto done; @@ -904,13 +921,14 @@ static int stack_write_compact(struct reftable_stack *st, st->readers[last]->max_update_index); err = reftable_new_merged_table(&mt, subtabs, subtabs_len, - st->config.hash_id); + st->opts.hash_id); if (err < 0) { reftable_free(subtabs); goto done; } - err = reftable_merged_table_seek_ref(mt, &it, ""); + merged_table_init_iter(mt, &it, BLOCK_TYPE_REF); + err = reftable_iterator_seek_ref(&it, ""); if (err < 0) goto done; @@ -934,7 +952,8 @@ static int stack_write_compact(struct reftable_stack *st, } reftable_iterator_destroy(&it); - err = reftable_merged_table_seek_log(mt, &it, ""); + merged_table_init_iter(mt, &it, BLOCK_TYPE_LOG); + err = reftable_iterator_seek_log(&it, ""); if (err < 0) goto done; @@ -1094,9 +1113,9 @@ static int stack_compact_range(struct reftable_stack *st, goto done; } - if (st->config.default_permissions) { + if (st->opts.default_permissions) { if (chmod(get_lock_file_path(&tables_list_lock), - st->config.default_permissions) < 0) { + st->opts.default_permissions) < 0) { err = REFTABLE_IO_ERROR; goto done; } @@ -1210,12 +1229,16 @@ static int segment_size(struct segment *s) return s->end - s->start; } -struct segment suggest_compaction_segment(uint64_t *sizes, size_t n) +struct segment suggest_compaction_segment(uint64_t *sizes, size_t n, + uint8_t factor) { struct segment seg = { 0 }; uint64_t bytes; size_t i; + if (!factor) + factor = DEFAULT_GEOMETRIC_FACTOR; + /* * If there are no tables or only a single one then we don't have to * compact anything. The sequence is geometric by definition already. @@ -1247,7 +1270,7 @@ struct segment suggest_compaction_segment(uint64_t *sizes, size_t n) * 64, 32, 16, 8, 4, 3, 1 */ for (i = n - 1; i > 0; i--) { - if (sizes[i - 1] < sizes[i] * 2) { + if (sizes[i - 1] < sizes[i] * factor) { seg.end = i + 1; bytes = sizes[i]; break; @@ -1273,7 +1296,7 @@ struct segment suggest_compaction_segment(uint64_t *sizes, size_t n) uint64_t curr = bytes; bytes += sizes[i - 1]; - if (sizes[i - 1] < curr * 2) { + if (sizes[i - 1] < curr * factor) { seg.start = i - 1; seg.bytes = bytes; } @@ -1286,7 +1309,7 @@ static uint64_t *stack_table_sizes_for_compaction(struct reftable_stack *st) { uint64_t *sizes = reftable_calloc(st->merged->stack_len, sizeof(*sizes)); - int version = (st->config.hash_id == GIT_SHA1_FORMAT_ID) ? 1 : 2; + int version = (st->opts.hash_id == GIT_SHA1_FORMAT_ID) ? 1 : 2; int overhead = header_size(version) - 1; int i = 0; for (i = 0; i < st->merged->stack_len; i++) { @@ -1299,7 +1322,8 @@ int reftable_stack_auto_compact(struct reftable_stack *st) { uint64_t *sizes = stack_table_sizes_for_compaction(st); struct segment seg = - suggest_compaction_segment(sizes, st->merged->stack_len); + suggest_compaction_segment(sizes, st->merged->stack_len, + st->opts.auto_compaction_factor); reftable_free(sizes); if (segment_size(&seg) > 0) return stack_compact_range_stats(st, seg.start, seg.end - 1, @@ -1325,9 +1349,11 @@ int reftable_stack_read_ref(struct reftable_stack *st, const char *refname, int reftable_stack_read_log(struct reftable_stack *st, const char *refname, struct reftable_log_record *log) { - struct reftable_iterator it = { NULL }; - struct reftable_merged_table *mt = reftable_stack_merged_table(st); - int err = reftable_merged_table_seek_log(mt, &it, refname); + struct reftable_iterator it = {0}; + int err; + + reftable_stack_init_log_iterator(st, &it); + err = reftable_iterator_seek_log(&it, refname); if (err) goto done; @@ -1435,11 +1461,11 @@ done: int reftable_stack_print_directory(const char *stackdir, uint32_t hash_id) { struct reftable_stack *stack = NULL; - struct reftable_write_options cfg = { .hash_id = hash_id }; + struct reftable_write_options opts = { .hash_id = hash_id }; struct reftable_merged_table *merged = NULL; struct reftable_table table = { NULL }; - int err = reftable_new_stack(&stack, stackdir, cfg); + int err = reftable_new_stack(&stack, stackdir, &opts); if (err < 0) goto done; diff --git a/reftable/stack.h b/reftable/stack.h index d43efa4..5b45cff 100644 --- a/reftable/stack.h +++ b/reftable/stack.h @@ -20,7 +20,7 @@ struct reftable_stack { char *reftable_dir; - struct reftable_write_options config; + struct reftable_write_options opts; struct reftable_reader **readers; size_t readers_len; @@ -35,6 +35,7 @@ struct segment { uint64_t bytes; }; -struct segment suggest_compaction_segment(uint64_t *sizes, size_t n); +struct segment suggest_compaction_segment(uint64_t *sizes, size_t n, + uint8_t factor); #endif diff --git a/reftable/stack_test.c b/reftable/stack_test.c index 7889f81..0f7b145 100644 --- a/reftable/stack_test.c +++ b/reftable/stack_test.c @@ -150,7 +150,7 @@ static void test_reftable_stack_add_one(void) char *dir = get_tmp_dir(__LINE__); struct strbuf scratch = STRBUF_INIT; int mask = umask(002); - struct reftable_write_options cfg = { + struct reftable_write_options opts = { .default_permissions = 0660, }; struct reftable_stack *st = NULL; @@ -163,7 +163,7 @@ static void test_reftable_stack_add_one(void) }; struct reftable_ref_record dest = { NULL }; struct stat stat_result = { 0 }; - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); err = reftable_stack_add(st, &write_test_ref, &ref); @@ -186,7 +186,7 @@ static void test_reftable_stack_add_one(void) strbuf_addstr(&scratch, "/tables.list"); err = stat(scratch.buf, &stat_result); EXPECT(!err); - EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions); + EXPECT((stat_result.st_mode & 0777) == opts.default_permissions); strbuf_reset(&scratch); strbuf_addstr(&scratch, dir); @@ -195,7 +195,7 @@ static void test_reftable_stack_add_one(void) strbuf_addstr(&scratch, st->readers[0]->name); err = stat(scratch.buf, &stat_result); EXPECT(!err); - EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions); + EXPECT((stat_result.st_mode & 0777) == opts.default_permissions); #else (void) stat_result; #endif @@ -209,7 +209,7 @@ static void test_reftable_stack_add_one(void) static void test_reftable_stack_uptodate(void) { - struct reftable_write_options cfg = { 0 }; + struct reftable_write_options opts = { 0 }; struct reftable_stack *st1 = NULL; struct reftable_stack *st2 = NULL; char *dir = get_tmp_dir(__LINE__); @@ -232,10 +232,10 @@ static void test_reftable_stack_uptodate(void) /* simulate multi-process access to the same stack by creating two stacks for the same directory. */ - err = reftable_new_stack(&st1, dir, cfg); + err = reftable_new_stack(&st1, dir, &opts); EXPECT_ERR(err); - err = reftable_new_stack(&st2, dir, cfg); + err = reftable_new_stack(&st2, dir, &opts); EXPECT_ERR(err); err = reftable_stack_add(st1, &write_test_ref, &ref1); @@ -257,8 +257,7 @@ static void test_reftable_stack_uptodate(void) static void test_reftable_stack_transaction_api(void) { char *dir = get_tmp_dir(__LINE__); - - struct reftable_write_options cfg = { 0 }; + struct reftable_write_options opts = { 0 }; struct reftable_stack *st = NULL; int err; struct reftable_addition *add = NULL; @@ -271,8 +270,7 @@ static void test_reftable_stack_transaction_api(void) }; struct reftable_ref_record dest = { NULL }; - - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); reftable_addition_destroy(add); @@ -301,12 +299,12 @@ static void test_reftable_stack_transaction_api(void) static void test_reftable_stack_transaction_api_performs_auto_compaction(void) { char *dir = get_tmp_dir(__LINE__); - struct reftable_write_options cfg = {0}; + struct reftable_write_options opts = {0}; struct reftable_addition *add = NULL; struct reftable_stack *st = NULL; int i, n = 20, err; - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); for (i = 0; i <= n; i++) { @@ -325,7 +323,7 @@ static void test_reftable_stack_transaction_api_performs_auto_compaction(void) * we can ensure that we indeed honor this setting and have * better control over when exactly auto compaction runs. */ - st->config.disable_auto_compact = i != n; + st->opts.disable_auto_compact = i != n; err = reftable_stack_new_addition(&add, st); EXPECT_ERR(err); @@ -361,13 +359,13 @@ static void test_reftable_stack_auto_compaction_fails_gracefully(void) .value_type = REFTABLE_REF_VAL1, .value.val1 = {0x01}, }; - struct reftable_write_options cfg = {0}; + struct reftable_write_options opts = {0}; struct reftable_stack *st; struct strbuf table_path = STRBUF_INIT; char *dir = get_tmp_dir(__LINE__); int err; - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); err = reftable_stack_add(st, write_test_ref, &ref); @@ -404,8 +402,7 @@ static int write_error(struct reftable_writer *wr, void *arg) static void test_reftable_stack_update_index_check(void) { char *dir = get_tmp_dir(__LINE__); - - struct reftable_write_options cfg = { 0 }; + struct reftable_write_options opts = { 0 }; struct reftable_stack *st = NULL; int err; struct reftable_ref_record ref1 = { @@ -421,7 +418,7 @@ static void test_reftable_stack_update_index_check(void) .value.symref = "master", }; - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); err = reftable_stack_add(st, &write_test_ref, &ref1); @@ -436,12 +433,11 @@ static void test_reftable_stack_update_index_check(void) static void test_reftable_stack_lock_failure(void) { char *dir = get_tmp_dir(__LINE__); - - struct reftable_write_options cfg = { 0 }; + struct reftable_write_options opts = { 0 }; struct reftable_stack *st = NULL; int err, i; - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); for (i = -1; i != REFTABLE_EMPTY_TABLE_ERROR; i--) { err = reftable_stack_add(st, &write_error, &i); @@ -456,7 +452,7 @@ static void test_reftable_stack_add(void) { int i = 0; int err = 0; - struct reftable_write_options cfg = { + struct reftable_write_options opts = { .exact_log_message = 1, .default_permissions = 0660, .disable_auto_compact = 1, @@ -469,7 +465,7 @@ static void test_reftable_stack_add(void) struct stat stat_result; int N = ARRAY_SIZE(refs); - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); for (i = 0; i < N; i++) { @@ -528,7 +524,7 @@ static void test_reftable_stack_add(void) strbuf_addstr(&path, "/tables.list"); err = stat(path.buf, &stat_result); EXPECT(!err); - EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions); + EXPECT((stat_result.st_mode & 0777) == opts.default_permissions); strbuf_reset(&path); strbuf_addstr(&path, dir); @@ -537,7 +533,7 @@ static void test_reftable_stack_add(void) strbuf_addstr(&path, st->readers[0]->name); err = stat(path.buf, &stat_result); EXPECT(!err); - EXPECT((stat_result.st_mode & 0777) == cfg.default_permissions); + EXPECT((stat_result.st_mode & 0777) == opts.default_permissions); #else (void) stat_result; #endif @@ -555,7 +551,7 @@ static void test_reftable_stack_add(void) static void test_reftable_stack_log_normalize(void) { int err = 0; - struct reftable_write_options cfg = { + struct reftable_write_options opts = { 0, }; struct reftable_stack *st = NULL; @@ -579,7 +575,7 @@ static void test_reftable_stack_log_normalize(void) .update_index = 1, }; - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); input.value.update.message = "one\ntwo"; @@ -612,8 +608,7 @@ static void test_reftable_stack_tombstone(void) { int i = 0; char *dir = get_tmp_dir(__LINE__); - - struct reftable_write_options cfg = { 0 }; + struct reftable_write_options opts = { 0 }; struct reftable_stack *st = NULL; int err; struct reftable_ref_record refs[2] = { { NULL } }; @@ -622,8 +617,7 @@ static void test_reftable_stack_tombstone(void) struct reftable_ref_record dest = { NULL }; struct reftable_log_record log_dest = { NULL }; - - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); /* even entries add the refs, odd entries delete them. */ @@ -691,8 +685,7 @@ static void test_reftable_stack_tombstone(void) static void test_reftable_stack_hash_id(void) { char *dir = get_tmp_dir(__LINE__); - - struct reftable_write_options cfg = { 0 }; + struct reftable_write_options opts = { 0 }; struct reftable_stack *st = NULL; int err; @@ -702,24 +695,24 @@ static void test_reftable_stack_hash_id(void) .value.symref = "target", .update_index = 1, }; - struct reftable_write_options cfg32 = { .hash_id = GIT_SHA256_FORMAT_ID }; + struct reftable_write_options opts32 = { .hash_id = GIT_SHA256_FORMAT_ID }; struct reftable_stack *st32 = NULL; - struct reftable_write_options cfg_default = { 0 }; + struct reftable_write_options opts_default = { 0 }; struct reftable_stack *st_default = NULL; struct reftable_ref_record dest = { NULL }; - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); err = reftable_stack_add(st, &write_test_ref, &ref); EXPECT_ERR(err); /* can't read it with the wrong hash ID. */ - err = reftable_new_stack(&st32, dir, cfg32); + err = reftable_new_stack(&st32, dir, &opts32); EXPECT(err == REFTABLE_FORMAT_ERROR); - /* check that we can read it back with default config too. */ - err = reftable_new_stack(&st_default, dir, cfg_default); + /* check that we can read it back with default opts too. */ + err = reftable_new_stack(&st_default, dir, &opts_default); EXPECT_ERR(err); err = reftable_stack_read_ref(st_default, "master", &dest); @@ -736,7 +729,7 @@ static void test_suggest_compaction_segment(void) { uint64_t sizes[] = { 512, 64, 17, 16, 9, 9, 9, 16, 2, 16 }; struct segment min = - suggest_compaction_segment(sizes, ARRAY_SIZE(sizes)); + suggest_compaction_segment(sizes, ARRAY_SIZE(sizes), 2); EXPECT(min.start == 1); EXPECT(min.end == 10); } @@ -745,15 +738,14 @@ static void test_suggest_compaction_segment_nothing(void) { uint64_t sizes[] = { 64, 32, 16, 8, 4, 2 }; struct segment result = - suggest_compaction_segment(sizes, ARRAY_SIZE(sizes)); + suggest_compaction_segment(sizes, ARRAY_SIZE(sizes), 2); EXPECT(result.start == result.end); } static void test_reflog_expire(void) { char *dir = get_tmp_dir(__LINE__); - - struct reftable_write_options cfg = { 0 }; + struct reftable_write_options opts = { 0 }; struct reftable_stack *st = NULL; struct reftable_log_record logs[20] = { { NULL } }; int N = ARRAY_SIZE(logs) - 1; @@ -764,8 +756,7 @@ static void test_reflog_expire(void) }; struct reftable_log_record log = { NULL }; - - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); for (i = 1; i <= N; i++) { @@ -828,21 +819,19 @@ static int write_nothing(struct reftable_writer *wr, void *arg) static void test_empty_add(void) { - struct reftable_write_options cfg = { 0 }; + struct reftable_write_options opts = { 0 }; struct reftable_stack *st = NULL; int err; char *dir = get_tmp_dir(__LINE__); - struct reftable_stack *st2 = NULL; - - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); err = reftable_stack_add(st, &write_nothing, NULL); EXPECT_ERR(err); - err = reftable_new_stack(&st2, dir, cfg); + err = reftable_new_stack(&st2, dir, &opts); EXPECT_ERR(err); clear_dir(dir); reftable_stack_destroy(st); @@ -861,16 +850,15 @@ static int fastlog2(uint64_t sz) static void test_reftable_stack_auto_compaction(void) { - struct reftable_write_options cfg = { + struct reftable_write_options opts = { .disable_auto_compact = 1, }; struct reftable_stack *st = NULL; char *dir = get_tmp_dir(__LINE__); - int err, i; int N = 100; - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); for (i = 0; i < N; i++) { @@ -900,13 +888,13 @@ static void test_reftable_stack_auto_compaction(void) static void test_reftable_stack_add_performs_auto_compaction(void) { - struct reftable_write_options cfg = { 0 }; + struct reftable_write_options opts = { 0 }; struct reftable_stack *st = NULL; struct strbuf refname = STRBUF_INIT; char *dir = get_tmp_dir(__LINE__); int err, i, n = 20; - err = reftable_new_stack(&st, dir, cfg); + err = reftable_new_stack(&st, dir, &opts); EXPECT_ERR(err); for (i = 0; i <= n; i++) { @@ -921,7 +909,7 @@ static void test_reftable_stack_add_performs_auto_compaction(void) * we can ensure that we indeed honor this setting and have * better control over when exactly auto compaction runs. */ - st->config.disable_auto_compact = i != n; + st->opts.disable_auto_compact = i != n; strbuf_reset(&refname); strbuf_addf(&refname, "branch-%04d", i); @@ -948,14 +936,13 @@ static void test_reftable_stack_add_performs_auto_compaction(void) static void test_reftable_stack_compaction_concurrent(void) { - struct reftable_write_options cfg = { 0 }; + struct reftable_write_options opts = { 0 }; struct reftable_stack *st1 = NULL, *st2 = NULL; char *dir = get_tmp_dir(__LINE__); - int err, i; int N = 3; - err = reftable_new_stack(&st1, dir, cfg); + err = reftable_new_stack(&st1, dir, &opts); EXPECT_ERR(err); for (i = 0; i < N; i++) { @@ -972,7 +959,7 @@ static void test_reftable_stack_compaction_concurrent(void) EXPECT_ERR(err); } - err = reftable_new_stack(&st2, dir, cfg); + err = reftable_new_stack(&st2, dir, &opts); EXPECT_ERR(err); err = reftable_stack_compact_all(st1, NULL); @@ -998,14 +985,13 @@ static void unclean_stack_close(struct reftable_stack *st) static void test_reftable_stack_compaction_concurrent_clean(void) { - struct reftable_write_options cfg = { 0 }; + struct reftable_write_options opts = { 0 }; struct reftable_stack *st1 = NULL, *st2 = NULL, *st3 = NULL; char *dir = get_tmp_dir(__LINE__); - int err, i; int N = 3; - err = reftable_new_stack(&st1, dir, cfg); + err = reftable_new_stack(&st1, dir, &opts); EXPECT_ERR(err); for (i = 0; i < N; i++) { @@ -1022,7 +1008,7 @@ static void test_reftable_stack_compaction_concurrent_clean(void) EXPECT_ERR(err); } - err = reftable_new_stack(&st2, dir, cfg); + err = reftable_new_stack(&st2, dir, &opts); EXPECT_ERR(err); err = reftable_stack_compact_all(st1, NULL); @@ -1031,7 +1017,7 @@ static void test_reftable_stack_compaction_concurrent_clean(void) unclean_stack_close(st1); unclean_stack_close(st2); - err = reftable_new_stack(&st3, dir, cfg); + err = reftable_new_stack(&st3, dir, &opts); EXPECT_ERR(err); err = reftable_stack_clean(st3); diff --git a/reftable/writer.c b/reftable/writer.c index 10eccaa..45b3e9c 100644 --- a/reftable/writer.c +++ b/reftable/writer.c @@ -117,25 +117,26 @@ static void writer_reinit_block_writer(struct reftable_writer *w, uint8_t typ) w->block_writer->restart_interval = w->opts.restart_interval; } -static struct strbuf reftable_empty_strbuf = STRBUF_INIT; - struct reftable_writer * reftable_new_writer(ssize_t (*writer_func)(void *, const void *, size_t), int (*flush_func)(void *), - void *writer_arg, struct reftable_write_options *opts) + void *writer_arg, const struct reftable_write_options *_opts) { struct reftable_writer *wp = reftable_calloc(1, sizeof(*wp)); + struct reftable_write_options opts = {0}; + + if (_opts) + opts = *_opts; + options_set_defaults(&opts); + if (opts.block_size >= (1 << 24)) + BUG("configured block size exceeds 16MB"); + strbuf_init(&wp->block_writer_data.last_key, 0); - options_set_defaults(opts); - if (opts->block_size >= (1 << 24)) { - /* TODO - error return? */ - abort(); - } - wp->last_key = reftable_empty_strbuf; - REFTABLE_CALLOC_ARRAY(wp->block, opts->block_size); + strbuf_init(&wp->last_key, 0); + REFTABLE_CALLOC_ARRAY(wp->block, opts.block_size); wp->write = writer_func; wp->write_arg = writer_arg; - wp->opts = *opts; + wp->opts = opts; wp->flush = flush_func; writer_reinit_block_writer(wp, BLOCK_TYPE_REF); diff --git a/remote-curl.c b/remote-curl.c index cae9838..6008d7e 100644 --- a/remote-curl.c +++ b/remote-curl.c @@ -266,12 +266,23 @@ static struct ref *parse_git_refs(struct discovery *heads, int for_push) return list; } +/* + * Try to detect the hash algorithm used by the remote repository when using + * the dumb HTTP transport. As dumb transports cannot tell us the object hash + * directly have to derive it from the advertised ref lengths. + */ static const struct git_hash_algo *detect_hash_algo(struct discovery *heads) { const char *p = memchr(heads->buf, '\t', heads->len); int algo; + + /* + * In case the remote has no refs we have no way to reliably determine + * the object hash used by that repository. In that case we simply fall + * back to SHA1, which may or may not be correct. + */ if (!p) - return the_hash_algo; + return &hash_algos[GIT_HASH_SHA1]; algo = hash_algo_by_length((p - heads->buf) / 2); if (algo == GIT_HASH_UNKNOWN) @@ -295,6 +306,12 @@ static struct ref *parse_info_refs(struct discovery *heads) "is this a git repository?", transport_anonymize_url(url.buf)); + /* + * Set the repository's hash algo to whatever we have just detected. + * This ensures that we can correctly parse the remote references. + */ + repo_set_hash_algo(the_repository, hash_algo_by_ptr(options.hash_algo)); + data = heads->buf; start = NULL; mid = data; @@ -305,7 +305,7 @@ static void read_remotes_file(struct remote_state *remote_state, static void read_branches_file(struct remote_state *remote_state, struct remote *remote) { - char *frag; + char *frag, *to_free = NULL; struct strbuf buf = STRBUF_INIT; FILE *f = fopen_or_warn(git_path("branches/%s", remote->name), "r"); @@ -333,7 +333,7 @@ static void read_branches_file(struct remote_state *remote_state, if (frag) *(frag++) = '\0'; else - frag = (char *)git_default_branch_name(0); + frag = to_free = repo_default_branch_name(the_repository, 0); add_url_alias(remote_state, remote, strbuf_detach(&buf, NULL)); refspec_appendf(&remote->fetch, "refs/heads/%s:refs/heads/%s", @@ -345,6 +345,8 @@ static void read_branches_file(struct remote_state *remote_state, */ refspec_appendf(&remote->push, "HEAD:refs/heads/%s", frag); remote->fetch_tags = 1; /* always auto-follow */ + + free(to_free); } static int handle_config(const char *key, const char *value, @@ -1198,8 +1200,10 @@ static char *guess_ref(const char *name, struct ref *peer) { struct strbuf buf = STRBUF_INIT; - const char *r = resolve_ref_unsafe(peer->name, RESOLVE_REF_READING, - NULL, NULL); + const char *r = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + peer->name, + RESOLVE_REF_READING, + NULL, NULL); if (!r) return NULL; @@ -1316,9 +1320,10 @@ static int match_explicit(struct ref *src, struct ref *dst, if (!dst_value) { int flag; - dst_value = resolve_ref_unsafe(matched_src->name, - RESOLVE_REF_READING, - NULL, &flag); + dst_value = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + matched_src->name, + RESOLVE_REF_READING, + NULL, &flag); if (!dst_value || ((flag & REF_ISSYMREF) && !starts_with(dst_value, "refs/heads/"))) @@ -1882,7 +1887,7 @@ const char *branch_get_upstream(struct branch *branch, struct strbuf *err) * or because it is not a real branch, and get_branch * auto-vivified it? */ - if (!ref_exists(branch->refname)) + if (!refs_ref_exists(get_main_ref_store(the_repository), branch->refname)) return error_buf(err, _("no such branch: '%s'"), branch->name); return error_buf(err, @@ -2168,13 +2173,13 @@ static int stat_branch_pair(const char *branch_name, const char *base, struct strvec argv = STRVEC_INIT; /* Cannot stat if what we used to build on no longer exists */ - if (read_ref(base, &oid)) + if (refs_read_ref(get_main_ref_store(the_repository), base, &oid)) return -1; theirs = lookup_commit_reference(the_repository, &oid); if (!theirs) return -1; - if (read_ref(branch_name, &oid)) + if (refs_read_ref(get_main_ref_store(the_repository), branch_name, &oid)) return -1; ours = lookup_commit_reference(the_repository, &oid); if (!ours) @@ -2278,7 +2283,8 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb, upstream_is_gone = 1; } - base = shorten_unambiguous_ref(full_base, 0); + base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), + full_base, 0); if (upstream_is_gone) { strbuf_addf(sb, _("Your branch is based on '%s', but the upstream is gone.\n"), @@ -2358,7 +2364,8 @@ struct ref *get_local_heads(void) { struct ref *local_refs = NULL, **local_tail = &local_refs; - for_each_ref(one_local_ref, &local_tail); + refs_for_each_ref(get_main_ref_store(the_repository), one_local_ref, + &local_tail); return local_refs; } @@ -2383,11 +2390,13 @@ struct ref *guess_remote_head(const struct ref *head, /* If a remote branch exists with the default branch name, let's use it. */ if (!all) { - char *ref = xstrfmt("refs/heads/%s", - git_default_branch_name(0)); + char *default_branch = repo_default_branch_name(the_repository, 0); + char *ref = xstrfmt("refs/heads/%s", default_branch); r = find_ref_by_name(refs, ref); free(ref); + free(default_branch); + if (r && oideq(&r->old_oid, &head->old_oid)) return copy_ref(r); @@ -2468,7 +2477,8 @@ struct ref *get_stale_heads(struct refspec *rs, struct ref *fetch_map) for (ref = fetch_map; ref; ref = ref->next) string_list_append(&ref_names, ref->name); string_list_sort(&ref_names); - for_each_ref(get_stale_heads_cb, &info); + refs_for_each_ref(get_main_ref_store(the_repository), + get_stale_heads_cb, &info); string_list_clear(&ref_names, 0); return stale_refs; } @@ -2553,7 +2563,7 @@ static int remote_tracking(struct remote *remote, const char *refname, dst = apply_refspecs(&remote->fetch, refname); if (!dst) return -1; /* no tracking ref for refname at remote */ - if (read_ref(dst, oid)) + if (refs_read_ref(get_main_ref_store(the_repository), dst, oid)) return -1; /* we know what the tracking ref is but we cannot read it */ *dst_refname = dst; @@ -2659,12 +2669,16 @@ static int is_reachable_in_reflog(const char *local, const struct ref *remote) * Get the timestamp from the latest entry * of the remote-tracking ref's reflog. */ - for_each_reflog_ent_reverse(remote->tracking_ref, peek_reflog, &date); + refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), + remote->tracking_ref, peek_reflog, + &date); cb.remote_commit = commit; cb.local_commits = &arr; cb.remote_reflog_timestamp = date; - ret = for_each_reflog_ent_reverse(local, check_and_collect_until, &cb); + ret = refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), + local, check_and_collect_until, + &cb); /* We found an entry in the reflog. */ if (ret > 0) diff --git a/replace-object.c b/replace-object.c index 5232155..73f5acb 100644 --- a/replace-object.c +++ b/replace-object.c @@ -8,12 +8,13 @@ #include "repository.h" #include "commit.h" -static int register_replace_ref(struct repository *r, - const char *refname, +static int register_replace_ref(const char *refname, const struct object_id *oid, int flag UNUSED, - void *cb_data UNUSED) + void *cb_data) { + struct repository *r = cb_data; + /* Get sha1 from refname */ const char *slash = strrchr(refname, '/'); const char *hash = slash ? slash + 1 : refname; @@ -50,7 +51,8 @@ void prepare_replace_object(struct repository *r) xmalloc(sizeof(*r->objects->replace_map)); oidmap_init(r->objects->replace_map, 0); - for_each_replace_ref(r, register_replace_ref, NULL); + refs_for_each_replace_ref(get_main_ref_store(r), + register_replace_ref, r); r->objects->replace_map_initialized = 1; pthread_mutex_unlock(&r->objects->replace_mutex); diff --git a/repository.c b/repository.c index 2118f56..deb6862 100644 --- a/repository.c +++ b/repository.c @@ -14,11 +14,34 @@ #include "sparse-index.h" #include "trace2.h" #include "promisor-remote.h" +#include "refs.h" /* The main repository */ static struct repository the_repo; struct repository *the_repository = &the_repo; +/* + * An escape hatch: if we hit a bug in the production code that fails + * to set an appropriate hash algorithm (most likely to happen when + * running outside a repository), we can tell the user who reported + * the crash to set the environment variable to "sha1" (all lowercase) + * to revert to the historical behaviour of defaulting to SHA-1. + */ +static void set_default_hash_algo(struct repository *repo) +{ + const char *hash_name; + int algo; + + hash_name = getenv("GIT_TEST_DEFAULT_HASH_ALGO"); + if (!hash_name) + return; + algo = hash_algo_by_name(hash_name); + if (algo == GIT_HASH_UNKNOWN) + return; + + repo_set_hash_algo(repo, algo); +} + void initialize_repository(struct repository *repo) { repo->objects = raw_object_store_new(); @@ -28,24 +51,26 @@ void initialize_repository(struct repository *repo) index_state_init(repo->index, repo); /* - * Unfortunately, we need to keep this hack around for the time being: + * When a command runs inside a repository, it learns what + * hash algorithm is in use from the repository, but some + * commands are designed to work outside a repository, yet + * they want to access the_hash_algo, if only for the length + * of the hashed value to see if their input looks like a + * plausible hash value. * - * - Not setting up the hash algorithm for `the_repository` leads to - * crashes because `the_hash_algo` is a macro that expands to - * `the_repository->hash_algo`. So if Git commands try to access - * `the_hash_algo` without a Git directory we crash. + * We are in the process of identifying such code paths and + * giving them an appropriate default individually; any + * unconverted code paths that try to access the_hash_algo + * will thus fail. The end-users however have an escape hatch + * to set GIT_TEST_DEFAULT_HASH_ALGO environment variable to + * "sha1" to get back the old behaviour of defaulting to SHA-1. * - * - Setting up the hash algorithm to be SHA1 by default breaks other - * commands when running with SHA256. - * - * This is another point in case why having global state is a bad idea. - * Eventually, we should remove this hack and stop setting the hash - * algorithm in this function altogether. Instead, it should only ever - * be set via our repository setup procedures. But that requires more - * work. + * This escape hatch is deliberately kept unadvertised, so + * that they see crashes and we can get a report before + * telling them about it. */ if (repo == the_repository) - repo_set_hash_algo(repo, GIT_HASH_SHA1); + set_default_hash_algo(repo); } static void expand_base_dir(char **out, const char *in, @@ -289,6 +314,9 @@ static void repo_clear_path_cache(struct repo_path_cache *cache) void repo_clear(struct repository *repo) { + struct hashmap_iter iter; + struct strmap_entry *e; + FREE_AND_NULL(repo->gitdir); FREE_AND_NULL(repo->commondir); FREE_AND_NULL(repo->graft_file); @@ -302,6 +330,8 @@ void repo_clear(struct repository *repo) parsed_object_pool_clear(repo->parsed_objects); FREE_AND_NULL(repo->parsed_objects); + FREE_AND_NULL(repo->settings.fsmonitor); + if (repo->config) { git_configset_clear(repo->config); FREE_AND_NULL(repo->config); @@ -327,6 +357,14 @@ void repo_clear(struct repository *repo) FREE_AND_NULL(repo->remote_state); } + strmap_for_each_entry(&repo->submodule_ref_stores, &iter, e) + ref_store_release(e->value); + strmap_clear(&repo->submodule_ref_stores, 1); + + strmap_for_each_entry(&repo->worktree_ref_stores, &iter, e) + ref_store_release(e->value); + strmap_clear(&repo->worktree_ref_stores, 1); + repo_clear_path_cache(&repo->cached_paths); } diff --git a/repository.h b/repository.h index 41ed225..4bd8969 100644 --- a/repository.h +++ b/repository.h @@ -1,6 +1,8 @@ #ifndef REPOSITORY_H #define REPOSITORY_H +#include "strmap.h" + struct config_set; struct fsmonitor_settings; struct git_hash_algo; @@ -109,6 +111,18 @@ struct repository { struct ref_store *refs_private; /* + * A strmap of ref_stores, stored by submodule name, accessible via + * `repo_get_submodule_ref_store()`. + */ + struct strmap submodule_ref_stores; + + /* + * A strmap of ref_stores, stored by worktree id, accessible via + * `get_worktree_ref_store()`. + */ + struct strmap worktree_ref_stores; + + /* * Contains path to often used file names. */ struct repo_path_cache cached_paths; @@ -47,11 +47,13 @@ static int update_refs(const struct reset_head_opts *opts, strbuf_addstr(&msg, "updating ORIG_HEAD"); reflog_orig_head = msg.buf; } - update_ref(reflog_orig_head, "ORIG_HEAD", - orig_head ? orig_head : head, - old_orig, 0, UPDATE_REFS_MSG_ON_ERR); + refs_update_ref(get_main_ref_store(the_repository), + reflog_orig_head, "ORIG_HEAD", + orig_head ? orig_head : head, + old_orig, 0, UPDATE_REFS_MSG_ON_ERR); } else if (old_orig) - delete_ref(NULL, "ORIG_HEAD", old_orig, 0); + refs_delete_ref(get_main_ref_store(the_repository), + NULL, "ORIG_HEAD", old_orig, 0); } if (!reflog_head) { @@ -60,16 +62,19 @@ static int update_refs(const struct reset_head_opts *opts, reflog_head = msg.buf; } if (!switch_to_branch) - ret = update_ref(reflog_head, "HEAD", oid, head, - detach_head ? REF_NO_DEREF : 0, - UPDATE_REFS_MSG_ON_ERR); + ret = refs_update_ref(get_main_ref_store(the_repository), + reflog_head, "HEAD", oid, head, + detach_head ? REF_NO_DEREF : 0, + UPDATE_REFS_MSG_ON_ERR); else { - ret = update_ref(reflog_branch ? reflog_branch : reflog_head, - switch_to_branch, oid, NULL, 0, - UPDATE_REFS_MSG_ON_ERR); + ret = refs_update_ref(get_main_ref_store(the_repository), + reflog_branch ? reflog_branch : reflog_head, + switch_to_branch, oid, NULL, 0, + UPDATE_REFS_MSG_ON_ERR); if (!ret) - ret = create_symref("HEAD", switch_to_branch, - reflog_head); + ret = refs_update_symref(get_main_ref_store(the_repository), + "HEAD", switch_to_branch, + reflog_head); } if (!ret && run_hook) run_hooks_l("post-checkout", @@ -1738,7 +1738,8 @@ void add_reflogs_to_pending(struct rev_info *revs, unsigned flags) cb.all_revs = revs; cb.all_flags = flags; cb.wt = NULL; - for_each_reflog(handle_one_reflog, &cb); + refs_for_each_reflog(get_main_ref_store(the_repository), + handle_one_reflog, &cb); if (!revs->single_worktree) add_other_reflogs_to_pending(&cb); @@ -1979,9 +1980,9 @@ static const char *lookup_other_head(struct object_id *oid) }; for (i = 0; i < ARRAY_SIZE(other_head); i++) - if (!read_ref_full(other_head[i], - RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, - oid, NULL)) { + if (!refs_read_ref_full(get_main_ref_store(the_repository), other_head[i], + RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, + oid, NULL)) { if (is_null_oid(oid)) die(_("%s exists but is a symbolic ref"), other_head[i]); return other_head[i]; @@ -2789,7 +2790,8 @@ static int handle_revision_pseudo_opt(struct rev_info *revs, } else if ((argcount = parse_long_opt("glob", argv, &optarg))) { struct all_refs_cb cb; init_all_refs_cb(&cb, revs, *flags); - for_each_glob_ref(handle_one_ref, optarg, &cb); + refs_for_each_glob_ref(get_main_ref_store(the_repository), + handle_one_ref, optarg, &cb); clear_ref_exclusions(&revs->ref_excludes); return argcount; } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) { @@ -2804,7 +2806,9 @@ static int handle_revision_pseudo_opt(struct rev_info *revs, return error(_("options '%s' and '%s' cannot be used together"), "--exclude-hidden", "--branches"); init_all_refs_cb(&cb, revs, *flags); - for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb); + refs_for_each_glob_ref_in(get_main_ref_store(the_repository), + handle_one_ref, optarg, + "refs/heads/", &cb); clear_ref_exclusions(&revs->ref_excludes); } else if (skip_prefix(arg, "--tags=", &optarg)) { struct all_refs_cb cb; @@ -2812,7 +2816,9 @@ static int handle_revision_pseudo_opt(struct rev_info *revs, return error(_("options '%s' and '%s' cannot be used together"), "--exclude-hidden", "--tags"); init_all_refs_cb(&cb, revs, *flags); - for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb); + refs_for_each_glob_ref_in(get_main_ref_store(the_repository), + handle_one_ref, optarg, + "refs/tags/", &cb); clear_ref_exclusions(&revs->ref_excludes); } else if (skip_prefix(arg, "--remotes=", &optarg)) { struct all_refs_cb cb; @@ -2820,7 +2826,9 @@ static int handle_revision_pseudo_opt(struct rev_info *revs, return error(_("options '%s' and '%s' cannot be used together"), "--exclude-hidden", "--remotes"); init_all_refs_cb(&cb, revs, *flags); - for_each_glob_ref_in(handle_one_ref, optarg, "refs/remotes/", &cb); + refs_for_each_glob_ref_in(get_main_ref_store(the_repository), + handle_one_ref, optarg, + "refs/remotes/", &cb); clear_ref_exclusions(&revs->ref_excludes); } else if (!strcmp(arg, "--reflog")) { add_reflogs_to_pending(revs, *flags); @@ -2911,7 +2919,8 @@ static void NORETURN diagnose_missing_default(const char *def) int flags; const char *refname; - refname = resolve_ref_unsafe(def, 0, NULL, &flags); + refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + def, 0, NULL, &flags); if (!refname || !(flags & REF_ISSYMREF) || (flags & REF_ISBROKEN)) die(_("your current branch appears to be broken")); @@ -645,7 +645,6 @@ static int cmd_reconfigure(int argc, const char **argv) }; struct string_list scalar_repos = STRING_LIST_INIT_DUP; int i, res = 0; - struct repository r = { NULL }; struct strbuf commondir = STRBUF_INIT, gitdir = STRBUF_INIT; argc = parse_options(argc, argv, NULL, options, @@ -665,6 +664,7 @@ static int cmd_reconfigure(int argc, const char **argv) for (i = 0; i < scalar_repos.nr; i++) { int succeeded = 0; + struct repository *old_repo, r = { NULL }; const char *dir = scalar_repos.items[i].string; strbuf_reset(&commondir); @@ -712,13 +712,17 @@ static int cmd_reconfigure(int argc, const char **argv) git_config_clear(); + if (repo_init(&r, gitdir.buf, commondir.buf)) + goto loop_end; + + old_repo = the_repository; the_repository = &r; - r.commondir = commondir.buf; - r.gitdir = gitdir.buf; if (set_recommended_config(1) >= 0) succeeded = 1; + the_repository = old_repo; + loop_end: if (!succeeded) { res = -1; diff --git a/sequencer.c b/sequencer.c index 88de4dc..aa2a239 100644 --- a/sequencer.c +++ b/sequencer.c @@ -266,7 +266,7 @@ static struct update_ref_record *init_update_ref_record(const char *ref) oidcpy(&rec->after, null_oid()); /* This may fail, but that's fine, we will keep the null OID. */ - read_ref(ref, &rec->before); + refs_read_ref(get_main_ref_store(the_repository), ref, &rec->before); return rec; } @@ -359,35 +359,32 @@ static const char *get_todo_path(const struct replay_opts *opts) static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, size_t ignore_footer) { - struct process_trailer_options opts = PROCESS_TRAILER_OPTIONS_INIT; - struct trailer_info info; - size_t i; + struct trailer_iterator iter; + size_t i = 0; int found_sob = 0, found_sob_last = 0; char saved_char; - opts.no_divider = 1; - if (ignore_footer) { saved_char = sb->buf[sb->len - ignore_footer]; sb->buf[sb->len - ignore_footer] = '\0'; } - trailer_info_get(&opts, sb->buf, &info); + trailer_iterator_init(&iter, sb->buf); if (ignore_footer) sb->buf[sb->len - ignore_footer] = saved_char; - if (info.trailer_block_start == info.trailer_block_end) - return 0; + while (trailer_iterator_advance(&iter)) { + i++; + if (sob && !strncmp(iter.raw, sob->buf, sob->len)) + found_sob = i; + } + trailer_iterator_release(&iter); - for (i = 0; i < info.trailer_nr; i++) - if (sob && !strncmp(info.trailers[i], sob->buf, sob->len)) { - found_sob = 1; - if (i == info.trailer_nr - 1) - found_sob_last = 1; - } + if (!i) + return 0; - trailer_info_release(&info); + found_sob_last = (int)i == found_sob; if (found_sob_last) return 3; @@ -440,7 +437,7 @@ int sequencer_remove_state(struct replay_opts *opts) char *eol = strchr(p, '\n'); if (eol) *eol = '\0'; - if (delete_ref("(rebase) cleanup", p, NULL, 0) < 0) { + if (refs_delete_ref(get_main_ref_store(the_repository), "(rebase) cleanup", p, NULL, 0) < 0) { warning(_("could not delete '%s'"), p); ret = -1; } @@ -661,11 +658,12 @@ static int fast_forward_to(struct repository *r, strbuf_addf(&sb, "%s: fast-forward", action_name(opts)); - transaction = ref_transaction_begin(&err); + transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!transaction || ref_transaction_update(transaction, "HEAD", to, unborn && !is_rebase_i(opts) ? - null_oid() : from, + null_oid() : from, NULL, NULL, 0, sb.buf, &err) || ref_transaction_commit(transaction, &err)) { ref_transaction_free(transaction); @@ -841,11 +839,12 @@ static int is_index_unchanged(struct repository *r) struct index_state *istate = r->index; const char *head_name; - if (!resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, &head_oid, NULL)) { + if (!refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING, &head_oid, NULL)) { /* Check to see if this is an unborn branch */ - head_name = resolve_ref_unsafe("HEAD", - RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, - &head_oid, NULL); + head_name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + "HEAD", + RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, + &head_oid, NULL); if (!head_name || !starts_with(head_name, "refs/heads/") || !is_null_oid(&head_oid)) @@ -1294,11 +1293,12 @@ int update_head_with_reflog(const struct commit *old_head, strbuf_addch(&sb, '\n'); } - transaction = ref_transaction_begin(err); + transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + err); if (!transaction || ref_transaction_update(transaction, "HEAD", new_head, old_head ? &old_head->object.oid : null_oid(), - 0, sb.buf, err) || + NULL, NULL, 0, sb.buf, err) || ref_transaction_commit(transaction, err)) { ret = -1; } @@ -1720,8 +1720,8 @@ out: static int write_rebase_head(struct object_id *oid) { - if (update_ref("rebase", "REBASE_HEAD", oid, - NULL, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) + if (refs_update_ref(get_main_ref_store(the_repository), "rebase", "REBASE_HEAD", oid, + NULL, REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) return error(_("could not update %s"), "REBASE_HEAD"); return 0; @@ -2455,12 +2455,12 @@ static int do_pick_commit(struct repository *r, if ((command == TODO_PICK || command == TODO_REWORD || command == TODO_EDIT) && !opts->no_commit && (res == 0 || res == 1) && - update_ref(NULL, "CHERRY_PICK_HEAD", &commit->object.oid, NULL, - REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) + refs_update_ref(get_main_ref_store(the_repository), NULL, "CHERRY_PICK_HEAD", &commit->object.oid, NULL, + REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) res = -1; if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) && - update_ref(NULL, "REVERT_HEAD", &commit->object.oid, NULL, - REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) + refs_update_ref(get_main_ref_store(the_repository), NULL, "REVERT_HEAD", &commit->object.oid, NULL, + REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) res = -1; if (res) { @@ -3364,7 +3364,7 @@ static int rollback_single_pick(struct repository *r) if (!refs_ref_exists(get_main_ref_store(r), "CHERRY_PICK_HEAD") && !refs_ref_exists(get_main_ref_store(r), "REVERT_HEAD")) return error(_("no cherry-pick or revert in progress")); - if (read_ref_full("HEAD", 0, &head_oid, NULL)) + if (refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", 0, &head_oid, NULL)) return error(_("cannot resolve HEAD")); if (is_null_oid(&head_oid)) return error(_("cannot abort from a branch yet to be born")); @@ -3375,7 +3375,7 @@ static int skip_single_pick(void) { struct object_id head; - if (read_ref_full("HEAD", 0, &head, NULL)) + if (refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", 0, &head, NULL)) return error(_("cannot resolve HEAD")); return reset_merge(&head); } @@ -3832,8 +3832,9 @@ static int do_label(struct repository *r, const char *name, int len) } else if (repo_get_oid(r, "HEAD", &head_oid)) { error(_("could not read HEAD")); ret = -1; - } else if (ref_transaction_update(transaction, ref_name.buf, &head_oid, - NULL, 0, msg.buf, &err) < 0 || + } else if (ref_transaction_update(transaction, ref_name.buf, + &head_oid, NULL, NULL, NULL, + 0, msg.buf, &err) < 0 || ref_transaction_commit(transaction, &err)) { error("%s", err.buf); ret = -1; @@ -3891,7 +3892,7 @@ static struct commit *lookup_label(struct repository *r, const char *label, strbuf_reset(buf); strbuf_addf(buf, "refs/rewritten/%.*s", len, label); - if (!read_ref(buf->buf, &oid)) { + if (!refs_read_ref(get_main_ref_store(the_repository), buf->buf, &oid)) { commit = lookup_commit_object(r, &oid); } else { /* fall back to non-rewritten ref or commit */ @@ -3987,9 +3988,10 @@ static int do_reset(struct repository *r, ret = error(_("could not write index")); if (!ret) - ret = update_ref(reflog_message(opts, "reset", "'%.*s'", - len, name), "HEAD", &oid, - NULL, 0, UPDATE_REFS_MSG_ON_ERR); + ret = refs_update_ref(get_main_ref_store(the_repository), reflog_message(opts, "reset", "'%.*s'", + len, name), + "HEAD", &oid, + NULL, 0, UPDATE_REFS_MSG_ON_ERR); cleanup: free((void *)desc.buffer); if (ret < 0) @@ -4471,7 +4473,7 @@ static int do_update_ref(struct repository *r, const char *refname) for_each_string_list_item(item, &list) { if (!strcmp(item->string, refname)) { struct update_ref_record *rec = item->util; - if (read_ref("HEAD", &rec->after)) + if (refs_read_ref(get_main_ref_store(the_repository), "HEAD", &rec->after)) return -1; break; } @@ -5031,15 +5033,15 @@ cleanup_head_ref: } msg = reflog_message(opts, "finish", "%s onto %s", head_ref.buf, buf.buf); - if (update_ref(msg, head_ref.buf, &head, &orig, - REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) { + if (refs_update_ref(get_main_ref_store(the_repository), msg, head_ref.buf, &head, &orig, + REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) { res = error(_("could not update %s"), head_ref.buf); goto cleanup_head_ref; } msg = reflog_message(opts, "finish", "returning to %s", head_ref.buf); - if (create_symref("HEAD", head_ref.buf, msg)) { + if (refs_update_symref(get_main_ref_store(the_repository), "HEAD", head_ref.buf, msg)) { res = error(_("could not update HEAD to %s"), head_ref.buf); goto cleanup_head_ref; @@ -6209,10 +6211,11 @@ static int add_decorations_to_list(const struct commit *commit, struct todo_add_branch_context *ctx) { const struct name_decoration *decoration = get_name_decoration(&commit->object); - const char *head_ref = resolve_ref_unsafe("HEAD", - RESOLVE_REF_READING, - NULL, - NULL); + const char *head_ref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + "HEAD", + RESOLVE_REF_READING, + NULL, + NULL); while (decoration) { struct todo_item *item; diff --git a/server-info.c b/server-info.c index e2fe0f9..6feaa45 100644 --- a/server-info.c +++ b/server-info.c @@ -175,7 +175,8 @@ static int add_info_ref(const char *path, const struct object_id *oid, static int generate_info_refs(struct update_info_ctx *uic) { - return for_each_ref(add_info_ref, uic); + return refs_for_each_ref(get_main_ref_store(the_repository), + add_info_ref, uic); } static int update_info_refs(int force) @@ -4,6 +4,7 @@ #include "environment.h" #include "exec-cmd.h" #include "gettext.h" +#include "hex.h" #include "object-name.h" #include "refs.h" #include "repository.h" @@ -16,6 +17,7 @@ #include "quote.h" #include "trace2.h" #include "worktree.h" +#include "exec-cmd.h" static int inside_git_dir = -1; static int inside_work_tree = -1; @@ -341,6 +343,58 @@ int get_common_dir_noenv(struct strbuf *sb, const char *gitdir) return ret; } +static int validate_headref(const char *path) +{ + struct stat st; + char buffer[256]; + const char *refname; + struct object_id oid; + int fd; + ssize_t len; + + if (lstat(path, &st) < 0) + return -1; + + /* Make sure it is a "refs/.." symlink */ + if (S_ISLNK(st.st_mode)) { + len = readlink(path, buffer, sizeof(buffer)-1); + if (len >= 5 && !memcmp("refs/", buffer, 5)) + return 0; + return -1; + } + + /* + * Anything else, just open it and try to see if it is a symbolic ref. + */ + fd = open(path, O_RDONLY); + if (fd < 0) + return -1; + len = read_in_full(fd, buffer, sizeof(buffer)-1); + close(fd); + + if (len < 0) + return -1; + buffer[len] = '\0'; + + /* + * Is it a symbolic ref? + */ + if (skip_prefix(buffer, "ref:", &refname)) { + while (isspace(*refname)) + refname++; + if (starts_with(refname, "refs/")) + return 0; + } + + /* + * Is this a detached HEAD? + */ + if (get_oid_hex_any(buffer, &oid) != GIT_HASH_UNKNOWN) + return 0; + + return -1; +} + /* * Test if it looks like we're at a git directory. * We want to see: @@ -1220,6 +1274,27 @@ static int ensure_valid_ownership(const char *gitfile, return data.is_safe; } +void die_upon_dubious_ownership(const char *gitfile, const char *worktree, + const char *gitdir) +{ + struct strbuf report = STRBUF_INIT, quoted = STRBUF_INIT; + const char *path; + + if (ensure_valid_ownership(gitfile, worktree, gitdir, &report)) + return; + + strbuf_complete(&report, '\n'); + path = gitfile ? gitfile : gitdir; + sq_quote_buf_pretty("ed, path); + + die(_("detected dubious ownership in repository at '%s'\n" + "%s" + "To add an exception for this directory, call:\n" + "\n" + "\tgit config --global --add safe.directory %s"), + path, report.buf, quoted.buf); +} + static int allowed_bare_repo_cb(const char *key, const char *value, const struct config_context *ctx UNUSED, void *d) @@ -1781,6 +1856,57 @@ int daemonize(void) #endif } +struct template_dir_cb_data { + char *path; + int initialized; +}; + +static int template_dir_cb(const char *key, const char *value, + const struct config_context *ctx, void *d) +{ + struct template_dir_cb_data *data = d; + + if (strcmp(key, "init.templatedir")) + return 0; + + if (!value) { + data->path = NULL; + } else { + char *path = NULL; + + FREE_AND_NULL(data->path); + if (!git_config_pathname((const char **)&path, key, value)) + data->path = path ? path : xstrdup(value); + } + + return 0; +} + +const char *get_template_dir(const char *option_template) +{ + const char *template_dir = option_template; + + if (!template_dir) + template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); + if (!template_dir) { + static struct template_dir_cb_data data; + + if (!data.initialized) { + git_protected_config(template_dir_cb, &data); + data.initialized = 1; + } + template_dir = data.path; + } + if (!template_dir) { + static char *dir; + + if (!dir) + dir = system_path(DEFAULT_GIT_TEMPLATE_DIR); + template_dir = dir; + } + return template_dir; +} + #ifdef NO_TRUSTABLE_FILEMODE #define TEST_FILEMODE 0 #else @@ -1856,8 +1982,9 @@ static void copy_templates_1(struct strbuf *path, struct strbuf *template_path, } } -static void copy_templates(const char *template_dir, const char *init_template_dir) +static void copy_templates(const char *option_template) { + const char *template_dir = get_template_dir(option_template); struct strbuf path = STRBUF_INIT; struct strbuf template_path = STRBUF_INIT; size_t template_len; @@ -1866,16 +1993,8 @@ static void copy_templates(const char *template_dir, const char *init_template_d DIR *dir; char *to_free = NULL; - if (!template_dir) - template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT); - if (!template_dir) - template_dir = init_template_dir; - if (!template_dir) - template_dir = to_free = system_path(DEFAULT_GIT_TEMPLATE_DIR); - if (!template_dir[0]) { - free(to_free); + if (!template_dir || !*template_dir) return; - } strbuf_addstr(&template_path, template_dir); strbuf_complete(&template_path, '/'); @@ -1980,10 +2099,11 @@ void create_reference_database(unsigned int ref_storage_format, const char *initial_branch, int quiet) { struct strbuf err = STRBUF_INIT; + char *to_free = NULL; int reinit = is_reinit(); repo_set_ref_storage_format(the_repository, ref_storage_format); - if (refs_init_db(get_main_ref_store(the_repository), 0, &err)) + if (ref_store_create_on_disk(get_main_ref_store(the_repository), 0, &err)) die("failed to set up refs db: %s", err.buf); /* @@ -1994,14 +2114,15 @@ void create_reference_database(unsigned int ref_storage_format, char *ref; if (!initial_branch) - initial_branch = git_default_branch_name(quiet); + initial_branch = to_free = + repo_default_branch_name(the_repository, quiet); ref = xstrfmt("refs/heads/%s", initial_branch); if (check_refname_format(ref, 0) < 0) die(_("invalid initial branch name: '%s'"), initial_branch); - if (create_symref("HEAD", ref, NULL) < 0) + if (refs_update_symref(get_main_ref_store(the_repository), "HEAD", ref, NULL) < 0) exit(1); free(ref); } @@ -2011,6 +2132,7 @@ void create_reference_database(unsigned int ref_storage_format, initial_branch); strbuf_release(&err); + free(to_free); } static int create_default_files(const char *template_path, @@ -2023,7 +2145,6 @@ static int create_default_files(const char *template_path, char *path; int reinit; int filemode; - const char *init_template_dir = NULL; const char *work_tree = get_git_work_tree(); /* @@ -2035,9 +2156,7 @@ static int create_default_files(const char *template_path, * values (since we've just potentially changed what's available on * disk). */ - git_config_get_pathname("init.templatedir", &init_template_dir); - copy_templates(template_path, init_template_dir); - free((char *)init_template_dir); + copy_templates(template_path); git_config_clear(); reset_shared_repository(); git_config(git_default_config, NULL); @@ -41,6 +41,18 @@ const char *read_gitfile_gently(const char *path, int *return_error_code); const char *resolve_gitdir_gently(const char *suspect, int *return_error_code); #define resolve_gitdir(path) resolve_gitdir_gently((path), NULL) +/* + * Check if a repository is safe and die if it is not, by verifying the + * ownership of the worktree (if any), the git directory, and the gitfile (if + * any). + * + * Exemptions for known-safe repositories can be added via `safe.directory` + * config settings; for non-bare repositories, their worktree needs to be + * added, for bare ones their git directory. + */ +void die_upon_dubious_ownership(const char *gitfile, const char *worktree, + const char *gitdir); + void setup_work_tree(void); /* @@ -172,6 +184,8 @@ int verify_repository_format(const struct repository_format *format, */ void check_repository_format(struct repository_format *fmt); +const char *get_template_dir(const char *option_template); + #define INIT_DB_QUIET (1 << 0) #define INIT_DB_EXIST_OK (1 << 1) #define INIT_DB_SKIP_REFDB (1 << 2) @@ -678,8 +678,10 @@ void assign_shallow_commits_to_refs(struct shallow_info *info, * connect to old refs. If not (e.g. force ref updates) it'll * have to go down to the current shallow commits. */ - head_ref(mark_uninteresting, NULL); - for_each_ref(mark_uninteresting, NULL); + refs_head_ref(get_main_ref_store(the_repository), mark_uninteresting, + NULL); + refs_for_each_ref(get_main_ref_store(the_repository), + mark_uninteresting, NULL); /* Mark potential bottoms so we won't go out of bound */ for (i = 0; i < nr_shallow; i++) { @@ -782,8 +784,8 @@ static void post_assign_shallow(struct shallow_info *info, info->nr_theirs = dst; memset(&ca, 0, sizeof(ca)); - head_ref(add_ref, &ca); - for_each_ref(add_ref, &ca); + refs_head_ref(get_main_ref_store(the_repository), add_ref, &ca); + refs_for_each_ref(get_main_ref_store(the_repository), add_ref, &ca); /* Remove unreachable shallow commits from "ours" */ for (i = dst = 0; i < info->nr_ours; i++) { @@ -822,8 +824,10 @@ int delayed_reachability_test(struct shallow_info *si, int c) struct commit_array ca; memset(&ca, 0, sizeof(ca)); - head_ref(add_ref, &ca); - for_each_ref(add_ref, &ca); + refs_head_ref(get_main_ref_store(the_repository), + add_ref, &ca); + refs_for_each_ref(get_main_ref_store(the_repository), + add_ref, &ca); si->commits = ca.commits; si->nr_commits = ca.nr; } diff --git a/submodule.c b/submodule.c index ce2d032..759cf1e 100644 --- a/submodule.c +++ b/submodule.c @@ -99,7 +99,8 @@ int is_staging_gitmodules_ok(struct index_state *istate) static int for_each_remote_ref_submodule(const char *submodule, each_ref_fn fn, void *cb_data) { - return refs_for_each_remote_ref(get_submodule_ref_store(submodule), + return refs_for_each_remote_ref(repo_get_submodule_ref_store(the_repository, + submodule), fn, cb_data); } @@ -1015,6 +1016,9 @@ static int submodule_has_commits(struct repository *r, .super_oid = super_oid }; + if (validate_submodule_path(path) < 0) + exit(128); + oid_array_for_each_unique(commits, check_has_commit, &has_commit); if (has_commit.result) { @@ -1137,6 +1141,9 @@ static int push_submodule(const char *path, const struct string_list *push_options, int dry_run) { + if (validate_submodule_path(path) < 0) + exit(128); + if (for_each_remote_ref_submodule(path, has_remote, NULL) > 0) { struct child_process cp = CHILD_PROCESS_INIT; strvec_push(&cp.args, "push"); @@ -1186,6 +1193,9 @@ static void submodule_push_check(const char *path, const char *head, struct child_process cp = CHILD_PROCESS_INIT; int i; + if (validate_submodule_path(path) < 0) + exit(128); + strvec_push(&cp.args, "submodule--helper"); strvec_push(&cp.args, "push-check"); strvec_push(&cp.args, head); @@ -1233,7 +1243,8 @@ int push_unpushed_submodules(struct repository *r, char *head; struct object_id head_oid; - head = resolve_refdup("HEAD", 0, &head_oid, NULL); + head = refs_resolve_refdup(get_main_ref_store(the_repository), + "HEAD", 0, &head_oid, NULL); if (!head) die(_("Failed to resolve HEAD as a valid ref.")); @@ -1271,7 +1282,8 @@ static int append_oid_to_array(const char *ref UNUSED, void check_for_new_submodule_commits(struct object_id *oid) { if (!initialized_fetch_ref_tips) { - for_each_ref(append_oid_to_array, &ref_tips_before_fetch); + refs_for_each_ref(get_main_ref_store(the_repository), + append_oid_to_array, &ref_tips_before_fetch); initialized_fetch_ref_tips = 1; } @@ -1517,6 +1529,9 @@ static struct fetch_task *fetch_task_create(struct submodule_parallel_fetch *spf struct fetch_task *task = xmalloc(sizeof(*task)); memset(task, 0, sizeof(*task)); + if (validate_submodule_path(path) < 0) + exit(128); + task->sub = submodule_from_path(spf->r, treeish_name, path); if (!task->sub) { @@ -1878,6 +1893,9 @@ unsigned is_submodule_modified(const char *path, int ignore_untracked) const char *git_dir; int ignore_cp_exit_code = 0; + if (validate_submodule_path(path) < 0) + exit(128); + strbuf_addf(&buf, "%s/.git", path); git_dir = read_gitfile(buf.buf); if (!git_dir) @@ -1954,6 +1972,9 @@ int submodule_uses_gitfile(const char *path) struct strbuf buf = STRBUF_INIT; const char *git_dir; + if (validate_submodule_path(path) < 0) + exit(128); + strbuf_addf(&buf, "%s/.git", path); git_dir = read_gitfile(buf.buf); if (!git_dir) { @@ -1993,6 +2014,9 @@ int bad_to_remove_submodule(const char *path, unsigned flags) struct strbuf buf = STRBUF_INIT; int ret = 0; + if (validate_submodule_path(path) < 0) + exit(128); + if (!file_exists(path) || is_empty_dir(path)) return 0; @@ -2043,6 +2067,9 @@ void submodule_unset_core_worktree(const struct submodule *sub) { struct strbuf config_path = STRBUF_INIT; + if (validate_submodule_path(sub->path) < 0) + exit(128); + submodule_name_to_gitdir(&config_path, the_repository, sub->name); strbuf_addstr(&config_path, "/config"); @@ -2057,6 +2084,9 @@ static int submodule_has_dirty_index(const struct submodule *sub) { struct child_process cp = CHILD_PROCESS_INIT; + if (validate_submodule_path(sub->path) < 0) + exit(128); + prepare_submodule_repo_env(&cp.env); cp.git_cmd = 1; @@ -2074,6 +2104,10 @@ static int submodule_has_dirty_index(const struct submodule *sub) static void submodule_reset_index(const char *path, const char *super_prefix) { struct child_process cp = CHILD_PROCESS_INIT; + + if (validate_submodule_path(path) < 0) + exit(128); + prepare_submodule_repo_env(&cp.env); cp.git_cmd = 1; @@ -2137,10 +2171,27 @@ int submodule_move_head(const char *path, const char *super_prefix, if (!submodule_uses_gitfile(path)) absorb_git_dir_into_superproject(path, super_prefix); + else { + char *dotgit = xstrfmt("%s/.git", path); + char *git_dir = xstrdup(read_gitfile(dotgit)); + + free(dotgit); + if (validate_submodule_git_dir(git_dir, + sub->name) < 0) + die(_("refusing to create/use '%s' in " + "another submodule's git dir"), + git_dir); + free(git_dir); + } } else { struct strbuf gitdir = STRBUF_INIT; submodule_name_to_gitdir(&gitdir, the_repository, sub->name); + if (validate_submodule_git_dir(gitdir.buf, + sub->name) < 0) + die(_("refusing to create/use '%s' in another " + "submodule's git dir"), + gitdir.buf); connect_work_tree_and_git_dir(path, gitdir.buf, 0); strbuf_release(&gitdir); @@ -2261,6 +2312,34 @@ int validate_submodule_git_dir(char *git_dir, const char *submodule_name) return 0; } +int validate_submodule_path(const char *path) +{ + char *p = xstrdup(path); + struct stat st; + int i, ret = 0; + char sep; + + for (i = 0; !ret && p[i]; i++) { + if (!is_dir_sep(p[i])) + continue; + + sep = p[i]; + p[i] = '\0'; + /* allow missing components, but no symlinks */ + ret = lstat(p, &st) || !S_ISLNK(st.st_mode) ? 0 : -1; + p[i] = sep; + if (ret) + error(_("expected '%.*s' in submodule path '%s' not to " + "be a symbolic link"), i, p, p); + } + if (!lstat(p, &st) && S_ISLNK(st.st_mode)) + ret = error(_("expected submodule path '%s' not to be a " + "symbolic link"), p); + free(p); + return ret; +} + + /* * Embeds a single submodules git directory into the superprojects git dir, * non recursively. @@ -2272,6 +2351,9 @@ static void relocate_single_git_dir_into_superproject(const char *path, struct strbuf new_gitdir = STRBUF_INIT; const struct submodule *sub; + if (validate_submodule_path(path) < 0) + exit(128); + if (submodule_uses_worktrees(path)) die(_("relocate_gitdir for submodule '%s' with " "more than one worktree not supported"), path); @@ -2313,6 +2395,9 @@ static void absorb_git_dir_into_superproject_recurse(const char *path, struct child_process cp = CHILD_PROCESS_INIT; + if (validate_submodule_path(path) < 0) + exit(128); + cp.dir = path; cp.git_cmd = 1; cp.no_stdin = 1; @@ -2337,6 +2422,10 @@ void absorb_git_dir_into_superproject(const char *path, int err_code; const char *sub_git_dir; struct strbuf gitdir = STRBUF_INIT; + + if (validate_submodule_path(path) < 0) + exit(128); + strbuf_addf(&gitdir, "%s/.git", path); sub_git_dir = resolve_gitdir_gently(gitdir.buf, &err_code); @@ -2479,6 +2568,9 @@ int submodule_to_gitdir(struct strbuf *buf, const char *submodule) const char *git_dir; int ret = 0; + if (validate_submodule_path(submodule) < 0) + exit(128); + strbuf_reset(buf); strbuf_addstr(buf, submodule); strbuf_complete(buf, '/'); diff --git a/submodule.h b/submodule.h index c55a25c..b50d29e 100644 --- a/submodule.h +++ b/submodule.h @@ -148,6 +148,11 @@ void submodule_name_to_gitdir(struct strbuf *buf, struct repository *r, */ int validate_submodule_git_dir(char *git_dir, const char *submodule_name); +/* + * Make sure that the given submodule path does not follow symlinks. + */ +int validate_submodule_path(const char *path); + #define SUBMODULE_MOVE_HEAD_DRY_RUN (1<<0) #define SUBMODULE_MOVE_HEAD_FORCE (1<<1) int submodule_move_head(const char *path, const char *super_prefix, @@ -48,7 +48,8 @@ CHAINLINTTESTS = $(sort $(patsubst chainlint/%.test,%,$(wildcard chainlint/*.tes CHAINLINT = '$(PERL_PATH_SQ)' chainlint.pl UNIT_TEST_SOURCES = $(wildcard unit-tests/t-*.c) UNIT_TEST_PROGRAMS = $(patsubst unit-tests/%.c,unit-tests/bin/%$(X),$(UNIT_TEST_SOURCES)) -UNIT_TESTS = $(sort $(filter-out unit-tests/bin/t-basic%,$(UNIT_TEST_PROGRAMS))) +UNIT_TESTS = $(sort $(UNIT_TEST_PROGRAMS)) +UNIT_TESTS_NO_DIR = $(notdir $(UNIT_TESTS)) # `test-chainlint` (which is a dependency of `test-lint`, `test` and `prove`) # checks all tests in all scripts via a single invocation, so tell individual @@ -67,7 +68,7 @@ failed: test -z "$$failed" || $(MAKE) $$failed prove: pre-clean check-chainlint $(TEST_LINT) - @echo "*** prove ***"; $(CHAINLINTSUPPRESS) $(PROVE) --exec '$(TEST_SHELL_PATH_SQ)' $(GIT_PROVE_OPTS) $(T) :: $(GIT_TEST_OPTS) + @echo "*** prove (shell & unit tests) ***"; $(CHAINLINTSUPPRESS) TEST_SHELL_PATH='$(TEST_SHELL_PATH_SQ)' $(PROVE) --exec ./run-test.sh $(GIT_PROVE_OPTS) $(T) $(UNIT_TESTS) :: $(GIT_TEST_OPTS) $(MAKE) clean-except-prove-cache $(T): @@ -76,7 +77,7 @@ $(T): $(UNIT_TESTS): @echo "*** $@ ***"; $@ -.PHONY: unit-tests unit-tests-raw unit-tests-prove +.PHONY: unit-tests unit-tests-raw unit-tests-prove unit-tests-test-tool unit-tests: $(DEFAULT_UNIT_TEST_TARGET) unit-tests-raw: $(UNIT_TESTS) @@ -84,6 +85,13 @@ unit-tests-raw: $(UNIT_TESTS) unit-tests-prove: @echo "*** prove - unit tests ***"; $(PROVE) $(GIT_PROVE_OPTS) $(UNIT_TESTS) +unit-tests-test-tool: + @echo "*** test-tool - unit tests **" + ( \ + cd unit-tests/bin && \ + ../../helper/test-tool$X run-command testsuite $(UNIT_TESTS_NO_DIR)\ + ) + pre-clean: $(RM) -r '$(TEST_RESULTS_DIRECTORY_SQ)' diff --git a/t/chainlint.pl b/t/chainlint.pl index 556ee91..1bbd985 100755 --- a/t/chainlint.pl +++ b/t/chainlint.pl @@ -716,11 +716,25 @@ sub fd_colors { sub ncores { # Windows - return $ENV{NUMBER_OF_PROCESSORS} if exists($ENV{NUMBER_OF_PROCESSORS}); + if (exists($ENV{NUMBER_OF_PROCESSORS})) { + my $ncpu = $ENV{NUMBER_OF_PROCESSORS}; + return $ncpu > 0 ? $ncpu : 1; + } # Linux / MSYS2 / Cygwin / WSL - do { local @ARGV='/proc/cpuinfo'; return scalar(grep(/^processor[\s\d]*:/, <>)); } if -r '/proc/cpuinfo'; + if (open my $fh, '<', '/proc/cpuinfo') { + my $cpuinfo = do { local $/; <$fh> }; + close($fh); + if ($cpuinfo =~ /^n?cpus active\s*:\s*(\d+)/m) { + return $1 if $1 > 0; + } + my @matches = ($cpuinfo =~ /^(processor|CPU)[\s\d]*:/mg); + return @matches ? scalar(@matches) : 1; + } # macOS & BSD - return qx/sysctl -n hw.ncpu/ if $^O =~ /(?:^darwin$|bsd)/; + if ($^O =~ /(?:^darwin$|bsd)/) { + my $ncpu = qx/sysctl -n hw.ncpu/; + return $ncpu > 0 ? $ncpu : 1; + } return 1; } diff --git a/t/unit-tests/t-basic.c b/t/helper/test-example-tap.c index fda1ae5..d072ad5 100644 --- a/t/unit-tests/t-basic.c +++ b/t/helper/test-example-tap.c @@ -1,4 +1,5 @@ -#include "test-lib.h" +#include "test-tool.h" +#include "t/unit-tests/test-lib.h" /* * The purpose of this "unit test" is to verify a few invariants of the unit @@ -69,7 +70,7 @@ static void t_empty(void) ; /* empty */ } -int cmd_main(int argc, const char **argv) +int cmd__example_tap(int argc, const char **argv) { test_res = TEST(check_res = check_int(1, ==, 1), "passing test"); TEST(t_res(1), "passing test and assertion return 1"); diff --git a/t/helper/test-path-utils.c b/t/helper/test-path-utils.c index 70396fa..bf0e23e 100644 --- a/t/helper/test-path-utils.c +++ b/t/helper/test-path-utils.c @@ -7,6 +7,7 @@ #include "string-list.h" #include "trace.h" #include "utf8.h" +#include "copy.h" /* * A "string_list_each_func_t" function that normalizes an entry from diff --git a/t/helper/test-reach.c b/t/helper/test-reach.c index 1e3b431..1ba226f 100644 --- a/t/helper/test-reach.c +++ b/t/helper/test-reach.c @@ -62,7 +62,7 @@ int cmd__reach(int ac, const char **av) die("failed to resolve %s", buf.buf + 2); orig = parse_object(r, &oid); - peeled = deref_tag_noverify(orig); + peeled = deref_tag_noverify(the_repository, orig); if (!peeled) die("failed to load commit for input %s resulting in oid %s\n", diff --git a/t/helper/test-ref-store.c b/t/helper/test-ref-store.c index 82bbf6e..c9efd74 100644 --- a/t/helper/test-ref-store.c +++ b/t/helper/test-ref-store.c @@ -82,7 +82,7 @@ static const char **get_store(const char **argv, struct ref_store **refs) add_to_alternates_memory(sb.buf); strbuf_release(&sb); - *refs = get_submodule_ref_store(gitdir); + *refs = repo_get_submodule_ref_store(the_repository, gitdir); } else if (skip_prefix(argv[0], "worktree:", &gitdir)) { struct worktree **p, **worktrees = get_worktrees(); @@ -118,7 +118,7 @@ static int cmd_create_symref(struct ref_store *refs, const char **argv) const char *target = notnull(*argv++, "target"); const char *logmsg = *argv++; - return refs_create_symref(refs, refname, target, logmsg); + return refs_update_symref(refs, refname, target, logmsg); } static struct flag_definition transaction_flags[] = { diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c index c0ed872..61eb117 100644 --- a/t/helper/test-run-command.c +++ b/t/helper/test-run-command.c @@ -65,6 +65,7 @@ struct testsuite { struct string_list tests, failed; int next; int quiet, immediate, verbose, verbose_log, trace, write_junit_xml; + const char *shell_path; }; #define TESTSUITE_INIT { \ .tests = STRING_LIST_INIT_DUP, \ @@ -80,7 +81,9 @@ static int next_test(struct child_process *cp, struct strbuf *err, void *cb, return 0; test = suite->tests.items[suite->next++].string; - strvec_pushl(&cp->args, "sh", test, NULL); + if (suite->shell_path) + strvec_push(&cp->args, suite->shell_path); + strvec_push(&cp->args, test); if (suite->quiet) strvec_push(&cp->args, "--quiet"); if (suite->immediate) @@ -155,6 +158,8 @@ static int testsuite(int argc, const char **argv) .task_finished = test_finished, .data = &suite, }; + struct strbuf progpath = STRBUF_INIT; + size_t path_prefix_len; argc = parse_options(argc, argv, NULL, options, testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION); @@ -162,26 +167,36 @@ static int testsuite(int argc, const char **argv) if (max_jobs <= 0) max_jobs = online_cpus(); + /* + * If we run without a shell, execute the programs directly from CWD. + */ + suite.shell_path = getenv("TEST_SHELL_PATH"); + if (!suite.shell_path) + strbuf_addstr(&progpath, "./"); + path_prefix_len = progpath.len; + dir = opendir("."); if (!dir) die("Could not open the current directory"); while ((d = readdir(dir))) { const char *p = d->d_name; - if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) || - !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' || - !ends_with(p, ".sh")) + if (!strcmp(p, ".") || !strcmp(p, "..")) continue; /* No pattern: match all */ if (!argc) { - string_list_append(&suite.tests, p); + strbuf_setlen(&progpath, path_prefix_len); + strbuf_addstr(&progpath, p); + string_list_append(&suite.tests, progpath.buf); continue; } for (i = 0; i < argc; i++) if (!wildmatch(argv[i], p, 0)) { - string_list_append(&suite.tests, p); + strbuf_setlen(&progpath, path_prefix_len); + strbuf_addstr(&progpath, p); + string_list_append(&suite.tests, progpath.buf); break; } } @@ -208,6 +223,7 @@ static int testsuite(int argc, const char **argv) string_list_clear(&suite.tests, 0); string_list_clear(&suite.failed, 0); + strbuf_release(&progpath); return ret; } diff --git a/t/helper/test-strcmp-offset.c b/t/helper/test-strcmp-offset.c deleted file mode 100644 index d8473cf..0000000 --- a/t/helper/test-strcmp-offset.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "test-tool.h" -#include "read-cache-ll.h" - -int cmd__strcmp_offset(int argc UNUSED, const char **argv) -{ - int result; - size_t offset; - - if (!argv[1] || !argv[2]) - die("usage: %s <string1> <string2>", argv[0]); - - result = strcmp_offset(argv[1], argv[2], &offset); - - /* - * Because different CRTs behave differently, only rely on signs - * of the result values. - */ - result = (result < 0 ? -1 : - result > 0 ? 1 : - 0); - printf("%d %"PRIuMAX"\n", result, (uintmax_t)offset); - return 0; -} diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c index 80a946b..7ad7d07 100644 --- a/t/helper/test-tool.c +++ b/t/helper/test-tool.c @@ -30,6 +30,7 @@ static struct test_cmd cmds[] = { { "dump-untracked-cache", cmd__dump_untracked_cache }, { "env-helper", cmd__env_helper }, { "example-decorate", cmd__example_decorate }, + { "example-tap", cmd__example_tap }, { "find-pack", cmd__find_pack }, { "fsmonitor-client", cmd__fsmonitor_client }, { "genrandom", cmd__genrandom }, @@ -77,7 +78,6 @@ static struct test_cmd cmds[] = { { "sha256", cmd__sha256 }, { "sigchain", cmd__sigchain }, { "simple-ipc", cmd__simple_ipc }, - { "strcmp-offset", cmd__strcmp_offset }, { "string-list", cmd__string_list }, { "submodule", cmd__submodule }, { "submodule-config", cmd__submodule_config }, diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h index 2808b92..d14b307 100644 --- a/t/helper/test-tool.h +++ b/t/helper/test-tool.h @@ -24,6 +24,7 @@ int cmd__dump_untracked_cache(int argc, const char **argv); int cmd__dump_reftable(int argc, const char **argv); int cmd__env_helper(int argc, const char **argv); int cmd__example_decorate(int argc, const char **argv); +int cmd__example_tap(int argc, const char **argv); int cmd__find_pack(int argc, const char **argv); int cmd__fsmonitor_client(int argc, const char **argv); int cmd__genrandom(int argc, const char **argv); @@ -70,7 +71,6 @@ int cmd__oid_array(int argc, const char **argv); int cmd__sha256(int argc, const char **argv); int cmd__sigchain(int argc, const char **argv); int cmd__simple_ipc(int argc, const char **argv); -int cmd__strcmp_offset(int argc, const char **argv); int cmd__string_list(int argc, const char **argv); int cmd__submodule(int argc, const char **argv); int cmd__submodule_config(int argc, const char **argv); diff --git a/t/lib-chunk.sh b/t/lib-chunk.sh index a7cd9c3..9f01df1 100644 --- a/t/lib-chunk.sh +++ b/t/lib-chunk.sh @@ -13,5 +13,6 @@ corrupt_chunk_file () { fn=$1; shift perl "$TEST_DIRECTORY"/lib-chunk/corrupt-chunk-file.pl \ "$@" <"$fn" >"$fn.tmp" && - mv "$fn.tmp" "$fn" + # some vintages of macOS 'mv' fails to overwrite a read-only file. + mv -f "$fn.tmp" "$fn" } diff --git a/t/run-test.sh b/t/run-test.sh new file mode 100755 index 0000000..13c353b --- /dev/null +++ b/t/run-test.sh @@ -0,0 +1,18 @@ +#!/bin/sh + +# A simple wrapper to run shell tests via TEST_SHELL_PATH, +# or exec unit tests directly. + +case "$1" in +*.sh) + if test -z "${TEST_SHELL_PATH}" + then + echo >&2 "ERROR: TEST_SHELL_PATH is empty or not set" + exit 1 + fi + exec "${TEST_SHELL_PATH}" "$@" + ;; +*) + exec "$@" + ;; +esac diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh index 6e300be..98b81e4 100755 --- a/t/t0000-basic.sh +++ b/t/t0000-basic.sh @@ -1201,6 +1201,34 @@ test_expect_success 'very long name in the index handled sanely' ' test $len = 4098 ' +# D/F conflict checking uses an optimization when adding to the end. +# make sure it does not get confused by `a-` sorting _between_ +# `a` and `a/`. +test_expect_success 'more update-index D/F conflicts' ' + # empty the index to make sure our entry is last + git read-tree --empty && + cacheinfo=100644,$(test_oid empty_blob) && + git update-index --add --cacheinfo $cacheinfo,path5/a && + + test_must_fail git update-index --add --cacheinfo $cacheinfo,path5/a/file && + test_must_fail git update-index --add --cacheinfo $cacheinfo,path5/a/b/file && + test_must_fail git update-index --add --cacheinfo $cacheinfo,path5/a/b/c/file && + + # "a-" sorts between "a" and "a/" + git update-index --add --cacheinfo $cacheinfo,path5/a- && + + test_must_fail git update-index --add --cacheinfo $cacheinfo,path5/a/file && + test_must_fail git update-index --add --cacheinfo $cacheinfo,path5/a/b/file && + test_must_fail git update-index --add --cacheinfo $cacheinfo,path5/a/b/c/file && + + cat >expected <<-\EOF && + path5/a + path5/a- + EOF + git ls-files >actual && + test_cmp expected actual +' + test_expect_success 'test_must_fail on a failing git command' ' test_must_fail git notacommand ' diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh index 774b52c..66ccb58 100755 --- a/t/t0003-attributes.sh +++ b/t/t0003-attributes.sh @@ -398,13 +398,19 @@ test_expect_success 'bad attr source defaults to reading .gitattributes file' ' ) ' -test_expect_success 'bare repo defaults to reading .gitattributes from HEAD' ' +test_expect_success 'bare repo no longer defaults to reading .gitattributes from HEAD' ' test_when_finished rm -rf test bare_with_gitattribute && git init test && test_commit -C test gitattributes .gitattributes "f/path test=val" && git clone --bare test bare_with_gitattribute && - echo "f/path: test: val" >expect && + + echo "f/path: test: unspecified" >expect && git -C bare_with_gitattribute check-attr test -- f/path >actual && + test_cmp expect actual && + + echo "f/path: test: val" >expect && + git -C bare_with_gitattribute -c attr.tree=HEAD \ + check-attr test -- f/path >actual && test_cmp expect actual ' @@ -428,6 +434,21 @@ test_expect_success 'precedence of --attr-source, GIT_ATTR_SOURCE, then attr.tre ) ' +test_expect_success 'diff without repository with attr source' ' + mkdir -p "$TRASH_DIRECTORY/outside/nongit" && + ( + cd "$TRASH_DIRECTORY/outside/nongit" && + GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/outside" && + export GIT_CEILING_DIRECTORIES && + touch file && + cat >expect <<-EOF && + fatal: cannot use --attr-source or GIT_ATTR_SOURCE without repo + EOF + test_must_fail env GIT_ATTR_SOURCE=HEAD git grep --no-index foo file 2>err && + test_cmp expect err + ) +' + test_expect_success 'bare repository: with --source' ' ( cd bare.git && @@ -572,6 +593,16 @@ test_expect_success EXPENSIVE 'large attributes file ignored in index' ' test_cmp expect err ' +test_expect_success EXPENSIVE 'large attributes blob ignored' ' + test_when_finished "git update-index --remove .gitattributes" && + blob=$(dd if=/dev/zero bs=1048576 count=101 2>/dev/null | git hash-object -w --stdin) && + git update-index --add --cacheinfo 100644,$blob,.gitattributes && + tree="$(git write-tree)" && + git check-attr --cached --all --source="$tree" path >/dev/null 2>err && + echo "warning: ignoring overly large gitattributes blob ${SQ}.gitattributes${SQ}" >expect && + test_cmp expect err +' + test_expect_success 'builtin object mode attributes work (dir and regular paths)' ' >normal && attr_check_object_mode normal 100644 && diff --git a/t/t0017-env-helper.sh b/t/t0017-env-helper.sh index fc14ba0..f3a1685 100755 --- a/t/t0017-env-helper.sh +++ b/t/t0017-env-helper.sh @@ -91,9 +91,16 @@ test_expect_success 'test-tool env-helper reads config thanks to trace2' ' git config -l 2>err && grep "exceeded maximum include depth" err && + # This validates that the assumption that we attempt to + # read the configuration and fail very early in the start-up + # sequence (due to trace2 subsystem), even before we notice + # that the directory named with "test-tool -C" does not exist + # and die. It is a dubious thing to test, though. test_must_fail \ env HOME="$(pwd)/home" GIT_TEST_ENV_HELPER=true \ - test-tool -C cycle env-helper --type=bool --default=0 --exit-code GIT_TEST_ENV_HELPER 2>err && + test-tool -C no-such-directory \ + env-helper --type=bool --default=0 \ + --exit-code GIT_TEST_ENV_HELPER 2>err && grep "exceeded maximum include depth" err ' diff --git a/t/t0018-advice.sh b/t/t0018-advice.sh index 0dcfb76..29306b3 100755 --- a/t/t0018-advice.sh +++ b/t/t0018-advice.sh @@ -2,6 +2,9 @@ test_description='Test advise_if_enabled functionality' +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=trunk +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME + TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh @@ -29,4 +32,72 @@ test_expect_success 'advice should not be printed when config variable is set to test_must_be_empty actual ' +test_expect_success 'advice should not be printed when --no-advice is used' ' + q_to_tab >expect <<-\EOF && + On branch trunk + + No commits yet + + Untracked files: + QREADME + + nothing added to commit but untracked files present + EOF + + test_when_finished "rm -fr advice-test" && + git init advice-test && + ( + cd advice-test && + >README && + git --no-advice status + ) >actual && + test_cmp expect actual +' + +test_expect_success 'advice should not be printed when GIT_ADVICE is set to false' ' + q_to_tab >expect <<-\EOF && + On branch trunk + + No commits yet + + Untracked files: + QREADME + + nothing added to commit but untracked files present + EOF + + test_when_finished "rm -fr advice-test" && + git init advice-test && + ( + cd advice-test && + >README && + GIT_ADVICE=false git status + ) >actual && + test_cmp expect actual +' + +test_expect_success 'advice should be printed when GIT_ADVICE is set to true' ' + q_to_tab >expect <<-\EOF && + On branch trunk + + No commits yet + + Untracked files: + (use "git add <file>..." to include in what will be committed) + QREADME + + nothing added to commit but untracked files present (use "git add" to track) + EOF + + test_when_finished "rm -fr advice-test" && + git init advice-test && + ( + cd advice-test && + >README && + GIT_ADVICE=true git status + ) >actual && + cat actual > /tmp/actual && + test_cmp expect actual +' + test_done diff --git a/t/t0033-safe-directory.sh b/t/t0033-safe-directory.sh index dc34968..11c3e8f 100755 --- a/t/t0033-safe-directory.sh +++ b/t/t0033-safe-directory.sh @@ -80,4 +80,28 @@ test_expect_success 'safe.directory in included file' ' git status ' +test_expect_success 'local clone of unowned repo refused in unsafe directory' ' + test_when_finished "rm -rf source" && + git init source && + ( + sane_unset GIT_TEST_ASSUME_DIFFERENT_OWNER && + test_commit -C source initial + ) && + test_must_fail git clone --local source target && + test_path_is_missing target +' + +test_expect_success 'local clone of unowned repo accepted in safe directory' ' + test_when_finished "rm -rf source" && + git init source && + ( + sane_unset GIT_TEST_ASSUME_DIFFERENT_OWNER && + test_commit -C source initial + ) && + test_must_fail git clone --local source target && + git config --global --add safe.directory "$(pwd)/source/.git" && + git clone --local source target && + test_path_is_dir target +' + test_done diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh index 8bb2a8b..45a7736 100755 --- a/t/t0040-parse-options.sh +++ b/t/t0040-parse-options.sh @@ -176,6 +176,23 @@ test_expect_success 'long options' ' test_cmp expect output ' +test_expect_success 'abbreviate to something longer than SHA1 length' ' + cat >expect <<-EOF && + boolean: 0 + integer: 0 + magnitude: 0 + timestamp: 0 + string: (not set) + abbrev: 100 + verbose: -1 + quiet: 0 + dry run: no + file: (not set) + EOF + test-tool parse-options --abbrev=100 >output && + test_cmp expect output +' + test_expect_success 'missing required value' ' cat >expect <<-\EOF && error: switch `s'\'' requires a value diff --git a/t/t0065-strcmp-offset.sh b/t/t0065-strcmp-offset.sh deleted file mode 100755 index 94e34c8..0000000 --- a/t/t0065-strcmp-offset.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/sh - -test_description='Test strcmp_offset functionality' - -TEST_PASSES_SANITIZE_LEAK=true -. ./test-lib.sh - -while read s1 s2 expect -do - test_expect_success "strcmp_offset($s1, $s2)" ' - echo "$expect" >expect && - test-tool strcmp-offset "$s1" "$s2" >actual && - test_cmp expect actual - ' -done <<-EOF -abc abc 0 3 -abc def -1 0 -abc abz -1 2 -abc abcdef -1 3 -EOF - -test_done diff --git a/t/t0080-unit-test-output.sh b/t/t0080-unit-test-output.sh index 6657c11..7bbb065 100755 --- a/t/t0080-unit-test-output.sh +++ b/t/t0080-unit-test-output.sh @@ -9,50 +9,50 @@ test_expect_success 'TAP output from unit tests' ' cat >expect <<-EOF && ok 1 - passing test ok 2 - passing test and assertion return 1 - # check "1 == 2" failed at t/unit-tests/t-basic.c:76 + # check "1 == 2" failed at t/helper/test-example-tap.c:77 # left: 1 # right: 2 not ok 3 - failing test ok 4 - failing test and assertion return 0 not ok 5 - passing TEST_TODO() # TODO ok 6 - passing TEST_TODO() returns 1 - # todo check ${SQ}check(x)${SQ} succeeded at t/unit-tests/t-basic.c:25 + # todo check ${SQ}check(x)${SQ} succeeded at t/helper/test-example-tap.c:26 not ok 7 - failing TEST_TODO() ok 8 - failing TEST_TODO() returns 0 - # check "0" failed at t/unit-tests/t-basic.c:30 + # check "0" failed at t/helper/test-example-tap.c:31 # skipping test - missing prerequisite - # skipping check ${SQ}1${SQ} at t/unit-tests/t-basic.c:32 + # skipping check ${SQ}1${SQ} at t/helper/test-example-tap.c:33 ok 9 - test_skip() # SKIP ok 10 - skipped test returns 1 # skipping test - missing prerequisite ok 11 - test_skip() inside TEST_TODO() # SKIP ok 12 - test_skip() inside TEST_TODO() returns 1 - # check "0" failed at t/unit-tests/t-basic.c:48 + # check "0" failed at t/helper/test-example-tap.c:49 not ok 13 - TEST_TODO() after failing check ok 14 - TEST_TODO() after failing check returns 0 - # check "0" failed at t/unit-tests/t-basic.c:56 + # check "0" failed at t/helper/test-example-tap.c:57 not ok 15 - failing check after TEST_TODO() ok 16 - failing check after TEST_TODO() returns 0 - # check "!strcmp("\thello\\\\", "there\"\n")" failed at t/unit-tests/t-basic.c:61 + # check "!strcmp("\thello\\\\", "there\"\n")" failed at t/helper/test-example-tap.c:62 # left: "\011hello\\\\" # right: "there\"\012" - # check "!strcmp("NULL", NULL)" failed at t/unit-tests/t-basic.c:62 + # check "!strcmp("NULL", NULL)" failed at t/helper/test-example-tap.c:63 # left: "NULL" # right: NULL - # check "${SQ}a${SQ} == ${SQ}\n${SQ}" failed at t/unit-tests/t-basic.c:63 + # check "${SQ}a${SQ} == ${SQ}\n${SQ}" failed at t/helper/test-example-tap.c:64 # left: ${SQ}a${SQ} # right: ${SQ}\012${SQ} - # check "${SQ}\\\\${SQ} == ${SQ}\\${SQ}${SQ}" failed at t/unit-tests/t-basic.c:64 + # check "${SQ}\\\\${SQ} == ${SQ}\\${SQ}${SQ}" failed at t/helper/test-example-tap.c:65 # left: ${SQ}\\\\${SQ} # right: ${SQ}\\${SQ}${SQ} not ok 17 - messages from failing string and char comparison - # BUG: test has no checks at t/unit-tests/t-basic.c:91 + # BUG: test has no checks at t/helper/test-example-tap.c:92 not ok 18 - test with no checks ok 19 - test with no checks returns 0 1..19 EOF - ! "$GIT_BUILD_DIR"/t/unit-tests/bin/t-basic >actual && + ! test-tool example-tap >actual && test_cmp expect actual ' diff --git a/t/t0211-trace2-perf.sh b/t/t0211-trace2-perf.sh index 13ef69b..070fe7a 100755 --- a/t/t0211-trace2-perf.sh +++ b/t/t0211-trace2-perf.sh @@ -233,7 +233,7 @@ have_counter_event () { pattern="d0|${thread}|${event}||||${category}|name:${name} value:${value}" && - grep "${patern}" ${file} + grep "${pattern}" ${file} } test_expect_success 'global counter test/test1' ' diff --git a/t/t0411-clone-from-partial.sh b/t/t0411-clone-from-partial.sh new file mode 100755 index 0000000..932bf20 --- /dev/null +++ b/t/t0411-clone-from-partial.sh @@ -0,0 +1,79 @@ +#!/bin/sh + +test_description='check that local clone does not fetch from promisor remotes' + +TEST_PASSES_SANITIZE_LEAK=true +. ./test-lib.sh + +test_expect_success 'create evil repo' ' + git init tmp && + test_commit -C tmp a && + git -C tmp config uploadpack.allowfilter 1 && + git clone --filter=blob:none --no-local --no-checkout tmp evil && + rm -rf tmp && + + git -C evil config remote.origin.uploadpack \"\$TRASH_DIRECTORY/fake-upload-pack\" && + write_script fake-upload-pack <<-\EOF && + echo >&2 "fake-upload-pack running" + >"$TRASH_DIRECTORY/script-executed" + exit 1 + EOF + export TRASH_DIRECTORY && + + # empty shallow file disables local clone optimization + >evil/.git/shallow +' + +test_expect_success 'local clone must not fetch from promisor remote and execute script' ' + rm -f script-executed && + test_must_fail git clone \ + --upload-pack="GIT_TEST_ASSUME_DIFFERENT_OWNER=true git-upload-pack" \ + evil clone1 2>err && + test_grep "detected dubious ownership" err && + test_grep ! "fake-upload-pack running" err && + test_path_is_missing script-executed +' + +test_expect_success 'clone from file://... must not fetch from promisor remote and execute script' ' + rm -f script-executed && + test_must_fail git clone \ + --upload-pack="GIT_TEST_ASSUME_DIFFERENT_OWNER=true git-upload-pack" \ + "file://$(pwd)/evil" clone2 2>err && + test_grep "detected dubious ownership" err && + test_grep ! "fake-upload-pack running" err && + test_path_is_missing script-executed +' + +test_expect_success 'fetch from file://... must not fetch from promisor remote and execute script' ' + rm -f script-executed && + test_must_fail git fetch \ + --upload-pack="GIT_TEST_ASSUME_DIFFERENT_OWNER=true git-upload-pack" \ + "file://$(pwd)/evil" 2>err && + test_grep "detected dubious ownership" err && + test_grep ! "fake-upload-pack running" err && + test_path_is_missing script-executed +' + +test_expect_success 'pack-objects should fetch from promisor remote and execute script' ' + rm -f script-executed && + echo "HEAD" | test_must_fail git -C evil pack-objects --revs --stdout >/dev/null 2>err && + test_grep "fake-upload-pack running" err && + test_path_is_file script-executed +' + +test_expect_success 'clone from promisor remote does not lazy-fetch by default' ' + rm -f script-executed && + test_must_fail git clone evil no-lazy 2>err && + test_grep "lazy fetching disabled" err && + test_path_is_missing script-executed +' + +test_expect_success 'promisor lazy-fetching can be re-enabled' ' + rm -f script-executed && + test_must_fail env GIT_NO_LAZY_FETCH=0 \ + git clone evil lazy-ok 2>err && + test_grep "fake-upload-pack running" err && + test_path_is_file script-executed +' + +test_done diff --git a/t/t0450/txt-help-mismatches b/t/t0450/txt-help-mismatches index a0777ac..28003f1 100644 --- a/t/t0450/txt-help-mismatches +++ b/t/t0450/txt-help-mismatches @@ -10,7 +10,6 @@ checkout checkout-index clone column -config credential credential-cache credential-store diff --git a/t/t0600-reffiles-backend.sh b/t/t0600-reffiles-backend.sh index a390cff..92f5703 100755 --- a/t/t0600-reffiles-backend.sh +++ b/t/t0600-reffiles-backend.sh @@ -424,7 +424,7 @@ test_expect_success SYMLINKS 'git branch -m with symlinked .git/refs' ' test_when_finished "rm -rf subdir" && git init --bare subdir && - rm -rfv subdir/refs subdir/objects subdir/packed-refs && + rm -rf subdir/refs subdir/objects subdir/packed-refs && ln -s ../.git/refs subdir/refs && ln -s ../.git/objects subdir/objects && ln -s ../.git/packed-refs subdir/packed-refs && diff --git a/t/t0610-reftable-basics.sh b/t/t0610-reftable-basics.sh index cc5bbfd..b06c469 100755 --- a/t/t0610-reftable-basics.sh +++ b/t/t0610-reftable-basics.sh @@ -10,6 +10,7 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME GIT_TEST_DEFAULT_REF_FORMAT=reftable export GIT_TEST_DEFAULT_REF_FORMAT +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh INVALID_OID=$(test_oid 001) diff --git a/t/t0613-reftable-write-options.sh b/t/t0613-reftable-write-options.sh new file mode 100755 index 0000000..e2708e1 --- /dev/null +++ b/t/t0613-reftable-write-options.sh @@ -0,0 +1,286 @@ +#!/bin/sh + +test_description='reftable write options' + +GIT_TEST_DEFAULT_REF_FORMAT=reftable +export GIT_TEST_DEFAULT_REF_FORMAT +# Disable auto-compaction for all tests as we explicitly control repacking of +# refs. +GIT_TEST_REFTABLE_AUTOCOMPACTION=false +export GIT_TEST_REFTABLE_AUTOCOMPACTION +# Block sizes depend on the hash function, so we force SHA1 here. +GIT_TEST_DEFAULT_HASH=sha1 +export GIT_TEST_DEFAULT_HASH +# Block sizes also depend on the actual refs we write, so we force "master" to +# be the default initial branch name. +GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master +export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME + +. ./test-lib.sh + +test_expect_success 'default write options' ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit initial && + git pack-refs && + cat >expect <<-EOF && + header: + block_size: 4096 + ref: + - length: 129 + restarts: 2 + log: + - length: 262 + restarts: 2 + EOF + test-tool dump-reftable -b .git/reftable/*.ref >actual && + test_cmp expect actual + ) +' + +test_expect_success 'disabled reflog writes no log blocks' ' + test_config_global core.logAllRefUpdates false && + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit initial && + git pack-refs && + cat >expect <<-EOF && + header: + block_size: 4096 + ref: + - length: 129 + restarts: 2 + EOF + test-tool dump-reftable -b .git/reftable/*.ref >actual && + test_cmp expect actual + ) +' + +test_expect_success 'many refs results in multiple blocks' ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit initial && + for i in $(test_seq 200) + do + printf "update refs/heads/branch-%d HEAD\n" "$i" || + return 1 + done >input && + git update-ref --stdin <input && + git pack-refs && + + cat >expect <<-EOF && + header: + block_size: 4096 + ref: + - length: 4049 + restarts: 11 + - length: 1136 + restarts: 3 + log: + - length: 4041 + restarts: 4 + - length: 4015 + restarts: 3 + - length: 4014 + restarts: 3 + - length: 4012 + restarts: 3 + - length: 3289 + restarts: 3 + EOF + test-tool dump-reftable -b .git/reftable/*.ref >actual && + test_cmp expect actual + ) +' + +test_expect_success 'tiny block size leads to error' ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit initial && + cat >expect <<-EOF && + error: unable to compact stack: entry too large + EOF + test_must_fail git -c reftable.blockSize=50 pack-refs 2>err && + test_cmp expect err + ) +' + +test_expect_success 'small block size leads to multiple ref blocks' ' + test_config_global core.logAllRefUpdates false && + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit A && + test_commit B && + git -c reftable.blockSize=100 pack-refs && + + cat >expect <<-EOF && + header: + block_size: 100 + ref: + - length: 53 + restarts: 1 + - length: 74 + restarts: 1 + - length: 38 + restarts: 1 + EOF + test-tool dump-reftable -b .git/reftable/*.ref >actual && + test_cmp expect actual + ) +' + +test_expect_success 'small block size fails with large reflog message' ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit A && + perl -e "print \"a\" x 500" >logmsg && + cat >expect <<-EOF && + fatal: update_ref failed for ref ${SQ}refs/heads/logme${SQ}: reftable: transaction failure: entry too large + EOF + test_must_fail git -c reftable.blockSize=100 \ + update-ref -m "$(cat logmsg)" refs/heads/logme HEAD 2>err && + test_cmp expect err + ) +' + +test_expect_success 'block size exceeding maximum supported size' ' + test_config_global core.logAllRefUpdates false && + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit A && + test_commit B && + cat >expect <<-EOF && + fatal: reftable block size cannot exceed 16MB + EOF + test_must_fail git -c reftable.blockSize=16777216 pack-refs 2>err && + test_cmp expect err + ) +' + +test_expect_success 'restart interval at every single record' ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit initial && + for i in $(test_seq 10) + do + printf "update refs/heads/branch-%d HEAD\n" "$i" || + return 1 + done >input && + git update-ref --stdin <input && + git -c reftable.restartInterval=1 pack-refs && + + cat >expect <<-EOF && + header: + block_size: 4096 + ref: + - length: 566 + restarts: 13 + log: + - length: 1393 + restarts: 12 + EOF + test-tool dump-reftable -b .git/reftable/*.ref >actual && + test_cmp expect actual + ) +' + +test_expect_success 'restart interval exceeding maximum supported interval' ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit initial && + cat >expect <<-EOF && + fatal: reftable block size cannot exceed 65535 + EOF + test_must_fail git -c reftable.restartInterval=65536 pack-refs 2>err && + test_cmp expect err + ) +' + +test_expect_success 'object index gets written by default with ref index' ' + test_config_global core.logAllRefUpdates false && + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit initial && + for i in $(test_seq 5) + do + printf "update refs/heads/branch-%d HEAD\n" "$i" || + return 1 + done >input && + git update-ref --stdin <input && + git -c reftable.blockSize=100 pack-refs && + + cat >expect <<-EOF && + header: + block_size: 100 + ref: + - length: 53 + restarts: 1 + - length: 95 + restarts: 1 + - length: 71 + restarts: 1 + - length: 80 + restarts: 1 + obj: + - length: 11 + restarts: 1 + EOF + test-tool dump-reftable -b .git/reftable/*.ref >actual && + test_cmp expect actual + ) +' + +test_expect_success 'object index can be disabled' ' + test_config_global core.logAllRefUpdates false && + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit initial && + for i in $(test_seq 5) + do + printf "update refs/heads/branch-%d HEAD\n" "$i" || + return 1 + done >input && + git update-ref --stdin <input && + git -c reftable.blockSize=100 -c reftable.indexObjects=false pack-refs && + + cat >expect <<-EOF && + header: + block_size: 100 + ref: + - length: 53 + restarts: 1 + - length: 95 + restarts: 1 + - length: 71 + restarts: 1 + - length: 80 + restarts: 1 + EOF + test-tool dump-reftable -b .git/reftable/*.ref >actual && + test_cmp expect actual + ) +' + +test_done diff --git a/t/t1007-hash-object.sh b/t/t1007-hash-object.sh index 64aea38..d73a5cc 100755 --- a/t/t1007-hash-object.sh +++ b/t/t1007-hash-object.sh @@ -260,4 +260,10 @@ test_expect_success '--literally with extra-long type' ' echo example | git hash-object -t $t --literally --stdin ' +test_expect_success '--stdin outside of repository (uses SHA-1)' ' + nongit git hash-object --stdin <hello >actual && + echo "$(test_oid --hash=sha1 hello)" >expect && + test_cmp expect actual +' + test_done diff --git a/t/t1300-config.sh b/t/t1300-config.sh index 9b65d9e..9de2d95 100755 --- a/t/t1300-config.sh +++ b/t/t1300-config.sh @@ -11,6 +11,34 @@ export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh +for mode in legacy subcommands +do + +case "$mode" in +legacy) + mode_prefix="--" + mode_get="" + mode_get_all="--get-all" + mode_get_regexp="--get-regexp" + mode_set="" + mode_replace_all="--replace-all" + mode_unset="--unset" + mode_unset_all="--unset-all" + ;; +subcommands) + mode_prefix="" + mode_get="get" + mode_get_all="get --all" + mode_get_regexp="get --regexp --all --show-names" + mode_set="set" + mode_replace_all="set --all" + mode_unset="unset" + mode_unset_all="unset --all" + ;; +*) + BUG "unknown mode $mode";; +esac + test_expect_success 'setup whitespace config' ' sed -e "s/^|//" \ -e "s/[$]$//" \ @@ -112,7 +140,7 @@ cat > expect << EOF penguin = little blue EOF test_expect_success 'initial' ' - git config section.penguin "little blue" && + git config ${mode_set} section.penguin "little blue" && test_cmp expect .git/config ' @@ -122,7 +150,7 @@ cat > expect << EOF Movie = BadPhysics EOF test_expect_success 'mixed case' ' - git config Section.Movie BadPhysics && + git config ${mode_set} Section.Movie BadPhysics && test_cmp expect .git/config ' @@ -134,7 +162,7 @@ cat > expect << EOF WhatEver = Second EOF test_expect_success 'similar section' ' - git config Sections.WhatEver Second && + git config ${mode_set} Sections.WhatEver Second && test_cmp expect .git/config ' @@ -147,7 +175,7 @@ cat > expect << EOF WhatEver = Second EOF test_expect_success 'uppercase section' ' - git config SECTION.UPPERCASE true && + git config ${mode_set} SECTION.UPPERCASE true && test_cmp expect .git/config ' @@ -174,8 +202,8 @@ EOF test_expect_success 'append comments' ' git config --replace-all --comment="Pygoscelis papua" section.penguin gentoo && - git config --comment="find fish" section.disposition peckish && - git config --comment="#abc" section.foo bar && + git config ${mode_set} --comment="find fish" section.disposition peckish && + git config ${mode_set} --comment="#abc" section.foo bar && git config --comment="and comment" section.spsp value && git config --comment=" # and comment" section.htsp value && @@ -184,7 +212,7 @@ test_expect_success 'append comments' ' ' test_expect_success 'Prohibited LF in comment' ' - test_must_fail git config --comment="a${LF}b" section.k v + test_must_fail git config ${mode_set} --comment="a${LF}b" section.k v ' test_expect_success 'non-match result' 'test_cmp expect .git/config' @@ -235,7 +263,7 @@ foo = bar EOF test_expect_success 'unset with cont. lines' ' - git config --unset beta.baz + git config ${mode_unset} beta.baz ' cat > expect <<\EOF @@ -262,7 +290,7 @@ EOF cp .git/config .git/config2 test_expect_success 'multiple unset' ' - git config --unset-all beta.haha + git config ${mode_unset_all} beta.haha ' cat > expect << EOF @@ -281,14 +309,14 @@ test_expect_success 'multiple unset is correct' ' cp .git/config2 .git/config test_expect_success '--replace-all missing value' ' - test_must_fail git config --replace-all beta.haha && + test_must_fail git config ${mode_replace_all} beta.haha && test_cmp .git/config2 .git/config ' rm .git/config2 test_expect_success '--replace-all' ' - git config --replace-all beta.haha gamma + git config ${mode_replace_all} beta.haha gamma ' cat > expect << EOF @@ -315,7 +343,7 @@ noIndent= sillyValue ; 'nother silly comment [nextSection] noNewline = ouch EOF test_expect_success 'really mean test' ' - git config beta.haha alpha && + git config ${mode_set} beta.haha alpha && test_cmp expect .git/config ' @@ -330,7 +358,7 @@ noIndent= sillyValue ; 'nother silly comment nonewline = wow EOF test_expect_success 'really really mean test' ' - git config nextsection.nonewline wow && + git config ${mode_set} nextsection.nonewline wow && test_cmp expect .git/config ' @@ -348,7 +376,7 @@ noIndent= sillyValue ; 'nother silly comment nonewline = wow EOF test_expect_success 'unset' ' - git config --unset beta.haha && + git config ${mode_unset} beta.haha && test_cmp expect .git/config ' @@ -384,7 +412,7 @@ test_expect_success 'multi-valued get-all returns all' ' wow wow2 for me EOF - git config --get-all nextsection.nonewline >actual && + git config ${mode_get_all} nextsection.nonewline >actual && test_cmp expect actual ' @@ -404,11 +432,11 @@ test_expect_success 'multivar replace' ' ' test_expect_success 'ambiguous unset' ' - test_must_fail git config --unset nextsection.nonewline + test_must_fail git config ${mode_unset} nextsection.nonewline ' test_expect_success 'invalid unset' ' - test_must_fail git config --unset somesection.nonewline + test_must_fail git config ${mode_unset} somesection.nonewline ' cat > expect << EOF @@ -422,7 +450,12 @@ noIndent= sillyValue ; 'nother silly comment EOF test_expect_success 'multivar unset' ' - git config --unset nextsection.nonewline "wow3$" && + case "$mode" in + legacy) + git config --unset nextsection.nonewline "wow3$";; + subcommands) + git config unset --value="wow3$" nextsection.nonewline;; + esac && test_cmp expect .git/config ' @@ -460,11 +493,11 @@ version.1.2.3eX.alpha=beta EOF test_expect_success 'working --list' ' - git config --list > output && + git config ${mode_prefix}list > output && test_cmp expect output ' test_expect_success '--list without repo produces empty output' ' - git --git-dir=nonexistent config --list >output && + git --git-dir=nonexistent config ${mode_prefix}list >output && test_must_be_empty output ' @@ -476,7 +509,7 @@ version.1.2.3eX.alpha EOF test_expect_success '--name-only --list' ' - git config --name-only --list >output && + git config ${mode_prefix}list --name-only >output && test_cmp expect output ' @@ -486,7 +519,7 @@ nextsection.nonewline wow2 for me EOF test_expect_success '--get-regexp' ' - git config --get-regexp in >output && + git config ${mode_get_regexp} in >output && test_cmp expect output ' @@ -496,7 +529,7 @@ nextsection.nonewline EOF test_expect_success '--name-only --get-regexp' ' - git config --name-only --get-regexp in >output && + git config ${mode_get_regexp} --name-only in >output && test_cmp expect output ' @@ -507,7 +540,7 @@ EOF test_expect_success '--add' ' git config --add nextsection.nonewline "wow4 for you" && - git config --get-all nextsection.nonewline > output && + git config ${mode_get_all} nextsection.nonewline > output && test_cmp expect output ' @@ -529,21 +562,21 @@ test_expect_success 'get variable with empty value' ' echo novalue.variable > expect test_expect_success 'get-regexp variable with no value' ' - git config --get-regexp novalue > output && + git config ${mode_get_regexp} novalue > output && test_cmp expect output ' echo 'novalue.variable true' > expect test_expect_success 'get-regexp --bool variable with no value' ' - git config --bool --get-regexp novalue > output && + git config ${mode_get_regexp} --bool novalue > output && test_cmp expect output ' echo 'emptyvalue.variable ' > expect test_expect_success 'get-regexp variable with empty value' ' - git config --get-regexp emptyvalue > output && + git config ${mode_get_regexp} emptyvalue > output && test_cmp expect output ' @@ -563,7 +596,8 @@ test_expect_success 'get bool variable with empty value' ' test_expect_success 'no arguments, but no crash' ' test_must_fail git config >output 2>&1 && - test_grep usage output + echo "error: no action specified" >expect && + test_cmp expect output ' cat > .git/config << EOF @@ -614,17 +648,17 @@ ein.bahn=strasse EOF test_expect_success 'alternative GIT_CONFIG' ' - GIT_CONFIG=other-config git config --list >output && + GIT_CONFIG=other-config git config ${mode_prefix}list >output && test_cmp expect output ' test_expect_success 'alternative GIT_CONFIG (--file)' ' - git config --file other-config --list >output && + git config ${mode_prefix}list --file other-config >output && test_cmp expect output ' test_expect_success 'alternative GIT_CONFIG (--file=-)' ' - git config --file - --list <other-config >output && + git config ${mode_prefix}list --file - <other-config >output && test_cmp expect output ' @@ -633,10 +667,11 @@ test_expect_success 'setting a value in stdin is an error' ' ' test_expect_success 'editing stdin is an error' ' - test_must_fail git config --file - --edit + test_must_fail git config ${mode_prefix}edit --file - ' test_expect_success 'refer config from subdirectory' ' + test_when_finished "rm -r x" && mkdir x && test_cmp_config -C x strasse --file=../other-config --get ein.bahn ' @@ -665,7 +700,7 @@ weird EOF test_expect_success 'rename section' ' - git config --rename-section branch.eins branch.zwei + git config ${mode_prefix}rename-section branch.eins branch.zwei ' cat > expect << EOF @@ -684,7 +719,7 @@ test_expect_success 'rename succeeded' ' ' test_expect_success 'rename non-existing section' ' - test_must_fail git config --rename-section \ + test_must_fail git config ${mode_prefix}rename-section \ branch."world domination" branch.drei ' @@ -693,7 +728,7 @@ test_expect_success 'rename succeeded' ' ' test_expect_success 'rename another section' ' - git config --rename-section branch."1 234 blabl/a" branch.drei + git config ${mode_prefix}rename-section branch."1 234 blabl/a" branch.drei ' cat > expect << EOF @@ -716,7 +751,7 @@ cat >> .git/config << EOF EOF test_expect_success 'rename a section with a var on the same line' ' - git config --rename-section branch.vier branch.zwei + git config ${mode_prefix}rename-section branch.vier branch.zwei ' cat > expect << EOF @@ -737,11 +772,11 @@ test_expect_success 'rename succeeded' ' ' test_expect_success 'renaming empty section name is rejected' ' - test_must_fail git config --rename-section branch.zwei "" + test_must_fail git config ${mode_prefix}rename-section branch.zwei "" ' test_expect_success 'renaming to bogus section is rejected' ' - test_must_fail git config --rename-section branch.zwei "bogus name" + test_must_fail git config ${mode_prefix}rename-section branch.zwei "bogus name" ' test_expect_success 'renaming a section with a long line' ' @@ -750,7 +785,7 @@ test_expect_success 'renaming a section with a long line' ' printf " c = d %1024s [a] e = f\\n" " " && printf "[a] g = h\\n" } >y && - git config -f y --rename-section a xyz && + git config ${mode_prefix}rename-section -f y a xyz && test_must_fail git config -f y b.e ' @@ -760,7 +795,7 @@ test_expect_success 'renaming an embedded section with a long line' ' printf " c = d %1024s [a] [foo] e = f\\n" " " && printf "[a] g = h\\n" } >y && - git config -f y --rename-section a xyz && + git config ${mode_prefix}rename-section -f y a xyz && test_must_fail git config -f y foo.e ' @@ -770,7 +805,7 @@ test_expect_success 'renaming a section with an overly-long line' ' printf " c = d %525000s e" " " && printf "[a] g = h\\n" } >y && - test_must_fail git config -f y --rename-section a xyz 2>err && + test_must_fail git config ${mode_prefix}rename-section -f y a xyz 2>err && grep "refusing to work with overly long line in .y. on line 2" err ' @@ -779,7 +814,7 @@ cat >> .git/config << EOF EOF test_expect_success 'remove section' ' - git config --remove-section branch.zwei + git config ${mode_prefix}remove-section branch.zwei ' cat > expect << EOF @@ -803,16 +838,16 @@ EOF test_expect_success 'section ending' ' rm -f .git/config && - git config gitcvs.enabled true && - git config gitcvs.ext.dbname %Ggitcvs1.%a.%m.sqlite && - git config gitcvs.dbname %Ggitcvs2.%a.%m.sqlite && + git config ${mode_set} gitcvs.enabled true && + git config ${mode_set} gitcvs.ext.dbname %Ggitcvs1.%a.%m.sqlite && + git config ${mode_set} gitcvs.dbname %Ggitcvs2.%a.%m.sqlite && test_cmp expect .git/config ' test_expect_success numbers ' - git config kilo.gram 1k && - git config mega.ton 1m && + git config ${mode_set} kilo.gram 1k && + git config ${mode_set} mega.ton 1m && echo 1024 >expect && echo 1048576 >>expect && git config --int --get kilo.gram >actual && @@ -821,20 +856,20 @@ test_expect_success numbers ' ' test_expect_success '--int is at least 64 bits' ' - git config giga.watts 121g && + git config ${mode_set} giga.watts 121g && echo >expect && test_cmp_config 129922760704 --int --get giga.watts ' test_expect_success 'invalid unit' ' - git config aninvalid.unit "1auto" && + git config ${mode_set} aninvalid.unit "1auto" && test_cmp_config 1auto aninvalid.unit && test_must_fail git config --int --get aninvalid.unit 2>actual && test_grep "bad numeric config value .1auto. for .aninvalid.unit. in file .git/config: invalid unit" actual ' test_expect_success 'invalid unit boolean' ' - git config commit.gpgsign "1true" && + git config ${mode_set} commit.gpgsign "1true" && test_cmp_config 1true commit.gpgsign && test_must_fail git config --bool --get commit.gpgsign 2>actual && test_grep "bad boolean config value .1true. for .commit.gpgsign." actual @@ -847,7 +882,7 @@ test_expect_success 'line number is reported correctly' ' ' test_expect_success 'invalid stdin config' ' - echo "[broken" | test_must_fail git config --list --file - >output 2>&1 && + echo "[broken" | test_must_fail git config ${mode_prefix}list --file - >output 2>&1 && test_grep "bad config line 1 in standard input" output ' @@ -864,14 +899,14 @@ EOF test_expect_success bool ' - git config bool.true1 01 && - git config bool.true2 -1 && - git config bool.true3 YeS && - git config bool.true4 true && - git config bool.false1 000 && - git config bool.false2 "" && - git config bool.false3 nO && - git config bool.false4 FALSE && + git config ${mode_set} bool.true1 01 && + git config ${mode_set} bool.true2 -1 && + git config ${mode_set} bool.true3 YeS && + git config ${mode_set} bool.true4 true && + git config ${mode_set} bool.false1 000 && + git config ${mode_set} bool.false2 "" && + git config ${mode_set} bool.false3 nO && + git config ${mode_set} bool.false4 FALSE && rm -f result && for i in 1 2 3 4 do @@ -882,7 +917,7 @@ test_expect_success bool ' test_expect_success 'invalid bool (--get)' ' - git config bool.nobool foobar && + git config ${mode_set} bool.nobool foobar && test_must_fail git config --bool --get bool.nobool' test_expect_success 'invalid bool (set)' ' @@ -1071,7 +1106,7 @@ test_expect_success 'get --expiry-date' ' test_expect_success 'get --type=color' ' rm .git/config && - git config foo.color "red" && + git config ${mode_set} foo.color "red" && git config --get --type=color foo.color >actual.raw && test_decode_color <actual.raw >actual && echo "<RED>" >expect && @@ -1108,18 +1143,18 @@ cat > expect << EOF EOF test_expect_success 'quoting' ' rm -f .git/config && - git config quote.leading " test" && - git config quote.ending "test " && - git config quote.semicolon "test;test" && - git config quote.hash "test#test" && + git config ${mode_set} quote.leading " test" && + git config ${mode_set} quote.ending "test " && + git config ${mode_set} quote.semicolon "test;test" && + git config ${mode_set} quote.hash "test#test" && test_cmp expect .git/config ' test_expect_success 'key with newline' ' - test_must_fail git config "key.with + test_must_fail git config ${mode_get} "key.with newline" 123' -test_expect_success 'value with newline' 'git config key.sub value.with\\\ +test_expect_success 'value with newline' 'git config ${mode_set} key.sub value.with\\\ newline' cat > .git/config <<\EOF @@ -1139,7 +1174,7 @@ section.quotecont=cont;inued EOF test_expect_success 'value continued on next line' ' - git config --list > result && + git config ${mode_prefix}list > result && test_cmp expect result ' @@ -1163,14 +1198,14 @@ Qsection.sub=section.val4 Qsection.sub=section.val5Q EOF test_expect_success '--null --list' ' - git config --null --list >result.raw && + git config ${mode_prefix}list --null >result.raw && nul_to_q <result.raw >result && echo >>result && test_cmp expect result ' test_expect_success '--null --get-regexp' ' - git config --null --get-regexp "val[0-9]" >result.raw && + git config ${mode_get_regexp} --null "val[0-9]" >result.raw && nul_to_q <result.raw >result && echo >>result && test_cmp expect result @@ -1178,26 +1213,27 @@ test_expect_success '--null --get-regexp' ' test_expect_success 'inner whitespace kept verbatim, spaces only' ' echo "foo bar" >expect && - git config section.val "foo bar" && - git config --get section.val >actual && + git config ${mode_set} section.val "foo bar" && + git config ${mode_get} section.val >actual && test_cmp expect actual ' test_expect_success 'inner whitespace kept verbatim, horizontal tabs only' ' echo "fooQQbar" | q_to_tab >expect && - git config section.val "$(cat expect)" && - git config --get section.val >actual && + git config ${mode_set} section.val "$(cat expect)" && + git config ${mode_get} section.val >actual && test_cmp expect actual ' test_expect_success 'inner whitespace kept verbatim, horizontal tabs and spaces' ' echo "foo Q bar" | q_to_tab >expect && - git config section.val "$(cat expect)" && - git config --get section.val >actual && + git config ${mode_set} section.val "$(cat expect)" && + git config ${mode_get} section.val >actual && test_cmp expect actual ' test_expect_success SYMLINKS 'symlinked configuration' ' + test_when_finished "rm myconfig" && ln -s notyet myconfig && git config --file=myconfig test.frotz nitfol && test -h myconfig && @@ -1218,10 +1254,11 @@ test_expect_success SYMLINKS 'symlinked configuration' ' ' test_expect_success SYMLINKS 'symlink to nonexistent configuration' ' + test_when_finished "rm linktonada linktolinktonada" && ln -s doesnotexist linktonada && ln -s linktonada linktolinktonada && - test_must_fail git config --file=linktonada --list && - test_must_fail git config --file=linktolinktonada --list + test_must_fail git config ${mode_prefix}list --file=linktonada && + test_must_fail git config ${mode_prefix}list --file=linktolinktonada ' test_expect_success 'check split_cmdline return' ' @@ -1229,12 +1266,12 @@ test_expect_success 'check split_cmdline return' ' git init repo && ( cd repo && - git config alias.split-cmdline-fix "echo \"" && + git config ${mode_set} alias.split-cmdline-fix "echo \"" && test_must_fail git split-cmdline-fix && echo foo >foo && git add foo && git commit -m "initial commit" && - git config branch.main.mergeoptions "echo \"" && + git config ${mode_set} branch.main.mergeoptions "echo \"" && test_must_fail git merge main ) ' @@ -1266,18 +1303,18 @@ test_expect_success 'git -c can represent empty string' ' ' test_expect_success 'key sanity-checking' ' - test_must_fail git config foo=bar && - test_must_fail git config foo=.bar && - test_must_fail git config foo.ba=r && - test_must_fail git config foo.1bar && - test_must_fail git config foo."ba + test_must_fail git config ${mode_get} foo=bar && + test_must_fail git config ${mode_get} foo=.bar && + test_must_fail git config ${mode_get} foo.ba=r && + test_must_fail git config ${mode_get} foo.1bar && + test_must_fail git config ${mode_get} foo."ba z".bar && - test_must_fail git config . false && - test_must_fail git config .foo false && - test_must_fail git config foo. false && - test_must_fail git config .foo. false && - git config foo.bar true && - git config foo."ba =z".bar false + test_must_fail git config ${mode_set} . false && + test_must_fail git config ${mode_set} .foo false && + test_must_fail git config ${mode_set} foo. false && + test_must_fail git config ${mode_set} .foo. false && + git config ${mode_set} foo.bar true && + git config ${mode_set} foo."ba =z".bar false ' test_expect_success 'git -c works with aliases of builtins' ' @@ -1319,7 +1356,7 @@ test_expect_success 'git -c complains about empty key and value' ' ' test_expect_success 'multiple git -c appends config' ' - test_config alias.x "!git -c x.two=2 config --get-regexp ^x\.*" && + test_config alias.x "!git -c x.two=2 config ${mode_get_regexp} ^x\.*" && cat >expect <<-\EOF && x.one 1 x.two 2 @@ -1478,14 +1515,14 @@ do done test_expect_success 'git -c is not confused by empty environment' ' - GIT_CONFIG_PARAMETERS="" git -c x.one=1 config --list + GIT_CONFIG_PARAMETERS="" git -c x.one=1 config ${mode_prefix}list ' test_expect_success 'GIT_CONFIG_PARAMETERS handles old-style entries' ' v="${SQ}key.one=foo${SQ}" && v="$v ${SQ}key.two=bar${SQ}" && v="$v ${SQ}key.ambiguous=section.whatever=value${SQ}" && - GIT_CONFIG_PARAMETERS=$v git config --get-regexp "key.*" >actual && + GIT_CONFIG_PARAMETERS=$v git config ${mode_get_regexp} "key.*" >actual && cat >expect <<-EOF && key.one foo key.two bar @@ -1498,7 +1535,7 @@ test_expect_success 'GIT_CONFIG_PARAMETERS handles new-style entries' ' v="${SQ}key.one${SQ}=${SQ}foo${SQ}" && v="$v ${SQ}key.two${SQ}=${SQ}bar${SQ}" && v="$v ${SQ}key.ambiguous=section.whatever${SQ}=${SQ}value${SQ}" && - GIT_CONFIG_PARAMETERS=$v git config --get-regexp "key.*" >actual && + GIT_CONFIG_PARAMETERS=$v git config ${mode_get_regexp} "key.*" >actual && cat >expect <<-EOF && key.one foo key.two bar @@ -1512,7 +1549,7 @@ test_expect_success 'old and new-style entries can mix' ' v="$v ${SQ}key.newone${SQ}=${SQ}newfoo${SQ}" && v="$v ${SQ}key.oldtwo=oldbar${SQ}" && v="$v ${SQ}key.newtwo${SQ}=${SQ}newbar${SQ}" && - GIT_CONFIG_PARAMETERS=$v git config --get-regexp "key.*" >actual && + GIT_CONFIG_PARAMETERS=$v git config ${mode_get_regexp} "key.*" >actual && cat >expect <<-EOF && key.oldone oldfoo key.newone newfoo @@ -1525,7 +1562,7 @@ test_expect_success 'old and new-style entries can mix' ' test_expect_success 'old and new bools with ambiguous subsection' ' v="${SQ}key.with=equals.oldbool${SQ}" && v="$v ${SQ}key.with=equals.newbool${SQ}=" && - GIT_CONFIG_PARAMETERS=$v git config --get-regexp "key.*" >actual && + GIT_CONFIG_PARAMETERS=$v git config ${mode_get_regexp} "key.*" >actual && cat >expect <<-EOF && key.with equals.oldbool key.with=equals.newbool @@ -1539,7 +1576,7 @@ test_expect_success 'detect bogus GIT_CONFIG_PARAMETERS' ' env.two two EOF GIT_CONFIG_PARAMETERS="${SQ}env.one=one${SQ} ${SQ}env.two=two${SQ}" \ - git config --get-regexp "env.*" >actual && + git config ${mode_get_regexp} "env.*" >actual && test_cmp expect actual && cat >expect <<-EOF && @@ -1547,12 +1584,12 @@ test_expect_success 'detect bogus GIT_CONFIG_PARAMETERS' ' env.two two EOF GIT_CONFIG_PARAMETERS="${SQ}env.one=one${SQ}\\$SQ$SQ$SQ ${SQ}env.two=two${SQ}" \ - git config --get-regexp "env.*" >actual && + git config ${mode_get_regexp} "env.*" >actual && test_cmp expect actual && test_must_fail env \ GIT_CONFIG_PARAMETERS="${SQ}env.one=one${SQ}\\$SQ ${SQ}env.two=two${SQ}" \ - git config --get-regexp "env.*" + git config ${mode_get_regexp} "env.*" ' test_expect_success 'git --config-env=key=envvar support' ' @@ -1600,7 +1637,7 @@ test_expect_success 'git -c and --config-env work together' ' ENVVAR=env-value git \ -c bar.cmd=cmd-value \ --config-env=bar.env=ENVVAR \ - config --get-regexp "^bar.*" >actual && + config ${mode_get_regexp} "^bar.*" >actual && test_cmp expect actual ' @@ -1628,7 +1665,7 @@ test_expect_success 'git config handles environment config pairs' ' GIT_CONFIG_COUNT=2 \ GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="foo" \ GIT_CONFIG_KEY_1="pair.two" GIT_CONFIG_VALUE_1="bar" \ - git config --get-regexp "pair.*" >actual && + git config ${mode_get_regexp} "pair.*" >actual && cat >expect <<-EOF && pair.one foo pair.two bar @@ -1638,7 +1675,7 @@ test_expect_success 'git config handles environment config pairs' ' test_expect_success 'git config ignores pairs without count' ' test_must_fail env GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \ - git config pair.one 2>error && + git config ${mode_get} pair.one 2>error && test_must_be_empty error ' @@ -1646,7 +1683,7 @@ test_expect_success 'git config ignores pairs exceeding count' ' GIT_CONFIG_COUNT=1 \ GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \ GIT_CONFIG_KEY_1="pair.two" GIT_CONFIG_VALUE_1="value" \ - git config --get-regexp "pair.*" >actual 2>error && + git config ${mode_get_regexp} "pair.*" >actual 2>error && cat >expect <<-EOF && pair.one value EOF @@ -1657,43 +1694,43 @@ test_expect_success 'git config ignores pairs exceeding count' ' test_expect_success 'git config ignores pairs with zero count' ' test_must_fail env \ GIT_CONFIG_COUNT=0 GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \ - git config pair.one 2>error && + git config ${mode_get} pair.one 2>error && test_must_be_empty error ' test_expect_success 'git config ignores pairs with empty count' ' test_must_fail env \ GIT_CONFIG_COUNT= GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \ - git config pair.one 2>error && + git config ${mode_get} pair.one 2>error && test_must_be_empty error ' test_expect_success 'git config fails with invalid count' ' - test_must_fail env GIT_CONFIG_COUNT=10a git config --list 2>error && + test_must_fail env GIT_CONFIG_COUNT=10a git config ${mode_prefix}list 2>error && test_grep "bogus count" error && - test_must_fail env GIT_CONFIG_COUNT=9999999999999999 git config --list 2>error && + test_must_fail env GIT_CONFIG_COUNT=9999999999999999 git config ${mode_prefix}list 2>error && test_grep "too many entries" error ' test_expect_success 'git config fails with missing config key' ' test_must_fail env GIT_CONFIG_COUNT=1 GIT_CONFIG_VALUE_0="value" \ - git config --list 2>error && + git config ${mode_prefix}list 2>error && test_grep "missing config key" error ' test_expect_success 'git config fails with missing config value' ' test_must_fail env GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="pair.one" \ - git config --list 2>error && + git config ${mode_prefix}list 2>error && test_grep "missing config value" error ' test_expect_success 'git config fails with invalid config pair key' ' test_must_fail env GIT_CONFIG_COUNT=1 \ GIT_CONFIG_KEY_0= GIT_CONFIG_VALUE_0=value \ - git config --list && + git config ${mode_prefix}list && test_must_fail env GIT_CONFIG_COUNT=1 \ GIT_CONFIG_KEY_0=missing-section GIT_CONFIG_VALUE_0=value \ - git config --list + git config ${mode_prefix}list ' test_expect_success 'environment overrides config file' ' @@ -1703,7 +1740,7 @@ test_expect_success 'environment overrides config file' ' one = value EOF GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pair.one GIT_CONFIG_VALUE_0=override \ - git config pair.one >actual && + git config ${mode_get} pair.one >actual && cat >expect <<-EOF && override EOF @@ -1713,7 +1750,7 @@ test_expect_success 'environment overrides config file' ' test_expect_success 'GIT_CONFIG_PARAMETERS overrides environment config' ' GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pair.one GIT_CONFIG_VALUE_0=value \ GIT_CONFIG_PARAMETERS="${SQ}pair.one=override${SQ}" \ - git config pair.one >actual && + git config ${mode_get} pair.one >actual && cat >expect <<-EOF && override EOF @@ -1732,8 +1769,8 @@ test_expect_success 'command line overrides environment config' ' test_expect_success 'git config --edit works' ' git config -f tmp test.value no && echo test.value=yes >expect && - GIT_EDITOR="echo [test]value=yes >" git config -f tmp --edit && - git config -f tmp --list >actual && + GIT_EDITOR="echo [test]value=yes >" git config ${mode_prefix}edit -f tmp && + git config ${mode_prefix}list -f tmp >actual && test_cmp expect actual ' @@ -1741,8 +1778,8 @@ test_expect_success 'git config --edit respects core.editor' ' git config -f tmp test.value no && echo test.value=yes >expect && test_config core.editor "echo [test]value=yes >" && - git config -f tmp --edit && - git config -f tmp --list >actual && + git config ${mode_prefix}edit -f tmp && + git config ${mode_prefix}list -f tmp >actual && test_cmp expect actual ' @@ -1788,20 +1825,28 @@ test_expect_success 'urlmatch' ' test_expect_code 1 git config --bool --get-urlmatch doesnt.exist https://good.example.com >actual && test_must_be_empty actual && + test_expect_code 1 git config get --url=https://good.example.com --bool doesnt.exist >actual && + test_must_be_empty actual && echo true >expect && git config --bool --get-urlmatch http.SSLverify https://good.example.com >actual && test_cmp expect actual && + git config get --bool --url=https://good.example.com http.SSLverify >actual && + test_cmp expect actual && echo false >expect && git config --bool --get-urlmatch http.sslverify https://weak.example.com >actual && test_cmp expect actual && + git config get --bool --url=https://weak.example.com http.sslverify >actual && + test_cmp expect actual && { echo http.cookiefile /tmp/cookie.txt && echo http.sslverify false } >expect && git config --get-urlmatch HTTP https://weak.example.com >actual && + test_cmp expect actual && + git config get --url=https://weak.example.com HTTP >actual && test_cmp expect actual ' @@ -1817,6 +1862,8 @@ test_expect_success 'urlmatch with --show-scope' ' local http.sslverify false EOF git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual && + test_cmp expect actual && + git config get --url=https://weak.example.com --show-scope HTTP >actual && test_cmp expect actual ' @@ -1849,45 +1896,67 @@ test_expect_success 'urlmatch favors more specific URLs' ' echo http.cookiefile /tmp/root.txt >expect && git config --get-urlmatch HTTP https://example.com >actual && test_cmp expect actual && + git config get --url=https://example.com HTTP >actual && + test_cmp expect actual && echo http.cookiefile /tmp/subdirectory.txt >expect && git config --get-urlmatch HTTP https://example.com/subdirectory >actual && test_cmp expect actual && + git config get --url=https://example.com/subdirectory HTTP >actual && + test_cmp expect actual && echo http.cookiefile /tmp/subdirectory.txt >expect && git config --get-urlmatch HTTP https://example.com/subdirectory/nested >actual && test_cmp expect actual && + git config get --url=https://example.com/subdirectory/nested HTTP >actual && + test_cmp expect actual && echo http.cookiefile /tmp/user.txt >expect && git config --get-urlmatch HTTP https://user@example.com/ >actual && test_cmp expect actual && + git config get --url=https://user@example.com/ HTTP >actual && + test_cmp expect actual && echo http.cookiefile /tmp/subdirectory.txt >expect && git config --get-urlmatch HTTP https://averylonguser@example.com/subdirectory >actual && test_cmp expect actual && + git config get --url=https://averylonguser@example.com/subdirectory HTTP >actual && + test_cmp expect actual && echo http.cookiefile /tmp/preceding.txt >expect && git config --get-urlmatch HTTP https://preceding.example.com >actual && test_cmp expect actual && + git config get --url=https://preceding.example.com HTTP >actual && + test_cmp expect actual && echo http.cookiefile /tmp/wildcard.txt >expect && git config --get-urlmatch HTTP https://wildcard.example.com >actual && test_cmp expect actual && + git config get --url=https://wildcard.example.com HTTP >actual && + test_cmp expect actual && echo http.cookiefile /tmp/sub.txt >expect && git config --get-urlmatch HTTP https://sub.example.com/wildcardwithsubdomain >actual && test_cmp expect actual && + git config get --url=https://sub.example.com/wildcardwithsubdomain HTTP >actual && + test_cmp expect actual && echo http.cookiefile /tmp/trailing.txt >expect && git config --get-urlmatch HTTP https://trailing.example.com >actual && test_cmp expect actual && + git config get --url=https://trailing.example.com HTTP >actual && + test_cmp expect actual && echo http.cookiefile /tmp/sub.txt >expect && git config --get-urlmatch HTTP https://user@sub.example.com >actual && test_cmp expect actual && + git config get --url=https://user@sub.example.com HTTP >actual && + test_cmp expect actual && echo http.cookiefile /tmp/multiwildcard.txt >expect && git config --get-urlmatch HTTP https://wildcard.example.org >actual && + test_cmp expect actual && + git config get --url=https://wildcard.example.org HTTP >actual && test_cmp expect actual ' @@ -1954,7 +2023,7 @@ test_expect_success '--unset last key removes section (except if commented)' ' # please be careful when you update the above variable EOF - git config --unset section.key && + git config ${mode_unset} section.key && test_cmp expect .git/config && cat >.git/config <<-\EOF && @@ -1967,7 +2036,7 @@ test_expect_success '--unset last key removes section (except if commented)' ' [next-section] EOF - git config --unset section.key && + git config ${mode_unset} section.key && test_cmp expect .git/config && q_to_tab >.git/config <<-\EOF && @@ -1977,7 +2046,7 @@ test_expect_success '--unset last key removes section (except if commented)' ' [two] key = true EOF - git config --unset two.key && + git config ${mode_unset} two.key && ! grep two .git/config && q_to_tab >.git/config <<-\EOF && @@ -1987,7 +2056,7 @@ test_expect_success '--unset last key removes section (except if commented)' ' [one] key = true EOF - git config --unset-all one.key && + git config ${mode_unset_all} one.key && test_line_count = 0 .git/config && q_to_tab >.git/config <<-\EOF && @@ -1997,7 +2066,7 @@ test_expect_success '--unset last key removes section (except if commented)' ' [two] Qkey = true EOF - git config --unset two.key && + git config ${mode_unset} two.key && grep two .git/config && q_to_tab >.git/config <<-\EOF && @@ -2009,8 +2078,8 @@ test_expect_success '--unset last key removes section (except if commented)' ' [TWO "subsection"] [one] EOF - git config --unset two.subsection.key && - test "not [two subsection]" = "$(git config one.key)" && + git config ${mode_unset} two.subsection.key && + test "not [two subsection]" = "$(git config ${mode_get} one.key)" && test_line_count = 3 .git/config ' @@ -2021,7 +2090,7 @@ test_expect_success '--unset-all removes section if empty & uncommented' ' key = value2 EOF - git config --unset-all section.key && + git config ${mode_unset_all} section.key && test_line_count = 0 .git/config ' @@ -2044,7 +2113,7 @@ test_expect_success POSIXPERM,PERL 'preserves existing permissions' ' git config imap.pass Hunter2 && perl -e \ "die q(badset) if ((stat(q(.git/config)))[2] & 07777) != 0600" && - git config --rename-section imap pop && + git config ${mode_prefix}rename-section imap pop && perl -e \ "die q(badrename) if ((stat(q(.git/config)))[2] & 07777) != 0600" ' @@ -2093,7 +2162,7 @@ test_expect_success '--show-origin with --list' ' command line: user.cmdline=true EOF GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=user.environ GIT_CONFIG_VALUE_0=true\ - git -c user.cmdline=true config --list --show-origin >output && + git -c user.cmdline=true config ${mode_prefix}list --show-origin >output && test_cmp expect output ' @@ -2110,7 +2179,7 @@ test_expect_success '--show-origin with --list --null' ' includeQcommand line:Quser.cmdline trueQ EOF - git -c user.cmdline=true config --null --list --show-origin >output.raw && + git -c user.cmdline=true config ${mode_prefix}list --null --show-origin >output.raw && nul_to_q <output.raw >output && # The here-doc above adds a newline that the --null output would not # include. Add it here to make the two comparable. @@ -2124,7 +2193,7 @@ test_expect_success '--show-origin with single file' ' file:.git/config user.override=local file:.git/config include.path=../include/relative.include EOF - git config --local --list --show-origin >output && + git config ${mode_prefix}list --local --show-origin >output && test_cmp expect output ' @@ -2133,7 +2202,7 @@ test_expect_success '--show-origin with --get-regexp' ' file:$HOME/.gitconfig user.global true file:.git/config user.local true EOF - git config --show-origin --get-regexp "user\.[g|l].*" >output && + git config ${mode_get_regexp} --show-origin "user\.[g|l].*" >output && test_cmp expect output ' @@ -2141,7 +2210,7 @@ test_expect_success '--show-origin getting a single key' ' cat >expect <<-\EOF && file:.git/config local EOF - git config --show-origin user.override >output && + git config ${mode_get} --show-origin user.override >output && test_cmp expect output ' @@ -2162,7 +2231,7 @@ test_expect_success !MINGW '--show-origin escape special file name characters' ' cat >expect <<-\EOF && file:"file\" (dq) and spaces.conf" user.custom=true EOF - git config --file "$WEIRDLY_NAMED_FILE" --show-origin --list >output && + git config ${mode_prefix}list --file "$WEIRDLY_NAMED_FILE" --show-origin >output && test_cmp expect output ' @@ -2170,7 +2239,7 @@ test_expect_success '--show-origin stdin' ' cat >expect <<-\EOF && standard input: user.custom=true EOF - git config --file - --show-origin --list <"$CUSTOM_CONFIG_FILE" >output && + git config ${mode_prefix}list --file - --show-origin <"$CUSTOM_CONFIG_FILE" >output && test_cmp expect output ' @@ -2197,7 +2266,7 @@ test_expect_success '--show-origin blob' ' cat >expect <<-EOF && blob:$blob user.custom=true EOF - git config --blob=$blob --show-origin --list >output && + git config ${mode_prefix}list --blob=$blob --show-origin >output && test_cmp expect output ) ' @@ -2213,7 +2282,7 @@ test_expect_success '--show-origin blob ref' ' cp "$CUSTOM_CONFIG_FILE" custom.conf && git add custom.conf && git commit -m "new config file" && - git config --blob=main:custom.conf --show-origin --list >output && + git config ${mode_prefix}list --blob=main:custom.conf --show-origin >output && test_cmp expect output ) ' @@ -2239,13 +2308,14 @@ test_expect_success '--show-scope with --list' ' worktree user.worktree=true command user.cmdline=true EOF + test_when_finished "git worktree remove wt1" && git worktree add wt1 && # We need these to test for worktree scope, but outside of this # test, this is just noise test_config core.repositoryformatversion 1 && test_config extensions.worktreeConfig true && git config --worktree user.worktree true && - git -c user.cmdline=true config --list --show-scope >output && + git -c user.cmdline=true config ${mode_prefix}list --show-scope >output && test_cmp expect output ' @@ -2254,7 +2324,7 @@ test_expect_success !MINGW '--show-scope with --blob' ' cat >expect <<-EOF && command user.custom=true EOF - git config --blob=$blob --show-scope --list >output && + git config ${mode_prefix}list --blob=$blob --show-scope >output && test_cmp expect output ' @@ -2264,7 +2334,7 @@ test_expect_success '--show-scope with --local' ' local user.override=local local include.path=../include/relative.include EOF - git config --local --list --show-scope >output && + git config ${mode_prefix}list --local --show-scope >output && test_cmp expect output ' @@ -2272,7 +2342,7 @@ test_expect_success '--show-scope getting a single value' ' cat >expect <<-\EOF && local true EOF - git config --show-scope --get user.local >output && + git config ${mode_get} --show-scope user.local >output && test_cmp expect output ' @@ -2288,7 +2358,7 @@ test_expect_success '--show-scope with --show-origin' ' local file:.git/../include/relative.include user.relative=include command command line: user.cmdline=true EOF - git -c user.cmdline=true config --list --show-origin --show-scope >output && + git -c user.cmdline=true config ${mode_prefix}list --show-origin --show-scope >output && test_cmp expect output ' @@ -2329,7 +2399,7 @@ test_expect_success 'override global and system config' ' global home.config=true local local.config=true EOF - git config --show-scope --list >output && + git config ${mode_prefix}list --show-scope >output && test_cmp expect output && cat >expect <<-EOF && @@ -2338,20 +2408,20 @@ test_expect_success 'override global and system config' ' local local.config=true EOF GIT_CONFIG_NOSYSTEM=false GIT_CONFIG_SYSTEM=custom-system-config GIT_CONFIG_GLOBAL=custom-global-config \ - git config --show-scope --list >output && + git config ${mode_prefix}list --show-scope >output && test_cmp expect output && cat >expect <<-EOF && local local.config=true EOF GIT_CONFIG_NOSYSTEM=false GIT_CONFIG_SYSTEM=/dev/null GIT_CONFIG_GLOBAL=/dev/null \ - git config --show-scope --list >output && + git config ${mode_prefix}list --show-scope >output && test_cmp expect output ' test_expect_success 'override global and system config with missing file' ' - test_must_fail env GIT_CONFIG_GLOBAL=does-not-exist GIT_CONFIG_SYSTEM=/dev/null git config --global --list && - test_must_fail env GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=does-not-exist git config --system --list && + test_must_fail env GIT_CONFIG_GLOBAL=does-not-exist GIT_CONFIG_SYSTEM=/dev/null git config ${mode_prefix}list --global && + test_must_fail env GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=does-not-exist git config ${mode_prefix}list --system && GIT_CONFIG_GLOBAL=does-not-exist GIT_CONFIG_SYSTEM=does-not-exist git version ' @@ -2467,7 +2537,7 @@ test_expect_success '--replace-all does not invent newlines' ' [abc] Qkey = b EOF - git config --replace-all abc.key b && + git config ${mode_replace_all} abc.key b && test_cmp expect .git/config ' @@ -2478,7 +2548,7 @@ test_expect_success 'set all config with value-pattern' ' # no match => add new entry cp initial config && git config --file=config abc.key two a+ && - git config --file=config --list >actual && + git config ${mode_prefix}list --file=config >actual && cat >expect <<-\EOF && abc.key=one abc.key=two @@ -2491,7 +2561,7 @@ test_expect_success 'set all config with value-pattern' ' # multiple values, no match => add git config --file=config abc.key three a+ && - git config --file=config --list >actual && + git config ${mode_prefix}list --file=config >actual && cat >expect <<-\EOF && abc.key=one abc.key=two @@ -2501,7 +2571,7 @@ test_expect_success 'set all config with value-pattern' ' # single match => replace git config --file=config abc.key four h+ && - git config --file=config --list >actual && + git config ${mode_prefix}list --file=config >actual && cat >expect <<-\EOF && abc.key=one abc.key=two @@ -2516,7 +2586,7 @@ test_expect_success '--replace-all and value-pattern' ' git config --file=config --add abc.key two && git config --file=config --add abc.key three && git config --file=config --replace-all abc.key four "o+" && - git config --file=config --list >actual && + git config ${mode_prefix}list --file=config >actual && cat >expect <<-\EOF && abc.key=four abc.key=three @@ -2532,20 +2602,20 @@ test_expect_success 'refuse --fixed-value for incompatible actions' ' test_must_fail git config --file=config --fixed-value --add dev.null bogus && test_must_fail git config --file=config --fixed-value --get-urlmatch dev.null bogus && test_must_fail git config --file=config --fixed-value --get-urlmatch dev.null bogus && - test_must_fail git config --file=config --fixed-value --rename-section dev null && - test_must_fail git config --file=config --fixed-value --remove-section dev && - test_must_fail git config --file=config --fixed-value --list && + test_must_fail git config ${mode_prefix}rename-section --file=config --fixed-value dev null && + test_must_fail git config ${mode_prefix}remove-section --file=config --fixed-value dev && + test_must_fail git config ${mode_prefix}list --file=config --fixed-value && test_must_fail git config --file=config --fixed-value --get-color dev.null && test_must_fail git config --file=config --fixed-value --get-colorbool dev.null && # These modes complain when --fixed-value has no value-pattern - test_must_fail git config --file=config --fixed-value dev.null bogus && - test_must_fail git config --file=config --fixed-value --replace-all dev.null bogus && - test_must_fail git config --file=config --fixed-value --get dev.null && - test_must_fail git config --file=config --fixed-value --get-all dev.null && - test_must_fail git config --file=config --fixed-value --get-regexp "dev.*" && - test_must_fail git config --file=config --fixed-value --unset dev.null && - test_must_fail git config --file=config --fixed-value --unset-all dev.null + test_must_fail git config ${mode_set} --file=config --fixed-value dev.null bogus && + test_must_fail git config ${mode_replace_all} --file=config --fixed-value dev.null bogus && + test_must_fail git config ${mode_prefix}get --file=config --fixed-value dev.null && + test_must_fail git config ${mode_get_all} --file=config --fixed-value dev.null && + test_must_fail git config ${mode_get_regexp} --file=config --fixed-value "dev.*" && + test_must_fail git config ${mode_unset} --file=config --fixed-value dev.null && + test_must_fail git config ${mode_unset_all} --file=config --fixed-value dev.null ' test_expect_success '--fixed-value uses exact string matching' ' @@ -2555,7 +2625,7 @@ test_expect_success '--fixed-value uses exact string matching' ' cp initial config && git config --file=config fixed.test bogus "$META" && - git config --file=config --list >actual && + git config ${mode_prefix}list --file=config >actual && cat >expect <<-EOF && fixed.test=$META fixed.test=bogus @@ -2564,7 +2634,7 @@ test_expect_success '--fixed-value uses exact string matching' ' cp initial config && git config --file=config --fixed-value fixed.test bogus "$META" && - git config --file=config --list >actual && + git config ${mode_prefix}list --file=config >actual && cat >expect <<-\EOF && fixed.test=bogus EOF @@ -2573,16 +2643,21 @@ test_expect_success '--fixed-value uses exact string matching' ' cp initial config && test_must_fail git config --file=config --unset fixed.test "$META" && git config --file=config --fixed-value --unset fixed.test "$META" && - test_must_fail git config --file=config fixed.test && + test_must_fail git config ${mode_get} --file=config fixed.test && + + cp initial config && + test_must_fail git config unset --file=config --value="$META" fixed.test && + git config unset --file=config --fixed-value --value="$META" fixed.test && + test_must_fail git config ${mode_get} --file=config fixed.test && cp initial config && test_must_fail git config --file=config --unset-all fixed.test "$META" && git config --file=config --fixed-value --unset-all fixed.test "$META" && - test_must_fail git config --file=config fixed.test && + test_must_fail git config ${mode_get} --file=config fixed.test && cp initial config && - git config --file=config --replace-all fixed.test bogus "$META" && - git config --file=config --list >actual && + git config --file=config fixed.test bogus "$META" && + git config ${mode_prefix}list --file=config >actual && cat >expect <<-EOF && fixed.test=$META fixed.test=bogus @@ -2590,7 +2665,7 @@ test_expect_success '--fixed-value uses exact string matching' ' test_cmp expect actual && git config --file=config --fixed-value --replace-all fixed.test bogus "$META" && - git config --file=config --list >actual && + git config ${mode_prefix}list --file=config >actual && cat >expect <<-EOF && fixed.test=bogus fixed.test=bogus @@ -2605,18 +2680,27 @@ test_expect_success '--get and --get-all with --fixed-value' ' git config --file=config --add fixed.test "$META" && git config --file=config --get fixed.test bogus && + git config get --file=config --value=bogus fixed.test && test_must_fail git config --file=config --get fixed.test "$META" && + test_must_fail git config get --file=config --value="$META" fixed.test && git config --file=config --get --fixed-value fixed.test "$META" && + git config get --file=config --fixed-value --value="$META" fixed.test && test_must_fail git config --file=config --get --fixed-value fixed.test non-existent && git config --file=config --get-all fixed.test bogus && + git config get --all --file=config --value=bogus fixed.test && test_must_fail git config --file=config --get-all fixed.test "$META" && + test_must_fail git config get --all --file=config --value="$META" fixed.test && git config --file=config --get-all --fixed-value fixed.test "$META" && + git config get --all --file=config --value="$META" --fixed-value fixed.test && test_must_fail git config --file=config --get-all --fixed-value fixed.test non-existent && git config --file=config --get-regexp fixed+ bogus && + git config get --regexp --file=config --value=bogus fixed+ && test_must_fail git config --file=config --get-regexp fixed+ "$META" && + test_must_fail git config get --regexp --file=config --value="$META" fixed+ && git config --file=config --get-regexp --fixed-value fixed+ "$META" && + git config get --regexp --file=config --fixed-value --value="$META" fixed+ && test_must_fail git config --file=config --get-regexp --fixed-value fixed+ non-existent ' @@ -2738,4 +2822,25 @@ test_expect_success 'includeIf.hasconfig:remote.*.url forbids remote url in such grep "fatal: remote URLs cannot be configured in file directly or indirectly included by includeIf.hasconfig:remote.*.url" err ' +test_expect_success 'negated mode causes failure' ' + test_must_fail git config --no-get 2>err && + grep "unknown option \`no-get${SQ}" err +' + +test_expect_success 'specifying multiple modes causes failure' ' + cat >expect <<-EOF && + error: options ${SQ}--get-all${SQ} and ${SQ}--get${SQ} cannot be used together + EOF + test_must_fail git config --get --get-all 2>err && + test_cmp expect err +' + +test_expect_success 'writing to stdin is rejected' ' + echo "fatal: writing to stdin is not supported" >expect && + test_must_fail git config ${mode_set} --file - foo.bar baz 2>err && + test_cmp expect err +' + +done + test_done diff --git a/t/t1350-config-hooks-path.sh b/t/t1350-config-hooks-path.sh index f6dc83e..45a0492 100755 --- a/t/t1350-config-hooks-path.sh +++ b/t/t1350-config-hooks-path.sh @@ -41,4 +41,11 @@ test_expect_success 'git rev-parse --git-path hooks' ' test .git/custom-hooks/abc = "$(cat actual)" ' +test_expect_success 'core.hooksPath=/dev/null' ' + git clone -c core.hooksPath=/dev/null . no-templates && + value="$(git -C no-templates config --local core.hooksPath)" && + # The Bash used by Git for Windows rewrites `/dev/null` to `nul` + { test /dev/null = "$value" || test nul = "$value"; } +' + test_done diff --git a/t/t1404-update-ref-errors.sh b/t/t1404-update-ref-errors.sh index 98e9158..67ebd81 100755 --- a/t/t1404-update-ref-errors.sh +++ b/t/t1404-update-ref-errors.sh @@ -100,7 +100,7 @@ df_test() { printf "%s\n" "delete $delname" "create $addname $D" fi >commands && test_must_fail git update-ref --stdin <commands 2>output.err && - grep "fatal:\( cannot lock ref $SQ$addname$SQ:\)\? $SQ$delref$SQ exists; cannot create $SQ$addref$SQ" output.err && + grep -E "fatal:( cannot lock ref $SQ$addname$SQ:)? $SQ$delref$SQ exists; cannot create $SQ$addref$SQ" output.err && printf "%s\n" "$C $delref" >expected-refs && git for-each-ref --format="%(objectname) %(refname)" $prefix/r >actual-refs && test_cmp expected-refs actual-refs diff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh index 2092488..067fd57 100755 --- a/t/t1416-ref-transaction-hooks.sh +++ b/t/t1416-ref-transaction-hooks.sh @@ -134,4 +134,27 @@ test_expect_success 'interleaving hook calls succeed' ' test_cmp expect target-repo.git/actual ' +test_expect_success 'hook captures git-symbolic-ref updates' ' + test_when_finished "rm actual" && + + test_hook reference-transaction <<-\EOF && + echo "$*" >>actual + while read -r line + do + printf "%s\n" "$line" + done >>actual + EOF + + git symbolic-ref refs/heads/symref refs/heads/main && + + cat >expect <<-EOF && + prepared + $ZERO_OID ref:refs/heads/main refs/heads/symref + committed + $ZERO_OID ref:refs/heads/main refs/heads/symref + EOF + + test_cmp expect actual +' + test_done diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh index a669e59..30c3191 100755 --- a/t/t1500-rev-parse.sh +++ b/t/t1500-rev-parse.sh @@ -304,4 +304,10 @@ test_expect_success 'rev-parse --bisect includes bad, excludes good' ' test_cmp expect actual ' +test_expect_success '--short= truncates to the actual hash length' ' + git rev-parse HEAD >expect && + git rev-parse --short=100 HEAD >actual && + test_cmp expect actual +' + test_done diff --git a/t/t1517-outside-repo.sh b/t/t1517-outside-repo.sh new file mode 100755 index 0000000..557808f --- /dev/null +++ b/t/t1517-outside-repo.sh @@ -0,0 +1,59 @@ +#!/bin/sh + +test_description='check random commands outside repo' + +TEST_PASSES_SANITIZE_LEAK=true +. ./test-lib.sh + +test_expect_success 'set up a non-repo directory and test file' ' + GIT_CEILING_DIRECTORIES=$(pwd) && + export GIT_CEILING_DIRECTORIES && + mkdir non-repo && + ( + cd non-repo && + # confirm that git does not find a repo + test_must_fail git rev-parse --git-dir + ) && + test_write_lines one two three four >nums && + git add nums && + cp nums nums.old && + test_write_lines five >>nums && + git diff >sample.patch +' + +test_expect_success 'compute a patch-id outside repository (uses SHA-1)' ' + nongit env GIT_DEFAULT_HASH=sha1 \ + git patch-id <sample.patch >patch-id.expect && + nongit \ + git patch-id <sample.patch >patch-id.actual && + test_cmp patch-id.expect patch-id.actual +' + +test_expect_success 'hash-object outside repository (uses SHA-1)' ' + nongit env GIT_DEFAULT_HASH=sha1 \ + git hash-object --stdin <sample.patch >hash.expect && + nongit \ + git hash-object --stdin <sample.patch >hash.actual && + test_cmp hash.expect hash.actual +' + +test_expect_success 'apply a patch outside repository' ' + ( + cd non-repo && + cp ../nums.old nums && + git apply ../sample.patch + ) && + test_cmp nums non-repo/nums +' + +test_expect_success 'grep outside repository' ' + git grep --cached two >expect && + ( + cd non-repo && + cp ../nums.old nums && + git grep --no-index two >../actual + ) && + test_cmp expect actual +' + +test_done diff --git a/t/t1700-split-index.sh b/t/t1700-split-index.sh index a7b7263..ac4a5b2 100755 --- a/t/t1700-split-index.sh +++ b/t/t1700-split-index.sh @@ -527,7 +527,7 @@ test_expect_success 'reading split index at alternate location' ' # ... and, for backwards compatibility, in the current GIT_DIR # as well. - mv -v ./reading-alternate-location/.git/sharedindex.* .git && + mv ./reading-alternate-location/.git/sharedindex.* .git && GIT_INDEX_FILE=./reading-alternate-location/.git/index \ git ls-files --cached >actual && test_cmp expect actual diff --git a/t/t2405-worktree-submodule.sh b/t/t2405-worktree-submodule.sh index 11018f3..1d7f605 100755 --- a/t/t2405-worktree-submodule.sh +++ b/t/t2405-worktree-submodule.sh @@ -5,6 +5,7 @@ test_description='Combination of submodules and multiple worktrees' GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh base_path=$(pwd -P) diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh index 28a95a7..6624a4f 100755 --- a/t/t3701-add-interactive.sh +++ b/t/t3701-add-interactive.sh @@ -160,6 +160,14 @@ test_expect_success 'revert works (commit)' ' grep "unchanged *+3/-0 file" output ' +test_expect_success 'reject multi-key input' ' + saved=$(git hash-object -w file) && + test_when_finished "git cat-file blob $saved >file" && + echo an extra line >>file && + test_write_lines aa | git add -p >actual && + test_grep "is expected, got ${SQ}aa${SQ}" actual +' + test_expect_success 'setup expected' ' cat >expected <<-\EOF EOF @@ -526,7 +534,7 @@ test_expect_success 'split hunk setup' ' test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test ' -test_expect_success 'goto hunk' ' +test_expect_success 'goto hunk 1 with "g 1"' ' test_when_finished "git reset" && tr _ " " >expect <<-EOF && (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? + 1: -1,2 +1,3 +15 @@ -542,7 +550,20 @@ test_expect_success 'goto hunk' ' test_cmp expect actual.trimmed ' -test_expect_success 'navigate to hunk via regex' ' +test_expect_success 'goto hunk 1 with "g1"' ' + test_when_finished "git reset" && + tr _ " " >expect <<-EOF && + _10 + +15 + _20 + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_ + EOF + test_write_lines s y g1 | git add -p >actual && + tail -n 4 <actual >actual.trimmed && + test_cmp expect actual.trimmed +' + +test_expect_success 'navigate to hunk via regex /pattern' ' test_when_finished "git reset" && tr _ " " >expect <<-EOF && (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,p,?]? @@ -1,2 +1,3 @@ @@ -556,6 +577,19 @@ test_expect_success 'navigate to hunk via regex' ' test_cmp expect actual.trimmed ' +test_expect_success 'navigate to hunk via regex / pattern' ' + test_when_finished "git reset" && + tr _ " " >expect <<-EOF && + _10 + +15 + _20 + (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,p,?]?_ + EOF + test_write_lines s y / 1,2 | git add -p >actual && + tail -n 4 <actual >actual.trimmed && + test_cmp expect actual.trimmed +' + test_expect_success 'split hunk "add -p (edit)"' ' # Split, say Edit and do nothing. Then: # diff --git a/t/t4046-diff-unmerged.sh b/t/t4046-diff-unmerged.sh index fb8c517..afda629 100755 --- a/t/t4046-diff-unmerged.sh +++ b/t/t4046-diff-unmerged.sh @@ -98,4 +98,12 @@ test_expect_success 'diff --stat' ' test_cmp diff-stat.expect diff-stat.actual ' +test_expect_success 'diff --quiet' ' + test_expect_code 1 git diff --cached --quiet +' + +test_expect_success 'diff --quiet --ignore-all-space' ' + test_expect_code 1 git diff --cached --quiet --ignore-all-space +' + test_done diff --git a/t/t4202-log.sh b/t/t4202-log.sh index 60fe60d..86c695e 100755 --- a/t/t4202-log.sh +++ b/t/t4202-log.sh @@ -2022,7 +2022,7 @@ test_expect_success GPGSM 'log --graph --show-signature x509' ' test_expect_success GPGSSH 'log --graph --show-signature ssh' ' test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" && git log --graph --show-signature -n1 signed-ssh >actual && - grep "${GOOD_SIGNATURE_TRUSTED}" actual + grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual ' test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'log shows failure on expired signature key' ' diff --git a/t/t4204-patch-id.sh b/t/t4204-patch-id.sh index a7fa94c..605faea 100755 --- a/t/t4204-patch-id.sh +++ b/t/t4204-patch-id.sh @@ -310,4 +310,38 @@ test_expect_success 'patch-id handles diffs with one line of before/after' ' test_config patchid.stable true && calc_patch_id diffu1stable <diffu1 ' + +test_expect_failure 'patch-id computes same ID with different object hashes' ' + test_when_finished "rm -rf repo-sha1 repo-sha256" && + + cat >diff <<-\EOF && + diff --git a/bar b/bar + index bdaf90f..31051f6 100644 + --- a/bar + +++ b/bar + @@ -2 +2,2 @@ + b + +c + EOF + + git init --object-format=sha1 repo-sha1 && + git -C repo-sha1 patch-id <diff >patch-id-sha1 && + git init --object-format=sha256 repo-sha256 && + git -C repo-sha256 patch-id <diff >patch-id-sha256 && + test_cmp patch-id-sha1 patch-id-sha256 +' + +test_expect_success 'patch-id without repository' ' + cat >diff <<-\EOF && + diff --git a/bar b/bar + index bdaf90f..31051f6 100644 + --- a/bar + +++ b/bar + @@ -2 +2,2 @@ + b + +c + EOF + nongit git patch-id <diff +' + test_done diff --git a/t/t5001-archive-attr.sh b/t/t5001-archive-attr.sh index eaf959d..7310774 100755 --- a/t/t5001-archive-attr.sh +++ b/t/t5001-archive-attr.sh @@ -133,7 +133,8 @@ test_expect_success 'git archive vs. bare' ' ' test_expect_success 'git archive with worktree attributes, bare' ' - (cd bare && git archive --worktree-attributes HEAD) >bare-worktree.tar && + (cd bare && + git -c attr.tree=HEAD archive --worktree-attributes HEAD) >bare-worktree.tar && (mkdir bare-worktree && cd bare-worktree && "$TAR" xf -) <bare-worktree.tar ' diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh index 33d34d5..3b3991a 100755 --- a/t/t5510-fetch.sh +++ b/t/t5510-fetch.sh @@ -518,7 +518,7 @@ test_expect_success 'fetch with a non-applying branch.<name>.merge' ' test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [1]' ' one_head=$(cd one && git rev-parse HEAD) && this_head=$(git rev-parse HEAD) && - git update-ref -d FETCH_HEAD && + rm .git/FETCH_HEAD && git fetch one && test $one_head = "$(git rev-parse --verify FETCH_HEAD)" && test $this_head = "$(git rev-parse --verify HEAD)" @@ -530,7 +530,7 @@ test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge one_ref=$(cd one && git symbolic-ref HEAD) && git config branch.main.remote blub && git config branch.main.merge "$one_ref" && - git update-ref -d FETCH_HEAD && + rm .git/FETCH_HEAD && git fetch one && test $one_head = "$(git rev-parse --verify FETCH_HEAD)" && test $this_head = "$(git rev-parse --verify HEAD)" @@ -540,7 +540,7 @@ test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge # the merge spec does not match the branch the remote HEAD points to test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [3]' ' git config branch.main.merge "${one_ref}_not" && - git update-ref -d FETCH_HEAD && + rm .git/FETCH_HEAD && git fetch one && test $one_head = "$(git rev-parse --verify FETCH_HEAD)" && test $this_head = "$(git rev-parse --verify HEAD)" @@ -1091,6 +1091,22 @@ test_expect_success 'branchname D/F conflict resolved by --prune' ' test_cmp expect actual ' +test_expect_success 'branchname D/F conflict rejected with targeted error message' ' + git clone . df-conflict-error && + git branch dir_conflict && + ( + cd df-conflict-error && + git update-ref refs/remotes/origin/dir_conflict/file HEAD && + test_must_fail git fetch 2>err && + test_grep "error: some local refs could not be updated; try running" err && + test_grep " ${SQ}git remote prune origin${SQ} to remove any old, conflicting branches" err && + git pack-refs --all && + test_must_fail git fetch 2>err-packed && + test_grep "error: some local refs could not be updated; try running" err-packed && + test_grep " ${SQ}git remote prune origin${SQ} to remove any old, conflicting branches" err-packed + ) +' + test_expect_success 'fetching a one-level ref works' ' test_commit extra && git reset --hard HEAD^ && @@ -1252,6 +1268,30 @@ EOF test_cmp fatal-expect fatal-actual ' +test_expect_success SYMLINKS 'clone does not get confused by a D/F conflict' ' + git init df-conflict && + ( + cd df-conflict && + ln -s .git a && + git add a && + test_tick && + git commit -m symlink && + test_commit a- && + rm a && + mkdir -p a/hooks && + write_script a/hooks/post-checkout <<-EOF && + echo WHOOPSIE >&2 + echo whoopsie >"$TRASH_DIRECTORY"/whoops + EOF + git add a/hooks/post-checkout && + test_tick && + git commit -m post-checkout + ) && + git clone df-conflict clone 2>err && + test_grep ! WHOOPS err && + test_path_is_missing whoops +' + . "$TEST_DIRECTORY"/lib-httpd.sh start_httpd diff --git a/t/t5550-http-fetch-dumb.sh b/t/t5550-http-fetch-dumb.sh index 4c3b327..5f16cbc 100755 --- a/t/t5550-http-fetch-dumb.sh +++ b/t/t5550-http-fetch-dumb.sh @@ -55,6 +55,21 @@ test_expect_success 'list refs from outside any repository' ' test_cmp expect actual ' + +test_expect_success 'list detached HEAD from outside any repository' ' + git clone --mirror "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \ + "$HTTPD_DOCUMENT_ROOT_PATH/repo-detached.git" && + git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo-detached.git" \ + update-ref --no-deref HEAD refs/heads/main && + git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo-detached.git" update-server-info && + cat >expect <<-EOF && + $(git rev-parse main) HEAD + $(git rev-parse main) refs/heads/main + EOF + nongit git ls-remote "$HTTPD_URL/dumb/repo-detached.git" >actual && + test_cmp expect actual +' + test_expect_success 'create password-protected repository' ' mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/" && cp -Rf "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \ diff --git a/t/t5601-clone.sh b/t/t5601-clone.sh index ca43185..cc0b953 100755 --- a/t/t5601-clone.sh +++ b/t/t5601-clone.sh @@ -650,6 +650,21 @@ test_expect_success CASE_INSENSITIVE_FS 'colliding file detection' ' test_grep "the following paths have collided" icasefs/warning ' +test_expect_success CASE_INSENSITIVE_FS,SYMLINKS \ + 'colliding symlink/directory keeps directory' ' + git init icasefs-colliding-symlink && + ( + cd icasefs-colliding-symlink && + a=$(printf a | git hash-object -w --stdin) && + printf "100644 %s 0\tA/dir/b\n120000 %s 0\ta\n" $a $a >idx && + git update-index --index-info <idx && + test_tick && + git commit -m initial + ) && + git clone icasefs-colliding-symlink icasefs-colliding-symlink-clone && + test_file_not_empty icasefs-colliding-symlink-clone/A/dir/b +' + test_expect_success 'clone with GIT_DEFAULT_HASH' ' ( sane_unset GIT_DEFAULT_HASH && diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh index 948f1bb..163c378 100755 --- a/t/t6302-for-each-ref-filter.sh +++ b/t/t6302-for-each-ref-filter.sh @@ -52,6 +52,23 @@ test_expect_success '--include-root-refs pattern prints pseudorefs' ' test_cmp expect actual ' +test_expect_success '--include-root-refs pattern does not print special refs' ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit initial && + git rev-parse HEAD >.git/MERGE_HEAD && + git for-each-ref --format="%(refname)" --include-root-refs >actual && + cat >expect <<-EOF && + HEAD + $(git symbolic-ref HEAD) + refs/tags/initial + EOF + test_cmp expect actual + ) +' + test_expect_success '--include-root-refs with other patterns' ' cat >expect <<-\EOF && HEAD @@ -62,6 +79,23 @@ test_expect_success '--include-root-refs with other patterns' ' test_cmp expect actual ' +test_expect_success '--include-root-refs omits dangling symrefs' ' + test_when_finished "rm -rf repo" && + git init repo && + ( + cd repo && + test_commit initial && + git symbolic-ref DANGLING_HEAD refs/heads/missing && + cat >expect <<-EOF && + HEAD + $(git symbolic-ref HEAD) + refs/tags/initial + EOF + git for-each-ref --format="%(refname)" --include-root-refs >actual && + test_cmp expect actual + ) +' + test_expect_success 'filtering with --points-at' ' cat >expect <<-\EOF && refs/heads/main diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh index 696866d..fa6336e 100755 --- a/t/t7004-tag.sh +++ b/t/t7004-tag.sh @@ -668,6 +668,115 @@ test_expect_success \ test_cmp expect actual ' +# trailers + +test_expect_success 'create tag with -m and --trailer' ' + get_tag_header tag-with-inline-message-and-trailers $commit commit $time >expect && + cat >>expect <<-\EOF && + create tag with trailers + + my-trailer: here + alt-trailer: there + EOF + git tag -m "create tag with trailers" \ + --trailer my-trailer=here \ + --trailer alt-trailer=there \ + tag-with-inline-message-and-trailers && + get_tag_msg tag-with-inline-message-and-trailers >actual && + test_cmp expect actual +' + +test_expect_success 'list tag extracting trailers' ' + cat >expect <<-\EOF && + my-trailer: here + alt-trailer: there + + EOF + git tag --list --format="%(trailers)" tag-with-inline-message-and-trailers >actual && + test_cmp expect actual +' + +test_expect_success 'create tag with -F and --trailer' ' + echo "create tag from message file using --trailer" >messagefilewithnotrailers && + get_tag_header tag-with-file-message-and-trailers $commit commit $time >expect && + cat >>expect <<-\EOF && + create tag from message file using --trailer + + my-trailer: here + alt-trailer: there + EOF + git tag -F messagefilewithnotrailers \ + --trailer my-trailer=here \ + --trailer alt-trailer=there \ + tag-with-file-message-and-trailers && + get_tag_msg tag-with-file-message-and-trailers >actual && + test_cmp expect actual +' + +test_expect_success 'create tag with -m and --trailer and --edit' ' + write_script fakeeditor <<-\EOF && + sed -e "1s/^/EDITED: /g" <"$1" >"$1-" + mv "$1-" "$1" + EOF + get_tag_header tag-with-edited-inline-message-and-trailers $commit commit $time >expect && + cat >>expect <<-\EOF && + EDITED: create tag with trailers + + my-trailer: here + alt-trailer: there + EOF + GIT_EDITOR=./fakeeditor git tag --edit \ + -m "create tag with trailers" \ + --trailer my-trailer=here \ + --trailer alt-trailer=there \ + tag-with-edited-inline-message-and-trailers && + get_tag_msg tag-with-edited-inline-message-and-trailers >actual && + test_cmp expect actual +' + +test_expect_success 'create tag with -F and --trailer and --edit' ' + echo "create tag from message file using --trailer" >messagefilewithnotrailers && + get_tag_header tag-with-edited-file-message-and-trailers $commit commit $time >expect && + cat >>expect <<-\EOF && + EDITED: create tag from message file using --trailer + + my-trailer: here + alt-trailer: there + EOF + GIT_EDITOR=./fakeeditor git tag --edit \ + -F messagefilewithnotrailers \ + --trailer my-trailer=here \ + --trailer alt-trailer=there \ + tag-with-edited-file-message-and-trailers && + get_tag_msg tag-with-edited-file-message-and-trailers >actual && + test_cmp expect actual +' + +test_expect_success 'create annotated tag and force editor when only --trailer is given' ' + write_script fakeeditor <<-\EOF && + echo "add a line" >"$1-" + cat <"$1" >>"$1-" + mv "$1-" "$1" + EOF + get_tag_header tag-with-trailers-and-no-message $commit commit $time >expect && + cat >>expect <<-\EOF && + add a line + + my-trailer: here + alt-trailer: there + EOF + GIT_EDITOR=./fakeeditor git tag \ + --trailer my-trailer=here \ + --trailer alt-trailer=there \ + tag-with-trailers-and-no-message && + get_tag_msg tag-with-trailers-and-no-message >actual && + test_cmp expect actual +' + +test_expect_success 'bad editor causes panic when only --trailer is given' ' + test_must_fail env GIT_EDITOR=false git tag --trailer my-trailer=here tag-will-not-exist +' + # listing messages for annotated non-signed tags: test_expect_success \ @@ -810,6 +919,11 @@ test_expect_success 'git tag --format with ahead-behind' ' refs/tags/tag-lines 0 1 ! refs/tags/tag-one-line 0 1 ! refs/tags/tag-right 0 0 ! + refs/tags/tag-with-edited-file-message-and-trailers 0 1 ! + refs/tags/tag-with-edited-inline-message-and-trailers 0 1 ! + refs/tags/tag-with-file-message-and-trailers 0 1 ! + refs/tags/tag-with-inline-message-and-trailers 0 1 ! + refs/tags/tag-with-trailers-and-no-message 0 1 ! refs/tags/tag-zero-lines 0 1 ! EOF git tag -l --format="%(refname) %(ahead-behind:HEAD) !" >actual 2>err && diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh index 5c4a89d..9814888 100755 --- a/t/t7400-submodule-basic.sh +++ b/t/t7400-submodule-basic.sh @@ -1451,4 +1451,35 @@ test_expect_success 'recursive clone respects -q' ' test_must_be_empty actual ' +test_expect_success '`submodule init` and `init.templateDir`' ' + mkdir -p tmpl/hooks && + write_script tmpl/hooks/post-checkout <<-EOF && + echo HOOK-RUN >&2 + echo I was here >hook.run + exit 1 + EOF + + test_config init.templateDir "$(pwd)/tmpl" && + test_when_finished \ + "git config --global --unset init.templateDir || true" && + ( + sane_unset GIT_TEMPLATE_DIR && + NO_SET_GIT_TEMPLATE_DIR=t && + export NO_SET_GIT_TEMPLATE_DIR && + + git config --global init.templateDir "$(pwd)/tmpl" && + test_must_fail git submodule \ + add "$submodurl" sub-global 2>err && + git config --global --unset init.templateDir && + test_grep HOOK-RUN err && + test_path_is_file sub-global/hook.run && + + git config init.templateDir "$(pwd)/tmpl" && + git submodule add "$submodurl" sub-local 2>err && + git config --unset init.templateDir && + test_grep ! HOOK-RUN err && + test_path_is_missing sub-local/hook.run + ) +' + test_done diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh index 8491b8c..297c6c3 100755 --- a/t/t7406-submodule-update.sh +++ b/t/t7406-submodule-update.sh @@ -1202,4 +1202,52 @@ test_expect_success 'commit with staged submodule change with ignoreSubmodules a add_submodule_commit_and_validate ' +test_expect_success CASE_INSENSITIVE_FS,SYMLINKS \ + 'submodule paths must not follow symlinks' ' + + # This is only needed because we want to run this in a self-contained + # test without having to spin up an HTTP server; However, it would not + # be needed in a real-world scenario where the submodule is simply + # hosted on a public site. + test_config_global protocol.file.allow always && + + # Make sure that Git tries to use symlinks on Windows + test_config_global core.symlinks true && + + tell_tale_path="$PWD/tell.tale" && + git init hook && + ( + cd hook && + mkdir -p y/hooks && + write_script y/hooks/post-checkout <<-EOF && + echo HOOK-RUN >&2 + echo hook-run >"$tell_tale_path" + EOF + git add y/hooks/post-checkout && + test_tick && + git commit -m post-checkout + ) && + + hook_repo_path="$(pwd)/hook" && + git init captain && + ( + cd captain && + git submodule add --name x/y "$hook_repo_path" A/modules/x && + test_tick && + git commit -m add-submodule && + + printf .git >dotgit.txt && + git hash-object -w --stdin <dotgit.txt >dot-git.hash && + printf "120000 %s 0\ta\n" "$(cat dot-git.hash)" >index.info && + git update-index --index-info <index.info && + test_tick && + git commit -m add-symlink + ) && + + test_path_is_missing "$tell_tale_path" && + git clone --recursive captain hooked 2>err && + test_grep ! HOOK-RUN err && + test_path_is_missing "$tell_tale_path" +' + test_done diff --git a/t/t7423-submodule-symlinks.sh b/t/t7423-submodule-symlinks.sh new file mode 100755 index 0000000..f45d806 --- /dev/null +++ b/t/t7423-submodule-symlinks.sh @@ -0,0 +1,68 @@ +#!/bin/sh + +test_description='check that submodule operations do not follow symlinks' + +TEST_PASSES_SANITIZE_LEAK=true +. ./test-lib.sh + +test_expect_success 'prepare' ' + git config --global protocol.file.allow always && + test_commit initial && + git init upstream && + test_commit -C upstream upstream submodule_file && + git submodule add ./upstream a/sm && + test_tick && + git commit -m submodule +' + +test_expect_success SYMLINKS 'git submodule update must not create submodule behind symlink' ' + rm -rf a b && + mkdir b && + ln -s b a && + test_path_is_missing b/sm && + test_must_fail git submodule update && + test_path_is_missing b/sm +' + +test_expect_success SYMLINKS,CASE_INSENSITIVE_FS 'git submodule update must not create submodule behind symlink on case insensitive fs' ' + rm -rf a b && + mkdir b && + ln -s b A && + test_must_fail git submodule update && + test_path_is_missing b/sm +' + +prepare_symlink_to_repo() { + rm -rf a && + mkdir a && + git init a/target && + git -C a/target fetch ../../upstream && + ln -s target a/sm +} + +test_expect_success SYMLINKS 'git restore --recurse-submodules must not be confused by a symlink' ' + prepare_symlink_to_repo && + test_must_fail git restore --recurse-submodules a/sm && + test_path_is_missing a/sm/submodule_file && + test_path_is_dir a/target/.git && + test_path_is_missing a/target/submodule_file +' + +test_expect_success SYMLINKS 'git restore --recurse-submodules must not migrate git dir of symlinked repo' ' + prepare_symlink_to_repo && + rm -rf .git/modules && + test_must_fail git restore --recurse-submodules a/sm && + test_path_is_dir a/target/.git && + test_path_is_missing .git/modules/a/sm && + test_path_is_missing a/target/submodule_file +' + +test_expect_success SYMLINKS 'git checkout -f --recurse-submodules must not migrate git dir of symlinked repo when removing submodule' ' + prepare_symlink_to_repo && + rm -rf .git/modules && + test_must_fail git checkout -f --recurse-submodules initial && + test_path_is_dir a/target/.git && + test_path_is_missing .git/modules/a/sm +' + +test_done diff --git a/t/t7450-bad-git-dotfiles.sh b/t/t7450-bad-git-dotfiles.sh index 46d4fb0..4a9c22c 100755 --- a/t/t7450-bad-git-dotfiles.sh +++ b/t/t7450-bad-git-dotfiles.sh @@ -320,7 +320,7 @@ test_expect_success WINDOWS 'prevent git~1 squatting on Windows' ' fi ' -test_expect_success 'git dirs of sibling submodules must not be nested' ' +test_expect_success 'setup submodules with nested git dirs' ' git init nested && test_commit -C nested nested && ( @@ -338,9 +338,39 @@ test_expect_success 'git dirs of sibling submodules must not be nested' ' git add .gitmodules thing1 thing2 && test_tick && git commit -m nested - ) && + ) +' + +test_expect_success 'git dirs of sibling submodules must not be nested' ' test_must_fail git clone --recurse-submodules nested clone 2>err && test_grep "is inside git dir" err ' +test_expect_success 'submodule git dir nesting detection must work with parallel cloning' ' + test_must_fail git clone --recurse-submodules --jobs=2 nested clone_parallel 2>err && + cat err && + grep -E "(already exists|is inside git dir|not a git repository)" err && + { + test_path_is_missing .git/modules/hippo/HEAD || + test_path_is_missing .git/modules/hippo/hooks/HEAD + } +' + +test_expect_success 'checkout -f --recurse-submodules must not use a nested gitdir' ' + git clone nested nested_checkout && + ( + cd nested_checkout && + git submodule init && + git submodule update thing1 && + mkdir -p .git/modules/hippo/hooks/refs && + mkdir -p .git/modules/hippo/hooks/objects/info && + echo "../../../../objects" >.git/modules/hippo/hooks/objects/info/alternates && + echo "ref: refs/heads/master" >.git/modules/hippo/hooks/HEAD + ) && + test_must_fail git -C nested_checkout checkout -f --recurse-submodules HEAD 2>err && + cat err && + grep "is inside git dir" err && + test_path_is_missing nested_checkout/thing2/.git +' + test_done diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh index 5a77100..58699f8 100755 --- a/t/t9001-send-email.sh +++ b/t/t9001-send-email.sh @@ -2526,7 +2526,7 @@ test_expect_success $PREREQ 'test forbidSendmailVariables behavior override' ' test_expect_success $PREREQ '--compose handles lowercase headers' ' write_script fake-editor <<-\EOF && - sed "s/^From:.*/from: edited-from@example.com/i" "$1" >"$1.tmp" && + sed "s/^[Ff][Rr][Oo][Mm]:.*/from: edited-from@example.com/" "$1" >"$1.tmp" && mv "$1.tmp" "$1" EOF clean_fake_sendmail && diff --git a/t/t9118-git-svn-funky-branch-names.sh b/t/t9118-git-svn-funky-branch-names.sh index d3261e3..a34fd46 100755 --- a/t/t9118-git-svn-funky-branch-names.sh +++ b/t/t9118-git-svn-funky-branch-names.sh @@ -38,7 +38,7 @@ test_expect_success 'setup svnrepo' ' # SVN 1.7 will truncate "not-a%40{0]" to just "not-a". # Look at what SVN wound up naming the branch and use that. # Be sure to escape the @ if it shows up. -non_reflog=$(svn_cmd ls "$svnrepo/pr ject/branches" | sed -ne '/not-a/ { s/\///; s/@/%40/; p }') +non_reflog=$(svn_cmd ls "$svnrepo/pr ject/branches" | sed -ne '/not-a/ { s/\///; s/@/%40/; p; }') test_expect_success 'test clone with funky branch names' ' git svn clone -s "$svnrepo/pr ject" project && diff --git a/t/t9200-git-cvsexportcommit.sh b/t/t9200-git-cvsexportcommit.sh index a44eabf..3d48421 100755 --- a/t/t9200-git-cvsexportcommit.sh +++ b/t/t9200-git-cvsexportcommit.sh @@ -4,6 +4,7 @@ # test_description='Test export of commits to CVS' +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh if ! test_have_prereq PERL; then diff --git a/t/t9210-scalar.sh b/t/t9210-scalar.sh index 428339e..a41b4fc 100755 --- a/t/t9210-scalar.sh +++ b/t/t9210-scalar.sh @@ -180,6 +180,44 @@ test_expect_success 'scalar reconfigure' ' test true = "$(git -C one/src config core.preloadIndex)" ' +test_expect_success 'scalar reconfigure --all with includeIf.onbranch' ' + repos="two three four" && + for num in $repos + do + git init $num/src && + scalar register $num/src && + git -C $num/src config includeif."onbranch:foo".path something && + git -C $num/src config core.preloadIndex false || return 1 + done && + + scalar reconfigure --all && + + for num in $repos + do + test true = "$(git -C $num/src config core.preloadIndex)" || return 1 + done +' + + test_expect_success 'scalar reconfigure --all with detached HEADs' ' + repos="two three four" && + for num in $repos + do + rm -rf $num/src && + git init $num/src && + scalar register $num/src && + git -C $num/src config core.preloadIndex false && + test_commit -C $num/src initial && + git -C $num/src switch --detach HEAD || return 1 + done && + + scalar reconfigure --all && + + for num in $repos + do + test true = "$(git -C $num/src config core.preloadIndex)" || return 1 + done +' + test_expect_success '`reconfigure -a` removes stale config entries' ' git init stale/src && scalar register stale && diff --git a/t/t9401-git-cvsserver-crlf.sh b/t/t9401-git-cvsserver-crlf.sh index a34805a..a67e6ab 100755 --- a/t/t9401-git-cvsserver-crlf.sh +++ b/t/t9401-git-cvsserver-crlf.sh @@ -12,6 +12,7 @@ repository using cvs CLI client via git-cvsserver server' GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +TEST_PASSES_SANITIZE_LEAK=true . ./test-lib.sh marked_as () { diff --git a/t/t9600-cvsimport.sh b/t/t9600-cvsimport.sh index 5680849..41fcf36 100755 --- a/t/t9600-cvsimport.sh +++ b/t/t9600-cvsimport.sh @@ -4,6 +4,7 @@ test_description='git cvsimport basic tests' GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +TEST_PASSES_SANITIZE_LEAK=true . ./lib-cvs.sh if ! test_have_prereq NOT_ROOT; then diff --git a/t/t9601-cvsimport-vendor-branch.sh b/t/t9601-cvsimport-vendor-branch.sh index 116cddb..e007669 100755 --- a/t/t9601-cvsimport-vendor-branch.sh +++ b/t/t9601-cvsimport-vendor-branch.sh @@ -35,6 +35,7 @@ test_description='git cvsimport handling of vendor branches' GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +TEST_PASSES_SANITIZE_LEAK=true . ./lib-cvs.sh setup_cvs_test_repository t9601 diff --git a/t/t9602-cvsimport-branches-tags.sh b/t/t9602-cvsimport-branches-tags.sh index e5266c9..3768e3b 100755 --- a/t/t9602-cvsimport-branches-tags.sh +++ b/t/t9602-cvsimport-branches-tags.sh @@ -7,6 +7,7 @@ test_description='git cvsimport handling of branches and tags' GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME +TEST_PASSES_SANITIZE_LEAK=true . ./lib-cvs.sh setup_cvs_test_repository t9602 diff --git a/t/t9603-cvsimport-patchsets.sh b/t/t9603-cvsimport-patchsets.sh index 19f38f7..2a387fd 100755 --- a/t/t9603-cvsimport-patchsets.sh +++ b/t/t9603-cvsimport-patchsets.sh @@ -12,6 +12,8 @@ # bug. test_description='git cvsimport testing for correct patchset estimation' + +TEST_PASSES_SANITIZE_LEAK=true . ./lib-cvs.sh setup_cvs_test_repository t9603 diff --git a/t/t9604-cvsimport-timestamps.sh b/t/t9604-cvsimport-timestamps.sh index 2d03259..9cf0685 100755 --- a/t/t9604-cvsimport-timestamps.sh +++ b/t/t9604-cvsimport-timestamps.sh @@ -1,6 +1,8 @@ #!/bin/sh test_description='git cvsimport timestamps' + +TEST_PASSES_SANITIZE_LEAK=true . ./lib-cvs.sh test_lazy_prereq POSIX_TIMEZONE ' diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh index 963f865..932d5ad 100755 --- a/t/t9902-completion.sh +++ b/t/t9902-completion.sh @@ -73,7 +73,7 @@ _get_comp_words_by_ref () print_comp () { local IFS=$'\n' - echo "${COMPREPLY[*]}" > out + printf '%s\n' "${COMPREPLY[*]}" > out } run_completion () @@ -2742,30 +2742,58 @@ do ' done -test_expect_success 'git config - section' ' - test_completion "git config br" <<-\EOF +test_expect_success 'git config subcommand' ' + test_completion "git config " <<-\EOF + edit Z + get Z + list Z + remove-section Z + rename-section Z + set Z + unset Z + EOF +' + +test_expect_success 'git config subcommand options' ' + test_completion "git config get --show-" <<-\EOF + --show-names Z + --show-origin Z + --show-scope Z + EOF +' + +test_expect_success 'git config get' ' + test_when_finished "rm -f cfgfile" && + git config set --file cfgfile foo.bar baz && + test_completion "git config get --file cfgfile foo." <<-\EOF + foo.bar Z + EOF +' + +test_expect_success 'git config set - section' ' + test_completion "git config set br" <<-\EOF branch.Z browser.Z EOF ' -test_expect_success 'git config - section include, includeIf' ' - test_completion "git config inclu" <<-\EOF +test_expect_success 'git config set - section include, includeIf' ' + test_completion "git config set inclu" <<-\EOF include.Z includeIf.Z EOF ' -test_expect_success 'git config - variable name' ' - test_completion "git config log.d" <<-\EOF +test_expect_success 'git config set - variable name' ' + test_completion "git config set log.d" <<-\EOF log.date Z log.decorate Z log.diffMerges Z EOF ' -test_expect_success 'git config - variable name include' ' - test_completion "git config include.p" <<-\EOF +test_expect_success 'git config set - variable name include' ' + test_completion "git config set include.p" <<-\EOF include.path Z EOF ' @@ -2776,8 +2804,8 @@ test_expect_success 'setup for git config submodule tests' ' git submodule add ./sub ' -test_expect_success 'git config - variable name - submodule and __git_compute_first_level_config_vars_for_section' ' - test_completion "git config submodule." <<-\EOF +test_expect_success 'git config set - variable name - submodule and __git_compute_first_level_config_vars_for_section' ' + test_completion "git config set submodule." <<-\EOF submodule.active Z submodule.alternateErrorStrategy Z submodule.alternateLocation Z @@ -2788,8 +2816,8 @@ test_expect_success 'git config - variable name - submodule and __git_compute_fi EOF ' -test_expect_success 'git config - variable name - __git_compute_second_level_config_vars_for_section' ' - test_completion "git config submodule.sub." <<-\EOF +test_expect_success 'git config set - variable name - __git_compute_second_level_config_vars_for_section' ' + test_completion "git config set submodule.sub." <<-\EOF submodule.sub.url Z submodule.sub.update Z submodule.sub.branch Z @@ -2799,8 +2827,8 @@ test_expect_success 'git config - variable name - __git_compute_second_level_con EOF ' -test_expect_success 'git config - value' ' - test_completion "git config color.pager " <<-\EOF +test_expect_success 'git config set - value' ' + test_completion "git config set color.pager " <<-\EOF false Z true Z EOF diff --git a/t/unit-tests/t-strcmp-offset.c b/t/unit-tests/t-strcmp-offset.c new file mode 100644 index 0000000..fe4c270 --- /dev/null +++ b/t/unit-tests/t-strcmp-offset.c @@ -0,0 +1,35 @@ +#include "test-lib.h" +#include "read-cache-ll.h" + +static void check_strcmp_offset(const char *string1, const char *string2, + int expect_result, uintmax_t expect_offset) +{ + size_t offset; + int result = strcmp_offset(string1, string2, &offset); + + /* + * Because different CRTs behave differently, only rely on signs of the + * result values. + */ + result = (result < 0 ? -1 : + result > 0 ? 1 : + 0); + + check_int(result, ==, expect_result); + check_uint((uintmax_t)offset, ==, expect_offset); +} + +#define TEST_STRCMP_OFFSET(string1, string2, expect_result, expect_offset) \ + TEST(check_strcmp_offset(string1, string2, expect_result, \ + expect_offset), \ + "strcmp_offset(%s, %s) works", #string1, #string2) + +int cmd_main(int argc, const char **argv) +{ + TEST_STRCMP_OFFSET("abc", "abc", 0, 3); + TEST_STRCMP_OFFSET("abc", "def", -1, 0); + TEST_STRCMP_OFFSET("abc", "abz", -1, 2); + TEST_STRCMP_OFFSET("abc", "abcdef", -1, 3); + + return test_done(); +} diff --git a/t/unit-tests/t-trailer.c b/t/unit-tests/t-trailer.c new file mode 100644 index 0000000..2ecca35 --- /dev/null +++ b/t/unit-tests/t-trailer.c @@ -0,0 +1,315 @@ +#include "test-lib.h" +#include "trailer.h" + +struct contents { + const char *raw; + const char *key; + const char *val; +}; + +static void t_trailer_iterator(const char *msg, size_t num_expected, + struct contents *contents) +{ + struct trailer_iterator iter; + size_t i = 0; + + trailer_iterator_init(&iter, msg); + while (trailer_iterator_advance(&iter)) { + if (num_expected) { + check_str(iter.raw, contents[i].raw); + check_str(iter.key.buf, contents[i].key); + check_str(iter.val.buf, contents[i].val); + } + i++; + } + trailer_iterator_release(&iter); + + check_uint(i, ==, num_expected); +} + +static void run_t_trailer_iterator(void) +{ + + static struct test_cases { + const char *name; + const char *msg; + size_t num_expected; + struct contents contents[10]; + } tc[] = { + { + "empty input", + "", + 0, + {{0}}, + }, + { + "no newline at beginning", + "Fixes: x\n" + "Acked-by: x\n" + "Reviewed-by: x\n", + 0, + {{0}}, + }, + { + "newline at beginning", + "\n" + "Fixes: x\n" + "Acked-by: x\n" + "Reviewed-by: x\n", + 3, + { + { + .raw = "Fixes: x\n", + .key = "Fixes", + .val = "x", + }, + { + .raw = "Acked-by: x\n", + .key = "Acked-by", + .val = "x", + }, + { + .raw = "Reviewed-by: x\n", + .key = "Reviewed-by", + .val = "x", + }, + { + 0 + }, + }, + }, + { + "without body text", + "subject: foo bar\n" + "\n" + "Fixes: x\n" + "Acked-by: x\n" + "Reviewed-by: x\n", + 3, + { + { + .raw = "Fixes: x\n", + .key = "Fixes", + .val = "x", + }, + { + .raw = "Acked-by: x\n", + .key = "Acked-by", + .val = "x", + }, + { + .raw = "Reviewed-by: x\n", + .key = "Reviewed-by", + .val = "x", + }, + { + 0 + }, + }, + }, + { + "with body text, without divider", + "my subject\n" + "\n" + "my body which is long\n" + "and contains some special\n" + "chars like : = ? !\n" + "hello\n" + "\n" + "Fixes: x\n" + "Acked-by: x\n" + "Reviewed-by: x\n" + "Signed-off-by: x\n", + 4, + { + { + .raw = "Fixes: x\n", + .key = "Fixes", + .val = "x", + }, + { + .raw = "Acked-by: x\n", + .key = "Acked-by", + .val = "x", + }, + { + .raw = "Reviewed-by: x\n", + .key = "Reviewed-by", + .val = "x", + }, + { + .raw = "Signed-off-by: x\n", + .key = "Signed-off-by", + .val = "x", + }, + { + 0 + }, + }, + }, + { + "with body text, without divider (second trailer block)", + "my subject\n" + "\n" + "my body which is long\n" + "and contains some special\n" + "chars like : = ? !\n" + "hello\n" + "\n" + "Fixes: x\n" + "Acked-by: x\n" + "Reviewed-by: x\n" + "Signed-off-by: x\n" + "\n" + /* + * Because this is the last trailer block, it takes + * precedence over the first one encountered above. + */ + "Helped-by: x\n" + "Signed-off-by: x\n", + 2, + { + { + .raw = "Helped-by: x\n", + .key = "Helped-by", + .val = "x", + }, + { + .raw = "Signed-off-by: x\n", + .key = "Signed-off-by", + .val = "x", + }, + { + 0 + }, + }, + }, + { + "with body text, with divider", + "my subject\n" + "\n" + "my body which is long\n" + "and contains some special\n" + "chars like : = ? !\n" + "hello\n" + "\n" + "---\n" + "\n" + /* + * This trailer still counts because the iterator + * always ignores the divider. + */ + "Signed-off-by: x\n", + 1, + { + { + .raw = "Signed-off-by: x\n", + .key = "Signed-off-by", + .val = "x", + }, + { + 0 + }, + }, + }, + { + "with non-trailer lines in trailer block", + "subject: foo bar\n" + "\n" + /* + * Even though this trailer block has a non-trailer line + * in it, it's still a valid trailer block because it's + * at least 25% trailers and is Git-generated (see + * git_generated_prefixes[] in trailer.c). + */ + "not a trailer line\n" + "not a trailer line\n" + "not a trailer line\n" + "Signed-off-by: x\n", + /* + * Even though there is only really 1 real "trailer" + * (Signed-off-by), we still have 4 trailer objects + * because we still want to iterate through the entire + * block. + */ + 4, + { + { + .raw = "not a trailer line\n", + .key = "not a trailer line", + .val = "", + }, + { + .raw = "not a trailer line\n", + .key = "not a trailer line", + .val = "", + }, + { + .raw = "not a trailer line\n", + .key = "not a trailer line", + .val = "", + }, + { + .raw = "Signed-off-by: x\n", + .key = "Signed-off-by", + .val = "x", + }, + { + 0 + }, + }, + }, + { + "with non-trailer lines (one too many) in trailer block", + "subject: foo bar\n" + "\n" + /* + * This block has only 20% trailers, so it's below the + * 25% threshold. + */ + "not a trailer line\n" + "not a trailer line\n" + "not a trailer line\n" + "not a trailer line\n" + "Signed-off-by: x\n", + 0, + {{0}}, + }, + { + "with non-trailer lines (only 1) in trailer block, but no Git-generated trailers", + "subject: foo bar\n" + "\n" + /* + * This block has only 1 non-trailer out of 10 (IOW, 90% + * trailers) but is not considered a trailer block + * because the 25% threshold only applies to cases where + * there was a Git-generated trailer. + */ + "Reviewed-by: x\n" + "Reviewed-by: x\n" + "Reviewed-by: x\n" + "Helped-by: x\n" + "Helped-by: x\n" + "Helped-by: x\n" + "Acked-by: x\n" + "Acked-by: x\n" + "Acked-by: x\n" + "not a trailer line\n", + 0, + {{0}}, + }, + }; + + for (int i = 0; i < sizeof(tc) / sizeof(tc[0]); i++) { + TEST(t_trailer_iterator(tc[i].msg, + tc[i].num_expected, + tc[i].contents), + "%s", tc[i].name); + } +} + +int cmd_main(int argc, const char **argv) +{ + run_t_trailer_iterator(); + return test_done(); +} @@ -91,10 +91,10 @@ struct object *deref_tag(struct repository *r, struct object *o, const char *war return o; } -struct object *deref_tag_noverify(struct object *o) +struct object *deref_tag_noverify(struct repository *r, struct object *o) { while (o && o->type == OBJ_TAG) { - o = parse_object(the_repository, &o->oid); + o = parse_object(r, &o->oid); if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged) o = ((struct tag *)o)->tagged; else @@ -16,7 +16,7 @@ int parse_tag_buffer(struct repository *r, struct tag *item, const void *data, u int parse_tag(struct tag *item); void release_tag_memory(struct tag *t); struct object *deref_tag(struct repository *r, struct object *, const char *, int); -struct object *deref_tag_noverify(struct object *); +struct object *deref_tag_noverify(struct repository *r, struct object *); int gpg_verify_tag(const struct object_id *oid, const char *name_to_report, unsigned flags); struct object_id *get_tagged_oid(struct tag *tag); @@ -11,6 +11,27 @@ * Copyright (c) 2013, 2014 Christian Couder <chriscool@tuxfamily.org> */ +struct trailer_info { + /* + * True if there is a blank line before the location pointed to by + * trailer_block_start. + */ + int blank_line_before_trailer; + + /* + * Offsets to the trailer block start and end positions in the input + * string. If no trailer block is found, these are both set to the + * "true" end of the input (find_end_of_log_message()). + */ + size_t trailer_block_start, trailer_block_end; + + /* + * Array of trailers found. + */ + char **trailers; + size_t trailer_nr; +}; + struct conf_info { char *name; char *key; @@ -952,20 +973,72 @@ static void unfold_value(struct strbuf *val) strbuf_release(&out); } +static struct trailer_info *trailer_info_new(void) +{ + struct trailer_info *info = xcalloc(1, sizeof(*info)); + return info; +} + +static struct trailer_info *trailer_info_get(const struct process_trailer_options *opts, + const char *str) +{ + struct trailer_info *info = trailer_info_new(); + size_t end_of_log_message = 0, trailer_block_start = 0; + struct strbuf **trailer_lines, **ptr; + char **trailer_strings = NULL; + size_t nr = 0, alloc = 0; + char **last = NULL; + + trailer_config_init(); + + end_of_log_message = find_end_of_log_message(str, opts->no_divider); + trailer_block_start = find_trailer_block_start(str, end_of_log_message); + + trailer_lines = strbuf_split_buf(str + trailer_block_start, + end_of_log_message - trailer_block_start, + '\n', + 0); + for (ptr = trailer_lines; *ptr; ptr++) { + if (last && isspace((*ptr)->buf[0])) { + struct strbuf sb = STRBUF_INIT; + strbuf_attach(&sb, *last, strlen(*last), strlen(*last)); + strbuf_addbuf(&sb, *ptr); + *last = strbuf_detach(&sb, NULL); + continue; + } + ALLOC_GROW(trailer_strings, nr + 1, alloc); + trailer_strings[nr] = strbuf_detach(*ptr, NULL); + last = find_separator(trailer_strings[nr], separators) >= 1 + ? &trailer_strings[nr] + : NULL; + nr++; + } + strbuf_list_free(trailer_lines); + + info->blank_line_before_trailer = ends_with_blank_line(str, + trailer_block_start); + info->trailer_block_start = trailer_block_start; + info->trailer_block_end = end_of_log_message; + info->trailers = trailer_strings; + info->trailer_nr = nr; + + return info; +} + /* - * Parse trailers in "str", populating the trailer info and "head" + * Parse trailers in "str", populating the trailer info and "trailer_objects" * linked list structure. */ -void parse_trailers(const struct process_trailer_options *opts, - struct trailer_info *info, - const char *str, - struct list_head *head) +struct trailer_info *parse_trailers(const struct process_trailer_options *opts, + const char *str, + struct list_head *trailer_objects) { + struct trailer_info *info; struct strbuf tok = STRBUF_INIT; struct strbuf val = STRBUF_INIT; size_t i; - trailer_info_get(opts, str, info); + info = trailer_info_get(opts, str); for (i = 0; i < info->trailer_nr; i++) { int separator_pos; @@ -978,17 +1051,19 @@ void parse_trailers(const struct process_trailer_options *opts, separator_pos); if (opts->unfold) unfold_value(&val); - add_trailer_item(head, + add_trailer_item(trailer_objects, strbuf_detach(&tok, NULL), strbuf_detach(&val, NULL)); } else if (!opts->only_trailers) { strbuf_addstr(&val, trailer); strbuf_strip_suffix(&val, "\n"); - add_trailer_item(head, + add_trailer_item(trailer_objects, NULL, strbuf_detach(&val, NULL)); } } + + return info; } void free_trailers(struct list_head *trailers) @@ -1000,48 +1075,19 @@ void free_trailers(struct list_head *trailers) } } -void trailer_info_get(const struct process_trailer_options *opts, - const char *str, - struct trailer_info *info) +size_t trailer_block_start(struct trailer_info *info) { - size_t end_of_log_message = 0, trailer_block_start = 0; - struct strbuf **trailer_lines, **ptr; - char **trailer_strings = NULL; - size_t nr = 0, alloc = 0; - char **last = NULL; - - trailer_config_init(); - - end_of_log_message = find_end_of_log_message(str, opts->no_divider); - trailer_block_start = find_trailer_block_start(str, end_of_log_message); + return info->trailer_block_start; +} - trailer_lines = strbuf_split_buf(str + trailer_block_start, - end_of_log_message - trailer_block_start, - '\n', - 0); - for (ptr = trailer_lines; *ptr; ptr++) { - if (last && isspace((*ptr)->buf[0])) { - struct strbuf sb = STRBUF_INIT; - strbuf_attach(&sb, *last, strlen(*last), strlen(*last)); - strbuf_addbuf(&sb, *ptr); - *last = strbuf_detach(&sb, NULL); - continue; - } - ALLOC_GROW(trailer_strings, nr + 1, alloc); - trailer_strings[nr] = strbuf_detach(*ptr, NULL); - last = find_separator(trailer_strings[nr], separators) >= 1 - ? &trailer_strings[nr] - : NULL; - nr++; - } - strbuf_list_free(trailer_lines); +size_t trailer_block_end(struct trailer_info *info) +{ + return info->trailer_block_end; +} - info->blank_line_before_trailer = ends_with_blank_line(str, - trailer_block_start); - info->trailer_block_start = trailer_block_start; - info->trailer_block_end = end_of_log_message; - info->trailers = trailer_strings; - info->trailer_nr = nr; +int blank_line_before_trailer_block(struct trailer_info *info) +{ + return info->blank_line_before_trailer; } void trailer_info_release(struct trailer_info *info) @@ -1050,6 +1096,7 @@ void trailer_info_release(struct trailer_info *info) for (i = 0; i < info->trailer_nr; i++) free(info->trailers[i]); free(info->trailers); + free(info); } void format_trailers(const struct process_trailer_options *opts, @@ -1117,21 +1164,19 @@ void format_trailers_from_commit(const struct process_trailer_options *opts, struct strbuf *out) { LIST_HEAD(trailer_objects); - struct trailer_info info; - - parse_trailers(opts, &info, msg, &trailer_objects); + struct trailer_info *info = parse_trailers(opts, msg, &trailer_objects); /* If we want the whole block untouched, we can take the fast path. */ if (!opts->only_trailers && !opts->unfold && !opts->filter && !opts->separator && !opts->key_only && !opts->value_only && !opts->key_value_separator) { - strbuf_add(out, msg + info.trailer_block_start, - info.trailer_block_end - info.trailer_block_start); + strbuf_add(out, msg + info->trailer_block_start, + info->trailer_block_end - info->trailer_block_start); } else format_trailers(opts, &trailer_objects, out); free_trailers(&trailer_objects); - trailer_info_release(&info); + trailer_info_release(info); } void trailer_iterator_init(struct trailer_iterator *iter, const char *msg) @@ -1140,23 +1185,21 @@ void trailer_iterator_init(struct trailer_iterator *iter, const char *msg) strbuf_init(&iter->key, 0); strbuf_init(&iter->val, 0); opts.no_divider = 1; - trailer_info_get(&opts, msg, &iter->internal.info); + iter->internal.info = trailer_info_get(&opts, msg); iter->internal.cur = 0; } int trailer_iterator_advance(struct trailer_iterator *iter) { - while (iter->internal.cur < iter->internal.info.trailer_nr) { - char *trailer = iter->internal.info.trailers[iter->internal.cur++]; - int separator_pos = find_separator(trailer, separators); - - if (separator_pos < 1) - continue; /* not a real trailer */ + if (iter->internal.cur < iter->internal.info->trailer_nr) { + char *line = iter->internal.info->trailers[iter->internal.cur++]; + int separator_pos = find_separator(line, separators); + iter->raw = line; strbuf_reset(&iter->key); strbuf_reset(&iter->val); parse_trailer(&iter->key, &iter->val, NULL, - trailer, separator_pos); + line, separator_pos); /* Always unfold values during iteration. */ unfold_value(&iter->val); return 1; @@ -1166,7 +1209,19 @@ int trailer_iterator_advance(struct trailer_iterator *iter) void trailer_iterator_release(struct trailer_iterator *iter) { - trailer_info_release(&iter->internal.info); + trailer_info_release(iter->internal.info); strbuf_release(&iter->val); strbuf_release(&iter->key); } + +int amend_file_with_trailers(const char *path, const struct strvec *trailer_args) +{ + struct child_process run_trailer = CHILD_PROCESS_INIT; + + run_trailer.git_cmd = 1; + strvec_pushl(&run_trailer.args, "interpret-trailers", + "--in-place", "--no-divider", + path, NULL); + strvec_pushv(&run_trailer.args, trailer_args->v); + return run_command(&run_trailer); +} @@ -4,6 +4,9 @@ #include "list.h" #include "strbuf.h" +struct trailer_info; +struct strvec; + enum trailer_where { WHERE_DEFAULT, WHERE_END, @@ -29,27 +32,6 @@ int trailer_set_where(enum trailer_where *item, const char *value); int trailer_set_if_exists(enum trailer_if_exists *item, const char *value); int trailer_set_if_missing(enum trailer_if_missing *item, const char *value); -struct trailer_info { - /* - * True if there is a blank line before the location pointed to by - * trailer_block_start. - */ - int blank_line_before_trailer; - - /* - * Offsets to the trailer block start and end positions in the input - * string. If no trailer block is found, these are both set to the - * "true" end of the input (find_end_of_log_message()). - */ - size_t trailer_block_start, trailer_block_end; - - /* - * Array of trailers found. - */ - char **trailers; - size_t trailer_nr; -}; - /* * A list that represents newly-added trailers, such as those provided * with the --trailer command line option of git-interpret-trailers. @@ -89,15 +71,63 @@ void parse_trailers_from_command_line_args(struct list_head *arg_head, void process_trailers_lists(struct list_head *head, struct list_head *arg_head); -void parse_trailers(const struct process_trailer_options *, - struct trailer_info *, - const char *str, - struct list_head *head); +/* + * Given some input string "str", return a pointer to an opaque trailer_info + * structure. Also populate the trailer_objects list with parsed trailer + * objects. Internally this calls trailer_info_get() to get the opaque pointer, + * but does some extra work to populate the trailer_objects linked list. + * + * The opaque trailer_info pointer can be used to check the position of the + * trailer block as offsets relative to the beginning of "str" in + * trailer_block_start() and trailer_block_end(). + * blank_line_before_trailer_block() returns 1 if there is a blank line just + * before the trailer block. All of these functions are useful for preserving + * the input before and after the trailer block, if we were to write out the + * original input (but with the trailer block itself modified); see + * builtin/interpret-trailers.c for an example. + * + * For iterating through the parsed trailer block (if you don't care about the + * position of the trailer block itself in the context of the larger string text + * from which it was parsed), please see trailer_iterator_init() which uses the + * trailer_info struct internally. + * + * Lastly, callers should call trailer_info_release() when they are done using + * the opaque pointer. + * + * NOTE: Callers should treat both trailer_info and trailer_objects as + * read-only items, because there is some overlap between the two (trailer_info + * has "char **trailers" string array, and trailer_objects will have the same + * data but as a linked list of trailer_item objects). This API does not perform + * any synchronization between the two. In the future we should be able to + * reduce the duplication and use just the linked list. + */ +struct trailer_info *parse_trailers(const struct process_trailer_options *, + const char *str, + struct list_head *trailer_objects); + +/* + * Return the offset of the start of the trailer block. That is, 0 is the start + * of the input ("str" in parse_trailers()) and some other positive number + * indicates how many bytes we have to skip over before we get to the beginning + * of the trailer block. + */ +size_t trailer_block_start(struct trailer_info *); + +/* + * Return the end of the trailer block, again relative to the start of the + * input. + */ +size_t trailer_block_end(struct trailer_info *); -void trailer_info_get(const struct process_trailer_options *, - const char *str, - struct trailer_info *); +/* + * Return 1 if the trailer block had an extra newline (blank line) just before + * it. + */ +int blank_line_before_trailer_block(struct trailer_info *); +/* + * Free trailer_info struct. + */ void trailer_info_release(struct trailer_info *info); void trailer_config_init(void); @@ -125,12 +155,19 @@ void format_trailers_from_commit(const struct process_trailer_options *, * trailer_iterator_release(&iter); */ struct trailer_iterator { + /* + * Raw line (e.g., "foo: bar baz") before being parsed as a trailer + * key/val pair as part of a trailer block (as the "key" and "val" + * fields below). If a line fails to parse as a trailer, then the "key" + * will be the entire line and "val" will be the empty string. + */ + const char *raw; struct strbuf key; struct strbuf val; /* private */ struct { - struct trailer_info info; + struct trailer_info *info; size_t cur; } internal; }; @@ -158,4 +195,11 @@ int trailer_iterator_advance(struct trailer_iterator *iter); */ void trailer_iterator_release(struct trailer_iterator *iter); +/* + * Augment a file to add trailers to it by running git-interpret-trailers. + * This calls run_command() and its return value is the same (i.e. 0 for + * success, various non-zero for other errors). See run-command.h. + */ +int amend_file_with_trailers(const char *path, const struct strvec *trailer_args); + #endif /* TRAILER_H */ diff --git a/transport-helper.c b/transport-helper.c index 8d284b2..780fcaf 100644 --- a/transport-helper.c +++ b/transport-helper.c @@ -551,7 +551,7 @@ static int fetch_with_import(struct transport *transport, else private = xstrdup(name); if (private) { - if (read_ref(private, &posn->old_oid) < 0) + if (refs_read_ref(get_main_ref_store(the_repository), private, &posn->old_oid) < 0) die(_("could not read ref %s"), private); free(private); } @@ -923,8 +923,10 @@ static int push_update_refs_status(struct helper_data *data, private = apply_refspecs(&data->rs, ref->name); if (!private) continue; - update_ref("update by helper", private, &(ref->new_oid), - NULL, 0, 0); + refs_update_ref(get_main_ref_store(the_repository), + "update by helper", private, + &(ref->new_oid), + NULL, 0, 0); free(private); } else { for (report = ref->report; report; report = report->next) { @@ -934,11 +936,12 @@ static int push_update_refs_status(struct helper_data *data, : ref->name); if (!private) continue; - update_ref("update by helper", private, - report->new_oid - ? report->new_oid - : &(ref->new_oid), - NULL, 0, 0); + refs_update_ref(get_main_ref_store(the_repository), + "update by helper", private, + report->new_oid + ? report->new_oid + : &(ref->new_oid), + NULL, 0, 0); free(private); } } @@ -1105,9 +1108,11 @@ static int push_refs_with_export(struct transport *transport, int flag; /* Follow symbolic refs (mainly for HEAD). */ - name = resolve_ref_unsafe(ref->peer_ref->name, - RESOLVE_REF_READING, - &oid, &flag); + name = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + ref->peer_ref->name, + RESOLVE_REF_READING, + &oid, + &flag); if (!name || !(flag & REF_ISSYMREF)) name = ref->peer_ref->name; @@ -1252,7 +1257,7 @@ static struct ref *get_refs_list_using_list(struct transport *transport, if (eon) { if (has_attribute(eon + 1, "unchanged")) { (*tail)->status |= REF_STATUS_UPTODATE; - if (read_ref((*tail)->name, &(*tail)->old_oid) < 0) + if (refs_read_ref(get_main_ref_store(the_repository), (*tail)->name, &(*tail)->old_oid) < 0) die(_("could not read ref %s"), (*tail)->name); } diff --git a/transport.c b/transport.c index df518ea..0ad04b7 100644 --- a/transport.c +++ b/transport.c @@ -100,8 +100,9 @@ static void set_upstreams(struct transport *transport, struct ref *refs, /* Follow symbolic refs (mainly for HEAD). */ localname = ref->peer_ref->name; remotename = ref->name; - tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING, - NULL, &flag); + tmp = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + localname, RESOLVE_REF_READING, + NULL, &flag); if (tmp && flag & REF_ISSYMREF && starts_with(tmp, "refs/heads/")) localname = tmp; @@ -543,10 +544,12 @@ static void update_one_tracking_ref(struct remote *remote, char *refname, if (verbose) fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst); if (deletion) - delete_ref(NULL, rs.dst, NULL, 0); + refs_delete_ref(get_main_ref_store(the_repository), + NULL, rs.dst, NULL, 0); else - update_ref("update by push", rs.dst, new_oid, - NULL, 0, 0); + refs_update_ref(get_main_ref_store(the_repository), + "update by push", rs.dst, new_oid, + NULL, 0, 0); free(rs.dst); } } @@ -814,7 +817,8 @@ void transport_print_push_status(const char *dest, struct ref *refs, if (transport_color_config() < 0) warning(_("could not parse transport.color.* config")); - head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL); + head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD", + RESOLVE_REF_READING, NULL, NULL); if (verbose) { for (ref = refs; ref; ref = ref->next) diff --git a/unpack-trees.c b/unpack-trees.c index c2b20b8..304ea2e 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -2318,7 +2318,8 @@ static int verify_clean_subdirectory(const struct cache_entry *ce, if (S_ISGITLINK(ce->ce_mode)) { struct object_id oid; - int sub_head = resolve_gitlink_ref(ce->name, "HEAD", &oid); + int sub_head = repo_resolve_gitlink_ref(the_repository, ce->name, + "HEAD", &oid); /* * If we are not going to update the submodule, then * we don't care. diff --git a/upload-pack.c b/upload-pack.c index 902144b..bbfb04c 100644 --- a/upload-pack.c +++ b/upload-pack.c @@ -618,7 +618,8 @@ static void for_each_namespaced_ref_1(each_ref_fn fn, if (allow_hidden_refs(data->allow_uor)) excludes = hidden_refs_to_excludes(&data->hidden_refs); - for_each_namespaced_ref(excludes, fn, data); + refs_for_each_namespaced_ref(get_main_ref_store(the_repository), + excludes, fn, data); } @@ -873,7 +874,8 @@ static void deepen(struct upload_pack_data *data, int depth) * Checking for reachable shallows requires that our refs be * marked with OUR_REF. */ - head_ref_namespaced(check_ref, data); + refs_head_ref_namespaced(get_main_ref_store(the_repository), + check_ref, data); for_each_namespaced_ref_1(check_ref, data); get_reachable_list(data, &reachable_shallows); @@ -1267,7 +1269,7 @@ static void write_v0_ref(struct upload_pack_data *data, packet_fwrite_fmt(stdout, "%s %s\n", oid_to_hex(oid), refname_nons); } capabilities = NULL; - if (!peel_iterated_oid(oid, &peeled)) + if (!peel_iterated_oid(the_repository, oid, &peeled)) packet_fwrite_fmt(stdout, "%s %s^{}\n", oid_to_hex(&peeled), refname_nons); return; } @@ -1288,7 +1290,8 @@ static int find_symref(const char *refname, if ((flag & REF_ISSYMREF) == 0) return 0; - symref_target = resolve_ref_unsafe(refname, 0, NULL, &flag); + symref_target = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), + refname, 0, NULL, &flag); if (!symref_target || (flag & REF_ISSYMREF) == 0) die("'%s' is a symref but it is not?", refname); item = string_list_append(cb_data, strip_namespace(refname)); @@ -1413,13 +1416,15 @@ void upload_pack(const int advertise_refs, const int stateless_rpc, if (data.timeout) data.daemon_mode = 1; - head_ref_namespaced(find_symref, &data.symref); + refs_head_ref_namespaced(get_main_ref_store(the_repository), + find_symref, &data.symref); if (advertise_refs || !data.stateless_rpc) { reset_timeout(data.timeout); if (advertise_refs) data.no_done = 1; - head_ref_namespaced(send_ref, &data); + refs_head_ref_namespaced(get_main_ref_store(the_repository), + send_ref, &data); for_each_namespaced_ref_1(send_ref, &data); if (!data.sent_capabilities) { const char *refname = "capabilities^{}"; @@ -1433,7 +1438,8 @@ void upload_pack(const int advertise_refs, const int stateless_rpc, advertise_shallow_grafts(1); packet_flush(1); } else { - head_ref_namespaced(check_ref, &data); + refs_head_ref_namespaced(get_main_ref_store(the_repository), + check_ref, &data); for_each_namespaced_ref_1(check_ref, &data); } @@ -1511,7 +1517,7 @@ static int parse_want_ref(struct packet_writer *writer, const char *line, strbuf_addf(&refname, "%s%s", get_git_namespace(), refname_nons); if (ref_is_hidden(refname_nons, refname.buf, hidden_refs) || - read_ref(refname.buf, &oid)) { + refs_read_ref(get_main_ref_store(the_repository), refname.buf, &oid)) { packet_writer_error(writer, "unknown ref %s", refname_nons); die("unknown ref %s", refname_nons); } @@ -286,7 +286,8 @@ int walker_fetch(struct walker *walker, int targets, char **target, ALLOC_ARRAY(oids, targets); if (write_ref) { - transaction = ref_transaction_begin(&err); + transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), + &err); if (!transaction) { error("%s", err.buf); goto done; @@ -294,7 +295,8 @@ int walker_fetch(struct walker *walker, int targets, char **target, } if (!walker->get_recover) { - for_each_ref(mark_complete, NULL); + refs_for_each_ref(get_main_ref_store(the_repository), + mark_complete, NULL); commit_list_sort_by_date(&complete); } @@ -324,7 +326,7 @@ int walker_fetch(struct walker *walker, int targets, char **target, strbuf_reset(&refname); strbuf_addf(&refname, "refs/%s", write_ref[i]); if (ref_transaction_update(transaction, refname.buf, - oids + i, NULL, 0, + oids + i, NULL, NULL, NULL, 0, msg ? msg : "fetch (unknown)", &err)) { error("%s", err.buf); @@ -65,6 +65,7 @@ static struct worktree *get_main_worktree(int skip_reading_head) strbuf_strip_suffix(&worktree_path, "/.git"); CALLOC_ARRAY(worktree, 1); + worktree->repo = the_repository; worktree->path = strbuf_detach(&worktree_path, NULL); /* * NEEDSWORK: If this function is called from a secondary worktree and @@ -98,6 +99,7 @@ struct worktree *get_linked_worktree(const char *id, strbuf_strip_suffix(&worktree_path, "/.git"); CALLOC_ARRAY(worktree, 1); + worktree->repo = the_repository; worktree->path = strbuf_detach(&worktree_path, NULL); worktree->id = xstrdup(id); if (!skip_reading_head) @@ -6,6 +6,8 @@ struct strbuf; struct worktree { + /* The repository this worktree belongs to. */ + struct repository *repo; char *path; char *id; char *head_ref; /* NULL if HEAD is broken or detached */ diff --git a/wt-status.c b/wt-status.c index bdfc23e..ff4be07 100644 --- a/wt-status.c +++ b/wt-status.c @@ -145,7 +145,8 @@ void wt_status_prepare(struct repository *r, struct wt_status *s) s->show_untracked_files = SHOW_NORMAL_UNTRACKED_FILES; s->use_color = -1; s->relative_paths = 1; - s->branch = resolve_refdup("HEAD", 0, NULL, NULL); + s->branch = refs_resolve_refdup(get_main_ref_store(the_repository), + "HEAD", 0, NULL, NULL); s->reference = "HEAD"; s->fp = stdout; s->index_file = get_index_file(); @@ -976,7 +977,8 @@ static int stash_count_refs(struct object_id *ooid UNUSED, static int count_stash_entries(void) { int n = 0; - for_each_reflog_ent("refs/stash", stash_count_refs, &n); + refs_for_each_reflog_ent(get_main_ref_store(the_repository), + "refs/stash", stash_count_refs, &n); return n; } @@ -1304,10 +1306,10 @@ static int split_commit_in_progress(struct wt_status *s) !s->branch || strcmp(s->branch, "HEAD")) return 0; - if (read_ref_full("HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, - &head_oid, &head_flags) || - read_ref_full("ORIG_HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, - &orig_head_oid, &orig_head_flags)) + if (refs_read_ref_full(get_main_ref_store(the_repository), "HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, + &head_oid, &head_flags) || + refs_read_ref_full(get_main_ref_store(the_repository), "ORIG_HEAD", RESOLVE_REF_READING | RESOLVE_REF_NO_RECURSE, + &orig_head_oid, &orig_head_flags)) return 0; if (head_flags & REF_ISSYMREF || orig_head_flags & REF_ISSYMREF) return 0; @@ -1679,7 +1681,7 @@ static void wt_status_get_detached_from(struct repository *r, char *ref = NULL; strbuf_init(&cb.buf, 0); - if (for_each_reflog_ent_reverse("HEAD", grab_1st_switch, &cb) <= 0) { + if (refs_for_each_reflog_ent_reverse(get_main_ref_store(the_repository), "HEAD", grab_1st_switch, &cb) <= 0) { strbuf_release(&cb.buf); return; } @@ -2087,7 +2089,8 @@ static void wt_shortstatus_print_tracking(struct wt_status *s) upstream_is_gone = 1; } - short_base = shorten_unambiguous_ref(base, 0); + short_base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), + base, 0); color_fprintf(s->fp, header_color, "..."); color_fprintf(s->fp, branch_color_remote, "%s", short_base); free(short_base); @@ -2220,7 +2223,8 @@ static void wt_porcelain_v2_print_tracking(struct wt_status *s) ab_info = stat_tracking_info(branch, &nr_ahead, &nr_behind, &base, 0, s->ahead_behind_flags); if (base) { - base = shorten_unambiguous_ref(base, 0); + base = refs_shorten_unambiguous_ref(get_main_ref_store(the_repository), + base, 0); fprintf(s->fp, "# branch.upstream %s%c", base, eol); free((char *)base); |