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

cli: Refactor out get_fetch_remotes into git_util.rs #4768

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

essiene
Copy link
Collaborator

@essiene essiene commented Nov 4, 2024

Similarly to git_fetch, this will be used in jj git sync, so
moving this out to maintain the same behaviour across fetch command and sync.

  • Refactor out get_fetch_remotes to git_util.rs.
  • inline the different get_*_remote* functions into get_fetch_remotes;
    The resulting function is still small enough to be easily readable.
  • Move get_single_remote into git_util.rs and update call sites.
  • Update use references

All tests still pass.

Issue: #1039

@essiene essiene force-pushed the essiene/pmwupzxmwsqz branch 2 times, most recently from fa6e402 to e53870d Compare November 4, 2024 21:23
@essiene essiene requested a review from yuja November 4, 2024 21:24
@essiene essiene force-pushed the essiene/pmwupzxmwsqz branch 2 times, most recently from 1591633 to 0b60444 Compare November 4, 2024 22:45
cli/src/git_util.rs Outdated Show resolved Hide resolved
Comment on lines 553 to 555
remotes: &[String],
use_all_remotes: bool,
Copy link
Collaborator

Choose a reason for hiding this comment

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

If both jj git sync and jj git fetch share command parameters, it might be better to extract common Args struct. Then, we can pass it in to helper functions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Makes sense.

I'm thinking this could actually collapse down to something like:

let fetcher = GitFetcher::new(ui, tx, git_repo, branch, remotes, use_all_remotes);
fetcher.fetch()?;

Inside fetcher.fetch we'd grab the remotes and then do the fetch. Mainly because I can't think of a good reason why to expose to function calls to the commands instead of just one, as the intermediate result is not used by callers.

Does that feel like overkill?

In the meantime, I'm thinking to merge this PR and make changes in a follow-up to keep things clearer.

Let me know if you think otherwise.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure. Do we have more methods than fetch()?

My concern about the current API is that it's hard to tell which String/bool argument represents which variable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not sure. Do we have more methods than fetch()?

I just realized we do need to expose get_fetch_remotes, since we use the result to build a message after the fetch succeeds further down.

My concern about the current API is that it's hard to tell which String/bool argument represents which variable.

I've added a FetchArgs struct that should make this better and doesn't end up feeling over designed.

wdyt?

Base automatically changed from essiene/vutlmutyytky to main November 5, 2024 12:47
@essiene essiene force-pushed the essiene/pmwupzxmwsqz branch 6 times, most recently from c9ebe82 to ae98431 Compare November 6, 2024 21:14
@@ -456,30 +466,36 @@ export or their "parent" bookmarks."#,
Ok(())
}

pub struct FetchArgs<'a> {
pub repo: &'a git2::Repository,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wouldn't include git2::Repository in args struct because it's not a user parameter and has different lifetime.

BTW, what I meant was to extract common clap::Args struct from GitFetchArgs. If jj git sync shares the same help messages, we can "flatten" the common Args struct into GitFetch/SyncArgs. If not, we'll need a separate parameter struct as you did.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I wouldn't include git2::Repository in args struct because it's not a user parameter and has different lifetime.

Removed.

BTW, what I meant was to extract common clap::Args struct from GitFetchArgs. If jj git sync shares the same help messages, we can "flatten" the common Args struct into GitFetch/SyncArgs. If not, we'll need a separate parameter struct as you did.

I've gone to flesh out a first draft of the sync command to be sure we need to share the help messages and yeah, it makes sense to share them. So I've extracted these out and flattened them back into GitFetchArgs.

Is this what you meant?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this what you meant?

yes, thanks.

@essiene essiene force-pushed the essiene/pmwupzxmwsqz branch 4 times, most recently from c2adc4e to 084797c Compare November 11, 2024 10:29
branch: args.fetch.branch.clone(),
remotes: remotes.clone(),
all_remotes: args.fetch.all_remotes,
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, we can't use the original args.fetch? That makes args.fetch useless..

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe FetchRemoteArgs can be extracted instead of all-in FetchArgs, but I'm not pretty sure if that makes sense. Sorry for the noise.

Similarly to `git_fetch`, this will be used in `jj git sync`, so
moving this out to maintain the same behaviour across `fetch` command and `sync`.

* Refactor out `get_fetch_remotes` to `git_util.rs`.
* inline the different `get_*_remote*` functions into `get_fetch_remotes`;
  The resulting function is still small enough to be easily readable.
* Move `get_single_remote` into `git_util.rs` and update call sites.
* Use common FetchArgs to pass arguments both to `get_fetch_remotes` and `git_fetch`
* Update use references

All tests still pass.

Issue: #1039
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.

2 participants