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

[8/15] testing: configure repos cloned with Git::clone_repo_into #1227

Closed
24 changes: 24 additions & 0 deletions git-branchless-lib/src/core/dag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,30 @@ impl Dag {
})
}

/// Determine the connected components among draft commits (commit "stacks")
/// that intersect with the provided set.
#[instrument]
pub fn query_stack_commits(&self, commit_set: CommitSet) -> eyre::Result<CommitSet> {
let draft_commits = self.query_draft_commits()?;
let stack_roots = self.query_roots(draft_commits.clone())?;
let stack_ancestors = self.query_range(stack_roots, commit_set)?;
let stack = self
// Note that for a graph like
//
// ```
// O
// |
// o A
// | \
// | o B
// |
// @ C
// ```
// this will return `{A, B, C}`, not just `{A, C}`.
.query_range(stack_ancestors, draft_commits.clone())?;
Ok(stack)
}

/// Wrapper around DAG method.
#[instrument]
pub fn query_all(&self) -> eyre::Result<CommitSet> {
Expand Down
4 changes: 2 additions & 2 deletions git-branchless-lib/src/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub use reference::{
};
pub use repo::{
message_prettify, AmendFastOptions, CherryPickFastOptions, CreateCommitFastError,
Error as RepoError, GitVersion, PatchId, Repo, ResolvedReferenceInfo, Result as RepoResult,
Time,
Error as RepoError, GitErrorCode, GitVersion, PatchId, Repo, ResolvedReferenceInfo,
Result as RepoResult, Time,
};
pub use run::{GitRunInfo, GitRunOpts, GitRunResult};
pub use snapshot::{WorkingCopyChangesType, WorkingCopySnapshot};
Expand Down
12 changes: 12 additions & 0 deletions git-branchless-lib/src/git/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,18 @@ impl<'repo> Branch<'repo> {
}
}

/// Rename the branch. The new name should not start with `refs/heads`.
#[instrument]
pub fn rename(&mut self, new_name: &str, force: bool) -> Result<()> {
self.inner
.rename(new_name, force)
.map_err(|err| Error::RenameBranch {
source: err,
new_name: new_name.to_owned(),
})?;
Ok(())
}

