-
Notifications
You must be signed in to change notification settings - Fork 39
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
feat: allow git remote repositories (https) as project locations #152
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1fef939
feat: use remote git repositories (https) as valid project location
Angelmmiguel 3a200c3
fix: add missing file notices
Angelmmiguel d9aad6a
fix: improve CLI output and fix typos
Angelmmiguel 3eff29c
feat: identify git references with an enum and revert Path -> String
Angelmmiguel 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,35 @@ | ||
// Copyright 2023 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
/// Defines the different options to configure the project. | ||
/// Every type has their own options. | ||
#[derive(Default)] | ||
pub struct Options { | ||
/// Options for Git repositories | ||
pub git: Option<GitOptions>, | ||
/// Options for local repositories | ||
pub local: Option<LocalOptions>, | ||
} | ||
|
||
/// For now, we don't have any particular option for this type. | ||
/// I'm keeping it as a placeholder | ||
#[derive(Default)] | ||
pub struct LocalOptions {} | ||
|
||
/// Defines a different reference when cloning the repository | ||
pub enum GitReference { | ||
/// Use a specific commit | ||
Commit(String), | ||
/// Use a specific tag | ||
Tag(String), | ||
/// Use a specific git branch | ||
Branch(String), | ||
} | ||
|
||
/// The different git options you can configure. | ||
#[derive(Default)] | ||
pub struct GitOptions { | ||
pub git_ref: Option<GitReference>, | ||
/// Change the directory to run the workers | ||
pub folder: Option<String>, | ||
} |
This file contains 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,73 @@ | ||
// Copyright 2023 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use crate::options::{GitReference, Options}; | ||
use anyhow::{anyhow, bail, Result}; | ||
use git2::{Oid, Repository}; | ||
use sha256::digest as sha256_digest; | ||
use std::{ | ||
env::temp_dir, | ||
fs::remove_dir_all, | ||
path::{Path, PathBuf}, | ||
}; | ||
|
||
// Default remote for git repos | ||
static DEFAULT_REMOTE: &str = "origin"; | ||
|
||
/// Prepare a project based on a git repository. This method | ||
/// clones the repo locally and returns the path in which it's located. | ||
pub fn prepare_git_project(location: &Path, options: Option<Options>) -> Result<PathBuf> { | ||
let project_url = location | ||
.to_str() | ||
.ok_or(anyhow!("The project URL cannot be retrieved"))?; | ||
// By default, we use temporary dirs | ||
let mut dir = temp_dir().join(sha256_digest(project_url)); | ||
|
||
if dir.exists() { | ||
// Clean up a previous download | ||
remove_dir_all(&dir)?; | ||
} | ||
|
||
let repo = match Repository::clone(project_url, &dir) { | ||
Ok(repo) => repo, | ||
Err(e) => bail!("There was an error cloning the repository: {e}"), | ||
}; | ||
|
||
if let Some(options) = options { | ||
if let Some(git) = options.git { | ||
if let Some(git_ref) = git.git_ref { | ||
match git_ref { | ||
GitReference::Commit(commit) => { | ||
let oid = Oid::from_str(&commit)?; | ||
let commit = repo.find_commit(oid)?; | ||
repo.checkout_tree(commit.as_object(), None)?; | ||
} | ||
GitReference::Tag(tag) => { | ||
let mut remote = repo.find_remote(DEFAULT_REMOTE)?; | ||
let tag_remote = format!("refs/tags/{tag}:refs/tags/{tag}"); | ||
remote.fetch(&[&tag_remote], None, None)?; | ||
|
||
let oid = Oid::from_str(&tag)?; | ||
let tag = repo.find_tag(oid)?; | ||
repo.checkout_tree(tag.as_object(), None)?; | ||
} | ||
GitReference::Branch(branch) => { | ||
let mut remote = repo.find_remote(DEFAULT_REMOTE)?; | ||
let head_remote = format!("refs/heads/{branch}:refs/heads/{branch}"); | ||
remote.fetch(&[&head_remote], None, None)?; | ||
|
||
let branch = repo.find_branch(&branch, git2::BranchType::Local)?; | ||
let reference = branch.into_reference(); | ||
repo.checkout_tree(&reference.peel(git2::ObjectType::Tree)?, None)?; | ||
} | ||
} | ||
} | ||
|
||
if let Some(folder) = git.folder { | ||
dir = dir.join(folder); | ||
} | ||
} | ||
} | ||
|
||
Ok(dir) | ||
} |
This file contains 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,4 @@ | ||
// Copyright 2023 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
pub mod git; |
Empty file.
This file contains 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.
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 have been looking for a better way to express this, but it requires
let_chains
and is unstable. :(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.
Yeah, a new approach for "let chains" would be really great 😄. I will keep an eye on this. Thanks for the pointer.