-
Notifications
You must be signed in to change notification settings - Fork 9
use github fast path to check for changes before doing the git pull #47
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
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use reqwest::{StatusCode, Url}; | ||
|
||
#[derive(Debug)] | ||
pub(crate) enum FastPath { | ||
UpToDate, | ||
NeedsFetch, | ||
Indeterminate, | ||
} | ||
|
||
/// extract username & repository from a fetch URL, only if it's on Github. | ||
fn user_and_repo_from_url_if_github(fetch_url: &gix::Url) -> Option<(String, String)> { | ||
let url = Url::parse(&fetch_url.to_string()).ok()?; | ||
if !(url.host_str() == Some("github.com")) { | ||
return None; | ||
} | ||
|
||
// This expects GitHub urls in the form `github.com/user/repo` and nothing | ||
// else | ||
let mut pieces = url.path_segments()?; | ||
let username = pieces.next()?; | ||
let repository = pieces.next()?; | ||
let repository = repository.strip_suffix(".git").unwrap_or(repository); | ||
if pieces.next().is_some() { | ||
return None; | ||
} | ||
Some((username.to_string(), repository.to_string())) | ||
} | ||
|
||
/// use github fast-path to check if the repository has any changes | ||
/// since the last seen reference. | ||
/// | ||
/// To save server side resources on github side, we can use an API | ||
/// to check if there are any changes in the repository before we | ||
/// actually run `git fetch`. | ||
/// | ||
/// On non-github fetch URLs we don't do anything and always run the fetch. | ||
/// | ||
/// Code gotten and adapted from | ||
/// https://github.com/rust-lang/cargo/blob/edd36eba5e0d6e0cfcb84bd0cc651ba8bf5e7f83/src/cargo/sources/git/utils.rs#L1396 | ||
/// | ||
/// GitHub documentation: | ||
/// https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#get-a-commit | ||
/// specifically using `application/vnd.github.sha` | ||
syphar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub(crate) fn has_changes( | ||
fetch_url: &gix::Url, | ||
last_seen_reference: &gix::ObjectId, | ||
branch_name: &str, | ||
) -> Result<FastPath, reqwest::Error> { | ||
let (username, repository) = match user_and_repo_from_url_if_github(fetch_url) { | ||
Some(url) => url, | ||
None => return Ok(FastPath::Indeterminate), | ||
}; | ||
|
||
let url = format!( | ||
"https://api.github.com/repos/{}/{}/commits/{}", | ||
username, repository, branch_name, | ||
); | ||
|
||
let client = reqwest::blocking::Client::builder() | ||
.user_agent("crates-index-diff") | ||
.build()?; | ||
let response = client | ||
.get(&url) | ||
.header("Accept", "application/vnd.github.sha") | ||
.header("If-None-Match", format!("\"{}\"", last_seen_reference)) | ||
.send()?; | ||
|
||
let status = response.status(); | ||
if status == StatusCode::NOT_MODIFIED { | ||
Ok(FastPath::UpToDate) | ||
} else if status.is_success() { | ||
Ok(FastPath::NeedsFetch) | ||
} else { | ||
// Usually response_code == 404 if the repository does not exist, and | ||
// response_code == 422 if exists but GitHub is unable to resolve the | ||
// requested rev. | ||
Ok(FastPath::Indeterminate) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use std::convert::TryFrom; | ||
|
||
#[test] | ||
fn test_github_http_url() { | ||
let (user, repo) = user_and_repo_from_url_if_github( | ||
&gix::Url::try_from("https://github.com/some_user/some_repo.git").unwrap(), | ||
) | ||
.unwrap(); | ||
assert_eq!(user, "some_user"); | ||
assert_eq!(repo, "some_repo"); | ||
} | ||
|
||
#[test] | ||
fn test_github_ssh_url() { | ||
let (user, repo) = user_and_repo_from_url_if_github( | ||
&gix::Url::try_from("ssh://[email protected]/some_user/some_repo.git").unwrap(), | ||
) | ||
.unwrap(); | ||
assert_eq!(user, "some_user"); | ||
assert_eq!(repo, "some_repo"); | ||
} | ||
|
||
#[test] | ||
fn test_non_github_url() { | ||
assert!(user_and_repo_from_url_if_github( | ||
&gix::Url::try_from("https://not_github.com/some_user/some_repo.git").unwrap(), | ||
) | ||
.is_none()); | ||
} | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.