/// Delete the branch.
#[instrument]
pub fn delete(&mut self) -> Result<()> {
Expand Down
8 changes: 8 additions & 0 deletions git-branchless-lib/src/git/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ pub enum Error {
name: ReferenceName,
},

#[error("could not rename branch to '{new_name}': {source}")]
RenameBranch {
source: git2::Error,
new_name: String,
},

#[error("could not delete branch: {0}")]
DeleteBranch(#[source] git2::Error),

Expand Down Expand Up @@ -277,6 +283,8 @@ pub enum Error {
/// Result type.
pub type Result<T> = std::result::Result<T, Error>;

pub use git2::ErrorCode as GitErrorCode;

/// Convert a `git2::Error` into an `eyre::Error` with an auto-generated message.
pub(super) fn wrap_git_error(error: git2::Error) -> eyre::Error {
eyre::eyre!("Git error {:?}: {}", error.code(), error.message())
Expand Down
12 changes: 12 additions & 0 deletions git-branchless-lib/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,18 @@ then you can only run tests in the main `git-branchless` and \
};

let (_stdout, _stderr) = self.run(args.as_slice())?;

// Configuration options are not inherited from the original repo, so
// set them in the cloned repo.
let new_repo = Git {
repo_path: target.repo_path.clone(),
..self.clone()
};
new_repo.init_repo_with_options(&GitInitOptions {
make_initial_commit: false,
run_branchless_init: false,
})?;

Ok(())
}

Expand Down
25 changes: 3 additions & 22 deletions git-branchless-revset/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,28 +290,9 @@ fn fn_draft(ctx: &mut Context, name: &str, args: &[Expr]) -> EvalResult {
#[instrument]
fn fn_stack(ctx: &mut Context, name: &str, args: &[Expr]) -> EvalResult {
let arg = eval0_or_1(ctx, name, args)?.unwrap_or_else(|| ctx.dag.head_commit.clone());
let draft_commits = ctx
.dag
.query_draft_commits()
.map_err(EvalError::OtherError)?;
let stack_roots = ctx.dag.query_roots(draft_commits.clone())?;
let stack_ancestors = ctx.dag.query_range(stack_roots, arg)?;
let stack = ctx
.dag
// Note that for a graph like
//
// ```
// O
// |
// o A
// | \
// | o B
// |
// @ C
// ```
// this will return `{A, B, C}`, not just `{A, C}`.
.query_range(stack_ancestors, draft_commits.clone())?;
Ok(stack)
ctx.dag
.query_stack_commits(arg)
.map_err(EvalError::OtherError)
}

type MatcherFn = dyn Fn(&Repo, &Commit) -> Result<bool, PatternError> + Sync + Send;
Expand Down
34 changes: 17 additions & 17 deletions git-branchless-submit/src/branch_forge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ impl Forge for BranchForge<'_> {

let commit_status = match branch_infos.as_slice() {
[] => CommitStatus {
submit_status: SubmitStatus::Unsubmitted,
submit_status: SubmitStatus::Local,
remote_name: None,
local_branch_name: None,
remote_branch_name: None,
local_commit_name: None,
remote_commit_name: None,
},

[BranchInfo {
Expand All @@ -156,8 +156,8 @@ impl Forge for BranchForge<'_> {
None => CommitStatus {
submit_status: SubmitStatus::Unsubmitted,
remote_name: None,
local_branch_name: Some(branch_name.clone()),
remote_branch_name: None,
local_commit_name: Some(branch_name.clone()),
remote_commit_name: None,
},

Some(upstream_branch) => CommitStatus {
Expand All @@ -167,16 +167,16 @@ impl Forge for BranchForge<'_> {
SubmitStatus::NeedsUpdate
},
remote_name: remote_name.clone(),
local_branch_name: Some(branch_name.clone()),
remote_branch_name: Some(upstream_branch.get_name()?.to_owned()),
local_commit_name: Some(branch_name.clone()),
remote_commit_name: Some(upstream_branch.get_name()?.to_owned()),
},
},

_branch_infos => CommitStatus {
submit_status: SubmitStatus::Unknown,
remote_name: None,
local_branch_name: None,
remote_branch_name: None,
local_commit_name: None,
remote_commit_name: None,
},
};
commit_statuses.insert(*commit_oid, commit_status);
Expand All @@ -196,10 +196,10 @@ impl Forge for BranchForge<'_> {
let CommitStatus {
submit_status: _,
remote_name: _,
local_branch_name,
remote_branch_name: _,
local_commit_name,
remote_commit_name: _,
} = commit_status;
local_branch_name.clone()
local_commit_name.clone()
})
.sorted()
.collect_vec();
Expand Down Expand Up @@ -244,12 +244,12 @@ These remotes are available: {}",
Ok(Ok(commits
.into_iter()
.filter_map(|(commit_oid, commit_status)| {
commit_status.local_branch_name.map(|local_branch_name| {
commit_status.local_commit_name.map(|local_commit_name| {
(
commit_oid,
CreateStatus {
final_commit_oid: commit_oid,
local_branch_name,
local_commit_name,
},
)
})
Expand All @@ -269,9 +269,9 @@ These remotes are available: {}",
CommitStatus {
submit_status: _,
remote_name: Some(remote_name),
local_branch_name: Some(local_branch_name),
remote_branch_name: _,
} => Some((remote_name, local_branch_name)),
local_commit_name: Some(local_commit_name),
remote_commit_name: _,
} => Some((remote_name, local_commit_name)),
commit_status => {
warn!(
?commit_status,
Expand Down
Loading
Loading