-
-
Notifications
You must be signed in to change notification settings - Fork 689
Git branch flow improvements #370
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8ac0d2e
Port git branch operations to use git binary
gschier 932ee40
Moved some git operations around
gschier ca655af
Branch renaming
gschier d40d8d9
Enhance Git dropdown with branch operations and submenu improvements
gschier 202141c
Fix dropdown selection skipping hidden items on open
gschier 1ebd7c1
Add git clone functionality with new workspace creation flow
gschier cd1c432
Add credential prompts for git clone and improve workspace import
gschier 1acd16d
Move clone credential logic to git package for consistency
gschier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,99 +1,153 @@ | ||
| use serde::{Deserialize, Serialize}; | ||
| use ts_rs::TS; | ||
|
|
||
| use crate::binary::new_binary_command; | ||
| use crate::error::Error::GenericError; | ||
| use crate::error::Result; | ||
| use crate::merge::do_merge; | ||
| use crate::repository::open_repo; | ||
| use crate::util::{bytes_to_string, get_branch_by_name, get_current_branch}; | ||
| use git2::BranchType; | ||
| use git2::build::CheckoutBuilder; | ||
| use log::info; | ||
| use std::path::Path; | ||
|
|
||
| pub fn git_checkout_branch(dir: &Path, branch_name: &str, force: bool) -> Result<String> { | ||
| if branch_name.starts_with("origin/") { | ||
| return git_checkout_remote_branch(dir, branch_name, force); | ||
| } | ||
| #[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)] | ||
| #[serde(rename_all = "snake_case", tag = "type")] | ||
| #[ts(export, export_to = "gen_git.ts")] | ||
| pub enum BranchDeleteResult { | ||
| Success { message: String }, | ||
| NotFullyMerged, | ||
| } | ||
|
|
||
| let repo = open_repo(dir)?; | ||
| let branch = get_branch_by_name(&repo, branch_name)?; | ||
| let branch_ref = branch.into_reference(); | ||
| let branch_tree = branch_ref.peel_to_tree()?; | ||
| pub async fn git_checkout_branch(dir: &Path, branch_name: &str, force: bool) -> Result<String> { | ||
| let branch_name = branch_name.trim_start_matches("origin/"); | ||
|
|
||
| let mut options = CheckoutBuilder::default(); | ||
| let mut args = vec!["checkout"]; | ||
| if force { | ||
| options.force(); | ||
| args.push("--force"); | ||
| } | ||
| args.push(branch_name); | ||
|
|
||
| repo.checkout_tree(branch_tree.as_object(), Some(&mut options))?; | ||
| repo.set_head(branch_ref.name().unwrap())?; | ||
| let out = new_binary_command(dir) | ||
| .await? | ||
| .args(&args) | ||
| .output() | ||
| .await | ||
| .map_err(|e| GenericError(format!("failed to run git checkout: {e}")))?; | ||
|
|
||
| let stdout = String::from_utf8_lossy(&out.stdout); | ||
| let stderr = String::from_utf8_lossy(&out.stderr); | ||
| let combined = format!("{}{}", stdout, stderr); | ||
|
|
||
| if !out.status.success() { | ||
| return Err(GenericError(format!("Failed to checkout: {}", combined.trim()))); | ||
| } | ||
|
|
||
| Ok(branch_name.to_string()) | ||
| } | ||
|
|
||
| pub(crate) fn git_checkout_remote_branch( | ||
| dir: &Path, | ||
| branch_name: &str, | ||
| force: bool, | ||
| ) -> Result<String> { | ||
| let branch_name = branch_name.trim_start_matches("origin/"); | ||
| let repo = open_repo(dir)?; | ||
| pub async fn git_create_branch(dir: &Path, name: &str, base: Option<&str>) -> Result<()> { | ||
| let mut cmd = new_binary_command(dir).await?; | ||
| cmd.arg("branch").arg(name); | ||
| if let Some(base_branch) = base { | ||
| cmd.arg(base_branch); | ||
| } | ||
|
|
||
| let out = | ||
| cmd.output().await.map_err(|e| GenericError(format!("failed to run git branch: {e}")))?; | ||
|
|
||
| let refname = format!("refs/remotes/origin/{}", branch_name); | ||
| let remote_ref = repo.find_reference(&refname)?; | ||
| let commit = remote_ref.peel_to_commit()?; | ||
| let stdout = String::from_utf8_lossy(&out.stdout); | ||
| let stderr = String::from_utf8_lossy(&out.stderr); | ||
| let combined = format!("{}{}", stdout, stderr); | ||
|
|
||
| let mut new_branch = repo.branch(branch_name, &commit, false)?; | ||
| let upstream_name = format!("origin/{}", branch_name); | ||
| new_branch.set_upstream(Some(&upstream_name))?; | ||
| if !out.status.success() { | ||
| return Err(GenericError(format!("Failed to create branch: {}", combined.trim()))); | ||
| } | ||
|
|
||
| git_checkout_branch(dir, branch_name, force) | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn git_create_branch(dir: &Path, name: &str) -> Result<()> { | ||
| let repo = open_repo(dir)?; | ||
| let head = match repo.head() { | ||
| Ok(h) => h, | ||
| Err(e) if e.code() == git2::ErrorCode::UnbornBranch => { | ||
| let msg = "Cannot create branch when there are no commits"; | ||
| return Err(GenericError(msg.into())); | ||
| } | ||
| Err(e) => return Err(e.into()), | ||
| }; | ||
| let head = head.peel_to_commit()?; | ||
| pub async fn git_delete_branch(dir: &Path, name: &str, force: bool) -> Result<BranchDeleteResult> { | ||
| let mut cmd = new_binary_command(dir).await?; | ||
|
|
||
| repo.branch(name, &head, false)?; | ||
| let out = | ||
| if force { cmd.args(["branch", "-D", name]) } else { cmd.args(["branch", "-d", name]) } | ||
| .output() | ||
| .await | ||
| .map_err(|e| GenericError(format!("failed to run git branch -d: {e}")))?; | ||
|
|
||
| let stdout = String::from_utf8_lossy(&out.stdout); | ||
| let stderr = String::from_utf8_lossy(&out.stderr); | ||
| let combined = format!("{}{}", stdout, stderr); | ||
|
|
||
| if combined.contains("not fully merged") { | ||
| return Ok(BranchDeleteResult::NotFullyMerged); | ||
| } | ||
|
|
||
| if !out.status.success() { | ||
| return Err(GenericError(format!("Failed to delete branch: {}", combined.trim()))); | ||
| } | ||
|
|
||
| Ok(BranchDeleteResult::Success { message: combined }) | ||
| } | ||
|
|
||
| pub async fn git_merge_branch(dir: &Path, name: &str) -> Result<()> { | ||
| let out = new_binary_command(dir) | ||
| .await? | ||
| .args(["merge", name]) | ||
| .output() | ||
| .await | ||
| .map_err(|e| GenericError(format!("failed to run git merge: {e}")))?; | ||
|
|
||
| let stdout = String::from_utf8_lossy(&out.stdout); | ||
| let stderr = String::from_utf8_lossy(&out.stderr); | ||
| let combined = format!("{}{}", stdout, stderr); | ||
|
|
||
| if !out.status.success() { | ||
| // Check for merge conflicts | ||
| if combined.to_lowercase().contains("conflict") { | ||
| return Err(GenericError( | ||
| "Merge conflicts detected. Please resolve them manually.".to_string(), | ||
| )); | ||
| } | ||
| return Err(GenericError(format!("Failed to merge: {}", combined.trim()))); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn git_delete_branch(dir: &Path, name: &str) -> Result<()> { | ||
| let repo = open_repo(dir)?; | ||
| let mut branch = get_branch_by_name(&repo, name)?; | ||
| pub async fn git_delete_remote_branch(dir: &Path, name: &str) -> Result<()> { | ||
| // Remote branch names come in as "origin/branch-name", extract the branch name | ||
| let branch_name = name.trim_start_matches("origin/"); | ||
|
|
||
| if branch.is_head() { | ||
| info!("Deleting head branch"); | ||
| let branches = repo.branches(Some(BranchType::Local))?; | ||
| let other_branch = branches.into_iter().filter_map(|b| b.ok()).find(|b| !b.0.is_head()); | ||
| let other_branch = match other_branch { | ||
| None => return Err(GenericError("Cannot delete only branch".into())), | ||
| Some(b) => bytes_to_string(b.0.name_bytes()?)?, | ||
| }; | ||
| let out = new_binary_command(dir) | ||
| .await? | ||
| .args(["push", "origin", "--delete", branch_name]) | ||
| .output() | ||
| .await | ||
| .map_err(|e| GenericError(format!("failed to run git push --delete: {e}")))?; | ||
|
|
||
| git_checkout_branch(dir, &other_branch, true)?; | ||
| } | ||
| let stdout = String::from_utf8_lossy(&out.stdout); | ||
| let stderr = String::from_utf8_lossy(&out.stderr); | ||
| let combined = format!("{}{}", stdout, stderr); | ||
|
|
||
| branch.delete()?; | ||
| if !out.status.success() { | ||
| return Err(GenericError(format!("Failed to delete remote branch: {}", combined.trim()))); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| pub fn git_merge_branch(dir: &Path, name: &str, _force: bool) -> Result<()> { | ||
| let repo = open_repo(dir)?; | ||
| let local_branch = get_current_branch(&repo)?.unwrap(); | ||
| pub async fn git_rename_branch(dir: &Path, old_name: &str, new_name: &str) -> Result<()> { | ||
| let out = new_binary_command(dir) | ||
| .await? | ||
| .args(["branch", "-m", old_name, new_name]) | ||
| .output() | ||
| .await | ||
| .map_err(|e| GenericError(format!("failed to run git branch -m: {e}")))?; | ||
|
|
||
| let commit_to_merge = get_branch_by_name(&repo, name)?.into_reference(); | ||
| let commit_to_merge = repo.reference_to_annotated_commit(&commit_to_merge)?; | ||
| let stdout = String::from_utf8_lossy(&out.stdout); | ||
| let stderr = String::from_utf8_lossy(&out.stderr); | ||
| let combined = format!("{}{}", stdout, stderr); | ||
|
|
||
| do_merge(&repo, &local_branch, &commit_to_merge)?; | ||
| if !out.status.success() { | ||
| return Err(GenericError(format!("Failed to rename branch: {}", combined.trim()))); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.