Repository navigation
[Backend] feat(git): software-core::git -- Git feature to handle ADJ updates #654
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
Open
pminmic
wants to merge
14
commits into
new-backend
Choose a base branch
from
backend/git
base: new-backend
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
fa9f338
fix: change rust version and add gix
pminmic 9bfceab
feat(backend, git): implementation of ADJ update handling
pminmic 58d320b
docs(backend, git): README
pminmic db77823
docs(backend, git): add some more comments
pminmic 15e7cff
merge(backend, git): new-backend into backend/git to be able to pull …
pminmic 71ccb29
feat(backend, git): separate module in submodules and propagate GitEr…
pminmic f8f04b0
docs(backend, git): update README
pminmic a33f650
update(backend, git): update cargo packages
pminmic 149be35
fix(backend, git): fix implementation of list_remote_branches and bra…
pminmic cda8fb6
docs(backend, git): small changes in README
pminmic 2cf2ee1
Merge branch 'new-backend' into backend/git
pminmic f5821af
refactor(backend, git): separete errors in specific file
pminmic 791cad7
feat(backend, adj): implement auto_adj() and create new LoadError for it
pminmic ec14d41
Merge branch 'new-backend' into backend/git
pminmic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,23 +2,35 @@ | |
| //! [`crate::model::PodData`], plus backend-session metadata not modeled as | ||
| //! vehicle data ([`crate::adj::AdjInfo`]). | ||
| //! | ||
| //! Loading (and if `git` feature is enabled also downloading) is not specific to `station`: a sniffer, a | ||
| //! simulator, or any other tool that reads an ADJ needs exactly the same | ||
| //! parsing. Scope for this iteration: no `sockets.json`, no | ||
| //! [`load_from_dir`] loads an existing ADJ directory. With the `git` feature | ||
| //! enabled, `auto_adj` updates the ADJ repository before loading its data | ||
| //! and returns the checked-out commit hash. | ||
| //! | ||
| //! Loading is not specific to `station`: a sniffer, a simulator, or any | ||
| //! other tool that reads an ADJ needs exactly the same parsing. | ||
| //! | ||
| //! Scope for this iteration: no `sockets.json`, no | ||
| //! `period`/`period_type`/`socket` packet fields (matches | ||
| //! [`crate::model::PacketDef`]'s own omission), and no schema validation | ||
| //! (`adj::validate` is a separate, deliberately out-of-scope concern for | ||
| //! now) — this module only surfaces structural errors it cannot avoid, | ||
| //! now). This module only surfaces structural errors it cannot avoid, | ||
| //! e.g. a packet referencing a measurement alias that doesn't exist. | ||
|
|
||
| mod error; | ||
| mod info; | ||
| mod raw; | ||
|
|
||
| #[cfg(feature = "git")] | ||
| use std::path::PathBuf; | ||
| #[cfg(feature = "git")] | ||
| use tracing::{info, instrument}; | ||
|
|
||
| pub use error::{LoadError, MeasurementError, ProtectionError}; | ||
| pub use info::{AdjInfo, ProtectionTypeInfo}; | ||
| pub use raw::load_from_dir; | ||
|
|
||
| #[cfg(feature = "git")] | ||
| use crate::git::{unwrap_repo_path, update_adj}; | ||
| use crate::model::PodData; | ||
|
|
||
| /// The result of loading an ADJ: the vehicle's data (as [`crate::model`] | ||
|
|
@@ -31,3 +43,31 @@ pub struct Adj { | |
| /// Backend-session metadata from `general_info.json`. | ||
| pub info: AdjInfo, | ||
| } | ||
|
|
||
| /// Updates the ADJ repository, loads its data, and returns its full commit hash. | ||
| /// | ||
| /// `path` identifies the repository directory, not its parent. If omitted, | ||
| /// the platform's user cache directory is used. | ||
| /// | ||
| /// Online, the repository is replaced and local edits and commits are discarded. | ||
| /// Offline, the current cached checkout is loaded without switching branches, | ||
| /// so it may differ from `branch`. Clone failures do not fall back to the cache. | ||
| /// | ||
| /// This performs blocking network and filesystem operations. Call during startup, | ||
| /// before readers use ADJ, with only one updater at a time. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`LoadError::Git`] if path resolution or the update fails. | ||
| /// Other variants report failures loading the checked-out ADJ data. | ||
| /// Loading failures do not restore the previous cache. | ||
| #[cfg(feature = "git")] | ||
| #[instrument(level = "debug")] | ||
| pub fn auto_adj(path: Option<PathBuf>, branch: &str) -> Result<(Adj, String), LoadError> { | ||
| let repo_path = unwrap_repo_path(path)?; | ||
| let hash = update_adj(Some(repo_path.clone()), branch)?; | ||
| let adj = load_from_dir(&repo_path)?; | ||
|
|
||
| info!(path = ?repo_path, %hash, "ADJ loaded"); | ||
| Ok((adj, hash)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function should return an objet that |
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
|
pminmic marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| //! Blocking Git helpers for ADJ, an application-owned, replaceable data cache. | ||
|
|
||
| mod clone; | ||
| mod error; | ||
| mod repository; | ||
| mod update; | ||
|
|
||
| pub use error::{CloneError, GitError, RepositoryError}; | ||
| pub use repository::{branch_exists, list_remote_branches, open_repo}; | ||
| pub(crate) use update::unwrap_repo_path; | ||
| pub use update::update_adj; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| # ADJ Git helpers | ||
|
|
||
| This module downloads and updates the local [ADJ repository](https://github.com/Hyperloop-UPV/adj) using [`gix`](https://crates.io/crates/gix), with [`tracing`](https://crates.io/crates/tracing) for logging. It supports branch selection and offline use of an existing local checkout. | ||
|
|
||
| ## Workflow | ||
|
|
||
| `update_adj(path, branch)` returns the full hash of the checked-out commit. | ||
| A custom path identifies the ADJ repository itself. The default is | ||
| `<cache directory>/hyperloop-control-station/adj`, using `dirs::cache_dir()`. | ||
| An existing invalid repository or inaccessible path returns an error. | ||
|
|
||
| The existing connectivity check attempts a TCP connection to `1.1.1.1:443` | ||
| with a three-second timeout. This checks external TCP connectivity, not GitHub | ||
| availability. | ||
|
|
||
| ### Online | ||
|
|
||
| Online updates replace the local checkout and do not preserve local edits or commits. | ||
|
|
||
| - Shallow-clone the requested branch into a temporary directory beside ADJ. | ||
| - Complete checkout and read its commit hash before touching the cache. | ||
| - Move the old cache aside and install the new checkout. | ||
| - Restore the old cache if installation fails. If restoration also fails, | ||
| preserve the backup and include its location in the error. | ||
| - Remove the old cache after successful installation. | ||
|
|
||
| A clone error leaves the old cache untouched and is returned directly; it does | ||
| not automatically fall back to offline operation. No separate fetch of the | ||
| existing cache, merge, or branch switch is needed because the checkout is replaced. | ||
|
|
||
| ### Offline | ||
|
|
||
| - If ADJ exists, return its current commit hash without updating or switching branches. | ||
| - Otherwise, return `ADJ is not available locally and cannot be downloaded`. | ||
|
|
||
| The cached branch may differ from the requested branch when offline. | ||
|
|
||
| ## Other Helpers | ||
|
|
||
| - `open_repo` opens the custom or default repository path. | ||
| - `list_remote_branches(repo)` queries the default fetch remote and returns sorted short | ||
| branch names, without downloading objects or updating local references. It | ||
| requires remote access and does not fall back to cached branches on failure. | ||
| - `branch_exists(repo, name)` uses that listing to check an exact short branch | ||
| name. Query errors are propagated; `false` means a successful query did not | ||
| find the branch, not that the remote was unreachable. | ||
|
|
||
| ## Limitations | ||
|
|
||
| Replacement is intended for startup, before the backend reads ADJ, with one | ||
| updater at a time. The backup and installation renames are not jointly atomic | ||
| across a process crash. The connectivity probe can succeed when GitHub is | ||
| unreachable, or fail when GitHub is reachable. | ||
|
|
||
| ## Errors | ||
|
|
||
| Public helpers return `GitError` with five categories: `Path`, `Repository`, | ||
| `Clone`, `Replacement`, and `UnavailableOffline`. `RepositoryError` and | ||
| `CloneError` group the concrete `gix` causes inside the corresponding categories. | ||
| If rollback fails, `Replacement` also retains the restoration error and backup | ||
| path for recovery. | ||
|
|
||
| ## Verification | ||
|
|
||
| Tests use temporary local Git repositories and the Git CLI to create fixtures; | ||
| they do not require external network access. Production Git operations use `gix`. | ||
|
|
||
| ```bash | ||
| cargo test -p software-core -F git | ||
| cargo doc --open -p software-core -F git --no-deps | ||
| ``` |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Explain here which will be the content of
hashif there is not online connection or if git fails.