add some features
This commit is contained in:
+90
-4
@@ -252,16 +252,38 @@ pub fn commit(path: String, message: String) -> Result<GitStatus, String> {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn pull(path: String) -> Result<GitStatus, String> {
|
||||
pub fn pull(
|
||||
path: String,
|
||||
username: Option<String>,
|
||||
password: Option<String>,
|
||||
) -> Result<GitStatus, String> {
|
||||
let repo = resolve_repo(&path)?;
|
||||
run_git(&repo, ["pull", "--ff-only"])?;
|
||||
match (username.as_deref(), password.as_deref()) {
|
||||
(Some(u), Some(p)) if !u.is_empty() || !p.is_empty() => {
|
||||
run_git_authenticated(&repo, ["pull", "--ff-only"], u, p)?;
|
||||
}
|
||||
_ => {
|
||||
run_git(&repo, ["pull", "--ff-only"])?;
|
||||
}
|
||||
}
|
||||
status_for_repo(&repo)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn push(path: String) -> Result<GitStatus, String> {
|
||||
pub fn push(
|
||||
path: String,
|
||||
username: Option<String>,
|
||||
password: Option<String>,
|
||||
) -> Result<GitStatus, String> {
|
||||
let repo = resolve_repo(&path)?;
|
||||
run_git(&repo, ["push"])?;
|
||||
match (username.as_deref(), password.as_deref()) {
|
||||
(Some(u), Some(p)) if !u.is_empty() || !p.is_empty() => {
|
||||
run_git_authenticated(&repo, ["push"], u, p)?;
|
||||
}
|
||||
_ => {
|
||||
run_git(&repo, ["push"])?;
|
||||
}
|
||||
}
|
||||
status_for_repo(&repo)
|
||||
}
|
||||
|
||||
@@ -1163,6 +1185,70 @@ fn validate_files(files: &[String]) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn write_askpass_script() -> Result<std::path::PathBuf, String> {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let path = std::env::temp_dir().join("gitlite_askpass.sh");
|
||||
let script = "#!/bin/sh\ncase \"$1\" in\n *[Uu]sername*) printf '%s\\n' \"$GIT_CRED_USER\" ;;\n *) printf '%s\\n' \"$GIT_CRED_PASS\" ;;\nesac\n";
|
||||
std::fs::write(&path, script)
|
||||
.map_err(|e| format!("Konnte Authentifizierungsskript nicht schreiben: {e}"))?;
|
||||
std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o700))
|
||||
.map_err(|e| format!("Konnte Skriptrechte nicht setzen: {e}"))?;
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
fn write_askpass_script() -> Result<std::path::PathBuf, String> {
|
||||
let path = std::env::temp_dir().join("gitlite_askpass.bat");
|
||||
let script = "@echo off\necho %1 | findstr /I \"sername\" >nul 2>&1\nif %errorlevel% == 0 (echo %GIT_CRED_USER%) else (echo %GIT_CRED_PASS%)\n";
|
||||
std::fs::write(&path, script)
|
||||
.map_err(|e| format!("Konnte Authentifizierungsskript nicht schreiben: {e}"))?;
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
fn run_git_authenticated<I, S>(
|
||||
repo: &Path,
|
||||
args: I,
|
||||
username: &str,
|
||||
password: &str,
|
||||
) -> Result<Vec<u8>, String>
|
||||
where
|
||||
I: IntoIterator<Item = S>,
|
||||
S: AsRef<OsStr>,
|
||||
{
|
||||
let askpass = write_askpass_script()?;
|
||||
|
||||
let result = Command::new("git")
|
||||
.arg("-C")
|
||||
.arg(repo)
|
||||
.args(args)
|
||||
.env("GIT_ASKPASS", &askpass)
|
||||
.env("GIT_TERMINAL_PROMPT", "0")
|
||||
.env("GIT_CRED_USER", username)
|
||||
.env("GIT_CRED_PASS", password)
|
||||
.output()
|
||||
.map_err(|err| format!("Git konnte nicht gestartet werden: {err}"));
|
||||
|
||||
let _ = std::fs::remove_file(&askpass);
|
||||
|
||||
let output = result?;
|
||||
|
||||
if output.status.success() {
|
||||
return Ok(output.stdout);
|
||||
}
|
||||
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
let details = if !stderr.trim().is_empty() {
|
||||
stderr.trim().to_string()
|
||||
} else if !stdout.trim().is_empty() {
|
||||
stdout.trim().to_string()
|
||||
} else {
|
||||
"Unbekannter Fehler".to_string()
|
||||
};
|
||||
Err(format!("Git-Befehl fehlgeschlagen: {details}"))
|
||||
}
|
||||
|
||||
fn run_git_with_paths(
|
||||
repo: &Path,
|
||||
base_args: &[&str],
|
||||
|
||||
Reference in New Issue
Block a user