speki_backend/
git.rs

1use std::process::Command;
2
3use crate::{config::Config, paths::get_share_path};
4
5pub fn git_save() {
6    Command::new("git").args(["add", "."]).output().unwrap();
7    Command::new("git")
8        .args(["commit", "-m", "save"])
9        .output()
10        .unwrap();
11
12    if Config::load().unwrap().git_remote.is_some() {
13        Command::new("git")
14            .args(["push", "-u", "origin", "main"])
15            .output()
16            .unwrap();
17    }
18}
19
20pub fn git_pull() {
21    Command::new("git").args(["pull"]).output().unwrap();
22}
23
24pub fn git_stuff(git_remote: &Option<String>) {
25    std::env::set_current_dir(get_share_path()).unwrap();
26
27    // Initiate git
28    Command::new("git").arg("init").output().unwrap();
29
30    if let Some(git_remote) = git_remote {
31        // Check if the remote repository is already set
32        let remote_check_output = Command::new("git")
33            .args(["remote", "get-url", "origin"])
34            .output()
35            .unwrap();
36
37        if remote_check_output.status.success() {
38            git_pull();
39        } else {
40            // Set the remote repository
41            Command::new("git")
42                .args(["remote", "add", "origin", git_remote])
43                .output()
44                .unwrap();
45        }
46    }
47
48    git_save();
49}