Skip to content

Conversation

owenbrooks
Copy link

Fixes #7083

Checklist

If applicable:

  • I have updated CHANGELOG.md
  • I have added/updated tests to cover my changes

@owenbrooks owenbrooks requested a review from a team as a code owner August 24, 2025 02:07
/// If not present, all branches are fetched and the repository's default
/// branch is the parent of the working-copy change
#[arg(long)]
branch: Option<String>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can support multiple branches, i.e. branch: Vec<StringPattern>?

If #7275 gets merged, these patterns can be set as the default refspecs so future jj git fetch wouldn't fetch all branches.

I'm not sure which branch should be checked out if there are multiple matching bookmarks, but maybe we can try the default first, then fall back to one of the matching bookmark?

Copy link
Author

@owenbrooks owenbrooks Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple branches makes sense.

I'm thinking checking out the first matching pattern could be nice. It gives the caller control over the branch which I feel is the purpose of this option.

So jj git clone <repo> --branch feature_branch --branch main --branch other would result in feature_branch being checked out, even if main is normally the default. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds also good, but well need to pick one arbitrarily for --branch glob:pattern. I don't mind if the first matching branch (in e.g. lexicographical order) were chosen if that's easier to implement.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added multiple branch support, where the first match in lexicographical order, of the first --branch pattern by position, becomes the working copy parent.

@owenbrooks owenbrooks force-pushed the push-zmmvnpytokvt branch 2 times, most recently from 6af27ac to 4aca08e Compare August 31, 2025 06:11
target_branches
.unwrap_or(&[StringPattern::everything()])
.to_vec(),
)?,
Copy link
Contributor

@yuja yuja Aug 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since #7275 gets merged, we can configure the remote to fetch only the specified branches/bookmarks. That seems better since future jj git fetch won't fetch other bookmarks. Then, we wouldn't have to specify the target branches here.

EDIT: we'll need to pass the same refspecs to fetch() function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just clarifying, there is no change needed here, but we now will have the expected behaviour for future calls to jj git fetch, right?

Copy link
Contributor

@yuja yuja Sep 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll probably need to pass the fetch refspecs to git::add_remote() to make them persist. Can you add test for clone --branch && fetch?

We can address this later, but I think it's better to include it in the same release.

Comment on lines 347 to 357
if first_match.is_none() {
return Err(user_error(format!(
"No such branch matching pattern: {first_pattern}",
)));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we make it error out, I think we should check if there are no bookmarks matching any of the patterns, not the first pattern. Another option is to check if each pattern matches at least one bookmark.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a check that every pattern must match at least one bookmark.

Copy link
Member

@benbrittain benbrittain Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the special logic for the first match then? They seem functionally equivalent to me, so that extra logic should probably be removed

@owenbrooks owenbrooks force-pushed the push-zmmvnpytokvt branch 3 times, most recently from c85eb9e to f6896f0 Compare September 2, 2025 12:33
When cloning with the branch option:
- Only the specified branch will be fetched
- The trunk alias is only set if the specified branch happens to be the default branch
- The clone fails if the branch does not exist in the remote
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FR: jj git clone should support --branch/-b to clone and switch to a specific branch
3 participants