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

Use gix for cloning repositories #4708

Merged
merged 1 commit into from
Aug 19, 2024
Merged

Use gix for cloning repositories #4708

merged 1 commit into from
Aug 19, 2024

Conversation

Caleb-T-Owens
Copy link
Contributor

@Caleb-T-Owens Caleb-T-Owens commented Aug 16, 2024

Looking through my emails this morning I saw this comment from @Byron on cloning azure devops #2651 (comment) (source gix issue: Byron/gitoxide#1025) repositories not working through gix. It however made me think: Currently in GitButler, both cloning via SSH URLs and cloning private repositories doesn't work. It also got me wondering if switching to gix, despite the aforementioned issue, could be a quick and easy win that would resolve a large majority of the issues with the current setup. After trying it out, it works pretty much perfectly.

 
     #[tauri::command(async)]
     pub fn git_clone_repository(repository_url: &str, target_dir: &Path) -> Result<(), Error> {
-        git2::Repository::clone(repository_url, target_dir).context("Cloning failed")?;
+        let url =
+            gix::url::parse(repository_url.into()).context("Failed to parse repository URL")?;
+        let should_interrupt = AtomicBool::new(false);
+        let mut prepared_clone =
+            gix::prepare_clone(url, target_dir).context("Failed to prepare clone")?;
+        let (mut prepared_checkout, _) = prepared_clone
+            .fetch_then_checkout(Discard, &should_interrupt)
+            .context("Failed to fetch")?;
+        let should_interrupt = AtomicBool::new(false);
+        prepared_checkout
+            .main_worktree(Discard, &should_interrupt)
+            .context("Failed to checkout main worktree")?;
         Ok(())
     }
 }

Copy link

vercel bot commented Aug 16, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
gitbutler-web ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 16, 2024 10:46am

@Byron
Copy link
Collaborator

Byron commented Aug 16, 2024

After trying it out, it works pretty much perfectly.

It sure lures you into thinking that 😅. However, you actually have to configure something to be sure it truly works, particularly on Windows. The variable of interest here is git_installation which should be true, that way it will obtain git-installation specific configuration that usually contains all the credential helper invocations, particularly on Windows. As this has to invoke the Git binary, it can be slow (on Windows, at least), so it's not done by default, also because of the security implications.

To get clone to work, it must be done like this though.

In future, should_interrupt should probably be hooked up as well to be turned on if the user tries to abort the operation.

That should be all the gotchas I can think of right now.

@Caleb-T-Owens
Copy link
Contributor Author

It sure lures you into thinking that 😅. However, you actually have to configure something to be sure it truly works, particularly on Windows. The variable of interest here is git_installation which should be true, that way it will obtain git-installation specific configuration that usually contains all the credential helper invocations, particularly on Windows. As this has to invoke the Git binary, it can be slow (on Windows, at least), so it's not done by default, also because of the security implications.

@Byron Could you show me how to provide that argument? I'm finding the gix documentation quite hard to dig through.

@Byron
Copy link
Collaborator

Byron commented Aug 17, 2024

Actually, I was wrong, and since cloning always needs the git binary installation configuration, it will automatically use it.
No change is actually needed.

Also sorry for confusing opening a repository with cloning it - the APIs are very different and it's not obvious how to affect the open-options (but it's possible with gix::clone::PrepareFetch(…) should it be needed at some point).

Maybe the backend could check for azure-devops HTTPS URLs and use git2 for these - this would be an example URL: https://dev.azure.com/bittrance/public/_git/gitoxide, and one whose clone should currently fail with gix.

Copy link
Member

@krlvi krlvi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@Caleb-T-Owens Caleb-T-Owens merged commit 575d8a0 into master Aug 19, 2024
18 checks passed
@Caleb-T-Owens Caleb-T-Owens deleted the Use-gix-for-cloning branch August 19, 2024 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@gitbutler/desktop rust Pull requests that update Rust code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants