cli: Don't sync on git-fetch

It's actually not such a great idea to sync on git-fetch and
git-ls-remote, as syncing is supposed to happen in the background
already, and if you want to force a sync, you can just call `rad sync`.

Previously, it would be annoying that when you called `rad sync` and
then `git fetch`, it would still attempt another sync, for example.

Moving forwards, it might make sense to call `git-fetch` on `rad sync`
(so the other way around), because `git-fetch` is free (it doesn't talk
to peers), and you almost always want your working copy up to date with
your storage.
This commit is contained in:
cloudhead 2023-12-11 12:01:05 +01:00
parent 63469fd3bc
commit 9ada30255b
No known key found for this signature in database
5 changed files with 3 additions and 89 deletions

View File

@ -13,7 +13,6 @@ f2de534b5e81d7c6e2dcaf58c3dd91573c0a0354 refs/heads/master
``` (stderr)
$ git fetch alice@z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
✓ Synced with 1 peer(s)
From rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi
* [new branch] alice/1 -> alice@z6MknSLrJoTcukLrE435hVNQT4JUhbvWLX4kUzqkEStBU8Vi/alice/1
```

View File

@ -55,7 +55,6 @@ Bob fetches these new changes and can see their branch is now behind:
``` ~bob (stderr)
$ git fetch rad
✓ Synced with 1 peer(s)
From rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji
3e674d1..27857ec patches/6ff4f09c1b5a81347981f59b02ef43a31a07cdae -> rad/patches/6ff4f09c1b5a81347981f59b02ef43a31a07cdae
```

View File

@ -50,7 +50,6 @@ $ git push
``` ~alice (stderr)
$ git checkout master -q
$ git pull
✓ Synced with 1 peer(s)
From rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji
+ c2aaf1c...8d5f1ba patches/0fd67a0364af1f79ed8770a35ed09d85571d4c21 -> rad/patches/0fd67a0364af1f79ed8770a35ed09d85571d4c21 (forced update)
$ git checkout - -q
@ -71,7 +70,6 @@ f2de534b5e81d7c6e2dcaf58c3dd91573c0a0354 branch 'master' of rad://z42hL2jL4XNk6
``` ~bob (stderr)
$ git checkout master -q
$ git pull
✓ Synced with 1 peer(s)
From rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji
+ c2aaf1c...8d5f1ba patches/0fd67a0364af1f79ed8770a35ed09d85571d4c21 -> rad/patches/0fd67a0364af1f79ed8770a35ed09d85571d4c21 (forced update)
```

View File

@ -15,11 +15,13 @@ peer. Upcoming versions of radicle will not require this step.
$ rad remote add z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk --name bob
✓ Remote bob added
✓ Remote-tracking branch bob/master created for z6Mkt67…v4N1tRk
$ rad sync -f
✓ Fetching rad:z42hL2jL4XNk6K8oHQaSWfMgCL7ji from z6Mkt67…v4N1tRk..
✓ Fetched repository from 1 seed(s)
```
``` (stderr)
$ git fetch bob
✓ Synced with 1 peer(s)
From rad://z42hL2jL4XNk6K8oHQaSWfMgCL7ji/z6Mkt67GdsW7715MEfRuP4pSZxJRJh6kj6Y48WRqVv4N1tRk
* [new branch] patches/3581e83ad18f5cdd806ab50fa11cfd5dd4e8ae1c -> bob/patches/3581e83ad18f5cdd806ab50fa11cfd5dd4e8ae1c
```

View File

@ -7,19 +7,14 @@ mod fetch;
mod list;
mod push;
use std::io::IsTerminal as _;
use std::path::PathBuf;
use std::str::FromStr;
use std::time;
use std::{env, io};
use radicle_cli::git::Rev;
use thiserror::Error;
use radicle::git;
use radicle::node::FetchResults;
use radicle::node::Handle as _;
use radicle::prelude::Id;
use radicle::storage::git::transport::local::{Url, UrlError};
use radicle::storage::{ReadRepository, WriteStorage};
use radicle_cli::terminal as cli;
@ -119,7 +114,6 @@ pub fn run(profile: radicle::Profile) -> Result<(), Error> {
let stdin = io::stdin();
let mut line = String::new();
let mut opts = Options::default();
let mut sync = SyncState::new(url.repo);
loop {
let tokens = read_line(&stdin, &mut line)?;
@ -155,8 +149,6 @@ pub fn run(profile: radicle::Profile) -> Result<(), Error> {
let oid = git::Oid::from_str(oid)?;
let refstr = git::RefString::try_from(*refstr)?;
sync.run(&opts, &profile).ok();
return fetch::run(vec![(oid, refstr)], &working, url, stored, &stdin)
.map_err(Error::from);
}
@ -173,8 +165,6 @@ pub fn run(profile: radicle::Profile) -> Result<(), Error> {
.map_err(Error::from);
}
["list"] => {
sync.run(&opts, &profile).ok();
list::for_fetch(&url, &stored)?;
}
["list", "for-push"] => {
@ -237,77 +227,3 @@ pub(crate) fn read_line<'a>(stdin: &io::Stdin, line: &'a mut String) -> io::Resu
Ok(tokens)
}
/// Keeps track of sync state.
struct SyncState {
/// Whether we tried to fetch already.
fetched: bool,
/// The repo id.
rid: Id,
}
impl SyncState {
fn new(rid: Id) -> Self {
Self {
fetched: false,
rid,
}
}
/// Connect to local node and try to fetch refs from the network.
/// If our node is not running, or the "no-sync" option is enabled,
/// we simply do nothing.
fn run(
&mut self,
opts: &Options,
profile: &radicle::Profile,
) -> Result<(), radicle::node::Error> {
if opts.no_sync || self.fetched {
return Ok(());
}
let mut node = radicle::Node::new(profile.socket());
if !node.is_running() {
return Ok(());
}
let target = 3;
let timeout = time::Duration::from_secs(6);
let seeds = node.seeds(self.rid)?;
let connected = seeds.connected().map(|s| s.nid).collect::<Vec<_>>();
if connected.is_empty() {
return Ok(());
}
let message = format!("Syncing with {} node(s)..", connected.len().min(target));
let mut spinner = if io::stderr().is_terminal() {
cli::spinner_to(message, io::stderr(), io::stderr())
} else {
cli::spinner_to(message, io::stderr(), io::sink())
};
// Fetch from connected seeds.
let mut results = FetchResults::default();
for seed in connected.into_iter() {
spinner.message(format!("Syncing with {seed}.."));
let result = node.fetch(self.rid, seed, timeout)?;
results.push(seed, result);
if results.success().count() >= target {
break;
}
}
let success = results.success().count();
if success > 0 {
spinner.message(format!("Synced with {success} peer(s)"));
spinner.finish();
} else {
spinner.failed();
}
// Make sure we don't try to fetch again while this program is running.
self.fetched = true;
Ok(())
}
}