use crate::*;
pub(crate) fn init() {
Command::new("git")
.arg("init")
.status()
.expect("Failed to execute git init");
}
pub(crate) fn config_global_add_safe_directory(path: &str) {
Command::new("git")
.args([
"config",
"--global",
"--add",
"safe.directory",
&format!("'{path}'"),
])
.status()
.expect("Failed to execute git config --global --add safe.directory './'");
}
pub(crate) fn config_advice_add_ignored_file_false() {
Command::new("git")
.args(["config", "advice.addIgnoredFile", "false"])
.status()
.expect("Failed to execute git config advice.addIgnoredFile false");
}
pub(crate) fn remote_add(remote: &Remote) {
Command::new("git")
.arg("remote")
.arg("add")
.arg(&remote.name)
.arg(&remote.url)
.status()
.expect("Failed to add remote");
}
pub(crate) fn add_all() {
Command::new("git")
.args(["add", "*"])
.status()
.expect("Failed to add *");
}
pub(crate) fn commit(msg: &str) {
Command::new("git")
.args(["commit", "-m", msg])
.status()
.unwrap_or_else(|_| panic!("Failed to commit -m {msg}"));
}
pub(crate) fn push(remote: &str) {
Command::new("git")
.args(["push", remote])
.status()
.expect("Failed to push to remote");
}
pub(crate) fn help() {
let get_package_name: &str = get_package_name();
println!("{get_package_name} extension usage: {get_package_name} acp\n");
Command::new("git")
.arg("help")
.status()
.expect("Failed to run help");
}
pub(crate) fn version() {
println!("{} version: {}", get_package_name(), get_package_version());
}
pub(crate) fn other(args: &Vec<OsString>) {
let status: ExitStatus = Command::new("git")
.args(args)
.status()
.expect("Failed to execute git command");
exit(status.code().unwrap_or_default());
}
pub(crate) fn publish_package() {
for attempt in 1..=MAX_RETRIES {
let status = Command::new("cargo")
.args(["publish", "--allow-dirty"])
.status();
match status {
Ok(exit_status) if exit_status.success() => {
println!("Successfully published package.");
return;
}
Ok(exit_status) => {
eprintln!(
"Attempt {attempt} failed with status: {exit_status}. Retrying in {RETRY_DELAY_SECS} seconds..."
);
}
Err(e) => {
eprintln!(
"Attempt {attempt} failed with error: {e}. Retrying in {RETRY_DELAY_SECS} seconds..."
);
}
}
if attempt < MAX_RETRIES {
std::thread::sleep(std::time::Duration::from_secs(RETRY_DELAY_SECS));
}
}
panic!("Failed to publish package after {MAX_RETRIES} attempts.");
}
pub(crate) fn init_repository(config: &Config) {
init();
config_global_add_safe_directory("./");
config_advice_add_ignored_file_false();
let current_dir: PathBuf = std::env::current_dir().unwrap();
let current_path: &str = current_dir.to_str().unwrap();
if let Some(remotes) = config.get(current_path) {
for remote in remotes {
remote_add(remote);
}
}
}
pub(crate) fn push_to_all_remotes(config: &Config) {
let current_dir: PathBuf = std::env::current_dir().unwrap();
let current_path: &str = current_dir.to_str().unwrap();
if let Some(remotes) = config.get(current_path) {
for remote in remotes {
push(&remote.name);
}
}
}
pub(crate) fn generate_auto_commit_message() -> String {
match fs::read_to_string("Cargo.toml") {
Ok(content) => {
if let Ok(cargo_toml) = toml::from_str::<CargoToml>(&content) {
format!("feat: v{}", cargo_toml.package.version)
} else {
let now = Local::now();
format!("feat: {}", now.format("%Y-%m-%d %H:%M:%S"))
}
}
Err(_) => {
let now = Local::now();
format!("feat: {}", now.format("%Y-%m-%d %H:%M:%S"))
}
}
}
pub(crate) fn add_commit_auto_push(config: &Config) {
add_all();
let commit_msg = generate_auto_commit_message();
commit(&commit_msg);
push_to_all_remotes(config);
}
pub(crate) fn add_commit_push_to_all_remotes(config: &Config) {
let current_dir: PathBuf = std::env::current_dir().unwrap();
let current_path: &str = current_dir.to_str().unwrap();
io::stdout().flush().unwrap();
let mut commit_msg_input: String = String::new();
io::stdin().read_line(&mut commit_msg_input).unwrap();
let commit_msg_trimmed: &str = commit_msg_input.trim();
let commit_msg = if commit_msg_trimmed.is_empty() {
generate_auto_commit_message()
} else {
commit_msg_trimmed.to_string()
};
add_all();
commit(&commit_msg);
if let Some(remotes) = config.get(current_path) {
for remote in remotes {
push(&remote.name);
}
}
}