260 Commits

Author SHA1 Message Date
Martin von Zweigbergk
f0545ee25c test: introduce test helpers for creating repo path types
I'm about to make the constructors return a `Result`. The helpers will
hide the unwrapping.
2025-04-15 14:42:23 +00:00
Yuya Nishihara
f87db58617 view: rename RemoteRefState::Tracking to Tracked
In jj's model, a local bookmark "tracks" remote bookmarks. It's wrong to call
a remote bookmark state as "tracking".
2025-03-31 01:41:31 +00:00
Yuya Nishihara
e4982a9c33 git: report HEAD export failure as warning
It's safe to commit transaction even if HEAD couldn't be updated due to
concurrent changes.

Closes #6098
2025-03-30 01:46:00 +00:00
Yuya Nishihara
cd1ee943e6 git: split GitResetHeadError, add UpdateHeadRef variant
This helps handle "HEAD" export failure differently. At this point, the set of
error variants look distinct enough to split the error types.
2025-03-30 01:46:00 +00:00
Yuya Nishihara
c33be3839d git: remove redundant error context messages, rename variants
Wrapped errors are usually displayed after "Failed to import refs ...", etc., so
the context is obvious. I also removed "Internal"/"Error" for consistency.
2025-03-30 01:46:00 +00:00
Yuya Nishihara
d6be4a598e git: use GitRefName newtypes in push functions 2025-03-28 01:29:30 +00:00
Yuya Nishihara
fb8e45d6f9 view: introduce GitRefName newtypes
We don't reuse the RefName type here because fully-qualified Git ref name
shouldn't be considered a local ref name in jj.
2025-03-28 01:29:30 +00:00
Yuya Nishihara
3656a62bf5 git: use RemoteName newtype in remote management functions 2025-03-26 13:03:37 +00:00
Yuya Nishihara
4ff2827d00 git: use RemoteName/RefName newtypes in fetch functions 2025-03-26 13:03:37 +00:00
Yuya Nishihara
02722eae54 view: port bookmark/tag name types to RefName/RemoteName
I tried to minimize this patch, but it seemed rather complicated than porting
most callers all at once. Remote management functions in git.rs are unchanged.
They'll be ported separately.

With this change, many non-template bookmark/remote name outputs should be
rendered in revset syntax.
2025-03-26 11:07:06 +00:00
Yuya Nishihara
cc8a80c548 ref_name: move RemoteRefSymbol types from refs module 2025-03-26 11:07:06 +00:00
Baltasar Dinis
8ae80523e4 git: forward rejection reason on push
We've been finding that a lot of bug reports on `jj git push` come from
sub-standard error reporting on the reasons the failure happens.

It can come from a number of places:
 - hook failure
 - remote branch protection
 - git config

This commit forwards the reason as explained by the ouptut of git push
to help users figure out what is happening.
2025-03-26 04:46:38 +00:00
Baltasar Dinis
89bb1c4e71 git: refactor push ref error reporting
We need to report more complicated errors on push.
Firstly, we can have a mix of unexpected ref locations and remote
rejections. We should report both at the same time.

Second, git gives us a reason for why a push failed.
For this to work, it's relevant to refactor the current error reporting
path to allow us to inject this information.
2025-03-26 04:46:38 +00:00
Yuya Nishihara
5ee1dbbd22 git: wrap list of failed exports in struct
I have no immediate plan, but I think we can make "jj git export" show exported
refs. The FailedRefExport type is replaced with a plain tuple since we have a
named type wrapping (symbol, reason) pairs now.
2025-03-24 01:18:24 +00:00
Yuya Nishihara
8ed9c36377 git: in import_refs(), use sorted vec as map of bookmarks/tags
For consistency with export_refs().

