Skip to content

Commit

Permalink
Merge pull request #826 from ichAB/quiet
Browse files Browse the repository at this point in the history
impl quiet for init
  • Loading branch information
genedna authored Feb 5, 2025
2 parents dec8d2f + 9de9097 commit 0321a89
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
3 changes: 3 additions & 0 deletions aria/contents/docs/libra/command/init/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ which sets up the repository without a working directory, containing only Libra
- `-h`, `--help` Print help
- `[directory]`
Initialize the repository with specified directory
- `-q`,`--quiet`
Suppress all output except for errors

1 change: 0 additions & 1 deletion libra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ tracing = { workspace = true }
tracing-subscriber = { workspace = true }
url = "2.5.3"
wax = "0.6.0"

[target.'cfg(unix)'.dependencies] # only on Unix
pager = "0.16.0"

Expand Down
2 changes: 1 addition & 1 deletion libra/src/command/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub async fn execute(args: CloneArgs) {

// CAUTION: change [current_dir] to the repo directory
env::set_current_dir(&local_path).unwrap();
let init_args = command::init::InitArgs { bare: false, initial_branch: None, repo_directory: local_path.to_str().unwrap().to_string() };
let init_args = command::init::InitArgs { bare: false, initial_branch: None, repo_directory: local_path.to_str().unwrap().to_string(),quiet:false };
command::init::execute(init_args).await;

/* fetch remote */
Expand Down
54 changes: 39 additions & 15 deletions libra/src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub struct InitArgs {
/// Create a repository in the specified directory
#[clap(default_value = ".")]
pub repo_directory: String,

/// Suppress all output
#[clap(long, short = 'q', required = false)]
pub quiet: bool,
}

/// Execute the init function
Expand Down Expand Up @@ -91,8 +95,9 @@ pub async fn init(args: InitArgs) -> io::Result<()> {

// Check if the root directory already exists
if is_reinit(&cur_dir) {
println!("Already initialized - [{}]", root_dir.display());

if !args.quiet {
eprintln!("Already initialized - [{}]", root_dir.display());
}
return Err(io::Error::new(
io::ErrorKind::AlreadyExists,
"Initialization failed: The repository is already initialized at the specified location.
Expand Down Expand Up @@ -155,10 +160,12 @@ pub async fn init(args: InitArgs) -> io::Result<()> {

// Set .libra as hidden
set_dir_hidden(root_dir.to_str().unwrap())?;
println!(
"Initializing empty Libra repository in {}",
root_dir.display()
);
if !args.quiet {
println!(
"Initializing empty Libra repository in {}",
root_dir.display()
);
}

Ok(())
}
Expand Down Expand Up @@ -258,7 +265,7 @@ mod tests {
// Set up the test environment without a Libra repository
test::setup_clean_testing_env();
let cur_dir = env::current_dir().unwrap();
let args = InitArgs { bare: false, initial_branch: None, repo_directory: cur_dir.to_str().unwrap().to_string() };
let args = InitArgs { bare: false, initial_branch: None, repo_directory: cur_dir.to_str().unwrap().to_string(),quiet:false };
// Run the init function
init(args).await.unwrap();

Expand All @@ -277,7 +284,7 @@ mod tests {
test::setup_clean_testing_env();
// Run the init function with --bare flag
let cur_dir = env::current_dir().unwrap();
let args = InitArgs { bare: true, initial_branch: None, repo_directory: cur_dir.to_str().unwrap().to_string() };
let args = InitArgs { bare: true, initial_branch: None, repo_directory: cur_dir.to_str().unwrap().to_string(),quiet:false};
// Run the init function
init(args).await.unwrap();

Expand All @@ -293,12 +300,12 @@ mod tests {

// Initialize a bare repository
let cur_dir = env::current_dir().unwrap();
let init_args = InitArgs { bare: false, initial_branch: None, repo_directory: cur_dir.to_str().unwrap().to_string() };
let init_args = InitArgs { bare: false, initial_branch: None, repo_directory: cur_dir.to_str().unwrap().to_string(),quiet:false };
init(init_args).await.unwrap(); // Execute init for bare repository

// Simulate trying to reinitialize the bare repo
let result = async {
let args = InitArgs { bare: true, initial_branch: None, repo_directory: cur_dir.to_str().unwrap().to_string() };
let args = InitArgs { bare: true, initial_branch: None, repo_directory: cur_dir.to_str().unwrap().to_string(),quiet:false };
init(args).await
};

Expand All @@ -314,7 +321,7 @@ mod tests {
// Set up the test environment without a Libra repository
test::setup_clean_testing_env();
let cur_dir = env::current_dir().unwrap();
let args = InitArgs { bare: false, initial_branch: Some("main".to_string()), repo_directory: cur_dir.to_str().unwrap().to_string() };
let args = InitArgs { bare: false, initial_branch: Some("main".to_string()), repo_directory: cur_dir.to_str().unwrap().to_string(),quiet:false };
// Run the init function
init(args).await.unwrap();

Expand Down Expand Up @@ -361,7 +368,7 @@ mod tests {
// Set up the test environment without a Libra repository
test::setup_clean_testing_env();
let cur_dir = env::current_dir().unwrap();
let args = InitArgs { bare: false, initial_branch: Some(branch_name.to_string()), repo_directory: cur_dir.to_str().unwrap().to_string() };
let args = InitArgs { bare: false, initial_branch: Some(branch_name.to_string()), repo_directory: cur_dir.to_str().unwrap().to_string(),quiet:false };
// Run the init function
let result = init(args).await;
// Check for the error
Expand All @@ -380,7 +387,7 @@ mod tests {
let cur_dir = env::current_dir().unwrap();
let test_dir = cur_dir.join("test");

let args = InitArgs { bare: false, initial_branch: None, repo_directory: test_dir.to_str().unwrap().to_owned() };
let args = InitArgs { bare: false, initial_branch: None, repo_directory: test_dir.to_str().unwrap().to_owned(),quiet:false };
// Run the init function
init(args).await.unwrap();

Expand All @@ -405,7 +412,7 @@ mod tests {
// Create a file with the same name as the test directory
fs::File::create(&test_dir).unwrap();

let args = InitArgs { bare: false, initial_branch: None, repo_directory: test_dir.to_str().unwrap().to_owned() };
let args = InitArgs { bare: false, initial_branch: None, repo_directory: test_dir.to_str().unwrap().to_owned(),quiet:false };
// Run the init function
let result = init(args).await;

Expand All @@ -428,7 +435,7 @@ mod tests {
fs::create_dir(&test_dir).unwrap();
fs::set_permissions(&test_dir, fs::Permissions::from_mode(0o444)).unwrap();

let args = InitArgs { bare: false, initial_branch: None, repo_directory: test_dir.to_str().unwrap().to_owned() };
let args = InitArgs { bare: false, initial_branch: None, repo_directory: test_dir.to_str().unwrap().to_owned(),quiet:false };
// Run the init function
let result = init(args).await;

Expand All @@ -437,5 +444,22 @@ mod tests {
assert_eq!(err.kind(), std::io::ErrorKind::PermissionDenied); // Check error type
assert!(err.to_string().contains("The target directory is read-only")); // Check error message
}

#[tokio::test]
/// Test the init function with the --quiet flag by using --show-output
async fn test_init_quiet() {
// Set up the test environment without a Libra repository
test::setup_clean_testing_env();
let cur_dir = env::current_dir().unwrap();
let args = InitArgs { bare: false, initial_branch: None, repo_directory: cur_dir.to_str().unwrap().to_string(),quiet:true };
// Run the init function
init(args).await.unwrap();

// Verify that the `.libra` directory exists
let libra_dir = Path::new(".libra");
assert!(libra_dir.exists(), ".libra directory does not exist");

// Verify the contents of the other directory
verify_init(libra_dir);
}
}
2 changes: 1 addition & 1 deletion libra/src/utils/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn setup_clean_testing_env() {
/// switch to test dir and create a new .libra
pub async fn setup_with_new_libra() {
setup_clean_testing_env();
let args = command::init::InitArgs { bare: false, initial_branch: None, repo_directory: util::cur_dir().to_str().unwrap().to_string() };
let args = command::init::InitArgs { bare: false, initial_branch: None, repo_directory: util::cur_dir().to_str().unwrap().to_string(),quiet:false };
command::init::init(args).await.unwrap();
}

Expand Down

1 comment on commit 0321a89

@vercel
Copy link

@vercel vercel bot commented on 0321a89 Feb 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

mega – ./

www.gitmega.dev
mega-gitmono.vercel.app
gitmega.dev
mega-git-main-gitmono.vercel.app

Please sign in to comment.