Skip to content

feat: patchable init mirroring #1070

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 32 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
27c46b2
feat: support forking when initializing
dervoeti Apr 25, 2025
b863cb8
fix: don't use fetchhead / update libgit / migrate patchable.toml files
dervoeti Apr 30, 2025
9c85873
chore: replace "forking" with "mirroring"
dervoeti Apr 30, 2025
cf27542
feat: keep original upstream / anonymous remote
dervoeti Apr 30, 2025
3034966
refactor: move progress tracking setup into utils function
dervoeti May 2, 2025
42e9cb4
feat: always use tags in our mirror repos
dervoeti May 5, 2025
3a64496
Update rust/patchable/src/main.rs
dervoeti May 5, 2025
1d444db
Update rust/patchable/src/main.rs
dervoeti May 5, 2025
06ca421
Update rust/patchable/src/main.rs
dervoeti May 5, 2025
5aa1769
Update rust/patchable/src/main.rs
dervoeti May 5, 2025
37b4bf2
Update rust/patchable/src/main.rs
dervoeti May 5, 2025
0db5985
Update rust/patchable/src/main.rs
dervoeti May 5, 2025
fbf681f
fix: span behaviour / mirror flag
dervoeti May 5, 2025
694e5c7
feat: separate product level configuration
dervoeti May 6, 2025
43f33c1
refactor: separate product and version config
dervoeti May 6, 2025
2adc5e1
fix: adjust README
dervoeti May 6, 2025
27a9096
chore: formatting / unnecessary clone
dervoeti May 7, 2025
081eec1
fix: tracing
dervoeti May 7, 2025
ea3fb74
fix: readme adjustment
dervoeti May 7, 2025
51b26ed
Make mirroring optional again
nightkr May 8, 2025
dd32e8a
feat: ssh support
dervoeti May 8, 2025
9923c9d
feat: remove url crate
dervoeti May 8, 2025
b101cd4
docs: update README
dervoeti May 8, 2025
b30dfe6
Update rust/patchable/src/main.rs
dervoeti May 8, 2025
098a440
Merge branch 'main' of https://github.com/stackabletech/docker-images…
dervoeti May 8, 2025
84c6ce6
feat: product-level configs for patchable
dervoeti May 8, 2025
9604e63
docs: fixed README.md for markdownlint
dervoeti May 8, 2025
155a162
chore: removed upstream from version configs / added missing product-…
dervoeti May 9, 2025
02a06a2
Merge branch 'main' of https://github.com/stackabletech/docker-images…
dervoeti May 9, 2025
6148608
fix: copy complete patches directories to include patchable product c…
dervoeti May 9, 2025
7e25a6d
Revert "fix: copy complete patches directories to include patchable p…
dervoeti May 9, 2025
11d1971
fix: copy patchable product config as well
dervoeti May 9, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ toml = "0.8.19"
tracing = "0.1.41"
tracing-indicatif = "0.3.9"
tracing-subscriber = { version = "0.3.19", features = ["env-filter"] }
url = "2.5.4"
1 change: 1 addition & 0 deletions rust/patchable/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ toml.workspace = true
tracing.workspace = true
tracing-indicatif.workspace = true
tracing-subscriber.workspace = true
url.workspace = true
61 changes: 43 additions & 18 deletions rust/patchable/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use snafu::{OptionExt, ResultExt as _, Snafu};
use tracing_indicatif::IndicatifLayer;
use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _};

use crate::utils::setup_git_credentials;

