Skip to content

Commit

Permalink
implement git fast-forward
Browse files Browse the repository at this point in the history
in the hurry to complete the application I forgot to implement the git fastforward feature, due to this the repo would never sync with the remote, thus an incomplete pull function

git pull => git fetch + git fastforward / merge

this commit essentially completes the project
  • Loading branch information
theinhumaneme committed Oct 15, 2024
1 parent 2b32821 commit 0d1de76
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 20 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "autodeploy"
version = "1.0.0"
version = "1.2.0"
edition = "2021"
authors = ["Kalyan Mudumby"]
description = "Configurable custom wrapper over git for quick and hassle free deployments"
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ fn main() {
repo_url,
&repository_path,
)
} else if pull_repository(&git_username, &git_password, &repository_path) {
println!("All branches have been fetched and updated successfully.");
} else {
pull_repository(&git_username, &git_password, &repository_path);
println!("Pulling the repository failed");
panic!();
}
let branch = prompt_branch_selection(&repository_path);
if let Some(value) = branch {
Expand Down
9 changes: 0 additions & 9 deletions src/utils/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ pub fn build_compose(compose_file_path: String) {
let args = ["compose", "-f", compose_file_path.as_str(), "build"];
if check_file(compose_file_path.clone()) {
execute_command(command, args.to_vec());
println!("Building Application Complete");
} else {
println!("Building Application Failed");
}
}

Expand All @@ -84,9 +81,6 @@ pub fn start_compose(compose_file_path: String, project: String) {
];
if check_file(compose_file_path.clone()) {
execute_command(command, args.to_vec());
println!("Application Started");
} else {
println!("Failed to start application");
}
}
pub fn stop_compose(compose_file_path: String, project: String) {
Expand All @@ -101,9 +95,6 @@ pub fn stop_compose(compose_file_path: String, project: String) {
];
if check_file(compose_file_path.clone()) {
execute_command(command, args.to_vec());
println!("Application Stopped");
} else {
println!("Failed to stop the application");
}
}
pub fn restart_compose(compose_file_path: String, project: String) {
Expand Down
53 changes: 47 additions & 6 deletions src/utils/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn check_repository(path: &Path) -> bool {
}
}
/// handle the user choice and clone the repo as required\
/// wrappper around the git2 library
/// wrapper around the git2 library
pub fn prompt_clone_repository(
git_username: &str,
git_password: &str,
Expand Down Expand Up @@ -68,9 +68,48 @@ pub fn pull_repository(git_username: &str, git_password: &str, repository_path:

// Fetch all branches from the remote
let fetch_status = remote.fetch(&["refs/heads/*:refs/remotes/origin/*"], Some(&mut fo), None);
let pull_status = fetch_status.is_ok();
println!("All branches have been fetched and updated successfully.");
pull_status
if fetch_status.is_err() {
println!("Failed to fetch from remote: {:?}", fetch_status.err());
return false;
}
let branches = repo.branches(Some(BranchType::Local)).unwrap();

for branch_result in branches {
let (branch, branch_type) = branch_result.unwrap();
if branch_type == BranchType::Local {
let branch_name = branch.name().unwrap().unwrap_or("<unknown>");

// Find the corresponding remote-tracking branch
let upstream_name = format!("refs/remotes/{}", branch_name);
if let Ok(upstream) = repo.find_reference(&upstream_name) {
let upstream_commit = upstream.peel_to_commit().unwrap();
let local_commit = branch.get().peel_to_commit().unwrap();

// Check if the local branch is behind the remote
if local_commit.id() != upstream_commit.id()
&& repo
.graph_descendant_of(upstream_commit.id(), local_commit.id())
.unwrap()
{
println!("Fast-forwarding branch: {}", branch_name);

// Fast-forward the branch
let mut branch_ref = branch.into_reference();
branch_ref
.set_target(upstream_commit.id(), "Fast-forwarding")
.unwrap();
} else {
println!(
"Branch '{}' is up-to-date or cannot be fast-forwarded.",
branch_name
);
}
} else {
println!("No upstream branch found for '{}'", branch_name);
}
}
}
true
}

/// prompt the user to select a branch.
Expand Down Expand Up @@ -137,12 +176,14 @@ pub fn branch_checkout(repository_path: &str, branch_selection: String) {
let commit = repo.find_commit(remote_branch_commit.id()).unwrap();

if let Ok(local_branch) = repo.find_branch(branch_selection.as_str(), BranchType::Local) {
// Optionally, you could choose to checkout this branch if needed:
// Checkout the existing local branch
repo.set_head(local_branch.get().name().unwrap()).unwrap();
} else {
// Create a new local branch that tracks the remote branch
repo.branch(branch_selection.as_str(), &commit, true)
let local_branch = repo
.branch(branch_selection.as_str(), &commit, false)
.unwrap();
repo.set_head(local_branch.get().name().unwrap()).unwrap();
}

// Checkout the new local branch
Expand Down

0 comments on commit 0d1de76

Please sign in to comment.