Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetchgit: shallow clone for submodules #254172

Merged
merged 2 commits into from
Nov 9, 2023

Conversation

pineapplehunter
Copy link
Contributor

Description of changes

I enabled shallow cloning for submodules.
It will not shallow clone if deepClone or leaveDotGit is true. I belaeve you don't have to deep clone when only leaveDotGit is true, but I thought it may change the hash, so I put it there. If it's not an issue, I'm happy to remove this restriction.
I tested with a few packages that have a submodule and it seemd to work without the hash changing. If there is a better way of testing I would like to know.

This should help for git repositorys with a large submodule (in my case the submodule was LLVM).

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 23.11 Release Notes (or backporting 23.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Copy link
Member

@Artturin Artturin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a few tests to fetchgit/tests.nix (runnable with nix build -f . "tests.fetchgit"), with hashes from before this change.

@pineapplehunter
Copy link
Contributor Author

Thanks for the review!
I think I should add a test with a submodule, but the repo used in the test (nix) does not have a submodule. Is there a good repo with a submodule that I can use? Or should I just choose a random repo?

@Artturin
Copy link
Member

Pick a random repo

@pineapplehunter
Copy link
Contributor Author

I think I have found that using fetchSubmodules and leaveDotGit or deepClone together, it will cause an error. The error message is like below.

...
fatal: not a git repository (or any parent up to mount point /)

looking at the script, I can see that it is trying to change directory into a .git in the submodule, which is a file, making it fail.

Looking through nixpkgs, I can only see 1 package which has both options enabled (and it is gated by NIXPKGS_ALLOW_INSECURE=1). Which means this is not a big issue now but worth debugging.

I will update this PR with some test cases to reproduce the results, and change this to a Draft for now.

@pineapplehunter
Copy link
Contributor Author

The issue seems to be leaveDotGit. It can be reproduced like this.

$ nix-prefetch-git --url https://github.com/pineapplehunter/nix-test-repo-with-submodule --rev 26473335b84ead88ee0a3b649b1c7fa4a91cfd4a --fetch-submodules --leave-dotGit
Initialized empty Git repository in /tmp/git-checkout-tmp-dohXPkOr/nix-test-repo-with-submodule-2647333/.git/
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 4 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 416 bytes | 416.00 KiB/s, done.
From https://github.com/pineapplehunter/nix-test-repo-with-submodule
 * branch            HEAD       -> FETCH_HEAD
Switched to a new branch 'fetchgit'
Submodule 'nix-test-repo-submodule' (https://github.com/pineapplehunter/nix-test-repo-submodule.git) registered for path 'nix-test-repo-submodule'
Cloning into '/tmp/git-checkout-tmp-dohXPkOr/nix-test-repo-with-submodule-2647333/nix-test-repo-submodule'...
Submodule path 'nix-test-repo-submodule': checked out '3223dfc5e18f5c15e0cb26ea7bdf5f12d215619b'
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), done.
Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Enumerating objects: 4, done.
Nothing new to pack.
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

@pineapplehunter
Copy link
Contributor Author

@Artturin
Okay, I think I figured it out.
This should all work now, but I had to change some parts of make_determinisitic_repo in nix-prefetch-git.
Is there a easy way to check if this breaks other packages?

Comment on lines 333 to 340
if [ -d "$gitdir" ]; then
# if `.git` is a directory (in the root of the repo),
# make the content of it deterministic
make_deterministic_repo "$(readlink -f "$gitdir")"
else
# if `.git` is a file (in a submodule),
# get the path of the git directory by reading the file and make it deterministic
make_deterministic_repo "$(dirname $gitdir)/$(cat $gitdir | awk '{ print $2 }')"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

981754a

Needs rebase

Comment on lines 210 to 211
[[ -z "$deepClone" ]] && [[ -z "$leaveDotGit" ]] && \
clean_git submodule update --init --recursive -j ${NIX_BUILD_CORES:-1} --depth 1 || \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[[ -z "$deepClone" ]] && [[ -z "$leaveDotGit" ]] && \
clean_git submodule update --init --recursive -j ${NIX_BUILD_CORES:-1} --depth 1 || \
# leaveDotGit exception is because shallow cloning will change hashes
[[ -z "$deepClone" ]] && [[ -z "$leaveDotGit" ]] && \
clean_git submodule update --init --recursive -j ${NIX_BUILD_CORES:-1} --depth 1 || \

@pineapplehunter pineapplehunter force-pushed the fetchgit-shallow branch 2 times, most recently from 4f9a1c2 to e2fa178 Compare November 8, 2023 23:58
Co-authored-by: Artturi <Artturin@artturin.com>
Copy link
Member

@Artturin Artturin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests pass before and after the fetchgit change

@Artturin Artturin merged commit d6e1c7c into NixOS:master Nov 9, 2023
18 checks passed
@Artturin
Copy link
Member

Artturin commented Nov 9, 2023

Thanks

@cafkafk cafkafk mentioned this pull request Nov 9, 2023
13 tasks
nyabinary pushed a commit to nyabinary/nixpkgs that referenced this pull request Nov 14, 2023
@pineapplehunter pineapplehunter deleted the fetchgit-shallow branch March 6, 2024 09:44
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/cant-build-solvespace-from-scratch-fetching-git-source-fails/46536/5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants