Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Older changes-codes for some states #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .bgit/hooks/post_git_add
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#! /usr/bin/env bash

echo "Hi"
2 changes: 1 addition & 1 deletion src/events/git_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{bgit_error::BGitError, rules::Rule};

use super::AtomicEvent;

pub(crate) struct GitAdd {
pub(crate) struct GitAdd {
name: String,
action_description: String,
pre_check_rules: Vec<Box<dyn Rule + Send + Sync>>,
Expand Down
5 changes: 4 additions & 1 deletion src/hook_executor/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ use std::os::unix::fs::PermissionsExt;


#[cfg(not(unix))]
#[allow(unused_variables)]
pub(crate) fn execute_hook_util(
pre_event_hook_path: &Path,
event_name: &str,
) -> Result<bool, Box<BGitError>> {
unimplemented!("Windows is not supported yet"); // TODO: Implement for Windows
eprintln!("TODO: Add SUPPORT is not supported yet!");
return Ok(true);
todo!("Windows is not supported yet"); // TODO: Implement for Windows
}

#[cfg(unix)]
Expand Down
53 changes: 53 additions & 0 deletions src/workflows/default/action/ta03_init_git_repo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::{
bgit_error::BGitError,
step::{ActionStep, Step, Task::ActionStepTask},
common_store::workflow_store::TASK_HAS_STASH,
};
use git2::{Repository, Error};
use std::env;
use std::path::Path;

pub(crate) struct InitGitRepo {
name: String,
}

impl ActionStep for InitGitRepo {
fn new(name: &str) -> Self
where
Self: Sized,
{
InitGitRepo {
name: name.to_owned(),
}
}

fn get_name(&self) -> &str {
&self.name
}

fn execute(&self) -> Result<Step, Box<BGitError>> {
let cwd = env::current_dir().expect("Failed to get current directory");
let path = Path::new(&cwd);

// Attempt to initialize a new Git repository in the current directory
match Repository::init(path) {
Ok(_) => {
println!("Initialized empty Git repository in {}", cwd.display());
Ok(Step::Task(ActionStepTask(Box::new(
TASK_HAS_STASH.copy_struct(),
))))
}
Err(e) => {
eprintln!("Failed to initialize Git repository: {}", e);
Err(Box::new(BGitError::new(
"Failed to initialize Git repository",
&format!("Git2 Error: {}", e),
"InitGitRepo",
0, // Replace with appropriate error code
0, // Replace with appropriate event code
0, // Replace with appropriate rule code
)))
}
}
}
}
71 changes: 71 additions & 0 deletions src/workflows/default/action/ta04_has_unstaged_files.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use crate::{
bgit_error::BGitError,
common_store::workflow_store::{TASK_ADD_FILES, TASK_COMMIT_CHANGES},
step::{ActionStep, Step, Task::ActionStepTask},
};
use git2::{Repository, StatusOptions};
use std::env;

pub(crate) struct HasUnstagedFiles {
name: String,
}

impl ActionStep for HasUnstagedFiles {
fn new(name: &str) -> Self
where
Self: Sized,
{
HasUnstagedFiles {
name: name.to_owned(),
}
}

fn get_name(&self) -> &str {
&self.name
}

fn execute(&self) -> Result<Step, Box<BGitError>> {
let cwd = env::current_dir().expect("Failed to get current directory");
let repo = match Repository::discover(cwd) {
Ok(repo) => repo,
Err(e) => {
return Err(Box::new(BGitError::new(
"Failed to open Git repository",
&format!("Git2 Error: {}", e),
"HasUnstagedFiles",
0, // Replace with appropriate error code
0, // Replace with appropriate event code
0, // Replace with appropriate rule code
)));
}
};

let mut status_opts = StatusOptions::new();
status_opts.include_untracked(true).recurse_untracked_dirs(true);
let statuses = match repo.statuses(Some(&mut status_opts)) {
Ok(statuses) => statuses,
Err(e) => {
return Err(Box::new(BGitError::new(
"Failed to get repository status",
&format!("Git2 Error: {}", e),
"HasUnstagedFiles",
0, // Replace with appropriate error code
0, // Replace with appropriate event code
0, // Replace with appropriate rule code
)));
}
};

if statuses.is_empty() {
println!("No unstaged files detected.");
Ok(Step::Task(ActionStepTask(Box::new(
TASK_COMMIT_CHANGES.copy_struct(),
))))
} else {
println!("Unstaged files detected.");
Ok(Step::Task(ActionStepTask(Box::new(
TASK_ADD_FILES.copy_struct(),
))))
}
}
}
79 changes: 79 additions & 0 deletions src/workflows/default/action/ta05_has_uncommited_changes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use crate::{
bgit_error::BGitError,
common_store::workflow_store::{TASK_COMMIT_CHANGES, TASK_PUSH_CHANGES},
step::{ActionStep, Step, Task::ActionStepTask},
};
use git2::{Repository, StatusOptions};
use std::env;

// Define some constants for error codes, event codes, and rule codes
const ERROR_CODE_REPO_OPEN: u32 = 1001;
const ERROR_CODE_REPO_STATUS: u32 = 1002;

const EVENT_CODE_UNCOMMITTED_CHECK: u32 = 2002;

const RULE_CODE_STATUS_CHECK: u32 = 3002;

pub(crate) struct HasUncommittedChanges {
name: String,
}

impl ActionStep for HasUncommittedChanges {
fn new(name: &str) -> Self
where
Self: Sized,
{
HasUncommittedChanges {
name: name.to_owned(),
}
}

fn get_name(&self) -> &str {
&self.name
}

fn execute(&self) -> Result<Step, Box<BGitError>> {
let cwd = env::current_dir().expect("Failed to get current directory");
let repo = match Repository::discover(cwd) {
Ok(repo) => repo,
Err(e) => {
return Err(Box::new(BGitError::new(
"Failed to open Git repository",
&format!("Git2 Error: {}", e),
"HasUncommittedChanges",
ERROR_CODE_REPO_OPEN,
EVENT_CODE_UNCOMMITTED_CHECK,
RULE_CODE_STATUS_CHECK,
)));
}
};

let mut status_opts = StatusOptions::new();
status_opts.include_untracked(false);
let statuses = match repo.statuses(Some(&mut status_opts)) {
Ok(statuses) => statuses,
Err(e) => {
return Err(Box::new(BGitError::new(
"Failed to get repository status",
&format!("Git2 Error: {}", e),
"HasUncommittedChanges",
ERROR_CODE_REPO_STATUS,
EVENT_CODE_UNCOMMITTED_CHECK,
RULE_CODE_STATUS_CHECK,
)));
}
};

if statuses.is_empty() {
println!("No uncommitted changes detected.");
Ok(Step::Task(ActionStepTask(Box::new(
TASK_PUSH_CHANGES.copy_struct(),
))))
} else {
println!("Uncommitted changes detected.");
Ok(Step::Task(ActionStepTask(Box::new(
TASK_COMMIT_CHANGES.copy_struct(),
))))
}
}
}
92 changes: 92 additions & 0 deletions src/workflows/default/action/ta06_add_all.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
use crate::{
bgit_error::BGitError,
common_store::workflow_store::TASK_COMMIT_CHANGES,
step::{ActionStep, Step, Task::ActionStepTask},
};
use git2::{Repository, IndexAddOption};
use std::env;

// Define some constants for error codes, event codes, and rule codes
const ERROR_CODE_REPO_OPEN: u32 = 1001;
const ERROR_CODE_ADD_FILES: u32 = 1003;

const EVENT_CODE_ADD_ALL: u32 = 2003;

const RULE_CODE_ADD_FILES: u32 = 3003;

pub(crate) struct AddAllFiles {
name: String,
}

impl ActionStep for AddAllFiles {
fn new(name: &str) -> Self
where
Self: Sized,
{
AddAllFiles {
name: name.to_owned(),
}
}

fn get_name(&self) -> &str {
&self.name
}

fn execute(&self) -> Result<Step, Box<BGitError>> {
let cwd = env::current_dir().expect("Failed to get current directory");
let repo = match Repository::discover(cwd) {
Ok(repo) => repo,
Err(e) => {
return Err(Box::new(BGitError::new(
"Failed to open Git repository",
&format!("Git2 Error: {}", e),
"AddAllFiles",
ERROR_CODE_REPO_OPEN,
EVENT_CODE_ADD_ALL,
RULE_CODE_ADD_FILES,
)));
}
};

let mut index = match repo.index() {
Ok(index) => index,
Err(e) => {
return Err(Box::new(BGitError::new(
"Failed to get repository index",
&format!("Git2 Error: {}", e),
"AddAllFiles",
ERROR_CODE_ADD_FILES,
EVENT_CODE_ADD_ALL,
RULE_CODE_ADD_FILES,
)));
}
};

if let Err(e) = index.add_all(["*"].iter(), IndexAddOption::DEFAULT, None) {
return Err(Box::new(BGitError::new(
"Failed to add files to staging",
&format!("Git2 Error: {}", e),
"AddAllFiles",
ERROR_CODE_ADD_FILES,
EVENT_CODE_ADD_ALL,
RULE_CODE_ADD_FILES,
)));
}

if let Err(e) = index.write() {
return Err(Box::new(BGitError::new(
"Failed to write index",
&format!("Git2 Error: {}", e),
"AddAllFiles",
ERROR_CODE_ADD_FILES,
EVENT_CODE_ADD_ALL,
RULE_CODE_ADD_FILES,
)));
}

println!("All unstaged files have been added to the staging area.");
Ok(Step::Task(ActionStepTask(Box::new(
TASK_COMMIT_CHANGES.copy_struct(),
))))
}
}
Loading
Loading