Skip to content

Commit

Permalink
git: add constant for pseudo remote name "git"
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Aug 29, 2023
1 parent f47da04 commit 0e26ef7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
5 changes: 4 additions & 1 deletion cli/src/commands/branch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,10 @@ fn cmd_branch_list(

for (name, branch_target) in all_branches {
let found_non_git_remote = {
let pseudo_remote_count = branch_target.remote_targets.contains_key("git") as usize;
let pseudo_remote_count = branch_target
.remote_targets
.contains_key(git::REMOTE_NAME_FOR_LOCAL_GIT_REPO)
as usize;
branch_target.remote_targets.len() - pseudo_remote_count > 0
};

Expand Down
15 changes: 11 additions & 4 deletions lib/src/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ use crate::revset;
use crate::settings::GitSettings;
use crate::view::{RefName, View};

/// Reserved remote name for the backing Git repo.
pub const REMOTE_NAME_FOR_LOCAL_GIT_REPO: &str = "git";

#[derive(Error, Debug)]
pub enum GitImportError {
#[error("Failed to read Git HEAD target commit {id}: {err}", id=id.hex())]
Expand Down Expand Up @@ -136,15 +139,19 @@ pub fn build_unified_branches_map(view: &View) -> (BTreeMap<String, BranchTarget
let mut bad_branch_names = Vec::new();
for (branch_name, git_tracking_target) in local_branch_git_tracking_refs(view) {
let branch_target = all_branches.entry(branch_name.to_owned()).or_default();
if branch_target.remote_targets.contains_key("git") {
if branch_target
.remote_targets
.contains_key(REMOTE_NAME_FOR_LOCAL_GIT_REPO)
{
// TODO(#1690): There should be a mechanism to prevent importing a
// remote named "git" in `jj git import`.
bad_branch_names.push(branch_name.to_owned());
} else {
// TODO: `BTreeMap::try_insert` could be used here once that's stabilized.
branch_target
.remote_targets
.insert("git".to_owned(), git_tracking_target.clone());
branch_target.remote_targets.insert(
REMOTE_NAME_FOR_LOCAL_GIT_REPO.to_owned(),
git_tracking_target.clone(),
);
}
}
(all_branches, bad_branch_names)
Expand Down
2 changes: 1 addition & 1 deletion lib/src/revset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1890,7 +1890,7 @@ fn resolve_remote_branch(repo: &dyn Repo, name: &str, remote: &str) -> Option<Ve
return Some(target.added_ids().cloned().collect());
}
// A remote with name "git" will shadow local-git tracking branches
if remote == "git" {
if remote == git::REMOTE_NAME_FOR_LOCAL_GIT_REPO {
let target = match name {
"HEAD" => view.git_head(),
_ => get_local_git_tracking_branch(view, name),
Expand Down

0 comments on commit 0e26ef7

Please sign in to comment.