diff --git a/libra/src/command/clone.rs b/libra/src/command/clone.rs index bb3b17138..823f86b8f 100644 --- a/libra/src/command/clone.rs +++ b/libra/src/command/clone.rs @@ -15,8 +15,6 @@ use std::cell::Cell; use std::path::PathBuf; use std::{env, fs}; -const ORIGIN: &str = "origin"; // default remote name, prevent spelling mistakes - #[derive(Parser, Debug)] pub struct CloneArgs { /// The remote repository location to clone from, usually a URL with HTTPS or SSH @@ -98,10 +96,10 @@ pub async fn execute(args: CloneArgs) { name: "origin".to_string(), url: remote_repo.clone(), }; - fetch::fetch_repository(remote_config, args.branch.clone()).await; + fetch::fetch_repository(remote_config.clone(), args.branch.clone()).await; /* setup */ - if let Err(e) = setup_repository(remote_repo.clone(), args.branch.clone()).await { + if let Err(e) = setup_repository(remote_config, args.branch.clone()).await { eprintln!("fatal: {}", e); return; } @@ -112,11 +110,11 @@ pub async fn execute(args: CloneArgs) { /// Sets up the local repository after a clone by configuring the remote, /// setting up the initial branch and HEAD, and creating the first reflog entry. async fn setup_repository( - remote_repo: String, + remote_config: RemoteConfig, specified_branch: Option, ) -> Result<(), String> { let db = crate::internal::db::get_db_conn_instance().await; - let remote_head = Head::remote_current_with_conn(db, ORIGIN).await; + let remote_head = Head::remote_current_with_conn(db, &remote_config.name).await; // Determine which branch to check out. let branch_to_checkout = match specified_branch { @@ -131,13 +129,14 @@ async fn setup_repository( }; if let Some(branch_name) = branch_to_checkout { - let origin_branch = Branch::find_branch_with_conn(db, &branch_name, Some(ORIGIN)) - .await - .ok_or_else(|| format!("fatal: remote branch '{}' not found.", branch_name))?; + let origin_branch = + Branch::find_branch_with_conn(db, &branch_name, Some(&remote_config.name)) + .await + .ok_or_else(|| format!("fatal: remote branch '{}' not found.", branch_name))?; // Prepare the reflog context *before* the transaction let action = ReflogAction::Clone { - from: remote_repo.clone(), + from: remote_config.url.clone(), }; let context = ReflogContext { @@ -174,12 +173,24 @@ async fn setup_repository( &merge_ref, ) .await; - Config::insert_with_conn(txn, "branch", Some(&branch_name), "remote", ORIGIN) - .await; + Config::insert_with_conn( + txn, + "branch", + Some(&branch_name), + "remote", + &remote_config.name, + ) + .await; // 4. Configure the remote URL - Config::insert_with_conn(txn, "remote", Some(ORIGIN), "url", &remote_repo) - .await; + Config::insert_with_conn( + txn, + "remote", + Some(&remote_config.name), + "url", + &remote_config.url, + ) + .await; Ok(()) }) }, @@ -200,13 +211,25 @@ async fn setup_repository( println!("warning: You appear to have cloned an empty repository."); // We only need to set the remote URL. No reflog is created as there are no commits. - Config::insert("remote", Some(ORIGIN), "url", &remote_repo).await; + Config::insert( + "remote", + Some(&remote_config.name), + "url", + &remote_config.url, + ) + .await; // Optionally set up a default branch config for a future 'master' or 'main' let default_branch = "master"; let merge_ref = format!("refs/heads/{}", default_branch); Config::insert("branch", Some(default_branch), "merge", &merge_ref).await; - Config::insert("branch", Some(default_branch), "remote", ORIGIN).await; + Config::insert( + "branch", + Some(default_branch), + "remote", + &remote_config.name, + ) + .await; } Ok(()) diff --git a/libra/src/command/fetch.rs b/libra/src/command/fetch.rs index b193c5bf4..5a8762710 100644 --- a/libra/src/command/fetch.rs +++ b/libra/src/command/fetch.rs @@ -12,7 +12,7 @@ use tokio::io::{AsyncRead, AsyncReadExt}; use tokio_util::io::StreamReader; use url::Url; -use crate::command::load_object; +use crate::command::{load_object, HEAD}; use crate::internal::db::get_db_conn_instance; use crate::internal::reflog; use crate::internal::reflog::{zero_sha1, ReflogAction, ReflogContext, ReflogError}; @@ -235,24 +235,34 @@ pub async fn fetch_repository(remote_config: RemoteConfig, branch: Option