-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Cache submodule into git db #16246
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
base: master
Are you sure you want to change the base?
Cache submodule into git db #16246
Conversation
|
r? @weihanglo rustbot has assigned @weihanglo. Use |
|
It would help reviewers if you break this down into atomic commits. For example, by making a commit for extracting See https://doc.crates.io/contrib/process/working-on-cargo.html#submitting-a-pull-request |
This comment has been minimized.
This comment has been minimized.
70350b1 to
ffd78e6
Compare
This comment has been minimized.
This comment has been minimized.
|
Done, the "extract code into update_db" part is a single commit now. I think the remains part needs to be in the same commit |
src/cargo/sources/git/utils.rs
Outdated
| .with_context(|| { | ||
| let name = child.name().unwrap_or(""); | ||
| format!("failed to fetch submodule `{name}` from {child_remote_url}",) | ||
| })?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like we should have this also on update_db and copy_to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried and the testsuite failed. Should I also adjust testsuite more? I do try to keep it unchanged as much as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depends on the failure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about put it only after db.copy_to? I tested and it do pass the testsuite without further modification. The original fetch was replaced by (gitsource create, update_db and db.copy_to), so put the error context to after db.copy_to seem enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aeda351
Done, and adjust the testsuite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See an alternative in #16246 (comment)
src/cargo/sources/git/utils.rs
Outdated
| SourceId::from_url(&format!("git+{child_remote_url}#{head}"))?, | ||
| gctx, | ||
| RemoteKind::GitDependency, | ||
| ) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What impact does this have on our progress bars?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly the same? The old fetch will have a single progress bar, which now changes into a progress bar for update_db and a brief progress bar after for db.copy. This progress bars behavior should be the same as when update git source. Most of the time I don't even notice the brief one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ea4eb86 should fix when submodule database exist but still print "updating submodule"
src/cargo/sources/git/utils.rs
Outdated
| db.copy_to(actual_rev, &repo.path(), gctx)?; | ||
|
|
||
| let obj = repo.find_object(head, None)?; | ||
| reset(&repo, &obj, gctx)?; | ||
| update_submodules(&repo, gctx, &child_remote_url) | ||
| reset(&repo, &obj, gctx) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be more proper to copy_to after reset?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I not wrong, the original fetch create checkout?/source at the submodules path. update_db only create git db at CARGO_HOME/git/db, not at the checkout submodules path, so db.copy_to need to run first before we can git reset.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
d4d3e6a
I break db.copy_to into small part, the recursive update_submodules part now keep at the original place
This comment has been minimized.
This comment has been minimized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the contribution!
We should have tests for this new caching behavior, especially for nested submodules. See https://doc.crates.io/contrib/process/working-on-cargo.html#making-a-change for how, and also mind the atomic commit pattern (adding test first capturing the existing behavior, and the fix commit showing behavior change through test diffs)
src/cargo/sources/git/source.rs
Outdated
| Ok(()) | ||
| } | ||
|
|
||
| pub(crate) fn update_db(&self) -> CargoResult<(GitDatabase, git2::Oid)> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update_db is an ambiguous name. Would love to see a doc comment of this with a clearer. A random suggestion:
| pub(crate) fn update_db(&self) -> CargoResult<(GitDatabase, git2::Oid)> { | |
| /// Ensures a local [`GitDatabase`] exists for this source, | |
| /// and returns it with the resolved revision to use. | |
| /// | |
| /// This won't fetch anything if the required revision is | |
| /// already available locally. | |
| pub(crate) fn ensure_db(&self) -> CargoResult<(GitDatabase, git2::Oid)> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c6c0f76 will fetch_db work?
src/cargo/sources/git/utils.rs
Outdated
| &child_remote_url, | ||
| &reference, | ||
| let mut source = GitSource::new( | ||
| SourceId::from_url(&format!("git+{child_remote_url}#{head}"))?, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of constructing URL manually, we have a SourceId::for_git for the purpose.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a474768, I notice the GitSource created by for_git always has precise=None, result to locked_rev being Deferred, so fetch_db always try to fetch if online. Is it appropriate to use with_git_precise for this?
src/cargo/sources/git/utils.rs
Outdated
|
|
||
| let obj = repo.find_object(head, None)?; | ||
| reset(&repo, &obj, gctx) | ||
| reset(&repo, &obj, gctx)?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we still need this reset, given GitCheckout::clone_into already reset it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
26ffef4 Fixed
src/cargo/sources/git/utils.rs
Outdated
| let name = child.name().unwrap_or(""); | ||
| format!("failed to fetch submodule `{name}` from {child_remote_url}",) | ||
| })?; | ||
| guard.mark_ok()?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The checkout is marked ready then we recurse into submodules. I feel like that means the checkout is not ready yet.
Would this situation happens? _User cancels the operation before recursing to nested submodules. They later they dependency on this submodule directly and Cargo assumes it is fresh so never recurse into nested submodules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I overlook when inline db.copy_to, my bad. But now the code almost the same as db.copy_to, with db.copy has additional check for freshness, should I just use db.copy_to instead for reduce code dups?
src/cargo/sources/git/utils.rs
Outdated
| .with_context(|| { | ||
| let name = child.name().unwrap_or(""); | ||
| format!("failed to fetch submodule `{name}` from {child_remote_url}",) | ||
| })?; | ||
| source.set_quiet(quiet); | ||
|
|
||
| let (db, actual_rev) = source.update_db(true)?; | ||
| let (db, actual_rev) = source.update_db(true).with_context(|| { | ||
| let name = child.name().unwrap_or(""); | ||
| format!("failed to fetch submodule `{name}` from {child_remote_url}",) | ||
| })?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of this, I wonder if we should consolidate this in the error context in fn update_submodules()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
26ffef4 There already exists a "failed to update submodule" error context in the fn update_submodules, so I think this fetch error context should only apply to fetch_db?
src/cargo/sources/git/utils.rs
Outdated
| })?; | ||
|
|
||
| // Clone and reset to the head commmit | ||
| // Clone and reset to the head commit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are open to rebasing/reorganizing your commit history throughout the iterations of a pull request. And personally I think this kind of commit should be squashed into wherever made this typo. That would help get a more understandable/traceable git commit history
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[not specific to this file]
[not hard blockers]
I noticed that in the commit history we have Merge branch 'master' of https://…. We encourage rebasing and having a well-structured git history for review/debugging/tracing purpose.
Also, chore: … were used in some commits, but chore should have not production code change (see https://stackoverflow.com/a/26944812). IMO they should either be refactor or fix.
(Some may be better being squashed into the entire fix ffd78e6, as those intermediates commits minimally help future review and code tracing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed, I won't merge unless needed
|
@rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
fetch each submodules using git database then checkout using the database
8705c44 to
a474768
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
Sorry for the late response, I force-pushed the commits. About new tests, if I am not mistaking, it should be only about the git db cache, not the correctness of git repo checkouts, as it should be already covered? Currently I can't find api for manipulating the cached git db, or example about testing the git cache db |
What does this PR try to resolve?
My attempt to continue #10279.
This:
Fixes #7987.
How to test and review this PR?
I tested using the original pull method:
and confirmed that the time to do the second update is negligible.
Also test if it can fetch submodule offline using the downloaded git db