#[derive(clap::Parser)]
struct ProductVersion {
/// The product name slug (such as druid)
Expand Down Expand Up @@ -164,6 +166,10 @@ enum Cmd {
/// Check out the base commit, without applying patches
#[clap(long)]
base_only: bool,

/// Use SSH for git operations
#[clap(long)]
ssh: bool,
},

/// Export the patches from the source tree at docker-images/<PRODUCT>/patchable-work/worktree/<VERSION>
Expand All @@ -185,8 +191,13 @@ enum Cmd {
#[clap(long)]
base: String,

/// Mirror the product version to the default mirror repository
#[clap(long)]
mirror: bool,

/// Use SSH for git operations
#[clap(long)]
ssh: bool,
},

/// Shows the patch directory for a given product version
Expand Down Expand Up @@ -235,6 +246,9 @@ pub enum Error {
path: PathBuf,
},

#[snafu(display("failed to rewrite URL for SSH: {source}"))]
UrlRewrite { source: utils::UrlRewriteError },

#[snafu(display(
"mirroring requested, but default-mirror is not configured in product configuration"
))]
Expand Down Expand Up @@ -326,7 +340,7 @@ fn main() -> Result<()> {
}
};
match opts.cmd {
Cmd::Checkout { pv, base_only } => {
Cmd::Checkout { pv, base_only, ssh } => {
let ctx = ProductVersionContext {
pv,
images_repo_root,
Expand All @@ -337,13 +351,20 @@ fn main() -> Result<()> {
let product_repo = repo::ensure_bare_repo(&product_repo_root)
.context(OpenProductRepoForCheckoutSnafu)?;

let mut upstream = version_config.mirror.unwrap_or_else(|| {
tracing::warn!("this product version is not mirrored, re-init it with --mirror before merging it");
product_config.upstream
});

if ssh {
upstream =
utils::rewrite_git_https_url_to_ssh(&upstream).context(UrlRewriteSnafu)?;
}

let base_commit = repo::resolve_and_fetch_commitish(
&product_repo,
&version_config.base.to_string(),
version_config.mirror.as_deref().unwrap_or_else(|| {
tracing::warn!("this product version is not mirrored, re-init it with --mirror before merging it");
&product_config.upstream
}),
&upstream,
)
.context(FetchBaseCommitSnafu)?;
let base_branch = ctx.base_branch();
Expand Down Expand Up @@ -453,7 +474,12 @@ fn main() -> Result<()> {
);
}

Cmd::Init { pv, base, mirror } => {
Cmd::Init {
pv,
base,
mirror,
ssh,
} => {
let ctx = ProductVersionContext {
pv,
images_repo_root,
Expand All @@ -468,7 +494,11 @@ fn main() -> Result<()> {
.context(OpenProductRepoForCheckoutSnafu)?;

let config = ctx.load_product_config()?;
let upstream = config.upstream;
let upstream = if ssh {
utils::rewrite_git_https_url_to_ssh(&config.upstream).context(UrlRewriteSnafu)?
} else {
config.upstream
};

// --base can be a reference, but patchable.toml should always have a resolved commit id,
// so that it cannot be changed under our feet (without us knowing so, anyway...).
Expand All @@ -478,10 +508,13 @@ fn main() -> Result<()> {
tracing::info!(?base, base.commit = ?base_commit, "resolved base commit");

let mirror_url = if mirror {
let mirror_url = config
let mut mirror_url = config
.default_mirror
.context(InitMirrorNotConfiguredSnafu)?;

if ssh {
mirror_url =
utils::rewrite_git_https_url_to_ssh(&mirror_url).context(UrlRewriteSnafu)?
};
// Add mirror remote
let mut mirror_remote =
product_repo
Expand All @@ -492,15 +525,7 @@ fn main() -> Result<()> {

// Push the base commit to the mirror
tracing::info!(commit = %base_commit, base = base, url = mirror_url, "pushing commit to mirror");
let mut callbacks = git2::RemoteCallbacks::new();
callbacks.credentials(|url, username_from_url, _allowed_types| {
git2::Cred::credential_helper(
&git2::Config::open_default()
.expect("failed to open default Git configuration"), // Use default git config,
url,
username_from_url,
)
});
let mut callbacks = setup_git_credentials();

// Add progress tracking for push operation
let (span_push, mut quant_push) =
Expand Down
6 changes: 3 additions & 3 deletions rust/patchable/src/repo.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::path::{self, Path, PathBuf};

use git2::{
FetchOptions, ObjectType, Oid, RemoteCallbacks, Repository, RepositoryInitOptions,
FetchOptions, ObjectType, Oid, Repository, RepositoryInitOptions,
WorktreeAddOptions,
};
use snafu::{ResultExt, Snafu};

use crate::{
error::{self, CommitRef},
utils::setup_progress_tracking,
utils::{setup_git_credentials, setup_progress_tracking},
};

#[derive(Debug, Snafu)]
Expand Down Expand Up @@ -157,7 +157,7 @@ pub fn resolve_and_fetch_commitish(
let _ = span_recv.enter();
let _ = span_index.enter();

let mut callbacks = RemoteCallbacks::new();
let mut callbacks = setup_git_credentials();
callbacks.transfer_progress(move |progress| {
quant_recv.update_span_progress(
progress.received_objects(),
Expand Down
45 changes: 45 additions & 0 deletions rust/patchable/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
use std::path::Path;

use git2::Repository;
use snafu::{OptionExt as _, ResultExt as _, Snafu};
use tracing::Span;
use tracing_indicatif::{span_ext::IndicatifSpanExt, style::ProgressStyle};
use url::Url;

/// Errors that can occur during URL rewriting.
#[derive(Debug, Snafu)]
pub enum UrlRewriteError {
#[snafu(display("Failed to parse URL {url:?}: {source}"))]
ParseUrl { source: url::ParseError, url: String },
#[snafu(display("URL {url:?} has no host component"))]
NoHostInUrl { url: String },
}

/// Rewrites a given URL to an SSH-style Git URL
/// For example, `https://github.com/user/repo.git` becomes `[email protected]:user/repo.git`.
pub fn rewrite_git_https_url_to_ssh(
original_url: &str,
) -> Result<String, UrlRewriteError> {
let parsed_url = Url::parse(original_url).context(ParseUrlSnafu {
url: original_url,
})?;

let host = parsed_url.host_str().context(NoHostInUrlSnafu {
url: original_url,
})?;
let path = parsed_url.path().trim_start_matches('/');

Ok(format!("git@{}:{}", host, path))
}

/// Runs a function whenever a `value` changes "enough".
///
Expand Down Expand Up @@ -97,3 +125,20 @@ pub fn setup_progress_tracking(span: tracing::Span) -> (tracing::Span, Quantizer
let quantizer = Quantizer::percent();
(span, quantizer)
}

/// Basic configuration of credentials for Git operations.
pub fn setup_git_credentials<'a>() -> git2::RemoteCallbacks<'a> {
let mut callbacks = git2::RemoteCallbacks::new();
callbacks.credentials(|url, username_from_url, allowed_types| {
if allowed_types.contains(git2::CredentialType::SSH_KEY) {
git2::Cred::ssh_key_from_agent(username_from_url.unwrap_or("git"))
} else {
git2::Cred::credential_helper(
&git2::Config::open_default().expect("failed to open default Git configuration"),
url,
username_from_url,
)
}
});
callbacks
}