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

fetch/prune branches #1000

Merged
merged 8 commits into from
Nov 23, 2021
Merged
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
77 changes: 77 additions & 0 deletions asyncgit/src/fetch_job.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//!

use crate::{
asyncjob::{AsyncJob, RunParams},
error::Result,
sync::cred::BasicAuthCredential,
sync::remotes::fetch_all,
AsyncGitNotification, ProgressPercent, CWD,
};

use std::sync::{Arc, Mutex};

enum JobState {
Request(Option<BasicAuthCredential>),
Response(Result<()>),
}

///
#[derive(Clone, Default)]
pub struct AsyncFetchJob {
state: Arc<Mutex<Option<JobState>>>,
}

///
impl AsyncFetchJob {
///
pub fn new(
basic_credential: Option<BasicAuthCredential>,
) -> Self {
Self {
state: Arc::new(Mutex::new(Some(JobState::Request(
basic_credential,
)))),
}
}

///
pub fn result(&self) -> Option<Result<()>> {
if let Ok(mut state) = self.state.lock() {
if let Some(state) = state.take() {
return match state {
JobState::Request(_) => None,
JobState::Response(result) => Some(result),
};
}
}

None
}
}

impl AsyncJob for AsyncFetchJob {
type Notification = AsyncGitNotification;
type Progress = ProgressPercent;

fn run(
&mut self,
_params: RunParams<Self::Notification, Self::Progress>,
) -> Result<Self::Notification> {
if let Ok(mut state) = self.state.lock() {
*state = state.take().map(|state| match state {
JobState::Request(basic_credentials) => {
//TODO: support progress
let result =
fetch_all(CWD, &basic_credentials, &None);

JobState::Response(result)
}
JobState::Response(result) => {
JobState::Response(result)
}
});
}

Ok(AsyncGitNotification::Fetch)
}
}
10 changes: 7 additions & 3 deletions asyncgit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ pub mod cached;
mod commit_files;
mod diff;
mod error;
mod fetch;
mod fetch_job;
mod progress;
mod pull;
mod push;
mod push_tags;
pub mod remote_progress;
Expand All @@ -44,8 +45,9 @@ pub use crate::{
commit_files::{AsyncCommitFiles, CommitFilesParams},
diff::{AsyncDiff, DiffParams, DiffType},
error::{Error, Result},
fetch::{AsyncFetch, FetchRequest},
fetch_job::AsyncFetchJob,
progress::ProgressPercent,
pull::{AsyncPull, FetchRequest},
push::{AsyncPush, PushRequest},
push_tags::{AsyncPushTags, PushTagsRequest},
remote_progress::{RemoteProgress, RemoteProgressState},
Expand Down Expand Up @@ -83,11 +85,13 @@ pub enum AsyncGitNotification {
///
PushTags,
///
Fetch,
Pull,
///
Blame,
///
RemoteTags,
///
Fetch,
}

/// current working directory `./`
Expand Down
8 changes: 4 additions & 4 deletions asyncgit/src/fetch.rs → asyncgit/src/pull.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ pub struct FetchRequest {
struct FetchState {}

///
pub struct AsyncFetch {
pub struct AsyncPull {
state: Arc<Mutex<Option<FetchState>>>,
last_result: Arc<Mutex<Option<(usize, String)>>>,
progress: Arc<Mutex<Option<ProgressNotification>>>,
sender: Sender<AsyncGitNotification>,
}

impl AsyncFetch {
impl AsyncPull {
///
pub fn new(sender: &Sender<AsyncGitNotification>) -> Self {
Self {
Expand Down Expand Up @@ -84,7 +84,7 @@ impl AsyncFetch {
let (progress_sender, receiver) = unbounded();

let handle = RemoteProgress::spawn_receiver_thread(
AsyncGitNotification::Fetch,
AsyncGitNotification::Pull,
sender.clone(),
receiver,
arc_progress,
Expand All @@ -108,7 +108,7 @@ impl AsyncFetch {
Self::clear_request(&arc_state).expect("clear error");

sender
.send(AsyncGitNotification::Fetch)
.send(AsyncGitNotification::Pull)
.expect("AsyncNotification error");
});

Expand Down
59 changes: 57 additions & 2 deletions asyncgit/src/sync/remotes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
cred::BasicAuthCredential,
remotes::push::ProgressNotification, utils,
},
ProgressPercent,
};
use crossbeam_channel::Sender;
use git2::{BranchType, FetchOptions, Repository};
Expand Down Expand Up @@ -75,14 +76,68 @@ pub(crate) fn get_default_remote_in_repo(
Err(Error::NoDefaultRemoteFound)
}

/// fetches from upstream/remote for `branch`
///
fn fetch_from_remote(
repo_path: &str,
remote: &str,
basic_credential: Option<BasicAuthCredential>,
progress_sender: Option<Sender<ProgressNotification>>,
) -> Result<()> {
let repo = utils::repo(repo_path)?;

let mut remote = repo.find_remote(remote)?;

let mut options = FetchOptions::new();
let callbacks = Callbacks::new(progress_sender, basic_credential);
options.prune(git2::FetchPrune::On);
options.remote_callbacks(callbacks.callbacks());
remote.fetch(&[] as &[&str], Some(&mut options), None)?;

Ok(())
}

/// updates/prunes all branches from all remotes
pub fn fetch_all(
repo_path: &str,
basic_credential: &Option<BasicAuthCredential>,
progress_sender: &Option<Sender<ProgressPercent>>,
) -> Result<()> {
scope_time!("fetch_all");

let repo = utils::repo(repo_path)?;
let remotes = repo
.remotes()?
.iter()
.flatten()
.map(String::from)
.collect::<Vec<_>>();
let remotes_count = remotes.len();

for (idx, remote) in remotes.into_iter().enumerate() {
fetch_from_remote(
repo_path,
&remote,
basic_credential.clone(),
None,
)?;

if let Some(sender) = progress_sender {
let progress = ProgressPercent::new(idx, remotes_count);
sender.send(progress)?;
}
}

Ok(())
}

/// fetches from upstream/remote for local `branch`
pub(crate) fn fetch(
repo_path: &str,
branch: &str,
basic_credential: Option<BasicAuthCredential>,
progress_sender: Option<Sender<ProgressNotification>>,
) -> Result<usize> {
scope_time!("fetch_origin");
scope_time!("fetch");

let repo = utils::repo(repo_path)?;
let branch_ref = repo
Expand Down
30 changes: 25 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use crate::{
BranchListComponent, CommandBlocking, CommandInfo,
CommitComponent, CompareCommitsComponent, Component,
ConfirmComponent, CreateBranchComponent, DrawableComponent,
ExternalEditorComponent, FileFindPopup, HelpComponent,
InspectCommitComponent, MsgComponent, OptionsPopupComponent,
PullComponent, PushComponent, PushTagsComponent,
RenameBranchComponent, RevisionFilesPopup, SharedOptions,
StashMsgComponent, TagCommitComponent, TagListComponent,
ExternalEditorComponent, FetchComponent, FileFindPopup,
HelpComponent, InspectCommitComponent, MsgComponent,
OptionsPopupComponent, PullComponent, PushComponent,
PushTagsComponent, RenameBranchComponent, RevisionFilesPopup,
SharedOptions, StashMsgComponent, TagCommitComponent,
TagListComponent,
},
input::{Input, InputEvent, InputState},
keys::{KeyConfig, SharedKeyConfig},
Expand Down Expand Up @@ -55,6 +56,7 @@ pub struct App {
push_popup: PushComponent,
push_tags_popup: PushTagsComponent,
pull_popup: PullComponent,
fetch_popup: FetchComponent,
tag_commit_popup: TagCommitComponent,
create_branch_popup: CreateBranchComponent,
rename_branch_popup: RenameBranchComponent,
Expand Down Expand Up @@ -158,6 +160,12 @@ impl App {
theme.clone(),
key_config.clone(),
),
fetch_popup: FetchComponent::new(
&queue,
sender,
theme.clone(),
key_config.clone(),
),
tag_commit_popup: TagCommitComponent::new(
queue.clone(),
theme.clone(),
Expand Down Expand Up @@ -389,6 +397,7 @@ impl App {
self.push_popup.update_git(ev)?;
self.push_tags_popup.update_git(ev)?;
self.pull_popup.update_git(ev);
self.fetch_popup.update_git(ev);
self.select_branch_popup.update_git(ev)?;
}

Expand Down Expand Up @@ -421,6 +430,7 @@ impl App {
|| self.push_popup.any_work_pending()
|| self.push_tags_popup.any_work_pending()
|| self.pull_popup.any_work_pending()
|| self.fetch_popup.any_work_pending()
|| self.revision_files_popup.any_work_pending()
|| self.tags_popup.any_work_pending()
}
Expand Down Expand Up @@ -453,6 +463,7 @@ impl App {
push_popup,
push_tags_popup,
pull_popup,
fetch_popup,
tag_commit_popup,
create_branch_popup,
rename_branch_popup,
Expand Down Expand Up @@ -489,6 +500,7 @@ impl App {
push_popup,
push_tags_popup,
pull_popup,
fetch_popup,
options_popup,
reset,
msg
Expand Down Expand Up @@ -696,6 +708,14 @@ impl App {
}
flags.insert(NeedsUpdate::ALL);
}
InternalEvent::FetchRemotes => {
if let Err(error) = self.fetch_popup.fetch() {
self.queue.push(InternalEvent::ShowErrorMsg(
error.to_string(),
));
}
flags.insert(NeedsUpdate::ALL);
}
InternalEvent::PushTags => {
self.push_tags_popup.push_tags()?;
flags.insert(NeedsUpdate::ALL);
Expand Down
8 changes: 8 additions & 0 deletions src/components/branchlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ impl Component for BranchListComponent {
true,
self.local,
));

out.push(CommandInfo::new(
strings::commands::fetch_remotes(&self.key_config),
true,
!self.local,
));
}
visibility_blocking(self)
}
Expand Down Expand Up @@ -290,6 +296,8 @@ impl Component for BranchListComponent {
self.queue
.push(InternalEvent::CompareCommits(b, None));
}
} else if e == self.key_config.keys.pull && !self.local {
self.queue.push(InternalEvent::FetchRemotes);
} else if e == self.key_config.keys.cmd_bar_toggle {
//do not consume if its the more key
return Ok(EventState::NotConsumed);
Expand Down
Loading