1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::process::Command;

use crate::{config::Config, paths::get_share_path};

pub fn git_save() {
    Command::new("git").args(["add", "."]).output().unwrap();
    Command::new("git")
        .args(["commit", "-m", "save"])
        .output()
        .unwrap();

    if Config::load().unwrap().git_remote.is_some() {
        Command::new("git")
            .args(["push", "-u", "origin", "main"])
            .output()
            .unwrap();
    }
}

pub fn git_pull() {
    Command::new("git").args(["pull"]).output().unwrap();
}

pub fn git_stuff(git_remote: &Option<String>) {
    std::env::set_current_dir(get_share_path()).unwrap();

    // Initiate git
    Command::new("git").arg("init").output().unwrap();

    if let Some(git_remote) = git_remote {
        // Check if the remote repository is already set
        let remote_check_output = Command::new("git")
            .args(["remote", "get-url", "origin"])
            .output()
            .unwrap();

        if remote_check_output.status.success() {
            git_pull();
        } else {
            // Set the remote repository
            Command::new("git")
                .args(["remote", "add", "origin", git_remote])
                .output()
                .unwrap();
        }
    }

    git_save();
}