Commit Graph

238 Commits

Author SHA1 Message Date
Martin von Zweigbergk
0c8a116771 rewrite: fix auto-rebasing after "branchy" rewrites
The `DescendantRebaser` was designed to help with rebasing in two
different use cases: 1) after regular rewriting of commits where the
change ID is preserved, and 2) after importing moved branches from
other repo (e.g. backing Git repo or remote). Many of the tests are
for the second use case, such as where a branch was moved
forward. However, I just noticed that there's a pretty common scenario
from the first use case that is not supported.

Let's say you have this history:

```
D
|
C C'
|/
B B'
|/
A
```

Here we want C' to be rebased onto B' and then D to be rebased onto
C''. However, because of the support for moving branches forward, we
would not rebase commits that were already rewritten, such as C' here
(see affected tests for details), which resulted in D getting rebased
onto C', and both B and B' remaining visible.

I think I was thinking when I designed it that it would be nice if you
could just tell `DescendantRebaser` that any descendants of a commit
should be moved forward. That may be useful, but I don't think we'll
want that for the general case of a branch moving forward. Perhaps
we'll want to make it configurable which branches it should happen
for. Either way, the way it was coded by not rebasing already
rewritten commits did not work for the case above. We may be able to
handle both cases better by considering each rewrite separately
instead of all destinations at once. For now, however, I've decided to
keep it simple, so I'm fixing the case above by sacrificing some of
the potentially useful functionality for moving branches forward.

