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

feat(sync): pull from remote main branch #589

Merged
merged 3 commits into from
Oct 10, 2022
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
18 changes: 7 additions & 11 deletions git-branchless-lib/src/core/check_out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use crate::util::ExitCode;
use super::config::get_undo_create_snapshots;
use super::effects::Effects;
use super::eventlog::{Event, EventLogDb, EventTransactionId};
use super::formatting::printable_styled_string;
use super::repo_ext::{RepoExt, RepoReferencesSnapshot};

/// An entity to check out.
Expand Down Expand Up @@ -141,16 +140,13 @@ pub fn check_out_commit(
writeln!(
effects.get_output_stream(),
"{}",
printable_styled_string(
effects.get_glyphs(),
StyledString::styled(
match target {
Some(target) => format!("Failed to check out commit: {target}"),
None => "Failed to check out commit".to_string(),
},
BaseColor::Red.light()
)
)?
effects.get_glyphs().render(StyledString::styled(
match target {
Some(target) => format!("Failed to check out commit: {target}"),
None => "Failed to check out commit".to_string(),
},
BaseColor::Red.light()
))?
)?;
return Ok(exit_code);
}
Expand Down
4 changes: 2 additions & 2 deletions git-branchless-lib/src/core/eventlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ impl EventReplayer {
) -> eyre::Result<Self> {
let (_effects, _progress) = effects.start_operation(OperationType::ProcessEvents);

let main_branch_reference_name = repo.get_main_branch_reference()?.get_name()?;
let main_branch_reference_name = repo.get_main_branch()?.get_reference_name()?;
let mut result = EventReplayer::new(main_branch_reference_name);
for event in event_log_db.get_events()? {
result.process_event(&event);
Expand Down Expand Up @@ -1209,7 +1209,7 @@ impl EventReplayer {
cursor: EventCursor,
repo: &Repo,
) -> eyre::Result<NonZeroOid> {
let main_branch_reference_name = repo.get_main_branch_reference()?.get_name()?;
let main_branch_reference_name = repo.get_main_branch()?.get_reference_name()?;
let main_branch_oid = self.get_cursor_branch_oid(cursor, &main_branch_reference_name)?;
match main_branch_oid {
Some(main_branch_oid) => Ok(main_branch_oid),
Expand Down
46 changes: 23 additions & 23 deletions git-branchless-lib/src/core/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,29 @@ impl Glyphs {
cycle_lower_left_corner: "└",
}
}

/// Write the provided string to `out`, using ANSI escape codes as necessary to
/// style it.
///
/// TODO: return something that implements `Display` instead of a `String`.
pub fn render(&self, string: StyledString) -> eyre::Result<String> {
let result = string
.spans()
.map(|span| {
let Span {
content,
attr,
width: _,
} = span;
if self.should_write_ansi_escape_codes {
Ok(render_style_as_ansi(content, *attr)?)
} else {
Ok(content.to_string())
}
})
.collect::<eyre::Result<String>>()?;
Ok(result)
}
}

impl std::fmt::Debug for Glyphs {
Expand Down Expand Up @@ -379,26 +402,3 @@ fn render_style_as_ansi(content: &str, style: Style) -> eyre::Result<String> {

Ok(output.to_string())
}

/// Write the provided string to `out`, using ANSI escape codes as necessary to
/// style it.
///
/// TODO: return something that implements `Display` instead of a `String`.
pub fn printable_styled_string(glyphs: &Glyphs, string: StyledString) -> eyre::Result<String> {
let result = string
.spans()
.map(|span| {
let Span {
content,
attr,
width: _,
} = span;
if glyphs.should_write_ansi_escape_codes {
Ok(render_style_as_ansi(content, *attr)?)
} else {
Ok(content.to_string())
}
})
.collect::<eyre::Result<String>>()?;
Ok(result)
}
75 changes: 30 additions & 45 deletions git-branchless-lib/src/core/repo_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use color_eyre::Help;
use eyre::Context;
use tracing::instrument;

use crate::git::{NonZeroOid, Reference, ReferenceName, Repo};
use crate::git::{Branch, BranchType, NonZeroOid, ReferenceName, Repo};

use super::config::get_main_branch_name;

Expand All @@ -25,8 +25,8 @@ pub struct RepoReferencesSnapshot {

/// Helper functions on [`Repo`].
pub trait RepoExt {
/// Get the `Reference` for the main branch for the repository.
fn get_main_branch_reference(&self) -> eyre::Result<Reference>;
/// Get the `Branch` for the main branch for the repository.
fn get_main_branch(&self) -> eyre::Result<Branch>;

/// Get the OID corresponding to the main branch.
fn get_main_branch_oid(&self) -> eyre::Result<NonZeroOid>;
Expand All @@ -42,54 +42,48 @@ pub trait RepoExt {
}

impl RepoExt for Repo {
fn get_main_branch_reference(&self) -> eyre::Result<Reference> {
fn get_main_branch(&self) -> eyre::Result<Branch> {
let main_branch_name = get_main_branch_name(self)?;
match self.find_branch(&main_branch_name, git2::BranchType::Local)? {
Some(branch) => match branch.get_upstream_branch()? {
Some(upstream_branch) => Ok(upstream_branch.into_reference()),
None => Ok(branch.into_reference()),
},
None => match self.find_branch(&main_branch_name, git2::BranchType::Remote)? {
Some(branch) => Ok(branch.into_reference()),
None => {
let suggestion = format!(
r"
match self.find_branch(&main_branch_name, BranchType::Local)? {
Some(branch) => Ok(branch),
None => {
let suggestion = format!(
r"
The main branch {:?} could not be found in your repository
at path: {:?}.
These branches exist: {:?}
Either create it, or update the main branch setting by running:

git branchless init --main-branch <branch>
",
get_main_branch_name(self)?,
self.get_path(),
self.get_all_local_branches()?
.into_iter()
.map(|branch| {
branch
.into_reference()
.get_name()
.map(|s| format!("{:?}", s))
.wrap_err("converting branch to reference")
})
.collect::<eyre::Result<Vec<String>>>()?,
);
Err(eyre::eyre!("Could not find repository main branch")
.with_suggestion(|| suggestion))
}
},
get_main_branch_name(self)?,
self.get_path(),
self.get_all_local_branches()?
.into_iter()
.map(|branch| {
branch
.into_reference()
.get_name()
.map(|s| format!("{:?}", s))
.wrap_err("converting branch to reference")
})
.collect::<eyre::Result<Vec<String>>>()?,
);
Err(eyre::eyre!("Could not find repository main branch")
.with_suggestion(|| suggestion))
}
}
}

#[instrument]
fn get_main_branch_oid(&self) -> eyre::Result<NonZeroOid> {
let main_branch_reference = self.get_main_branch_reference()?;
let commit = main_branch_reference.peel_to_commit()?;
match commit {
Some(commit) => Ok(commit.get_oid()),
let main_branch = self.get_main_branch()?;
let main_branch_oid = main_branch.get_oid()?;
match main_branch_oid {
Some(main_branch_oid) => Ok(main_branch_oid),
None => eyre::bail!(
"Could not find commit pointed to by main branch: {:?}",
main_branch_reference.get_name()?
main_branch.get_name()?,
),
}
}
Expand All @@ -109,15 +103,6 @@ Either create it, or update the main branch setting by running:
}
}

// The main branch may be a remote branch, in which case it won't be
// returned in the iteration above.
let main_branch_name = self.get_main_branch_reference()?.get_name()?;
let main_branch_oid = self.get_main_branch_oid()?;
result
.entry(main_branch_oid)
.or_insert_with(HashSet::new)
.insert(main_branch_name);

Ok(result)
}

Expand Down
Loading