changed_git_refs doesn't have to be sorted, but the sorting cost wouldn't matter
in practice.
2025-03-23 01:32:29 +00:00
Yuya Nishihara
b6419ca790 git: in import_refs(), split known remote bookmarks/tags maps
It's simpler, and more consistent with export_refs().
2025-03-23 01:32:29 +00:00
Yuya Nishihara
7d59d0564e git: use translated remote symbols in export_refs()
I also made to_git_ref_name() stricter as it seemed odd that empty tag name and
remote name were allowed.
2025-03-23 01:32:29 +00:00
Emily
a56b78bdb6 git: make git2 support optional
This helps us prepare for removing the functionality down the line and
makes things easier for people building or packaging their own Jujutsu.
2025-03-16 06:07:28 +00:00
Ilya Grigoriev
acaedc3382 cleanup: enable unused_trait_names clippy lint and run clippy --fix 2025-03-16 00:35:56 +00:00
Yuya Nishihara
e054a291b1 git: use translated remote symbols in import_refs()
This removes the special case for RefName::LocalBranch(_), which can be
processed as a remote bookmark.

"jj git import" now prints local bookmarks and tags with @git suffix. I think
this is more correct since these refs are imported from the backing Git
repository.
2025-03-12 02:20:00 +00:00
Yuya Nishihara
540ae1b697 git: pass remote (kind, symbol) parameters to git_ref_filter()
I'll reimplement import/export internals to be based off the translated remote
symbols.
2025-03-12 02:20:00 +00:00
Emily
fe9e71b90e git: remove .gitmodules parsing code
This is redundant with `gix`’s native API and not used by anything
but a hidden debugging command.
2025-03-11 02:37:13 +00:00
Yuya Nishihara
c0066083ad git: make import of refs/remotes/git/* non-error, warn failed refs instead
I'm going to reimplement git_ref_filter to process translated remote bookmark
names, and "git" remote will mean the local Git-tracking remote there. The
reserved remote name is checked prior to filtering because refs in that remote
cannot be represented as remote symbols.

I originally implemented the error handling the other way because we didn't
have a machinery to report partial import failure. Now we have stats, it's
easy to report skipped ref names.
2025-03-10 02:04:33 +00:00
Emily
09f6cafa2b git: add remote management tests 2025-03-09 21:17:10 +00:00
Ilya Grigoriev
5eae2d92a0 tests: run insta --force-update-snapshots
This is a replacement for #5558.

Thanks to @yuja 's https://github.com/mitsuhiko/insta/pull/722, this is
now easy to generate.
2025-03-06 21:35:08 +00:00
Baltasar Dinis
876262a41b cli/tests: have clone helper take in a remote name
This is relevant for the git_fetch tests
2025-03-05 05:39:19 +00:00
Yuya Nishihara
6c597d962d git: on reset_head(), check if the actual HEAD points to known location
This will probably mitigate problems of concurrent updates. If two concurrent
processes tried to import + reset HEAD, one of them should fail.

Closes #3754
2025-02-28 05:45:29 +00:00
Baltasar Dinis
e9e127c233 lib/tests: move remaining git test to gitoxide 2025-02-28 03:58:46 +00:00
Yuya Nishihara
ae8e6e8e8e view: port remote_bookmark accessors to RemoteRefSymbol 2025-02-26 03:17:45 +00:00
Yuya Nishihara
6c71f3cbf2 git: use RemoteRefSymbolBuf to represent RefName::RemoteBranch variant
This also changes the formatting to use revset compatible form.
2025-02-26 03:17:45 +00:00
Baltasar Dinis
837250277e lib/tests: port lib tests to gitoxide
As per #5548, this moves the majority of the tests in lib/test over to
gitoxide
2025-02-21 18:41:30 +00:00
Yuya Nishihara
7898eb9f82 git: split function that queries remote default branch, call only when needed
With git.subprocess = true, it's more important to skip unneeded remote
operations. Each remote command may involve user intervention if authentication
requires manual step.

This change also means that the remote connection is no longer reused in git2
impl. I think the added cost is acceptable. The git2 impl will hopefully be
removed soon, and the remote branch name is needed only when cloning new repo.
2025-02-03 03:43:11 +00:00
Yuya Nishihara
658cd3e0fd git: resolve git2::Repository internally in GitFetch::new()
git2::Repository will be removed from the subprocess code path.
2025-02-01 11:04:38 +00:00
Yuya Nishihara
9bf29accc2 git: resolve git2::Repository internally in push_updates()
git2::Repository will be removed from the subprocess code path.
2025-02-01 11:04:38 +00:00
Martin von Zweigbergk
4ac970fcc5 cleanup: prefer MutableRepo::repo() over repo_mut()
When we don't need a mutable reference, we should be using `repo()`.
2025-01-31 18:03:01 +00:00
Baltasar Dinis
1be574c219 git: update jj git clone|fetch to use new GitFetch api directly.
* Make the new `GitFetch` api public.
* Move `git::fetch` to `lib/tests/test_git.rs` as `git_fetch`, to minimize
  churn in the tests. Update test call sites to use `git_fetch`
* Delete the `git::fetch` from `lib/src/git.rs`.
* Update `jj git clone` and `git_fetch` in `cli/src/git_utils.rs` to use
  the new api directly. Removing one redundant layer of indirection.
* This fixes #4920 as it first fetches from all remotes before `import_refs()`
  is called, so there is no race condition if the same commit is treated
  differently in different remotes specified in the same command.

Original commit by @essiene
2025-01-28 18:29:01 +00:00
Scott Taylor
00069119d8 tests: keep user's $PATH while running jj commands
Currently, the Git subprocess tests only work on Linux due to a default
path being used for the `git` executable when `$PATH` is unset. This can
break if Git isn't installed at the expected path. Also, I believe it is
currently necessary to set the `$TEST_GIT_EXECUTABLE_PATH` environment
variable on Windows for tests to pass. Instead, we should use the user's
`$PATH` to locate the `git` executable, as well as any other executables
that are needed. This also makes `$TEST_GIT_EXECUTABLE_PATH` no longer
necessary, so it can be removed.
2025-01-26 17:16:59 +00:00
bsdinis
35440ce1bd git: spawn a separate git process for network operations
Reasoning:

`jj` fails to push/fetch over ssh depending on the system.
Issue #4979 lists over 20 related issues on this and proposes spawning
a `git` subprocess for tasks related to the network (in fact, just push/fetch
are enough).

This PR implements this.

Implementation Details:

This PR implements shelling out to `git` via `std::process::Command`.
There are 2 sharp edges with the patch:
 - it relies on having to parse out git errors to match the error codes
   (and parsing git2's errors in one particular instance to match the
   error behaviour). This seems mostly unavoidable

 - to ensure matching behaviour with git2, the tests are maintained across the
   two implementations. This is done using test_case, as with the rest
   of the codebase

Testing:

Run the rust tests:
```
$ cargo test
```

Build:
```
$ cargo build
```

Clone a private repo:
```
$ path/to/jj git clone --config='git.subprocess=true' <REPO_SSH_URL>
```

Create new commit and push
```
$ echo "TEST" > this_is_a_test_file.txt
$ path/to/jj describe -m 'test commit'
$ path/to/jj git push --config='git.subprocess=true' -b <branch>
```


Issues Closed

With a grain of salt, but most of these problems should be fixed (or at least checked if they are fixed). They are the ones listed in #4979 .

SSH:
- https://github.com/jj-vcs/jj/issues/63
- https://github.com/jj-vcs/jj/issues/440
- https://github.com/jj-vcs/jj/issues/1455
- https://github.com/jj-vcs/jj/issues/1507
- https://github.com/jj-vcs/jj/issues/2931
- https://github.com/jj-vcs/jj/issues/2958
- https://github.com/jj-vcs/jj/issues/3322
- https://github.com/jj-vcs/jj/issues/4101
- https://github.com/jj-vcs/jj/issues/4333
- https://github.com/jj-vcs/jj/issues/4386
- https://github.com/jj-vcs/jj/issues/4488
- https://github.com/jj-vcs/jj/issues/4591
- https://github.com/jj-vcs/jj/issues/4802
- https://github.com/jj-vcs/jj/issues/4870
- https://github.com/jj-vcs/jj/issues/4937
- https://github.com/jj-vcs/jj/issues/4978
- https://github.com/jj-vcs/jj/issues/5120
- https://github.com/jj-vcs/jj/issues/5166

Clone/fetch/push/pull:
- https://github.com/jj-vcs/jj/issues/360
- https://github.com/jj-vcs/jj/issues/1278
- https://github.com/jj-vcs/jj/issues/1957
- https://github.com/jj-vcs/jj/issues/2295
- https://github.com/jj-vcs/jj/issues/3851
- https://github.com/jj-vcs/jj/issues/4177
- https://github.com/jj-vcs/jj/issues/4682
- https://github.com/jj-vcs/jj/issues/4719
- https://github.com/jj-vcs/jj/issues/4889
- https://github.com/jj-vcs/jj/discussions/5147
- https://github.com/jj-vcs/jj/issues/5238

Notable Holdouts:
 - Interactive HTTP authentication (https://github.com/jj-vcs/jj/issues/401, https://github.com/jj-vcs/jj/issues/469)
 - libssh2-sys dependency on windows problem (can only be removed if/when we get rid of libgit2): https://github.com/jj-vcs/jj/issues/3984
2025-01-23 16:50:53 +00:00
bsdinis
c773c0296f git: test git fetch with more than two remotes and branches
In some cases, there are non trivial codepaths for fetching multiple
branches explicitly. In particular, it might be the case that fetching
works for n = 2 but not n = 3.

This commit changes a cli test to have 3 remotes and 3 branches, and a
lib test with 3 branches, only one of them succeds.
2025-01-23 16:50:53 +00:00
Yuya Nishihara
5d1f3d006d repo: pass &Commit to record_abandoned_commit() to simplify error handling 2025-01-17 00:28:25 +00:00
Yuya Nishihara
f14a6e6f19 git: remove PartialEq requirement from GitPushError
Non-trivial error types usually don't implement Eq/PartialEq.
2025-01-10 01:16:33 +00:00
Scott Taylor
42b390bbc4 git: use merged parent tree for git index
Instead of setting the index to match the tree of HEAD, we now set the
index to the merged parent tree of the working copy commit. This means
that if you edit a merge commit, it will make the Git index look like it
would in the middle of a `git merge` operation (with all of the
successfully-merged files staged in the index).

If there are any 2-sided conflicts in the merged parent tree, then they
will be added to the index as conflicts. Since Git doesn't support
conflicts with more than 2 sides, many-sided conflicts are staged as the
first side of the conflict. The following commit will improve this.
2025-01-06 19:17:51 -06:00
Scott Taylor
9cc8b35251 git: use gix instead of git2 to update index
This will give us more fine-grained control over what files we put in
the index, allowing us to create conflicted index states. We also still
need to use git2 to clean up the merge/rebase state, since gix doesn't
have any function for this currently.
2025-01-06 19:17:51 -06:00
Scott Taylor
8e3ec9c58c git: add tests for index in colocated repo
These tests should still pass after we switch to gix for resetting the
index, so we need to make sure they don't rely on the cached index from
the `git2::Repository` instance.
2025-01-06 19:17:51 -06:00
Yuya Nishihara
cff73841ed repo: remove &UserSettings argument from new/rewrite_commit(), use self.settings 2024-12-31 10:51:57 +09:00
Yuya Nishihara
14b52205fb repo: remove &UserSettings argument from start_transaction(), use self.settings 2024-12-31 10:51:57 +09:00
Martin von Zweigbergk
b836e0ae95 docs/cli: update URLs to from martinvonz user to jj-vcs org
We just migrated to the jj-vcs GitHub org, so we should point to the
new GitHub URLs.
2024-12-17 12:44:44 -08:00
Essien Ita Essien
694dfb8722 jj-lib: abstract a lower-level api for fetching from git.
* GitFetch{} separates `fetch()` from `import_refs()`.
* This allows more control over the stages of the fetch so multiple fetches
  can happen before `import_refs` resolves the changes in the local jj repo.
* Implement `git::fetch` in terms of the new api.
* Add a test case for initial fetch from a repo without `HEAD` set. This tests
  the default branch retrieving behaviour.

Issue: #4923
2024-12-01 12:22:09 +00:00
Martin von Zweigbergk
de6da1a088 transaction: propagate errors from commit() 2024-11-13 23:05:24 -08:00
Yuya Nishihara
7be4904982 tests: fix flakiness in shallow Git repo test
This test reliably failed if I dropped tv_nsec part from statx().

Since we reload the repo now, several assertions get "fixed". I've added
index().has_id() test to clarify that it's still broken.
2024-11-12 20:27:51 +09:00