Another fix necessary for the scenario shown above was to make sure we
always rebase C' before D. Before this patch, that depended on the
order in the index. This patch fixes that by modifying the topological
order to take rewrites into account, making D depend not only on C but
also on C'. (I suppose you could instead say that C depends on both B
and C'; I don't know if that'd make a difference.)
2022-01-27 22:20:14 -08:00
Martin von Zweigbergk
5b93ae6d4b rewrite: make it possible to rebase descendants multiple times
Despite what the documentation said, we don't clear the record of
rewritten and abandoned commits at the end. This change fixes that,
and adds a test showing that it's possible to call
`MutableRepo::rebase_descendants()` multiple times.
2022-01-27 22:11:07 -08:00
Martin von Zweigbergk
10ebf35c27 repo: add a convenience function for rebasing all descendants
All non-test users of `create_descendant_rebaser()` just want to
rebase all commits, so let's make that easy.
2022-01-27 08:28:44 -08:00
Martin von Zweigbergk
38180555de working_copy: keep track of operation ID (#13)
When there are concurrent operations that want to update the working
copy, it's useful to know which operation was the last to successfully
update the working copy. That can help use decide how to resolve a
mismatch between the repo view's record and the working copy's
record. If we detect such a difference, we can look at the working
copy's operation ID to see if it was updated by an operation before or
after we loaded the repo.

If the working copy's record says that it was updated at operation A
and we have loaded the repo at operation B (after A), we know that the
working copy is stale, so we can automatically update it (or tell the
user to run some command to update it if we think that's more
user-friendly).

Conversely, if we have loaded the repo at operation A and the working
copy's record says that it was updated at operation B, we know that
there was some concurrent operation that updated it. We can then
decide to print a warning telling the user that we skipped updating
because of the conflict. We already have logic for not updating the
working copy if the repo is loaded at an earlier operation, but maybe
we can drop that if we record the operation in the working copy (as
this patch does).
2022-01-19 19:15:29 -08:00
Martin von Zweigbergk
9e1869dcef working_copy: pass in old commit ID to check_out()
`WorkingCopy::check_out()` currently fails if the commit recorded on
disk has changed since it was last read. It fails with a "concurrent
checkout" error. That usually works well in practice, but one can
imagine cases where it's not correct. For an example where the current
behavior is wrong, consider this sequence of events:

 1. Process A loads the repo and working copy.

 2. Process B loads the repo at operation A. It has not loaded the
    working copy yet.

 3. Process A writes an operation and updates the working copy.

 4. Process B loads the working copy and sees that it is checked out
    to the commit process B set it to. We don't currently have any
    checks that the working copy commit matches the view's checkout
    (though I plan to add that).

 5. Process B finishes its operation (which is now divergent with the
    operation written by process A). It updates the working copy to
    the checkout set in the repo view by process B. There's no data
    loss here, but the behavior is surprising because we would usually
    tell the user that we detected a concurrent update to the working
    copy.

We should instead check that the working copy's commit on disk matches
what the previous repo view said, i.e. the view at the start of the
operation we just committed. This patch does that by having the caller
pass in the expected old commit ID.
2022-01-19 09:04:01 -08:00
Martin von Zweigbergk
8c97fdf5d6 working_copy: remove untrack() now that we have more flexible reset() 2022-01-19 09:04:01 -08:00
Martin von Zweigbergk
cd4fbd3565 working_copy: add a reset() function for Git-like reset
We already have two usecases that can be modeled as updating the
`TreeState` without touching the working copy:

 1. `jj untrack` can be implemented as removing paths from the tree
    object and then doing a reset of the working copy state.

 2. Importing Git HEAD when sharing the working copy with a Git repo.

This patch adds that functionality to `TreeState`.
2022-01-19 08:32:59 -08:00
Martin von Zweigbergk
25d19e8a65 working_copy: start improving interface for mutations
This patch changes the interface for making changes to the working
copy by replacing `write_tree()` and `untrack()` by a single
`start_mutation()` method. The two functions now live on the returned
`LockedWorkingCopy` object instead. That is more flexible because the
caller can make multiple changes while the working copy is locked. It
also helps us reduce the risk of buggy callers that read the commit ID
before taking the lock, because we can now make it accessible only on
`LockedWorkingCopy`.
2022-01-19 08:32:59 -08:00
Martin von Zweigbergk
0fadac38d6 working_copy: remove current_commit() (leaving current_commit_id()
`WorkingCopy::current_commit()` has been there from the beginning. It
has made less sense since we made the repo view keep track of the
current checkout. Let's remove it.
2022-01-15 17:11:56 -08:00
Martin von Zweigbergk
277f42d98a revsets: add author() and committer() functions (#46)
Filtering by the author or committer is quite common.
2021-12-15 22:50:29 -08:00
Martin von Zweigbergk
c185b395f6 revsets: swap meaning of operators ~ and - (#46)
As suggested by @arxanas, this makes `-` symmetric with `+` and `-` is
easier to type than `~`.
2021-12-12 23:02:29 -08:00
Martin von Zweigbergk
35a712cc48 revsets: change Git-like range operator ,,, operator to .. (#46) 2021-12-12 00:20:00 -08:00
Martin von Zweigbergk
98659a16e1 revsets: change DAG range operator ,, operator to : (#46) 2021-12-12 00:20:00 -08:00
Martin von Zweigbergk
63c90c04c8 revsets: change parent/children operators to foo~/foo+ (#46) 2021-12-11 23:47:34 -08:00
Martin von Zweigbergk
ccfaf0f601 git: on import, only add ref target as head if target changed (#44)
If you import Git refs, then rebase a commit pointed to by some Git
ref, and then re-import Git refs, you don't want the old commit to be
made a visible head again. That's particularly annoying when Git refs
are automatically updated by every command.
2021-12-11 11:03:40 -08:00
Martin von Zweigbergk
cd0192e1b3 cli: extract a function for importing Git refs and HEAD (#44)
`WorkspaceCommandHelper::for_loaded_repo()` was getting a bit long.
2021-12-11 10:20:30 -08:00
Martin von Zweigbergk
47b3abd0f7 git: add function for exporting to underlying Git repo (#44) 2021-12-11 09:30:12 -08:00
Martin von Zweigbergk
aa78f97d55 git: refactor tests by extracting some common setup (#44) 2021-12-10 23:12:22 -08:00
Martin von Zweigbergk
c678a89794 cleanup: fix some issues reported by new clippy and/or rustc 2021-12-10 14:12:45 -08:00
Martin von Zweigbergk
7b724512dd repo: don't use stale view when checking for changes
I recently (0c441d9558) made it so we don't create an operation when
nothing changed. Soon thereafter (94e03f5ac8), I broke that when I
introduced a cache-invalidation bug when I made the filtering-out of
non-heads be lazy. This patch fixes that and also adds a test to
prevent regressions.
2021-12-05 22:01:48 -08:00
Martin von Zweigbergk
f1bfe85f45 revsets: add git_head() revset
This is part of #44.
2021-12-01 11:08:53 -08:00
Martin von Zweigbergk
8a2f630ac0 git: start tracking HEAD of underlying Git repo
This patch adds a place for tracking the current `HEAD` commit in the
underlying Git repo. It updates `git::import_refs()` to record it. We
don't use it anywhere yet.

This is part of #44.
2021-12-01 11:08:53 -08:00
Martin von Zweigbergk
06bccb3387 transaction: remove Drop implementation
I can't remember when the `Drop` implementation last helped me find a
bug, so let's just remove it.
2021-12-01 10:31:35 -08:00
Martin von Zweigbergk
a27e77205a git: don't import refs/remotes/origin/HEAD as a branch called "HEAD"
`refs/remotes/origin/*` are usually (remote-tracking) branches, but
`refs/remotes/origin/HEAD` is not.
2021-12-01 08:15:36 -08:00
Martin von Zweigbergk
c6cba59c27 workspace: move creation of .jj/ directory from Repo to Workspace 2021-11-25 21:08:43 -08:00
Martin von Zweigbergk
f17aced374 workspace: move search for .jj/ directory from Repo to Workspace 2021-11-25 21:08:43 -08:00
Martin von Zweigbergk
ea172f5f24 tests: when reloading a repo, pass in repo path, not workspace root
I'm about to change `ReadonlyRepo::load()` to take the path to the
`.jj/` directory, so this patch prepares for that. It already works
because `ReadonlyRepo::load()` will search up the directory tree for
the `.jj/` entry.
2021-11-25 21:07:28 -08:00
Martin von Zweigbergk
831a0a7707 tests: remove some more calls to Repo::working_copy_path() 2021-11-25 21:07:28 -08:00
Martin von Zweigbergk
466d35d4bc workspace: add functions for initializing a repo
`ReadonlyRepo::init_*()` currently calls `WorkingCopy::init()`. In
order to remove that dependency, this patch wraps the
`ReadonlyRepo::init_*()` functions in new `Workspace` functions. A
later patch will have those functions call `WorkspaceCopy::init()`.`
2021-11-25 21:07:28 -08:00
Martin von Zweigbergk
c08adba424 repo: remove working_copy(), only used in tests
This is another step towards removing coupling between the repo and
the working copy, so we can have multiple working copies for a single
repo (#13).
2021-11-25 21:04:56 -08:00
Martin von Zweigbergk
afe3bd5c2f testutils: update write_working_copy_file() to take a Workspace 2021-11-25 21:04:56 -08:00
Martin von Zweigbergk
f82459ab1b test: remove a trivial use of Repo::working_copy_path() 2021-11-25 21:04:56 -08:00
Martin von Zweigbergk
d8626645b8 tests: avoid Repo::working_copy_path() in test_bad_locking
I'm about to remove `Repo::working_copy_path()` since I'm further
decoupling the repo from the working copy.
2021-11-25 21:04:56 -08:00
Martin von Zweigbergk
2c91af339b tests: extract variable for store in test_conflicts 2021-11-25 21:04:56 -08:00
Martin von Zweigbergk
ce094f64d5 testutils: make init_repo return a struct, including a Workspace
This is a fairly mechanical change; I'll do some minor cleanups in
later commits.
2021-11-25 21:04:56 -08:00
Martin von Zweigbergk
70073b94a8 repo: stop keeping a WorkingCopy instance
The `Repo` doesn't do anything with the `WorkingCopy` except keeping a
reference to it for its users to use. In fact, the entire lib crate
doesn't do antyhing with the `WorkingCopy`. It therefore seems simpler
to have the users of the crate manage the `WorkingCopy` instance. This
patch does that by letting `Workspace` own it. By not keeping an
instance in `Repo`, which is `Sync`, we can also drop the
`Arc<Mutex<>>` wrapping.

I left `Repo::working_copy()` for convenience for now, but now it
creates a new instance every time. It's only used in tests.

This further decoupling should help us add support for multiple
working copies (#13).
2021-11-25 21:04:56 -08:00
Martin von Zweigbergk
8cf5dd286a backend: make Vec inside CommitId non-public
The recent e5dd93cbf7, whose description says "cleanup: make Vec
inside CommitId etc. non-public", made all ID types in the `backend`
module *except* for `CommitId` non-public :P This patch makes
2021-11-19 23:19:00 -08:00
Martin von Zweigbergk
6c0f3625ff tests: avoid accessing working copy just to get its commit ID
The view has been the source of truth for the currently checked-out
commit for a long time now.
2021-11-17 12:45:18 -08:00
Martin von Zweigbergk
33b272f5fa working_copy: make some functions require mutable references
We use interior mutability for caching in `WorkingCopy`, but let's
still take mutable reference in the functions where the state change
is visible.
2021-11-17 10:15:33 -08:00
Martin von Zweigbergk
287602966e working_copy: add a method for untracking specified paths 2021-11-17 08:41:37 -08:00
Martin von Zweigbergk
ced252f766 cleanup: replace some as_slice() by & 2021-11-10 10:55:58 -08:00
Martin von Zweigbergk
27107637f2 git: fix regression in pruning of remote refs
A while ago, I replaced a call to git2-rs's `Remote::fetch()` by calls
to `Remote::download()` and `Remote::update_tips()`. The function is
documented to be a convenience for those function, but it turns out
that the pruning of deleted remote refs is a separate call
(`Remote::prune()`), so we need to call that too.
2021-11-07 16:02:18 -08:00
Martin von Zweigbergk
e901a4e66b repo: don't materialize conflicts on checkout
Since the working copy can now handle conflicts, we don't need to
materialize conflicts when checking out a commit.

Before this patch, we used to create a new commit on top whenever we
checked out a commit with conflicts. That new commit was intended just
for resolving the conflicts. The typical workflow was the resolve the
conflicts and then amend. To use the same workflow after this patch,
one needs to explicitly create a new commit on top with `jj new` after
checking out a commit with conflict.
2021-11-07 15:17:51 -08:00
Martin von Zweigbergk
ea82340654 working_copy: preserve conflicts in the working copy until markers are removed
I realized only recently that we can try to parse conflict markers in
files and leave them as conflicted if they haven't changed. If they
have changed and some conflict markers have been removed, we can even
update the conflict with that partial resolution.

This change teaches the working copy to write conflicts to the working
copy. It used to expect that the caller had already updated the tree
by materializing conflicts. With this change, we also start parsing
the conflict markers and leave the conflicts unresolved in the working
copy if the conflict markers remain.

There are some cases that we don't handle yet. For example, we don't
even try to set the executable bit correctly when we write
conflicts. OTOH, we didn't do that even before this change.

We still never actually write conflicts to the working copy (outside
of tests) because we currently materialize conflicts in
`MutRepo::check_out()`. I'll change that next.
2021-11-07 15:17:51 -08:00
Martin von Zweigbergk
cea6061f3d conflicts: add a function for parsing a materialized conflict
I initially made the working copy materialize conflicts in its
`check_out()` method. Then I changed it later (exactly a year ago, on
Halloween of 2020, actually) so that the working copy expected
conflicts to already have been materalized, which happens in
`MutableRepo::check_out`().

I think my reasoning then was that the file system cannot represent a
conflict. While it's true that the file system itself doesn't have
information to know whether a file represents a conflict, we can
record that ourselves. We already record whether a file is executable
or not and then preserve that if we're on a file system that isn't
able to record it. It's not that different to do the same for
conflicts if we're on a file system that doesn't understand conflicts
(i.e. all file systems).

The plan is to have the working copy remember whether a file
represents a conflict. When we check if it has changed, we parse the
file, including conflict markers, and recreate the conflict from
it. We should be able to do that losslessly (and we should adjust
formats to make it possible if we find cases where it's not).

Having the working copy preserve conflict states has several
advantages:

 * Because conflicts are not materialized in the working copy, you can
   rebase the conflicted commit and the working copy without causing
   more conflicts (that's currently a UX bug I run into every now and
   then).

 * If you don't change anything in the working copy, it will be
   unchanged compared to its parent, which means we'll automatically
   abandon it if you update away from it.

 * The user can choose to resolve only some of the conflicts in a file
   and squash those in, and it'll work they way you'd hope.

 * It should make it easier to implement support for external merge
   tools (#18) without having them treat the working copy differently.

This patch prepares for that work by adding support for parsing
materialized conflicts.
2021-11-07 15:17:51 -08:00
Martin von Zweigbergk
9abee0096f conflicts: propagate errors from materialize_conflict()
All current callers pass in a buffer, so it should never fail, but the
function itself can't know that.
2021-10-24 23:02:00 -07:00
Martin von Zweigbergk
b4a0e513dd revset graph: make order of edges stable
While working on demos, I noticed that `jj log` output in the
octocat/Hello-World repo was unstable: sometimes the first parent of
the merge was on the left and sometimes it was on the right. This
patch fixes that by sorting the edges by position in the index just
before returning them. It seems that most applications would want
stable output so I put it in the `RevsetGraphIterator` rather than
doing at the call site in the CLI. I ordered them with the reverse
index position rather than forward because it seemed to make the
graphs in the git.git repo slight nicer, with the left-most edge going
between subsequent releases.

There performance difference is within the noise level.
2021-10-23 20:33:59 -07:00
Martin von Zweigbergk
33bf6ce1d5 view: don't force commits pointed to by refs to be current heads
If you rewrite a commit that's also available on some remote, you'll
currently see both the old version and the new version in the view,
which means they're divergent. They're not logically divergent (the
rewritten version should replace the old version), so this is a UX
bug. I think it indicates that the set of current heads should be
redefined to be the *desired* heads. That's also what I had suspected
in the TODO removed by this change.  I think another indication that
we should hide the old heads even if they have e.g. a remote branch
pointing to them is that we don't want them to be rebased if we
rewrite an ancestor.

So that's what I decided to do: let the view's heads be the desired
heads. The user can still define revsets for showing non-current
commits pointed to by e.g. remote branches.
2021-10-23 09:15:05 -07:00
Martin von Zweigbergk
c0ae4b16e8 trees: try to resolve file conflicts after simplifying conflicts
This fixes a bug I've run into somewhat frequently. What happens is
that if you have a conflict on top of another conflict and you resolve
the conflict in the bottom commit, we just simplify the `Conflict`
object in the second commit, but we don't try to resolve the new
conflict. That shows up as an unexpected "conflict" in `jj log`
output, and when you check out the commit, there are actually no
conflicts, so you can just `jj squash` right away.

This patch fixes that bug. It also teaches the code to work with more
than 3 parts in the merge, so if there's a 5-way conflict, for
example, we still try to resolve it if possible.
2021-10-22 09:20:50 -07:00
Martin von Zweigbergk
2e4dc019d9 git: don't update public heads for now 2021-10-20 14:23:58 -07:00