Skip to content

Commit

Permalink
git: get_git_repo -> get_git_backend_repo, distinguish it from curren…
Browse files Browse the repository at this point in the history
…t worktree

Now that colocated workspaces exist, it is necessary to distinguish the
two kinds of git repository `jj` might open. There's the git backend
(which is either a bare repository or a colocated one) and the worktrees
for all the colocated workspaces. Both of these are a
`gix/git2::Repository`, the difference is just which path you opened. So
you have to distinguish them with names.

This rename mostly serves as an opportunity to review at least some of
the usages of explicit usage of the git backend repo.
  • Loading branch information
cormacrelf committed Oct 19, 2024
1 parent 1053aa9 commit 1012a2b
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 19 deletions.
4 changes: 2 additions & 2 deletions cli/src/commands/git/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use crate::commands::git::map_git_error;
use crate::commands::git::maybe_add_gitignore;
use crate::config::write_config_value_to_file;
use crate::config::ConfigNamePathBuf;
use crate::git_util::get_git_repo;
use crate::git_util::get_git_backend_repo;
use crate::git_util::print_git_import_stats;
use crate::git_util::with_remote_git_callbacks;
use crate::ui::Ui;
Expand Down Expand Up @@ -204,7 +204,7 @@ fn do_git_clone(
} else {
Workspace::init_internal_git(command.settings(), wc_path)?
};
let git_repo = get_git_repo(repo.store())?;
let git_repo = get_git_backend_repo(repo.store())?;
writeln!(
ui.status(),
r#"Fetching into new repo in "{}""#,
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/git/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::command_error::user_error_with_hint;
use crate::command_error::CommandError;
use crate::commands::git::get_single_remote;
use crate::commands::git::map_git_error;
use crate::git_util::get_git_repo;
use crate::git_util::get_git_backend_repo;
use crate::git_util::print_git_import_stats;
use crate::git_util::with_remote_git_callbacks;
use crate::ui::Ui;
Expand Down Expand Up @@ -60,7 +60,7 @@ pub fn cmd_git_fetch(
args: &GitFetchArgs,
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let git_repo = get_git_repo(workspace_command.repo().store())?;
let git_repo = get_git_backend_repo(workspace_command.repo().store())?;
let remotes = if args.all_remotes {
get_all_remotes(&git_repo)?
} else if args.remotes.is_empty() {
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/git/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::command_error::CommandError;
use crate::commands::git::maybe_add_gitignore;
use crate::config::write_config_value_to_file;
use crate::config::ConfigNamePathBuf;
use crate::git_util::get_git_repo;
use crate::git_util::get_git_backend_repo;
use crate::git_util::is_colocated_git_workspace;
use crate::git_util::print_failed_git_export;
use crate::git_util::print_git_import_stats;
Expand Down Expand Up @@ -242,7 +242,7 @@ pub fn maybe_set_repository_level_trunk_alias(
ui: &Ui,
workspace_command: &WorkspaceCommandHelper,
) -> Result<(), CommandError> {
let git_repo = get_git_repo(workspace_command.repo().store())?;
let git_repo = get_git_backend_repo(workspace_command.repo().store())?;
if let Ok(reference) = git_repo.find_reference("refs/remotes/origin/HEAD") {
if let Some(reference_name) = reference.symbolic_target() {
if let Some(RefName::RemoteBranch {
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/git/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use crate::command_error::CommandError;
use crate::commands::git::get_single_remote;
use crate::commands::git::map_git_error;
use crate::formatter::Formatter;
use crate::git_util::get_git_repo;
use crate::git_util::get_git_backend_repo;
use crate::git_util::with_remote_git_callbacks;
use crate::git_util::GitSidebandProgressMessageWriter;
use crate::ui::Ui;
Expand Down Expand Up @@ -143,7 +143,7 @@ pub fn cmd_git_push(
args: &GitPushArgs,
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let git_repo = get_git_repo(workspace_command.repo().store())?;
let git_repo = get_git_backend_repo(workspace_command.repo().store())?;

let remote = if let Some(name) = &args.remote {
name.clone()
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/git/remote/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use jj_lib::repo::Repo;

use crate::cli_util::CommandHelper;
use crate::command_error::CommandError;
use crate::git_util::get_git_repo;
use crate::git_util::get_git_backend_repo;
use crate::ui::Ui;

/// Add a Git remote
Expand All @@ -36,7 +36,7 @@ pub fn cmd_git_remote_add(
) -> Result<(), CommandError> {
let workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo();
let git_repo = get_git_repo(repo.store())?;
let git_repo = get_git_backend_repo(repo.store())?;
git::add_remote(&git_repo, &args.remote, &args.url)?;
Ok(())
}
4 changes: 2 additions & 2 deletions cli/src/commands/git/remote/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use jj_lib::repo::Repo;

use crate::cli_util::CommandHelper;
use crate::command_error::CommandError;
use crate::git_util::get_git_repo;
use crate::git_util::get_git_backend_repo;
use crate::ui::Ui;

/// List Git remotes
Expand All @@ -32,7 +32,7 @@ pub fn cmd_git_remote_list(
) -> Result<(), CommandError> {
let workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo();
let git_repo = get_git_repo(repo.store())?;
let git_repo = get_git_backend_repo(repo.store())?;
for remote_name in git_repo.remotes()?.iter().flatten() {
let remote = git_repo.find_remote(remote_name)?;
writeln!(
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/git/remote/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use jj_lib::repo::Repo;

use crate::cli_util::CommandHelper;
use crate::command_error::CommandError;
use crate::git_util::get_git_repo;
use crate::git_util::get_git_backend_repo;
use crate::ui::Ui;

/// Remove a Git remote and forget its bookmarks
Expand All @@ -34,7 +34,7 @@ pub fn cmd_git_remote_remove(
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo();
let git_repo = get_git_repo(repo.store())?;
let git_repo = get_git_backend_repo(repo.store())?;
let mut tx = workspace_command.start_transaction();
git::remove_remote(tx.repo_mut(), &git_repo, &args.remote)?;
if tx.repo().has_changes() {
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/git/remote/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use jj_lib::repo::Repo;

use crate::cli_util::CommandHelper;
use crate::command_error::CommandError;
use crate::git_util::get_git_repo;
use crate::git_util::get_git_backend_repo;
use crate::ui::Ui;

/// Rename a Git remote
Expand All @@ -36,7 +36,7 @@ pub fn cmd_git_remote_rename(
) -> Result<(), CommandError> {
let mut workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo();
let git_repo = get_git_repo(repo.store())?;
let git_repo = get_git_backend_repo(repo.store())?;
let mut tx = workspace_command.start_transaction();
git::rename_remote(tx.repo_mut(), &git_repo, &args.old, &args.new)?;
if tx.repo().has_changes() {
Expand Down
4 changes: 2 additions & 2 deletions cli/src/commands/git/remote/set_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use jj_lib::repo::Repo;

use crate::cli_util::CommandHelper;
use crate::command_error::CommandError;
use crate::git_util::get_git_repo;
use crate::git_util::get_git_backend_repo;
use crate::ui::Ui;

/// Set the URL of a Git remote
Expand All @@ -36,7 +36,7 @@ pub fn cmd_git_remote_set_url(
) -> Result<(), CommandError> {
let workspace_command = command.workspace_helper(ui)?;
let repo = workspace_command.repo();
let git_repo = get_git_repo(repo.store())?;
let git_repo = get_git_backend_repo(repo.store())?;
git::set_remote_url(&git_repo, &args.remote, &args.url)?;
Ok(())
}
11 changes: 10 additions & 1 deletion cli/src/git_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,16 @@ use crate::formatter::Formatter;
use crate::progress::Progress;
use crate::ui::Ui;

pub fn get_git_repo(store: &Store) -> Result<git2::Repository, CommandError> {
/// This opens a [git2::Repository] for the underlying [GitBackend], assuming
/// this store is git-backed.
///
/// If the JJ repo is colocated with the git repo, then this will often not be
/// what you want, because its HEAD will always be for the default workspace.
/// Most JJ commands that access the HEAD need the one for the current
/// workspace.
///
/// However, sometimes you don't care. For example, when you run `jj git fetch`.
pub fn get_git_backend_repo(store: &Store) -> Result<git2::Repository, CommandError> {
match store.backend_impl().downcast_ref::<GitBackend>() {
None => Err(user_error("The repo is not backed by a git repo")),
Some(git_backend) => Ok(git_backend.open_git_repo()?),
Expand Down

0 comments on commit 1012a2b

Please sign in to comment.