git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 00/10] Add the Git Change command
@ 2022-09-23 18:55 Christophe Poucet via GitGitGadget
  2022-09-23 18:55 ` [PATCH 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
                   ` (12 more replies)
  0 siblings, 13 replies; 66+ messages in thread
From: Christophe Poucet via GitGitGadget @ 2022-09-23 18:55 UTC (permalink / raw)
  To: git; +Cc: Christophe Poucet

I'm reviving the original git evolve work that was started by
sxenos@google.com
(https://public-inbox.org/git/20190215043105.163688-1-sxenos@google.com/)

This work is intended to make it easier to deal with stacked changes.

The following set of patches introduces the design doc on the evolve command
as well as the basics of the git change command.

Chris Poucet (4):
  sha1-array: implement oid_array_readonly_contains
  ref-filter: add the metas namespace to ref-filter
  evolve: add delete command
  evolve: add documentation for `git change`

Stefan Xenos (6):
  technical doc: add a design doc for the evolve command
  evolve: add support for parsing metacommits
  evolve: add the change-table structure
  evolve: add support for writing metacommits
  evolve: implement the git change command
  evolve: add the git change list command

 .gitignore                         |    1 +
 Documentation/git-change.txt       |   55 ++
 Documentation/technical/evolve.txt | 1051 ++++++++++++++++++++++++++++
 Makefile                           |    4 +
 builtin.h                          |    1 +
 builtin/change.c                   |  342 +++++++++
 change-table.c                     |  179 +++++
 change-table.h                     |  132 ++++
 git.c                              |    1 +
 metacommit-parser.c                |  110 +++
 metacommit-parser.h                |   19 +
 metacommit.c                       |  404 +++++++++++
 metacommit.h                       |   58 ++
 oid-array.c                        |   12 +
 oid-array.h                        |    7 +
 ref-filter.c                       |   10 +-
 ref-filter.h                       |    8 +-
 t/helper/test-oid-array.c          |    6 +
 t/t0064-oid-array.sh               |   22 +
 19 files changed, 2418 insertions(+), 4 deletions(-)
 create mode 100644 Documentation/git-change.txt
 create mode 100644 Documentation/technical/evolve.txt
 create mode 100644 builtin/change.c
 create mode 100644 change-table.c
 create mode 100644 change-table.h
 create mode 100644 metacommit-parser.c
 create mode 100644 metacommit-parser.h
 create mode 100644 metacommit.c
 create mode 100644 metacommit.h


base-commit: 4b79ee4b0cd1130ba8907029cdc5f6a1632aca26
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1356%2Fpoucet%2Fevolve-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1356/poucet/evolve-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1356
-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 66+ messages in thread

* [PATCH 01/10] technical doc: add a design doc for the evolve command
  2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
@ 2022-09-23 18:55 ` Stefan Xenos via GitGitGadget
  2022-09-23 19:59   ` Jerry Zhang
                     ` (3 more replies)
  2022-09-23 18:55 ` [PATCH 02/10] sha1-array: implement oid_array_readonly_contains Chris Poucet via GitGitGadget
                   ` (11 subsequent siblings)
  12 siblings, 4 replies; 66+ messages in thread
From: Stefan Xenos via GitGitGadget @ 2022-09-23 18:55 UTC (permalink / raw)
  To: git; +Cc: Christophe Poucet, Stefan Xenos

From: Stefan Xenos <sxenos@google.com>

This document describes what a change graph for
git would look like, the behavior of the evolve command,
and the changes planned for other commands.

It was originally proposed in 2018, see
https://public-inbox.org/git/20181115005546.212538-1-sxenos@google.com/

Signed-off-by: Stefan Xenos <sxenos@google.com>
Signed-off-by: Chris Poucet <poucet@google.com>
---
 Documentation/technical/evolve.txt | 1051 ++++++++++++++++++++++++++++
 1 file changed, 1051 insertions(+)
 create mode 100644 Documentation/technical/evolve.txt

diff --git a/Documentation/technical/evolve.txt b/Documentation/technical/evolve.txt
new file mode 100644
index 00000000000..68ee2457e52
--- /dev/null
+++ b/Documentation/technical/evolve.txt
@@ -0,0 +1,1051 @@
+Evolve
+======
+
+Objective
+=========
+Create an "evolve" command to help users craft a high quality commit history.
+Users can improve commits one at a time and in any order, then run git evolve to
+rewrite their recent history to ensure everything is up-to-date. We track
+amendments to a commit over time in a change graph. Users can share their
+progress with others by exchanging their change graphs using the standard push,
+fetch, and format-patch commands.
+
+Status
+======
+This proposal has not been implemented yet.
+
+Background
+==========
+Imagine you have three sequential changes up for review and you receive feedback
+that requires editing all three changes. We'll define the word "change"
+formally later, but for the moment let's say that a change is a work-in-progress
+whose final version will be submitted as a commit in the future.
+
+While you're editing one change, more feedback arrives on one of the others.
+What do you do?
+
+The evolve command is a convenient way to work with chains of commits that are
+under review. Whenever you rebase or amend a commit, the repository remembers
+that the old commit is obsolete and has been replaced by the new one. Then, at
+some point in the future, you can run "git evolve" and the correct sequence of
+rebases will occur in the correct order such that no commit has an obsolete
+parent.
+
+Part of making the "evolve" command work involves tracking the edits to a commit
+over time, which is why we need an change graph. However, the change
+graph will also bring other benefits:
+
+- Users can view the history of a change directly (the sequence of amends and
+  rebases it has undergone, orthogonal to the history of the branch it is on).
+- It will be possible to quickly locate and list all the changes the user
+  currently has in progress.
+- It can be used as part of other high-level commands that combine or split
+  changes.
+- It can be used to decorate commits (in git log, gitk, etc) that are either
+  obsolete or are the tip of a work in progress.
+- By pushing and pulling the change graph, users can collaborate more
+  easily on changes-in-progress. This is better than pushing and pulling the
+  commits themselves since the change graph can be used to locate a more
+  specific merge base, allowing for better merges between different versions of
+  the same change.
+- It could be used to correctly rebase local changes and other local branches
+  after running git-filter-branch.
+- It can replace the change-id footer used by gerrit.
+
+Goals
+-----
+Legend: Goals marked with P0 are required. Goals marked with Pn should be
+attempted unless they interfere with goals marked with Pn-1.
+
+P0. All commands that modify commits (such as the normal commit --amend or
+    rebase command) should mark the old commit as being obsolete and replaced by
+    the new one. No additional commands should be required to keep the
+    change graph up-to-date.
+P0. Any commit that may be involved in a future evolve command should not be
+    garbage collected. Specifically:
+    - Commits that obsolete another should not be garbage collected until
+      user-specified conditions have occurred and the change has expired from
+      the reflog. User specified conditions for removing changes include:
+      - The user explicitly deleted the change.
+      - The change was merged into a specific branch.
+    - Commits that have been obsoleted by another should not be garbage
+      collected if any of their replacements are still being retained.
+P0. A commit can be obsoleted by more than one replacement (called divergence).
+P0. Users must be able to resolve divergence (convergence).
+P1. Users should be able to share chains of obsolete changes in order to
+    collaborate on WIP changes.
+P2. Such sharing should be at the user’s option. That is, it should be possible
+    to directly share a change without also sharing the file states or commit
+    comments from the obsolete changes that led up to it, and the choice not to
+    share those commits should not require changing any commit hashes.
+P2. It should be possible to discard part or all of the change graph
+    without discarding the commits themselves that are already present in
+    branches and the reflog.
+P2. Provide sufficient information to replace gerrit's Change-Id footers.
+
+Similar technologies
+--------------------
+There are some other technologies that address the same end-user problem.
+
+Rebase -i can be used to solve the same problem, but users can't easily switch
+tasks midway through an interactive rebase or have more than one interactive
+rebase going on at the same time. It can't handle the case where you have
+multiple changes sharing the same parent when that parent needs to be rebased
+and won't let you collaborate with others on resolving a complicated interactive
+rebase. You can think of rebase -i as a top-down approach and the evolve command
+as the bottom-up approach to the same problem.
+
+Several patch queue managers have been built on top of git (such as topgit,
+stgit, and quilt). They address the same user need. However they also rely on
+state managed outside git that needs to be kept in sync. Such state can be
+easily damaged when running a git native command that is unaware of the patch
+queue. They also typically require an explicit initialization step to be done by
+the user which creates workflow problems.
+
+Mercurial implements a very similar feature in its EvolveExtension. The behavior
+of the evolve command itself is very similar, but the storage format for the
+change graph differs. In the case of mercurial, each change set can have one or
+more obsolescence markers that point to other changesets that they replace. This
+is similar to the "Commit Headers" approach considered in the other options
+appendix. The approach proposed here stores obsolescence information in a
+separate metacommit graph, which makes exchanging of obsolescence information
+optional.
+
+Mercurial's default behavior makes it easy to find and switch between
+non-obsolete changesets that aren't currently on any branch. We introduce the
+notion of a new ref namespace that enables a similar workflow via a different
+mechanism. Mercurial has the notion of changeset phases which isn't present
+in git and creates new ways for a changeset to diverge. Git doesn't need
+to deal with these issues, but it has to deal with the problems of picking an
+upstream branch as a target for rebases and protecting obsolescence information
+from GC. We also introduce some additional transformations (see
+obsolescence-over-cherry-pick, below) that aren't present in the mercurial
+implementation.
+
+Semi-related work
+-----------------
+There are other technologies that address different problems but have some
+similarities with this proposal.
+
+Replacements (refs/replace) are superficially similar to obsolescences in that
+they describe that one commit should be replaced by another. However, they
+differ in both how they are created and how they are intended to be used.
+Obsolescences are created automatically by the commands a user runs, and they
+describe the user’s intent to perform a future rebase. Obsolete commits still
+appear in branches, logs, etc like normal commits (possibly with an extra
+decoration that marks them as obsolete). Replacements are typically created
+explicitly by the user, they are meant to be kept around for a long time, and
+they describe a replacement to be applied at read-time rather than as the input
+to a future operation. When a replaced commit is queried, it is typically hidden
+and swapped out with its replacement as though the replacement has already
+occurred.
+
+Git-imerge is a project to help make complicated merges easier, particularly
+when merging or rebasing long chains of patches. It is not an alternative to
+the change graph, but its algorithm of applying smaller incremental merges
+could be used as part of the evolve algorithm in the future.
+
+Overview
+========
+We introduce the notion of “meta-commits” which describe how one commit was
+created from other commits. A branch of meta-commits is known as a change.
+Changes are created and updated automatically whenever a user runs a command
+that creates a commit. They are used for locating obsolete commits, providing a
+list of a user’s unsubmitted work in progress, and providing a stable name for
+each unsubmitted change.
+
+Users can exchange edit histories by pushing and fetching changes.
+
+New commands will be introduced for manipulating changes and resolving
+divergence between them. Existing commands that create commits will be updated
+to modify the meta-commit graph and create changes where necessary.
+
+Example usage
+-------------
+# First create three dependent changes
+$ echo foo>bar.txt && git add .
+$ git commit -m "This is a test"
+created change metas/this_is_a_test
+$ echo foo2>bar2.txt && git add .
+$ git commit -m "This is also a test"
+created change metas/this_is_also_a_test
+$ echo foo3>bar3.txt && git add .
+$ git commit -m "More testing"
+created change metas/more_testing
+
+# List all our changes in progress
+$ git change list
+metas/this_is_a_test
+metas/this_is_also_a_test
+* metas/more_testing
+metas/some_change_already_merged_upstream
+
+# Now modify the earliest change, using its stable name
+$ git reset --hard metas/this_is_a_test
+$ echo morefoo>>bar.txt && git add . && git commit --amend --no-edit
+
+# Use git-evolve to fix up any dependent changes
+$ git evolve
+rebasing metas/this_is_also_a_test onto metas/this_is_a_test
+rebasing metas/more_testing onto metas/this_is_also_a_test
+Done
+
+# Use git-obslog to view the history of the this_is_a_test change
+$ git log --obslog
+93f110 metas/this_is_a_test@{0} commit (amend): This is a test
+930219 metas/this_is_a_test@{1} commit: This is a test
+
+# Now create an unrelated change
+$ git reset --hard origin/master
+$ echo newchange>unrelated.txt && git add .
+$ git commit -m "Unrelated change"
+created change metas/unrelated_change
+
+# Fetch the latest code from origin/master and use git-evolve
+# to rebase all dependent changes.
+$ git fetch origin master
+$ git evolve origin/master
+deleting metas/some_change_already_merged_upstream
+rebasing metas/this_is_a_test onto origin/master
+rebasing metas/this_is_also_a_test onto metas/this_is_a_test
+rebasing metas/more_testing onto metas/this_is_also_a_test
+rebasing metas/unrelated_change onto origin/master
+Conflict detected! Resolve it and then use git evolve --continue to resume.
+
+# Sort out the conflict
+$ git mergetool
+$ git evolve origin/master
+Done
+
+# Share the full history of edits for the this_is_a_test change
+# with a review server
+$ git push origin metas/this_is_a_test:refs/for/master
+# Share the lastest commit for “Unrelated change”, without history
+$ git push origin HEAD:refs/for/master
+
+Detailed design
+===============
+Obsolescence information is stored as a graph of meta-commits. A meta-commit is
+a specially-formatted merge commit that describes how one commit was created
+from others.
+
+Meta-commits look like this:
+
+$ git cat-file -p <example_meta_commit>
+tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
+parent aa7ce55545bf2c14bef48db91af1a74e2347539a
+parent d64309ee51d0af12723b6cb027fc9f195b15a5e9
+parent 7e1bbcd3a0fa854a7a9eac9bf1eea6465de98136
+author Stefan Xenos <sxenos@gmail.com> 1540841596 -0700
+committer Stefan Xenos <sxenos@gmail.com> 1540841596 -0700
+parent-type c r o
+
+This says “commit aa7ce555 makes commit d64309ee obsolete. It was created by
+cherry-picking commit 7e1bbcd3”.
+
+The tree for meta-commits is always the empty tree, but future versions of git
+may attach other trees here. For forward-compatibility fsck should ignore such
+trees if found on future repository versions. This will allow future versions of
+git to add metadata to the meta-commit tree without breaking forwards
+compatibility.
+
+The commit comment for a meta-commit is an auto-generated user-readable string
+describing the command that produced the meta commit. These strings are shown
+to the user when they view the obslog.
+
+Parent-type
+-----------
+The “parent-type” field in the commit header identifies a commit as a
+meta-commit and indicates the meaning for each of its parents. It is never
+present for normal commits. It contains a space-deliminated list of enum values
+whose order matches the order of the parents. Possible parent types are:
+
+- c: (content) the content parent identifies the commit that this meta-commit is
+  describing.
+- r: (replaced) indicates that this parent is made obsolete by the content
+  parent.
+- o: (origin) indicates that the content parent was generated by cherry-picking
+  this parent.
+- a: (abandoned) used in place of a content parent for abandoned changes. Points
+  to the final content commit for the change at the time it was abandoned.
+
+There must be exactly one content or abandoned parent for each meta-commit and
+it is always the first parent. The content commit will always be a normal commit
+and not a meta-commit. However, future versions of git may create meta-commits
+for other meta-commits and the fsck tool must be aware of this for forwards
+compatibility.
+
+A meta-commit can have zero or more replaced parents. An amend operation creates
+a single replaced parent. A merge used to resolve divergence (see divergence,
+below) will create multiple replaced parents. A meta-commit may have no
+replaced parents if it describes a cherry-pick or squash merge that copies one
+or more commits but does not replace them.
+
+A meta-commit can have zero or more origin parents. A cherry-pick creates a
+single origin parent. Certain types of squash merge will create multiple origin
+parents. Origin parents don't directly cause their origin to become obsolete,
+but are used when computing blame or locating a merge base. The section
+on obsolescence over cherry-picks describes how the evolve command uses
+origin parents.
+
+A replaced parent or origin parent may be either a normal commit (indicating
+the oldest-known version of a change) or another meta-commit (for a change that
+has already been modified one or more times).
+
+The parent-type field needs to go after the committer field since git's rules
+for forwards-compatibility require that new fields to be at the end of the
+header. Putting a new field in the middle of the header would break fsck.
+
+The presence of an abandoned parent indicates that the change should be pruned
+by the evolve command, and removed from the repository's history. Any follow-up
+changes should rebased onto the parent of the pruned commit. The abandoned
+parent points to the version of the change that should be restored if the user
+attempts to restore the change.
+
+Changes
+-------
+A branch of meta-commits describes how a commit was produced and what previous
+commits it is based on. It is also an identifier for a thing the user is
+currently working on. We refer to such a meta-branch as a change.
+
+Local changes are stored in the new refs/metas namespace. Remote changes are
+stored in the refs/remote/<remotename>/metas namespace.
+
+The list of changes in refs/metas is more than just a mechanism for the evolve
+command to locate obsolete commits. It is also a convenient list of all of a
+user’s work in progress and their current state - a list of things they’re
+likely to want to come back to.
+
+Strictly speaking, it is the presence of the branch in the refs/metas namespace
+that marks a branch as being a change, not the fact that it points to a
+metacommit. Metacommits are only created when a commit is amended or rebased, so
+in the case where a change points to a commit that has never been modified, the
+change points to that initial commit rather than a metacommit.
+
+Changes are also stored in the refs/hiddenmetas namespace. Hiddenmetas holds
+metadata for historical changes that are not currently in progress by the user.
+Commands like filter-branch and other bulk import commands create metadata in
+this namespace.
+
+Note that the changes in hiddenmetas get special treatment in several ways:
+
+- They are not cleaned up automatically once merged, since it is expected that
+  they refer to historical changes.
+- User commands that modify changes don't append to these changes as they would
+  to a change in refs/metas.
+- They are not displayed when the user lists their local changes.
+
+Obsolescence
+------------
+A commit is considered obsolete if it is reachable from the “replaces” edges
+anywhere in the history of a change and it isn’t the head of that change.
+Commits may be the content for 0 or more meta-commits. If the same commit
+appears in multiple changes, it is not obsolete if it is the head of any of
+those changes.
+
+Note that there is an exception to this rule. The metas namespace takes
+precedence over the hiddenmetas namespace for the purpose of obsolescence. That
+is, if a change appears in a replaces edge of a change in the metas namespace,
+it is obsolete even if it also appears as the head of a change in the
+hiddenmetas namespace.
+
+This special case prevents the hiddenmetas namespace from creating divergence
+with the user's work in progress, and allows the user to resolve historical
+divergence by creating new changes in the metas namespace.
+
+Divergence
+----------
+From the user’s perspective, two changes are divergent if they both ask for
+different replacements to the same commit. More precisely, a target commit is
+considered divergent if there is more than one commit at the head of a change in
+refs/metas that leads to the target commit via an unbroken chain of “replaces”
+parents.
+
+Much like a merge conflict, divergence is a situation that requires user
+intervention to resolve. The evolve command will stop when it encounters
+divergence and prompt the user to resolve the problem. Users can solve the
+problem in several ways:
+
+- Discard one of the changes (by deleting its change branch).
+- Merge the two changes (producing a single change branch).
+- Copy one of the changes (keep both commits, but one of them gets a new
+  metacommit appended to its history that is connected to its predecessor via an
+  origin edge rather than a replaces edge. That new change no longer obsoletes
+  the original.)
+
+Obsolescence across cherry-picks
+--------------------------------
+By default the evolve command will treat cherry-picks and squash merges as being
+completely separate from the original. Further amendments to the original commit
+will have no effect on the cherry-picked copy. However, this behavior may not be
+desirable in all circumstances.
+
+The evolve command may at some point support an option to look for cases where
+the source of a cherry-pick or squash merge has itself been amended, and
+automatically apply that same change to the cherry-picked copy. In such cases,
+it would traverse origin edges rather than ignoring them, and would treat a
+commit with origin edges as being obsolete if any of its origins were obsolete.
+
+Garbage collection
+------------------
+For GC purposes, meta-commits are normal commits. Just as a commit causes its
+parents and tree to be retained, a meta-commit also causes its parents to be
+retained.
+
+Change creation
+---------------
+Changes are created automatically whenever the user runs a command like “commit”
+that has the semantics of creating a new change. They also move forward
+automatically even if they’re not checked out. For example, whenever the user
+runs a command like “commit --amend” that modifies a commit, all branches in
+refs/metas that pointed to the old commit move forward to point to its
+replacement instead. This also happens when the user is working from a detached
+head.
+
+This does not mean that every commit has a corresponding change. By default,
+changes only exist for recent locally-created commits. Users may explicitly pull
+changes from other users or keep their changes around for a long time, but
+either behavior requires a user to opt-in. Code review systems like gerrit may
+also choose to keep changes around forever.
+
+Note that the changes in refs/metas serve a dual function as both a way to
+identify obsolete changes and as a way for the user to keep track of their work
+in progress. If we were only concerned with identifying obsolete changes, it
+would be sufficient to create the change branch lazily the first time a commit
+is obsoleted. Addressing the second use - of refs/metas as a mechanism for
+keeping track of work in progress - is the reason for eagerly creating the
+change on first commit.
+
+Change naming
+-------------
+When a change is first created, the only requirement for its name is that it
+must be unique. Good names would also serve as useful mnemonics and be easy to
+type. For example, a short word from the commit message containing no numbers or
+special characters and that shows up with low frequency in other commit messages
+would make a good choice.
+
+Different users may prefer different heuristics for their change names. For this
+reason a new hook will be introduced to compute change names. Git will invoke
+the hook for all newly-created changes and will append a numeric suffix if the
+name isn’t unique. The default heuristics are not specified by this proposal and
+may change during implementation.
+
+Change deletion
+---------------
+Changes are normally only interesting to a user while a commit is still in
+development and under review. Once the commit has submitted wherever it is
+going, its change can be discarded.
+
+The normal way of deleting changes makes this easy to do - changes are deleted
+by the evolve command when it detects that the change is present in an upstream
+branch. It does this in two ways: if the latest commit in a change either shows
+up in the branch history or the change becomes empty after a rebase, it is
+considered merged and the change is discarded. In this context, an “upstream
+branch” is any branch passed in as the upstream argument of the evolve command.
+
+In case this sometimes deletes a useful change, such automatic deletions are
+recorded in the reflog allowing them to be easily recovered.
+
+Sharing changes
+---------------
+Change histories are shared by pushing or fetching meta-commits and change
+branches. This provides users with a lot of control of what to share and
+repository implementations with control over what to retain.
+
+Users that only want to share the content of a commit can do so by pushing the
+commit itself as they currently would. Users that want to share an edit history
+for the commit can push its change, which would point to a meta-commit rather
+than the commit itself if there is any history to share. Note that multiple
+changes can refer to the same commits, so it’s possible to construct and push a
+different history for the same commit in order to remove sensitive or irrelevant
+intermediate states.
+
+Imagine the user is working on a change “mychange” that is currently the latest
+commit on master. They have two ways to share it:
+
+# User shares just a commit without its history
+> git push origin master
+
+# User shares the full history of the commit to a review system
+> git push origin metas/mychange:refs/for/master
+
+# User fetches a collaborator’s modifications to their change
+> git fetch remotename metas/mychange
+# Which updates the ref remote/remotename/metas/mychange
+
+This will cause more intermediate states to be shared with the server than would
+have been shared previously. A review system like gerrit would need to keep
+track of which states had been explicitly pushed versus other intermediate
+states in order to de-emphasize (or hide) the extra intermediate states from the
+user interface.
+
+Merge-base
+----------
+Merge-base will be changed to search the meta-commit graph for common ancestors
+as well as the commit graph, and will generally prefer results from the
+meta-commit graph over the commit graph. Merge-base will consider meta-commits
+from all changes, and will traverse both origin and obsolete edges.
+
+The reason for this is that - when merging two versions of the same commit
+together - an earlier version of that same commit will usually be much more
+similar than their common parent. This should make the workflow of collaborating
+on unsubmitted patches as convenient as the workflow for collaborating in a
+topic branch by eliminating repeated merges.
+
+Configuration
+-------------
+The core.enableChanges configuration variable enables the creation and update
+of change branches. This is enabled by default.
+
+User interface
+--------------
+All git porcelain commands that create commits are classified as having one of
+four behaviors: modify, create, copy, or import. These behaviors are discussed
+in more detail below.
+
+Modify commands
+---------------
+Modification commands (commit --amend, rebase) will mark the old commit as
+obsolete by creating a new meta-commit that references the old one as a
+replaced parent. In the event that multiple changes point to the same commit,
+this is done independently for every such change.
+
+More specifically, modifications work like this:
+
+1. Locate all existing changes for which the old commit is the content for the
+   head of the change branch. If no such branch exists, create one that points
+   to the old commit. Changes that include this commit in their history but not
+   at their head are explicitly not included.
+2. For every such change, create a new meta-commit that references the new
+   commit as its content and references the old head of the change as a
+   replaced parent.
+3. Move the change branch forward to point to the new meta-commit.
+
+Copy commands
+-------------
+Copy commands (cherry-pick, merge --squash) create a new meta-commit that
+references the old commits as origin parents. Besides the fact that the new
+parents are tagged differently, copy commands work the same way as modify
+commands.
+
+Create commands
+---------------
+Creation commands (commit, merge) create a new commit and a new change that
+points to that commit. The do not create any meta-commits.
+
+Import commands
+---------------
+Import commands (fetch, pull) do not create any new meta-commits or changes
+unless that is specifically what they are importing. For example, the fetch
+command would update remote/origin/metas/change35 and fetch all referenced
+meta-commits if asked to do so directly, but it wouldn’t create any changes or
+meta-commits for commits discovered on the master branch when running “git fetch
+origin master”.
+
+Other commands
+--------------
+Some commands don’t fit cleanly into one of the above categories.
+
+Semantically, filter-branch should be treated as a modify command, but doing so
+is likely to create a lot of irrelevant clutter in the changes namespace and the
+large number of extra change refs may introduce performance problems. We
+recommend treating filter-branch as an import command initially, but making it
+behave more like a modify command in future follow-up work. One possible
+solution may be to treat commits that are part of existing changes as being
+modified but to avoid creating changes for other rewritten changes. Another
+solution may be to record the modifications as changes in the hiddenmetas
+namespace.
+
+Once the evolve command can handle obsolescence across cherry-picks, such
+cherry-picks will result in a hybrid move-and-copy operation. It will create
+cherry-picks that replace other cherry-picks, which will have both origin edges
+(pointing to the new source commit being picked) and replacement edges (pointing
+to the previous cherry-pick being replaced).
+
+Evolve
+------
+The evolve command performs the correct sequence of rebases such that no change
+has an obsolete parent. The syntax looks like this:
+
+git evolve [upstream…]
+
+It takes an optional list of upstream branches. All changes whose parent shows
+up in the history of one of the upstream branches will be rebased onto the
+upstream branch before resolving obsolete parents.
+
+Any change whose latest state is found in an upstream branch (or that ends up
+empty after rebase) will be deleted. This is the normal mechanism for deleting
+changes. Changes are created automatically on the first commit, and are deleted
+automatically when evolve determines that they’ve been merged upstream.
+
+Orphan commits are commits with obsolete parents. The evolve command then
+repeatedly rebases orphan commits with non-orphan parents until there are either
+no orphan commits left, or a merge conflict is discovered. It will also
+terminate if it detects a divergent parent or a cycle that can't be resolved
+using any of the enabled transformations.
+
+When evolve discovers divergence, it will first check if it can resolve the
+divergence automatically using one of its enabled transformations. Supported
+transformations are:
+
+- Check if the user has already merged the divergent changes in a follow-up
+  change. That is, look for an existing merge in a follow-up change where all
+  the parents are divergent versions of the same change. Squash that merge with
+  its parents and use the result as the resolution for the divergence.
+
+- Attempt to auto-merge all the divergent changes (disabled by default).
+
+Each of the transformations can be enabled or disabled by command line options.
+
+Cycles can occur when two changes reference one another as parents. This can
+happen when both changes use an obsolete version of the other change as their
+parent. Although there are never cycles in the commit graph, users can create
+cycles in the change graph by rebasing changes onto obsolete commits. The evolve
+command has a transformation that will detect and break cycles by arbitrarily
+picking one of the changes to go first. If this generates a merge conflict,
+it tries each of the other changes in sequence to see if any ordering merges
+cleanly. If no possible ordering merges cleanly, it picks one and terminates
+to let the user resolve the merge conflict.
+
+If the working tree is dirty, evolve will attempt to stash the user's changes
+before applying the evolve and then reapply those changes afterward, in much
+the same way as rebase --autostash does.
+
+Checkout
+--------
+Running checkout on a change by name has the same effect as checking out a
+detached head pointing to the latest commit on that change-branch. There is no
+need to ever have HEAD point to a change since changes always move forward when
+necessary, no matter what branch the user has checked out
+
+Meta-commits themselves cannot be checked out by their hash.
+
+Reset
+-----
+Resetting a branch to a change by name is the same as resetting to the content
+(or abandoned) commit at that change’s head.
+
+Commit
+------
+Commit --amend gets modify semantics and will move existing changes forward. The
+normal form of commit gets create semantics and will create a new change.
+
+$ touch foo && git add . && git commit -m "foo" && git tag A
+$ touch bar && git add . && git commit -m "bar" && git tag B
+$ touch baz && git add . && git commit -m "baz" && git tag C
+
+This produces the following commits:
+A(tree=[foo])
+B(tree=[foo, bar], parent=A)
+C(tree=[foo, bar, baz], parent=B)
+
+...along with three changes:
+metas/foo = A
+metas/bar = B
+metas/baz = C
+
+Running commit --amend does the following:
+$ git checkout B
+$ touch zoom && git add . && git commit --amend -m "baz and zoom"
+$ git tag D
+
+Commits:
+A(tree=[foo])
+B(tree=[foo, bar], parent=A)
+C(tree=[foo, bar, baz], parent=B)
+D(tree=[foo, bar, zoom], parent=A)
+Dmeta(content=D, obsolete=B)
+
+Changes:
+metas/foo = A
+metas/bar = Dmeta
+metas/baz = C
+
+Merge
+-----
+Merge gets create, modify, or copy semantics based on what is being merged and
+the options being used.
+
+The --squash version of merge gets copy semantics (it produces a new change that
+is marked as a copy of all the original changes that were squashed into it).
+
+The “modify” version of merge replaces both of the original commits with the
+resulting merge commit. This is one of the standard mechanisms for resolving
+divergence. The parents of the merge commit are the parents of the two commits
+being merged. The resulting commit will not be a merge commit if both of the
+original commits had the same parent or if one was the parent of the other.
+
+The “create” version of merge creates a new change pointing to a merge commit
+that has both original commits as parents. The result is what merge produces now
+- a new merge commit. However, this version of merge doesn’t directly resolve
+divergence.
+
+To select between these two behaviors, merge gets new “--amend” and “--noamend”
+options which select between the “create” and “modify” behaviors respectively,
+with noamend being the default.
+
+For example, imagine we created two divergent changes like this:
+
+$ touch foo && git add . && git commit -m "foo" && git tag A
+$ touch bar && git add . && git commit -m "bar" && git tag B
+$ touch baz && git add . && git commit --amend -m "bar and baz"
+$ git tag C
+$ git checkout B
+$ touch bam && git add . && git commit --amend -m "bar and bam"
+$ git tag D
+
+At this point the commit graph looks like this:
+
+A(tree=[foo])
+B(tree=[bar], parent=A)
+C(tree=[bar, baz], parent=A)
+D(tree=[bar, bam], parent=A)
+Cmeta(content=C, obsoletes=B)
+Dmeta(content=D, obsoletes=B)
+
+There would be three active changes with heads pointing as follows:
+
+metas/changeA=A
+metas/changeB=Cmeta
+metas/changeB2=Dmeta
+
+ChangeB and changeB2 are divergent at this point. Lets consider what happens if
+perform each type of merge between changeB and changeB2.
+
+Merge example: Amend merge
+One way to resolve divergent changes is to use an amend merge. Recall that HEAD
+is currently pointing to D at this point.
+
+$ git merge --amend metas/changeB
+
+Here we’ve asked for an amend merge since we’re trying to resolve divergence
+between two versions of the same change. There are no conflicts so we end up
+with this:
+
+E(tree=[bar, baz, bam], parent=A)
+Emeta(content=E, obsoletes=[Cmeta, Dmeta])
+
+With the following branches:
+
+metas/changeA=A
+metas/changeB=Emeta
+metas/changeB2=Emeta
+
+Notice that the result of the “amend merge” is a replacement for C and D rather
+than a new commit with C and D as parents (as a normal merge would have
+produced). The parents of the amend merge are the parents of C and D which - in
+this case - is just A, so the result is not a merge commit. Also notice that
+changeB and changeB2 are now aliases for the same change.
+
+Merge example: Noamend merge
+Consider what would have happened if we’d used a noamend merge instead. Recall
+that HEAD was at D and our branches looked like this:
+
+metas/changeA=A
+metas/changeB=Cmeta
+metas/changeB2=Dmeta
+
+$ git merge --noamend metas/changeB
+
+That would produce the sort of merge we’d normally expect today:
+
+F(tree=[bar, baz, bam], parent=[C, D])
+
+And our changes would look like this:
+metas/changeA=A
+metas/changeB=Cmeta
+metas/changeB2=Dmeta
+metas/changeF=F
+
+In this case, changeB and changeB2 are still divergent and we’ve created a new
+change for our merge commit. However, this is just a temporary state. The next
+time we run the “evolve” command, it will discover the divergence but also
+discover the merge commit F that resolves it. Evolve will suggest converting F
+into an amend merge in order to resolve the divergence and will display the
+command for doing so.
+
+Rebase
+------
+In general the rebase command is treated as a modify command. When a change is
+rebased, the new commit replaces the original.
+
+Rebase --abort is special. Its intent is to restore git to the state it had
+prior to running rebase. It should move back any changes to point to the refs
+they had prior to running rebase and delete any new changes that were created as
+part of the rebase. To achieve this, rebase will save the state of all changes
+in refs/metas prior to running rebase and will restore the entire namespace
+after rebase completes (deleting any newly-created changes). Newly-created
+metacommits are left in place, but will have no effect until garbage collected
+since metacommits are only used if they are reachable from refs/metas.
+
+Change
+------
+The “change” command can be used to list, rename, reset or delete change. It has
+a number of subcommands.
+
+The "list" subcommand lists local changes. If given the -r argument, it lists
+remote changes.
+
+The "rename" subcommand renames a change, given its old and new name. If the old
+name is omitted and there is exactly one change pointing to the current HEAD,
+that change is renamed. If there are no changes pointing to the current HEAD,
+one is created with the given name.
+
+The "forget" subcommand deletes a change by deleting its ref from the metas/
+namespace. This is the normal way to delete extra aliases for a change if the
+change has more than one name. By default, this will refuse to delete the last
+alias for a change if there are any other changes that reference this change as
+a parent.
+
+The "update" subcommand adds a new state to a change. It uses the default
+algorithm for assigning change names. If the content commit is omitted, HEAD is
+used. If given the optional --force argument, it will overwrite any existing
+change of the same name. This latter form of "update" can be used to effectively
+reset changes.
+
+The "update" command can accept any number of --origin and --replace arguments.
+If any are present, the resulting change branch will point to a metacommit
+containing the given origin and replacement edges.
+
+The "abandon" command deletes a change using obsolescence markers. It marks the
+change as being obsolete and having been replaced by its parent. If given no
+arguments, it applies to the current commit. Running evolve will cause any
+abandoned changes to be removed from the branch. Any child changes will be
+reparented on top of the parent of the abandoned change. If the current change
+is abandoned, HEAD will move to point to its parent.
+
+The "restore" command restores a previously-abandoned change.
+
+The "prune" command deletes all obsolete changes and all changes that are
+present in the given branch. Note that such changes can be recovered from the
+reflog.
+
+Combined with the GC protection that is offered, this is intended to facilitate
+a workflow that relies on changes instead of branches. Users could choose to
+work with no local branches and use changes instead - both for mailing list and
+gerrit workflows.
+
+Log
+---
+When a commit is shown in git log that is part of a change, it is decorated with
+extra change information. If it is the head of a change, the name of the change
+is shown next to the list of branches. If it is obsolete, it is decorated with
+the text “obsolete, <n> commits behind <changename>”.
+
+Log gets a new --obslog argument indicating that the obsolescence graph should
+be followed instead of the commit graph. This also changes the default
+formatting options to make them more appropriate for viewing different
+iterations of the same commit.
+
+Pull
+----
+
+Pull gets an --evolve argument that will automatically attempt to run "evolve"
+on any affected branches after pulling.
+
+We also introduce an "evolve" enum value for the branch.<name>.rebase config
+value. When set, the evolve behavior will happen automatically for that branch
+after every pull even if the --evolve argument is not used.
+
+Next
+----
+
+The "next" command will reset HEAD to a non-obsolete commit that refers to this
+change as its parent. If there is more than one such change, the user will be
+prompted. If given the --evolve argument, the next commit will be evolved if
+necessary first.
+
+The "next" command can be thought of as the opposite of
+"git reset --hard HEAD^" in that it navigates to a child commit rather than a
+parent.
+
+Prev
+----
+
+The "prev" command will reset HEAD to the latest version of the parent change.
+If the parent change isn't obsolete, this is equivalent to
+"git reset --hard HEAD^". If the parent commit is obsolete, it resets to the
+latest replacement for the parent commit.
+
+Other options considered
+========================
+We considered several other options for storing the obsolescence graph. This
+section describes the other options and why they were rejected.
+
+Commit header
+-------------
+Add an “obsoletes” field to the commit header that points backwards from a
+commit to the previous commits it obsoletes.
+
+Pros:
+- Very simple
+- Easy to traverse from a commit to the previous commits it obsoletes.
+Cons:
+- Adds a cost to the storage format, even for commits where the change history
+  is uninteresting.
+- Unconditionally prevents the change history from being garbage collected.
+- Always causes the change history to be shared when pushing or pulling changes.
+
+Git notes
+---------
+Instead of storing obsolescence information in metacommits, the metacommit
+content could go in a new notes namespace - say refs/notes/metacommit. Each note
+would contain the list of obsolete and origin parents. An automerger could
+be supplied to make it easy to merge the metacommit notes from different remotes.
+
+Pros:
+- Easy to locate all commits obsoleted by a given commit (since there would only
+  be one metacommit for any given commit).
+Cons:
+- Wrong GC behavior (obsolete commits wouldn’t automatically be retained by GC)
+  unless we introduced a special case for these kinds of notes.
+- No way to selectively share or pull the metacommits for one specific change.
+  It would be all-or-nothing, which would be expensive. This could be addressed
+  by changes to the protocol, but this would be invasive.
+- Requires custom auto-merging behavior on fetch.
+
+Tags
+----
+Put the content of the metacommit in a message attached to tag on the
+replacement commit. This is very similar to the git notes approach and has the
+same pros and cons.
+
+Simple forward references
+-------------------------
+Record an edge from an obsolete commit to its replacement in this form:
+
+refs/obsoletes/<A>
+
+pointing to commit <B> as an indication that B is the replacement for the
+obsolete commit A.
+
+Pros:
+- Protects <B> from being garbage collected.
+- Fast lookup for the evolve operation, without additional search structures
+  (“what is the replacement for <A>?” is very fast).
+
+Cons:
+- Can’t represent divergence (which is a P0 requirement).
+- Creates lots of refs (which can be inefficient)
+- Doesn’t provide a way to fetch only refs for a specific change.
+- The obslog command requires a search of all refs.
+
+Complex forward references
+--------------------------
+Record an edge from an obsolete commit to its replacement in this form:
+
+refs/obsoletes/<change_id>/obs<A>_<B>
+
+Pointing to commit <B> as an indication that B is the replacement for obsolete
+commit A.
+
+Pros:
+- Permits sharing and fetching refs for only a specific change.
+- Supports divergence
+- Protects <B> from being garbage collected.
+
+Cons:
+- Creates lots of refs, which is inefficient.
+- Doesn’t provide a good lookup structure for lookups in either direction.
+
+Backward references
+-------------------
+Record an edge from a replacement commit to the obsolete one in this form:
+
+refs/obsolescences/<B>
+
+Cons:
+- Doesn’t provide a way to resolve divergence (which is a P0 requirement).
+- Doesn’t protect <B> from being garbage collected (which could be fixed by
+  combining this with a refs/metas namespace, as in the metacommit variant).
+
+Obsolescences file
+------------------
+Create a custom file (or files) in .git recording obsolescences.
+
+Pros:
+- Can store exactly the information we want with exactly the performance we want
+  for all operations. For example, there could be a disk-based hashtable
+  permitting constant time lookups in either direction.
+
+Cons:
+- Handling GC, pushing, and pulling would all require custom solutions. GC
+  issues could be addressed with a repository format extension.
+
+Squash points
+-------------
+We treat changes like topic branches, and use special squash points to mark
+places in the commit graph that separate changes.
+
+We create and update change branches in refs/metas at the same time we
+would have in the metacommit proposal. However, rather than pointing to a
+metacommit branch they point to normal commits and are treated as “squash
+points” - markers for sequences of commits intended to be squashed together on
+submission.
+
+Amends and rebases work differently than they do now. Rather than actually
+containing the desired state of a commit, they contain a delta from the previous
+version along with a squash point indicating that the preceding changes are
+intended to be squashed on submission. Specifically, amends would become new
+changes and rebases would become merge commits with the old commit and new
+parent as parents.
+
+When the changes are finally submitted, the squashes are executed, producing the
+final version of the commit.
+
+In addition to the squash points, git would maintain a set of “nosquash” tags
+for commits that were used as ancestors of a change that are not meant to be
+included in the squash.
+
+For example, if we have this commit graph:
+
+A(...)
+B(parent=A)
+C(parent=B)
+
+...and we amend B to produce D, we’d get:
+
+A(...)
+B(parent=A)
+C(parent=B)
+D(parent=B)
+
+...along with a new change branch indicating D should be squashed with its
+parents when submitted:
+
+metas/changeB = D
+metas/changeC = C
+
+We’d also create a nosquash tag for A indicating that A shouldn’t be included
+when changeB is squashed.
+
+If a user amends the change again, they’d get:
+
+A(...)
+B(parent=A)
+C(parent=B)
+D(parent=B)
+E(parent=D)
+
+metas/changeB = E
+metas/changeC = C
+
+Pros:
+- Good GC behavior.
+- Provides a natural way to share changes (they’re just normal branches).
+- Merge-base works automatically without special cases.
+- Rewriting the obslog would be easy using existing git commands.
+- No new data types needed.
+Cons:
+- No way to connect the squashed version of a change to the original, so no way
+  to automatically clean up old changes. This also means users lose all benefits
+  of the evolve command if they prematurely squash their commits. This may occur
+  if a user thinks a change is ready for submission, squashes it, and then later
+  discovers an additional change to make.
+- Histories would look very cluttered (users would see all previous edits to
+  their commit in the commit log, and all previous rebases would show up as
+  merges). Could be quite hard for users to tell what is going on. (Possible
+  fix: also implement a new smart log feature that displays the log as though
+  the squashes had occurred).
+- Need to change the current behavior of current commands (like amend and
+  rebase) in ways that will be unexpected to many users.
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH 02/10] sha1-array: implement oid_array_readonly_contains
  2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
  2022-09-23 18:55 ` [PATCH 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
@ 2022-09-23 18:55 ` Chris Poucet via GitGitGadget
  2022-09-26 13:08   ` Phillip Wood
  2022-09-23 18:55 ` [PATCH 03/10] ref-filter: add the metas namespace to ref-filter Chris Poucet via GitGitGadget
                   ` (10 subsequent siblings)
  12 siblings, 1 reply; 66+ messages in thread
From: Chris Poucet via GitGitGadget @ 2022-09-23 18:55 UTC (permalink / raw)
  To: git; +Cc: Christophe Poucet, Chris Poucet

From: Chris Poucet <poucet@google.com>

Implement a "readonly_contains" function for oid_array that won't
sort the array if it is unsorted. This can be used to test containment in
the rare situations where the array order matters.

The function has intentionally been given a name that is more cumbersome
than the "lookup" function, which is what most callers will will want
in most situations.

Signed-off-by: Chris Poucet <poucet@google.com>
---
 oid-array.c               | 12 ++++++++++++
 oid-array.h               |  7 +++++++
 t/helper/test-oid-array.c |  6 ++++++
 t/t0064-oid-array.sh      | 22 ++++++++++++++++++++++
 4 files changed, 47 insertions(+)

diff --git a/oid-array.c b/oid-array.c
index 73ba76e9e9a..1e12651d245 100644
--- a/oid-array.c
+++ b/oid-array.c
@@ -28,6 +28,18 @@ static const struct object_id *oid_access(size_t index, const void *table)
 	return &array[index];
 }
 
+int oid_array_readonly_contains(const struct oid_array *array,
+				const struct object_id* oid) {
+	int i;
+
+	if (array->sorted)
+		return oid_pos(oid, array->oid, array->nr, oid_access) >= 0;
+	for (i = 0; i < array->nr; i++)
+		if (oideq(&array->oid[i], oid))
+			return 1;
+	return 0;
+}
+
 int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
 {
 	oid_array_sort(array);
diff --git a/oid-array.h b/oid-array.h
index f60f9af6741..e056eb61fa2 100644
--- a/oid-array.h
+++ b/oid-array.h
@@ -58,6 +58,13 @@ struct oid_array {
 
 #define OID_ARRAY_INIT { 0 }
 
+/**
+ * Sees whether an array contains an object ID. Optimized for when the array is
+ * sorted but does not require the array to be sorted.
+ */
+int oid_array_readonly_contains(const struct oid_array *array,
+				const struct object_id* oid);
+
 /**
  * Add an item to the set. The object ID will be placed at the end of the array
  * (but note that some operations below may lose this ordering).
diff --git a/t/helper/test-oid-array.c b/t/helper/test-oid-array.c
index d1324d086a2..0dbfc91ca8d 100644
--- a/t/helper/test-oid-array.c
+++ b/t/helper/test-oid-array.c
@@ -28,10 +28,16 @@ int cmd__oid_array(int argc, const char **argv)
 			if (get_oid_hex(arg, &oid))
 				die("not a hexadecimal oid: %s", arg);
 			printf("%d\n", oid_array_lookup(&array, &oid));
+		} else if (skip_prefix(line.buf, "readonly_contains ", &arg)) {
+			if (get_oid_hex(arg, &oid))
+				die("not a hexadecimal oid: %s", arg);
+			printf("%d\n", oid_array_readonly_contains(&array, &oid));
 		} else if (!strcmp(line.buf, "clear"))
 			oid_array_clear(&array);
 		else if (!strcmp(line.buf, "for_each_unique"))
 			oid_array_for_each_unique(&array, print_oid, NULL);
+		else if (!strcmp(line.buf, "for_each"))
+			oid_array_for_each(&array, print_oid, NULL);
 		else
 			die("unknown command: %s", line.buf);
 	}
diff --git a/t/t0064-oid-array.sh b/t/t0064-oid-array.sh
index 88c89e8f48a..aa677af132d 100755
--- a/t/t0064-oid-array.sh
+++ b/t/t0064-oid-array.sh
@@ -35,6 +35,28 @@ test_expect_success 'ordered enumeration with duplicate suppression' '
 	test_cmp expect actual
 '
 
+test_expect_success 'readonly_contains finds existing' '
+	echo 1 >expect &&
+	echoid "" 88 44 aa 55 >>expect &&
+	{
+		echoid append 88 44 aa 55 &&
+		echoid readonly_contains 55 &&
+		echo for_each
+	} | test-tool oid-array >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'readonly_contains non-existing query' '
+	echo 0 >expect &&
+	echoid "" 88 44 aa 55 >>expect &&
+	{
+		echoid append 88 44 aa 55 &&
+		echoid readonly_contains 33 &&
+		echo for_each
+	} | test-tool oid-array >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'lookup' '
 	{
 		echoid append 88 44 aa 55 &&
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH 03/10] ref-filter: add the metas namespace to ref-filter
  2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
  2022-09-23 18:55 ` [PATCH 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
  2022-09-23 18:55 ` [PATCH 02/10] sha1-array: implement oid_array_readonly_contains Chris Poucet via GitGitGadget
@ 2022-09-23 18:55 ` Chris Poucet via GitGitGadget
  2022-09-26 13:13   ` Phillip Wood
  2022-09-23 18:55 ` [PATCH 04/10] evolve: add support for parsing metacommits Stefan Xenos via GitGitGadget
                   ` (9 subsequent siblings)
  12 siblings, 1 reply; 66+ messages in thread
From: Chris Poucet via GitGitGadget @ 2022-09-23 18:55 UTC (permalink / raw)
  To: git; +Cc: Christophe Poucet, Chris Poucet

From: Chris Poucet <poucet@google.com>

The metas namespace will contain refs for changes in progress. Add
support for searching this namespace.

Signed-off-by: Chris Poucet <poucet@google.com>
---
 ref-filter.c | 8 ++++++--
 ref-filter.h | 4 +++-
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/ref-filter.c b/ref-filter.c
index fd1cb14b0f1..6a1789c623f 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -2200,7 +2200,8 @@ static int ref_kind_from_refname(const char *refname)
 	} ref_kind[] = {
 		{ "refs/heads/" , FILTER_REFS_BRANCHES },
 		{ "refs/remotes/" , FILTER_REFS_REMOTES },
-		{ "refs/tags/", FILTER_REFS_TAGS}
+		{ "refs/tags/", FILTER_REFS_TAGS},
+		{ "refs/metas/", FILTER_REFS_CHANGES }
 	};
 
 	if (!strcmp(refname, "HEAD"))
@@ -2218,7 +2219,8 @@ static int filter_ref_kind(struct ref_filter *filter, const char *refname)
 {
 	if (filter->kind == FILTER_REFS_BRANCHES ||
 	    filter->kind == FILTER_REFS_REMOTES ||
-	    filter->kind == FILTER_REFS_TAGS)
+	    filter->kind == FILTER_REFS_TAGS ||
+	    filter->kind == FILTER_REFS_CHANGES)
 		return filter->kind;
 	return ref_kind_from_refname(refname);
 }
@@ -2435,6 +2437,8 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
 			ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata);
 		else if (filter->kind == FILTER_REFS_TAGS)
 			ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata);
+		else if (filter->kind == FILTER_REFS_CHANGES)
+			ret = for_each_fullref_in("refs/metas/", ref_filter_handler, &ref_cbdata);
 		else if (filter->kind & FILTER_REFS_ALL)
 			ret = for_each_fullref_in_pattern(filter, ref_filter_handler, &ref_cbdata);
 		if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
diff --git a/ref-filter.h b/ref-filter.h
index aa0eea4ecf5..064fbef8e50 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -17,8 +17,10 @@
 #define FILTER_REFS_BRANCHES       0x0004
 #define FILTER_REFS_REMOTES        0x0008
 #define FILTER_REFS_OTHERS         0x0010
+#define FILTER_REFS_CHANGES        0x0040
 #define FILTER_REFS_ALL            (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
-				    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
+				    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS | \
+				    FILTER_REFS_CHANGES)
 #define FILTER_REFS_DETACHED_HEAD  0x0020
 #define FILTER_REFS_KIND_MASK      (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH 04/10] evolve: add support for parsing metacommits
  2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
                   ` (2 preceding siblings ...)
  2022-09-23 18:55 ` [PATCH 03/10] ref-filter: add the metas namespace to ref-filter Chris Poucet via GitGitGadget
@ 2022-09-23 18:55 ` Stefan Xenos via GitGitGadget
  2022-09-26 13:27   ` Phillip Wood
  2022-09-23 18:55 ` [PATCH 05/10] evolve: add the change-table structure Stefan Xenos via GitGitGadget
                   ` (8 subsequent siblings)
  12 siblings, 1 reply; 66+ messages in thread
From: Stefan Xenos via GitGitGadget @ 2022-09-23 18:55 UTC (permalink / raw)
  To: git; +Cc: Christophe Poucet, Stefan Xenos

From: Stefan Xenos <sxenos@google.com>

This patch adds the get_metacommit_content method, which can classify
commits as either metacommits or normal commits, determine whether they
are abandoned, and extract the content commit's object id from the
metacommit.

Signed-off-by: Stefan Xenos <sxenos@google.com>
Signed-off-by: Chris Poucet <poucet@google.com>
---
 Makefile            |   1 +
 metacommit-parser.c | 110 ++++++++++++++++++++++++++++++++++++++++++++
 metacommit-parser.h |  19 ++++++++
 3 files changed, 130 insertions(+)
 create mode 100644 metacommit-parser.c
 create mode 100644 metacommit-parser.h

diff --git a/Makefile b/Makefile
index cac3452edb9..b2bcc00c289 100644
--- a/Makefile
+++ b/Makefile
@@ -999,6 +999,7 @@ LIB_OBJS += merge-ort.o
 LIB_OBJS += merge-ort-wrappers.o
 LIB_OBJS += merge-recursive.o
 LIB_OBJS += merge.o
+LIB_OBJS += metacommit-parser.o
 LIB_OBJS += midx.o
 LIB_OBJS += name-hash.o
 LIB_OBJS += negotiator/default.o
diff --git a/metacommit-parser.c b/metacommit-parser.c
new file mode 100644
index 00000000000..70c1428bfc6
--- /dev/null
+++ b/metacommit-parser.c
@@ -0,0 +1,110 @@
+#include "cache.h"
+#include "metacommit-parser.h"
+#include "commit.h"
+
+/*
+ * Search the commit buffer for a line starting with the given key. Unlike
+ * find_commit_header, this also searches the commit message body.
+ */
+static const char *find_key(const char *msg, const char *key, size_t *out_len)
+{
+	int key_len = strlen(key);
+	const char *line = msg;
+
+	while (line) {
+		const char *eol = strchrnul(line, '\n');
+
+		if (eol - line > key_len && !memcmp(line, key, key_len) &&
+		    line[key_len] == ' ') {
+			*out_len = eol - line - key_len - 1;
+			return line + key_len + 1;
+		}
+		line = *eol ? eol + 1 : NULL;
+	}
+	return NULL;
+}
+
+static struct commit *get_commit_by_index(struct commit_list *to_search, int index)
+{
+	while (to_search && index) {
+		to_search = to_search->next;
+		index--;
+	}
+
+	if (!to_search)
+		return NULL;
+
+	return to_search->item;
+}
+
+/*
+ * Writes the index of the content parent to "result". Returns the metacommit
+ * type. See the METACOMMIT_TYPE_* constants.
+ */
+static int index_of_content_commit(const char *buffer, int *result)
+{
+	int index = 0;
+	int ret = METACOMMIT_TYPE_NONE;
+	size_t parent_types_size;
+	const char *parent_types = find_key(buffer, "parent-type",
+		&parent_types_size);
+	const char *end;
+	const char *enum_start = parent_types;
+	int enum_length = 0;
+
+	if (!parent_types)
+		return METACOMMIT_TYPE_NONE;
+
+	end = &parent_types[parent_types_size];
+
+	while (1) {
+		char next = *parent_types;
+		if (next == ' ' || parent_types >= end) {
+			if (enum_length == 1) {
+				char first_char_in_enum = *enum_start;
+				if (first_char_in_enum == 'c') {
+					ret = METACOMMIT_TYPE_NORMAL;
+					break;
+				}
+				if (first_char_in_enum == 'a') {
+					ret = METACOMMIT_TYPE_ABANDONED;
+					break;
+				}
+			}
+			if (parent_types >= end)
+				return METACOMMIT_TYPE_NONE;
+			enum_start = parent_types + 1;
+			enum_length = 0;
+			index++;
+		} else {
+			enum_length++;
+		}
+		parent_types++;
+	}
+
+	*result = index;
+	return ret;
+}
+
+/*
+ * Writes the content parent's object id to "content".
+ * Returns the metacommit type. See the METACOMMIT_TYPE_* constants.
+ */
+int get_metacommit_content(struct commit *commit, struct object_id *content)
+{
+	const char *buffer = get_commit_buffer(commit, NULL);
+	int index = 0;
+	int ret = index_of_content_commit(buffer, &index);
+	struct commit *content_parent;
+
+	if (ret == METACOMMIT_TYPE_NONE)
+		return ret;
+
+	content_parent = get_commit_by_index(commit->parents, index);
+
+	if (!content_parent)
+		return METACOMMIT_TYPE_NONE;
+
+	oidcpy(content, &(content_parent->object.oid));
+	return ret;
+}
diff --git a/metacommit-parser.h b/metacommit-parser.h
new file mode 100644
index 00000000000..1c74bd6d699
--- /dev/null
+++ b/metacommit-parser.h
@@ -0,0 +1,19 @@
+#ifndef METACOMMIT_PARSER_H
+#define METACOMMIT_PARSER_H
+
+#include "commit.h"
+#include "hash.h"
+
+/* Indicates a normal commit (non-metacommit) */
+#define METACOMMIT_TYPE_NONE 0
+/* Indicates a metacommit with normal content (non-abandoned) */
+#define METACOMMIT_TYPE_NORMAL 1
+/* Indicates a metacommit with abandoned content */
+#define METACOMMIT_TYPE_ABANDONED 2
+
+struct commit;
+
+extern int get_metacommit_content(
+	struct commit *commit, struct object_id *content);
+
+#endif
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH 05/10] evolve: add the change-table structure
  2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
                   ` (3 preceding siblings ...)
  2022-09-23 18:55 ` [PATCH 04/10] evolve: add support for parsing metacommits Stefan Xenos via GitGitGadget
@ 2022-09-23 18:55 ` Stefan Xenos via GitGitGadget
  2022-09-27 13:27   ` Phillip Wood
  2022-09-23 18:55 ` [PATCH 06/10] evolve: add support for writing metacommits Stefan Xenos via GitGitGadget
                   ` (7 subsequent siblings)
  12 siblings, 1 reply; 66+ messages in thread
From: Stefan Xenos via GitGitGadget @ 2022-09-23 18:55 UTC (permalink / raw)
  To: git; +Cc: Christophe Poucet, Stefan Xenos

From: Stefan Xenos <sxenos@google.com>

A change table stores a list of changes, and supports efficient lookup
from a commit hash to the list of changes that reference that commit
directly.

It can be used to look up content commits or metacommits at the head
of a change, but does not support lookup of commits referenced as part
of the commit history.

Signed-off-by: Stefan Xenos <sxenos@google.com>
Signed-off-by: Chris Poucet <poucet@google.com>
---
 Makefile       |   1 +
 change-table.c | 179 +++++++++++++++++++++++++++++++++++++++++++++++++
 change-table.h | 132 ++++++++++++++++++++++++++++++++++++
 3 files changed, 312 insertions(+)
 create mode 100644 change-table.c
 create mode 100644 change-table.h

diff --git a/Makefile b/Makefile
index b2bcc00c289..2b847e7e7de 100644
--- a/Makefile
+++ b/Makefile
@@ -913,6 +913,7 @@ LIB_OBJS += bulk-checkin.o
 LIB_OBJS += bundle-uri.o
 LIB_OBJS += bundle.o
 LIB_OBJS += cache-tree.o
+LIB_OBJS += change-table.o
 LIB_OBJS += cbtree.o
 LIB_OBJS += chdir-notify.o
 LIB_OBJS += checkout.o
diff --git a/change-table.c b/change-table.c
new file mode 100644
index 00000000000..c61ba29f1ed
--- /dev/null
+++ b/change-table.c
@@ -0,0 +1,179 @@
+#include "cache.h"
+#include "change-table.h"
+#include "commit.h"
+#include "ref-filter.h"
+#include "metacommit-parser.h"
+
+void change_table_init(struct change_table *to_initialize)
+{
+	memset(to_initialize, 0, sizeof(*to_initialize));
+	mem_pool_init(&to_initialize->memory_pool, 0);
+	to_initialize->memory_pool.block_alloc = 4*1024 - sizeof(struct mp_block);
+	oidmap_init(&to_initialize->oid_to_metadata_index, 0);
+	string_list_init_dup(&to_initialize->refname_to_change_head);
+}
+
+static void change_list_clear(struct change_list *to_clear) {
+	string_list_clear(&to_clear->additional_refnames, 0);
+}
+
+static void commit_change_list_entry_clear(
+	struct commit_change_list_entry *to_clear) {
+	change_list_clear(&to_clear->changes);
+}
+
+void change_table_clear(struct change_table *to_clear)
+{
+	struct oidmap_iter iter;
+	struct commit_change_list_entry *next;
+	for (next = oidmap_iter_first(&to_clear->oid_to_metadata_index, &iter);
+		next;
+		next = oidmap_iter_next(&iter)) {
+
+		commit_change_list_entry_clear(next);
+	}
+
+	oidmap_free(&to_clear->oid_to_metadata_index, 0);
+	string_list_clear(&to_clear->refname_to_change_head, 0);
+	mem_pool_discard(&to_clear->memory_pool, 0);
+}
+
+static void add_head_to_commit(struct change_table *to_modify,
+	const struct object_id *to_add, const char *refname)
+{
+	struct commit_change_list_entry *entry;
+
+	/**
+	 * Note: the indices in the map are 1-based. 0 is used to indicate a missing
+	 * element.
+	 */
+	entry = oidmap_get(&to_modify->oid_to_metadata_index, to_add);
+	if (!entry) {
+		entry = mem_pool_calloc(&to_modify->memory_pool, 1,
+			sizeof(*entry));
+		oidcpy(&entry->entry.oid, to_add);
+		oidmap_put(&to_modify->oid_to_metadata_index, entry);
+		string_list_init_nodup(&entry->changes.additional_refnames);
+	}
+
+	if (!entry->changes.first_refname)
+		entry->changes.first_refname = refname;
+	else
+		string_list_insert(&entry->changes.additional_refnames, refname);
+}
+
+void change_table_add(struct change_table *to_modify, const char *refname,
+	struct commit *to_add)
+{
+	struct change_head *new_head;
+	struct string_list_item *new_item;
+	int metacommit_type;
+
+	new_head = mem_pool_calloc(&to_modify->memory_pool, 1,
+		sizeof(*new_head));
+
+	oidcpy(&new_head->head, &to_add->object.oid);
+
+	metacommit_type = get_metacommit_content(to_add, &new_head->content);
+	if (metacommit_type == METACOMMIT_TYPE_NONE)
+		oidcpy(&new_head->content, &to_add->object.oid);
+	new_head->abandoned = (metacommit_type == METACOMMIT_TYPE_ABANDONED);
+	new_head->remote = starts_with(refname, "refs/remote/");
+	new_head->hidden = starts_with(refname, "refs/hiddenmetas/");
+
+	new_item = string_list_insert(&to_modify->refname_to_change_head, refname);
+	new_item->util = new_head;
+	/* Use pointers to the copy of the string we're retaining locally */
+	refname = new_item->string;
+
+	if (!oideq(&new_head->content, &new_head->head))
+		add_head_to_commit(to_modify, &new_head->content, refname);
+	add_head_to_commit(to_modify, &new_head->head, refname);
+}
+
+void change_table_add_all_visible(struct change_table *to_modify,
+	struct repository* repo)
+{
+	struct ref_filter filter;
+	const char *name_patterns[] = {NULL};
+	memset(&filter, 0, sizeof(filter));
+	filter.kind = FILTER_REFS_CHANGES;
+	filter.name_patterns = name_patterns;
+
+	change_table_add_matching_filter(to_modify, repo, &filter);
+}
+
+void change_table_add_matching_filter(struct change_table *to_modify,
+	struct repository* repo, struct ref_filter *filter)
+{
+	struct ref_array matching_refs;
+	int i;
+
+	memset(&matching_refs, 0, sizeof(matching_refs));
+	filter_refs(&matching_refs, filter, filter->kind);
+
+	/**
+	 * Determine the object id for the latest content commit for each change.
+	 * Fetch the commit at the head of each change ref. If it's a normal commit,
+	 * that's the commit we want. If it's a metacommit, locate its content parent
+	 * and use that.
+	 */
+
+	for (i = 0; i < matching_refs.nr; i++) {
+		struct ref_array_item *item = matching_refs.items[i];
+		struct commit *commit = item->commit;
+
+		commit = lookup_commit_reference_gently(repo, &item->objectname, 1);
+
+		if (commit)
+			change_table_add(to_modify, item->refname, commit);
+	}
+
+	ref_array_clear(&matching_refs);
+}
+
+static int return_true_callback(const char *refname, void *cb_data)
+{
+	return 1;
+}
+
+int change_table_has_change_referencing(struct change_table *changes,
+	const struct object_id *referenced_commit_id)
+{
+	return for_each_change_referencing(changes, referenced_commit_id,
+		return_true_callback, NULL);
+}
+
+int for_each_change_referencing(struct change_table *table,
+	const struct object_id *referenced_commit_id, each_change_fn fn, void *cb_data)
+{
+	const struct change_list *changes;
+	int i;
+	int retvalue;
+	struct commit_change_list_entry *entry;
+
+	entry = oidmap_get(&table->oid_to_metadata_index,
+		referenced_commit_id);
+	/* If this commit isn't referenced by any changes, it won't be in the map */
+	if (!entry)
+		return 0;
+	changes = &entry->changes;
+	if (!changes->first_refname)
+		return 0;
+	retvalue = fn(changes->first_refname, cb_data);
+	for (i = 0; retvalue == 0 && i < changes->additional_refnames.nr; i++)
+		retvalue = fn(changes->additional_refnames.items[i].string, cb_data);
+	return retvalue;
+}
+
+struct change_head* get_change_head(struct change_table *heads,
+	const char* refname)
+{
+	struct string_list_item *item = string_list_lookup(
+		&heads->refname_to_change_head, refname);
+
+	if (!item)
+		return NULL;
+
+	return (struct change_head *)item->util;
+}
diff --git a/change-table.h b/change-table.h
new file mode 100644
index 00000000000..166b5ed8073
--- /dev/null
+++ b/change-table.h
@@ -0,0 +1,132 @@
+#ifndef CHANGE_TABLE_H
+#define CHANGE_TABLE_H
+
+#include "oidmap.h"
+
+struct commit;
+struct ref_filter;
+
+/**
+ * This struct holds a list of change refs. The first element is stored inline,
+ * to optimize for small lists.
+ */
+struct change_list {
+	/**
+	 * Ref name for the first change in the list, or null if none.
+	 *
+	 * This field is private. Use for_each_change_in to read.
+	 */
+	const char* first_refname;
+	/**
+	 * List of additional change refs. Note that this is empty if the list
+	 * contains 0 or 1 elements.
+	 *
+	 * This field is private. Use for_each_change_in to read.
+	 */
+	struct string_list additional_refnames;
+};
+
+/**
+ * Holds information about the head of a single change.
+ */
+struct change_head {
+	/**
+	 * The location pointed to by the head of the change. May be a commit or a
+	 * metacommit.
+	 */
+	struct object_id head;
+	/**
+	 * The content commit for the latest commit in the change. Always points to a
+	 * real commit, never a metacommit.
+	 */
+	struct object_id content;
+	/**
+	 * Abandoned: indicates that the content commit should be removed from the
+	 * history.
+	 *
+	 * Hidden: indicates that the change is an inactive change from the
+	 * hiddenmetas namespace. Such changes will be hidden from the user by
+	 * default.
+	 *
+	 * Deleted: indicates that the change has been removed from the repository.
+	 * That is the ref was deleted since the time this struct was created. Such
+	 * entries should be ignored.
+	 */
+	unsigned int abandoned:1,
+		hidden:1,
+		remote:1,
+		deleted:1;
+};
+
+/**
+ * Holds the list of change refs whose content points to a particular content
+ * commit.
+ */
+struct commit_change_list_entry {
+	struct oidmap_entry entry;
+	struct change_list changes;
+};
+
+/**
+ * Holds information about the heads of each change, and permits effecient
+ * lookup from a commit to the changes that reference it directly.
+ *
+ * All fields should be considered private. Use the change_table functions
+ * to interact with this struct.
+ */
+struct change_table {
+	/**
+	 * Memory pool for the objects allocated by the change table.
+	 */
+	struct mem_pool memory_pool;
+	/* Map object_id to commit_change_list_entry structs. */
+	struct oidmap oid_to_metadata_index;
+	/**
+	 * List of ref names. The util value points to a change_head structure
+	 * allocated from memory_pool.
+	 */
+	struct string_list refname_to_change_head;
+};
+
+extern void change_table_init(struct change_table *to_initialize);
+extern void change_table_clear(struct change_table *to_clear);
+
+/* Adds the given change head to the change_table struct */
+extern void change_table_add(struct change_table *to_modify,
+	const char *refname, struct commit *target);
+
+/**
+ * Adds the non-hidden local changes to the given change_table struct.
+ */
+extern void change_table_add_all_visible(struct change_table *to_modify,
+	struct repository *repo);
+
+/*
+ * Adds all changes matching the given ref filter to the given change_table
+ * struct.
+ */
+extern void change_table_add_matching_filter(struct change_table *to_modify,
+	struct repository* repo, struct ref_filter *filter);
+
+typedef int each_change_fn(const char *refname, void *cb_data);
+
+extern int change_table_has_change_referencing(struct change_table *changes,
+	const struct object_id *referenced_commit_id);
+
+/**
+ * Iterates over all changes that reference the given commit. For metacommits,
+ * this is the list of changes that point directly to that metacommit.
+ * For normal commits, this is the list of changes that have this commit as
+ * their latest content.
+ */
+extern int for_each_change_referencing(struct change_table *heads,
+	const struct object_id *referenced_commit_id, each_change_fn fn, void *cb_data);
+
+/**
+ * Returns the change head for the given refname. Returns NULL if no such change
+ * exists.
+ */
+extern struct change_head* get_change_head(struct change_table *heads,
+	const char* refname);
+
+#endif
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH 06/10] evolve: add support for writing metacommits
  2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
                   ` (4 preceding siblings ...)
  2022-09-23 18:55 ` [PATCH 05/10] evolve: add the change-table structure Stefan Xenos via GitGitGadget
@ 2022-09-23 18:55 ` Stefan Xenos via GitGitGadget
  2022-09-28 14:27   ` Phillip Wood
  2022-09-23 18:55 ` [PATCH 07/10] evolve: implement the git change command Stefan Xenos via GitGitGadget
                   ` (6 subsequent siblings)
  12 siblings, 1 reply; 66+ messages in thread
From: Stefan Xenos via GitGitGadget @ 2022-09-23 18:55 UTC (permalink / raw)
  To: git; +Cc: Christophe Poucet, Stefan Xenos

From: Stefan Xenos <sxenos@google.com>

metacommit.c supports the creation of metacommits and
adds the API needed to create and update changes.

Create the "modify_change" function that can be called from modification
commands like "rebase" and "git amend" to record obsolescences in the
change graph.

Create the "record_metacommit" function for recording more complicated
commit relationships in the commit graph.

Create the "write_metacommit" function for low-level creation of
metacommits.

Signed-off-by: Stefan Xenos <sxenos@google.com>
Signed-off-by: Chris Poucet <poucet@google.com>
---
 Makefile     |   1 +
 metacommit.c | 404 +++++++++++++++++++++++++++++++++++++++++++++++++++
 metacommit.h |  58 ++++++++
 3 files changed, 463 insertions(+)
 create mode 100644 metacommit.c
 create mode 100644 metacommit.h

diff --git a/Makefile b/Makefile
index 2b847e7e7de..68082ef94c7 100644
--- a/Makefile
+++ b/Makefile
@@ -1000,6 +1000,7 @@ LIB_OBJS += merge-ort.o
 LIB_OBJS += merge-ort-wrappers.o
 LIB_OBJS += merge-recursive.o
 LIB_OBJS += merge.o
+LIB_OBJS += metacommit.o
 LIB_OBJS += metacommit-parser.o
 LIB_OBJS += midx.o
 LIB_OBJS += name-hash.o
diff --git a/metacommit.c b/metacommit.c
new file mode 100644
index 00000000000..d2b859a4d3b
--- /dev/null
+++ b/metacommit.c
@@ -0,0 +1,404 @@
+#include "cache.h"
+#include "metacommit.h"
+#include "commit.h"
+#include "change-table.h"
+#include "refs.h"
+
+void init_metacommit_data(struct metacommit_data *state)
+{
+	memset(state, 0, sizeof(*state));
+}
+
+void clear_metacommit_data(struct metacommit_data *state)
+{
+	oid_array_clear(&state->replace);
+	oid_array_clear(&state->origin);
+}
+
+static void compute_default_change_name(struct commit *initial_commit,
+	struct strbuf* result)
+{
+	struct strbuf default_name;
+	const char *buffer;
+	const char *subject;
+	const char *eol;
+	int len;
+	strbuf_init(&default_name, 0);
+	buffer = get_commit_buffer(initial_commit, NULL);
+	find_commit_subject(buffer, &subject);
+	eol = strchrnul(subject, '\n');
+	for (len = 0;subject < eol && len < 10; ++subject, ++len) {
+		char next = *subject;
+		if (isspace(next))
+			continue;
+
+		strbuf_addch(&default_name, next);
+	}
+	sanitize_refname_component(default_name.buf, result);
+}
+
+/**
+ * Computes a change name for a change rooted at the given initial commit. Good
+ * change names should be memorable, unique, and easy to type. They are not
+ * required to match the commit comment.
+ */
+static void compute_change_name(struct commit *initial_commit, struct strbuf* result)
+{
+	struct strbuf default_name;
+	struct object_id unused;
+
+	strbuf_init(&default_name, 0);
+	if (initial_commit)
+		compute_default_change_name(initial_commit, &default_name);
+	else
+		strbuf_addstr(&default_name, "change");
+	strbuf_addstr(result, "refs/metas/");
+	strbuf_addbuf(result, &default_name);
+
+	/* If there is already a change of this name, append a suffix */
+	if (!read_ref(result->buf, &unused)) {
+		int suffix = 2;
+		int original_length = result->len;
+
+		while (1) {
+			strbuf_addf(result, "%d", suffix);
+			if (read_ref(result->buf, &unused))
+				break;
+			strbuf_remove(result, original_length, result->len - original_length);
+			++suffix;
+		}
+	}
+
+	strbuf_release(&default_name);
+}
+
+struct resolve_metacommit_callback_data
+{
+	struct change_table* active_changes;
+	struct string_list *changes;
+	struct oid_array *heads;
+};
+
+static int resolve_metacommit_callback(const char *refname, void *cb_data)
+{
+	struct resolve_metacommit_callback_data *data = (struct resolve_metacommit_callback_data *)cb_data;
+	struct change_head *chhead;
+
+	chhead = get_change_head(data->active_changes, refname);
+
+	if (data->changes)
+		string_list_append(data->changes, refname)->util = &(chhead->head);
+	if (data->heads)
+		oid_array_append(data->heads, &(chhead->head));
+
+	return 0;
+}
+
+/**
+ * Produces the final form of a metacommit based on the current change refs.
+ */
+static void resolve_metacommit(
+	struct repository* repo,
+	struct change_table* active_changes,
+	const struct metacommit_data *to_resolve,
+	struct metacommit_data *resolved_output,
+	struct string_list *to_advance,
+	int allow_append)
+{
+	int i;
+	int len = to_resolve->replace.nr;
+	struct resolve_metacommit_callback_data cbdata;
+	int old_change_list_length = to_advance->nr;
+	struct commit* content;
+
+	oidcpy(&resolved_output->content, &to_resolve->content);
+
+	/* First look for changes that point to any of the replacement edges in the
+	 * metacommit. These will be the changes that get advanced by this
+	 * metacommit. */
+	resolved_output->abandoned = to_resolve->abandoned;
+	cbdata.active_changes = active_changes;
+	cbdata.changes = to_advance;
+	cbdata.heads = &(resolved_output->replace);
+
+	if (allow_append) {
+		for (i = 0; i < len; i++) {
+			int old_number = resolved_output->replace.nr;
+			for_each_change_referencing(active_changes, &(to_resolve->replace.oid[i]),
+				resolve_metacommit_callback, &cbdata);
+			/* If no changes were found, use the unresolved value. */
+			if (old_number == resolved_output->replace.nr)
+				oid_array_append(&(resolved_output->replace), &(to_resolve->replace.oid[i]));
+		}
+	}
+
+	cbdata.changes = NULL;
+	cbdata.heads = &(resolved_output->origin);
+
+	len = to_resolve->origin.nr;
+	for (i = 0; i < len; i++) {
+		int old_number = resolved_output->origin.nr;
+		for_each_change_referencing(active_changes, &(to_resolve->origin.oid[i]),
+			resolve_metacommit_callback, &cbdata);
+		if (old_number == resolved_output->origin.nr)
+			oid_array_append(&(resolved_output->origin), &(to_resolve->origin.oid[i]));
+	}
+
+	/* If no changes were advanced by this metacommit, we'll need to create a new
+	 * one. */
+	if (to_advance->nr == old_change_list_length) {
+		struct strbuf change_name;
+
+		strbuf_init(&change_name, 80);
+		content = lookup_commit_reference_gently(repo, &(to_resolve->content), 1);
+
+		compute_change_name(content, &change_name);
+		string_list_append(to_advance, change_name.buf);
+		strbuf_release(&change_name);
+	}
+}
+
+static void lookup_commits(
+	struct repository *repo,
+	struct oid_array *to_lookup,
+	struct commit_list **result)
+{
+	int i = to_lookup->nr;
+
+	while (--i >= 0) {
+		struct object_id *next = &(to_lookup->oid[i]);
+		struct commit *commit = lookup_commit_reference_gently(repo, next, 1);
+		commit_list_insert(commit, result);
+	}
+}
+
+#define PARENT_TYPE_PREFIX "parent-type "
+
+/**
+ * Creates a new metacommit object with the given content. Writes the object
+ * id of the newly-created commit to result.
+ */
+int write_metacommit(struct repository *repo, struct metacommit_data *state,
+	struct object_id *result)
+{
+	struct commit_list *parents = NULL;
+	struct strbuf comment;
+	int i;
+	struct commit *content;
+
+	strbuf_init(&comment, strlen(PARENT_TYPE_PREFIX)
+		+ 1 + 2 * (state->origin.nr + state->replace.nr));
+	lookup_commits(repo, &state->origin, &parents);
+	lookup_commits(repo, &state->replace, &parents);
+	content = lookup_commit_reference_gently(repo, &state->content, 1);
+	if (!content) {
+		strbuf_release(&comment);
+		free_commit_list(parents);
+		return -1;
+	}
+	commit_list_insert(content, &parents);
+
+	strbuf_addstr(&comment, PARENT_TYPE_PREFIX);
+	strbuf_addstr(&comment, state->abandoned ? "a" : "c");
+	for (i = 0; i < state->replace.nr; i++)
+		strbuf_addstr(&comment, " r");
+
+	for (i = 0; i < state->origin.nr; i++)
+		strbuf_addstr(&comment, " o");
+
+	/* The parents list will be freed by this call. */
+	commit_tree(comment.buf, comment.len, repo->hash_algo->empty_tree, parents,
+		result, NULL, NULL);
+
+	strbuf_release(&comment);
+	return 0;
+}
+
+/**
+ * Returns true iff the given metacommit is abandoned, has one or more origin
+ * parents, or has one or more replacement parents.
+ */
+static int is_nontrivial_metacommit(struct metacommit_data *state)
+{
+	return state->replace.nr || state->origin.nr || state->abandoned;
+}
+
+/*
+ * Records the relationships described by the given metacommit in the
+ * repository.
+ *
+ * If override_change is NULL (the default), an attempt will be made
+ * to append to existing changes wherever possible instead of creating new ones.
+ * If override_change is non-null, only the given change ref will be updated.
+ *
+ * options is a bitwise combination of the UPDATE_OPTION_* flags.
+ */
+int record_metacommit(
+	struct repository *repo,
+	const struct metacommit_data *metacommit, const char *override_change,
+	int options, struct strbuf *err)
+{
+		struct change_table chtable;
+		struct string_list changes;
+		int result;
+
+		change_table_init(&chtable);
+		change_table_add_all_visible(&chtable, repo);
+		string_list_init_dup(&changes);
+
+		result = record_metacommit_withresult(repo, &chtable, metacommit,
+			override_change, options, err, &changes);
+
+		string_list_clear(&changes, 0);
+		change_table_clear(&chtable);
+		return result;
+}
+
+/*
+ * Records the relationships described by the given metacommit in the
+ * repository.
+ *
+ * If override_change is NULL (the default), an attempt will be made
+ * to append to existing changes wherever possible instead of creating new ones.
+ * If override_change is non-null, only the given change ref will be updated.
+ *
+ * The changes list is filled in with the list of change refs that were updated,
+ * with the util pointers pointing to the old object IDS for those changes.
+ * The object ID pointers all point to objects owned by the change_table and
+ * will go out of scope when the change_table is destroyed.
+ *
+ * options is a bitwise combination of the UPDATE_OPTION_* flags.
+ */
+int record_metacommit_withresult(
+	struct repository *repo,
+	struct change_table *chtable,
+	const struct metacommit_data *metacommit,
+	const char *override_change,
+	int options, struct strbuf *err,
+	struct string_list *changes)
+{
+	static const char *msg = "updating change";
+	struct metacommit_data resolved_metacommit;
+	struct object_id commit_target;
+	struct ref_transaction *transaction = NULL;
+	struct change_head *overridden_head;
+	const struct object_id *old_head;
+
+	int i;
+	int ret = 0;
+	int force = (options & UPDATE_OPTION_FORCE);
+
+	init_metacommit_data(&resolved_metacommit);
+
+	resolve_metacommit(repo, chtable, metacommit, &resolved_metacommit, changes,
+		(options & UPDATE_OPTION_NOAPPEND) == 0);
+
+	if (override_change) {
+		string_list_clear(changes, 0);
+		overridden_head = get_change_head(chtable, override_change);
+		if (!overridden_head) {
+			/* This is an existing change */
+			old_head = &overridden_head->head;
+			if (!force) {
+				if (!oid_array_readonly_contains(&(resolved_metacommit.replace),
+					&overridden_head->head)) {
+					/* Attempted non-fast-forward change */
+					strbuf_addf(err, _("non-fast-forward update to '%s'"),
+						override_change);
+					ret = -1;
+					goto cleanup;
+				}
+			}
+		} else
+			/* ...then this is a newly-created change */
+			old_head = null_oid();
+
+		/* The expected "current" head of the change is stored in the util
+		 * pointer. */
+		string_list_append(changes, override_change)->util = (void*)old_head;
+	}
+
+	if (is_nontrivial_metacommit(&resolved_metacommit)) {
+		/* If there are any origin or replacement parents, create a new metacommit
+		 * object. */
+		if (write_metacommit(repo, &resolved_metacommit, &commit_target) < 0) {
+			ret = -1;
+			goto cleanup;
+		}
+	} else
+		/**
+		 * If the metacommit would only contain a content commit, point to the
+		 * commit itself rather than creating a trivial metacommit.
+		 */
+		oidcpy(&commit_target, &(resolved_metacommit.content));
+
+	/**
+	 * If a change already exists with this target and we're not forcing an
+	 * update to some specific override_change && change, there's nothing to do.
+	 */
+	if (!override_change
+		&& change_table_has_change_referencing(chtable, &commit_target))
+		/* Not an error */
+		goto cleanup;
+
+	transaction = ref_transaction_begin(err);
+
+	/* Update the refs for each affected change */
+	if (!transaction)
+		ret = -1;
+	else {
+		for (i = 0; i < changes->nr; i++) {
+			struct string_list_item *it = &changes->items[i];
+
+			/**
+			 * The expected current head of the change is stored in the util pointer.
+			 * It is null if the change should be newly-created.
+			 */
+			if (it->util) {
+				if (ref_transaction_update(transaction, it->string, &commit_target,
+					force ? NULL : it->util, 0, msg, err))
+
+					ret = -1;
+			} else {
+				if (ref_transaction_create(transaction, it->string,
+					&commit_target, 0, msg, err))
+
+					ret = -1;
+			}
+		}
+
+		if (!ret)
+			if (ref_transaction_commit(transaction, err))
+				ret = -1;
+	}
+
+cleanup:
+	ref_transaction_free(transaction);
+	clear_metacommit_data(&resolved_metacommit);
+
+	return ret;
+}
+
+/**
+ * Should be invoked after a command that has "modify" semantics - commands that
+ * create a new commit based on an old commit and treat the new one as a
+ * replacement for the old one. This method records the replacement in the
+ * change graph, such that a future evolve operation will rebase children of
+ * the old commit onto the new commit.
+ */
+void modify_change(
+	struct repository *repo,
+	const struct object_id *old_commit,
+	const struct object_id *new_commit,
+	struct strbuf *err)
+{
+	struct metacommit_data metacommit;
+
+	init_metacommit_data(&metacommit);
+	oidcpy(&(metacommit.content), new_commit);
+	oid_array_append(&(metacommit.replace), old_commit);
+
+	record_metacommit(repo, &metacommit, NULL, 0, err);
+
+	clear_metacommit_data(&metacommit);
+}
diff --git a/metacommit.h b/metacommit.h
new file mode 100644
index 00000000000..fdb253f0f04
--- /dev/null
+++ b/metacommit.h
@@ -0,0 +1,58 @@
+#ifndef METACOMMIT_H
+#define METACOMMIT_H
+
+#include "hash.h"
+#include "oid-array.h"
+#include "repository.h"
+#include "string-list.h"
+
+
+struct change_table;
+
+/* If specified, non-fast-forward changes are permitted. */
+#define UPDATE_OPTION_FORCE     0x0001
+/**
+ * If specified, no attempt will be made to append to existing changes.
+ * Normally, if a metacommit points to a commit in its replace or origin
+ * list and an existing change points to that same commit as its content, the
+ * new metacommit will attempt to append to that same change. This may replace
+ * the commit parent with one or more metacommits from the head of the appended
+ * changes. This option disables this behavior, and will always create a new
+ * change rather than reusing existing changes.
+ */
+#define UPDATE_OPTION_NOAPPEND  0x0002
+
+/* Metacommit Data */
+
+struct metacommit_data {
+	struct object_id content;
+	struct oid_array replace;
+	struct oid_array origin;
+	int abandoned;
+};
+
+extern void init_metacommit_data(struct metacommit_data *state);
+
+extern void clear_metacommit_data(struct metacommit_data *state);
+
+extern int record_metacommit(struct repository *repo,
+	const struct metacommit_data *metacommit,
+	const char* override_change, int options, struct strbuf *err);
+
+extern int record_metacommit_withresult(
+	struct repository *repo,
+	struct change_table *chtable,
+	const struct metacommit_data *metacommit,
+	const char *override_change,
+	int options,
+	struct strbuf *err,
+	struct string_list *changes);
+
+extern void modify_change(struct repository *repo,
+	const struct object_id *old_commit, const struct object_id *new_commit,
+	struct strbuf *err);
+
+extern int write_metacommit(struct repository *repo, struct metacommit_data *state,
+	struct object_id *result);
+
+#endif
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH 07/10] evolve: implement the git change command
  2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
                   ` (5 preceding siblings ...)
  2022-09-23 18:55 ` [PATCH 06/10] evolve: add support for writing metacommits Stefan Xenos via GitGitGadget
@ 2022-09-23 18:55 ` Stefan Xenos via GitGitGadget
  2022-09-25  9:10   ` Phillip Wood
  2022-09-26  8:25   ` Ævar Arnfjörð Bjarmason
  2022-09-23 18:55 ` [PATCH 08/10] evolve: add the git change list command Stefan Xenos via GitGitGadget
                   ` (5 subsequent siblings)
  12 siblings, 2 replies; 66+ messages in thread
From: Stefan Xenos via GitGitGadget @ 2022-09-23 18:55 UTC (permalink / raw)
  To: git; +Cc: Christophe Poucet, Stefan Xenos

From: Stefan Xenos <sxenos@google.com>

Implement the git change update command, which
are sufficient for constructing change graphs.

For example, to create a new change (a stable name) that refers to HEAD:

git change update -c HEAD

To record a rebase or amend in the change graph:

git change update -c <new_commit> -r <old_commit>

To record a cherry-pick in the change graph:

git change update -c <new_commit> -o <original_commit>

Signed-off-by: Stefan Xenos <sxenos@google.com>
Signed-off-by: Chris Poucet <poucet@google.com>
---
 .gitignore       |   1 +
 Makefile         |   1 +
 builtin.h        |   1 +
 builtin/change.c | 199 +++++++++++++++++++++++++++++++++++++++++++++++
 git.c            |   1 +
 ref-filter.c     |   2 +-
 ref-filter.h     |   4 +
 7 files changed, 208 insertions(+), 1 deletion(-)
 create mode 100644 builtin/change.c

diff --git a/.gitignore b/.gitignore
index b3dcafcb331..a57fd8d8897 100644
--- a/.gitignore
+++ b/.gitignore
@@ -28,6 +28,7 @@
 /git-bugreport
 /git-bundle
 /git-cat-file
+/git-change
 /git-check-attr
 /git-check-ignore
 /git-check-mailmap
diff --git a/Makefile b/Makefile
index 68082ef94c7..82f68f13d9f 100644
--- a/Makefile
+++ b/Makefile
@@ -1142,6 +1142,7 @@ BUILTIN_OBJS += builtin/branch.o
 BUILTIN_OBJS += builtin/bugreport.o
 BUILTIN_OBJS += builtin/bundle.o
 BUILTIN_OBJS += builtin/cat-file.o
+BUILTIN_OBJS += builtin/change.o
 BUILTIN_OBJS += builtin/check-attr.o
 BUILTIN_OBJS += builtin/check-ignore.o
 BUILTIN_OBJS += builtin/check-mailmap.o
diff --git a/builtin.h b/builtin.h
index 8901a34d6bf..c10f20c972c 100644
--- a/builtin.h
+++ b/builtin.h
@@ -122,6 +122,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix);
 int cmd_bugreport(int argc, const char **argv, const char *prefix);
 int cmd_bundle(int argc, const char **argv, const char *prefix);
 int cmd_cat_file(int argc, const char **argv, const char *prefix);
+int cmd_change(int argc, const char **argv, const char *prefix);
 int cmd_checkout(int argc, const char **argv, const char *prefix);
 int cmd_checkout__worker(int argc, const char **argv, const char *prefix);
 int cmd_checkout_index(int argc, const char **argv, const char *prefix);
diff --git a/builtin/change.c b/builtin/change.c
new file mode 100644
index 00000000000..b0e29e87ec9
--- /dev/null
+++ b/builtin/change.c
@@ -0,0 +1,199 @@
+#include "builtin.h"
+#include "ref-filter.h"
+#include "parse-options.h"
+#include "metacommit.h"
+#include "change-table.h"
+#include "config.h"
+
+static const char * const builtin_change_usage[] = {
+	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
+	NULL
+};
+
+static const char * const builtin_update_usage[] = {
+	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
+	NULL
+};
+
+struct update_state {
+	int options;
+	const char* change;
+	const char* content;
+	struct string_list replace;
+	struct string_list origin;
+};
+
+static void init_update_state(struct update_state *state)
+{
+	memset(state, 0, sizeof(*state));
+	state->content = "HEAD";
+	string_list_init_nodup(&state->replace);
+	string_list_init_nodup(&state->origin);
+}
+
+static void clear_update_state(struct update_state *state)
+{
+	string_list_clear(&state->replace, 0);
+	string_list_clear(&state->origin, 0);
+}
+
+static int update_option_parse_replace(const struct option *opt,
+				       const char *arg, int unset)
+{
+	struct update_state *state = opt->value;
+	string_list_append(&state->replace, arg);
+	return 0;
+}
+
+static int update_option_parse_origin(const struct option *opt,
+				      const char *arg, int unset)
+{
+	struct update_state *state = opt->value;
+	string_list_append(&state->origin, arg);
+	return 0;
+}
+
+static int resolve_commit(const char *committish, struct object_id *result)
+{
+	struct commit *commit;
+	if (get_oid_committish(committish, result))
+		die(_("Failed to resolve '%s' as a valid revision."), committish);
+	commit = lookup_commit_reference(the_repository, result);
+	if (!commit)
+		die(_("Could not parse object '%s'."), committish);
+	oidcpy(result, &commit->object.oid);
+	return 0;
+}
+
+static void resolve_commit_list(const struct string_list *commitsish_list,
+	struct oid_array* result)
+{
+	int i;
+	for (i = 0; i < commitsish_list->nr; i++) {
+		struct string_list_item *item = &commitsish_list->items[i];
+		struct object_id next;
+		resolve_commit(item->string, &next);
+		oid_array_append(result, &next);
+	}
+}
+
+/*
+ * Given the command-line options for the update command, fills in a
+ * metacommit_data with the corresponding changes.
+ */
+static void get_metacommit_from_command_line(
+	const struct update_state* commands, struct metacommit_data *result)
+{
+	resolve_commit(commands->content, &(result->content));
+	resolve_commit_list(&(commands->replace), &(result->replace));
+	resolve_commit_list(&(commands->origin), &(result->origin));
+}
+
+static int perform_update(
+	struct repository *repo,
+	const struct update_state *state,
+	struct strbuf *err)
+{
+	struct metacommit_data metacommit;
+	struct change_table chtable;
+	struct string_list changes;
+	int ret;
+	int i;
+
+	change_table_init(&chtable);
+	change_table_add_all_visible(&chtable, repo);
+	string_list_init_dup(&changes);
+
+	init_metacommit_data(&metacommit);
+
+	get_metacommit_from_command_line(state, &metacommit);
+
+	ret = record_metacommit_withresult(repo, &chtable, &metacommit,
+		state->change, state->options, err, &changes);
+
+	for (i = 0; i < changes.nr; i++) {
+		struct string_list_item *it = &changes.items[i];
+
+		const char* name = lstrip_ref_components(it->string, 1);
+		if (!name)
+			die(_("Failed to remove `refs/` from %s"), it->string);
+
+		if (it->util)
+			fprintf(stdout, N_("Updated change %s\n"), name);
+		else
+			fprintf(stdout, N_("Created change %s\n"), name);
+	}
+
+	string_list_clear(&changes, 0);
+	change_table_clear(&chtable);
+	clear_metacommit_data(&metacommit);
+
+	return ret;
+}
+
+static int change_update(int argc, const char **argv, const char* prefix)
+{
+	int result;
+	int force = 0;
+	int newchange = 0;
+	struct strbuf err = STRBUF_INIT;
+	struct update_state state;
+	struct option options[] = {
+		{ OPTION_CALLBACK, 'r', "replace", &state, N_("commit"),
+			N_("marks the given commit as being obsolete"),
+			0, update_option_parse_replace },
+		{ OPTION_CALLBACK, 'o', "origin", &state, N_("commit"),
+			N_("marks the given commit as being the origin of this commit"),
+			0, update_option_parse_origin },
+		OPT_BOOL('F', "force", &force,
+			N_("overwrite an existing change of the same name")),
+		OPT_STRING('c', "content", &state.content, N_("commit"),
+				 N_("identifies the new content commit for the change")),
+		OPT_STRING('g', "change", &state.change, N_("commit"),
+				 N_("name of the change to update")),
+		OPT_BOOL('n', "new", &newchange,
+			N_("create a new change - do not append to any existing change")),
+		OPT_END()
+	};
+
+	init_update_state(&state);
+
+	argc = parse_options(argc, argv, prefix, options, builtin_update_usage, 0);
+
+	if (force) state.options |= UPDATE_OPTION_FORCE;
+	if (newchange) state.options |= UPDATE_OPTION_NOAPPEND;
+
+	result = perform_update(the_repository, &state, &err);
+
+	if (result < 0) {
+		error("%s", err.buf);
+		strbuf_release(&err);
+	}
+
+	clear_update_state(&state);
+
+	return result;
+}
+
+int cmd_change(int argc, const char **argv, const char *prefix)
+{
+	/* No options permitted before subcommand currently */
+	struct option options[] = {
+		OPT_END()
+	};
+	int result = 1;
+
+	argc = parse_options(argc, argv, prefix, options, builtin_change_usage,
+		PARSE_OPT_STOP_AT_NON_OPTION);
+
+	if (argc < 1)
+		usage_with_options(builtin_change_usage, options);
+	else if (!strcmp(argv[0], "update"))
+		result = change_update(argc, argv, prefix);
+	else {
+		error(_("Unknown subcommand: %s"), argv[0]);
+		usage_with_options(builtin_change_usage, options);
+	}
+
+	return result ? 1 : 0;
+}
diff --git a/git.c b/git.c
index da411c53822..837b1abc53b 100644
--- a/git.c
+++ b/git.c
@@ -498,6 +498,7 @@ static struct cmd_struct commands[] = {
 	{ "bugreport", cmd_bugreport, RUN_SETUP_GENTLY },
 	{ "bundle", cmd_bundle, RUN_SETUP_GENTLY },
 	{ "cat-file", cmd_cat_file, RUN_SETUP },
+	{ "change", cmd_change, RUN_SETUP},
 	{ "check-attr", cmd_check_attr, RUN_SETUP },
 	{ "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
 	{ "check-mailmap", cmd_check_mailmap, RUN_SETUP },
diff --git a/ref-filter.c b/ref-filter.c
index 6a1789c623f..2d7a919d547 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1557,7 +1557,7 @@ static inline char *copy_advance(char *dst, const char *src)
 	return dst;
 }
 
-static const char *lstrip_ref_components(const char *refname, int len)
+const char *lstrip_ref_components(const char *refname, int len)
 {
 	long remaining = len;
 	const char *start = xstrdup(refname);
diff --git a/ref-filter.h b/ref-filter.h
index 064fbef8e50..7a7737e9552 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -145,4 +145,8 @@ struct ref_array_item *ref_array_push(struct ref_array *array,
 				      const char *refname,
 				      const struct object_id *oid);
 
+/* Strips `len` prefix components from the refname. */
+const char *lstrip_ref_components(const char *refname, int len);
+
+
 #endif /*  REF_FILTER_H  */
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH 08/10] evolve: add the git change list command
  2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
                   ` (6 preceding siblings ...)
  2022-09-23 18:55 ` [PATCH 07/10] evolve: implement the git change command Stefan Xenos via GitGitGadget
@ 2022-09-23 18:55 ` Stefan Xenos via GitGitGadget
  2022-09-23 18:55 ` [PATCH 09/10] evolve: add delete command Chris Poucet via GitGitGadget
                   ` (4 subsequent siblings)
  12 siblings, 0 replies; 66+ messages in thread
From: Stefan Xenos via GitGitGadget @ 2022-09-23 18:55 UTC (permalink / raw)
  To: git; +Cc: Christophe Poucet, Stefan Xenos

From: Stefan Xenos <sxenos@google.com>

This command lists the ongoing changes from the refs/metas
namespace.

Signed-off-by: Stefan Xenos <sxenos@google.com>
Signed-off-by: Chris Poucet <poucet@google.com>
---
 builtin/change.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)

diff --git a/builtin/change.c b/builtin/change.c
index b0e29e87ec9..67d708dc8de 100644
--- a/builtin/change.c
+++ b/builtin/change.c
@@ -6,15 +6,78 @@
 #include "config.h"
 
 static const char * const builtin_change_usage[] = {
+	N_("git change list [<pattern>...]"),
 	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
 	NULL
 };
 
+static const char * const builtin_list_usage[] = {
+	N_("git change list [<pattern>...]"),
+	NULL
+};
+
 static const char * const builtin_update_usage[] = {
 	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
 	NULL
 };
 
+static int change_list(int argc, const char **argv, const char* prefix)
+{
+	struct option options[] = {
+		OPT_END()
+	};
+	struct ref_filter filter;
+	/* TODO: See below
+	struct ref_sorting *sorting;
+	struct string_list sorting_options = STRING_LIST_INIT_DUP; */
+	struct ref_format format = REF_FORMAT_INIT;
+	struct ref_array array;
+	int i;
+
+	argc = parse_options(argc, argv, prefix, options, builtin_list_usage, 0);
+
+	setup_ref_filter_porcelain_msg();
+
+	memset(&filter, 0, sizeof(filter));
+	memset(&array, 0, sizeof(array));
+
+	filter.kind = FILTER_REFS_CHANGES;
+	filter.name_patterns = argv;
+
+	filter_refs(&array, &filter, FILTER_REFS_CHANGES);
+
+	/* TODO: This causes a crash. It sets one of the atom_value handlers to
+	 * something invalid, which causes a crash later when we call
+	 * show_ref_array_item. Figure out why this happens and put back the sorting.
+	 *
+	 * sorting = ref_sorting_options(&sorting_options);
+	 * ref_array_sort(sorting, &array); */
+
+	if (!format.format)
+		format.format = "%(refname:lstrip=1)";
+
+	if (verify_ref_format(&format))
+		die(_("unable to parse format string"));
+
+	for (i = 0; i < array.nr; i++) {
+		struct strbuf output = STRBUF_INIT;
+		struct strbuf err = STRBUF_INIT;
+		if (format_ref_array_item(array.items[i], &format, &output, &err))
+			die("%s", err.buf);
+		fwrite(output.buf, 1, output.len, stdout);
+		putchar('\n');
+
+		strbuf_release(&err);
+		strbuf_release(&output);
+	}
+
+	ref_array_clear(&array);
+	/* TODO: see above
+	ref_sorting_release(sorting); */
+
+	return 0;
+}
+
 struct update_state {
 	int options;
 	const char* change;
@@ -188,6 +251,8 @@ int cmd_change(int argc, const char **argv, const char *prefix)
 
 	if (argc < 1)
 		usage_with_options(builtin_change_usage, options);
+	else if (!strcmp(argv[0], "list"))
+		result = change_list(argc, argv, prefix);
 	else if (!strcmp(argv[0], "update"))
 		result = change_update(argc, argv, prefix);
 	else {
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH 09/10] evolve: add delete command
  2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
                   ` (7 preceding siblings ...)
  2022-09-23 18:55 ` [PATCH 08/10] evolve: add the git change list command Stefan Xenos via GitGitGadget
@ 2022-09-23 18:55 ` Chris Poucet via GitGitGadget
  2022-09-26  8:38   ` Ævar Arnfjörð Bjarmason
  2022-09-23 18:55 ` [PATCH 10/10] evolve: add documentation for `git change` Chris Poucet via GitGitGadget
                   ` (3 subsequent siblings)
  12 siblings, 1 reply; 66+ messages in thread
From: Chris Poucet via GitGitGadget @ 2022-09-23 18:55 UTC (permalink / raw)
  To: git; +Cc: Christophe Poucet, Chris Poucet

From: Chris Poucet <poucet@google.com>

The delete command allows a user to delete one or more changes.
This effectively deletes the corresponding /refs/metas/foo ref.

Signed-off-by: Chris Poucet <poucet@google.com>
---
 builtin/change.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 80 insertions(+), 2 deletions(-)

diff --git a/builtin/change.c b/builtin/change.c
index 67d708dc8de..07d029d82d5 100644
--- a/builtin/change.c
+++ b/builtin/change.c
@@ -4,10 +4,12 @@
 #include "metacommit.h"
 #include "change-table.h"
 #include "config.h"
+#include "refs.h"
 
 static const char * const builtin_change_usage[] = {
 	N_("git change list [<pattern>...]"),
-	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
+	N_("git change update [--force] [--replace <treeish>...] [--origin <treeish>...] [--content <newtreeish>]"),
+	N_("git change delete <change-name>..."),
 	NULL
 };
 
@@ -17,7 +19,12 @@ static const char * const builtin_list_usage[] = {
 };
 
 static const char * const builtin_update_usage[] = {
-	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
+	N_("git change update [--force] [--replace <treeish>...] [--origin <treeish>...] [--content <newtreeish>]"),
+	NULL
+};
+
+static const char * const builtin_delete_usage[] = {
+	N_("git change delete <change-name>..."),
 	NULL
 };
 
@@ -238,6 +245,75 @@ static int change_update(int argc, const char **argv, const char* prefix)
 	return result;
 }
 
+typedef int (*each_change_name_fn)(const char *name, const char *ref,
+				   const struct object_id *oid, void *cb_data);
+
+static int for_each_change_name(const char **argv, each_change_name_fn fn,
+				void *cb_data)
+{
+	const char **p;
+	struct strbuf ref = STRBUF_INIT;
+	int had_error = 0;
+	struct object_id oid;
+
+	for (p = argv; *p; p++) {
+		strbuf_reset(&ref);
+		/* Convenience functionality to avoid having to type `metas/` */
+		if (strncmp("metas/", *p, 5)) {
+			strbuf_addf(&ref, "refs/metas/%s", *p);
+		} else {
+			strbuf_addf(&ref, "refs/%s", *p);
+		}
+		if (read_ref(ref.buf, &oid)) {
+			error(_("change '%s' not found."), *p);
+			had_error = 1;
+			continue;
+		}
+		if (fn(*p, ref.buf, &oid, cb_data))
+			had_error = 1;
+	}
+	strbuf_release(&ref);
+	return had_error;
+}
+
+static int collect_changes(const char *name, const char *ref,
+			   const struct object_id *oid, void *cb_data)
+{
+	struct string_list *ref_list = cb_data;
+
+	string_list_append(ref_list, ref);
+	ref_list->items[ref_list->nr - 1].util = oiddup(oid);
+	return 0;
+}
+
+static int change_delete(int argc, const char **argv, const char* prefix) {
+	int result = 0;
+	struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
+	struct string_list_item *item;
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options, builtin_delete_usage, 0);
+
+	result = for_each_change_name(argv, collect_changes, (void *)&refs_to_delete);
+	if (delete_refs(NULL, &refs_to_delete, REF_NO_DEREF))
+		result = 1;
+
+	for_each_string_list_item(item, &refs_to_delete) {
+		const char *name = item->string;
+		struct object_id *oid = item->util;
+		if (!ref_exists(name))
+			printf(_("Deleted change '%s' (was %s)\n"),
+				item->string + 5,
+				find_unique_abbrev(oid, DEFAULT_ABBREV));
+
+		free(oid);
+	}
+	string_list_clear(&refs_to_delete, 0);
+	return result;
+}
+
 int cmd_change(int argc, const char **argv, const char *prefix)
 {
 	/* No options permitted before subcommand currently */
@@ -255,6 +331,8 @@ int cmd_change(int argc, const char **argv, const char *prefix)
 		result = change_list(argc, argv, prefix);
 	else if (!strcmp(argv[0], "update"))
 		result = change_update(argc, argv, prefix);
+	else if (!strcmp(argv[0], "delete"))
+		result = change_delete(argc, argv, prefix);
 	else {
 		error(_("Unknown subcommand: %s"), argv[0]);
 		usage_with_options(builtin_change_usage, options);
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH 10/10] evolve: add documentation for `git change`
  2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
                   ` (8 preceding siblings ...)
  2022-09-23 18:55 ` [PATCH 09/10] evolve: add delete command Chris Poucet via GitGitGadget
@ 2022-09-23 18:55 ` Chris Poucet via GitGitGadget
  2022-09-25  8:41   ` Phillip Wood
  2022-09-25  8:39 ` [PATCH 00/10] Add the Git Change command Phillip Wood
                   ` (2 subsequent siblings)
  12 siblings, 1 reply; 66+ messages in thread
From: Chris Poucet via GitGitGadget @ 2022-09-23 18:55 UTC (permalink / raw)
  To: git; +Cc: Christophe Poucet, Chris Poucet

From: Chris Poucet <poucet@google.com>

Signed-off-by: Chris Poucet <poucet@google.com>
---
 Documentation/git-change.txt | 55 ++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 Documentation/git-change.txt

diff --git a/Documentation/git-change.txt b/Documentation/git-change.txt
new file mode 100644
index 00000000000..ea9a8e619b9
--- /dev/null
+++ b/Documentation/git-change.txt
@@ -0,0 +1,55 @@
+git-change(1)
+=============
+
+NAME
+----
+git-change - Create, list, update or delete changes
+
+SYNOPSIS
+--------
+[verse]
+'git change' list [<pattern>...]
+'git change' update [-g <change-name> | -n] [--force] [--replace <treeish>...] [--origin <treeish>...] [--content <newtreeish>]
+'git change' delete <change-name>...
+
+DESCRIPTION
+-----------
+
+`git change list`: lists all existing <change-name>s.
+
+`git change delete`: deletes the given <change-name>s.
+
+`git change update`: creates or updates a <change-name>.
+
+If no arguments are given to `update` then a change is added to the
+`refs/metas/` directory, unless a change already exists for the given commit.
+
+A <change-name> starts with `metas/` and represents the current change that is
+being worked on.
+
+OPTIONS
+-------
+-c::
+--content::
+	Identifies the content commit for the change
+
+-o::
+--origin::
+	Marks the given commit as being the origin of this commit.
+
+-r::
+--replace::
+	Marks the given commit as being obsoleted by the new commit.
+
+-g::
+	<change-name> to update
+
+-n::
+	Indicates that the change is new and an existing change should not be updated.
+
+--force::
+	Overwite an existing change of the same name.
+
+GIT
+---
+Part of the linkgit:git[1] suite
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* Re: [PATCH 01/10] technical doc: add a design doc for the evolve command
  2022-09-23 18:55 ` [PATCH 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
@ 2022-09-23 19:59   ` Jerry Zhang
  2022-09-28 21:26   ` Junio C Hamano
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 66+ messages in thread
From: Jerry Zhang @ 2022-09-23 19:59 UTC (permalink / raw)
  To: Stefan Xenos via GitGitGadget; +Cc: git, Christophe Poucet, Stefan Xenos

On Fri, Sep 23, 2022 at 11:56 AM Stefan Xenos via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Stefan Xenos <sxenos@google.com>
>
> This document describes what a change graph for
> git would look like, the behavior of the evolve command,
> and the changes planned for other commands.
>
> It was originally proposed in 2018, see
> https://public-inbox.org/git/20181115005546.212538-1-sxenos@google.com/
>
> Signed-off-by: Stefan Xenos <sxenos@google.com>
> Signed-off-by: Chris Poucet <poucet@google.com>
> ---
>  Documentation/technical/evolve.txt | 1051 ++++++++++++++++++++++++++++
>  1 file changed, 1051 insertions(+)
>  create mode 100644 Documentation/technical/evolve.txt
>
> diff --git a/Documentation/technical/evolve.txt b/Documentation/technical/evolve.txt
> new file mode 100644
> index 00000000000..68ee2457e52
> --- /dev/null
> +++ b/Documentation/technical/evolve.txt
> @@ -0,0 +1,1051 @@
> +Evolve
> +======
> +
> +Objective
> +=========
> +Create an "evolve" command to help users craft a high quality commit history.
> +Users can improve commits one at a time and in any order, then run git evolve to
> +rewrite their recent history to ensure everything is up-to-date. We track
> +amendments to a commit over time in a change graph. Users can share their
> +progress with others by exchanging their change graphs using the standard push,
> +fetch, and format-patch commands.
> +
> +Status
> +======
> +This proposal has not been implemented yet.
> +
> +Background
> +==========
> +Imagine you have three sequential changes up for review and you receive feedback
> +that requires editing all three changes. We'll define the word "change"
> +formally later, but for the moment let's say that a change is a work-in-progress
> +whose final version will be submitted as a commit in the future.
> +
> +While you're editing one change, more feedback arrives on one of the others.
> +What do you do?
> +
> +The evolve command is a convenient way to work with chains of commits that are
> +under review. Whenever you rebase or amend a commit, the repository remembers
> +that the old commit is obsolete and has been replaced by the new one. Then, at
> +some point in the future, you can run "git evolve" and the correct sequence of
> +rebases will occur in the correct order such that no commit has an obsolete
> +parent.
> +
> +Part of making the "evolve" command work involves tracking the edits to a commit
> +over time, which is why we need an change graph. However, the change
> +graph will also bring other benefits:
> +
> +- Users can view the history of a change directly (the sequence of amends and
> +  rebases it has undergone, orthogonal to the history of the branch it is on).
> +- It will be possible to quickly locate and list all the changes the user
> +  currently has in progress.
> +- It can be used as part of other high-level commands that combine or split
> +  changes.
> +- It can be used to decorate commits (in git log, gitk, etc) that are either
> +  obsolete or are the tip of a work in progress.
> +- By pushing and pulling the change graph, users can collaborate more
> +  easily on changes-in-progress. This is better than pushing and pulling the
> +  commits themselves since the change graph can be used to locate a more
> +  specific merge base, allowing for better merges between different versions of
> +  the same change.
> +- It could be used to correctly rebase local changes and other local branches
> +  after running git-filter-branch.
> +- It can replace the change-id footer used by gerrit.
> +
> +Goals
> +-----
> +Legend: Goals marked with P0 are required. Goals marked with Pn should be
> +attempted unless they interfere with goals marked with Pn-1.
> +
> +P0. All commands that modify commits (such as the normal commit --amend or
> +    rebase command) should mark the old commit as being obsolete and replaced by
> +    the new one. No additional commands should be required to keep the
> +    change graph up-to-date.
> +P0. Any commit that may be involved in a future evolve command should not be
> +    garbage collected. Specifically:
> +    - Commits that obsolete another should not be garbage collected until
> +      user-specified conditions have occurred and the change has expired from
> +      the reflog. User specified conditions for removing changes include:
> +      - The user explicitly deleted the change.
> +      - The change was merged into a specific branch.
> +    - Commits that have been obsoleted by another should not be garbage
> +      collected if any of their replacements are still being retained.
> +P0. A commit can be obsoleted by more than one replacement (called divergence).
> +P0. Users must be able to resolve divergence (convergence).
> +P1. Users should be able to share chains of obsolete changes in order to
> +    collaborate on WIP changes.
> +P2. Such sharing should be at the user’s option. That is, it should be possible
> +    to directly share a change without also sharing the file states or commit
> +    comments from the obsolete changes that led up to it, and the choice not to
> +    share those commits should not require changing any commit hashes.
> +P2. It should be possible to discard part or all of the change graph
> +    without discarding the commits themselves that are already present in
> +    branches and the reflog.
> +P2. Provide sufficient information to replace gerrit's Change-Id footers.
> +
> +Similar technologies
> +--------------------
> +There are some other technologies that address the same end-user problem.
> +
> +Rebase -i can be used to solve the same problem, but users can't easily switch
> +tasks midway through an interactive rebase or have more than one interactive
> +rebase going on at the same time. It can't handle the case where you have
> +multiple changes sharing the same parent when that parent needs to be rebased
> +and won't let you collaborate with others on resolving a complicated interactive
> +rebase. You can think of rebase -i as a top-down approach and the evolve command
> +as the bottom-up approach to the same problem.
I'll mention some other tools in this space too:

revup amend (https://github.com/Skydio/revup/blob/main/docs/amend.md)
(I'm the author) allows insertion of cached changes into any commit in
the current history, and then reapplies the rest of history on top of
those changes. It uses a "git apply --cached" engine under the hood so
doesn't touch the working directory (although it will soon use the new
git merge-tree). When paired with "revup upload" which creates and
pushes multiple branches in the background for you, its possible to
work on a "graph" of changes on a single branch linearly, then have
the true graph structure created at upload time.

git-revise (https://github.com/mystor/git-revise) does some very
similar things except it uses "git merge-file" combined with manually
merging the resulting trees. git branchstack
(https://github.com/krobelus/git-branchstack) can also create branches
in the background for you with the same mechanism.

These tools don't store any external state, but as such also don't
provide any specific collaboration mechanism for individual changes,
so I'm interested in the "evolve" approach as well.
> +
> +Several patch queue managers have been built on top of git (such as topgit,
> +stgit, and quilt). They address the same user need. However they also rely on
> +state managed outside git that needs to be kept in sync. Such state can be
> +easily damaged when running a git native command that is unaware of the patch
> +queue. They also typically require an explicit initialization step to be done by
> +the user which creates workflow problems.
> +
> +Mercurial implements a very similar feature in its EvolveExtension. The behavior
> +of the evolve command itself is very similar, but the storage format for the
> +change graph differs. In the case of mercurial, each change set can have one or
> +more obsolescence markers that point to other changesets that they replace. This
> +is similar to the "Commit Headers" approach considered in the other options
> +appendix. The approach proposed here stores obsolescence information in a
> +separate metacommit graph, which makes exchanging of obsolescence information
> +optional.
> +
> +Mercurial's default behavior makes it easy to find and switch between
> +non-obsolete changesets that aren't currently on any branch. We introduce the
> +notion of a new ref namespace that enables a similar workflow via a different
> +mechanism. Mercurial has the notion of changeset phases which isn't present
> +in git and creates new ways for a changeset to diverge. Git doesn't need
> +to deal with these issues, but it has to deal with the problems of picking an
> +upstream branch as a target for rebases and protecting obsolescence information
> +from GC. We also introduce some additional transformations (see
> +obsolescence-over-cherry-pick, below) that aren't present in the mercurial
> +implementation.
> +
> +Semi-related work
> +-----------------
> +There are other technologies that address different problems but have some
> +similarities with this proposal.
> +
> +Replacements (refs/replace) are superficially similar to obsolescences in that
> +they describe that one commit should be replaced by another. However, they
> +differ in both how they are created and how they are intended to be used.
> +Obsolescences are created automatically by the commands a user runs, and they
> +describe the user’s intent to perform a future rebase. Obsolete commits still
> +appear in branches, logs, etc like normal commits (possibly with an extra
> +decoration that marks them as obsolete). Replacements are typically created
> +explicitly by the user, they are meant to be kept around for a long time, and
> +they describe a replacement to be applied at read-time rather than as the input
> +to a future operation. When a replaced commit is queried, it is typically hidden
> +and swapped out with its replacement as though the replacement has already
> +occurred.
> +
> +Git-imerge is a project to help make complicated merges easier, particularly
> +when merging or rebasing long chains of patches. It is not an alternative to
> +the change graph, but its algorithm of applying smaller incremental merges
> +could be used as part of the evolve algorithm in the future.
> +
> +Overview
> +========
> +We introduce the notion of “meta-commits” which describe how one commit was
> +created from other commits. A branch of meta-commits is known as a change.
> +Changes are created and updated automatically whenever a user runs a command
> +that creates a commit. They are used for locating obsolete commits, providing a
> +list of a user’s unsubmitted work in progress, and providing a stable name for
> +each unsubmitted change.
> +
> +Users can exchange edit histories by pushing and fetching changes.
> +
> +New commands will be introduced for manipulating changes and resolving
> +divergence between them. Existing commands that create commits will be updated
> +to modify the meta-commit graph and create changes where necessary.
> +
> +Example usage
> +-------------
> +# First create three dependent changes
> +$ echo foo>bar.txt && git add .
> +$ git commit -m "This is a test"
> +created change metas/this_is_a_test
> +$ echo foo2>bar2.txt && git add .
> +$ git commit -m "This is also a test"
> +created change metas/this_is_also_a_test
> +$ echo foo3>bar3.txt && git add .
> +$ git commit -m "More testing"
> +created change metas/more_testing
> +
> +# List all our changes in progress
> +$ git change list
> +metas/this_is_a_test
> +metas/this_is_also_a_test
> +* metas/more_testing
> +metas/some_change_already_merged_upstream
> +
> +# Now modify the earliest change, using its stable name
> +$ git reset --hard metas/this_is_a_test
> +$ echo morefoo>>bar.txt && git add . && git commit --amend --no-edit
> +
> +# Use git-evolve to fix up any dependent changes
> +$ git evolve
> +rebasing metas/this_is_also_a_test onto metas/this_is_a_test
> +rebasing metas/more_testing onto metas/this_is_also_a_test
> +Done
> +
> +# Use git-obslog to view the history of the this_is_a_test change
> +$ git log --obslog
> +93f110 metas/this_is_a_test@{0} commit (amend): This is a test
> +930219 metas/this_is_a_test@{1} commit: This is a test
> +
> +# Now create an unrelated change
> +$ git reset --hard origin/master
> +$ echo newchange>unrelated.txt && git add .
> +$ git commit -m "Unrelated change"
> +created change metas/unrelated_change
> +
> +# Fetch the latest code from origin/master and use git-evolve
> +# to rebase all dependent changes.
> +$ git fetch origin master
> +$ git evolve origin/master
> +deleting metas/some_change_already_merged_upstream
> +rebasing metas/this_is_a_test onto origin/master
> +rebasing metas/this_is_also_a_test onto metas/this_is_a_test
> +rebasing metas/more_testing onto metas/this_is_also_a_test
> +rebasing metas/unrelated_change onto origin/master
> +Conflict detected! Resolve it and then use git evolve --continue to resume.
> +
> +# Sort out the conflict
> +$ git mergetool
> +$ git evolve origin/master
> +Done
> +
> +# Share the full history of edits for the this_is_a_test change
> +# with a review server
> +$ git push origin metas/this_is_a_test:refs/for/master
> +# Share the lastest commit for “Unrelated change”, without history
> +$ git push origin HEAD:refs/for/master
> +
> +Detailed design
> +===============
> +Obsolescence information is stored as a graph of meta-commits. A meta-commit is
> +a specially-formatted merge commit that describes how one commit was created
> +from others.
> +
> +Meta-commits look like this:
> +
> +$ git cat-file -p <example_meta_commit>
> +tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
> +parent aa7ce55545bf2c14bef48db91af1a74e2347539a
> +parent d64309ee51d0af12723b6cb027fc9f195b15a5e9
> +parent 7e1bbcd3a0fa854a7a9eac9bf1eea6465de98136
> +author Stefan Xenos <sxenos@gmail.com> 1540841596 -0700
> +committer Stefan Xenos <sxenos@gmail.com> 1540841596 -0700
> +parent-type c r o
> +
> +This says “commit aa7ce555 makes commit d64309ee obsolete. It was created by
> +cherry-picking commit 7e1bbcd3”.
> +
> +The tree for meta-commits is always the empty tree, but future versions of git
> +may attach other trees here. For forward-compatibility fsck should ignore such
> +trees if found on future repository versions. This will allow future versions of
> +git to add metadata to the meta-commit tree without breaking forwards
> +compatibility.
> +
> +The commit comment for a meta-commit is an auto-generated user-readable string
> +describing the command that produced the meta commit. These strings are shown
> +to the user when they view the obslog.
> +
> +Parent-type
> +-----------
> +The “parent-type” field in the commit header identifies a commit as a
> +meta-commit and indicates the meaning for each of its parents. It is never
> +present for normal commits. It contains a space-deliminated list of enum values
> +whose order matches the order of the parents. Possible parent types are:
> +
> +- c: (content) the content parent identifies the commit that this meta-commit is
> +  describing.
> +- r: (replaced) indicates that this parent is made obsolete by the content
> +  parent.
> +- o: (origin) indicates that the content parent was generated by cherry-picking
> +  this parent.
> +- a: (abandoned) used in place of a content parent for abandoned changes. Points
> +  to the final content commit for the change at the time it was abandoned.
> +
> +There must be exactly one content or abandoned parent for each meta-commit and
> +it is always the first parent. The content commit will always be a normal commit
> +and not a meta-commit. However, future versions of git may create meta-commits
> +for other meta-commits and the fsck tool must be aware of this for forwards
> +compatibility.
> +
> +A meta-commit can have zero or more replaced parents. An amend operation creates
> +a single replaced parent. A merge used to resolve divergence (see divergence,
> +below) will create multiple replaced parents. A meta-commit may have no
> +replaced parents if it describes a cherry-pick or squash merge that copies one
> +or more commits but does not replace them.
> +
> +A meta-commit can have zero or more origin parents. A cherry-pick creates a
> +single origin parent. Certain types of squash merge will create multiple origin
> +parents. Origin parents don't directly cause their origin to become obsolete,
> +but are used when computing blame or locating a merge base. The section
> +on obsolescence over cherry-picks describes how the evolve command uses
> +origin parents.
> +
> +A replaced parent or origin parent may be either a normal commit (indicating
> +the oldest-known version of a change) or another meta-commit (for a change that
> +has already been modified one or more times).
> +
> +The parent-type field needs to go after the committer field since git's rules
> +for forwards-compatibility require that new fields to be at the end of the
> +header. Putting a new field in the middle of the header would break fsck.
> +
> +The presence of an abandoned parent indicates that the change should be pruned
> +by the evolve command, and removed from the repository's history. Any follow-up
> +changes should rebased onto the parent of the pruned commit. The abandoned
> +parent points to the version of the change that should be restored if the user
> +attempts to restore the change.
> +
> +Changes
> +-------
> +A branch of meta-commits describes how a commit was produced and what previous
> +commits it is based on. It is also an identifier for a thing the user is
> +currently working on. We refer to such a meta-branch as a change.
> +
> +Local changes are stored in the new refs/metas namespace. Remote changes are
> +stored in the refs/remote/<remotename>/metas namespace.
> +
> +The list of changes in refs/metas is more than just a mechanism for the evolve
> +command to locate obsolete commits. It is also a convenient list of all of a
> +user’s work in progress and their current state - a list of things they’re
> +likely to want to come back to.
> +
> +Strictly speaking, it is the presence of the branch in the refs/metas namespace
> +that marks a branch as being a change, not the fact that it points to a
> +metacommit. Metacommits are only created when a commit is amended or rebased, so
> +in the case where a change points to a commit that has never been modified, the
> +change points to that initial commit rather than a metacommit.
> +
> +Changes are also stored in the refs/hiddenmetas namespace. Hiddenmetas holds
> +metadata for historical changes that are not currently in progress by the user.
> +Commands like filter-branch and other bulk import commands create metadata in
> +this namespace.
> +
> +Note that the changes in hiddenmetas get special treatment in several ways:
> +
> +- They are not cleaned up automatically once merged, since it is expected that
> +  they refer to historical changes.
> +- User commands that modify changes don't append to these changes as they would
> +  to a change in refs/metas.
> +- They are not displayed when the user lists their local changes.
> +
> +Obsolescence
> +------------
> +A commit is considered obsolete if it is reachable from the “replaces” edges
> +anywhere in the history of a change and it isn’t the head of that change.
> +Commits may be the content for 0 or more meta-commits. If the same commit
> +appears in multiple changes, it is not obsolete if it is the head of any of
> +those changes.
> +
> +Note that there is an exception to this rule. The metas namespace takes
> +precedence over the hiddenmetas namespace for the purpose of obsolescence. That
> +is, if a change appears in a replaces edge of a change in the metas namespace,
> +it is obsolete even if it also appears as the head of a change in the
> +hiddenmetas namespace.
> +
> +This special case prevents the hiddenmetas namespace from creating divergence
> +with the user's work in progress, and allows the user to resolve historical
> +divergence by creating new changes in the metas namespace.
> +
> +Divergence
> +----------
> +From the user’s perspective, two changes are divergent if they both ask for
> +different replacements to the same commit. More precisely, a target commit is
> +considered divergent if there is more than one commit at the head of a change in
> +refs/metas that leads to the target commit via an unbroken chain of “replaces”
> +parents.
> +
> +Much like a merge conflict, divergence is a situation that requires user
> +intervention to resolve. The evolve command will stop when it encounters
> +divergence and prompt the user to resolve the problem. Users can solve the
> +problem in several ways:
> +
> +- Discard one of the changes (by deleting its change branch).
> +- Merge the two changes (producing a single change branch).
> +- Copy one of the changes (keep both commits, but one of them gets a new
> +  metacommit appended to its history that is connected to its predecessor via an
> +  origin edge rather than a replaces edge. That new change no longer obsoletes
> +  the original.)
> +
> +Obsolescence across cherry-picks
> +--------------------------------
> +By default the evolve command will treat cherry-picks and squash merges as being
> +completely separate from the original. Further amendments to the original commit
> +will have no effect on the cherry-picked copy. However, this behavior may not be
> +desirable in all circumstances.
> +
> +The evolve command may at some point support an option to look for cases where
> +the source of a cherry-pick or squash merge has itself been amended, and
> +automatically apply that same change to the cherry-picked copy. In such cases,
> +it would traverse origin edges rather than ignoring them, and would treat a
> +commit with origin edges as being obsolete if any of its origins were obsolete.
> +
> +Garbage collection
> +------------------
> +For GC purposes, meta-commits are normal commits. Just as a commit causes its
> +parents and tree to be retained, a meta-commit also causes its parents to be
> +retained.
> +
> +Change creation
> +---------------
> +Changes are created automatically whenever the user runs a command like “commit”
> +that has the semantics of creating a new change. They also move forward
> +automatically even if they’re not checked out. For example, whenever the user
> +runs a command like “commit --amend” that modifies a commit, all branches in
> +refs/metas that pointed to the old commit move forward to point to its
> +replacement instead. This also happens when the user is working from a detached
> +head.
> +
> +This does not mean that every commit has a corresponding change. By default,
> +changes only exist for recent locally-created commits. Users may explicitly pull
> +changes from other users or keep their changes around for a long time, but
> +either behavior requires a user to opt-in. Code review systems like gerrit may
> +also choose to keep changes around forever.
> +
> +Note that the changes in refs/metas serve a dual function as both a way to
> +identify obsolete changes and as a way for the user to keep track of their work
> +in progress. If we were only concerned with identifying obsolete changes, it
> +would be sufficient to create the change branch lazily the first time a commit
> +is obsoleted. Addressing the second use - of refs/metas as a mechanism for
> +keeping track of work in progress - is the reason for eagerly creating the
> +change on first commit.
> +
> +Change naming
> +-------------
> +When a change is first created, the only requirement for its name is that it
> +must be unique. Good names would also serve as useful mnemonics and be easy to
> +type. For example, a short word from the commit message containing no numbers or
> +special characters and that shows up with low frequency in other commit messages
> +would make a good choice.
> +
> +Different users may prefer different heuristics for their change names. For this
> +reason a new hook will be introduced to compute change names. Git will invoke
> +the hook for all newly-created changes and will append a numeric suffix if the
> +name isn’t unique. The default heuristics are not specified by this proposal and
> +may change during implementation.
> +
> +Change deletion
> +---------------
> +Changes are normally only interesting to a user while a commit is still in
> +development and under review. Once the commit has submitted wherever it is
> +going, its change can be discarded.
> +
> +The normal way of deleting changes makes this easy to do - changes are deleted
> +by the evolve command when it detects that the change is present in an upstream
> +branch. It does this in two ways: if the latest commit in a change either shows
> +up in the branch history or the change becomes empty after a rebase, it is
> +considered merged and the change is discarded. In this context, an “upstream
> +branch” is any branch passed in as the upstream argument of the evolve command.
> +
> +In case this sometimes deletes a useful change, such automatic deletions are
> +recorded in the reflog allowing them to be easily recovered.
> +
> +Sharing changes
> +---------------
> +Change histories are shared by pushing or fetching meta-commits and change
> +branches. This provides users with a lot of control of what to share and
> +repository implementations with control over what to retain.
> +
> +Users that only want to share the content of a commit can do so by pushing the
> +commit itself as they currently would. Users that want to share an edit history
> +for the commit can push its change, which would point to a meta-commit rather
> +than the commit itself if there is any history to share. Note that multiple
> +changes can refer to the same commits, so it’s possible to construct and push a
> +different history for the same commit in order to remove sensitive or irrelevant
> +intermediate states.
> +
> +Imagine the user is working on a change “mychange” that is currently the latest
> +commit on master. They have two ways to share it:
> +
> +# User shares just a commit without its history
> +> git push origin master
> +
> +# User shares the full history of the commit to a review system
> +> git push origin metas/mychange:refs/for/master
> +
> +# User fetches a collaborator’s modifications to their change
> +> git fetch remotename metas/mychange
> +# Which updates the ref remote/remotename/metas/mychange
> +
> +This will cause more intermediate states to be shared with the server than would
> +have been shared previously. A review system like gerrit would need to keep
> +track of which states had been explicitly pushed versus other intermediate
> +states in order to de-emphasize (or hide) the extra intermediate states from the
> +user interface.
> +
> +Merge-base
> +----------
> +Merge-base will be changed to search the meta-commit graph for common ancestors
> +as well as the commit graph, and will generally prefer results from the
> +meta-commit graph over the commit graph. Merge-base will consider meta-commits
> +from all changes, and will traverse both origin and obsolete edges.
> +
> +The reason for this is that - when merging two versions of the same commit
> +together - an earlier version of that same commit will usually be much more
> +similar than their common parent. This should make the workflow of collaborating
> +on unsubmitted patches as convenient as the workflow for collaborating in a
> +topic branch by eliminating repeated merges.
> +
> +Configuration
> +-------------
> +The core.enableChanges configuration variable enables the creation and update
> +of change branches. This is enabled by default.
> +
> +User interface
> +--------------
> +All git porcelain commands that create commits are classified as having one of
> +four behaviors: modify, create, copy, or import. These behaviors are discussed
> +in more detail below.
> +
> +Modify commands
> +---------------
> +Modification commands (commit --amend, rebase) will mark the old commit as
> +obsolete by creating a new meta-commit that references the old one as a
> +replaced parent. In the event that multiple changes point to the same commit,
> +this is done independently for every such change.
> +
> +More specifically, modifications work like this:
> +
> +1. Locate all existing changes for which the old commit is the content for the
> +   head of the change branch. If no such branch exists, create one that points
> +   to the old commit. Changes that include this commit in their history but not
> +   at their head are explicitly not included.
> +2. For every such change, create a new meta-commit that references the new
> +   commit as its content and references the old head of the change as a
> +   replaced parent.
> +3. Move the change branch forward to point to the new meta-commit.
> +
> +Copy commands
> +-------------
> +Copy commands (cherry-pick, merge --squash) create a new meta-commit that
> +references the old commits as origin parents. Besides the fact that the new
> +parents are tagged differently, copy commands work the same way as modify
> +commands.
> +
> +Create commands
> +---------------
> +Creation commands (commit, merge) create a new commit and a new change that
> +points to that commit. The do not create any meta-commits.
> +
> +Import commands
> +---------------
> +Import commands (fetch, pull) do not create any new meta-commits or changes
> +unless that is specifically what they are importing. For example, the fetch
> +command would update remote/origin/metas/change35 and fetch all referenced
> +meta-commits if asked to do so directly, but it wouldn’t create any changes or
> +meta-commits for commits discovered on the master branch when running “git fetch
> +origin master”.
> +
> +Other commands
> +--------------
> +Some commands don’t fit cleanly into one of the above categories.
> +
> +Semantically, filter-branch should be treated as a modify command, but doing so
> +is likely to create a lot of irrelevant clutter in the changes namespace and the
> +large number of extra change refs may introduce performance problems. We
> +recommend treating filter-branch as an import command initially, but making it
> +behave more like a modify command in future follow-up work. One possible
> +solution may be to treat commits that are part of existing changes as being
> +modified but to avoid creating changes for other rewritten changes. Another
> +solution may be to record the modifications as changes in the hiddenmetas
> +namespace.
> +
> +Once the evolve command can handle obsolescence across cherry-picks, such
> +cherry-picks will result in a hybrid move-and-copy operation. It will create
> +cherry-picks that replace other cherry-picks, which will have both origin edges
> +(pointing to the new source commit being picked) and replacement edges (pointing
> +to the previous cherry-pick being replaced).
> +
> +Evolve
> +------
> +The evolve command performs the correct sequence of rebases such that no change
> +has an obsolete parent. The syntax looks like this:
> +
> +git evolve [upstream…]
> +
> +It takes an optional list of upstream branches. All changes whose parent shows
> +up in the history of one of the upstream branches will be rebased onto the
> +upstream branch before resolving obsolete parents.
> +
> +Any change whose latest state is found in an upstream branch (or that ends up
> +empty after rebase) will be deleted. This is the normal mechanism for deleting
> +changes. Changes are created automatically on the first commit, and are deleted
> +automatically when evolve determines that they’ve been merged upstream.
> +
> +Orphan commits are commits with obsolete parents. The evolve command then
> +repeatedly rebases orphan commits with non-orphan parents until there are either
> +no orphan commits left, or a merge conflict is discovered. It will also
> +terminate if it detects a divergent parent or a cycle that can't be resolved
> +using any of the enabled transformations.
> +
> +When evolve discovers divergence, it will first check if it can resolve the
> +divergence automatically using one of its enabled transformations. Supported
> +transformations are:
> +
> +- Check if the user has already merged the divergent changes in a follow-up
> +  change. That is, look for an existing merge in a follow-up change where all
> +  the parents are divergent versions of the same change. Squash that merge with
> +  its parents and use the result as the resolution for the divergence.
> +
> +- Attempt to auto-merge all the divergent changes (disabled by default).
> +
> +Each of the transformations can be enabled or disabled by command line options.
> +
> +Cycles can occur when two changes reference one another as parents. This can
> +happen when both changes use an obsolete version of the other change as their
> +parent. Although there are never cycles in the commit graph, users can create
> +cycles in the change graph by rebasing changes onto obsolete commits. The evolve
> +command has a transformation that will detect and break cycles by arbitrarily
> +picking one of the changes to go first. If this generates a merge conflict,
> +it tries each of the other changes in sequence to see if any ordering merges
> +cleanly. If no possible ordering merges cleanly, it picks one and terminates
> +to let the user resolve the merge conflict.
> +
> +If the working tree is dirty, evolve will attempt to stash the user's changes
> +before applying the evolve and then reapply those changes afterward, in much
> +the same way as rebase --autostash does.
> +
> +Checkout
> +--------
> +Running checkout on a change by name has the same effect as checking out a
> +detached head pointing to the latest commit on that change-branch. There is no
> +need to ever have HEAD point to a change since changes always move forward when
> +necessary, no matter what branch the user has checked out
> +
> +Meta-commits themselves cannot be checked out by their hash.
> +
> +Reset
> +-----
> +Resetting a branch to a change by name is the same as resetting to the content
> +(or abandoned) commit at that change’s head.
> +
> +Commit
> +------
> +Commit --amend gets modify semantics and will move existing changes forward. The
> +normal form of commit gets create semantics and will create a new change.
> +
> +$ touch foo && git add . && git commit -m "foo" && git tag A
> +$ touch bar && git add . && git commit -m "bar" && git tag B
> +$ touch baz && git add . && git commit -m "baz" && git tag C
> +
> +This produces the following commits:
> +A(tree=[foo])
> +B(tree=[foo, bar], parent=A)
> +C(tree=[foo, bar, baz], parent=B)
> +
> +...along with three changes:
> +metas/foo = A
> +metas/bar = B
> +metas/baz = C
> +
> +Running commit --amend does the following:
> +$ git checkout B
> +$ touch zoom && git add . && git commit --amend -m "baz and zoom"
> +$ git tag D
> +
> +Commits:
> +A(tree=[foo])
> +B(tree=[foo, bar], parent=A)
> +C(tree=[foo, bar, baz], parent=B)
> +D(tree=[foo, bar, zoom], parent=A)
> +Dmeta(content=D, obsolete=B)
> +
> +Changes:
> +metas/foo = A
> +metas/bar = Dmeta
> +metas/baz = C
> +
> +Merge
> +-----
> +Merge gets create, modify, or copy semantics based on what is being merged and
> +the options being used.
> +
> +The --squash version of merge gets copy semantics (it produces a new change that
> +is marked as a copy of all the original changes that were squashed into it).
> +
> +The “modify” version of merge replaces both of the original commits with the
> +resulting merge commit. This is one of the standard mechanisms for resolving
> +divergence. The parents of the merge commit are the parents of the two commits
> +being merged. The resulting commit will not be a merge commit if both of the
> +original commits had the same parent or if one was the parent of the other.
> +
> +The “create” version of merge creates a new change pointing to a merge commit
> +that has both original commits as parents. The result is what merge produces now
> +- a new merge commit. However, this version of merge doesn’t directly resolve
> +divergence.
> +
> +To select between these two behaviors, merge gets new “--amend” and “--noamend”
> +options which select between the “create” and “modify” behaviors respectively,
> +with noamend being the default.
> +
> +For example, imagine we created two divergent changes like this:
> +
> +$ touch foo && git add . && git commit -m "foo" && git tag A
> +$ touch bar && git add . && git commit -m "bar" && git tag B
> +$ touch baz && git add . && git commit --amend -m "bar and baz"
> +$ git tag C
> +$ git checkout B
> +$ touch bam && git add . && git commit --amend -m "bar and bam"
> +$ git tag D
> +
> +At this point the commit graph looks like this:
> +
> +A(tree=[foo])
> +B(tree=[bar], parent=A)
> +C(tree=[bar, baz], parent=A)
> +D(tree=[bar, bam], parent=A)
> +Cmeta(content=C, obsoletes=B)
> +Dmeta(content=D, obsoletes=B)
> +
> +There would be three active changes with heads pointing as follows:
> +
> +metas/changeA=A
> +metas/changeB=Cmeta
> +metas/changeB2=Dmeta
> +
> +ChangeB and changeB2 are divergent at this point. Lets consider what happens if
> +perform each type of merge between changeB and changeB2.
> +
> +Merge example: Amend merge
> +One way to resolve divergent changes is to use an amend merge. Recall that HEAD
> +is currently pointing to D at this point.
> +
> +$ git merge --amend metas/changeB
> +
> +Here we’ve asked for an amend merge since we’re trying to resolve divergence
> +between two versions of the same change. There are no conflicts so we end up
> +with this:
> +
> +E(tree=[bar, baz, bam], parent=A)
> +Emeta(content=E, obsoletes=[Cmeta, Dmeta])
> +
> +With the following branches:
> +
> +metas/changeA=A
> +metas/changeB=Emeta
> +metas/changeB2=Emeta
> +
> +Notice that the result of the “amend merge” is a replacement for C and D rather
> +than a new commit with C and D as parents (as a normal merge would have
> +produced). The parents of the amend merge are the parents of C and D which - in
> +this case - is just A, so the result is not a merge commit. Also notice that
> +changeB and changeB2 are now aliases for the same change.
> +
> +Merge example: Noamend merge
> +Consider what would have happened if we’d used a noamend merge instead. Recall
> +that HEAD was at D and our branches looked like this:
> +
> +metas/changeA=A
> +metas/changeB=Cmeta
> +metas/changeB2=Dmeta
> +
> +$ git merge --noamend metas/changeB
> +
> +That would produce the sort of merge we’d normally expect today:
> +
> +F(tree=[bar, baz, bam], parent=[C, D])
> +
> +And our changes would look like this:
> +metas/changeA=A
> +metas/changeB=Cmeta
> +metas/changeB2=Dmeta
> +metas/changeF=F
> +
> +In this case, changeB and changeB2 are still divergent and we’ve created a new
> +change for our merge commit. However, this is just a temporary state. The next
> +time we run the “evolve” command, it will discover the divergence but also
> +discover the merge commit F that resolves it. Evolve will suggest converting F
> +into an amend merge in order to resolve the divergence and will display the
> +command for doing so.
> +
> +Rebase
> +------
> +In general the rebase command is treated as a modify command. When a change is
> +rebased, the new commit replaces the original.
> +
> +Rebase --abort is special. Its intent is to restore git to the state it had
> +prior to running rebase. It should move back any changes to point to the refs
> +they had prior to running rebase and delete any new changes that were created as
> +part of the rebase. To achieve this, rebase will save the state of all changes
> +in refs/metas prior to running rebase and will restore the entire namespace
> +after rebase completes (deleting any newly-created changes). Newly-created
> +metacommits are left in place, but will have no effect until garbage collected
> +since metacommits are only used if they are reachable from refs/metas.
> +
> +Change
> +------
> +The “change” command can be used to list, rename, reset or delete change. It has
> +a number of subcommands.
> +
> +The "list" subcommand lists local changes. If given the -r argument, it lists
> +remote changes.
> +
> +The "rename" subcommand renames a change, given its old and new name. If the old
> +name is omitted and there is exactly one change pointing to the current HEAD,
> +that change is renamed. If there are no changes pointing to the current HEAD,
> +one is created with the given name.
> +
> +The "forget" subcommand deletes a change by deleting its ref from the metas/
> +namespace. This is the normal way to delete extra aliases for a change if the
> +change has more than one name. By default, this will refuse to delete the last
> +alias for a change if there are any other changes that reference this change as
> +a parent.
> +
> +The "update" subcommand adds a new state to a change. It uses the default
> +algorithm for assigning change names. If the content commit is omitted, HEAD is
> +used. If given the optional --force argument, it will overwrite any existing
> +change of the same name. This latter form of "update" can be used to effectively
> +reset changes.
> +
> +The "update" command can accept any number of --origin and --replace arguments.
> +If any are present, the resulting change branch will point to a metacommit
> +containing the given origin and replacement edges.
> +
> +The "abandon" command deletes a change using obsolescence markers. It marks the
> +change as being obsolete and having been replaced by its parent. If given no
> +arguments, it applies to the current commit. Running evolve will cause any
> +abandoned changes to be removed from the branch. Any child changes will be
> +reparented on top of the parent of the abandoned change. If the current change
> +is abandoned, HEAD will move to point to its parent.
> +
> +The "restore" command restores a previously-abandoned change.
> +
> +The "prune" command deletes all obsolete changes and all changes that are
> +present in the given branch. Note that such changes can be recovered from the
> +reflog.
> +
> +Combined with the GC protection that is offered, this is intended to facilitate
> +a workflow that relies on changes instead of branches. Users could choose to
> +work with no local branches and use changes instead - both for mailing list and
> +gerrit workflows.
> +
> +Log
> +---
> +When a commit is shown in git log that is part of a change, it is decorated with
> +extra change information. If it is the head of a change, the name of the change
> +is shown next to the list of branches. If it is obsolete, it is decorated with
> +the text “obsolete, <n> commits behind <changename>”.
> +
> +Log gets a new --obslog argument indicating that the obsolescence graph should
> +be followed instead of the commit graph. This also changes the default
> +formatting options to make them more appropriate for viewing different
> +iterations of the same commit.
> +
> +Pull
> +----
> +
> +Pull gets an --evolve argument that will automatically attempt to run "evolve"
> +on any affected branches after pulling.
> +
> +We also introduce an "evolve" enum value for the branch.<name>.rebase config
> +value. When set, the evolve behavior will happen automatically for that branch
> +after every pull even if the --evolve argument is not used.
> +
> +Next
> +----
> +
> +The "next" command will reset HEAD to a non-obsolete commit that refers to this
> +change as its parent. If there is more than one such change, the user will be
> +prompted. If given the --evolve argument, the next commit will be evolved if
> +necessary first.
> +
> +The "next" command can be thought of as the opposite of
> +"git reset --hard HEAD^" in that it navigates to a child commit rather than a
> +parent.
> +
> +Prev
> +----
> +
> +The "prev" command will reset HEAD to the latest version of the parent change.
> +If the parent change isn't obsolete, this is equivalent to
> +"git reset --hard HEAD^". If the parent commit is obsolete, it resets to the
> +latest replacement for the parent commit.
> +
> +Other options considered
> +========================
> +We considered several other options for storing the obsolescence graph. This
> +section describes the other options and why they were rejected.
> +
> +Commit header
> +-------------
> +Add an “obsoletes” field to the commit header that points backwards from a
> +commit to the previous commits it obsoletes.
> +
> +Pros:
> +- Very simple
> +- Easy to traverse from a commit to the previous commits it obsoletes.
> +Cons:
> +- Adds a cost to the storage format, even for commits where the change history
> +  is uninteresting.
> +- Unconditionally prevents the change history from being garbage collected.
> +- Always causes the change history to be shared when pushing or pulling changes.
> +
> +Git notes
> +---------
> +Instead of storing obsolescence information in metacommits, the metacommit
> +content could go in a new notes namespace - say refs/notes/metacommit. Each note
> +would contain the list of obsolete and origin parents. An automerger could
> +be supplied to make it easy to merge the metacommit notes from different remotes.
> +
> +Pros:
> +- Easy to locate all commits obsoleted by a given commit (since there would only
> +  be one metacommit for any given commit).
> +Cons:
> +- Wrong GC behavior (obsolete commits wouldn’t automatically be retained by GC)
> +  unless we introduced a special case for these kinds of notes.
> +- No way to selectively share or pull the metacommits for one specific change.
> +  It would be all-or-nothing, which would be expensive. This could be addressed
> +  by changes to the protocol, but this would be invasive.
> +- Requires custom auto-merging behavior on fetch.
> +
> +Tags
> +----
> +Put the content of the metacommit in a message attached to tag on the
> +replacement commit. This is very similar to the git notes approach and has the
> +same pros and cons.
> +
> +Simple forward references
> +-------------------------
> +Record an edge from an obsolete commit to its replacement in this form:
> +
> +refs/obsoletes/<A>
> +
> +pointing to commit <B> as an indication that B is the replacement for the
> +obsolete commit A.
> +
> +Pros:
> +- Protects <B> from being garbage collected.
> +- Fast lookup for the evolve operation, without additional search structures
> +  (“what is the replacement for <A>?” is very fast).
> +
> +Cons:
> +- Can’t represent divergence (which is a P0 requirement).
> +- Creates lots of refs (which can be inefficient)
> +- Doesn’t provide a way to fetch only refs for a specific change.
> +- The obslog command requires a search of all refs.
> +
> +Complex forward references
> +--------------------------
> +Record an edge from an obsolete commit to its replacement in this form:
> +
> +refs/obsoletes/<change_id>/obs<A>_<B>
> +
> +Pointing to commit <B> as an indication that B is the replacement for obsolete
> +commit A.
> +
> +Pros:
> +- Permits sharing and fetching refs for only a specific change.
> +- Supports divergence
> +- Protects <B> from being garbage collected.
> +
> +Cons:
> +- Creates lots of refs, which is inefficient.
> +- Doesn’t provide a good lookup structure for lookups in either direction.
> +
> +Backward references
> +-------------------
> +Record an edge from a replacement commit to the obsolete one in this form:
> +
> +refs/obsolescences/<B>
> +
> +Cons:
> +- Doesn’t provide a way to resolve divergence (which is a P0 requirement).
> +- Doesn’t protect <B> from being garbage collected (which could be fixed by
> +  combining this with a refs/metas namespace, as in the metacommit variant).
> +
> +Obsolescences file
> +------------------
> +Create a custom file (or files) in .git recording obsolescences.
> +
> +Pros:
> +- Can store exactly the information we want with exactly the performance we want
> +  for all operations. For example, there could be a disk-based hashtable
> +  permitting constant time lookups in either direction.
> +
> +Cons:
> +- Handling GC, pushing, and pulling would all require custom solutions. GC
> +  issues could be addressed with a repository format extension.
> +
> +Squash points
> +-------------
> +We treat changes like topic branches, and use special squash points to mark
> +places in the commit graph that separate changes.
> +
> +We create and update change branches in refs/metas at the same time we
> +would have in the metacommit proposal. However, rather than pointing to a
> +metacommit branch they point to normal commits and are treated as “squash
> +points” - markers for sequences of commits intended to be squashed together on
> +submission.
> +
> +Amends and rebases work differently than they do now. Rather than actually
> +containing the desired state of a commit, they contain a delta from the previous
> +version along with a squash point indicating that the preceding changes are
> +intended to be squashed on submission. Specifically, amends would become new
> +changes and rebases would become merge commits with the old commit and new
> +parent as parents.
> +
> +When the changes are finally submitted, the squashes are executed, producing the
> +final version of the commit.
> +
> +In addition to the squash points, git would maintain a set of “nosquash” tags
> +for commits that were used as ancestors of a change that are not meant to be
> +included in the squash.
> +
> +For example, if we have this commit graph:
> +
> +A(...)
> +B(parent=A)
> +C(parent=B)
> +
> +...and we amend B to produce D, we’d get:
> +
> +A(...)
> +B(parent=A)
> +C(parent=B)
> +D(parent=B)
> +
> +...along with a new change branch indicating D should be squashed with its
> +parents when submitted:
> +
> +metas/changeB = D
> +metas/changeC = C
> +
> +We’d also create a nosquash tag for A indicating that A shouldn’t be included
> +when changeB is squashed.
> +
> +If a user amends the change again, they’d get:
> +
> +A(...)
> +B(parent=A)
> +C(parent=B)
> +D(parent=B)
> +E(parent=D)
> +
> +metas/changeB = E
> +metas/changeC = C
> +
> +Pros:
> +- Good GC behavior.
> +- Provides a natural way to share changes (they’re just normal branches).
> +- Merge-base works automatically without special cases.
> +- Rewriting the obslog would be easy using existing git commands.
> +- No new data types needed.
> +Cons:
> +- No way to connect the squashed version of a change to the original, so no way
> +  to automatically clean up old changes. This also means users lose all benefits
> +  of the evolve command if they prematurely squash their commits. This may occur
> +  if a user thinks a change is ready for submission, squashes it, and then later
> +  discovers an additional change to make.
> +- Histories would look very cluttered (users would see all previous edits to
> +  their commit in the commit log, and all previous rebases would show up as
> +  merges). Could be quite hard for users to tell what is going on. (Possible
> +  fix: also implement a new smart log feature that displays the log as though
> +  the squashes had occurred).
> +- Need to change the current behavior of current commands (like amend and
> +  rebase) in ways that will be unexpected to many users.
> --
> gitgitgadget
>

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 00/10] Add the Git Change command
  2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
                   ` (9 preceding siblings ...)
  2022-09-23 18:55 ` [PATCH 10/10] evolve: add documentation for `git change` Chris Poucet via GitGitGadget
@ 2022-09-25  8:39 ` Phillip Wood
  2022-10-04  9:33   ` Chris P
  2022-10-04 14:24 ` Phillip Wood
  2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
  12 siblings, 1 reply; 66+ messages in thread
From: Phillip Wood @ 2022-09-25  8:39 UTC (permalink / raw)
  To: Christophe Poucet via GitGitGadget, git; +Cc: Christophe Poucet

Hi Christophe

On 23/09/2022 19:55, Christophe Poucet via GitGitGadget wrote:
> I'm reviving the original git evolve work that was started by
> sxenos@google.com
> (https://public-inbox.org/git/20190215043105.163688-1-sxenos@google.com/)
> 
> This work is intended to make it easier to deal with stacked changes.
> 
> The following set of patches introduces the design doc on the evolve command
> as well as the basics of the git change command.

Thanks for picking this up, having an evolve command would be a really 
useful addition to git. I read the final four patches as I was 
interested to see how a user would use "git change" to track changes to 
a set of commits. Unfortunately because there are no tests and scant 
documentation there are no examples of how to do this. Looking at the 
patches I felt like it would have been helpful to mark them as RFC to 
indicate that the author is requesting feedback but does not consider 
them ready for merging.

I'm confused as to why the command is called "change" (which I don't 
find particularly descriptive) when every patch subject is "evolve". It 
definitely makes sense to request feedback on a large topic like this 
before everything is implemented but I'd be nervous of merging the early 
stages before there is a working evolve command. For an example of a 
successful multipart topic see 
https://lore.kernel.org/git/pull.1248.git.1654545325.gitgitgadget@gmail.com/ 
Knowing the author of that series the commit messages should also give 
you a good idea of the level of detail expected.

Best Wishes

Phillip

> Chris Poucet (4):
>    sha1-array: implement oid_array_readonly_contains
>    ref-filter: add the metas namespace to ref-filter
>    evolve: add delete command
>    evolve: add documentation for `git change`
> 
> Stefan Xenos (6):
>    technical doc: add a design doc for the evolve command
>    evolve: add support for parsing metacommits
>    evolve: add the change-table structure
>    evolve: add support for writing metacommits
>    evolve: implement the git change command
>    evolve: add the git change list command
> 
>   .gitignore                         |    1 +
>   Documentation/git-change.txt       |   55 ++
>   Documentation/technical/evolve.txt | 1051 ++++++++++++++++++++++++++++
>   Makefile                           |    4 +
>   builtin.h                          |    1 +
>   builtin/change.c                   |  342 +++++++++
>   change-table.c                     |  179 +++++
>   change-table.h                     |  132 ++++
>   git.c                              |    1 +
>   metacommit-parser.c                |  110 +++
>   metacommit-parser.h                |   19 +
>   metacommit.c                       |  404 +++++++++++
>   metacommit.h                       |   58 ++
>   oid-array.c                        |   12 +
>   oid-array.h                        |    7 +
>   ref-filter.c                       |   10 +-
>   ref-filter.h                       |    8 +-
>   t/helper/test-oid-array.c          |    6 +
>   t/t0064-oid-array.sh               |   22 +
>   19 files changed, 2418 insertions(+), 4 deletions(-)
>   create mode 100644 Documentation/git-change.txt
>   create mode 100644 Documentation/technical/evolve.txt
>   create mode 100644 builtin/change.c
>   create mode 100644 change-table.c
>   create mode 100644 change-table.h
>   create mode 100644 metacommit-parser.c
>   create mode 100644 metacommit-parser.h
>   create mode 100644 metacommit.c
>   create mode 100644 metacommit.h
> 
> 
> base-commit: 4b79ee4b0cd1130ba8907029cdc5f6a1632aca26
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1356%2Fpoucet%2Fevolve-v1
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1356/poucet/evolve-v1
> Pull-Request: https://github.com/gitgitgadget/git/pull/1356

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 10/10] evolve: add documentation for `git change`
  2022-09-23 18:55 ` [PATCH 10/10] evolve: add documentation for `git change` Chris Poucet via GitGitGadget
@ 2022-09-25  8:41   ` Phillip Wood
  0 siblings, 0 replies; 66+ messages in thread
From: Phillip Wood @ 2022-09-25  8:41 UTC (permalink / raw)
  To: Chris Poucet via GitGitGadget, git; +Cc: Christophe Poucet, Chris Poucet

Hi Chris

On 23/09/2022 19:55, Chris Poucet via GitGitGadget wrote:
> From: Chris Poucet <poucet@google.com>
> 
> Signed-off-by: Chris Poucet <poucet@google.com>
> ---
>   Documentation/git-change.txt | 55 ++++++++++++++++++++++++++++++++++++
>   1 file changed, 55 insertions(+)
>   create mode 100644 Documentation/git-change.txt
> 
> diff --git a/Documentation/git-change.txt b/Documentation/git-change.txt
> new file mode 100644
> index 00000000000..ea9a8e619b9
> --- /dev/null
> +++ b/Documentation/git-change.txt
> @@ -0,0 +1,55 @@
> +git-change(1)
> +=============
> +
> +NAME
> +----
> +git-change - Create, list, update or delete changes
> +
> +SYNOPSIS
> +--------
> +[verse]
> +'git change' list [<pattern>...]
> +'git change' update [-g <change-name> | -n] [--force] [--replace <treeish>...] [--origin <treeish>...] [--content <newtreeish>]
> +'git change' delete <change-name>...
> +
> +DESCRIPTION
> +-----------
> +
> +`git change list`: lists all existing <change-name>s.
> +
> +`git change delete`: deletes the given <change-name>s.
> +
> +`git change update`: creates or updates a <change-name>.
> +
> +If no arguments are given to `update` then a change is added to the
> +`refs/metas/` directory, unless a change already exists for the given commit.
> +
> +A <change-name> starts with `metas/` and represents the current change that is
> +being worked on.

It would be really useful for users if this documentation included an 
introduction to the concepts behind the command and examples of how they 
should use it to track changes to a patch series.

Best Wishes

Phillip

> +OPTIONS
> +-------
> +-c::
> +--content::
> +	Identifies the content commit for the change
> +
> +-o::
> +--origin::
> +	Marks the given commit as being the origin of this commit.
> +
> +-r::
> +--replace::
> +	Marks the given commit as being obsoleted by the new commit.
> +
> +-g::
> +	<change-name> to update
> +
> +-n::
> +	Indicates that the change is new and an existing change should not be updated.
> +
> +--force::
> +	Overwite an existing change of the same name.
> +
> +GIT
> +---
> +Part of the linkgit:git[1] suite

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 07/10] evolve: implement the git change command
  2022-09-23 18:55 ` [PATCH 07/10] evolve: implement the git change command Stefan Xenos via GitGitGadget
@ 2022-09-25  9:10   ` Phillip Wood
  2022-09-26  8:23     ` Ævar Arnfjörð Bjarmason
  2022-09-26  8:25   ` Ævar Arnfjörð Bjarmason
  1 sibling, 1 reply; 66+ messages in thread
From: Phillip Wood @ 2022-09-25  9:10 UTC (permalink / raw)
  To: Stefan Xenos via GitGitGadget, git; +Cc: Christophe Poucet, Stefan Xenos

Hi Chris

On 23/09/2022 19:55, Stefan Xenos via GitGitGadget wrote:
> From: Stefan Xenos <sxenos@google.com>
> 
> Implement the git change update command, which
> are sufficient for constructing change graphs.
> 
> For example, to create a new change (a stable name) that refers to HEAD:
> 
> git change update -c HEAD
> 
> To record a rebase or amend in the change graph:
> 
> git change update -c <new_commit> -r <old_commit>
> 
> To record a cherry-pick in the change graph:
> 
> git change update -c <new_commit> -o <original_commit>

While it is good to have this example it would be better to have some 
documentation about how to use this command. It would be very helpful to 
have the documentation added before the code so that reviewers have an 
overview of the command when they come to review the code.

The commit message should also discuss why it is called "change" rather 
than "evolve". For more details on commit messages for this project see 
"Describe your changes well" in Documentation/SubmittingPatches. Having 
some tests would make it clear how this command is intended to be used 
as well as demonstrating that the implementation works.

> Signed-off-by: Stefan Xenos <sxenos@google.com>
> Signed-off-by: Chris Poucet <poucet@google.com>
> ---

> +struct update_state {
> +	int options;
> +	const char* change;
> +	const char* content;
> +	struct string_list replace;
> +	struct string_list origin;
> +};
> +
> +static void init_update_state(struct update_state *state)
> +{
> +	memset(state, 0, sizeof(*state));
> +	state->content = "HEAD";
> +	string_list_init_nodup(&state->replace);
> +	string_list_init_nodup(&state->origin);
> +}

In general we prefer to use initializer macros over functions. So this 
would become

#define UPDATE_STATE_INIT {			\
	.content = "HEAD",			\
	.replace = STRING_LIST_INIT_NODUP,	\
	.origin = STRING_LIST_INIT_NODUP	\
}

and lower down we'd have

struct update_state state = UPDATE_STATE_INIT;

Likewise we prefer

	struct foo = { 0 };

over

	struct foo foo;
	memset(&foo, 0, sizeof(foo));

> +int cmd_change(int argc, const char **argv, const char *prefix)
> +{
> +	/* No options permitted before subcommand currently */
> +	struct option options[] = {
> +		OPT_END()
> +	};
> +	int result = 1;
> +
> +	argc = parse_options(argc, argv, prefix, options, builtin_change_usage,
> +		PARSE_OPT_STOP_AT_NON_OPTION);
> +
> +	if (argc < 1)
> +		usage_with_options(builtin_change_usage, options);
> +	else if (!strcmp(argv[0], "update"))
> +		result = change_update(argc, argv, prefix);

Since Stefan wrote this code the parse options api has been improved to 
support sub commands so this should be updated to use that support.


Thanks again for picking up these patches, I'm excited to see an evolve 
command for git.

Best Wishes

Phillip

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 07/10] evolve: implement the git change command
  2022-09-25  9:10   ` Phillip Wood
@ 2022-09-26  8:23     ` Ævar Arnfjörð Bjarmason
  0 siblings, 0 replies; 66+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-09-26  8:23 UTC (permalink / raw)
  To: Phillip Wood
  Cc: Stefan Xenos via GitGitGadget, git, Christophe Poucet,
	Stefan Xenos


On Sun, Sep 25 2022, Phillip Wood wrote:

> Hi Chris
>
> On 23/09/2022 19:55, Stefan Xenos via GitGitGadget wrote:
>> From: Stefan Xenos <sxenos@google.com>
>> +static void init_update_state(struct update_state *state)
>> +{
>> +	memset(state, 0, sizeof(*state));
>> +	state->content = "HEAD";
>> +	string_list_init_nodup(&state->replace);
>> +	string_list_init_nodup(&state->origin);
>> +}
>
> In general we prefer to use initializer macros over functions. So this
> would become
>
> #define UPDATE_STATE_INIT {			\
> 	.content = "HEAD",			\
> 	.replace = STRING_LIST_INIT_NODUP,	\
> 	.origin = STRING_LIST_INIT_NODUP	\
> }

*nod*, although our usual style is not to indent the "\"'s like that. But just:

	#define FOO { \
		.bar = "baz", \
		[...]

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 07/10] evolve: implement the git change command
  2022-09-23 18:55 ` [PATCH 07/10] evolve: implement the git change command Stefan Xenos via GitGitGadget
  2022-09-25  9:10   ` Phillip Wood
@ 2022-09-26  8:25   ` Ævar Arnfjörð Bjarmason
  2022-10-05 12:30     ` Chris P
  1 sibling, 1 reply; 66+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-09-26  8:25 UTC (permalink / raw)
  To: Stefan Xenos via GitGitGadget; +Cc: git, Christophe Poucet, Stefan Xenos


On Fri, Sep 23 2022, Stefan Xenos via GitGitGadget wrote:

> From: Stefan Xenos <sxenos@google.com>

> +static const char * const builtin_change_usage[] = {
> +	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
> +	NULL
> +};
> +
> +static const char * const builtin_update_usage[] = {
> +	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
> +	NULL
> +};

This (and the corresponding later *.txt version) should indent the
overly long -h line, probably after "[--replace <treeish>...]".

> +struct update_state {
> +	int options;

I think this should be an enum in your earlier 06/10. Makes things more

> +		die(_("Failed to resolve '%s' as a valid revision."), committish);

This and other error should start with a lower-case letter, see
CodingGuidelines on errors.

> [...]
> +		die(_("Could not parse object '%s'."), committish);

Ditto etc.

> +	int i;
> +	for (i = 0; i < commitsish_list->nr; i++) {

A string_list uses a size_t for a nr, not int, so lets make that "size_t
i".

This both makes things more obvious, and helps some compilers spot
unsigned v.s. signed issues.


> +	int i;

ditto size_t above...

> +	for (i = 0; i < changes.nr; i++) {

...for this iteration...

> +		struct string_list_item *it = &changes.items[i];

...but actually don't you just want for_each_string_list_item() instead?

> +		if (it->util)
> +			fprintf(stdout, N_("Updated change %s\n"), name);
> +		else
> +			fprintf(stdout, N_("Created change %s\n"), name);

The use of N_() here is wrong, you should use _(), N_() just marks
things for translation, but doesn't use it.

We also tend to try to avoid adding \n in translations needlessly. And
since you're printing to stdout this can be:


	if (...)
		printf(_("Updated change %s"), name);
	...
	putchar('\n')      



> +	}
> +
> +	string_list_clear(&changes, 0);
> +	change_table_clear(&chtable);
> +	clear_metacommit_data(&metacommit);
> +
> +	return ret;
> +}
> +
> +static int change_update(int argc, const char **argv, const char* prefix)
> +{
> +	int result;
> +	int force = 0;
> +	int newchange = 0;
> +	struct strbuf err = STRBUF_INIT;
> +	struct update_state state;
> +	struct option options[] = {
> +		{ OPTION_CALLBACK, 'r', "replace", &state, N_("commit"),
> +			N_("marks the given commit as being obsolete"),
> +			0, update_option_parse_replace },
> +		{ OPTION_CALLBACK, 'o', "origin", &state, N_("commit"),
> +			N_("marks the given commit as being the origin of this commit"),
> +			0, update_option_parse_origin },
> +		OPT_BOOL('F', "force", &force,
> +			N_("overwrite an existing change of the same name")),
> +		OPT_STRING('c', "content", &state.content, N_("commit"),
> +				 N_("identifies the new content commit for the change")),
> +		OPT_STRING('g', "change", &state.change, N_("commit"),
> +				 N_("name of the change to update")),
> +		OPT_BOOL('n', "new", &newchange,
> +			N_("create a new change - do not append to any existing change")),
> +		OPT_END()
> +	};
> +
> +	init_update_state(&state);
> +
> +	argc = parse_options(argc, argv, prefix, options, builtin_update_usage, 0);
> +
> +	if (force) state.options |= UPDATE_OPTION_FORCE;
> +	if (newchange) state.options |= UPDATE_OPTION_NOAPPEND;

Just use OPT_SET_INT_F() and skip the indirection thorugh OPT_BOOL(),
that macro itself is a thin wrapper for OPT_SET_INT_F().

I.e. you can drop these "force" and "newchange" variables, andjust set
your state.options directly.

> +int cmd_change(int argc, const char **argv, const char *prefix)
> +{
> +	/* No options permitted before subcommand currently */
> +	struct option options[] = {
> +		OPT_END()
> +	};
> +	int result = 1;
> +
> +	argc = parse_options(argc, argv, prefix, options, builtin_change_usage,
> +		PARSE_OPT_STOP_AT_NON_OPTION);
> +
> +	if (argc < 1)
> +		usage_with_options(builtin_change_usage, options);
> +	else if (!strcmp(argv[0], "update"))
> +		result = change_update(argc, argv, prefix);
> +	else {
> +		error(_("Unknown subcommand: %s"), argv[0]);
> +		usage_with_options(builtin_change_usage, options);
> +	}

This was presumably written before the recent OPT_SUBCOMMAND(), and
should instead use that API.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 09/10] evolve: add delete command
  2022-09-23 18:55 ` [PATCH 09/10] evolve: add delete command Chris Poucet via GitGitGadget
@ 2022-09-26  8:38   ` Ævar Arnfjörð Bjarmason
  2022-09-26  9:10     ` Chris Poucet
  0 siblings, 1 reply; 66+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-09-26  8:38 UTC (permalink / raw)
  To: Chris Poucet via GitGitGadget; +Cc: git, Christophe Poucet, Chris Poucet


On Fri, Sep 23 2022, Chris Poucet via GitGitGadget wrote:

> From: Chris Poucet <poucet@google.com>
>  static const char * const builtin_change_usage[] = {
>  	N_("git change list [<pattern>...]"),
> -	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
> +	N_("git change update [--force] [--replace <treeish>...] [--origin <treeish>...] [--content <newtreeish>]"),

Here you're just correcting a typo in an earlier commit, squash it into that one instead.

>  static const char * const builtin_update_usage[] = {
> -	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
> +	N_("git change update [--force] [--replace <treeish>...] [--origin <treeish>...] [--content <newtreeish>]"),

Ditto.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 09/10] evolve: add delete command
  2022-09-26  8:38   ` Ævar Arnfjörð Bjarmason
@ 2022-09-26  9:10     ` Chris Poucet
  0 siblings, 0 replies; 66+ messages in thread
From: Chris Poucet @ 2022-09-26  9:10 UTC (permalink / raw)
  Cc: git

On Mon, Sep 26, 2022 at 10:38 AM Ævar Arnfjörð Bjarmason
<avarab@gmail.com> wrote:
>
>
> On Fri, Sep 23 2022, Chris Poucet via GitGitGadget wrote:
>
> > From: Chris Poucet <poucet@google.com>
> >  static const char * const builtin_change_usage[] = {
> >       N_("git change list [<pattern>...]"),
> > -     N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
> > +     N_("git change update [--force] [--replace <treeish>...] [--origin <treeish>...] [--content <newtreeish>]"),
>
> Here you're just correcting a typo in an earlier commit, squash it into that one instead.

Done, thank you.
>
> >  static const char * const builtin_update_usage[] = {
> > -     N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
> > +     N_("git change update [--force] [--replace <treeish>...] [--origin <treeish>...] [--content <newtreeish>]"),
>
> Ditto.

Done, thank you.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 02/10] sha1-array: implement oid_array_readonly_contains
  2022-09-23 18:55 ` [PATCH 02/10] sha1-array: implement oid_array_readonly_contains Chris Poucet via GitGitGadget
@ 2022-09-26 13:08   ` Phillip Wood
  0 siblings, 0 replies; 66+ messages in thread
From: Phillip Wood @ 2022-09-26 13:08 UTC (permalink / raw)
  To: Chris Poucet via GitGitGadget, git; +Cc: Christophe Poucet, Chris Poucet

Hi Chris

On 23/09/2022 19:55, Chris Poucet via GitGitGadget wrote:
> From: Chris Poucet <poucet@google.com>
> 
> Implement a "readonly_contains" function for oid_array that won't
> sort the array if it is unsorted. This can be used to test containment in
> the rare situations where the array order matters.
> 
> The function has intentionally been given a name that is more cumbersome
> than the "lookup" function, which is what most callers will will want
> in most situations.

It certainly is more cumbersome. I also find it completely impenetrable, 
I wonder if lookup_unsorted or lookup_no_sort strike better balance 
between being cumbersome and descriptive.

> Signed-off-by: Chris Poucet <poucet@google.com>
> ---
>   oid-array.c               | 12 ++++++++++++
>   oid-array.h               |  7 +++++++
>   t/helper/test-oid-array.c |  6 ++++++
>   t/t0064-oid-array.sh      | 22 ++++++++++++++++++++++
>   4 files changed, 47 insertions(+)
> 
> diff --git a/oid-array.c b/oid-array.c
> index 73ba76e9e9a..1e12651d245 100644
> --- a/oid-array.c
> +++ b/oid-array.c
> @@ -28,6 +28,18 @@ static const struct object_id *oid_access(size_t index, const void *table)
>   	return &array[index];
>   }
>   
> +int oid_array_readonly_contains(const struct oid_array *array,
> +				const struct object_id* oid) {
> +	int i;

array->nr is size_t so i should be as well.

Best Wishes

Phillip


^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 03/10] ref-filter: add the metas namespace to ref-filter
  2022-09-23 18:55 ` [PATCH 03/10] ref-filter: add the metas namespace to ref-filter Chris Poucet via GitGitGadget
@ 2022-09-26 13:13   ` Phillip Wood
  2022-10-04  9:50     ` Chris P
  0 siblings, 1 reply; 66+ messages in thread
From: Phillip Wood @ 2022-09-26 13:13 UTC (permalink / raw)
  To: Chris Poucet via GitGitGadget, git; +Cc: Christophe Poucet, Chris Poucet

Hi Chris

On 23/09/2022 19:55, Chris Poucet via GitGitGadget wrote:
> From: Chris Poucet <poucet@google.com>
> 
> The metas namespace will contain refs for changes in progress. Add
> support for searching this namespace.

I assume this is to save having to write "refs/metas/" when we want to 
search for meta commits?

> Signed-off-by: Chris Poucet <poucet@google.com>
> --- > diff --git a/ref-filter.h b/ref-filter.h
> index aa0eea4ecf5..064fbef8e50 100644
> --- a/ref-filter.h
> +++ b/ref-filter.h
> @@ -17,8 +17,10 @@
>   #define FILTER_REFS_BRANCHES       0x0004
>   #define FILTER_REFS_REMOTES        0x0008
>   #define FILTER_REFS_OTHERS         0x0010
> +#define FILTER_REFS_CHANGES        0x0040

It would be nice to keep FILTER_REFS_OTHERS at the end I think (we don't 
need to worry about abi compatibility), also what happened to 0x0020?

Best Wishes

Phillip

>   #define FILTER_REFS_ALL            (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
> -				    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
> +				    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS | \
> +				    FILTER_REFS_CHANGES)
>   #define FILTER_REFS_DETACHED_HEAD  0x0020
>   #define FILTER_REFS_KIND_MASK      (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
>   



^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 04/10] evolve: add support for parsing metacommits
  2022-09-23 18:55 ` [PATCH 04/10] evolve: add support for parsing metacommits Stefan Xenos via GitGitGadget
@ 2022-09-26 13:27   ` Phillip Wood
  2022-10-04 11:21     ` Chris P
  0 siblings, 1 reply; 66+ messages in thread
From: Phillip Wood @ 2022-09-26 13:27 UTC (permalink / raw)
  To: Stefan Xenos via GitGitGadget, git; +Cc: Christophe Poucet, Stefan Xenos

Hi Chris

On 23/09/2022 19:55, Stefan Xenos via GitGitGadget wrote:
> From: Stefan Xenos <sxenos@google.com>
> 
> This patch adds the get_metacommit_content method, which can classify
> commits as either metacommits or normal commits, determine whether they
> are abandoned, and extract the content commit's object id from the
> metacommit.
> 
> Signed-off-by: Stefan Xenos <sxenos@google.com>
> Signed-off-by: Chris Poucet <poucet@google.com>
> ---
>   Makefile            |   1 +
>   metacommit-parser.c | 110 ++++++++++++++++++++++++++++++++++++++++++++
>   metacommit-parser.h |  19 ++++++++
>   3 files changed, 130 insertions(+)
>   create mode 100644 metacommit-parser.c
>   create mode 100644 metacommit-parser.h
> 
> diff --git a/Makefile b/Makefile
> index cac3452edb9..b2bcc00c289 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -999,6 +999,7 @@ LIB_OBJS += merge-ort.o
>   LIB_OBJS += merge-ort-wrappers.o
>   LIB_OBJS += merge-recursive.o
>   LIB_OBJS += merge.o
> +LIB_OBJS += metacommit-parser.o

There seems to be a problem with the indent here

>   LIB_OBJS += midx.o
>   LIB_OBJS += name-hash.o
>   LIB_OBJS += negotiator/default.o

 > diff --git a/metacommit-parser.h b/metacommit-parser.h
 > new file mode 100644
 > index 00000000000..1c74bd6d699
 > --- /dev/null
 > +++ b/metacommit-parser.h
 > @@ -0,0 +1,19 @@
 > +#ifndef METACOMMIT_PARSER_H
 > +#define METACOMMIT_PARSER_H
 > +
 > +#include "commit.h"
 > +#include "hash.h"
 > +
 > +/* Indicates a normal commit (non-metacommit) */
 > +#define METACOMMIT_TYPE_NONE 0
 > +/* Indicates a metacommit with normal content (non-abandoned) */
 > +#define METACOMMIT_TYPE_NORMAL 1
 > +/* Indicates a metacommit with abandoned content */
 > +#define METACOMMIT_TYPE_ABANDONED 2

Is it possible to define these as an enum? It would make the signature 
of get_meta_commit_content() nicer.

 > +struct commit;

What's this for? We're including commit.h above.

 > +extern int get_metacommit_content(
 > +	struct commit *commit, struct object_id *content);

> diff --git a/metacommit-parser.c b/metacommit-parser.c
> new file mode 100644
> index 00000000000..70c1428bfc6
> --- /dev/null
> +++ b/metacommit-parser.c
> @@ -0,0 +1,110 @@
> +#include "cache.h"
> +#include "metacommit-parser.h"
> +#include "commit.h"
> +
> +/*
> + * Search the commit buffer for a line starting with the given key. Unlike
> + * find_commit_header, this also searches the commit message body.
> + */

There is no explanation in the code or commit message as to why this 
function is needed. The documentation added in the first commit says 
that "parent-type" header is a commit header. I think the answer is that 
this series does not implement that header but uses the commit message 
instead. That's perfectly fine for a proof of concept but it is 
precisely the sort of detail that should be described it the commit 
message and probably flagged up in the cover letter.

> +static const char *find_key(const char *msg, const char *key, size_t *out_len)
> +{
> +	int key_len = strlen(key);
> +	const char *line = msg;
> +
> +	while (line) {
> +		const char *eol = strchrnul(line, '\n');
> +
> +		if (eol - line > key_len && !memcmp(line, key, key_len) &&
> +		    line[key_len] == ' ') {
> +			*out_len = eol - line - key_len - 1;
> +			return line + key_len + 1;
> +		}
> +		line = *eol ? eol + 1 : NULL;
> +	}
> +	return NULL;
> +}
> +
> +static struct commit *get_commit_by_index(struct commit_list *to_search, int index)
> +{
> +	while (to_search && index) {
> +		to_search = to_search->next;
> +		index--;
> +	}
> +
> +	if (!to_search)
> +		return NULL;
> +
> +	return to_search->item;
> +}

This function is a useful utility for struct commit_list and should live 
in commit.c. It could be used to simplify object-name.c:get_parent() for 
example.

> +/*
> + * Writes the index of the content parent to "result". Returns the metacommit
> + * type. See the METACOMMIT_TYPE_* constants.
> + */
> +static int index_of_content_commit(const char *buffer, int *result)

I found the signature confusing as it is returning an int but that is 
not the index. Switching to an enum for the metacommit types would 
clarify that.

> +{
> +	int index = 0;
> +	int ret = METACOMMIT_TYPE_NONE;
> +	size_t parent_types_size;
> +	const char *parent_types = find_key(buffer, "parent-type",
> +		&parent_types_size);
> +	const char *end;
> +	const char *enum_start = parent_types;
> +	int enum_length = 0;
> +
> +	if (!parent_types)
> +		return METACOMMIT_TYPE_NONE;
> +
> +	end = &parent_types[parent_types_size];
> +
> +	while (1) {
> +		char next = *parent_types;
> +		if (next == ' ' || parent_types >= end) {
> +			if (enum_length == 1) {

if enum_length != 1 then there is an error in the parent-type header and 
we should probably bail out.

> +				char first_char_in_enum = *enum_start;

It's not just the first character, it's the only character, do we really 
need such a long variable name? (how about just calling it "type")

I'll try and take at look at the next couple of patches later in the week.

Best Wishes

Phillip


^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 05/10] evolve: add the change-table structure
  2022-09-23 18:55 ` [PATCH 05/10] evolve: add the change-table structure Stefan Xenos via GitGitGadget
@ 2022-09-27 13:27   ` Phillip Wood
  2022-09-27 13:50     ` Ævar Arnfjörð Bjarmason
                       ` (2 more replies)
  0 siblings, 3 replies; 66+ messages in thread
From: Phillip Wood @ 2022-09-27 13:27 UTC (permalink / raw)
  To: Stefan Xenos via GitGitGadget, git; +Cc: Christophe Poucet, Stefan Xenos

Hi Chris

On 23/09/2022 19:55, Stefan Xenos via GitGitGadget wrote:
> From: Stefan Xenos <sxenos@google.com>
> 
> A change table stores a list of changes, and supports efficient lookup
> from a commit hash to the list of changes that reference that commit
> directly.
> 
> It can be used to look up content commits or metacommits at the head
> of a change, but does not support lookup of commits referenced as part
> of the commit history.
> 
> Signed-off-by: Stefan Xenos <sxenos@google.com>
> Signed-off-by: Chris Poucet <poucet@google.com>

 > diff --git a/change-table.h b/change-table.h
 > new file mode 100644
 > index 00000000000..166b5ed8073
 > --- /dev/null
 > +++ b/change-table.h
 > @@ -0,0 +1,132 @@
 > +#ifndef CHANGE_TABLE_H
 > +#define CHANGE_TABLE_H
 > +
 > +#include "oidmap.h"
 > +
 > +struct commit;
 > +struct ref_filter;
 > +
 > +/**

We tend to just use '/*' rather than '/**'

 > + * This struct holds a list of change refs. The first element is 
stored inline,
 > + * to optimize for small lists.
 > + */
 > +struct change_list {
 > +	/**
 > +	 * Ref name for the first change in the list, or null if none.
 > +	 *
 > +	 * This field is private. Use for_each_change_in to read.
 > +	 */
 > +	const char* first_refname;
 > +	/**
 > +	 * List of additional change refs. Note that this is empty if the list
 > +	 * contains 0 or 1 elements.
 > +	 *
 > +	 * This field is private. Use for_each_change_in to read.
 > +	 */
 > +	struct string_list additional_refnames;

Splitting this feels like a premature optimization. We don't have any 
tests yet, let alone any real-world experience using this code. Also if 
we want to save memory for lists with a single entry why are we 
embedding the struct string_list rather than just storing a pointer to it?

I think it would be simpler to use a struct strset to hold the refnames 
as we don't need the util field offered by struct string_list.

 > +/**
 > + * Holds information about the head of a single change.
 > + */
 > +struct change_head {
 > +	/**
 > +	 * The location pointed to by the head of the change. May be a 
commit or a
 > +	 * metacommit.
 > +	 */
 > +	struct object_id head;

I found this duality between commits and metacommits rather confusing - 
why isn't the head always a metacommit?

 > +/**
 > + * Holds information about the heads of each change, and permits 
effecient

s/effecient/efficient/

 > + * lookup from a commit to the changes that reference it directly.
 > + *
 > + * All fields should be considered private. Use the change_table 
functions
 > + * to interact with this struct.
 > + */
 > +struct change_table {
 > +	/**
 > +	 * Memory pool for the objects allocated by the change table.
 > +	 */
 > +	struct mem_pool memory_pool;
 > +	/* Map object_id to commit_change_list_entry structs. */
 > +	struct oidmap oid_to_metadata_index;
 > +	/**
 > +	 * List of ref names. The util value points to a change_head structure
 > +	 * allocated from memory_pool.
 > +	 */
 > +	struct string_list refname_to_change_head;

I think these days we'd use a strmap for this for O(1) lookups.

 > +};
 > +
 > +extern void change_table_init(struct change_table *to_initialize);

The struct change_table argument to all these functions changes its name 
more often than a criminal on the run. I would find it much easier to 
follow the code if we consistently called this argument "table"

 > + * Adds all changes matching the given ref filter to the given 
change_table
 > + * struct.
 > + */
 > +extern void change_table_add_matching_filter(struct change_table 
*to_modify,
 > +	struct repository* repo, struct ref_filter *filter);

I can't see any callers outside of change-table.c so do we really need 
to export this function.

> diff --git a/change-table.c b/change-table.c
> new file mode 100644
> index 00000000000..c61ba29f1ed
> --- /dev/null
> +++ b/change-table.c
> @@ -0,0 +1,179 @@
> +#include "cache.h"
> +#include "change-table.h"
> +#include "commit.h"
> +#include "ref-filter.h"
> +#include "metacommit-parser.h"
> +
> +void change_table_init(struct change_table *to_initialize)
> +{
> +	memset(to_initialize, 0, sizeof(*to_initialize));
> +	mem_pool_init(&to_initialize->memory_pool, 0);
> +	to_initialize->memory_pool.block_alloc = 4*1024 - sizeof(struct mp_block);

If we're using a mempool to minimize the allocation overhead we should 
leave .block_alloc set to the default value of 1MB rather than changing 
it to 4kB

> +	oidmap_init(&to_initialize->oid_to_metadata_index, 0);
> +	string_list_init_dup(&to_initialize->refname_to_change_head);
> +}
> +
> +static void change_list_clear(struct change_list *to_clear) {
> +	string_list_clear(&to_clear->additional_refnames, 0);
> +}
> +
> +static void commit_change_list_entry_clear(
> +	struct commit_change_list_entry *to_clear) {
> +	change_list_clear(&to_clear->changes);
> +}
> +
> +void change_table_clear(struct change_table *to_clear)
> +{
> +	struct oidmap_iter iter;
> +	struct commit_change_list_entry *next;
> +	for (next = oidmap_iter_first(&to_clear->oid_to_metadata_index, &iter);
> +		next;
> +		next = oidmap_iter_next(&iter)) {
> +
> +		commit_change_list_entry_clear(next);
> +	}
> +
> +	oidmap_free(&to_clear->oid_to_metadata_index, 0);
> +	string_list_clear(&to_clear->refname_to_change_head, 0);
> +	mem_pool_discard(&to_clear->memory_pool, 0);
> +}
> +
> +static void add_head_to_commit(struct change_table *to_modify,
> +	const struct object_id *to_add, const char *refname)

I found the function and argument names rather confusing. If I've 
understood the code correctly then this function is adding an assoation 
between the commit "to_add" and "refname". Despite its name "to_add" may 
already exist in the change table.

The formatting is a bit off as well (as are most of the function 
declarations in this patch and the next), we'd write that as

static void add_head_to_commit(struct change_table *table,
			       const struct object_id *to_add,
			       const char *refname)

> +{
> +	struct commit_change_list_entry *entry;
> +
> +	/**
> +	 * Note: the indices in the map are 1-based. 0 is used to indicate a missing
> +	 * element.
> +	 */

I'm confused by this comment, what indices is it talking about?

> +	entry = oidmap_get(&to_modify->oid_to_metadata_index, to_add);
> +	if (!entry) {
> +		entry = mem_pool_calloc(&to_modify->memory_pool, 1,
> +			sizeof(*entry));
> +		oidcpy(&entry->entry.oid, to_add);
> +		oidmap_put(&to_modify->oid_to_metadata_index, entry);
> +		string_list_init_nodup(&entry->changes.additional_refnames);
> +	}
> +
> +	if (!entry->changes.first_refname)
> +		entry->changes.first_refname = refname;
> +	else
> +		string_list_insert(&entry->changes.additional_refnames, refname);

This is an example of the complexity added by the current definition of 
struct change_list.

> +void change_table_add(struct change_table *to_modify, const char *refname,
> +	struct commit *to_add)
> +{
> +	struct change_head *new_head;
> +	struct string_list_item *new_item;
> +	int metacommit_type;
> +
> +	new_head = mem_pool_calloc(&to_modify->memory_pool, 1,
> +		sizeof(*new_head));
> +
> +	oidcpy(&new_head->head, &to_add->object.oid);
> +
> +	metacommit_type = get_metacommit_content(to_add, &new_head->content);
> +	if (metacommit_type == METACOMMIT_TYPE_NONE)
> +		oidcpy(&new_head->content, &to_add->object.oid);

If to_add is not a metacommit then the content is to_add itself, 
otherwise it will have been set by the call to get_metacommit_content().

> +	new_head->abandoned = (metacommit_type == METACOMMIT_TYPE_ABANDONED);

Style: I don't think we normally bother with parentheses here

> +	new_head->remote = starts_with(refname, "refs/remote/");
> +	new_head->hidden = starts_with(refname, "refs/hiddenmetas/");
> +
> +	new_item = string_list_insert(&to_modify->refname_to_change_head, refname);
> +	new_item->util = new_head;
> +	/* Use pointers to the copy of the string we're retaining locally */

string_list_insert() copied the string and we're using that copy. Saying 
we're retaining it locally when it will outlive this function call is 
confusing.

> +	refname = new_item->string;
> +
> +	if (!oideq(&new_head->content, &new_head->head))
> +		add_head_to_commit(to_modify, &new_head->content, refname);

If to_add is a metacommit then we remember the link between refname and 
the content commit.

> +	add_head_to_commit(to_modify, &new_head->head, refname);

We also remember the link between refname and to_add

> +}
> +
> +void change_table_add_all_visible(struct change_table *to_modify,
> +	struct repository* repo)
> +{
> +	struct ref_filter filter;

rather than using memset we'd write (the same goes for all the other 
memset() calls in this series, unless they're operation on a heap 
allocation)

	struct ref_filter filter = { 0 };

> +	const char *name_patterns[] = {NULL};
> +	memset(&filter, 0, sizeof(filter));
> +	filter.kind = FILTER_REFS_CHANGES;
> +	filter.name_patterns = name_patterns;
> +
> +	change_table_add_matching_filter(to_modify, repo, &filter);
> +}
> +
> +void change_table_add_matching_filter(struct change_table *to_modify,
> +	struct repository* repo, struct ref_filter *filter)
> +{
> +	struct ref_array matching_refs;
> +	int i;
> +
> +	memset(&matching_refs, 0, sizeof(matching_refs));
> +	filter_refs(&matching_refs, filter, filter->kind);
> +
> +	/**
> +	 * Determine the object id for the latest content commit for each change.
> +	 * Fetch the commit at the head of each change ref. If it's a normal commit,
> +	 * that's the commit we want. If it's a metacommit, locate its content parent
> +	 * and use that.
> +	 */
> +
> +	for (i = 0; i < matching_refs.nr; i++) {
> +		struct ref_array_item *item = matching_refs.items[i];
> +		struct commit *commit = item->commit;
> +
> +		commit = lookup_commit_reference_gently(repo, &item->objectname, 1);

We're assigning commit twice - why do we need to look it up if 
filter_refs returns it?

There are a number of places where we call 
lookup_commit_reference_gently(..., 1) to silence the warning if the 
objectname does not dereference to a commit. It is not clear to me that 
we want to hide those errors. Indeed I think we should be doing

		commit = lookup_commit_reference(repo, oid)
		if (!commit)
			BUG("commit missing ...")

unless there is a good reason that the lookup can fail.

> +		if (commit)
> +			change_table_add(to_modify, item->refname, commit);
> +	}
> +
> +	ref_array_clear(&matching_refs);
> +}

> +int for_each_change_referencing(struct change_table *table,
> +	const struct object_id *referenced_commit_id, each_change_fn fn, void *cb_data)
> +{
> +	const struct change_list *changes;
> +	int i;
> +	int retvalue;

We normally use "ret" for this

> +	struct commit_change_list_entry *entry;
> +
> +	entry = oidmap_get(&table->oid_to_metadata_index,
> +		referenced_commit_id);

This should be indented to start below the '(' of the function call.

> +	/* If this commit isn't referenced by any changes, it won't be in the map */
> +	if (!entry)
> +		return 0;
> +	changes = &entry->changes;
> +	if (!changes->first_refname)
> +		return 0;
> +	retvalue = fn(changes->first_refname, cb_data);
> +	for (i = 0; retvalue == 0 && i < changes->additional_refnames.nr; i++)
> +		retvalue = fn(changes->additional_refnames.items[i].string, cb_data);

Using an strset for struct change_list would simplify this

> +	return retvalue;
> +}
> +
> +struct change_head* get_change_head(struct change_table *heads,
> +	const char* refname)
> +{
> +	struct string_list_item *item = string_list_lookup(
> +		&heads->refname_to_change_head, refname);
> +
> +	if (!item)
> +		return NULL;
> +
> +	return (struct change_head *)item->util;

We don't bother with casting void* pointers like this. In any case this 
whole function could become

	return strmap_get(table, refname)

if we used an strmap instead of a string_list.


Aside from the style issues and using api's that have been added since 
Stefan wrote these patches this looks pretty sound. The only thing I 
don't really get why the public api allows normal commits to be added to 
the change table (I can see why we might want to add the content commit 
as well when we add a metacommit but that should be done internally)

Best Wishes

Phillip

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 05/10] evolve: add the change-table structure
  2022-09-27 13:27   ` Phillip Wood
@ 2022-09-27 13:50     ` Ævar Arnfjörð Bjarmason
  2022-09-27 14:13       ` Phillip Wood
  2022-09-27 14:18     ` Phillip Wood
  2022-10-04 14:48     ` Chris P
  2 siblings, 1 reply; 66+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-09-27 13:50 UTC (permalink / raw)
  To: phillip.wood
  Cc: Stefan Xenos via GitGitGadget, git, Christophe Poucet,
	Stefan Xenos


On Tue, Sep 27 2022, Phillip Wood wrote:

> On 23/09/2022 19:55, Stefan Xenos via GitGitGadget wrote:
>> From: Stefan Xenos <sxenos@google.com>
>> A change table stores a list of changes, and supports efficient
>> lookup
>> from a commit hash to the list of changes that reference that commit
>> directly.
>> It can be used to look up content commits or metacommits at the head
>> of a change, but does not support lookup of commits referenced as part
>> of the commit history.
>> Signed-off-by: Stefan Xenos <sxenos@google.com>
>> Signed-off-by: Chris Poucet <poucet@google.com>
>
>> diff --git a/change-table.h b/change-table.h
>> new file mode 100644
>> index 00000000000..166b5ed8073
>> --- /dev/null
>> +++ b/change-table.h
>> @@ -0,0 +1,132 @@
>> +#ifndef CHANGE_TABLE_H
>> +#define CHANGE_TABLE_H
>> +
>> +#include "oidmap.h"
>> +
>> +struct commit;
>> +struct ref_filter;
>> +
>> +/**
>
> We tend to just use '/*' rather than '/**'

No, we use both, and /** is correct here. It's an API-doc syntax, see
e.g. strbuf.h.

It's explicitly meant for this sort of thing, i.e. comments on public
structs in a header & the functions in a header (and struct members,
etc.).

>> + * This struct holds a list of change refs. The first element is
>   stored inline,
>> + * to optimize for small lists.
>> + */
>> +struct change_list {
>> +	/**
>> +	 * Ref name for the first change in the list, or null if none.
>> +	 *
>> +	 * This field is private. Use for_each_change_in to read.
>> +	 */
>> +	const char* first_refname;
>> +	/**

While we're on nits we tend to add an extra \n before the next API
comment...

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 05/10] evolve: add the change-table structure
  2022-09-27 13:50     ` Ævar Arnfjörð Bjarmason
@ 2022-09-27 14:13       ` Phillip Wood
  2022-09-27 15:28         ` Ævar Arnfjörð Bjarmason
  0 siblings, 1 reply; 66+ messages in thread
From: Phillip Wood @ 2022-09-27 14:13 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Stefan Xenos via GitGitGadget, git, Christophe Poucet



On 27/09/2022 14:50, Ævar Arnfjörð Bjarmason wrote:
> 
> On Tue, Sep 27 2022, Phillip Wood wrote:
> 
>> On 23/09/2022 19:55, Stefan Xenos via GitGitGadget wrote:
>>> From: Stefan Xenos <sxenos@google.com>
>>> A change table stores a list of changes, and supports efficient
>>> lookup
>>> from a commit hash to the list of changes that reference that commit
>>> directly.
>>> It can be used to look up content commits or metacommits at the head
>>> of a change, but does not support lookup of commits referenced as part
>>> of the commit history.
>>> Signed-off-by: Stefan Xenos <sxenos@google.com>
>>> Signed-off-by: Chris Poucet <poucet@google.com>
>>
>>> diff --git a/change-table.h b/change-table.h
>>> new file mode 100644
>>> index 00000000000..166b5ed8073
>>> --- /dev/null
>>> +++ b/change-table.h
>>> @@ -0,0 +1,132 @@
>>> +#ifndef CHANGE_TABLE_H
>>> +#define CHANGE_TABLE_H
>>> +
>>> +#include "oidmap.h"
>>> +
>>> +struct commit;
>>> +struct ref_filter;
>>> +
>>> +/**
>>
>> We tend to just use '/*' rather than '/**'
> 
> No, we use both, and /** is correct here. It's an API-doc syntax, see
> e.g. strbuf.h.
> 
> It's explicitly meant for this sort of thing, i.e. comments on public
> structs in a header & the functions in a header (and struct members,
> etc.).

We don't do that consistently, we don't mention them in CodingGuidelines 
and we don't use anything that processes API-doc comments. It would be a 
lot simpler and it would be consistent with our coding guidelines just 
to use the same style everywhere. That would avoid problems where this 
series uses API-doc comments for in-code comments in .c files and where 
single line comments in header files do not use the API-doc syntax.

Best Wishes

Phillip

>>> + * This struct holds a list of change refs. The first element is
>>    stored inline,
>>> + * to optimize for small lists.
>>> + */
>>> +struct change_list {
>>> +	/**
>>> +	 * Ref name for the first change in the list, or null if none.
>>> +	 *
>>> +	 * This field is private. Use for_each_change_in to read.
>>> +	 */
>>> +	const char* first_refname;
>>> +	/**
> 
> While we're on nits we tend to add an extra \n before the next API
> comment...

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 05/10] evolve: add the change-table structure
  2022-09-27 13:27   ` Phillip Wood
  2022-09-27 13:50     ` Ævar Arnfjörð Bjarmason
@ 2022-09-27 14:18     ` Phillip Wood
  2022-10-04 14:48     ` Chris P
  2 siblings, 0 replies; 66+ messages in thread
From: Phillip Wood @ 2022-09-27 14:18 UTC (permalink / raw)
  To: Stefan Xenos via GitGitGadget, git
  Cc: Christophe Poucet, Ævar Arnfjörð Bjarmason

On 27/09/2022 14:27, Phillip Wood wrote:
>> +    /**
>> +     * Determine the object id for the latest content commit for each 
>> change.
>> +     * Fetch the commit at the head of each change ref. If it's a 
>> normal commit,
>> +     * that's the commit we want. If it's a metacommit, locate its 
>> content parent
>> +     * and use that.
>> +     */
>> +
>> +    for (i = 0; i < matching_refs.nr; i++) {
>> +        struct ref_array_item *item = matching_refs.items[i];
>> +        struct commit *commit = item->commit;
>> +
>> +        commit = lookup_commit_reference_gently(repo, 
>> &item->objectname, 1);
> 
> We're assigning commit twice - why do we need to look it up if 
> filter_refs returns it?

I think we want to look it up to check that item->objectname is a 
commit. item->commit is not filled unless we specify the verbose flag 
and I'm not sure what the implications of setting that are. If we get an 
objectname that does not name a commit we should call BUG() as suggested 
below.

> There are a number of places where we call 
> lookup_commit_reference_gently(..., 1) to silence the warning if the 
> objectname does not dereference to a commit. It is not clear to me that 
> we want to hide those errors. Indeed I think we should be doing
> 
>          commit = lookup_commit_reference(repo, oid)
>          if (!commit)
>              BUG("commit missing ...")
> 
> unless there is a good reason that the lookup can fail.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 05/10] evolve: add the change-table structure
  2022-09-27 14:13       ` Phillip Wood
@ 2022-09-27 15:28         ` Ævar Arnfjörð Bjarmason
  2022-09-28 14:33           ` Phillip Wood
  0 siblings, 1 reply; 66+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-09-27 15:28 UTC (permalink / raw)
  To: phillip.wood; +Cc: Stefan Xenos via GitGitGadget, git, Christophe Poucet


On Tue, Sep 27 2022, Phillip Wood wrote:

> On 27/09/2022 14:50, Ævar Arnfjörð Bjarmason wrote:
>> On Tue, Sep 27 2022, Phillip Wood wrote:
>> 
>>> On 23/09/2022 19:55, Stefan Xenos via GitGitGadget wrote:
>>>> From: Stefan Xenos <sxenos@google.com>
>>>> A change table stores a list of changes, and supports efficient
>>>> lookup
>>>> from a commit hash to the list of changes that reference that commit
>>>> directly.
>>>> It can be used to look up content commits or metacommits at the head
>>>> of a change, but does not support lookup of commits referenced as part
>>>> of the commit history.
>>>> Signed-off-by: Stefan Xenos <sxenos@google.com>
>>>> Signed-off-by: Chris Poucet <poucet@google.com>
>>>
>>>> diff --git a/change-table.h b/change-table.h
>>>> new file mode 100644
>>>> index 00000000000..166b5ed8073
>>>> --- /dev/null
>>>> +++ b/change-table.h
>>>> @@ -0,0 +1,132 @@
>>>> +#ifndef CHANGE_TABLE_H
>>>> +#define CHANGE_TABLE_H
>>>> +
>>>> +#include "oidmap.h"
>>>> +
>>>> +struct commit;
>>>> +struct ref_filter;
>>>> +
>>>> +/**
>>>
>>> We tend to just use '/*' rather than '/**'
>> No, we use both, and /** is correct here. It's an API-doc syntax,
>> see
>> e.g. strbuf.h.
>> It's explicitly meant for this sort of thing, i.e. comments on
>> public
>> structs in a header & the functions in a header (and struct members,
>> etc.).
>
> We don't do that consistently, we don't mention them in
> CodingGuidelines and we don't use anything that processes API-doc
> comments. It would be a lot simpler and it would be consistent with
> our coding guidelines just to use the same style everywhere. That
> would avoid problems where this series uses API-doc comments for
> in-code comments in .c files and where single line comments in header
> files do not use the API-doc syntax.

Yes, this isn't documented in CodingGuidelines (but FWIW is in various
commit messages).

I'm pointing out that this isn't a mistake, but the preferred style for
new API docs.

At least Emacs knows how to highlight these differently, which is the
main use I personally get out of them, I don't know what other use-cases
there are for them...


^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 06/10] evolve: add support for writing metacommits
  2022-09-23 18:55 ` [PATCH 06/10] evolve: add support for writing metacommits Stefan Xenos via GitGitGadget
@ 2022-09-28 14:27   ` Phillip Wood
  2022-10-05  9:40     ` Chris P
  0 siblings, 1 reply; 66+ messages in thread
From: Phillip Wood @ 2022-09-28 14:27 UTC (permalink / raw)
  To: Stefan Xenos via GitGitGadget, git
  Cc: Christophe Poucet, Stefan Xenos,
	Ævar Arnfjörð Bjarmason

Hi Chris

On 23/09/2022 19:55, Stefan Xenos via GitGitGadget wrote:
> From: Stefan Xenos <sxenos@google.com>
> 
> metacommit.c supports the creation of metacommits and
> adds the API needed to create and update changes.
> 
> Create the "modify_change" function that can be called from modification
> commands like "rebase" and "git amend" to record obsolescences in the
> change graph.
> 
> Create the "record_metacommit" function for recording more complicated
> commit relationships in the commit graph.
> 
> Create the "write_metacommit" function for low-level creation of
> metacommits.

The commit message fails to mention that we do not create a 
"parent-type" header when we create a metacommit but instead abuse the 
commit message.

As with the other patches there are a lot of style comments, but to try 
and limit the noise I've only commented on one instance of each - you 
should apply the comments to all occurrences in all patches.

I've left a couple of questions where I'm not sure exactly what the code 
is trying to do but apart from an easily fixed NULL pointer de-reference 
and not actually creating the "parent-type" header it looks pretty good. 
I was glad to see that there are no obvious memory leaks. I do think the 
patches in this series would be easier to follow if the function 
parameter names were nouns rather than verbs.

> Signed-off-by: Stefan Xenos <sxenos@google.com>
> Signed-off-by: Chris Poucet <poucet@google.com>
> ---
>   Makefile     |   1 +
>   metacommit.c | 404 +++++++++++++++++++++++++++++++++++++++++++++++++++
>   metacommit.h |  58 ++++++++
>   3 files changed, 463 insertions(+)
>   create mode 100644 metacommit.c
>   create mode 100644 metacommit.h
> 
> diff --git a/Makefile b/Makefile
> index 2b847e7e7de..68082ef94c7 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1000,6 +1000,7 @@ LIB_OBJS += merge-ort.o
>   LIB_OBJS += merge-ort-wrappers.o
>   LIB_OBJS += merge-recursive.o
>   LIB_OBJS += merge.o
> +LIB_OBJS += metacommit.o
>   LIB_OBJS += metacommit-parser.o

I think the code to parse and create metacommits (as well as the change 
table code) could quite happily live in the same file.

> diff --git a/metacommit.c b/metacommit.c
> new file mode 100644
> index 00000000000..d2b859a4d3b
> --- /dev/null
> +++ b/metacommit.c
> @@ -0,0 +1,404 @@
> +#include "cache.h"
> +#include "metacommit.h"
> +#include "commit.h"
> +#include "change-table.h"
> +#include "refs.h"
> +
> +void init_metacommit_data(struct metacommit_data *state)
> +{
> +	memset(state, 0, sizeof(*state));
> +}
We'd normally use an initializer macro instead

	#define METACOMMIT_DATA_INIT = { 0 }

> +void clear_metacommit_data(struct metacommit_data *state)
> +{
> +	oid_array_clear(&state->replace);
> +	oid_array_clear(&state->origin);
> +}
> +
> +static void compute_default_change_name(struct commit *initial_commit,
> +	struct strbuf* result)
> +{
> +	struct strbuf default_name;

The canonical way to initialize an strbuf that is not on the heap is

	struct strbuf buf = STRBUF_INIT;

> +	const char *buffer;
> +	const char *subject;
> +	const char *eol;
> +	int len;
> +	strbuf_init(&default_name, 0);
> +	buffer = get_commit_buffer(initial_commit, NULL);
> +	find_commit_subject(buffer, &subject);
> +	eol = strchrnul(subject, '\n');
> +	for (len = 0;subject < eol && len < 10; ++subject, ++len) {

There's a space missing after the first ';'. We prefer post-increments 
to pre-increments unless the pre-increment is significant.

> +		char next = *subject;
> +		if (isspace(next))
> +			continue;
> +
> +		strbuf_addch(&default_name, next);
> +	}
> +	sanitize_refname_component(default_name.buf, result);

I suspect we need to call unuse_commit_buffer(initial_commit) here.

> +}
> +
> +/**
> + * Computes a change name for a change rooted at the given initial commit. Good
> + * change names should be memorable, unique, and easy to type. They are not
> + * required to match the commit comment.
> + */
> +static void compute_change_name(struct commit *initial_commit, struct strbuf* result)
> +{
> +	struct strbuf default_name;
> +	struct object_id unused;
> +
> +	strbuf_init(&default_name, 0);
> +	if (initial_commit)
> +		compute_default_change_name(initial_commit, &default_name);
> +	else
> +		strbuf_addstr(&default_name, "change");

What does it mean to call this function with initial_commit == NULL?

> +	strbuf_addstr(result, "refs/metas/");
> +	strbuf_addbuf(result, &default_name);
> +	/* If there is already a change of this name, append a suffix */
> +	if (!read_ref(result->buf, &unused)) {
> +		int suffix = 2;
> +		int original_length = result->len;

This is one of many places where we have a size_t len or nr member and 
assign it to an int. I think it would be clearer to use a size_t instead 
to avoid adding any more signed<->unsigned conversions.

> +
> +		while (1) {
> +			strbuf_addf(result, "%d", suffix);
> +			if (read_ref(result->buf, &unused))
> +				break;
> +			strbuf_remove(result, original_length, result->len - original_length);
> +			++suffix;
> +		}
> +	}
> +
> +	strbuf_release(&default_name);
> +}
> +
> +struct resolve_metacommit_callback_data

While there are some structs with a _callback_data suffix in the code 
base, it is far more common to use _context and name any corresponding 
variables ctx.

> +{
> +	struct change_table* active_changes;
> +	struct string_list *changes;
> +	struct oid_array *heads;
> +};
> +
> +static int resolve_metacommit_callback(const char *refname, void *cb_data)
> +{
> +	struct resolve_metacommit_callback_data *data = (struct resolve_metacommit_callback_data *)cb_data;

We don't use redundant casts such as this.

> +	struct change_head *chhead;
> +
> +	chhead = get_change_head(data->active_changes, refname);

This is really a comment on the previous patch but are there uses of 
for_each_change_referencing() for which just the refname is sufficient? 
It might be more convenient to pass the change head into the callback as 
well.

> +
> +	if (data->changes)
> +		string_list_append(data->changes, refname)->util = &(chhead->head);

We don't use redundant parentheses such as this (and this patch does not 
use them consistently)

> +	if (data->heads)
> +		oid_array_append(data->heads, &(chhead->head));
> +
> +	return 0;
> +}
> +
> +/**
> + * Produces the final form of a metacommit based on the current change refs.
> + */
> +static void resolve_metacommit(
> +	struct repository* repo,
> +	struct change_table* active_changes,
> +	const struct metacommit_data *to_resolve,

[testing my understanding] This is the metacommit we want to update

> +	struct metacommit_data *resolved_output,

This is the updated metacommit returned to the user

> +	struct string_list *to_advance,

Is also an output? It ends up as a list of refname to change head mappings

> +	int allow_append)
> +{
> +	int i;
> +	int len = to_resolve->replace.nr;
> +	struct resolve_metacommit_callback_data cbdata;

This would be a good place to a designated initializer.

	struct resolve_metacommit_context ctx = {
		.active_changes = active_changes,
		.changes = to_advance,
		.heads = &resolved_output->replace
	};

> +	int old_change_list_length = to_advance->nr;
> +	struct commit* content;
> +
> +	oidcpy(&resolved_output->content, &to_resolve->content);
> +
> +	/* First look for changes that point to any of the replacement edges in the
> +	 * metacommit. These will be the changes that get advanced by this
> +	 * metacommit. */

Style: '/*' & '*/' should be on their own lines.

> +	resolved_output->abandoned = to_resolve->abandoned;
> +	cbdata.active_changes = active_changes;
> +	cbdata.changes = to_advance;
> +	cbdata.heads = &(resolved_output->replace);
> +
> +	if (allow_append) {
> +		for (i = 0; i < len; i++) {
> +			int old_number = resolved_output->replace.nr;
> +			for_each_change_referencing(active_changes, &(to_resolve->replace.oid[i]),
> +				resolve_metacommit_callback, &cbdata);
> +			/* If no changes were found, use the unresolved value. */
> +			if (old_number == resolved_output->replace.nr)
> +				oid_array_append(&(resolved_output->replace), &(to_resolve->replace.oid[i]));

We see if there are any refs under refs/metas/ which point to 
'to_resolve' or its content and if there are we add those refs and the 
corresponding change head to 'to_advance'. If we don't find any refs 
then we copy the replace oid from 'to_resolve' to 'resolved_output'

If allow_append is false then we ignore all the replace oids in 'to_resolve'

> +		}
> +	}
> +
> +	cbdata.changes = NULL;
> +	cbdata.heads = &(resolved_output->origin);
> +
> +	len = to_resolve->origin.nr;
> +	for (i = 0; i < len; i++) {
> +		int old_number = resolved_output->origin.nr;
> +		for_each_change_referencing(active_changes, &(to_resolve->origin.oid[i]),
> +			resolve_metacommit_callback, &cbdata);
> +		if (old_number == resolved_output->origin.nr)
> +			oid_array_append(&(resolved_output->origin), &(to_resolve->origin.oid[i]));
> +	}

This is copying the origin oids in the same way as we copied the replace 
oids above.

> +	/* If no changes were advanced by this metacommit, we'll need to create a new
> +	 * one. */
> +	if (to_advance->nr == old_change_list_length) {
> +		struct strbuf change_name;
> +
> +		strbuf_init(&change_name, 80);
> +		content = lookup_commit_reference_gently(repo, &(to_resolve->content), 1);
> +
> +		compute_change_name(content, &change_name);
> +		string_list_append(to_advance, change_name.buf);
> +		strbuf_release(&change_name);
> +	}
> +}
> +
> +static void lookup_commits(
> +	struct repository *repo,
> +	struct oid_array *to_lookup,
> +	struct commit_list **result)
> +{
> +	int i = to_lookup->nr;
> +
> +	while (--i >= 0) {
> +		struct object_id *next = &(to_lookup->oid[i]);
> +		struct commit *commit = lookup_commit_reference_gently(repo, next, 1);
> +		commit_list_insert(commit, result);
> +	}

We walk backwards because commit_list_insert prepends to the list - good.

> +}
> +
> +#define PARENT_TYPE_PREFIX "parent-type "
> +
> +/**
> + * Creates a new metacommit object with the given content. Writes the object
> + * id of the newly-created commit to result.
> + */
> +int write_metacommit(struct repository *repo, struct metacommit_data *state,
> +	struct object_id *result)
> +{
> +	struct commit_list *parents = NULL;
> +	struct strbuf comment;
> +	int i;
> +	struct commit *content;
> +
> +	strbuf_init(&comment, strlen(PARENT_TYPE_PREFIX)
> +		+ 1 + 2 * (state->origin.nr + state->replace.nr));
> +	lookup_commits(repo, &state->origin, &parents);
> +	lookup_commits(repo, &state->replace, &parents);
> +	content = lookup_commit_reference_gently(repo, &state->content, 1);
> +	if (!content) {
> +		strbuf_release(&comment);
> +		free_commit_list(parents);
> +		return -1;
> +	}
> +	commit_list_insert(content, &parents);
> +
> +	strbuf_addstr(&comment, PARENT_TYPE_PREFIX);
> +	strbuf_addstr(&comment, state->abandoned ? "a" : "c");
> +	for (i = 0; i < state->replace.nr; i++)
> +		strbuf_addstr(&comment, " r");
> +
> +	for (i = 0; i < state->origin.nr; i++)
> +		strbuf_addstr(&comment, " o"); > +	/* The parents list will be freed by this call. */
> +	commit_tree(comment.buf, comment.len, repo->hash_algo->empty_tree, parents,
> +		result, NULL, NULL);

It would be relatively easy to use commit_tree_extended() with 
extra_headers so that we create a commit with a "parent-type" header 
rather than abusing the commit message.

	struct commit_extra_header extra = { .key = "parent-type" };
	
	/* build header value in strbuf */

	extra.value = buf.buf;
	extra.len = buf.len;
	commit_tree_extended("", 0, repo->hash_algo->empty_tree,
			     parents, result, NULL, NULL, NULL,
			     &extra);

> +
> +	strbuf_release(&comment);
> +	return 0;
> +}
> +
> +/**
> + * Returns true iff the given metacommit is abandoned, has one or more origin
> + * parents, or has one or more replacement parents.
> + */
> +static int is_nontrivial_metacommit(struct metacommit_data *state)
> +{
> +	return state->replace.nr || state->origin.nr || state->abandoned;
> +}
> +
> +/*
> + * Records the relationships described by the given metacommit in the
> + * repository.
> + *
> + * If override_change is NULL (the default), an attempt will be made
> + * to append to existing changes wherever possible instead of creating new ones.
> + * If override_change is non-null, only the given change ref will be updated.

So override_head is the refname of an existing change?

> + * options is a bitwise combination of the UPDATE_OPTION_* flags.
> + */
> +int record_metacommit(
> +	struct repository *repo,
> +	const struct metacommit_data *metacommit, const char *override_change,
> +	int options, struct strbuf *err)
> +{
> +		struct change_table chtable;
> +		struct string_list changes;
> +		int result;
> +
> +		change_table_init(&chtable);
> +		change_table_add_all_visible(&chtable, repo);
> +		string_list_init_dup(&changes);
> +
> +		result = record_metacommit_withresult(repo, &chtable, metacommit,
> +			override_change, options, err, &changes);
> +
> +		string_list_clear(&changes, 0);
> +		change_table_clear(&chtable);
> +		return result;
> +}
> +
> +/*
> + * Records the relationships described by the given metacommit in the
> + * repository.
> + *
> + * If override_change is NULL (the default), an attempt will be made
> + * to append to existing changes wherever possible instead of creating new ones.
> + * If override_change is non-null, only the given change ref will be updated.
> + *
> + * The changes list is filled in with the list of change refs that were updated,
> + * with the util pointers pointing to the old object IDS for those changes.
> + * The object ID pointers all point to objects owned by the change_table and
> + * will go out of scope when the change_table is destroyed.

That potentially sounds like an invitation to create use after free bugs 
unless we're careful. Does this function need to be public?

> + *
> + * options is a bitwise combination of the UPDATE_OPTION_* flags.
> + */
> +int record_metacommit_withresult(
> +	struct repository *repo,
> +	struct change_table *chtable,
> +	const struct metacommit_data *metacommit,
> +	const char *override_change,
> +	int options, struct strbuf *err,
> +	struct string_list *changes)
> +{
> +	static const char *msg = "updating change";
> +	struct metacommit_data resolved_metacommit;
> +	struct object_id commit_target;
> +	struct ref_transaction *transaction = NULL;
> +	struct change_head *overridden_head;
> +	const struct object_id *old_head;
> +
> +	int i;
> +	int ret = 0;
> +	int force = (options & UPDATE_OPTION_FORCE);
> +
> +	init_metacommit_data(&resolved_metacommit);
> +
> +	resolve_metacommit(repo, chtable, metacommit, &resolved_metacommit, changes,
> +		(options & UPDATE_OPTION_NOAPPEND) == 0);
> +
> +	if (override_change) {
> +		string_list_clear(changes, 0);
> +		overridden_head = get_change_head(chtable, override_change);
> +		if (!overridden_head) {

We enter this branch if overridden_head is NULL

> +			/* This is an existing change */
> +			old_head = &overridden_head->head;

Here we de-reference overridden_head which is NULL

> +			if (!force) {
> +				if (!oid_array_readonly_contains(&(resolved_metacommit.replace),
> +					&overridden_head->head)) {
> +					/* Attempted non-fast-forward change */
> +					strbuf_addf(err, _("non-fast-forward update to '%s'"),
> +						override_change);
> +					ret = -1;
> +					goto cleanup;
> +				}
> +			}
> +		} else

Style: if one branch of an if statement requires braces then all 
branches should have braces.

> +			/* ...then this is a newly-created change */
> +			old_head = null_oid();
> +
> +		/* The expected "current" head of the change is stored in the util
> +		 * pointer. */
> +		string_list_append(changes, override_change)->util = (void*)old_head;

No need to cast here

> +	}
> +
> +	if (is_nontrivial_metacommit(&resolved_metacommit)) {
> +		/* If there are any origin or replacement parents, create a new metacommit
> +		 * object. */
> +		if (write_metacommit(repo, &resolved_metacommit, &commit_target) < 0) {
> +			ret = -1;
> +			goto cleanup;
> +		}
> +	} else
> +		/**
> +		 * If the metacommit would only contain a content commit, point to the
> +		 * commit itself rather than creating a trivial metacommit.
> +		 */
> +		oidcpy(&commit_target, &(resolved_metacommit.content));

Oh, is this optimization why we don't insist on metacommits but also 
allow ordinary commits to be added to the change table?

> diff --git a/metacommit.h b/metacommit.h
> new file mode 100644
> index 00000000000..fdb253f0f04
> --- /dev/null
> +++ b/metacommit.h
> @@ -0,0 +1,58 @@
> +#ifndef METACOMMIT_H
> +#define METACOMMIT_H
> +
> +#include "hash.h"
> +#include "oid-array.h"
> +#include "repository.h"
> +#include "string-list.h"
> +
> +
> +struct change_table;
> +
> +/* If specified, non-fast-forward changes are permitted. */
> +#define UPDATE_OPTION_FORCE     0x0001
> +/**
> + * If specified, no attempt will be made to append to existing changes.
> + * Normally, if a metacommit points to a commit in its replace or origin
> + * list and an existing change points to that same commit as its content, the
> + * new metacommit will attempt to append to that same change. This may replace
> + * the commit parent with one or more metacommits from the head of the appended
> + * changes. This option disables this behavior, and will always create a new
> + * change rather than reusing existing changes.
> + */
> +#define UPDATE_OPTION_NOAPPEND  0x0002
> +
> +/* Metacommit Data */
> +
> +struct metacommit_data {
> +	struct object_id content;
> +	struct oid_array replace;
> +	struct oid_array origin;
> +	int abandoned;
> +};
> +
> +extern void init_metacommit_data(struct metacommit_data *state);
> +
> +extern void clear_metacommit_data(struct metacommit_data *state);
> +
> +extern int record_metacommit(struct repository *repo,
> +	const struct metacommit_data *metacommit,
> +	const char* override_change, int options, struct strbuf *err);
> +
> +extern int record_metacommit_withresult(
> +	struct repository *repo,
> +	struct change_table *chtable,
> +	const struct metacommit_data *metacommit,
> +	const char *override_change,
> +	int options,
> +	struct strbuf *err,
> +	struct string_list *changes);

Does this need to be public? i.e. why would one call this rather than 
record_metacommit()?

> +extern void modify_change(struct repository *repo,
> +	const struct object_id *old_commit, const struct object_id *new_commit,
> +	struct strbuf *err);
> +
> +extern int write_metacommit(struct repository *repo, struct metacommit_data *state,
> +	struct object_id *result);

The documentation for the flags is very welcome but this header could to 
with the api being documented as well.

Best Wishes

Phillip

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 05/10] evolve: add the change-table structure
  2022-09-27 15:28         ` Ævar Arnfjörð Bjarmason
@ 2022-09-28 14:33           ` Phillip Wood
  2022-09-28 15:14             ` Ævar Arnfjörð Bjarmason
  2022-09-28 15:59             ` Junio C Hamano
  0 siblings, 2 replies; 66+ messages in thread
From: Phillip Wood @ 2022-09-28 14:33 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Stefan Xenos via GitGitGadget, git, Christophe Poucet

On 27/09/2022 16:28, Ævar Arnfjörð Bjarmason wrote:
> 
> On Tue, Sep 27 2022, Phillip Wood wrote:
> 
>> On 27/09/2022 14:50, Ævar Arnfjörð Bjarmason wrote:
>>> On Tue, Sep 27 2022, Phillip Wood wrote:
>>>
>>>> On 23/09/2022 19:55, Stefan Xenos via GitGitGadget wrote:
>>>>> +/**
>>>>
>>>> We tend to just use '/*' rather than '/**'
>>> No, we use both, and /** is correct here. It's an API-doc syntax,
>>> see
>>> e.g. strbuf.h.
>>> It's explicitly meant for this sort of thing, i.e. comments on
>>> public
>>> structs in a header & the functions in a header (and struct members,
>>> etc.).
>>
>> We don't do that consistently, we don't mention them in
>> CodingGuidelines and we don't use anything that processes API-doc
>> comments. It would be a lot simpler and it would be consistent with
>> our coding guidelines just to use the same style everywhere. That
>> would avoid problems where this series uses API-doc comments for
>> in-code comments in .c files and where single line comments in header
>> files do not use the API-doc syntax.
> 
> Yes, this isn't documented in CodingGuidelines (but FWIW is in various
> commit messages).
> 
> I'm pointing out that this isn't a mistake, but the preferred style for
> new API docs.

It seems a bit a stretch to call it the preferred style, however I had 
thought all our uses were historic but that's not the case.

Chris if you want to use '/**' style comments for the API docs then 
please do so consistently and do not use them elsewhere.

> At least Emacs knows how to highlight these differently, which is the
> main use I personally get out of them, I don't know what other use-cases
> there are for them...

I've come across them in projects that use gtk-doc or other 
documentation generators where it is necessary to distinguish 
'documentation' from 'code comments'. I don't think they add much value 
if one is not generating documentation from the source, it is just one 
more thing for contributors to remember.

Best Wishes

Phillip



^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 05/10] evolve: add the change-table structure
  2022-09-28 14:33           ` Phillip Wood
@ 2022-09-28 15:14             ` Ævar Arnfjörð Bjarmason
  2022-09-28 15:59             ` Junio C Hamano
  1 sibling, 0 replies; 66+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2022-09-28 15:14 UTC (permalink / raw)
  To: phillip.wood; +Cc: Stefan Xenos via GitGitGadget, git, Christophe Poucet


On Wed, Sep 28 2022, Phillip Wood wrote:

> On 27/09/2022 16:28, Ævar Arnfjörð Bjarmason wrote:
>> On Tue, Sep 27 2022, Phillip Wood wrote:
>> 
>>> On 27/09/2022 14:50, Ævar Arnfjörð Bjarmason wrote:
>>>> On Tue, Sep 27 2022, Phillip Wood wrote:
>>>>
>>>>> On 23/09/2022 19:55, Stefan Xenos via GitGitGadget wrote:
>>>>>> +/**
>>>>>
>>>>> We tend to just use '/*' rather than '/**'
>>>> No, we use both, and /** is correct here. It's an API-doc syntax,
>>>> see
>>>> e.g. strbuf.h.
>>>> It's explicitly meant for this sort of thing, i.e. comments on
>>>> public
>>>> structs in a header & the functions in a header (and struct members,
>>>> etc.).
>>>
>>> We don't do that consistently, we don't mention them in
>>> CodingGuidelines and we don't use anything that processes API-doc
>>> comments. It would be a lot simpler and it would be consistent with
>>> our coding guidelines just to use the same style everywhere. That
>>> would avoid problems where this series uses API-doc comments for
>>> in-code comments in .c files and where single line comments in header
>>> files do not use the API-doc syntax.
>> Yes, this isn't documented in CodingGuidelines (but FWIW is in
>> various
>> commit messages).
>> I'm pointing out that this isn't a mistake, but the preferred style
>> for
>> new API docs.
>
> It seems a bit a stretch to call it the preferred style, however I had
> thought all our uses were historic but that's not the case.

FWIW that claim of mine comes from my (admittedly fuzzy recollection of)
history, around the time that this was being worked out (e.g. where API
docs should live, they used to mostly be in Documentation/technical/,
now they're mostly in *.h) we ended up with this mention in CodingGudielines:

 - When you come up with an API, document its functions and structures
   in the header file that exposes the API to its callers. Use what is
   in "strbuf.h" as a model for the appropriate tone and level of
   detail.

I.e. at some point we decreed strbuf.h as a best-practice model to
follow. I think every one of my own use of "/**" has come after reading
that file...

But patches to make it more explicit are most welcome. FWIW I think I
looked into that once, but couldn't find a canonical reference for what
this syntax is even called.

Hrm, looking a bit more I see it's probably JavaDoc (at least Emacs
highlights it as such). A grep of:

	git grep '^ \* @' -- '*.[ch]'

Shows that we make almost no use of the meta-syntax (i.e. "tags":
https://en.wikipedia.org/wiki/Javadoc#Table_of_Javadoc_tags)

> Chris if you want to use '/**' style comments for the API docs then
> please do so consistently and do not use them elsewhere.
>
>> At least Emacs knows how to highlight these differently, which is the
>> main use I personally get out of them, I don't know what other use-cases
>> there are for them...
>
> I've come across them in projects that use gtk-doc or other
> documentation generators where it is necessary to distinguish 
> 'documentation' from 'code comments'. I don't think they add much
> value if one is not generating documentation from the source, it is
> just one more thing for contributors to remember.

I for one find it very useful these "API docs" are shown differently in
my editor, even if I'm not using some fancier (and hypothetical) "make
javadoc" target.

I.e. you can ideally skim the *.h file and see at a glance what's an
implementation comment v.s. API doc comment.

Except that even for the supposed role model of strbuf.h we forgot in
some cases, and should have the below applied to it, oh well... :)

diff --git a/strbuf.h b/strbuf.h
index 76965a17d44..46549986ae3 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -492,7 +492,7 @@ int strbuf_getline_lf(struct strbuf *sb, FILE *fp);
 /* Uses NUL as the line terminator */
 int strbuf_getline_nul(struct strbuf *sb, FILE *fp);
 
-/*
+/**
  * Similar to strbuf_getline_lf(), but additionally treats a CR that
  * comes immediately before the LF as part of the terminator.
  * This is the most friendly version to be used to read "text" files
@@ -610,7 +610,7 @@ static inline struct strbuf **strbuf_split(const struct strbuf *sb,
 	return strbuf_split_max(sb, terminator, 0);
 }
 
-/*
+/**
  * Adds all strings of a string list to the strbuf, separated by the given
  * separator.  For example, if sep is
  *   ', '
@@ -653,7 +653,7 @@ int launch_editor(const char *path, struct strbuf *buffer,
 int launch_sequence_editor(const char *path, struct strbuf *buffer,
 			   const char *const *env);
 
-/*
+/**
  * In contrast to `launch_editor()`, this function writes out the contents
  * of the specified file first, then clears the `buffer`, then launches
  * the editor and reads back in the file contents into the `buffer`.
@@ -693,7 +693,7 @@ static inline void strbuf_complete_line(struct strbuf *sb)
 	strbuf_complete(sb, '\n');
 }
 
-/*
+/**
  * Copy "name" to "sb", expanding any special @-marks as handled by
  * interpret_branch_name(). The result is a non-qualified branch name
  * (so "foo" or "origin/master" instead of "refs/heads/foo" or
@@ -707,7 +707,7 @@ static inline void strbuf_complete_line(struct strbuf *sb)
 void strbuf_branchname(struct strbuf *sb, const char *name,
 		       unsigned allowed);
 
-/*
+/**
  * Like strbuf_branchname() above, but confirm that the result is
  * syntactically valid to be used as a local branch name in refs/heads/.
  *

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* Re: [PATCH 05/10] evolve: add the change-table structure
  2022-09-28 14:33           ` Phillip Wood
  2022-09-28 15:14             ` Ævar Arnfjörð Bjarmason
@ 2022-09-28 15:59             ` Junio C Hamano
  1 sibling, 0 replies; 66+ messages in thread
From: Junio C Hamano @ 2022-09-28 15:59 UTC (permalink / raw)
  To: Phillip Wood
  Cc: Ævar Arnfjörð Bjarmason,
	Stefan Xenos via GitGitGadget, git, Christophe Poucet

Phillip Wood <phillip.wood123@gmail.com> writes:

> Chris if you want to use '/**' style comments for the API docs then
> please do so consistently and do not use them elsewhere.

Thanks.

> I've come across them in projects that use gtk-doc or other
> documentation generators where it is necessary to distinguish
> 'documentation' from 'code comments'. I don't think they add much
> value if one is not generating documentation from the source, it is
> just one more thing for contributors to remember.

Yes, I personally find them annoying, but has tolerated them so far,
hoping that something good (read: automated documentation out of
comments) emerge someday, simply because the first ones were added
by folks who were interested in that direction, which unfortunately
has never materialized.

Maybe it would eventually happen, but I think there are a lot of
clean-up to do before it happens.  I somehow suspect that the sooner
the mechanism to create the documentation set, however incomplete
and messy the result is with the current material, the more incentive
the contributors have to apply /** vs /* distinction properly, but
of course on the other hand, until the existing material gets
cleaned up, the care they need to take to make the distinction does
feel like a makework.  So, I dunno.

Thanks.


^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 01/10] technical doc: add a design doc for the evolve command
  2022-09-23 18:55 ` [PATCH 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
  2022-09-23 19:59   ` Jerry Zhang
@ 2022-09-28 21:26   ` Junio C Hamano
  2022-09-28 22:20   ` Junio C Hamano
  2022-09-29 19:57   ` Jonathan Tan
  3 siblings, 0 replies; 66+ messages in thread
From: Junio C Hamano @ 2022-09-28 21:26 UTC (permalink / raw)
  To: Stefan Xenos via GitGitGadget; +Cc: git, Christophe Poucet

"Stefan Xenos via GitGitGadget" <gitgitgadget@gmail.com> writes:

> From: Stefan Xenos <sxenos@google.com>

[the above address should bounce, and has been removed from cc: list]

> +Background
> +==========
> +Imagine you have three sequential changes up for review and you receive feedback
> +that requires editing all three changes. We'll define the word "change"
> +formally later, but for the moment let's say that a change is a work-in-progress
> +whose final version will be submitted as a commit in the future.
> +
> +While you're editing one change, more feedback arrives on one of the others.
> +What do you do?
> +
> +The evolve command is a convenient way to work with chains of commits that are
> +under review. Whenever you rebase or amend a commit, the repository remembers
> +that the old commit is obsolete and has been replaced by the new one. Then, at
> +some point in the future, you can run "git evolve" and the correct sequence of
> +rebases will occur in the correct order such that no commit has an obsolete
> +parent.
> +
> +Part of making the "evolve" command work involves tracking the edits to a commit
> +over time, which is why we need an change graph. However, the change
> +graph will also bring other benefits:

It would be assuring to hear that "change graph" will also be
defined and explained formally later, just like "change" will in the
previous paragraph.  We will later see mention of "metacommits" and
"meta-commits" in this document, and I am guessing both of them are
quasi-synonyms to "change graph". If that is true, it is better to
stick to a single terminology.

> +Goals
> +-----
> +Legend: Goals marked with P0 are required. Goals marked with Pn should be
> +attempted unless they interfere with goals marked with Pn-1.
> +
> +P0. All commands that modify commits (such as the normal commit --amend or
> +    rebase command) should mark the old commit as being obsolete and replaced by
> +    the new one. No additional commands should be required to keep the
> +    change graph up-to-date.
> +P0. Any commit that may be involved in a future evolve command should not be
> +    garbage collected. Specifically:
> +    - Commits that obsolete another should not be garbage collected until
> +      user-specified conditions have occurred and the change has expired from
> +      the reflog. User specified conditions for removing changes include:
> +      - The user explicitly deleted the change.
> +      - The change was merged into a specific branch.
> +    - Commits that have been obsoleted by another should not be garbage
> +      collected if any of their replacements are still being retained.
> +P0. A commit can be obsoleted by more than one replacement (called divergence).
> +P0. Users must be able to resolve divergence (convergence).

P0: a single parent commit should keep only one parent. IOW, the
"change graph" implementation should not contaminate the end-result
commit in the regular part of the history, which is the product of
the final iteration of a "change"

IOW ...

> +P2. It should be possible to discard part or all of the change graph
> +    without discarding the commits themselves that are already present in
> +    branches and the reflog.

... this item should be P0.

> +Overview
> +========
> +We introduce the notion of “meta-commits” which describe how one commit was

Random appearance of smart quotes are annoying.  We'll be formatting
the doc via AsciiDoc, so let's stick to vanilla double or single quotes.

> +created from other commits. A branch of meta-commits is known as a change.
> +Changes are created and updated automatically whenever a user runs a command
> +that creates a commit. They are used for locating obsolete commits, providing a
> +list of a user’s unsubmitted work in progress, and providing a stable name for
> +each unsubmitted change.

Can "change graph" also be defined and explained here, too?  Or if
it is pretty much a synonym to "a branch of meta-commits", then
perhaps the document does not have to introduce the term "change
graph" and still stay understandable?

> +Detailed design
> +===============
> +Obsolescence information is stored as a graph of meta-commits. A meta-commit is
> +a specially-formatted merge commit that describes how one commit was created
> +from others.
> +
> +Meta-commits look like this:
> +
> +$ git cat-file -p <example_meta_commit>
> +tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
> +parent aa7ce55545bf2c14bef48db91af1a74e2347539a
> +parent d64309ee51d0af12723b6cb027fc9f195b15a5e9
> +parent 7e1bbcd3a0fa854a7a9eac9bf1eea6465de98136
> +author Stefan Xenos <sxenos@gmail.com> 1540841596 -0700
> +committer Stefan Xenos <sxenos@gmail.com> 1540841596 -0700
> +parent-type c r o
> +
> +This says “commit aa7ce555 makes commit d64309ee obsolete. It was created by
> +cherry-picking commit 7e1bbcd3”.
> +
> +The tree for meta-commits is always the empty tree, but future versions of git
> +may attach other trees here. For forward-compatibility fsck should ignore such
> +trees if found on future repository versions. This will allow future versions of
> +git to add metadata to the meta-commit tree without breaking forwards
> +compatibility.

Not clear why "trees" need to be ignored only by fsck but not others
like fetch/push, and I'd strongly advise against making such a
special case.  If they are missing, they are missing and should be
reported as corruptoin, and if you do not like it, do not add a
missing tree.

> +Parent-type
> +-----------
> +The “parent-type” field in the commit header identifies a commit as a
> +meta-commit and indicates the meaning for each of its parents. It is never
> +present for normal commits. It contains a space-deliminated list of enum values
> +whose order matches the order of the parents. Possible parent types are:

> +- c: (content) the content parent identifies the commit that this meta-commit is
> +  describing.
> +- r: (replaced) indicates that this parent is made obsolete by the content
> +  parent.
> +- o: (origin) indicates that the content parent was generated by cherry-picking
> +  this parent.
> +- a: (abandoned) used in place of a content parent for abandoned changes. Points
> +  to the final content commit for the change at the time it was abandoned.

Don't be cute with parent-type using single letters. You'll thank me
later when you need two types that share the first letter.

> +A meta-commit can have zero or more origin parents. A cherry-pick creates a
> +single origin parent. Certain types of squash merge will create multiple origin
> +parents. Origin parents don't directly cause their origin to become obsolete,
> +but are used when computing blame or locating a merge base. The section
> +on obsolescence over cherry-picks describes how the evolve command uses
> +origin parents.

Should it make a difference among doing these operations?

 - running "commit --amend" after "cherry-pick --no-commit" possibly with editing

 - running "commit --amend" after manually editing the same way, and

 - running "commit --amend" after "cherry-pick", possibly with editing?

It seems that the first two will not be captured while the last one
leaves 'origin'.  What should happen after running "commit --amend"
after "apply --index" possibly with editing?

What's the point of giving 'origin' only for "cherry-pick" and
squash merge?  I am wondering if we want to record contributions
sourced from an e-mailed patch from elsewhere (currently people use
external services like patchwork to do this)?

For the purpose of discussing "evolve", should "rebase" (with or
without "-i") be treated pretty much the same as a series of
"cherry-pick" mixed with "commit --amend" (possibly preceded with a
manual edit), followed by finally replacing the tip of the branch?
In the end result, the replaced commits after a "rebase" become
accessible only from reflog, but other than that, these two bulk
transplanting operations shouldn't be all that different.

> +The parent-type field needs to go after the committer field since git's rules
> +for forwards-compatibility require that new fields to be at the end of the
> +header. Putting a new field in the middle of the header would break fsck.

You can do without introducing a new header to avoid compatibility
issue by recording the information in the body of the commit object,
which would be even cleaner.

> +Change deletion
> +---------------
> +Changes are normally only interesting to a user while a commit is still in
> +development and under review. Once the commit has submitted wherever it is
> +going, its change can be discarded.
> +
> +The normal way of deleting changes makes this easy to do - changes are deleted
> +by the evolve command when it detects that the change is present in an upstream
> +branch. It does this in two ways: if the latest commit in a change either shows
> +up in the branch history or the change becomes empty after a rebase, it is
> +considered merged and the change is discarded. In this context, an “upstream
> +branch” is any branch passed in as the upstream argument of the evolve command.
> +
> +In case this sometimes deletes a useful change, such automatic deletions are
> +recorded in the reflog allowing them to be easily recovered.

Deleting a useful change is recorded in the reflog?  Isn't a change
recorded as a ref in metas/ hierarchy? Doesn't the removal of such a
ref remove its reflog as well?

I guess the above silly questions come from the fact that the
document does not make it clear reflog of what ref it is recorded.

> +Modify commands
> +---------------
> +Modification commands (commit --amend, rebase) will mark the old commit as
> +obsolete by creating a new meta-commit that references the old one as a
> +replaced parent. In the event that multiple changes point to the same commit,
> +this is done independently for every such change.
> +
> +More specifically, modifications work like this:
> +
> +1. Locate all existing changes for which the old commit is the content for the
> +   head of the change branch. If no such branch exists, create one that points
> +   to the old commit. Changes that include this commit in their history but not
> +   at their head are explicitly not included.
> +2. For every such change, create a new meta-commit that references the new
> +   commit as its content and references the old head of the change as a
> +   replaced parent.
> +3. Move the change branch forward to point to the new meta-commit.
> +
> +Copy commands
> +-------------
> +Copy commands (cherry-pick, merge --squash) create a new meta-commit that
> +references the old commits as origin parents. Besides the fact that the new
> +parents are tagged differently, copy commands work the same way as modify
> +commands.

It is unclear what benefit we will get by separating "Copy" commands
from "Modify" commands.  "checkout A && cherry-pick B" may make a
new copy of the edit the commit at the tip of branch B wanted to
make at the tip of branch A, but "commit --amend" is the same, in
that it makes a new copy of the edit the commit at the tip of the
current branch wanted to make, and the original copy is available in
both cases. It is just that the original of "cherry-pick B" is
slightly easier to access (i.e. it is still at the tip of branch B,
until the branch gains new commits on top of it) than the original
of "commit --amend" (i.e. the user needs to know that @{1} is the
previous state). Shouldn't all commands that create a new commit
object using some existing material (i.e. not from scratch) be
treated equally, without splitting them into two camps?

IOW, the above explains that the new parents are tagged differently,
but it does not explain why it is a good idea to do so.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 01/10] technical doc: add a design doc for the evolve command
  2022-09-23 18:55 ` [PATCH 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
  2022-09-23 19:59   ` Jerry Zhang
  2022-09-28 21:26   ` Junio C Hamano
@ 2022-09-28 22:20   ` Junio C Hamano
  2022-09-29  9:17     ` Phillip Wood
  2022-09-29 19:57   ` Jonathan Tan
  3 siblings, 1 reply; 66+ messages in thread
From: Junio C Hamano @ 2022-09-28 22:20 UTC (permalink / raw)
  To: Stefan Xenos via GitGitGadget; +Cc: git, Christophe Poucet

"Stefan Xenos via GitGitGadget" <gitgitgadget@gmail.com> writes:

> +Rebase
> +------
> +In general the rebase command is treated as a modify command. When a change is
> +rebased, the new commit replaces the original.
> +
> +Rebase --abort is special. Its intent is to restore git to the state it had
> +prior to running rebase. It should move back any changes to point to the refs
> +they had prior to running rebase and delete any new changes that were created as
> +part of the rebase. To achieve this, rebase will save the state of all changes
> +in refs/metas prior to running rebase and will restore the entire namespace
> +after rebase completes (deleting any newly-created changes). Newly-created
> +metacommits are left in place, but will have no effect until garbage collected
> +since metacommits are only used if they are reachable from refs/metas.

One thing that makes me nervous is how well your analysis capture
"unusual" but still reasonable ways to use these commands, as the
workflows of people are quite different.

For example, I almost never do "git checkout topic && git rebase
origin"; instead I would do "git checkout topic && git rebase origin
HEAD^0" to first make a detached HEAD out of the topic, in order to
have two copies explicitly available to be compared after "rebase"
finishes.  After doing so and get satisfied by the result of
comparison between topic and HEAD, I may do "git checkout -B topic"
to update.  Would that leave exactly the same set of metacommits as
the case where I didn't do the "first rebase the detached HEAD and
then update the bracnh for real" and instead "rebase the topic"
directly?

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 01/10] technical doc: add a design doc for the evolve command
  2022-09-28 22:20   ` Junio C Hamano
@ 2022-09-29  9:17     ` Phillip Wood
  0 siblings, 0 replies; 66+ messages in thread
From: Phillip Wood @ 2022-09-29  9:17 UTC (permalink / raw)
  To: Junio C Hamano, Stefan Xenos via GitGitGadget; +Cc: git, Christophe Poucet

On 28/09/2022 23:20, Junio C Hamano wrote:
> "Stefan Xenos via GitGitGadget" <gitgitgadget@gmail.com> writes:
> 
>> +Rebase
>> +------
>> +In general the rebase command is treated as a modify command. When a change is
>> +rebased, the new commit replaces the original.
>> +
>> +Rebase --abort is special. Its intent is to restore git to the state it had
>> +prior to running rebase. It should move back any changes to point to the refs
>> +they had prior to running rebase and delete any new changes that were created as
>> +part of the rebase. To achieve this, rebase will save the state of all changes
>> +in refs/metas prior to running rebase and will restore the entire namespace
>> +after rebase completes (deleting any newly-created changes).

That wont work now that we have multiple worktrees. If a user starts two 
rebases of two different branches in two different worktrees and aborts 
one of them we loose the new meta-commits of both. Each rebase will need 
to track which refs under refs/metas/ it has updated (that will also 
save the overhead of copying the entire refs/metas/ subtree).

> Newly-created
>> +metacommits are left in place, but will have no effect until garbage collected
>> +since metacommits are only used if they are reachable from refs/metas.
> 
> One thing that makes me nervous is how well your analysis capture
> "unusual" but still reasonable ways to use these commands, as the
> workflows of people are quite different.
> 
> For example, I almost never do "git checkout topic && git rebase
> origin"; instead I would do "git checkout topic && git rebase origin
> HEAD^0" to first make a detached HEAD out of the topic, in order to
> have two copies explicitly available to be compared after "rebase"
> finishes.  After doing so and get satisfied by the result of
> comparison between topic and HEAD, I may do "git checkout -B topic"
> to update.  Would that leave exactly the same set of metacommits as
> the case where I didn't do the "first rebase the detached HEAD and
> then update the bracnh for real" and instead "rebase the topic"
> directly?

As I understand it we have a ref under refs/metas/ for each commit. If 
that understanding is correct the two cases you describe should be 
recorded identically I think.

Best Wishes

Phillip

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 01/10] technical doc: add a design doc for the evolve command
  2022-09-23 18:55 ` [PATCH 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
                     ` (2 preceding siblings ...)
  2022-09-28 22:20   ` Junio C Hamano
@ 2022-09-29 19:57   ` Jonathan Tan
  3 siblings, 0 replies; 66+ messages in thread
From: Jonathan Tan @ 2022-09-29 19:57 UTC (permalink / raw)
  To: Stefan Xenos via GitGitGadget
  Cc: Jonathan Tan, git, Christophe Poucet, Stefan Xenos

"Stefan Xenos via GitGitGadget" <gitgitgadget@gmail.com> writes:
> +Background
> +==========
> +Imagine you have three sequential changes up for review and you receive feedback
> +that requires editing all three changes. We'll define the word "change"
> +formally later, but for the moment let's say that a change is a work-in-progress
> +whose final version will be submitted as a commit in the future.
[snip]
> +Part of making the "evolve" command work involves tracking the edits to a commit
> +over time, which is why we need an change graph. 

Reading later, I thought that a "change" is a connected subset of
elements in the set of metacommits, so a "change" is already a graph.
I'll mentally substitute "the concept of a change" for "an [sic] change
graph" for now - hopefully that's correct.

> +- It can be used as part of other high-level commands that combine or split
> +  changes.

Is the current concept of a change suitable for combining and splitting?
There is divergence, but that seems like a commit being modified in 2
different ways, not a commit being split into 2.

> +Goals
> +-----
[snip]
> +P0. A commit can be obsoleted by more than one replacement (called divergence).
> +P0. Users must be able to resolve divergence (convergence).

Is divergence important? It seems to me that both the internals and the
UX could be simplified if we don't allow divergence, and systems like
Gerrit don't have it either (as far as I know, in Gerrit, all commits
bearing the same Change-Id just form a sequence in the order that they
were pushed to the server, with no branching).

> +Overview
> +========
> +We introduce the notion of “meta-commits” which describe how one commit was
> +created from other commits. A branch of meta-commits is known as a change.

"Branch" here is confusing. Do you mean a connected set of meta-commits?

("Branch" has a specific meaning in Git - a ref of the form
refs/heads/??. If you mean that refs of the form refs/metas/?? point to
changes in a 1:1 manner, then the term "ref" is appropriate.)

> +Example usage
> +-------------
> +# First create three dependent changes
> +$ echo foo>bar.txt && git add .
> +$ git commit -m "This is a test"
> +created change metas/this_is_a_test
> +$ echo foo2>bar2.txt && git add .
> +$ git commit -m "This is also a test"
> +created change metas/this_is_also_a_test
> +$ echo foo3>bar3.txt && git add .
> +$ git commit -m "More testing"
> +created change metas/more_testing
> +
> +# List all our changes in progress
> +$ git change list
> +metas/this_is_a_test
> +metas/this_is_also_a_test
> +* metas/more_testing
> +metas/some_change_already_merged_upstream
> +
> +# Now modify the earliest change, using its stable name
> +$ git reset --hard metas/this_is_a_test
> +$ echo morefoo>>bar.txt && git add . && git commit --amend --no-edit

So up to here, I thought that we would have 2 refs metas/this_is_a_test2
and metas/this_is_a_test, with the latter's commit being one of the
parents of the former's commit. (This is because we presumably need to
be able to represent the situation in which the user checks out the
original "This is a test" commit and modifies it, so we still need to
hang on to the metas/this_is_a_test.)

> +# Use git-evolve to fix up any dependent changes
> +$ git evolve
> +rebasing metas/this_is_also_a_test onto metas/this_is_a_test
> +rebasing metas/more_testing onto metas/this_is_also_a_test
> +Done

So I'm surprised that there's no mention of this_is_a_test2 here. In
addition, linearizing a change like this doesn't seem to be described in
this document.

Having said that, I don't think that linearizing changes is important to
the goals of this "evolve" concept, so maybe one thing we can do is to
not support it at all.

Fast-forward...

> +# Fetch the latest code from origin/master and use git-evolve
> +# to rebase all dependent changes.
> +$ git fetch origin master
> +$ git evolve origin/master
> +deleting metas/some_change_already_merged_upstream
> +rebasing metas/this_is_a_test onto origin/master
> +rebasing metas/this_is_also_a_test onto metas/this_is_a_test
> +rebasing metas/more_testing onto metas/this_is_also_a_test
> +rebasing metas/unrelated_change onto origin/master
> +Conflict detected! Resolve it and then use git evolve --continue to resume.
> +
> +# Sort out the conflict
> +$ git mergetool
> +$ git evolve origin/master
> +Done

This is what I expected from the evolve mechanism, so that's great :-)

The conflict resolution needs to be discussed further, though. It is
superficially similar to rebase, but with rebase, the ref being rebased
is only rewritten at the end of the process, so it is always possible to
abort halfway. Here, multiple refs are written during the process, so it
is not as easy to abort.

> +Parent-type
> +-----------
> +The “parent-type” field in the commit header identifies a commit as a
> +meta-commit and indicates the meaning for each of its parents. It is never
> +present for normal commits. It contains a space-deliminated list of enum values
> +whose order matches the order of the parents. Possible parent types are:
> +
> +- c: (content) the content parent identifies the commit that this meta-commit is
> +  describing.
> +- r: (replaced) indicates that this parent is made obsolete by the content
> +  parent.
> +- o: (origin) indicates that the content parent was generated by cherry-picking
> +  this parent.
> +- a: (abandoned) used in place of a content parent for abandoned changes. Points
> +  to the final content commit for the change at the time it was abandoned.

How would the "o" parent be useful to the user?

> +Changes
> +-------
[snip]
> +Changes are also stored in the refs/hiddenmetas namespace. Hiddenmetas holds
> +metadata for historical changes that are not currently in progress by the user.
> +Commands like filter-branch and other bulk import commands create metadata in
> +this namespace.
> +
> +Note that the changes in hiddenmetas get special treatment in several ways:
> +
> +- They are not cleaned up automatically once merged, since it is expected that
> +  they refer to historical changes.
> +- User commands that modify changes don't append to these changes as they would
> +  to a change in refs/metas.
> +- They are not displayed when the user lists their local changes.

The presence of refs/hiddenmetas further muddies the already unclear
lifecycle of meta-commits and their refs. Non-hidden meta-commits get
cleaned up when their latest commit appears upstream, so they may get
deleted when the user doesn't expect it (especially if the user is
using, say, a prefetch mechanism that downloads refs at night). We're
adding to this a class of refs that don't get cleaned up at all.

Besides the disk space taken by the meta-commits, having more refs
typically reduces performance e.g. because all refs generally take part
in packfile negotiation during fetching. (And they probably should
continue with this behavior because sharing meta-commits is one of the
features we want.) So I think that having a clear cleanup strategy is a
good idea, and permanent archiving probably shouldn't be it.

> +Change creation
> +---------------
> +Changes are created automatically whenever the user runs a command like “commit”
> +that has the semantics of creating a new change. They also move forward
> +automatically even if they’re not checked out. For example, whenever the user
> +runs a command like “commit --amend” that modifies a commit, all branches in
> +refs/metas that pointed to the old commit move forward to point to its
> +replacement instead.

What happens in the following?

  $ echo "hello" >hello.txt
  $ git add hello.txt
  $ git commit -m "hello"
  $ git tag hello
  $ echo "one" >hello.txt
  $ git commit -a --amend # this updates refs/metas/hello
  $ git checkout hello
  $ echo "one" >hello.txt
  $ git commit -a --amend # does this update refs/metas/hello too?

> +Sharing changes
> +---------------
> +Change histories are shared by pushing or fetching meta-commits and change
> +branches. This provides users with a lot of control of what to share and
> +repository implementations with control over what to retain.
> +
> +Users that only want to share the content of a commit can do so by pushing the
> +commit itself as they currently would. Users that want to share an edit history
> +for the commit can push its change, which would point to a meta-commit rather
> +than the commit itself if there is any history to share. Note that multiple
> +changes can refer to the same commits, so it’s possible to construct and push a
> +different history for the same commit in order to remove sensitive or irrelevant
> +intermediate states.

It looks difficult to remove such intermediate states, but maybe that
doesn't have to be dealt with in the initial design.

> +Checkout
> +--------
> +Running checkout on a change by name has the same effect as checking out a
> +detached head pointing to the latest commit on that change-branch. There is no
> +need to ever have HEAD point to a change since changes always move forward when
> +necessary, no matter what branch the user has checked out
> +
> +Meta-commits themselves cannot be checked out by their hash.

This is the same behavior as for annotated tags, but I guess we can't
use them because those can only have one referent.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 00/10] Add the Git Change command
  2022-09-25  8:39 ` [PATCH 00/10] Add the Git Change command Phillip Wood
@ 2022-10-04  9:33   ` Chris P
  0 siblings, 0 replies; 66+ messages in thread
From: Chris P @ 2022-10-04  9:33 UTC (permalink / raw)
  To: phillip.wood; +Cc: Christophe Poucet via GitGitGadget, git, Christophe Poucet

> Thanks for picking this up, having an evolve command would be a really
> useful addition to git. I read the final four patches as I was
> interested to see how a user would use "git change" to track changes to
> a set of commits. Unfortunately because there are no tests and scant
> documentation there are no examples of how to do this. Looking at the
> patches I felt like it would have been helpful to mark them as RFC to
> indicate that the author is requesting feedback but does not consider
> them ready for merging.

Thanks for the feedback, I'll mark them as RFC.

> I'm confused as to why the command is called "change" (which I don't
> find particularly descriptive) when every patch subject is "evolve". It
> definitely makes sense to request feedback on a large topic like this
> before everything is implemented but I'd be nervous of merging the early
> stages before there is a working evolve command. For an example of a
> successful multipart topic see
> https://lore.kernel.org/git/pull.1248.git.1654545325.gitgitgadget@gmail.com/
> Knowing the author of that series the commit messages should also give
> you a good idea of the level of detail expected.

The `git change` command is a lower-level command used to directly
manipulate changes, as a user you should not be engaging with those.
What is missing is the more complicated  `git evolve` command.
I admit that I don't yet know how to implement that or the changes that
need to happen to all create/modify commands.

Still learning git, so apologies for any mistakes and thank you for your
consideration

- simply chris

On Sun, Sep 25, 2022 at 10:40 AM Phillip Wood <phillip.wood123@gmail.com> wrote:
>
> Hi Christophe
>
> On 23/09/2022 19:55, Christophe Poucet via GitGitGadget wrote:
> > I'm reviving the original git evolve work that was started by
> > sxenos@google.com
> > (https://public-inbox.org/git/20190215043105.163688-1-sxenos@google.com/)
> >
> > This work is intended to make it easier to deal with stacked changes.
> >
> > The following set of patches introduces the design doc on the evolve command
> > as well as the basics of the git change command.
>
> Thanks for picking this up, having an evolve command would be a really
> useful addition to git. I read the final four patches as I was
> interested to see how a user would use "git change" to track changes to
> a set of commits. Unfortunately because there are no tests and scant
> documentation there are no examples of how to do this. Looking at the
> patches I felt like it would have been helpful to mark them as RFC to
> indicate that the author is requesting feedback but does not consider
> them ready for merging.
>
> I'm confused as to why the command is called "change" (which I don't
> find particularly descriptive) when every patch subject is "evolve". It
> definitely makes sense to request feedback on a large topic like this
> before everything is implemented but I'd be nervous of merging the early
> stages before there is a working evolve command. For an example of a
> successful multipart topic see
> https://lore.kernel.org/git/pull.1248.git.1654545325.gitgitgadget@gmail.com/
> Knowing the author of that series the commit messages should also give
> you a good idea of the level of detail expected.
>
> Best Wishes
>
> Phillip
>
> > Chris Poucet (4):
> >    sha1-array: implement oid_array_readonly_contains
> >    ref-filter: add the metas namespace to ref-filter
> >    evolve: add delete command
> >    evolve: add documentation for `git change`
> >
> > Stefan Xenos (6):
> >    technical doc: add a design doc for the evolve command
> >    evolve: add support for parsing metacommits
> >    evolve: add the change-table structure
> >    evolve: add support for writing metacommits
> >    evolve: implement the git change command
> >    evolve: add the git change list command
> >
> >   .gitignore                         |    1 +
> >   Documentation/git-change.txt       |   55 ++
> >   Documentation/technical/evolve.txt | 1051 ++++++++++++++++++++++++++++
> >   Makefile                           |    4 +
> >   builtin.h                          |    1 +
> >   builtin/change.c                   |  342 +++++++++
> >   change-table.c                     |  179 +++++
> >   change-table.h                     |  132 ++++
> >   git.c                              |    1 +
> >   metacommit-parser.c                |  110 +++
> >   metacommit-parser.h                |   19 +
> >   metacommit.c                       |  404 +++++++++++
> >   metacommit.h                       |   58 ++
> >   oid-array.c                        |   12 +
> >   oid-array.h                        |    7 +
> >   ref-filter.c                       |   10 +-
> >   ref-filter.h                       |    8 +-
> >   t/helper/test-oid-array.c          |    6 +
> >   t/t0064-oid-array.sh               |   22 +
> >   19 files changed, 2418 insertions(+), 4 deletions(-)
> >   create mode 100644 Documentation/git-change.txt
> >   create mode 100644 Documentation/technical/evolve.txt
> >   create mode 100644 builtin/change.c
> >   create mode 100644 change-table.c
> >   create mode 100644 change-table.h
> >   create mode 100644 metacommit-parser.c
> >   create mode 100644 metacommit-parser.h
> >   create mode 100644 metacommit.c
> >   create mode 100644 metacommit.h
> >
> >
> > base-commit: 4b79ee4b0cd1130ba8907029cdc5f6a1632aca26
> > Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1356%2Fpoucet%2Fevolve-v1
> > Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1356/poucet/evolve-v1
> > Pull-Request: https://github.com/gitgitgadget/git/pull/1356

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 03/10] ref-filter: add the metas namespace to ref-filter
  2022-09-26 13:13   ` Phillip Wood
@ 2022-10-04  9:50     ` Chris P
  0 siblings, 0 replies; 66+ messages in thread
From: Chris P @ 2022-10-04  9:50 UTC (permalink / raw)
  To: phillip.wood; +Cc: Chris Poucet via GitGitGadget, git, Chris Poucet

> I assume this is to save having to write "refs/metas/" when we want to
> search for meta commits?

Yes, though currently it still requires "metas/", I'm trying to figure
out how to
remove that.

> Signed-off-by: Chris Poucet <poucet@google.com>
> --- > diff --git a/ref-filter.h b/ref-filter.h
> index aa0eea4ecf5..064fbef8e50 100644
> --- a/ref-filter.h
> +++ b/ref-filter.h
> @@ -17,8 +17,10 @@
>   #define FILTER_REFS_BRANCHES       0x0004
>   #define FILTER_REFS_REMOTES        0x0008
>   #define FILTER_REFS_OTHERS         0x0010
> +#define FILTER_REFS_CHANGES        0x0040

> It would be nice to keep FILTER_REFS_OTHERS at the end I think (we don't
> need to worry about abi compatibility), also what happened to 0x0020?

The 0x0020 is listed further below, I don't know why the previous
author decided to
put them out of order. I've renumbered them as you've requested with
the assumption
that this data never gets serialized to storage.

> Best Wishes

Thank you for all the feedback!

- simply chris

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 04/10] evolve: add support for parsing metacommits
  2022-09-26 13:27   ` Phillip Wood
@ 2022-10-04 11:21     ` Chris P
  2022-10-04 14:10       ` Phillip Wood
  0 siblings, 1 reply; 66+ messages in thread
From: Chris P @ 2022-10-04 11:21 UTC (permalink / raw)
  To: phillip.wood; +Cc: Stefan Xenos via GitGitGadget, git, Christophe Poucet

> > This patch adds the get_metacommit_content method, which can classify
> > commits as either metacommits or normal commits, determine whether they
> > are abandoned, and extract the content commit's object id from the
> > metacommit.
> > diff --git a/Makefile b/Makefile
> > index cac3452edb9..b2bcc00c289 100644
> > --- a/Makefile
> > +++ b/Makefile
> > @@ -999,6 +999,7 @@ LIB_OBJS += merge-ort.o
> >   LIB_OBJS += merge-ort-wrappers.o
> >   LIB_OBJS += merge-recursive.o
> >   LIB_OBJS += merge.o
> > +LIB_OBJS += metacommit-parser.o
>
> There seems to be a problem with the indent here

I'm not sure I follow, there's not indentation on that line?
>
> >   LIB_OBJS += midx.o
> >   LIB_OBJS += name-hash.o
> >   LIB_OBJS += negotiator/default.o
>
>  > diff --git a/metacommit-parser.h b/metacommit-parser.h
>  > new file mode 100644
>  > index 00000000000..1c74bd6d699
>  > --- /dev/null
>  > +++ b/metacommit-parser.h
>  > @@ -0,0 +1,19 @@
>  > +#ifndef METACOMMIT_PARSER_H
>  > +#define METACOMMIT_PARSER_H
>  > +
>  > +#include "commit.h"
>  > +#include "hash.h"
>  > +
>  > +/* Indicates a normal commit (non-metacommit) */
>  > +#define METACOMMIT_TYPE_NONE 0
>  > +/* Indicates a metacommit with normal content (non-abandoned) */
>  > +#define METACOMMIT_TYPE_NORMAL 1
>  > +/* Indicates a metacommit with abandoned content */
>  > +#define METACOMMIT_TYPE_ABANDONED 2
>
> Is it possible to define these as an enum? It would make the signature
> of get_meta_commit_content() nicer.
>
>  > +struct commit;
>
> What's this for? We're including commit.h above.

Forgot to remove this as I added the include commit.h later.

>
>  > +extern int get_metacommit_content(
>  > +    struct commit *commit, struct object_id *content);
>
> > diff --git a/metacommit-parser.c b/metacommit-parser.c
> > new file mode 100644
> > index 00000000000..70c1428bfc6
> > --- /dev/null
> > +++ b/metacommit-parser.c
> > @@ -0,0 +1,110 @@
> > +#include "cache.h"
> > +#include "metacommit-parser.h"
> > +#include "commit.h"
> > +
> > +/*
> > + * Search the commit buffer for a line starting with the given key. Unlike
> > + * find_commit_header, this also searches the commit message body.
> > + */
>
> There is no explanation in the code or commit message as to why this
> function is needed. The documentation added in the first commit says
> that "parent-type" header is a commit header. I think the answer is that
> this series does not implement that header but uses the commit message
> instead. That's perfectly fine for a proof of concept but it is
> precisely the sort of detail that should be described it the commit
> message and probably flagged up in the cover letter.

I admit I thought I thought this was part of the header because it
shows up before
the blank line before the commit title.

How do I make this a commit header?

>
> > +static const char *find_key(const char *msg, const char *key, size_t *out_len)
> > +{
> > +     int key_len = strlen(key);
> > +     const char *line = msg;
> > +
> > +     while (line) {
> > +             const char *eol = strchrnul(line, '\n');
> > +
> > +             if (eol - line > key_len && !memcmp(line, key, key_len) &&
> > +                 line[key_len] == ' ') {
> > +                     *out_len = eol - line - key_len - 1;
> > +                     return line + key_len + 1;
> > +             }
> > +             line = *eol ? eol + 1 : NULL;
> > +     }
> > +     return NULL;
> > +}
> > +
> > +static struct commit *get_commit_by_index(struct commit_list *to_search, int index)
> > +{
> > +     while (to_search && index) {
> > +             to_search = to_search->next;
> > +             index--;
> > +     }
> > +
> > +     if (!to_search)
> > +             return NULL;
> > +
> > +     return to_search->item;
> > +}
>
> This function is a useful utility for struct commit_list and should live
> in commit.c. It could be used to simplify object-name.c:get_parent() for
> example.

Done.  I'll defer cleaning up get_parent to a potentially later change to avoid
muddying up this change too much.

>
> > +/*
> > + * Writes the index of the content parent to "result". Returns the metacommit
> > + * type. See the METACOMMIT_TYPE_* constants.
> > + */
> > +static int index_of_content_commit(const char *buffer, int *result)
>
> I found the signature confusing as it is returning an int but that is
> not the index. Switching to an enum for the metacommit types would
> clarify that.

Done.

>
> > +{
> > +     int index = 0;
> > +     int ret = METACOMMIT_TYPE_NONE;
> > +     size_t parent_types_size;
> > +     const char *parent_types = find_key(buffer, "parent-type",
> > +             &parent_types_size);
> > +     const char *end;
> > +     const char *enum_start = parent_types;
> > +     int enum_length = 0;
> > +
> > +     if (!parent_types)
> > +             return METACOMMIT_TYPE_NONE;
> > +
> > +     end = &parent_types[parent_types_size];
> > +
> > +     while (1) {
> > +             char next = *parent_types;
> > +             if (next == ' ' || parent_types >= end) {
> > +                     if (enum_length == 1) {
>
> if enum_length != 1 then there is an error in the parent-type header and
> we should probably bail out.
>
> > +                             char first_char_in_enum = *enum_start;
>
> It's not just the first character, it's the only character, do we really
> need such a long variable name? (how about just calling it "type")

Done.

> I'll try and take at look at the next couple of patches later in the week.

Thank you for all the reviews!

-- simply chris

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 04/10] evolve: add support for parsing metacommits
  2022-10-04 11:21     ` Chris P
@ 2022-10-04 14:10       ` Phillip Wood
  0 siblings, 0 replies; 66+ messages in thread
From: Phillip Wood @ 2022-10-04 14:10 UTC (permalink / raw)
  To: Chris P; +Cc: Stefan Xenos via GitGitGadget, git, Christophe Poucet

Hi Chris

On 04/10/2022 12:21, Chris P wrote:
>>> This patch adds the get_metacommit_content method, which can classify
>>> commits as either metacommits or normal commits, determine whether they
>>> are abandoned, and extract the content commit's object id from the
>>> metacommit.
>>> diff --git a/Makefile b/Makefile
>>> index cac3452edb9..b2bcc00c289 100644
>>> --- a/Makefile
>>> +++ b/Makefile
>>> @@ -999,6 +999,7 @@ LIB_OBJS += merge-ort.o
>>>    LIB_OBJS += merge-ort-wrappers.o
>>>    LIB_OBJS += merge-recursive.o
>>>    LIB_OBJS += merge.o
>>> +LIB_OBJS += metacommit-parser.o
>>
>> There seems to be a problem with the indent here
> 
> I'm not sure I follow, there's not indentation on that line?

For some reason LIB_OBJS on that line does not line up with the lines 
either side of it in my mailer, but looking at the patch on 
lore.kernel.org it seems fine so I think the problem was at my end.

>>> diff --git a/metacommit-parser.c b/metacommit-parser.c
>>> new file mode 100644
>>> index 00000000000..70c1428bfc6
>>> --- /dev/null
>>> +++ b/metacommit-parser.c
>>> @@ -0,0 +1,110 @@
>>> +#include "cache.h"
>>> +#include "metacommit-parser.h"
>>> +#include "commit.h"
>>> +
>>> +/*
>>> + * Search the commit buffer for a line starting with the given key. Unlike
>>> + * find_commit_header, this also searches the commit message body.
>>> + */
>>
>> There is no explanation in the code or commit message as to why this
>> function is needed. The documentation added in the first commit says
>> that "parent-type" header is a commit header. I think the answer is that
>> this series does not implement that header but uses the commit message
>> instead. That's perfectly fine for a proof of concept but it is
>> precisely the sort of detail that should be described it the commit
>> message and probably flagged up in the cover letter.
> 
> I admit I thought I thought this was part of the header because it
> shows up before
> the blank line before the commit title.

If I create a meta-commit and then run "git cat-file commit" on it I see

tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
parent fd7e455287603d5bb2e3623dc442b592411cbfe9
parent d79ce1670bdcb76e6d1da2ae095e890ccb326ae9
author A U Thor <author@example.com> 1112912113 -0700
committer C O Mitter <committer@example.com> 1112912113 -0700

parent-type c r

i.e. the parent-type comes after the blank line that separates the 
headers from the message

> How do I make this a commit header?

I've left some comments on the patch that creates the meta-commits. 
Since I wrote the above Junio has commented[1] that he prefers the 
commit message approach to adding a new header so I'd leave the creation 
as it is for now and change find_key() just to look at the commit 
message. (I do prefer the idea of a new header as it provides an 
unambiguous way to distinguish meta-commits from normal commits but lets 
see how using the commit message pans out)

[1] https://lore.kernel.org/git/xmqqsfkbqjgz.fsf@gitster.g/

>>> +static const char *find_key(const char *msg, const char *key, size_t *out_len)
>>> +{
>>> +     int key_len = strlen(key);
>>> +     const char *line = msg;
>>> +
>>> +     while (line) {
>>> +             const char *eol = strchrnul(line, '\n');
>>> +
>>> +             if (eol - line > key_len && !memcmp(line, key, key_len) &&
>>> +                 line[key_len] == ' ') {
>>> +                     *out_len = eol - line - key_len - 1;
>>> +                     return line + key_len + 1;
>>> +             }
>>> +             line = *eol ? eol + 1 : NULL;
>>> +     }
>>> +     return NULL;
>>> +}
>>> +
>>> +static struct commit *get_commit_by_index(struct commit_list *to_search, int index)
>>> +{
>>> +     while (to_search && index) {
>>> +             to_search = to_search->next;
>>> +             index--;
>>> +     }
>>> +
>>> +     if (!to_search)
>>> +             return NULL;
>>> +
>>> +     return to_search->item;
>>> +}
>>
>> This function is a useful utility for struct commit_list and should live
>> in commit.c. It could be used to simplify object-name.c:get_parent() for
>> example.
> 
> Done.  I'll defer cleaning up get_parent to a potentially later change to avoid
> muddying up this change too much.

Sure, get_parent() was meant as an example of why the function is useful 
outside of this work, while you're very welcome to clean it up please 
don't feel that you are obliged to.

>> I'll try and take at look at the next couple of patches later in the week.
> 
> Thank you for all the reviews!

You're welcome, I'm excited to see evolve getting some attention again.

Phillip


> -- simply chris

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 00/10] Add the Git Change command
  2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
                   ` (10 preceding siblings ...)
  2022-09-25  8:39 ` [PATCH 00/10] Add the Git Change command Phillip Wood
@ 2022-10-04 14:24 ` Phillip Wood
  2022-10-04 15:19   ` Chris P
  2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
  12 siblings, 1 reply; 66+ messages in thread
From: Phillip Wood @ 2022-10-04 14:24 UTC (permalink / raw)
  To: Christophe Poucet via GitGitGadget, git; +Cc: Christophe Poucet

Hi Chris

On 23/09/2022 19:55, Christophe Poucet via GitGitGadget wrote:
> I'm reviving the original git evolve work that was started by
> sxenos@google.com
> (https://public-inbox.org/git/20190215043105.163688-1-sxenos@google.com/)
> 
> This work is intended to make it easier to deal with stacked changes.
> 
> The following set of patches introduces the design doc on the evolve command
> as well as the basics of the git change command.

Our test suite can be a little tricky to get started with and I was impatient to
check the basic functionality of these patches so I've written some simple
example tests for the change command and a couple of fixups to make them pass.

Best Wishes

Phillip

---- >8 ----

 From a7c38d0f388e4d8a1f3debcc3069a7fb43084eda Mon Sep 17 00:00:00 2001
From: Phillip Wood <phillip.wood@dunelm.org.uk>
Date: Tue, 4 Oct 2022 15:12:36 +0100
Subject: [PATCH 1/3] fixup! evolve: add support for writing metacommits

---
  metacommit.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/metacommit.c b/metacommit.c
index d2b859a4d3..8f970fa104 100644
--- a/metacommit.c
+++ b/metacommit.c
@@ -296,7 +296,7 @@ int record_metacommit_withresult(
         if (override_change) {
                 string_list_clear(changes, 0);
                 overridden_head = get_change_head(chtable, override_change);
-               if (!overridden_head) {
+               if (overridden_head) {
                         /* This is an existing change */
                         old_head = &overridden_head->head;
                         if (!force) {
-- 
2.37.3.947.g1b8ba4da7f.dirty


 From cc7e8ba0b1a90268ced85d3f0c91aed49f2246d6 Mon Sep 17 00:00:00 2001
From: Phillip Wood <phillip.wood@dunelm.org.uk>
Date: Tue, 4 Oct 2022 15:15:32 +0100
Subject: [PATCH 2/3] fixup! evolve: implement the git change command

---
  t/t9999-changes.sh | 126 +++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 126 insertions(+)
  create mode 100755 t/t9999-changes.sh

diff --git a/t/t9999-changes.sh b/t/t9999-changes.sh
new file mode 100755
index 0000000000..9e58925b23
--- /dev/null
+++ b/t/t9999-changes.sh
@@ -0,0 +1,126 @@
+#!/bin/sh
+
+test_description='git change - low level meta-commit management'
+
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+test_expect_success 'setup commits and meta-commits' '
+       for c in one two three
+       do
+               test_commit $c &&
+               git change update --content $c >actual 2>err &&
+               echo "Created change metas/$c" >expect &&
+               test_cmp expect actual &&
+               test_must_be_empty err &&
+               test_cmp_rev refs/metas/$c $c || return 1
+       done
+'
+
+# Check a meta-commit has the correct parents Call with the object
+# name of the meta-commit followed by pairs of type and parent
+check_meta_commit () {
+       name=$1
+       shift
+       while test $# -gt 0
+       do
+               printf '%s %s\n' $1 $(git rev-parse --verify $2)
+               shift
+               shift
+       done | sort >expect
+       git cat-file commit $name >metacommit &&
+       # commit body should consist of parent-type
+           types="$(sed -n '/^$/ {
+                       :loop
+                       n
+                       s/^parent-type //
+                       p
+                       b loop
+                   }' metacommit)" &&
+       while read key value
+       do
+               # TODO: don't sort the first parent
+               if test "$key" = "parent"
+               then
+                       type="${types%% *}"
+                       test -n "$type" || return 1
+                       printf '%s %s\n' $type $value
+                       types="${types#?}"
+                       types="${types# }"
+               elif test "$key" = "tree"
+               then
+                       test_cmp_rev "$value" $EMPTY_TREE || return 1
+               elif test -z "$key"
+               then
+                       # only parse commit headers
+                       break
+               fi
+       done <metacommit >actual-unsorted &&
+       test -z "$types" &&
+       sort >actual <actual-unsorted &&
+       test_cmp expect actual
+}
+
+test_expect_success 'update meta-commits after rebase' '
+       (
+               set_fake_editor &&
+               FAKE_AMEND=edited &&
+               FAKE_LINES="reword 1 pick 2 fixup 3" &&
+               export FAKE_AMEND FAKE_LINES &&
+               git rebase -i --root
+       ) &&
+
+       # update meta-commits
+       git change update --replace tags/one --content HEAD~1 >out 2>err &&
+       echo "Updated change metas/one" >expect &&
+       test_cmp expect out &&
+       test_must_be_empty err &&
+       git change update --replace tags/two --content HEAD@{2} &&
+       oid=$(git rev-parse --verify metas/two) &&
+       git change update --replace HEAD@{2} --replace tags/three \
+               --content HEAD &&
+
+       # check meta-commits
+       check_meta_commit metas/one c HEAD~1 r tags/one &&
+       check_meta_commit $oid c HEAD@{2} r tags/two &&
+       # NB this checks that "git change update" uses the meta-commit ($oid)
+       #    corresponding to the replaces commit (HEAD@2 above) given on the
+       #    commandline.
+       check_meta_commit metas/two c HEAD r $oid r tags/three &&
+       check_meta_commit metas/three c HEAD r $oid r tags/three
+'
+
+reset_meta_commits () {
+    for c in one two three
+    do
+       echo "update refs/metas/$c refs/tags/$c^0"
+    done | git update-ref --stdin
+}
+
+test_expect_success 'override change name' '
+       # TODO: builtin/change.c expects --change to be the full refname,
+       #       ideally it would prepend refs/metas to the string given by the
+       #       user.
+       git change update --change refs/metas/another-one --content one &&
+       test_cmp_rev metas/another-one one
+'
+
+test_expect_success 'non-fast forward meta-commit update refused' '
+       test_must_fail git change update --change refs/metas/one --content two \
+               >out 2>err &&
+       echo "error: non-fast-forward update to ${SQ}refs/metas/one${SQ}" \
+               >expect &&
+       test_cmp expect err &&
+       test_must_be_empty out
+'
+
+test_expect_success 'forced non-fast forward update succeeds' '
+       git change update --change refs/metas/one --content two --force \
+               >out 2>err &&
+       echo "Updated change metas/one" >expect &&
+       test_cmp expect out &&
+       test_must_be_empty err
+'
+
+test_done
-- 
2.37.3.947.g1b8ba4da7f.dirty


 From 7784f253fa799dd11fcbc81fe815fb387af52d97 Mon Sep 17 00:00:00 2001
From: Phillip Wood <phillip.wood@dunelm.org.uk>
Date: Tue, 4 Oct 2022 15:16:05 +0100
Subject: [PATCH 3/3] fixup! evolve: add the git change list command

---
  builtin/change.c   | 16 +++++-----------
  t/t9999-changes.sh | 11 +++++++++++
  2 files changed, 16 insertions(+), 11 deletions(-)

diff --git a/builtin/change.c b/builtin/change.c
index 07d029d82d..888ef648fa 100644
--- a/builtin/change.c
+++ b/builtin/change.c
@@ -34,9 +34,8 @@ static int change_list(int argc, const char **argv, const char* prefix)
                 OPT_END()
         };
         struct ref_filter filter;
-       /* TODO: See below
         struct ref_sorting *sorting;
-       struct string_list sorting_options = STRING_LIST_INIT_DUP; */
+       struct string_list sorting_options = STRING_LIST_INIT_DUP;
         struct ref_format format = REF_FORMAT_INIT;
         struct ref_array array;
         int i;
@@ -53,19 +52,15 @@ static int change_list(int argc, const char **argv, const char* prefix)
  
         filter_refs(&array, &filter, FILTER_REFS_CHANGES);
  
-       /* TODO: This causes a crash. It sets one of the atom_value handlers to
-        * something invalid, which causes a crash later when we call
-        * show_ref_array_item. Figure out why this happens and put back the sorting.
-        *
-        * sorting = ref_sorting_options(&sorting_options);
-        * ref_array_sort(sorting, &array); */
-
         if (!format.format)
                 format.format = "%(refname:lstrip=1)";
  
         if (verify_ref_format(&format))
                 die(_("unable to parse format string"));
  
+       sorting = ref_sorting_options(&sorting_options);
+       ref_array_sort(sorting, &array);
+
         for (i = 0; i < array.nr; i++) {
                 struct strbuf output = STRBUF_INIT;
                 struct strbuf err = STRBUF_INIT;
@@ -79,8 +74,7 @@ static int change_list(int argc, const char **argv, const char* prefix)
         }
  
         ref_array_clear(&array);
-       /* TODO: see above
-       ref_sorting_release(sorting); */
+       ref_sorting_release(sorting);
  
         return 0;
  }
diff --git a/t/t9999-changes.sh b/t/t9999-changes.sh
index 9e58925b23..9312eba86d 100755
--- a/t/t9999-changes.sh
+++ b/t/t9999-changes.sh
@@ -123,4 +123,15 @@ test_expect_success 'forced non-fast forward update succeeds' '
         test_must_be_empty err
  '
  
+test_expect_success 'list changes' '
+       cat >expect <<-\EOF &&
+       metas/another-one
+       metas/one
+       metas/three
+       metas/two
+       EOF
+       git change list >actual &&
+       test_cmp expect actual
+'
+
  test_done
-- 
2.37.3.947.g1b8ba4da7f.dirty

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* Re: [PATCH 05/10] evolve: add the change-table structure
  2022-09-27 13:27   ` Phillip Wood
  2022-09-27 13:50     ` Ævar Arnfjörð Bjarmason
  2022-09-27 14:18     ` Phillip Wood
@ 2022-10-04 14:48     ` Chris P
  2 siblings, 0 replies; 66+ messages in thread
From: Chris P @ 2022-10-04 14:48 UTC (permalink / raw)
  To: phillip.wood; +Cc: Stefan Xenos via GitGitGadget, git, Christophe Poucet

>  > +/**
>
> We tend to just use '/*' rather than '/**'

It seems there's some disagreement on this. Regardless, I changed the
ones in the implementation to be "/*"

>
>  > + * This struct holds a list of change refs. The first element is
> stored inline,
>  > + * to optimize for small lists.
>  > + */
>  > +struct change_list {
>  > +    /**
>  > +     * Ref name for the first change in the list, or null if none.
>  > +     *
>  > +     * This field is private. Use for_each_change_in to read.
>  > +     */
>  > +    const char* first_refname;
>  > +    /**
>  > +     * List of additional change refs. Note that this is empty if the list
>  > +     * contains 0 or 1 elements.
>  > +     *
>  > +     * This field is private. Use for_each_change_in to read.
>  > +     */
>  > +    struct string_list additional_refnames;
>
> Splitting this feels like a premature optimization. We don't have any
> tests yet, let alone any real-world experience using this code. Also if
> we want to save memory for lists with a single entry why are we
> embedding the struct string_list rather than just storing a pointer to it?

Agreed, simplified to a strset. Thanks for the suggestion.

>
> I think it would be simpler to use a struct strset to hold the refnames
> as we don't need the util field offered by struct string_list.

Done.

>
>  > +/**
>  > + * Holds information about the head of a single change.
>  > + */
>  > +struct change_head {
>  > +    /**
>  > +     * The location pointed to by the head of the change. May be a
> commit or a
>  > +     * metacommit.
>  > +     */
>  > +    struct object_id head;
>
> I found this duality between commits and metacommits rather confusing -
> why isn't the head always a metacommit?

There is no reason to create a metacommit for the first commit you create.
You only need one if you're replacing a commit with another commit.

>
>  > +/**
>  > + * Holds information about the heads of each change, and permits
> effecient
>
> s/effecient/efficient/

Done.

>
>  > + * lookup from a commit to the changes that reference it directly.
>  > + *
>  > + * All fields should be considered private. Use the change_table
> functions
>  > + * to interact with this struct.
>  > + */
>  > +struct change_table {
>  > +    /**
>  > +     * Memory pool for the objects allocated by the change table.
>  > +     */
>  > +    struct mem_pool memory_pool;
>  > +    /* Map object_id to commit_change_list_entry structs. */
>  > +    struct oidmap oid_to_metadata_index;
>  > +    /**
>  > +     * List of ref names. The util value points to a change_head structure
>  > +     * allocated from memory_pool.
>  > +     */
>  > +    struct string_list refname_to_change_head;
>
> I think these days we'd use a strmap for this for O(1) lookups.

Way better!

>
>  > +};
>  > +
>  > +extern void change_table_init(struct change_table *to_initialize);
>
> The struct change_table argument to all these functions changes its name
> more often than a criminal on the run. I would find it much easier to
> follow the code if we consistently called this argument "table"

Agreed, changed them all to "table".

>
>  > + * Adds all changes matching the given ref filter to the given
> change_table
>  > + * struct.
>  > + */
>  > +extern void change_table_add_matching_filter(struct change_table
> *to_modify,
>  > +    struct repository* repo, struct ref_filter *filter);
>
> I can't see any callers outside of change-table.c so do we really need
> to export this function.

Thanks for verifying, done.

> > +
> > +void change_table_init(struct change_table *to_initialize)
> > +{
> > +     memset(to_initialize, 0, sizeof(*to_initialize));
> > +     mem_pool_init(&to_initialize->memory_pool, 0);
> > +     to_initialize->memory_pool.block_alloc = 4*1024 - sizeof(struct mp_block);
>
> If we're using a mempool to minimize the allocation overhead we should
> leave .block_alloc set to the default value of 1MB rather than changing
> it to 4kB

Good question, I don't know the typical sizes that we'll get for these,
so for now just sticking with the default seems sensible.

> > +
> > +static void add_head_to_commit(struct change_table *to_modify,
> > +     const struct object_id *to_add, const char *refname)
>
> I found the function and argument names rather confusing. If I've
> understood the code correctly then this function is adding an assoation
> between the commit "to_add" and "refname". Despite its name "to_add" may
> already exist in the change table.
>
> The formatting is a bit off as well (as are most of the function
> declarations in this patch and the next), we'd write that as
>
> static void add_head_to_commit(struct change_table *table,
>                                const struct object_id *to_add,
>                                const char *refname)

Thanks, I wasn't clear on the guidelines. I hope the new format makes
more sense.

>
> > +{
> > +     struct commit_change_list_entry *entry;
> > +
> > +     /**
> > +      * Note: the indices in the map are 1-based. 0 is used to indicate a missing
> > +      * element.
> > +      */
>
> I'm confused by this comment, what indices is it talking about?

No idea, removed.

> > +
> > +     if (!entry->changes.first_refname)
> > +             entry->changes.first_refname = refname;
> > +     else
> > +             string_list_insert(&entry->changes.additional_refnames, refname);
>
> This is an example of the complexity added by the current definition of
> struct change_list.

Yes, simplified.

>
> > +void change_table_add(struct change_table *to_modify, const char *refname,
> > +     struct commit *to_add)
> > +{
> > +     struct change_head *new_head;
> > +     struct string_list_item *new_item;
> > +     int metacommit_type;
> > +
> > +     new_head = mem_pool_calloc(&to_modify->memory_pool, 1,
> > +             sizeof(*new_head));
> > +
> > +     oidcpy(&new_head->head, &to_add->object.oid);
> > +
> > +     metacommit_type = get_metacommit_content(to_add, &new_head->content);
> > +     if (metacommit_type == METACOMMIT_TYPE_NONE)
> > +             oidcpy(&new_head->content, &to_add->object.oid);
>
> If to_add is not a metacommit then the content is to_add itself,
> otherwise it will have been set by the call to get_metacommit_content().

Yes, added the comment.

>
> > +     new_head->abandoned = (metacommit_type == METACOMMIT_TYPE_ABANDONED);
>
> Style: I don't think we normally bother with parentheses here

I admit I prefer it here because operator priority isn't always
obvious (it could be read as
(new_head->abandoned = metacommit_type) == METACOMMIT_TYPE_ABANDONED;

>
> > +     new_head->remote = starts_with(refname, "refs/remote/");
> > +     new_head->hidden = starts_with(refname, "refs/hiddenmetas/");
> > +
> > +     new_item = string_list_insert(&to_modify->refname_to_change_head, refname);
> > +     new_item->util = new_head;
> > +     /* Use pointers to the copy of the string we're retaining locally */
>
> string_list_insert() copied the string and we're using that copy. Saying
> we're retaining it locally when it will outlive this function call is
> confusing.

This is now obsolete with the move to strmap.

>
> > +     refname = new_item->string;
> > +
> > +     if (!oideq(&new_head->content, &new_head->head))
> > +             add_head_to_commit(to_modify, &new_head->content, refname);
>
> If to_add is a metacommit then we remember the link between refname and
> the content commit.
>
> > +     add_head_to_commit(to_modify, &new_head->head, refname);
>
> We also remember the link between refname and to_add

Thanks, added the comment.

>
> > +}
> > +
> > +void change_table_add_all_visible(struct change_table *to_modify,
> > +     struct repository* repo)
> > +{
> > +     struct ref_filter filter;
>
> rather than using memset we'd write (the same goes for all the other
> memset() calls in this series, unless they're operation on a heap
> allocation)
>
>         struct ref_filter filter = { 0 };

Thanks, I wasn't aware of that trick.

>
> > +     const char *name_patterns[] = {NULL};
> > +     memset(&filter, 0, sizeof(filter));
> > +     filter.kind = FILTER_REFS_CHANGES;
> > +     filter.name_patterns = name_patterns;
> > +
> > +     change_table_add_matching_filter(to_modify, repo, &filter);
> > +}
> > +
> > +void change_table_add_matching_filter(struct change_table *to_modify,
> > +     struct repository* repo, struct ref_filter *filter)
> > +{
> > +     struct ref_array matching_refs;
> > +     int i;
> > +
> > +     memset(&matching_refs, 0, sizeof(matching_refs));
> > +     filter_refs(&matching_refs, filter, filter->kind);
> > +
> > +     /**
> > +      * Determine the object id for the latest content commit for each change.
> > +      * Fetch the commit at the head of each change ref. If it's a normal commit,
> > +      * that's the commit we want. If it's a metacommit, locate its content parent
> > +      * and use that.
> > +      */
> > +
> > +     for (i = 0; i < matching_refs.nr; i++) {
> > +             struct ref_array_item *item = matching_refs.items[i];
> > +             struct commit *commit = item->commit;
> > +
> > +             commit = lookup_commit_reference_gently(repo, &item->objectname, 1);
>
> We're assigning commit twice - why do we need to look it up if
> filter_refs returns it?

I think this is a case of missing logic if you look at what the
comment above it says.

>
> There are a number of places where we call
> lookup_commit_reference_gently(..., 1) to silence the warning if the
> objectname does not dereference to a commit. It is not clear to me that
> we want to hide those errors. Indeed I think we should be doing

Agreed, move to this.

>
>                 commit = lookup_commit_reference(repo, oid)
>                 if (!commit)
>                         BUG("commit missing ...")
>
> unless there is a good reason that the lookup can fail.

I can't think of any but then I'm not the original author.

>
> > +             if (commit)
> > +                     change_table_add(to_modify, item->refname, commit);
> > +     }
> > +
> > +     ref_array_clear(&matching_refs);
> > +}
>
> > +int for_each_change_referencing(struct change_table *table,
> > +     const struct object_id *referenced_commit_id, each_change_fn fn, void *cb_data)
> > +{
> > +     const struct change_list *changes;
> > +     int i;
> > +     int retvalue;
>
> We normally use "ret" for this

Done.

>
> > +     struct commit_change_list_entry *entry;
> > +
> > +     entry = oidmap_get(&table->oid_to_metadata_index,
> > +             referenced_commit_id);
>
> This should be indented to start below the '(' of the function call.

Done.

>
> > +     /* If this commit isn't referenced by any changes, it won't be in the map */
> > +     if (!entry)
> > +             return 0;
> > +     changes = &entry->changes;
> > +     if (!changes->first_refname)
> > +             return 0;
> > +     retvalue = fn(changes->first_refname, cb_data);
> > +     for (i = 0; retvalue == 0 && i < changes->additional_refnames.nr; i++)
> > +             retvalue = fn(changes->additional_refnames.items[i].string, cb_data);
>
> Using an strset for struct change_list would simplify this

Agreed! Simplified.

>
> > +     return retvalue;
> > +}
> > +
> > +struct change_head* get_change_head(struct change_table *heads,
> > +     const char* refname)
> > +{
> > +     struct string_list_item *item = string_list_lookup(
> > +             &heads->refname_to_change_head, refname);
> > +
> > +     if (!item)
> > +             return NULL;
> > +
> > +     return (struct change_head *)item->util;
>
> We don't bother with casting void* pointers like this. In any case this
> whole function could become
>
>         return strmap_get(table, refname)
>
> if we used an strmap instead of a string_list.
>
>
> Aside from the style issues and using api's that have been added since
> Stefan wrote these patches this looks pretty sound. The only thing I
> don't really get why the public api allows normal commits to be added to
> the change table (I can see why we might want to add the content commit
> as well when we add a metacommit but that should be done internally)
>
> Best Wishes
>
> Phillip

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 00/10] Add the Git Change command
  2022-10-04 14:24 ` Phillip Wood
@ 2022-10-04 15:19   ` Chris P
  2022-10-04 15:55     ` Chris P
  2022-10-04 15:57     ` Phillip Wood
  0 siblings, 2 replies; 66+ messages in thread
From: Chris P @ 2022-10-04 15:19 UTC (permalink / raw)
  To: phillip.wood, Christophe Poucet; +Cc: Christophe Poucet via GitGitGadget, git

Thanks a lot.

Is there something special I must do to get these scripts to work? The
entire script fails for me, despite having build git-change.

On Tue, Oct 4, 2022 at 4:24 PM Phillip Wood <phillip.wood123@gmail.com> wrote:
>
> Hi Chris
>
> On 23/09/2022 19:55, Christophe Poucet via GitGitGadget wrote:
> > I'm reviving the original git evolve work that was started by
> > sxenos@google.com
> > (https://public-inbox.org/git/20190215043105.163688-1-sxenos@google.com/)
> >
> > This work is intended to make it easier to deal with stacked changes.
> >
> > The following set of patches introduces the design doc on the evolve command
> > as well as the basics of the git change command.
>
> Our test suite can be a little tricky to get started with and I was impatient to
> check the basic functionality of these patches so I've written some simple
> example tests for the change command and a couple of fixups to make them pass.
>
> Best Wishes
>
> Phillip
>
> ---- >8 ----
>
>  From a7c38d0f388e4d8a1f3debcc3069a7fb43084eda Mon Sep 17 00:00:00 2001
> From: Phillip Wood <phillip.wood@dunelm.org.uk>
> Date: Tue, 4 Oct 2022 15:12:36 +0100
> Subject: [PATCH 1/3] fixup! evolve: add support for writing metacommits
>
> ---
>   metacommit.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/metacommit.c b/metacommit.c
> index d2b859a4d3..8f970fa104 100644
> --- a/metacommit.c
> +++ b/metacommit.c
> @@ -296,7 +296,7 @@ int record_metacommit_withresult(
>          if (override_change) {
>                  string_list_clear(changes, 0);
>                  overridden_head = get_change_head(chtable, override_change);
> -               if (!overridden_head) {
> +               if (overridden_head) {
>                          /* This is an existing change */
>                          old_head = &overridden_head->head;
>                          if (!force) {
> --
> 2.37.3.947.g1b8ba4da7f.dirty
>
>
>  From cc7e8ba0b1a90268ced85d3f0c91aed49f2246d6 Mon Sep 17 00:00:00 2001
> From: Phillip Wood <phillip.wood@dunelm.org.uk>
> Date: Tue, 4 Oct 2022 15:15:32 +0100
> Subject: [PATCH 2/3] fixup! evolve: implement the git change command
>
> ---
>   t/t9999-changes.sh | 126 +++++++++++++++++++++++++++++++++++++++++++++
>   1 file changed, 126 insertions(+)
>   create mode 100755 t/t9999-changes.sh
>
> diff --git a/t/t9999-changes.sh b/t/t9999-changes.sh
> new file mode 100755
> index 0000000000..9e58925b23
> --- /dev/null
> +++ b/t/t9999-changes.sh
> @@ -0,0 +1,126 @@
> +#!/bin/sh
> +
> +test_description='git change - low level meta-commit management'
> +
> +. ./test-lib.sh
> +
> +. "$TEST_DIRECTORY"/lib-rebase.sh
> +
> +test_expect_success 'setup commits and meta-commits' '
> +       for c in one two three
> +       do
> +               test_commit $c &&
> +               git change update --content $c >actual 2>err &&
> +               echo "Created change metas/$c" >expect &&
> +               test_cmp expect actual &&
> +               test_must_be_empty err &&
> +               test_cmp_rev refs/metas/$c $c || return 1
> +       done
> +'
> +
> +# Check a meta-commit has the correct parents Call with the object
> +# name of the meta-commit followed by pairs of type and parent
> +check_meta_commit () {
> +       name=$1
> +       shift
> +       while test $# -gt 0
> +       do
> +               printf '%s %s\n' $1 $(git rev-parse --verify $2)
> +               shift
> +               shift
> +       done | sort >expect
> +       git cat-file commit $name >metacommit &&
> +       # commit body should consist of parent-type
> +           types="$(sed -n '/^$/ {
> +                       :loop
> +                       n
> +                       s/^parent-type //
> +                       p
> +                       b loop
> +                   }' metacommit)" &&
> +       while read key value
> +       do
> +               # TODO: don't sort the first parent
> +               if test "$key" = "parent"
> +               then
> +                       type="${types%% *}"
> +                       test -n "$type" || return 1
> +                       printf '%s %s\n' $type $value
> +                       types="${types#?}"
> +                       types="${types# }"
> +               elif test "$key" = "tree"
> +               then
> +                       test_cmp_rev "$value" $EMPTY_TREE || return 1
> +               elif test -z "$key"
> +               then
> +                       # only parse commit headers
> +                       break
> +               fi
> +       done <metacommit >actual-unsorted &&
> +       test -z "$types" &&
> +       sort >actual <actual-unsorted &&
> +       test_cmp expect actual
> +}
> +
> +test_expect_success 'update meta-commits after rebase' '
> +       (
> +               set_fake_editor &&
> +               FAKE_AMEND=edited &&
> +               FAKE_LINES="reword 1 pick 2 fixup 3" &&
> +               export FAKE_AMEND FAKE_LINES &&
> +               git rebase -i --root
> +       ) &&
> +
> +       # update meta-commits
> +       git change update --replace tags/one --content HEAD~1 >out 2>err &&
> +       echo "Updated change metas/one" >expect &&
> +       test_cmp expect out &&
> +       test_must_be_empty err &&
> +       git change update --replace tags/two --content HEAD@{2} &&
> +       oid=$(git rev-parse --verify metas/two) &&
> +       git change update --replace HEAD@{2} --replace tags/three \
> +               --content HEAD &&
> +
> +       # check meta-commits
> +       check_meta_commit metas/one c HEAD~1 r tags/one &&
> +       check_meta_commit $oid c HEAD@{2} r tags/two &&
> +       # NB this checks that "git change update" uses the meta-commit ($oid)
> +       #    corresponding to the replaces commit (HEAD@2 above) given on the
> +       #    commandline.
> +       check_meta_commit metas/two c HEAD r $oid r tags/three &&
> +       check_meta_commit metas/three c HEAD r $oid r tags/three
> +'
> +
> +reset_meta_commits () {
> +    for c in one two three
> +    do
> +       echo "update refs/metas/$c refs/tags/$c^0"
> +    done | git update-ref --stdin
> +}
> +
> +test_expect_success 'override change name' '
> +       # TODO: builtin/change.c expects --change to be the full refname,
> +       #       ideally it would prepend refs/metas to the string given by the
> +       #       user.
> +       git change update --change refs/metas/another-one --content one &&
> +       test_cmp_rev metas/another-one one
> +'
> +
> +test_expect_success 'non-fast forward meta-commit update refused' '
> +       test_must_fail git change update --change refs/metas/one --content two \
> +               >out 2>err &&
> +       echo "error: non-fast-forward update to ${SQ}refs/metas/one${SQ}" \
> +               >expect &&
> +       test_cmp expect err &&
> +       test_must_be_empty out
> +'
> +
> +test_expect_success 'forced non-fast forward update succeeds' '
> +       git change update --change refs/metas/one --content two --force \
> +               >out 2>err &&
> +       echo "Updated change metas/one" >expect &&
> +       test_cmp expect out &&
> +       test_must_be_empty err
> +'
> +
> +test_done
> --
> 2.37.3.947.g1b8ba4da7f.dirty
>
>
>  From 7784f253fa799dd11fcbc81fe815fb387af52d97 Mon Sep 17 00:00:00 2001
> From: Phillip Wood <phillip.wood@dunelm.org.uk>
> Date: Tue, 4 Oct 2022 15:16:05 +0100
> Subject: [PATCH 3/3] fixup! evolve: add the git change list command
>
> ---
>   builtin/change.c   | 16 +++++-----------
>   t/t9999-changes.sh | 11 +++++++++++
>   2 files changed, 16 insertions(+), 11 deletions(-)
>
> diff --git a/builtin/change.c b/builtin/change.c
> index 07d029d82d..888ef648fa 100644
> --- a/builtin/change.c
> +++ b/builtin/change.c
> @@ -34,9 +34,8 @@ static int change_list(int argc, const char **argv, const char* prefix)
>                  OPT_END()
>          };
>          struct ref_filter filter;
> -       /* TODO: See below
>          struct ref_sorting *sorting;
> -       struct string_list sorting_options = STRING_LIST_INIT_DUP; */
> +       struct string_list sorting_options = STRING_LIST_INIT_DUP;
>          struct ref_format format = REF_FORMAT_INIT;
>          struct ref_array array;
>          int i;
> @@ -53,19 +52,15 @@ static int change_list(int argc, const char **argv, const char* prefix)
>
>          filter_refs(&array, &filter, FILTER_REFS_CHANGES);
>
> -       /* TODO: This causes a crash. It sets one of the atom_value handlers to
> -        * something invalid, which causes a crash later when we call
> -        * show_ref_array_item. Figure out why this happens and put back the sorting.
> -        *
> -        * sorting = ref_sorting_options(&sorting_options);
> -        * ref_array_sort(sorting, &array); */
> -
>          if (!format.format)
>                  format.format = "%(refname:lstrip=1)";
>
>          if (verify_ref_format(&format))
>                  die(_("unable to parse format string"));
>
> +       sorting = ref_sorting_options(&sorting_options);
> +       ref_array_sort(sorting, &array);
> +
>          for (i = 0; i < array.nr; i++) {
>                  struct strbuf output = STRBUF_INIT;
>                  struct strbuf err = STRBUF_INIT;
> @@ -79,8 +74,7 @@ static int change_list(int argc, const char **argv, const char* prefix)
>          }
>
>          ref_array_clear(&array);
> -       /* TODO: see above
> -       ref_sorting_release(sorting); */
> +       ref_sorting_release(sorting);
>
>          return 0;
>   }
> diff --git a/t/t9999-changes.sh b/t/t9999-changes.sh
> index 9e58925b23..9312eba86d 100755
> --- a/t/t9999-changes.sh
> +++ b/t/t9999-changes.sh
> @@ -123,4 +123,15 @@ test_expect_success 'forced non-fast forward update succeeds' '
>          test_must_be_empty err
>   '
>
> +test_expect_success 'list changes' '
> +       cat >expect <<-\EOF &&
> +       metas/another-one
> +       metas/one
> +       metas/three
> +       metas/two
> +       EOF
> +       git change list >actual &&
> +       test_cmp expect actual
> +'
> +
>   test_done
> --
> 2.37.3.947.g1b8ba4da7f.dirty

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 00/10] Add the Git Change command
  2022-10-04 15:19   ` Chris P
@ 2022-10-04 15:55     ` Chris P
  2022-10-04 16:00       ` Phillip Wood
  2022-10-04 15:57     ` Phillip Wood
  1 sibling, 1 reply; 66+ messages in thread
From: Chris P @ 2022-10-04 15:55 UTC (permalink / raw)
  To: phillip.wood, Christophe Poucet; +Cc: Christophe Poucet via GitGitGadget, git

I got the tests to work, except the last one, that one fails.

How can I see the output of actual and expect?

On Tue, Oct 4, 2022 at 5:19 PM Chris P <christophe.poucet@gmail.com> wrote:
>
> Thanks a lot.
>
> Is there something special I must do to get these scripts to work? The
> entire script fails for me, despite having build git-change.
>
> On Tue, Oct 4, 2022 at 4:24 PM Phillip Wood <phillip.wood123@gmail.com> wrote:
> >
> > Hi Chris
> >
> > On 23/09/2022 19:55, Christophe Poucet via GitGitGadget wrote:
> > > I'm reviving the original git evolve work that was started by
> > > sxenos@google.com
> > > (https://public-inbox.org/git/20190215043105.163688-1-sxenos@google.com/)
> > >
> > > This work is intended to make it easier to deal with stacked changes.
> > >
> > > The following set of patches introduces the design doc on the evolve command
> > > as well as the basics of the git change command.
> >
> > Our test suite can be a little tricky to get started with and I was impatient to
> > check the basic functionality of these patches so I've written some simple
> > example tests for the change command and a couple of fixups to make them pass.
> >
> > Best Wishes
> >
> > Phillip
> >
> > ---- >8 ----
> >
> >  From a7c38d0f388e4d8a1f3debcc3069a7fb43084eda Mon Sep 17 00:00:00 2001
> > From: Phillip Wood <phillip.wood@dunelm.org.uk>
> > Date: Tue, 4 Oct 2022 15:12:36 +0100
> > Subject: [PATCH 1/3] fixup! evolve: add support for writing metacommits
> >
> > ---
> >   metacommit.c | 2 +-
> >   1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/metacommit.c b/metacommit.c
> > index d2b859a4d3..8f970fa104 100644
> > --- a/metacommit.c
> > +++ b/metacommit.c
> > @@ -296,7 +296,7 @@ int record_metacommit_withresult(
> >          if (override_change) {
> >                  string_list_clear(changes, 0);
> >                  overridden_head = get_change_head(chtable, override_change);
> > -               if (!overridden_head) {
> > +               if (overridden_head) {
> >                          /* This is an existing change */
> >                          old_head = &overridden_head->head;
> >                          if (!force) {
> > --
> > 2.37.3.947.g1b8ba4da7f.dirty
> >
> >
> >  From cc7e8ba0b1a90268ced85d3f0c91aed49f2246d6 Mon Sep 17 00:00:00 2001
> > From: Phillip Wood <phillip.wood@dunelm.org.uk>
> > Date: Tue, 4 Oct 2022 15:15:32 +0100
> > Subject: [PATCH 2/3] fixup! evolve: implement the git change command
> >
> > ---
> >   t/t9999-changes.sh | 126 +++++++++++++++++++++++++++++++++++++++++++++
> >   1 file changed, 126 insertions(+)
> >   create mode 100755 t/t9999-changes.sh
> >
> > diff --git a/t/t9999-changes.sh b/t/t9999-changes.sh
> > new file mode 100755
> > index 0000000000..9e58925b23
> > --- /dev/null
> > +++ b/t/t9999-changes.sh
> > @@ -0,0 +1,126 @@
> > +#!/bin/sh
> > +
> > +test_description='git change - low level meta-commit management'
> > +
> > +. ./test-lib.sh
> > +
> > +. "$TEST_DIRECTORY"/lib-rebase.sh
> > +
> > +test_expect_success 'setup commits and meta-commits' '
> > +       for c in one two three
> > +       do
> > +               test_commit $c &&
> > +               git change update --content $c >actual 2>err &&
> > +               echo "Created change metas/$c" >expect &&
> > +               test_cmp expect actual &&
> > +               test_must_be_empty err &&
> > +               test_cmp_rev refs/metas/$c $c || return 1
> > +       done
> > +'
> > +
> > +# Check a meta-commit has the correct parents Call with the object
> > +# name of the meta-commit followed by pairs of type and parent
> > +check_meta_commit () {
> > +       name=$1
> > +       shift
> > +       while test $# -gt 0
> > +       do
> > +               printf '%s %s\n' $1 $(git rev-parse --verify $2)
> > +               shift
> > +               shift
> > +       done | sort >expect
> > +       git cat-file commit $name >metacommit &&
> > +       # commit body should consist of parent-type
> > +           types="$(sed -n '/^$/ {
> > +                       :loop
> > +                       n
> > +                       s/^parent-type //
> > +                       p
> > +                       b loop
> > +                   }' metacommit)" &&
> > +       while read key value
> > +       do
> > +               # TODO: don't sort the first parent
> > +               if test "$key" = "parent"
> > +               then
> > +                       type="${types%% *}"
> > +                       test -n "$type" || return 1
> > +                       printf '%s %s\n' $type $value
> > +                       types="${types#?}"
> > +                       types="${types# }"
> > +               elif test "$key" = "tree"
> > +               then
> > +                       test_cmp_rev "$value" $EMPTY_TREE || return 1
> > +               elif test -z "$key"
> > +               then
> > +                       # only parse commit headers
> > +                       break
> > +               fi
> > +       done <metacommit >actual-unsorted &&
> > +       test -z "$types" &&
> > +       sort >actual <actual-unsorted &&
> > +       test_cmp expect actual
> > +}
> > +
> > +test_expect_success 'update meta-commits after rebase' '
> > +       (
> > +               set_fake_editor &&
> > +               FAKE_AMEND=edited &&
> > +               FAKE_LINES="reword 1 pick 2 fixup 3" &&
> > +               export FAKE_AMEND FAKE_LINES &&
> > +               git rebase -i --root
> > +       ) &&
> > +
> > +       # update meta-commits
> > +       git change update --replace tags/one --content HEAD~1 >out 2>err &&
> > +       echo "Updated change metas/one" >expect &&
> > +       test_cmp expect out &&
> > +       test_must_be_empty err &&
> > +       git change update --replace tags/two --content HEAD@{2} &&
> > +       oid=$(git rev-parse --verify metas/two) &&
> > +       git change update --replace HEAD@{2} --replace tags/three \
> > +               --content HEAD &&
> > +
> > +       # check meta-commits
> > +       check_meta_commit metas/one c HEAD~1 r tags/one &&
> > +       check_meta_commit $oid c HEAD@{2} r tags/two &&
> > +       # NB this checks that "git change update" uses the meta-commit ($oid)
> > +       #    corresponding to the replaces commit (HEAD@2 above) given on the
> > +       #    commandline.
> > +       check_meta_commit metas/two c HEAD r $oid r tags/three &&
> > +       check_meta_commit metas/three c HEAD r $oid r tags/three
> > +'
> > +
> > +reset_meta_commits () {
> > +    for c in one two three
> > +    do
> > +       echo "update refs/metas/$c refs/tags/$c^0"
> > +    done | git update-ref --stdin
> > +}
> > +
> > +test_expect_success 'override change name' '
> > +       # TODO: builtin/change.c expects --change to be the full refname,
> > +       #       ideally it would prepend refs/metas to the string given by the
> > +       #       user.
> > +       git change update --change refs/metas/another-one --content one &&
> > +       test_cmp_rev metas/another-one one
> > +'
> > +
> > +test_expect_success 'non-fast forward meta-commit update refused' '
> > +       test_must_fail git change update --change refs/metas/one --content two \
> > +               >out 2>err &&
> > +       echo "error: non-fast-forward update to ${SQ}refs/metas/one${SQ}" \
> > +               >expect &&
> > +       test_cmp expect err &&
> > +       test_must_be_empty out
> > +'
> > +
> > +test_expect_success 'forced non-fast forward update succeeds' '
> > +       git change update --change refs/metas/one --content two --force \
> > +               >out 2>err &&
> > +       echo "Updated change metas/one" >expect &&
> > +       test_cmp expect out &&
> > +       test_must_be_empty err
> > +'
> > +
> > +test_done
> > --
> > 2.37.3.947.g1b8ba4da7f.dirty
> >
> >
> >  From 7784f253fa799dd11fcbc81fe815fb387af52d97 Mon Sep 17 00:00:00 2001
> > From: Phillip Wood <phillip.wood@dunelm.org.uk>
> > Date: Tue, 4 Oct 2022 15:16:05 +0100
> > Subject: [PATCH 3/3] fixup! evolve: add the git change list command
> >
> > ---
> >   builtin/change.c   | 16 +++++-----------
> >   t/t9999-changes.sh | 11 +++++++++++
> >   2 files changed, 16 insertions(+), 11 deletions(-)
> >
> > diff --git a/builtin/change.c b/builtin/change.c
> > index 07d029d82d..888ef648fa 100644
> > --- a/builtin/change.c
> > +++ b/builtin/change.c
> > @@ -34,9 +34,8 @@ static int change_list(int argc, const char **argv, const char* prefix)
> >                  OPT_END()
> >          };
> >          struct ref_filter filter;
> > -       /* TODO: See below
> >          struct ref_sorting *sorting;
> > -       struct string_list sorting_options = STRING_LIST_INIT_DUP; */
> > +       struct string_list sorting_options = STRING_LIST_INIT_DUP;
> >          struct ref_format format = REF_FORMAT_INIT;
> >          struct ref_array array;
> >          int i;
> > @@ -53,19 +52,15 @@ static int change_list(int argc, const char **argv, const char* prefix)
> >
> >          filter_refs(&array, &filter, FILTER_REFS_CHANGES);
> >
> > -       /* TODO: This causes a crash. It sets one of the atom_value handlers to
> > -        * something invalid, which causes a crash later when we call
> > -        * show_ref_array_item. Figure out why this happens and put back the sorting.
> > -        *
> > -        * sorting = ref_sorting_options(&sorting_options);
> > -        * ref_array_sort(sorting, &array); */
> > -
> >          if (!format.format)
> >                  format.format = "%(refname:lstrip=1)";
> >
> >          if (verify_ref_format(&format))
> >                  die(_("unable to parse format string"));
> >
> > +       sorting = ref_sorting_options(&sorting_options);
> > +       ref_array_sort(sorting, &array);
> > +
> >          for (i = 0; i < array.nr; i++) {
> >                  struct strbuf output = STRBUF_INIT;
> >                  struct strbuf err = STRBUF_INIT;
> > @@ -79,8 +74,7 @@ static int change_list(int argc, const char **argv, const char* prefix)
> >          }
> >
> >          ref_array_clear(&array);
> > -       /* TODO: see above
> > -       ref_sorting_release(sorting); */
> > +       ref_sorting_release(sorting);
> >
> >          return 0;
> >   }
> > diff --git a/t/t9999-changes.sh b/t/t9999-changes.sh
> > index 9e58925b23..9312eba86d 100755
> > --- a/t/t9999-changes.sh
> > +++ b/t/t9999-changes.sh
> > @@ -123,4 +123,15 @@ test_expect_success 'forced non-fast forward update succeeds' '
> >          test_must_be_empty err
> >   '
> >
> > +test_expect_success 'list changes' '
> > +       cat >expect <<-\EOF &&
> > +       metas/another-one
> > +       metas/one
> > +       metas/three
> > +       metas/two
> > +       EOF
> > +       git change list >actual &&
> > +       test_cmp expect actual
> > +'
> > +
> >   test_done
> > --
> > 2.37.3.947.g1b8ba4da7f.dirty

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 00/10] Add the Git Change command
  2022-10-04 15:19   ` Chris P
  2022-10-04 15:55     ` Chris P
@ 2022-10-04 15:57     ` Phillip Wood
  1 sibling, 0 replies; 66+ messages in thread
From: Phillip Wood @ 2022-10-04 15:57 UTC (permalink / raw)
  To: Chris P, Christophe Poucet; +Cc: Christophe Poucet via GitGitGadget, git

On 04/10/2022 16:19, Chris P wrote:
> Thanks a lot.
> 
> Is there something special I must do to get these scripts to work? The
> entire script fails for me, despite having build git-change.

If you do

make
cd t
./t9999-changes.sh -v -i [--root=/dev/shm]

it should run. You need to change into the t directory before running 
our tests and if you've just run "make git" before then it wont have 
created the scripts in bin-wrappers that the tests use. Are you able to 
run any of the other tests successfully?

Phillip

> On Tue, Oct 4, 2022 at 4:24 PM Phillip Wood <phillip.wood123@gmail.com> wrote:
>>
>> Hi Chris
>>
>> On 23/09/2022 19:55, Christophe Poucet via GitGitGadget wrote:
>>> I'm reviving the original git evolve work that was started by
>>> sxenos@google.com
>>> (https://public-inbox.org/git/20190215043105.163688-1-sxenos@google.com/)
>>>
>>> This work is intended to make it easier to deal with stacked changes.
>>>
>>> The following set of patches introduces the design doc on the evolve command
>>> as well as the basics of the git change command.
>>
>> Our test suite can be a little tricky to get started with and I was impatient to
>> check the basic functionality of these patches so I've written some simple
>> example tests for the change command and a couple of fixups to make them pass.
>>
>> Best Wishes
>>
>> Phillip
>>
>> ---- >8 ----
>>
>>   From a7c38d0f388e4d8a1f3debcc3069a7fb43084eda Mon Sep 17 00:00:00 2001
>> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>> Date: Tue, 4 Oct 2022 15:12:36 +0100
>> Subject: [PATCH 1/3] fixup! evolve: add support for writing metacommits
>>
>> ---
>>    metacommit.c | 2 +-
>>    1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/metacommit.c b/metacommit.c
>> index d2b859a4d3..8f970fa104 100644
>> --- a/metacommit.c
>> +++ b/metacommit.c
>> @@ -296,7 +296,7 @@ int record_metacommit_withresult(
>>           if (override_change) {
>>                   string_list_clear(changes, 0);
>>                   overridden_head = get_change_head(chtable, override_change);
>> -               if (!overridden_head) {
>> +               if (overridden_head) {
>>                           /* This is an existing change */
>>                           old_head = &overridden_head->head;
>>                           if (!force) {
>> --
>> 2.37.3.947.g1b8ba4da7f.dirty
>>
>>
>>   From cc7e8ba0b1a90268ced85d3f0c91aed49f2246d6 Mon Sep 17 00:00:00 2001
>> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>> Date: Tue, 4 Oct 2022 15:15:32 +0100
>> Subject: [PATCH 2/3] fixup! evolve: implement the git change command
>>
>> ---
>>    t/t9999-changes.sh | 126 +++++++++++++++++++++++++++++++++++++++++++++
>>    1 file changed, 126 insertions(+)
>>    create mode 100755 t/t9999-changes.sh
>>
>> diff --git a/t/t9999-changes.sh b/t/t9999-changes.sh
>> new file mode 100755
>> index 0000000000..9e58925b23
>> --- /dev/null
>> +++ b/t/t9999-changes.sh
>> @@ -0,0 +1,126 @@
>> +#!/bin/sh
>> +
>> +test_description='git change - low level meta-commit management'
>> +
>> +. ./test-lib.sh
>> +
>> +. "$TEST_DIRECTORY"/lib-rebase.sh
>> +
>> +test_expect_success 'setup commits and meta-commits' '
>> +       for c in one two three
>> +       do
>> +               test_commit $c &&
>> +               git change update --content $c >actual 2>err &&
>> +               echo "Created change metas/$c" >expect &&
>> +               test_cmp expect actual &&
>> +               test_must_be_empty err &&
>> +               test_cmp_rev refs/metas/$c $c || return 1
>> +       done
>> +'
>> +
>> +# Check a meta-commit has the correct parents Call with the object
>> +# name of the meta-commit followed by pairs of type and parent
>> +check_meta_commit () {
>> +       name=$1
>> +       shift
>> +       while test $# -gt 0
>> +       do
>> +               printf '%s %s\n' $1 $(git rev-parse --verify $2)
>> +               shift
>> +               shift
>> +       done | sort >expect
>> +       git cat-file commit $name >metacommit &&
>> +       # commit body should consist of parent-type
>> +           types="$(sed -n '/^$/ {
>> +                       :loop
>> +                       n
>> +                       s/^parent-type //
>> +                       p
>> +                       b loop
>> +                   }' metacommit)" &&
>> +       while read key value
>> +       do
>> +               # TODO: don't sort the first parent
>> +               if test "$key" = "parent"
>> +               then
>> +                       type="${types%% *}"
>> +                       test -n "$type" || return 1
>> +                       printf '%s %s\n' $type $value
>> +                       types="${types#?}"
>> +                       types="${types# }"
>> +               elif test "$key" = "tree"
>> +               then
>> +                       test_cmp_rev "$value" $EMPTY_TREE || return 1
>> +               elif test -z "$key"
>> +               then
>> +                       # only parse commit headers
>> +                       break
>> +               fi
>> +       done <metacommit >actual-unsorted &&
>> +       test -z "$types" &&
>> +       sort >actual <actual-unsorted &&
>> +       test_cmp expect actual
>> +}
>> +
>> +test_expect_success 'update meta-commits after rebase' '
>> +       (
>> +               set_fake_editor &&
>> +               FAKE_AMEND=edited &&
>> +               FAKE_LINES="reword 1 pick 2 fixup 3" &&
>> +               export FAKE_AMEND FAKE_LINES &&
>> +               git rebase -i --root
>> +       ) &&
>> +
>> +       # update meta-commits
>> +       git change update --replace tags/one --content HEAD~1 >out 2>err &&
>> +       echo "Updated change metas/one" >expect &&
>> +       test_cmp expect out &&
>> +       test_must_be_empty err &&
>> +       git change update --replace tags/two --content HEAD@{2} &&
>> +       oid=$(git rev-parse --verify metas/two) &&
>> +       git change update --replace HEAD@{2} --replace tags/three \
>> +               --content HEAD &&
>> +
>> +       # check meta-commits
>> +       check_meta_commit metas/one c HEAD~1 r tags/one &&
>> +       check_meta_commit $oid c HEAD@{2} r tags/two &&
>> +       # NB this checks that "git change update" uses the meta-commit ($oid)
>> +       #    corresponding to the replaces commit (HEAD@2 above) given on the
>> +       #    commandline.
>> +       check_meta_commit metas/two c HEAD r $oid r tags/three &&
>> +       check_meta_commit metas/three c HEAD r $oid r tags/three
>> +'
>> +
>> +reset_meta_commits () {
>> +    for c in one two three
>> +    do
>> +       echo "update refs/metas/$c refs/tags/$c^0"
>> +    done | git update-ref --stdin
>> +}
>> +
>> +test_expect_success 'override change name' '
>> +       # TODO: builtin/change.c expects --change to be the full refname,
>> +       #       ideally it would prepend refs/metas to the string given by the
>> +       #       user.
>> +       git change update --change refs/metas/another-one --content one &&
>> +       test_cmp_rev metas/another-one one
>> +'
>> +
>> +test_expect_success 'non-fast forward meta-commit update refused' '
>> +       test_must_fail git change update --change refs/metas/one --content two \
>> +               >out 2>err &&
>> +       echo "error: non-fast-forward update to ${SQ}refs/metas/one${SQ}" \
>> +               >expect &&
>> +       test_cmp expect err &&
>> +       test_must_be_empty out
>> +'
>> +
>> +test_expect_success 'forced non-fast forward update succeeds' '
>> +       git change update --change refs/metas/one --content two --force \
>> +               >out 2>err &&
>> +       echo "Updated change metas/one" >expect &&
>> +       test_cmp expect out &&
>> +       test_must_be_empty err
>> +'
>> +
>> +test_done
>> --
>> 2.37.3.947.g1b8ba4da7f.dirty
>>
>>
>>   From 7784f253fa799dd11fcbc81fe815fb387af52d97 Mon Sep 17 00:00:00 2001
>> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>> Date: Tue, 4 Oct 2022 15:16:05 +0100
>> Subject: [PATCH 3/3] fixup! evolve: add the git change list command
>>
>> ---
>>    builtin/change.c   | 16 +++++-----------
>>    t/t9999-changes.sh | 11 +++++++++++
>>    2 files changed, 16 insertions(+), 11 deletions(-)
>>
>> diff --git a/builtin/change.c b/builtin/change.c
>> index 07d029d82d..888ef648fa 100644
>> --- a/builtin/change.c
>> +++ b/builtin/change.c
>> @@ -34,9 +34,8 @@ static int change_list(int argc, const char **argv, const char* prefix)
>>                   OPT_END()
>>           };
>>           struct ref_filter filter;
>> -       /* TODO: See below
>>           struct ref_sorting *sorting;
>> -       struct string_list sorting_options = STRING_LIST_INIT_DUP; */
>> +       struct string_list sorting_options = STRING_LIST_INIT_DUP;
>>           struct ref_format format = REF_FORMAT_INIT;
>>           struct ref_array array;
>>           int i;
>> @@ -53,19 +52,15 @@ static int change_list(int argc, const char **argv, const char* prefix)
>>
>>           filter_refs(&array, &filter, FILTER_REFS_CHANGES);
>>
>> -       /* TODO: This causes a crash. It sets one of the atom_value handlers to
>> -        * something invalid, which causes a crash later when we call
>> -        * show_ref_array_item. Figure out why this happens and put back the sorting.
>> -        *
>> -        * sorting = ref_sorting_options(&sorting_options);
>> -        * ref_array_sort(sorting, &array); */
>> -
>>           if (!format.format)
>>                   format.format = "%(refname:lstrip=1)";
>>
>>           if (verify_ref_format(&format))
>>                   die(_("unable to parse format string"));
>>
>> +       sorting = ref_sorting_options(&sorting_options);
>> +       ref_array_sort(sorting, &array);
>> +
>>           for (i = 0; i < array.nr; i++) {
>>                   struct strbuf output = STRBUF_INIT;
>>                   struct strbuf err = STRBUF_INIT;
>> @@ -79,8 +74,7 @@ static int change_list(int argc, const char **argv, const char* prefix)
>>           }
>>
>>           ref_array_clear(&array);
>> -       /* TODO: see above
>> -       ref_sorting_release(sorting); */
>> +       ref_sorting_release(sorting);
>>
>>           return 0;
>>    }
>> diff --git a/t/t9999-changes.sh b/t/t9999-changes.sh
>> index 9e58925b23..9312eba86d 100755
>> --- a/t/t9999-changes.sh
>> +++ b/t/t9999-changes.sh
>> @@ -123,4 +123,15 @@ test_expect_success 'forced non-fast forward update succeeds' '
>>           test_must_be_empty err
>>    '
>>
>> +test_expect_success 'list changes' '
>> +       cat >expect <<-\EOF &&
>> +       metas/another-one
>> +       metas/one
>> +       metas/three
>> +       metas/two
>> +       EOF
>> +       git change list >actual &&
>> +       test_cmp expect actual
>> +'
>> +
>>    test_done
>> --
>> 2.37.3.947.g1b8ba4da7f.dirty

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 00/10] Add the Git Change command
  2022-10-04 15:55     ` Chris P
@ 2022-10-04 16:00       ` Phillip Wood
  0 siblings, 0 replies; 66+ messages in thread
From: Phillip Wood @ 2022-10-04 16:00 UTC (permalink / raw)
  To: Chris P, Christophe Poucet; +Cc: Christophe Poucet via GitGitGadget, git

On 04/10/2022 16:55, Chris P wrote:
> I got the tests to work, except the last one, that one fails.
> 
> How can I see the output of actual and expect?

If you run the test with -i then it will stop at the first failure and 
you can inspect the files in the directory 'trash 
directory.t9999-changes'. Running with -v (verbose) -x (shell tracing) 
is also useful when debugging.

Phillip

> On Tue, Oct 4, 2022 at 5:19 PM Chris P <christophe.poucet@gmail.com> wrote:
>>
>> Thanks a lot.
>>
>> Is there something special I must do to get these scripts to work? The
>> entire script fails for me, despite having build git-change.
>>
>> On Tue, Oct 4, 2022 at 4:24 PM Phillip Wood <phillip.wood123@gmail.com> wrote:
>>>
>>> Hi Chris
>>>
>>> On 23/09/2022 19:55, Christophe Poucet via GitGitGadget wrote:
>>>> I'm reviving the original git evolve work that was started by
>>>> sxenos@google.com
>>>> (https://public-inbox.org/git/20190215043105.163688-1-sxenos@google.com/)
>>>>
>>>> This work is intended to make it easier to deal with stacked changes.
>>>>
>>>> The following set of patches introduces the design doc on the evolve command
>>>> as well as the basics of the git change command.
>>>
>>> Our test suite can be a little tricky to get started with and I was impatient to
>>> check the basic functionality of these patches so I've written some simple
>>> example tests for the change command and a couple of fixups to make them pass.
>>>
>>> Best Wishes
>>>
>>> Phillip
>>>
>>> ---- >8 ----
>>>
>>>   From a7c38d0f388e4d8a1f3debcc3069a7fb43084eda Mon Sep 17 00:00:00 2001
>>> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>>> Date: Tue, 4 Oct 2022 15:12:36 +0100
>>> Subject: [PATCH 1/3] fixup! evolve: add support for writing metacommits
>>>
>>> ---
>>>    metacommit.c | 2 +-
>>>    1 file changed, 1 insertion(+), 1 deletion(-)
>>>
>>> diff --git a/metacommit.c b/metacommit.c
>>> index d2b859a4d3..8f970fa104 100644
>>> --- a/metacommit.c
>>> +++ b/metacommit.c
>>> @@ -296,7 +296,7 @@ int record_metacommit_withresult(
>>>           if (override_change) {
>>>                   string_list_clear(changes, 0);
>>>                   overridden_head = get_change_head(chtable, override_change);
>>> -               if (!overridden_head) {
>>> +               if (overridden_head) {
>>>                           /* This is an existing change */
>>>                           old_head = &overridden_head->head;
>>>                           if (!force) {
>>> --
>>> 2.37.3.947.g1b8ba4da7f.dirty
>>>
>>>
>>>   From cc7e8ba0b1a90268ced85d3f0c91aed49f2246d6 Mon Sep 17 00:00:00 2001
>>> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>>> Date: Tue, 4 Oct 2022 15:15:32 +0100
>>> Subject: [PATCH 2/3] fixup! evolve: implement the git change command
>>>
>>> ---
>>>    t/t9999-changes.sh | 126 +++++++++++++++++++++++++++++++++++++++++++++
>>>    1 file changed, 126 insertions(+)
>>>    create mode 100755 t/t9999-changes.sh
>>>
>>> diff --git a/t/t9999-changes.sh b/t/t9999-changes.sh
>>> new file mode 100755
>>> index 0000000000..9e58925b23
>>> --- /dev/null
>>> +++ b/t/t9999-changes.sh
>>> @@ -0,0 +1,126 @@
>>> +#!/bin/sh
>>> +
>>> +test_description='git change - low level meta-commit management'
>>> +
>>> +. ./test-lib.sh
>>> +
>>> +. "$TEST_DIRECTORY"/lib-rebase.sh
>>> +
>>> +test_expect_success 'setup commits and meta-commits' '
>>> +       for c in one two three
>>> +       do
>>> +               test_commit $c &&
>>> +               git change update --content $c >actual 2>err &&
>>> +               echo "Created change metas/$c" >expect &&
>>> +               test_cmp expect actual &&
>>> +               test_must_be_empty err &&
>>> +               test_cmp_rev refs/metas/$c $c || return 1
>>> +       done
>>> +'
>>> +
>>> +# Check a meta-commit has the correct parents Call with the object
>>> +# name of the meta-commit followed by pairs of type and parent
>>> +check_meta_commit () {
>>> +       name=$1
>>> +       shift
>>> +       while test $# -gt 0
>>> +       do
>>> +               printf '%s %s\n' $1 $(git rev-parse --verify $2)
>>> +               shift
>>> +               shift
>>> +       done | sort >expect
>>> +       git cat-file commit $name >metacommit &&
>>> +       # commit body should consist of parent-type
>>> +           types="$(sed -n '/^$/ {
>>> +                       :loop
>>> +                       n
>>> +                       s/^parent-type //
>>> +                       p
>>> +                       b loop
>>> +                   }' metacommit)" &&
>>> +       while read key value
>>> +       do
>>> +               # TODO: don't sort the first parent
>>> +               if test "$key" = "parent"
>>> +               then
>>> +                       type="${types%% *}"
>>> +                       test -n "$type" || return 1
>>> +                       printf '%s %s\n' $type $value
>>> +                       types="${types#?}"
>>> +                       types="${types# }"
>>> +               elif test "$key" = "tree"
>>> +               then
>>> +                       test_cmp_rev "$value" $EMPTY_TREE || return 1
>>> +               elif test -z "$key"
>>> +               then
>>> +                       # only parse commit headers
>>> +                       break
>>> +               fi
>>> +       done <metacommit >actual-unsorted &&
>>> +       test -z "$types" &&
>>> +       sort >actual <actual-unsorted &&
>>> +       test_cmp expect actual
>>> +}
>>> +
>>> +test_expect_success 'update meta-commits after rebase' '
>>> +       (
>>> +               set_fake_editor &&
>>> +               FAKE_AMEND=edited &&
>>> +               FAKE_LINES="reword 1 pick 2 fixup 3" &&
>>> +               export FAKE_AMEND FAKE_LINES &&
>>> +               git rebase -i --root
>>> +       ) &&
>>> +
>>> +       # update meta-commits
>>> +       git change update --replace tags/one --content HEAD~1 >out 2>err &&
>>> +       echo "Updated change metas/one" >expect &&
>>> +       test_cmp expect out &&
>>> +       test_must_be_empty err &&
>>> +       git change update --replace tags/two --content HEAD@{2} &&
>>> +       oid=$(git rev-parse --verify metas/two) &&
>>> +       git change update --replace HEAD@{2} --replace tags/three \
>>> +               --content HEAD &&
>>> +
>>> +       # check meta-commits
>>> +       check_meta_commit metas/one c HEAD~1 r tags/one &&
>>> +       check_meta_commit $oid c HEAD@{2} r tags/two &&
>>> +       # NB this checks that "git change update" uses the meta-commit ($oid)
>>> +       #    corresponding to the replaces commit (HEAD@2 above) given on the
>>> +       #    commandline.
>>> +       check_meta_commit metas/two c HEAD r $oid r tags/three &&
>>> +       check_meta_commit metas/three c HEAD r $oid r tags/three
>>> +'
>>> +
>>> +reset_meta_commits () {
>>> +    for c in one two three
>>> +    do
>>> +       echo "update refs/metas/$c refs/tags/$c^0"
>>> +    done | git update-ref --stdin
>>> +}
>>> +
>>> +test_expect_success 'override change name' '
>>> +       # TODO: builtin/change.c expects --change to be the full refname,
>>> +       #       ideally it would prepend refs/metas to the string given by the
>>> +       #       user.
>>> +       git change update --change refs/metas/another-one --content one &&
>>> +       test_cmp_rev metas/another-one one
>>> +'
>>> +
>>> +test_expect_success 'non-fast forward meta-commit update refused' '
>>> +       test_must_fail git change update --change refs/metas/one --content two \
>>> +               >out 2>err &&
>>> +       echo "error: non-fast-forward update to ${SQ}refs/metas/one${SQ}" \
>>> +               >expect &&
>>> +       test_cmp expect err &&
>>> +       test_must_be_empty out
>>> +'
>>> +
>>> +test_expect_success 'forced non-fast forward update succeeds' '
>>> +       git change update --change refs/metas/one --content two --force \
>>> +               >out 2>err &&
>>> +       echo "Updated change metas/one" >expect &&
>>> +       test_cmp expect out &&
>>> +       test_must_be_empty err
>>> +'
>>> +
>>> +test_done
>>> --
>>> 2.37.3.947.g1b8ba4da7f.dirty
>>>
>>>
>>>   From 7784f253fa799dd11fcbc81fe815fb387af52d97 Mon Sep 17 00:00:00 2001
>>> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>>> Date: Tue, 4 Oct 2022 15:16:05 +0100
>>> Subject: [PATCH 3/3] fixup! evolve: add the git change list command
>>>
>>> ---
>>>    builtin/change.c   | 16 +++++-----------
>>>    t/t9999-changes.sh | 11 +++++++++++
>>>    2 files changed, 16 insertions(+), 11 deletions(-)
>>>
>>> diff --git a/builtin/change.c b/builtin/change.c
>>> index 07d029d82d..888ef648fa 100644
>>> --- a/builtin/change.c
>>> +++ b/builtin/change.c
>>> @@ -34,9 +34,8 @@ static int change_list(int argc, const char **argv, const char* prefix)
>>>                   OPT_END()
>>>           };
>>>           struct ref_filter filter;
>>> -       /* TODO: See below
>>>           struct ref_sorting *sorting;
>>> -       struct string_list sorting_options = STRING_LIST_INIT_DUP; */
>>> +       struct string_list sorting_options = STRING_LIST_INIT_DUP;
>>>           struct ref_format format = REF_FORMAT_INIT;
>>>           struct ref_array array;
>>>           int i;
>>> @@ -53,19 +52,15 @@ static int change_list(int argc, const char **argv, const char* prefix)
>>>
>>>           filter_refs(&array, &filter, FILTER_REFS_CHANGES);
>>>
>>> -       /* TODO: This causes a crash. It sets one of the atom_value handlers to
>>> -        * something invalid, which causes a crash later when we call
>>> -        * show_ref_array_item. Figure out why this happens and put back the sorting.
>>> -        *
>>> -        * sorting = ref_sorting_options(&sorting_options);
>>> -        * ref_array_sort(sorting, &array); */
>>> -
>>>           if (!format.format)
>>>                   format.format = "%(refname:lstrip=1)";
>>>
>>>           if (verify_ref_format(&format))
>>>                   die(_("unable to parse format string"));
>>>
>>> +       sorting = ref_sorting_options(&sorting_options);
>>> +       ref_array_sort(sorting, &array);
>>> +
>>>           for (i = 0; i < array.nr; i++) {
>>>                   struct strbuf output = STRBUF_INIT;
>>>                   struct strbuf err = STRBUF_INIT;
>>> @@ -79,8 +74,7 @@ static int change_list(int argc, const char **argv, const char* prefix)
>>>           }
>>>
>>>           ref_array_clear(&array);
>>> -       /* TODO: see above
>>> -       ref_sorting_release(sorting); */
>>> +       ref_sorting_release(sorting);
>>>
>>>           return 0;
>>>    }
>>> diff --git a/t/t9999-changes.sh b/t/t9999-changes.sh
>>> index 9e58925b23..9312eba86d 100755
>>> --- a/t/t9999-changes.sh
>>> +++ b/t/t9999-changes.sh
>>> @@ -123,4 +123,15 @@ test_expect_success 'forced non-fast forward update succeeds' '
>>>           test_must_be_empty err
>>>    '
>>>
>>> +test_expect_success 'list changes' '
>>> +       cat >expect <<-\EOF &&
>>> +       metas/another-one
>>> +       metas/one
>>> +       metas/three
>>> +       metas/two
>>> +       EOF
>>> +       git change list >actual &&
>>> +       test_cmp expect actual
>>> +'
>>> +
>>>    test_done
>>> --
>>> 2.37.3.947.g1b8ba4da7f.dirty

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 06/10] evolve: add support for writing metacommits
  2022-09-28 14:27   ` Phillip Wood
@ 2022-10-05  9:40     ` Chris P
  2022-10-05 11:09       ` Phillip Wood
  0 siblings, 1 reply; 66+ messages in thread
From: Chris P @ 2022-10-05  9:40 UTC (permalink / raw)
  To: phillip.wood
  Cc: git, Ævar Arnfjörð Bjarmason, Christophe Poucet

+cc my work account due to a bug in gitgitgadget

>
> I think the code to parse and create metacommits (as well as the change
> table code) could quite happily live in the same file.

I am considering this, it would hide the change_table as that doesn't
need to be exposed (for now at least).  My only concern is that it
would make the commit-msg rather unwieldy.

>
> > diff --git a/metacommit.c b/metacommit.c
> > new file mode 100644
> > index 00000000000..d2b859a4d3b
> > --- /dev/null
> > +++ b/metacommit.c
> > @@ -0,0 +1,404 @@
> > +#include "cache.h"
> > +#include "metacommit.h"
> > +#include "commit.h"
> > +#include "change-table.h"
> > +#include "refs.h"
> > +
> > +void init_metacommit_data(struct metacommit_data *state)
> > +{
> > +     memset(state, 0, sizeof(*state));
> > +}
> We'd normally use an initializer macro instead
>
>         #define METACOMMIT_DATA_INIT = { 0 }

Thanks, done.

>
> > +void clear_metacommit_data(struct metacommit_data *state)
> > +{
> > +     oid_array_clear(&state->replace);
> > +     oid_array_clear(&state->origin);
> > +}
> > +
> > +static void compute_default_change_name(struct commit *initial_commit,
> > +     struct strbuf* result)
> > +{
> > +     struct strbuf default_name;
>
> The canonical way to initialize an strbuf that is not on the heap is
>
>         struct strbuf buf = STRBUF_INIT;

Done.

>
> > +     const char *buffer;
> > +     const char *subject;
> > +     const char *eol;
> > +     int len;
> > +     strbuf_init(&default_name, 0);
> > +     buffer = get_commit_buffer(initial_commit, NULL);
> > +     find_commit_subject(buffer, &subject);
> > +     eol = strchrnul(subject, '\n');
> > +     for (len = 0;subject < eol && len < 10; ++subject, ++len) {
>
> There's a space missing after the first ';'. We prefer post-increments
> to pre-increments unless the pre-increment is significant.

Done and done :)

>
> > +             char next = *subject;
> > +             if (isspace(next))
> > +                     continue;
> > +
> > +             strbuf_addch(&default_name, next);
> > +     }
> > +     sanitize_refname_component(default_name.buf, result);
>
> I suspect we need to call unuse_commit_buffer(initial_commit) here.

Oh interesting, yes.

>
> > +}
> > +
> > +/**
> > + * Computes a change name for a change rooted at the given initial commit. Good
> > + * change names should be memorable, unique, and easy to type. They are not
> > + * required to match the commit comment.
> > + */
> > +static void compute_change_name(struct commit *initial_commit, struct strbuf* result)
> > +{
> > +     struct strbuf default_name;
> > +     struct object_id unused;
> > +
> > +     strbuf_init(&default_name, 0);
> > +     if (initial_commit)
> > +             compute_default_change_name(initial_commit, &default_name);
> > +     else
> > +             strbuf_addstr(&default_name, "change");
>
> What does it mean to call this function with initial_commit == NULL?

I don't know to be honest, the call site always seems to pass one in.
Changed it to BUG.

>
> > +     strbuf_addstr(result, "refs/metas/");
> > +     strbuf_addbuf(result, &default_name);
> > +     /* If there is already a change of this name, append a suffix */
> > +     if (!read_ref(result->buf, &unused)) {
> > +             int suffix = 2;
> > +             int original_length = result->len;
>
> This is one of many places where we have a size_t len or nr member and
> assign it to an int. I think it would be clearer to use a size_t instead
> to avoid adding any more signed<->unsigned conversions.

Done, I had to leave one place because it did while(i >= 0);

>
> > +
> > +             while (1) {
> > +                     strbuf_addf(result, "%d", suffix);
> > +                     if (read_ref(result->buf, &unused))
> > +                             break;
> > +                     strbuf_remove(result, original_length, result->len - original_length);
> > +                     ++suffix;
> > +             }
> > +     }
> > +
> > +     strbuf_release(&default_name);
> > +}
> > +
> > +struct resolve_metacommit_callback_data
>
> While there are some structs with a _callback_data suffix in the code
> base, it is far more common to use _context and name any corresponding
> variables ctx.

Done.

>
> > +{
> > +     struct change_table* active_changes;
> > +     struct string_list *changes;
> > +     struct oid_array *heads;
> > +};
> > +
> > +static int resolve_metacommit_callback(const char *refname, void *cb_data)
> > +{
> > +     struct resolve_metacommit_callback_data *data = (struct resolve_metacommit_callback_data *)cb_data;
>
> We don't use redundant casts such as this.

Thanks :)

>
> > +     struct change_head *chhead;
> > +
> > +     chhead = get_change_head(data->active_changes, refname);
>
> This is really a comment on the previous patch but are there uses of
> for_each_change_referencing() for which just the refname is sufficient?
> It might be more convenient to pass the change head into the callback as
> well.

I'm not sure, will investigate post-squash

>
> > +
> > +     if (data->changes)
> > +             string_list_append(data->changes, refname)->util = &(chhead->head);
>
> We don't use redundant parentheses such as this (and this patch does not
> use them consistently)

Done.

>
> > +     if (data->heads)
> > +             oid_array_append(data->heads, &(chhead->head));
> > +
> > +     return 0;
> > +}
> > +
> > +/**
> > + * Produces the final form of a metacommit based on the current change refs.
> > + */
> > +static void resolve_metacommit(
> > +     struct repository* repo,
> > +     struct change_table* active_changes,
> > +     const struct metacommit_data *to_resolve,
>
> [testing my understanding] This is the metacommit we want to update

Maybe you can help me find a bug.  If you run `git-change update`
twice without changing commits, it prints that it created a second
one, but then if you `git-change list` it doesn't show that last one
because it doesn't create an extra one if there's already a change
pointing at HEAD.

Also, thanks for all the comments, it's helping my understanding too.
In general do you want all these comments added to the code?

>
> > +     struct metacommit_data *resolved_output,
>
> This is the updated metacommit returned to the user
>
> > +     struct string_list *to_advance,
>
> Is also an output? It ends up as a list of refname to change head mappings

Yes, this is consumed in the change.c command.  I was considering
making this a strintmap that maps to an enum, because we need a
tristate (updated, created, untouched). But unfortunately the oids
assigned to `->util` are consumed later in the function.

>
> > +     int allow_append)
> > +{
> > +     int i;
> > +     int len = to_resolve->replace.nr;
> > +     struct resolve_metacommit_callback_data cbdata;
>
> This would be a good place to a designated initializer.
>
>         struct resolve_metacommit_context ctx = {
>                 .active_changes = active_changes,
>                 .changes = to_advance,
>                 .heads = &resolved_output->replace
>         };

I'm learning new C :D

>
> > +     int old_change_list_length = to_advance->nr;
> > +     struct commit* content;
> > +
> > +     oidcpy(&resolved_output->content, &to_resolve->content);
> > +
> > +     /* First look for changes that point to any of the replacement edges in the
> > +      * metacommit. These will be the changes that get advanced by this
> > +      * metacommit. */
>
> Style: '/*' & '*/' should be on their own lines.

Done

>
> > +     resolved_output->abandoned = to_resolve->abandoned;
> > +     cbdata.active_changes = active_changes;
> > +     cbdata.changes = to_advance;
> > +     cbdata.heads = &(resolved_output->replace);
> > +
> > +     if (allow_append) {
> > +             for (i = 0; i < len; i++) {
> > +                     int old_number = resolved_output->replace.nr;
> > +                     for_each_change_referencing(active_changes, &(to_resolve->replace.oid[i]),
> > +                             resolve_metacommit_callback, &cbdata);
> > +                     /* If no changes were found, use the unresolved value. */
> > +                     if (old_number == resolved_output->replace.nr)
> > +                             oid_array_append(&(resolved_output->replace), &(to_resolve->replace.oid[i]));
>
> We see if there are any refs under refs/metas/ which point to
> 'to_resolve' or its content and if there are we add those refs and the
> corresponding change head to 'to_advance'. If we don't find any refs
> then we copy the replace oid from 'to_resolve' to 'resolved_output'
>
> If allow_append is false then we ignore all the replace oids in 'to_resolve'
>
> > +             }
> > +     }
> > +
> > +     cbdata.changes = NULL;
> > +     cbdata.heads = &(resolved_output->origin);
> > +
> > +     len = to_resolve->origin.nr;
> > +     for (i = 0; i < len; i++) {
> > +             int old_number = resolved_output->origin.nr;
> > +             for_each_change_referencing(active_changes, &(to_resolve->origin.oid[i]),
> > +                     resolve_metacommit_callback, &cbdata);
> > +             if (old_number == resolved_output->origin.nr)
> > +                     oid_array_append(&(resolved_output->origin), &(to_resolve->origin.oid[i]));
> > +     }
>
> This is copying the origin oids in the same way as we copied the replace
> oids above.
>
> > +     /* If no changes were advanced by this metacommit, we'll need to create a new
> > +      * one. */
> > +     if (to_advance->nr == old_change_list_length) {
> > +             struct strbuf change_name;
> > +
> > +             strbuf_init(&change_name, 80);
> > +             content = lookup_commit_reference_gently(repo, &(to_resolve->content), 1);
> > +
> > +             compute_change_name(content, &change_name);
> > +             string_list_append(to_advance, change_name.buf);
> > +             strbuf_release(&change_name);
> > +     }
> > +}
> > +
> > +static void lookup_commits(
> > +     struct repository *repo,
> > +     struct oid_array *to_lookup,
> > +     struct commit_list **result)
> > +{
> > +     int i = to_lookup->nr;
> > +
> > +     while (--i >= 0) {
> > +             struct object_id *next = &(to_lookup->oid[i]);
> > +             struct commit *commit = lookup_commit_reference_gently(repo, next, 1);
> > +             commit_list_insert(commit, result);
> > +     }
>
> We walk backwards because commit_list_insert prepends to the list - good.
>
> > +}
> > +
> > +#define PARENT_TYPE_PREFIX "parent-type "
> > +
> > +/**
> > + * Creates a new metacommit object with the given content. Writes the object
> > + * id of the newly-created commit to result.
> > + */
> > +int write_metacommit(struct repository *repo, struct metacommit_data *state,
> > +     struct object_id *result)
> > +{
> > +     struct commit_list *parents = NULL;
> > +     struct strbuf comment;
> > +     int i;
> > +     struct commit *content;
> > +
> > +     strbuf_init(&comment, strlen(PARENT_TYPE_PREFIX)
> > +             + 1 + 2 * (state->origin.nr + state->replace.nr));
> > +     lookup_commits(repo, &state->origin, &parents);
> > +     lookup_commits(repo, &state->replace, &parents);
> > +     content = lookup_commit_reference_gently(repo, &state->content, 1);
> > +     if (!content) {
> > +             strbuf_release(&comment);
> > +             free_commit_list(parents);
> > +             return -1;
> > +     }
> > +     commit_list_insert(content, &parents);
> > +
> > +     strbuf_addstr(&comment, PARENT_TYPE_PREFIX);
> > +     strbuf_addstr(&comment, state->abandoned ? "a" : "c");
> > +     for (i = 0; i < state->replace.nr; i++)
> > +             strbuf_addstr(&comment, " r");
> > +
> > +     for (i = 0; i < state->origin.nr; i++)
> > +             strbuf_addstr(&comment, " o"); > +      /* The parents list will be freed by this call. */
> > +     commit_tree(comment.buf, comment.len, repo->hash_algo->empty_tree, parents,
> > +             result, NULL, NULL);
>
> It would be relatively easy to use commit_tree_extended() with
> extra_headers so that we create a commit with a "parent-type" header
> rather than abusing the commit message.
>
>         struct commit_extra_header extra = { .key = "parent-type" };
>
>         /* build header value in strbuf */
>
>         extra.value = buf.buf;
>         extra.len = buf.len;
>         commit_tree_extended("", 0, repo->hash_algo->empty_tree,
>                              parents, result, NULL, NULL, NULL,
>                              &extra);

Sounds like a potentially good idea, to avoid that people accidentally
create metacommits that aren't real.
What would that look like on the parsing side as well as the test-setup?

>
> > +
> > +     strbuf_release(&comment);
> > +     return 0;
> > +}
> > +
> > +/**
> > + * Returns true iff the given metacommit is abandoned, has one or more origin
> > + * parents, or has one or more replacement parents.
> > + */
> > +static int is_nontrivial_metacommit(struct metacommit_data *state)
> > +{
> > +     return state->replace.nr || state->origin.nr || state->abandoned;
> > +}
> > +
> > +/*
> > + * Records the relationships described by the given metacommit in the
> > + * repository.
> > + *
> > + * If override_change is NULL (the default), an attempt will be made
> > + * to append to existing changes wherever possible instead of creating new ones.
> > + * If override_change is non-null, only the given change ref will be updated.
>
> So override_head is the refname of an existing change?

Yes, this comes from the commandline with the option of '-g' (Which
unfortunately is not documented).
>
> > + * options is a bitwise combination of the UPDATE_OPTION_* flags.
> > + */
> > +int record_metacommit(
> > +     struct repository *repo,
> > +     const struct metacommit_data *metacommit, const char *override_change,
> > +     int options, struct strbuf *err)
> > +{
> > +             struct change_table chtable;
> > +             struct string_list changes;
> > +             int result;
> > +
> > +             change_table_init(&chtable);
> > +             change_table_add_all_visible(&chtable, repo);
> > +             string_list_init_dup(&changes);
> > +
> > +             result = record_metacommit_withresult(repo, &chtable, metacommit,
> > +                     override_change, options, err, &changes);
> > +
> > +             string_list_clear(&changes, 0);
> > +             change_table_clear(&chtable);
> > +             return result;
> > +}
> > +
> > +/*
> > + * Records the relationships described by the given metacommit in the
> > + * repository.
> > + *
> > + * If override_change is NULL (the default), an attempt will be made
> > + * to append to existing changes wherever possible instead of creating new ones.
> > + * If override_change is non-null, only the given change ref will be updated.
> > + *
> > + * The changes list is filled in with the list of change refs that were updated,
> > + * with the util pointers pointing to the old object IDS for those changes.
> > + * The object ID pointers all point to objects owned by the change_table and
> > + * will go out of scope when the change_table is destroyed.
>
> That potentially sounds like an invitation to create use after free bugs
> unless we're careful. Does this function need to be public?

So the change command uses the changes list to determine what to print
to the output.  It looks whether it's a new change or a created change
based on whether the oid is null or not.
We could return a strintmap instead that points at status per change.
I tried that approach but unfortunately, `changes` is used later in
this function. I could still consider a strintmap, it would just be
that there's some duplicative storage of information until the
function exits. LMKWYT.

>
> > + *
> > + * options is a bitwise combination of the UPDATE_OPTION_* flags.
> > + */
> > +int record_metacommit_withresult(
> > +     struct repository *repo,
> > +     struct change_table *chtable,
> > +     const struct metacommit_data *metacommit,
> > +     const char *override_change,
> > +     int options, struct strbuf *err,
> > +     struct string_list *changes)
> > +{
> > +     static const char *msg = "updating change";
> > +     struct metacommit_data resolved_metacommit;
> > +     struct object_id commit_target;
> > +     struct ref_transaction *transaction = NULL;
> > +     struct change_head *overridden_head;
> > +     const struct object_id *old_head;
> > +
> > +     int i;
> > +     int ret = 0;
> > +     int force = (options & UPDATE_OPTION_FORCE);
> > +
> > +     init_metacommit_data(&resolved_metacommit);
> > +
> > +     resolve_metacommit(repo, chtable, metacommit, &resolved_metacommit, changes,
> > +             (options & UPDATE_OPTION_NOAPPEND) == 0);
> > +
> > +     if (override_change) {
> > +             string_list_clear(changes, 0);
> > +             overridden_head = get_change_head(chtable, override_change);
> > +             if (!overridden_head) {
>
> We enter this branch if overridden_head is NULL

Good catch!

>
> > +                     /* This is an existing change */
> > +                     old_head = &overridden_head->head;
>
> Here we de-reference overridden_head which is NULL

Yep.

>
> > +                     if (!force) {
> > +                             if (!oid_array_readonly_contains(&(resolved_metacommit.replace),
> > +                                     &overridden_head->head)) {
> > +                                     /* Attempted non-fast-forward change */
> > +                                     strbuf_addf(err, _("non-fast-forward update to '%s'"),
> > +                                             override_change);
> > +                                     ret = -1;
> > +                                     goto cleanup;
> > +                             }
> > +                     }
> > +             } else
>
> Style: if one branch of an if statement requires braces then all
> branches should have braces.

Thanks, fixed.

>
> > +                     /* ...then this is a newly-created change */
> > +                     old_head = null_oid();
> > +
> > +             /* The expected "current" head of the change is stored in the util
> > +              * pointer. */
> > +             string_list_append(changes, override_change)->util = (void*)old_head;
>
> No need to cast here

Actually it's required because old_head is a const*
>
> > +     }
> > +
> > +     if (is_nontrivial_metacommit(&resolved_metacommit)) {
> > +             /* If there are any origin or replacement parents, create a new metacommit
> > +              * object. */
> > +             if (write_metacommit(repo, &resolved_metacommit, &commit_target) < 0) {
> > +                     ret = -1;
> > +                     goto cleanup;
> > +             }
> > +     } else
> > +             /**
> > +              * If the metacommit would only contain a content commit, point to the
> > +              * commit itself rather than creating a trivial metacommit.
> > +              */
> > +             oidcpy(&commit_target, &(resolved_metacommit.content));
>
> Oh, is this optimization why we don't insist on metacommits but also
> allow ordinary commits to be added to the change table?

Yes.

> > +
> > +extern int record_metacommit_withresult(
> > +     struct repository *repo,
> > +     struct change_table *chtable,
> > +     const struct metacommit_data *metacommit,
> > +     const char *override_change,
> > +     int options,
> > +     struct strbuf *err,
> > +     struct string_list *changes);
>
> Does this need to be public? i.e. why would one call this rather than
> record_metacommit()?

Turns out the change command needs the `changes` string_list.  I've
made this one private and extended the public one with the changes
list.
I could potentially still return a strintmap but I'm on the fence.

>
> > +extern void modify_change(struct repository *repo,
> > +     const struct object_id *old_commit, const struct object_id *new_commit,
> > +     struct strbuf *err);
> > +
> > +extern int write_metacommit(struct repository *repo, struct metacommit_data *state,
> > +     struct object_id *result);
>
> The documentation for the flags is very welcome but this header could to
> with the api being documented as well.
>
> Best Wishes
>
> Phillip



>
> > +extern void modify_change(struct repository *repo,
> > +     const struct object_id *old_commit, const struct object_id *new_commit,
> > +     struct strbuf *err);
> > +
> > +extern int write_metacommit(struct repository *repo, struct metacommit_data *state,
> > +     struct object_id *result);
>
> The documentation for the flags is very welcome but this header could to
> with the api being documented as well.
>
> Best Wishes
>
> Phillip

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 06/10] evolve: add support for writing metacommits
  2022-10-05  9:40     ` Chris P
@ 2022-10-05 11:09       ` Phillip Wood
  0 siblings, 0 replies; 66+ messages in thread
From: Phillip Wood @ 2022-10-05 11:09 UTC (permalink / raw)
  To: Chris P, phillip.wood
  Cc: git, Ævar Arnfjörð Bjarmason, Christophe Poucet

Hi Chris

On 05/10/2022 10:40, Chris P wrote:
>>> +/**
>>> + * Produces the final form of a metacommit based on the current change refs.
>>> + */
>>> +static void resolve_metacommit(
>>> +     struct repository* repo,
>>> +     struct change_table* active_changes,
>>> +     const struct metacommit_data *to_resolve,
>>
>> [testing my understanding] This is the metacommit we want to update
> 
> Maybe you can help me find a bug.  If you run `git-change update`
> twice without changing commits, it prints that it created a second
> one, but then if you `git-change list` it doesn't show that last one
> because it doesn't create an extra one if there's already a change
> pointing at HEAD.

That's something I thought that we should add a test for but didn't get 
round to. I'll have a look and get back to you

> Also, thanks for all the comments, it's helping my understanding too.
> In general do you want all these comments added to the code?

Not unless you think the code needs them (from what I remember it is 
already fairly well commented), they're just me thinking out loud as I 
read the code.

I'm going to be offline for the rest of the day, I'll catch up with the 
rest of your comments tomorrow or Friday.

Phillip


^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH 07/10] evolve: implement the git change command
  2022-09-26  8:25   ` Ævar Arnfjörð Bjarmason
@ 2022-10-05 12:30     ` Chris P
  0 siblings, 0 replies; 66+ messages in thread
From: Chris P @ 2022-10-05 12:30 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Stefan Xenos via GitGitGadget, git, Stefan Xenos

On Mon, Sep 26, 2022 at 10:35 AM Ævar Arnfjörð Bjarmason
<avarab@gmail.com> wrote:
>
>
> On Fri, Sep 23 2022, Stefan Xenos via GitGitGadget wrote:
>
> > From: Stefan Xenos <sxenos@google.com>
>
> > +static const char * const builtin_change_usage[] = {
> > +     N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
> > +     NULL
> > +};
> > +
> > +static const char * const builtin_update_usage[] = {
> > +     N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
> > +     NULL
> > +};
>
> This (and the corresponding later *.txt version) should indent the
> overly long -h line, probably after "[--replace <treeish>...]".
>
> > +struct update_state {
> > +     int options;
>
> I think this should be an enum in your earlier 06/10. Makes things more
>
> > +             die(_("Failed to resolve '%s' as a valid revision."), committish);
>
> This and other error should start with a lower-case letter, see
> CodingGuidelines on errors.

Done.

>
> > [...]
> > +             die(_("Could not parse object '%s'."), committish);
>
> Ditto etc.
>
> > +     int i;
> > +     for (i = 0; i < commitsish_list->nr; i++) {
>
> A string_list uses a size_t for a nr, not int, so lets make that "size_t
> i".
>
> This both makes things more obvious, and helps some compilers spot
> unsigned v.s. signed issues.

Done.

>
>
> > +     int i;
>
> ditto size_t above...
>
> > +     for (i = 0; i < changes.nr; i++) {
>
> ...for this iteration...

Obsolete, moved to using for_each_string_list_item

>
> > +             struct string_list_item *it = &changes.items[i];
>
> ...but actually don't you just want for_each_string_list_item() instead?
>
> > +             if (it->util)
> > +                     fprintf(stdout, N_("Updated change %s\n"), name);
> > +             else
> > +                     fprintf(stdout, N_("Created change %s\n"), name);
>
> The use of N_() here is wrong, you should use _(), N_() just marks
> things for translation, but doesn't use it.
>

Done.

> We also tend to try to avoid adding \n in translations needlessly. And
> since you're printing to stdout this can be:
>
>
>         if (...)
>                 printf(_("Updated change %s"), name);
>         ...
>         putchar('\n')

Done

>
>
> > +     }
> > +
> > +     string_list_clear(&changes, 0);
> > +     change_table_clear(&chtable);
> > +     clear_metacommit_data(&metacommit);
> > +
> > +     return ret;
> > +}
> > +
> > +static int change_update(int argc, const char **argv, const char* prefix)
> > +{
> > +     int result;
> > +     int force = 0;
> > +     int newchange = 0;
> > +     struct strbuf err = STRBUF_INIT;
> > +     struct update_state state;
> > +     struct option options[] = {
> > +             { OPTION_CALLBACK, 'r', "replace", &state, N_("commit"),
> > +                     N_("marks the given commit as being obsolete"),
> > +                     0, update_option_parse_replace },
> > +             { OPTION_CALLBACK, 'o', "origin", &state, N_("commit"),
> > +                     N_("marks the given commit as being the origin of this commit"),
> > +                     0, update_option_parse_origin },
> > +             OPT_BOOL('F', "force", &force,
> > +                     N_("overwrite an existing change of the same name")),
> > +             OPT_STRING('c', "content", &state.content, N_("commit"),
> > +                              N_("identifies the new content commit for the change")),
> > +             OPT_STRING('g', "change", &state.change, N_("commit"),
> > +                              N_("name of the change to update")),
> > +             OPT_BOOL('n', "new", &newchange,
> > +                     N_("create a new change - do not append to any existing change")),
> > +             OPT_END()
> > +     };
> > +
> > +     init_update_state(&state);
> > +
> > +     argc = parse_options(argc, argv, prefix, options, builtin_update_usage, 0);
> > +
> > +     if (force) state.options |= UPDATE_OPTION_FORCE;
> > +     if (newchange) state.options |= UPDATE_OPTION_NOAPPEND;
>
> Just use OPT_SET_INT_F() and skip the indirection thorugh OPT_BOOL(),
> that macro itself is a thin wrapper for OPT_SET_INT_F().
>
> I.e. you can drop these "force" and "newchange" variables, andjust set
> your state.options directly.

Done.

>
> > +int cmd_change(int argc, const char **argv, const char *prefix)
> > +{
> > +     /* No options permitted before subcommand currently */
> > +     struct option options[] = {
> > +             OPT_END()
> > +     };
> > +     int result = 1;
> > +
> > +     argc = parse_options(argc, argv, prefix, options, builtin_change_usage,
> > +             PARSE_OPT_STOP_AT_NON_OPTION);
> > +
> > +     if (argc < 1)
> > +             usage_with_options(builtin_change_usage, options);
> > +     else if (!strcmp(argv[0], "update"))
> > +             result = change_update(argc, argv, prefix);
> > +     else {
> > +             error(_("Unknown subcommand: %s"), argv[0]);
> > +             usage_with_options(builtin_change_usage, options);
> > +     }
>
> This was presumably written before the recent OPT_SUBCOMMAND(), and
> should instead use that API.

Done, thanks!

^ permalink raw reply	[flat|nested] 66+ messages in thread

* [PATCH v2 00/10] RFC: Git Evolve / Change
  2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
                   ` (11 preceding siblings ...)
  2022-10-04 14:24 ` Phillip Wood
@ 2022-10-05 14:59 ` Christophe Poucet via GitGitGadget
  2022-10-05 14:59   ` [PATCH v2 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
                     ` (10 more replies)
  12 siblings, 11 replies; 66+ messages in thread
From: Christophe Poucet via GitGitGadget @ 2022-10-05 14:59 UTC (permalink / raw)
  To: git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet

I'm reviving the original git evolve work that was started by
sxenos@google.com
(https://public-inbox.org/git/20190215043105.163688-1-sxenos@google.com/)

This work is intended to make it easier to deal with stacked changes.

The following set of patches introduces the design doc on the evolve command
as well as the basics of the git change command.

Chris Poucet (5):
  sha1-array: implement oid_array_readonly_contains
  ref-filter: add the metas namespace to ref-filter
  evolve: add delete command
  evolve: add documentation for `git change`
  evolve: add tests for the git-change command

Stefan Xenos (5):
  technical doc: add a design doc for the evolve command
  evolve: add support for parsing metacommits
  evolve: add the change-table structure
  evolve: add support for writing metacommits
  evolve: implement the git change command

 .gitignore                         |    1 +
 Documentation/git-change.txt       |   55 ++
 Documentation/technical/evolve.txt | 1070 ++++++++++++++++++++++++++++
 Makefile                           |    4 +
 builtin.h                          |    1 +
 builtin/change.c                   |  330 +++++++++
 change-table.c                     |  164 +++++
 change-table.h                     |  122 ++++
 commit.c                           |   13 +
 commit.h                           |    5 +
 git.c                              |    1 +
 metacommit-parser.c                |   97 +++
 metacommit-parser.h                |   19 +
 metacommit.c                       |  410 +++++++++++
 metacommit.h                       |   75 ++
 oid-array.c                        |   12 +
 oid-array.h                        |    7 +
 ref-filter.c                       |   10 +-
 ref-filter.h                       |   10 +-
 t/helper/test-oid-array.c          |    6 +
 t/t0064-oid-array.sh               |   22 +
 t/t9990-changes.sh                 |  148 ++++
 22 files changed, 2577 insertions(+), 5 deletions(-)
 create mode 100644 Documentation/git-change.txt
 create mode 100644 Documentation/technical/evolve.txt
 create mode 100644 builtin/change.c
 create mode 100644 change-table.c
 create mode 100644 change-table.h
 create mode 100644 metacommit-parser.c
 create mode 100644 metacommit-parser.h
 create mode 100644 metacommit.c
 create mode 100644 metacommit.h
 create mode 100755 t/t9990-changes.sh


base-commit: 3dcec76d9df911ed8321007b1d197c1a206dc164
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1356%2Fpoucet%2Fevolve-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1356/poucet/evolve-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1356

Range-diff vs v1:

  1:  a0cf68f8ba2 !  1:  a5eb9325419 technical doc: add a design doc for the evolve command
     @@ Documentation/technical/evolve.txt (new)
      +rebase. You can think of rebase -i as a top-down approach and the evolve command
      +as the bottom-up approach to the same problem.
      +
     ++Revup amend (https://github.com/Skydio/revup/blob/main/docs/amend.md)
     ++allows insertion of cached changes into any commit in
     ++the current history, and then reapplies the rest of history on top of
     ++those changes. It uses a "git apply --cached" engine under the hood so
     ++doesn't touch the working directory (although it will soon use the new
     ++git merge-tree). When paired with "revup upload" which creates and
     ++pushes multiple branches in the background for you, its possible to
     ++work on a "graph" of changes on a single branch linearly, then have
     ++the true graph structure created at upload time.
     ++
     ++git-revise (https://github.com/mystor/git-revise) does some very
     ++similar things except it uses "git merge-file" combined with manually
     ++merging the resulting trees. git branchstack
     ++(https://github.com/krobelus/git-branchstack) can also create branches
     ++in the background with the same mechanism.
     ++
     ++These tools don't store any external state, but as such also don't
     ++provide any specific collaboration mechanism for individual changes.
     ++
      +Several patch queue managers have been built on top of git (such as topgit,
      +stgit, and quilt). They address the same user need. However they also rely on
      +state managed outside git that needs to be kept in sync. Such state can be
  2:  84588312c1d =  2:  ed5106d6080 sha1-array: implement oid_array_readonly_contains
  3:  54e559967df !  3:  c59066ebc10 ref-filter: add the metas namespace to ref-filter
     @@ ref-filter.c: int filter_refs(struct ref_array *array, struct ref_filter *filter
      
       ## ref-filter.h ##
      @@
     + #define FILTER_REFS_TAGS           0x0002
       #define FILTER_REFS_BRANCHES       0x0004
       #define FILTER_REFS_REMOTES        0x0008
     - #define FILTER_REFS_OTHERS         0x0010
     -+#define FILTER_REFS_CHANGES        0x0040
     +-#define FILTER_REFS_OTHERS         0x0010
     ++#define FILTER_REFS_CHANGES        0x0010
     ++#define FILTER_REFS_OTHERS         0x0040
       #define FILTER_REFS_ALL            (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
      -				    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
      +				    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS | \
  4:  2e9a4a9bd81 !  4:  408941e7400 evolve: add support for parsing metacommits
     @@ Makefile: LIB_OBJS += merge-ort.o
       LIB_OBJS += name-hash.o
       LIB_OBJS += negotiator/default.o
      
     + ## commit.c ##
     +@@ commit.c: struct commit_list *reverse_commit_list(struct commit_list *list)
     + 	return next;
     + }
     + 
     ++struct commit *get_commit_by_index(struct commit_list *to_search, int index)
     ++{
     ++	while (to_search && index) {
     ++		to_search = to_search->next;
     ++		index--;
     ++	}
     ++
     ++	if (!to_search)
     ++		return NULL;
     ++
     ++	return to_search->item;
     ++}
     ++
     + void free_commit_list(struct commit_list *list)
     + {
     + 	while (list)
     +
     + ## commit.h ##
     +@@ commit.h: struct commit_list *copy_commit_list(struct commit_list *list);
     + /* Modify list in-place to reverse it, returning new head; list will be tail */
     + struct commit_list *reverse_commit_list(struct commit_list *list);
     + 
     ++/* Returns the commit at `index` or NULL if the index exceeds the `to_search`
     ++ * list */
     ++struct commit *get_commit_by_index(struct commit_list *to_search, int index);
     ++
     + void free_commit_list(struct commit_list *list);
     + 
     ++
     + struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
     + 
     + int has_non_ascii(const char *text);
     +
       ## metacommit-parser.c (new) ##
      @@
      +#include "cache.h"
     @@ metacommit-parser.c (new)
      +	return NULL;
      +}
      +
     -+static struct commit *get_commit_by_index(struct commit_list *to_search, int index)
     -+{
     -+	while (to_search && index) {
     -+		to_search = to_search->next;
     -+		index--;
     -+	}
     -+
     -+	if (!to_search)
     -+		return NULL;
     -+
     -+	return to_search->item;
     -+}
     -+
      +/*
      + * Writes the index of the content parent to "result". Returns the metacommit
      + * type. See the METACOMMIT_TYPE_* constants.
      + */
     -+static int index_of_content_commit(const char *buffer, int *result)
     ++static enum metacommit_type index_of_content_commit(const char *buffer, int *result)
      +{
      +	int index = 0;
      +	int ret = METACOMMIT_TYPE_NONE;
     @@ metacommit-parser.c (new)
      +		char next = *parent_types;
      +		if (next == ' ' || parent_types >= end) {
      +			if (enum_length == 1) {
     -+				char first_char_in_enum = *enum_start;
     -+				if (first_char_in_enum == 'c') {
     ++				char type = *enum_start;
     ++				if (type == 'c') {
      +					ret = METACOMMIT_TYPE_NORMAL;
      +					break;
      +				}
     -+				if (first_char_in_enum == 'a') {
     ++				if (type == 'a') {
      +					ret = METACOMMIT_TYPE_ABANDONED;
      +					break;
      +				}
     @@ metacommit-parser.c (new)
      + * Writes the content parent's object id to "content".
      + * Returns the metacommit type. See the METACOMMIT_TYPE_* constants.
      + */
     -+int get_metacommit_content(struct commit *commit, struct object_id *content)
     ++enum metacommit_type get_metacommit_content(struct commit *commit, struct object_id *content)
      +{
      +	const char *buffer = get_commit_buffer(commit, NULL);
      +	int index = 0;
     -+	int ret = index_of_content_commit(buffer, &index);
     ++	enum metacommit_type ret = index_of_content_commit(buffer, &index);
      +	struct commit *content_parent;
      +
      +	if (ret == METACOMMIT_TYPE_NONE)
     @@ metacommit-parser.h (new)
      +#include "commit.h"
      +#include "hash.h"
      +
     -+/* Indicates a normal commit (non-metacommit) */
     -+#define METACOMMIT_TYPE_NONE 0
     -+/* Indicates a metacommit with normal content (non-abandoned) */
     -+#define METACOMMIT_TYPE_NORMAL 1
     -+/* Indicates a metacommit with abandoned content */
     -+#define METACOMMIT_TYPE_ABANDONED 2
     -+
     -+struct commit;
     ++enum metacommit_type {
     ++	/* Indicates a normal commit (non-metacommit) */
     ++	METACOMMIT_TYPE_NONE = 0,
     ++	/* Indicates a metacommit with normal content (non-abandoned) */
     ++	METACOMMIT_TYPE_NORMAL = 1,
     ++	/* Indicates a metacommit with abandoned content */
     ++	METACOMMIT_TYPE_ABANDONED = 2,
     ++};
      +
     -+extern int get_metacommit_content(
     ++enum metacommit_type get_metacommit_content(
      +	struct commit *commit, struct object_id *content);
      +
      +#endif
  5:  2b3a00a6702 !  5:  48cd92d35ef evolve: add the change-table structure
     @@ change-table.c (new)
      +#include "ref-filter.h"
      +#include "metacommit-parser.h"
      +
     -+void change_table_init(struct change_table *to_initialize)
     ++void change_table_init(struct change_table *table)
      +{
     -+	memset(to_initialize, 0, sizeof(*to_initialize));
     -+	mem_pool_init(&to_initialize->memory_pool, 0);
     -+	to_initialize->memory_pool.block_alloc = 4*1024 - sizeof(struct mp_block);
     -+	oidmap_init(&to_initialize->oid_to_metadata_index, 0);
     -+	string_list_init_dup(&to_initialize->refname_to_change_head);
     ++	memset(table, 0, sizeof(*table));
     ++	mem_pool_init(&table->memory_pool, 0);
     ++	oidmap_init(&table->oid_to_metadata_index, 0);
     ++	strmap_init(&table->refname_to_change_head);
      +}
      +
     -+static void change_list_clear(struct change_list *to_clear) {
     -+	string_list_clear(&to_clear->additional_refnames, 0);
     ++static void change_list_clear(struct change_list *change_list) {
     ++	strset_clear(&change_list->refnames);
      +}
      +
      +static void commit_change_list_entry_clear(
     -+	struct commit_change_list_entry *to_clear) {
     -+	change_list_clear(&to_clear->changes);
     ++	struct commit_change_list_entry *entry) {
     ++	change_list_clear(&entry->changes);
      +}
      +
     -+void change_table_clear(struct change_table *to_clear)
     ++void change_table_clear(struct change_table *table)
      +{
      +	struct oidmap_iter iter;
      +	struct commit_change_list_entry *next;
     -+	for (next = oidmap_iter_first(&to_clear->oid_to_metadata_index, &iter);
     ++	for (next = oidmap_iter_first(&table->oid_to_metadata_index, &iter);
      +		next;
      +		next = oidmap_iter_next(&iter)) {
      +
      +		commit_change_list_entry_clear(next);
      +	}
      +
     -+	oidmap_free(&to_clear->oid_to_metadata_index, 0);
     -+	string_list_clear(&to_clear->refname_to_change_head, 0);
     -+	mem_pool_discard(&to_clear->memory_pool, 0);
     ++	oidmap_free(&table->oid_to_metadata_index, 0);
     ++	strmap_clear(&table->refname_to_change_head, 0);
     ++	mem_pool_discard(&table->memory_pool, 0);
      +}
      +
     -+static void add_head_to_commit(struct change_table *to_modify,
     -+	const struct object_id *to_add, const char *refname)
     ++static void add_head_to_commit(struct change_table *table,
     ++			       const struct object_id *to_add,
     ++			       const char *refname)
      +{
      +	struct commit_change_list_entry *entry;
      +
     -+	/**
     -+	 * Note: the indices in the map are 1-based. 0 is used to indicate a missing
     -+	 * element.
     -+	 */
     -+	entry = oidmap_get(&to_modify->oid_to_metadata_index, to_add);
     ++	entry = oidmap_get(&table->oid_to_metadata_index, to_add);
      +	if (!entry) {
     -+		entry = mem_pool_calloc(&to_modify->memory_pool, 1,
     -+			sizeof(*entry));
     ++		entry = mem_pool_calloc(&table->memory_pool, 1, sizeof(*entry));
      +		oidcpy(&entry->entry.oid, to_add);
     -+		oidmap_put(&to_modify->oid_to_metadata_index, entry);
     -+		string_list_init_nodup(&entry->changes.additional_refnames);
     ++		strset_init(&entry->changes.refnames);
     ++		oidmap_put(&table->oid_to_metadata_index, entry);
      +	}
     -+
     -+	if (!entry->changes.first_refname)
     -+		entry->changes.first_refname = refname;
     -+	else
     -+		string_list_insert(&entry->changes.additional_refnames, refname);
     ++	strset_add(&entry->changes.refnames, refname);
      +}
      +
     -+void change_table_add(struct change_table *to_modify, const char *refname,
     -+	struct commit *to_add)
     ++void change_table_add(struct change_table *table,
     ++		      const char *refname,
     ++		      struct commit *to_add)
      +{
      +	struct change_head *new_head;
     -+	struct string_list_item *new_item;
      +	int metacommit_type;
      +
     -+	new_head = mem_pool_calloc(&to_modify->memory_pool, 1,
     -+		sizeof(*new_head));
     ++	new_head = mem_pool_calloc(&table->memory_pool, 1, sizeof(*new_head));
      +
      +	oidcpy(&new_head->head, &to_add->object.oid);
      +
      +	metacommit_type = get_metacommit_content(to_add, &new_head->content);
     ++	/* If to_add is not a metacommit then the content is to_add itself,
     ++	 * otherwise it will have been set by the call to
     ++	 * get_metacommit_content.
     ++	 */
      +	if (metacommit_type == METACOMMIT_TYPE_NONE)
      +		oidcpy(&new_head->content, &to_add->object.oid);
      +	new_head->abandoned = (metacommit_type == METACOMMIT_TYPE_ABANDONED);
      +	new_head->remote = starts_with(refname, "refs/remote/");
      +	new_head->hidden = starts_with(refname, "refs/hiddenmetas/");
      +
     -+	new_item = string_list_insert(&to_modify->refname_to_change_head, refname);
     -+	new_item->util = new_head;
     -+	/* Use pointers to the copy of the string we're retaining locally */
     -+	refname = new_item->string;
     -+
     -+	if (!oideq(&new_head->content, &new_head->head))
     -+		add_head_to_commit(to_modify, &new_head->content, refname);
     -+	add_head_to_commit(to_modify, &new_head->head, refname);
     -+}
     -+
     -+void change_table_add_all_visible(struct change_table *to_modify,
     -+	struct repository* repo)
     -+{
     -+	struct ref_filter filter;
     -+	const char *name_patterns[] = {NULL};
     -+	memset(&filter, 0, sizeof(filter));
     -+	filter.kind = FILTER_REFS_CHANGES;
     -+	filter.name_patterns = name_patterns;
     ++	strmap_put(&table->refname_to_change_head, refname, new_head);
      +
     -+	change_table_add_matching_filter(to_modify, repo, &filter);
     ++	if (!oideq(&new_head->content, &new_head->head)) {
     ++		/* We also remember to link between refname and the content oid */
     ++		add_head_to_commit(table, &new_head->content, refname);
     ++	}
     ++	add_head_to_commit(table, &new_head->head, refname);
      +}
      +
     -+void change_table_add_matching_filter(struct change_table *to_modify,
     -+	struct repository* repo, struct ref_filter *filter)
     ++static void change_table_add_matching_filter(struct change_table *table,
     ++					     struct repository* repo,
     ++					     struct ref_filter *filter)
      +{
     -+	struct ref_array matching_refs;
      +	int i;
     ++	struct ref_array matching_refs = { 0 };
      +
     -+	memset(&matching_refs, 0, sizeof(matching_refs));
      +	filter_refs(&matching_refs, filter, filter->kind);
      +
     -+	/**
     ++	/*
      +	 * Determine the object id for the latest content commit for each change.
      +	 * Fetch the commit at the head of each change ref. If it's a normal commit,
      +	 * that's the commit we want. If it's a metacommit, locate its content parent
     @@ change-table.c (new)
      +
      +	for (i = 0; i < matching_refs.nr; i++) {
      +		struct ref_array_item *item = matching_refs.items[i];
     -+		struct commit *commit = item->commit;
     ++		struct commit *commit;
      +
     -+		commit = lookup_commit_reference_gently(repo, &item->objectname, 1);
     -+
     -+		if (commit)
     -+			change_table_add(to_modify, item->refname, commit);
     ++		commit = lookup_commit_reference(repo, &item->objectname);
     ++		if (!commit) {
     ++			BUG("Invalid commit for refs/meta: %s", item->refname);
     ++		}
     ++		change_table_add(table, item->refname, commit);
      +	}
      +
      +	ref_array_clear(&matching_refs);
      +}
      +
     ++void change_table_add_all_visible(struct change_table *table,
     ++	struct repository* repo)
     ++{
     ++	struct ref_filter filter = { 0 };
     ++	const char *name_patterns[] = {NULL};
     ++	filter.kind = FILTER_REFS_CHANGES;
     ++	filter.name_patterns = name_patterns;
     ++
     ++	change_table_add_matching_filter(table, repo, &filter);
     ++}
     ++
      +static int return_true_callback(const char *refname, void *cb_data)
      +{
      +	return 1;
      +}
      +
     -+int change_table_has_change_referencing(struct change_table *changes,
     ++int change_table_has_change_referencing(struct change_table *table,
      +	const struct object_id *referenced_commit_id)
      +{
     -+	return for_each_change_referencing(changes, referenced_commit_id,
     ++	return for_each_change_referencing(table, referenced_commit_id,
      +		return_true_callback, NULL);
      +}
      +
      +int for_each_change_referencing(struct change_table *table,
      +	const struct object_id *referenced_commit_id, each_change_fn fn, void *cb_data)
      +{
     -+	const struct change_list *changes;
     -+	int i;
     -+	int retvalue;
     -+	struct commit_change_list_entry *entry;
     ++	int ret;
     ++	struct commit_change_list_entry *ccl_entry;
     ++	struct hashmap_iter iter;
     ++	struct strmap_entry *entry;
      +
     -+	entry = oidmap_get(&table->oid_to_metadata_index,
     -+		referenced_commit_id);
     ++	ccl_entry = oidmap_get(&table->oid_to_metadata_index,
     ++			       referenced_commit_id);
      +	/* If this commit isn't referenced by any changes, it won't be in the map */
     -+	if (!entry)
     ++	if (!ccl_entry)
      +		return 0;
     -+	changes = &entry->changes;
     -+	if (!changes->first_refname)
     -+		return 0;
     -+	retvalue = fn(changes->first_refname, cb_data);
     -+	for (i = 0; retvalue == 0 && i < changes->additional_refnames.nr; i++)
     -+		retvalue = fn(changes->additional_refnames.items[i].string, cb_data);
     -+	return retvalue;
     ++	strset_for_each_entry(&ccl_entry->changes.refnames, &iter, entry) {
     ++		ret = fn(entry->key, cb_data);
     ++		if (ret != 0) break;
     ++	}
     ++	return ret;
      +}
      +
     -+struct change_head* get_change_head(struct change_table *heads,
     ++struct change_head* get_change_head(struct change_table *table,
      +	const char* refname)
      +{
     -+	struct string_list_item *item = string_list_lookup(
     -+		&heads->refname_to_change_head, refname);
     -+
     -+	if (!item)
     -+		return NULL;
     -+
     -+	return (struct change_head *)item->util;
     ++	return strmap_get(&table->refname_to_change_head, refname);
      +}
      
       ## change-table.h (new) ##
     @@ change-table.h (new)
      +#define CHANGE_TABLE_H
      +
      +#include "oidmap.h"
     ++#include "strmap.h"
      +
      +struct commit;
      +struct ref_filter;
      +
      +/**
     -+ * This struct holds a list of change refs. The first element is stored inline,
     -+ * to optimize for small lists.
     ++ * This struct holds a set of change refs.
      + */
      +struct change_list {
      +	/**
     -+	 * Ref name for the first change in the list, or null if none.
     -+	 *
     ++	 * The refnames in this set.
      +	 * This field is private. Use for_each_change_in to read.
      +	 */
     -+	const char* first_refname;
     -+	/**
     -+	 * List of additional change refs. Note that this is empty if the list
     -+	 * contains 0 or 1 elements.
     -+	 *
     -+	 * This field is private. Use for_each_change_in to read.
     -+	 */
     -+	struct string_list additional_refnames;
     ++	struct strset refnames;
      +};
      +
      +/**
     @@ change-table.h (new)
      +};
      +
      +/**
     -+ * Holds information about the heads of each change, and permits effecient
     ++ * Holds information about the heads of each change, and permits efficient
      + * lookup from a commit to the changes that reference it directly.
      + *
      + * All fields should be considered private. Use the change_table functions
     @@ change-table.h (new)
      +	/* Map object_id to commit_change_list_entry structs. */
      +	struct oidmap oid_to_metadata_index;
      +	/**
     -+	 * List of ref names. The util value points to a change_head structure
     -+	 * allocated from memory_pool.
     ++	 * Map of refnames to change_head structure which are allocated from
     ++	 * memory_pool.
      +	 */
     -+	struct string_list refname_to_change_head;
     ++	struct strmap refname_to_change_head;
      +};
      +
     -+extern void change_table_init(struct change_table *to_initialize);
     -+extern void change_table_clear(struct change_table *to_clear);
     ++extern void change_table_init(struct change_table *table);
     ++extern void change_table_clear(struct change_table *table);
      +
      +/* Adds the given change head to the change_table struct */
     -+extern void change_table_add(struct change_table *to_modify,
     -+	const char *refname, struct commit *target);
     ++extern void change_table_add(struct change_table *table,
     ++			     const char *refname,
     ++			     struct commit *target);
      +
      +/**
      + * Adds the non-hidden local changes to the given change_table struct.
      + */
     -+extern void change_table_add_all_visible(struct change_table *to_modify,
     -+	struct repository *repo);
     -+
     -+/*
     -+ * Adds all changes matching the given ref filter to the given change_table
     -+ * struct.
     -+ */
     -+extern void change_table_add_matching_filter(struct change_table *to_modify,
     -+	struct repository* repo, struct ref_filter *filter);
     ++extern void change_table_add_all_visible(struct change_table *table,
     ++					 struct repository *repo);
      +
      +typedef int each_change_fn(const char *refname, void *cb_data);
      +
     -+extern int change_table_has_change_referencing(struct change_table *changes,
     ++extern int change_table_has_change_referencing(
     ++	struct change_table *table,
      +	const struct object_id *referenced_commit_id);
      +
      +/**
     @@ change-table.h (new)
      + * For normal commits, this is the list of changes that have this commit as
      + * their latest content.
      + */
     -+extern int for_each_change_referencing(struct change_table *heads,
     -+	const struct object_id *referenced_commit_id, each_change_fn fn, void *cb_data);
     ++extern int for_each_change_referencing(
     ++	struct change_table *table,
     ++	const struct object_id *referenced_commit_id,
     ++	each_change_fn fn,
     ++	void *cb_data);
      +
      +/**
      + * Returns the change head for the given refname. Returns NULL if no such change
      + * exists.
      + */
     -+extern struct change_head* get_change_head(struct change_table *heads,
     ++extern struct change_head* get_change_head(struct change_table *table,
      +	const char* refname);
      +
      +#endif
  6:  56c6770997b !  6:  353d97d0f38 evolve: add support for writing metacommits
     @@ metacommit.c (new)
      +#include "change-table.h"
      +#include "refs.h"
      +
     -+void init_metacommit_data(struct metacommit_data *state)
     -+{
     -+	memset(state, 0, sizeof(*state));
     -+}
     -+
      +void clear_metacommit_data(struct metacommit_data *state)
      +{
     ++	oidcpy(&state->content, null_oid());
      +	oid_array_clear(&state->replace);
      +	oid_array_clear(&state->origin);
     ++	state->abandoned = 0;
      +}
      +
      +static void compute_default_change_name(struct commit *initial_commit,
     -+	struct strbuf* result)
     ++					struct strbuf* result)
      +{
     -+	struct strbuf default_name;
     ++	struct strbuf default_name = STRBUF_INIT;
      +	const char *buffer;
      +	const char *subject;
      +	const char *eol;
     -+	int len;
     -+	strbuf_init(&default_name, 0);
     ++	size_t len;
      +	buffer = get_commit_buffer(initial_commit, NULL);
      +	find_commit_subject(buffer, &subject);
      +	eol = strchrnul(subject, '\n');
     -+	for (len = 0;subject < eol && len < 10; ++subject, ++len) {
     ++	for (len = 0; subject < eol && len < 10; subject++, len++) {
      +		char next = *subject;
      +		if (isspace(next))
      +			continue;
     @@ metacommit.c (new)
      +		strbuf_addch(&default_name, next);
      +	}
      +	sanitize_refname_component(default_name.buf, result);
     ++	unuse_commit_buffer(initial_commit, buffer);
      +}
      +
     -+/**
     ++/*
      + * Computes a change name for a change rooted at the given initial commit. Good
      + * change names should be memorable, unique, and easy to type. They are not
      + * required to match the commit comment.
      + */
      +static void compute_change_name(struct commit *initial_commit, struct strbuf* result)
      +{
     -+	struct strbuf default_name;
     ++	struct strbuf default_name = STRBUF_INIT;
      +	struct object_id unused;
      +
     -+	strbuf_init(&default_name, 0);
      +	if (initial_commit)
      +		compute_default_change_name(initial_commit, &default_name);
      +	else
     -+		strbuf_addstr(&default_name, "change");
     ++		BUG("initial commit is NULL");
      +	strbuf_addstr(result, "refs/metas/");
      +	strbuf_addbuf(result, &default_name);
      +
      +	/* If there is already a change of this name, append a suffix */
      +	if (!read_ref(result->buf, &unused)) {
      +		int suffix = 2;
     -+		int original_length = result->len;
     ++		size_t original_length = result->len;
      +
      +		while (1) {
      +			strbuf_addf(result, "%d", suffix);
      +			if (read_ref(result->buf, &unused))
      +				break;
     -+			strbuf_remove(result, original_length, result->len - original_length);
     ++			strbuf_remove(result, original_length,
     ++				      result->len - original_length);
      +			++suffix;
      +		}
      +	}
     @@ metacommit.c (new)
      +	strbuf_release(&default_name);
      +}
      +
     -+struct resolve_metacommit_callback_data
     ++struct resolve_metacommit_context
      +{
      +	struct change_table* active_changes;
      +	struct string_list *changes;
     @@ metacommit.c (new)
      +
      +static int resolve_metacommit_callback(const char *refname, void *cb_data)
      +{
     -+	struct resolve_metacommit_callback_data *data = (struct resolve_metacommit_callback_data *)cb_data;
     ++	struct resolve_metacommit_context *data = cb_data;
      +	struct change_head *chhead;
      +
      +	chhead = get_change_head(data->active_changes, refname);
      +
      +	if (data->changes)
     -+		string_list_append(data->changes, refname)->util = &(chhead->head);
     ++		string_list_append(data->changes, refname)->util = &chhead->head;
      +	if (data->heads)
      +		oid_array_append(data->heads, &(chhead->head));
      +
      +	return 0;
      +}
      +
     -+/**
     ++/*
      + * Produces the final form of a metacommit based on the current change refs.
      + */
      +static void resolve_metacommit(
     @@ metacommit.c (new)
      +	struct string_list *to_advance,
      +	int allow_append)
      +{
     -+	int i;
     -+	int len = to_resolve->replace.nr;
     -+	struct resolve_metacommit_callback_data cbdata;
     ++	size_t i;
     ++	size_t len = to_resolve->replace.nr;
     ++	struct resolve_metacommit_context ctx = {
     ++		.active_changes = active_changes,
     ++		.changes = to_advance,
     ++		.heads = &resolved_output->replace
     ++	};
      +	int old_change_list_length = to_advance->nr;
      +	struct commit* content;
      +
      +	oidcpy(&resolved_output->content, &to_resolve->content);
      +
     -+	/* First look for changes that point to any of the replacement edges in the
     ++	/*
     ++	 * First look for changes that point to any of the replacement edges in the
      +	 * metacommit. These will be the changes that get advanced by this
     -+	 * metacommit. */
     ++	 * metacommit.
     ++	 */
      +	resolved_output->abandoned = to_resolve->abandoned;
     -+	cbdata.active_changes = active_changes;
     -+	cbdata.changes = to_advance;
     -+	cbdata.heads = &(resolved_output->replace);
      +
      +	if (allow_append) {
      +		for (i = 0; i < len; i++) {
      +			int old_number = resolved_output->replace.nr;
     -+			for_each_change_referencing(active_changes, &(to_resolve->replace.oid[i]),
     -+				resolve_metacommit_callback, &cbdata);
     ++			for_each_change_referencing(
     ++				active_changes,
     ++				&(to_resolve->replace.oid[i]),
     ++				resolve_metacommit_callback,
     ++				&ctx);
      +			/* If no changes were found, use the unresolved value. */
      +			if (old_number == resolved_output->replace.nr)
     -+				oid_array_append(&(resolved_output->replace), &(to_resolve->replace.oid[i]));
     ++				oid_array_append(&(resolved_output->replace),
     ++						 &(to_resolve->replace.oid[i]));
      +		}
      +	}
      +
     -+	cbdata.changes = NULL;
     -+	cbdata.heads = &(resolved_output->origin);
     ++	ctx.changes = NULL;
     ++	ctx.heads = &(resolved_output->origin);
      +
      +	len = to_resolve->origin.nr;
      +	for (i = 0; i < len; i++) {
      +		int old_number = resolved_output->origin.nr;
     -+		for_each_change_referencing(active_changes, &(to_resolve->origin.oid[i]),
     -+			resolve_metacommit_callback, &cbdata);
     ++		for_each_change_referencing(
     ++			active_changes,
     ++			&(to_resolve->origin.oid[i]),
     ++			resolve_metacommit_callback,
     ++			&ctx);
      +		if (old_number == resolved_output->origin.nr)
     -+			oid_array_append(&(resolved_output->origin), &(to_resolve->origin.oid[i]));
     ++			oid_array_append(&(resolved_output->origin),
     ++					 &(to_resolve->origin.oid[i]));
      +	}
      +
     -+	/* If no changes were advanced by this metacommit, we'll need to create a new
     -+	 * one. */
     ++	/*
     ++	 * If no changes were advanced by this metacommit, we'll need to create
     ++	 * a new one. */
      +	if (to_advance->nr == old_change_list_length) {
      +		struct strbuf change_name;
      +
      +		strbuf_init(&change_name, 80);
     -+		content = lookup_commit_reference_gently(repo, &(to_resolve->content), 1);
     ++
     ++		content = lookup_commit_reference_gently(
     ++			repo, &(to_resolve->content), 1);
      +
      +		compute_change_name(content, &change_name);
      +		string_list_append(to_advance, change_name.buf);
     @@ metacommit.c (new)
      +
      +	while (--i >= 0) {
      +		struct object_id *next = &(to_lookup->oid[i]);
     -+		struct commit *commit = lookup_commit_reference_gently(repo, next, 1);
     ++		struct commit *commit =
     ++			lookup_commit_reference_gently(repo, next, 1);
      +		commit_list_insert(commit, result);
      +	}
      +}
      +
      +#define PARENT_TYPE_PREFIX "parent-type "
      +
     -+/**
     -+ * Creates a new metacommit object with the given content. Writes the object
     -+ * id of the newly-created commit to result.
     -+ */
      +int write_metacommit(struct repository *repo, struct metacommit_data *state,
      +	struct object_id *result)
      +{
      +	struct commit_list *parents = NULL;
      +	struct strbuf comment;
     -+	int i;
     ++	size_t i;
      +	struct commit *content;
      +
      +	strbuf_init(&comment, strlen(PARENT_TYPE_PREFIX)
     @@ metacommit.c (new)
      +		strbuf_addstr(&comment, " o");
      +
      +	/* The parents list will be freed by this call. */
     -+	commit_tree(comment.buf, comment.len, repo->hash_algo->empty_tree, parents,
     -+		result, NULL, NULL);
     ++	commit_tree(
     ++		comment.buf,
     ++		comment.len,
     ++		repo->hash_algo->empty_tree,
     ++		parents,
     ++		result,
     ++		NULL,
     ++		NULL);
      +
      +	strbuf_release(&comment);
      +	return 0;
      +}
      +
     -+/**
     ++/*
      + * Returns true iff the given metacommit is abandoned, has one or more origin
      + * parents, or has one or more replacement parents.
      + */
     @@ metacommit.c (new)
      + * to append to existing changes wherever possible instead of creating new ones.
      + * If override_change is non-null, only the given change ref will be updated.
      + *
     -+ * options is a bitwise combination of the UPDATE_OPTION_* flags.
     -+ */
     -+int record_metacommit(
     -+	struct repository *repo,
     -+	const struct metacommit_data *metacommit, const char *override_change,
     -+	int options, struct strbuf *err)
     -+{
     -+		struct change_table chtable;
     -+		struct string_list changes;
     -+		int result;
     -+
     -+		change_table_init(&chtable);
     -+		change_table_add_all_visible(&chtable, repo);
     -+		string_list_init_dup(&changes);
     -+
     -+		result = record_metacommit_withresult(repo, &chtable, metacommit,
     -+			override_change, options, err, &changes);
     -+
     -+		string_list_clear(&changes, 0);
     -+		change_table_clear(&chtable);
     -+		return result;
     -+}
     -+
     -+/*
     -+ * Records the relationships described by the given metacommit in the
     -+ * repository.
     -+ *
     -+ * If override_change is NULL (the default), an attempt will be made
     -+ * to append to existing changes wherever possible instead of creating new ones.
     -+ * If override_change is non-null, only the given change ref will be updated.
     -+ *
      + * The changes list is filled in with the list of change refs that were updated,
      + * with the util pointers pointing to the old object IDS for those changes.
      + * The object ID pointers all point to objects owned by the change_table and
     @@ metacommit.c (new)
      + *
      + * options is a bitwise combination of the UPDATE_OPTION_* flags.
      + */
     -+int record_metacommit_withresult(
     ++static int record_metacommit_withresult(
      +	struct repository *repo,
      +	struct change_table *chtable,
      +	const struct metacommit_data *metacommit,
      +	const char *override_change,
     -+	int options, struct strbuf *err,
     ++	int options,
     ++	struct strbuf *err,
      +	struct string_list *changes)
      +{
      +	static const char *msg = "updating change";
     -+	struct metacommit_data resolved_metacommit;
     ++	struct metacommit_data resolved_metacommit = METACOMMIT_DATA_INIT;
      +	struct object_id commit_target;
      +	struct ref_transaction *transaction = NULL;
      +	struct change_head *overridden_head;
      +	const struct object_id *old_head;
      +
     -+	int i;
     ++	size_t i;
      +	int ret = 0;
      +	int force = (options & UPDATE_OPTION_FORCE);
      +
     -+	init_metacommit_data(&resolved_metacommit);
     -+
      +	resolve_metacommit(repo, chtable, metacommit, &resolved_metacommit, changes,
      +		(options & UPDATE_OPTION_NOAPPEND) == 0);
      +
      +	if (override_change) {
      +		string_list_clear(changes, 0);
      +		overridden_head = get_change_head(chtable, override_change);
     -+		if (!overridden_head) {
     ++		if (overridden_head) {
      +			/* This is an existing change */
      +			old_head = &overridden_head->head;
      +			if (!force) {
     @@ metacommit.c (new)
      +			/* ...then this is a newly-created change */
      +			old_head = null_oid();
      +
     -+		/* The expected "current" head of the change is stored in the util
     -+		 * pointer. */
     -+		string_list_append(changes, override_change)->util = (void*)old_head;
     ++		/*
     ++		 * The expected "current" head of the change is stored in the
     ++		 * util pointer. Cast required because old_head is const*
     ++		 */
     ++		string_list_append(changes, override_change)->util = (void *)old_head;
      +	}
      +
      +	if (is_nontrivial_metacommit(&resolved_metacommit)) {
     @@ metacommit.c (new)
      +			ret = -1;
      +			goto cleanup;
      +		}
     -+	} else
     -+		/**
     ++	} else {
     ++		/*
      +		 * If the metacommit would only contain a content commit, point to the
      +		 * commit itself rather than creating a trivial metacommit.
      +		 */
      +		oidcpy(&commit_target, &(resolved_metacommit.content));
     ++	}
      +
     -+	/**
     ++	/*
      +	 * If a change already exists with this target and we're not forcing an
      +	 * update to some specific override_change && change, there's nothing to do.
      +	 */
     @@ metacommit.c (new)
      +		for (i = 0; i < changes->nr; i++) {
      +			struct string_list_item *it = &changes->items[i];
      +
     -+			/**
     ++			/*
      +			 * The expected current head of the change is stored in the util pointer.
      +			 * It is null if the change should be newly-created.
      +			 */
     @@ metacommit.c (new)
      +	return ret;
      +}
      +
     -+/**
     -+ * Should be invoked after a command that has "modify" semantics - commands that
     -+ * create a new commit based on an old commit and treat the new one as a
     -+ * replacement for the old one. This method records the replacement in the
     -+ * change graph, such that a future evolve operation will rebase children of
     -+ * the old commit onto the new commit.
     -+ */
     ++int record_metacommit(
     ++	struct repository *repo,
     ++	const struct metacommit_data *metacommit,
     ++	const char *override_change,
     ++	int options,
     ++	struct strbuf *err,
     ++	struct string_list *changes)
     ++{
     ++		struct change_table chtable;
     ++		int result;
     ++
     ++		change_table_init(&chtable);
     ++		change_table_add_all_visible(&chtable, repo);
     ++
     ++		result = record_metacommit_withresult(
     ++			repo,
     ++			&chtable,
     ++			metacommit,
     ++			override_change,
     ++			options,
     ++			err,
     ++			changes);
     ++
     ++		change_table_clear(&chtable);
     ++		return result;
     ++}
     ++
      +void modify_change(
      +	struct repository *repo,
      +	const struct object_id *old_commit,
      +	const struct object_id *new_commit,
      +	struct strbuf *err)
      +{
     -+	struct metacommit_data metacommit;
     ++	struct string_list changes = STRING_LIST_INIT_DUP;
     ++	struct metacommit_data metacommit = METACOMMIT_DATA_INIT;
      +
     -+	init_metacommit_data(&metacommit);
      +	oidcpy(&(metacommit.content), new_commit);
      +	oid_array_append(&(metacommit.replace), old_commit);
      +
     -+	record_metacommit(repo, &metacommit, NULL, 0, err);
     ++	record_metacommit(repo, &metacommit, NULL, 0, err, &changes);
      +
      +	clear_metacommit_data(&metacommit);
     ++	string_list_clear(&changes, 0);
      +}
      
       ## metacommit.h (new) ##
     @@ metacommit.h (new)
      +#include "repository.h"
      +#include "string-list.h"
      +
     -+
     -+struct change_table;
     -+
      +/* If specified, non-fast-forward changes are permitted. */
      +#define UPDATE_OPTION_FORCE     0x0001
      +/**
     @@ metacommit.h (new)
      +	int abandoned;
      +};
      +
     -+extern void init_metacommit_data(struct metacommit_data *state);
     ++#define METACOMMIT_DATA_INIT { 0 }
      +
      +extern void clear_metacommit_data(struct metacommit_data *state);
      +
     -+extern int record_metacommit(struct repository *repo,
     -+	const struct metacommit_data *metacommit,
     -+	const char* override_change, int options, struct strbuf *err);
     -+
     -+extern int record_metacommit_withresult(
     ++/**
     ++ * Records the relationships described by the given metacommit in the
     ++ * repository.
     ++ *
     ++ * If override_change is NULL (the default), an attempt will be made
     ++ * to append to existing changes wherever possible instead of creating new ones.
     ++ * If override_change is non-null, only the given change ref will be updated.
     ++ *
     ++ * options is a bitwise combination of the UPDATE_OPTION_* flags.
     ++ */
     ++int record_metacommit(
      +	struct repository *repo,
     -+	struct change_table *chtable,
      +	const struct metacommit_data *metacommit,
     -+	const char *override_change,
     ++	const char* override_change,
      +	int options,
      +	struct strbuf *err,
      +	struct string_list *changes);
      +
     -+extern void modify_change(struct repository *repo,
     -+	const struct object_id *old_commit, const struct object_id *new_commit,
     ++/**
     ++ * Should be invoked after a command that has "modify" semantics - commands that
     ++ * create a new commit based on an old commit and treat the new one as a
     ++ * replacement for the old one. This method records the replacement in the
     ++ * change graph, such that a future evolve operation will rebase children of
     ++ * the old commit onto the new commit.
     ++ */
     ++void modify_change(
     ++	struct repository *repo,
     ++	const struct object_id *old_commit,
     ++	const struct object_id *new_commit,
      +	struct strbuf *err);
      +
     -+extern int write_metacommit(struct repository *repo, struct metacommit_data *state,
     ++/**
     ++ * Creates a new metacommit object with the given content. Writes the object
     ++ * id of the newly-created commit to result.
     ++ */
     ++int write_metacommit(
     ++	struct repository *repo,
     ++	struct metacommit_data *state,
      +	struct object_id *result);
      +
      +#endif
  7:  91402834184 !  7:  f7a90700e0e evolve: implement the git change command
     @@ builtin/change.c (new)
      +#include "ref-filter.h"
      +#include "parse-options.h"
      +#include "metacommit.h"
     -+#include "change-table.h"
      +#include "config.h"
      +
      +static const char * const builtin_change_usage[] = {
     -+	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
     ++	N_("git change list [<pattern>...]"),
     ++	N_("git change update [--force] [--replace <treeish>...] "
     ++	   "[--origin <treeish>...] [--content <newtreeish>]"),
     ++	NULL
     ++};
     ++
     ++static const char * const builtin_list_usage[] = {
     ++	N_("git change list [<pattern>...]"),
      +	NULL
      +};
      +
      +static const char * const builtin_update_usage[] = {
     -+	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
     ++	N_("git change update [--force] [--replace <treeish>...] "
     ++	"[--origin <treeish>...] [--content <newtreeish>]"),
      +	NULL
      +};
      +
     ++static int change_list(int argc, const char **argv, const char* prefix)
     ++{
     ++	struct option options[] = {
     ++		OPT_END()
     ++	};
     ++	struct ref_filter filter = { 0 };
     ++	struct ref_sorting *sorting;
     ++	struct string_list sorting_options = STRING_LIST_INIT_DUP;
     ++	struct ref_format format = REF_FORMAT_INIT;
     ++	struct ref_array array = { 0 };
     ++	size_t i;
     ++
     ++	argc = parse_options(argc, argv, prefix, options, builtin_list_usage, 0);
     ++
     ++	setup_ref_filter_porcelain_msg();
     ++
     ++	filter.kind = FILTER_REFS_CHANGES;
     ++	filter.name_patterns = argv;
     ++
     ++	filter_refs(&array, &filter, FILTER_REFS_CHANGES);
     ++
     ++	/* TODO: This causes a crash. It sets one of the atom_value handlers to
     ++	 * something invalid, which causes a crash later when we call
     ++	 * show_ref_array_item. Figure out why this happens and put back the sorting.
     ++	 *
     ++	 * sorting = ref_sorting_options(&sorting_options);
     ++	 * ref_array_sort(sorting, &array); */
     ++
     ++	if (!format.format)
     ++		format.format = "%(refname:lstrip=1)";
     ++
     ++	if (verify_ref_format(&format))
     ++		die(_("unable to parse format string"));
     ++
     ++	sorting = ref_sorting_options(&sorting_options);
     ++	ref_array_sort(sorting, &array);
     ++
     ++
     ++	for (i = 0; i < array.nr; i++) {
     ++		struct strbuf output = STRBUF_INIT;
     ++		struct strbuf err = STRBUF_INIT;
     ++		if (format_ref_array_item(array.items[i], &format, &output, &err))
     ++			die("%s", err.buf);
     ++		fwrite(output.buf, 1, output.len, stdout);
     ++		putchar('\n');
     ++
     ++		strbuf_release(&err);
     ++		strbuf_release(&output);
     ++	}
     ++
     ++	ref_array_clear(&array);
     ++	ref_sorting_release(sorting);
     ++
     ++	return 0;
     ++}
     ++
      +struct update_state {
      +	int options;
      +	const char* change;
     @@ builtin/change.c (new)
      +	struct string_list origin;
      +};
      +
     -+static void init_update_state(struct update_state *state)
     -+{
     -+	memset(state, 0, sizeof(*state));
     -+	state->content = "HEAD";
     -+	string_list_init_nodup(&state->replace);
     -+	string_list_init_nodup(&state->origin);
     ++#define UPDATE_STATE_INIT { \
     ++	.content = "HEAD", \
     ++	.replace = STRING_LIST_INIT_NODUP, \
     ++	.origin = STRING_LIST_INIT_NODUP \
      +}
      +
      +static void clear_update_state(struct update_state *state)
     @@ builtin/change.c (new)
      +{
      +	struct commit *commit;
      +	if (get_oid_committish(committish, result))
     -+		die(_("Failed to resolve '%s' as a valid revision."), committish);
     ++		die(_("failed to resolve '%s' as a valid revision."), committish);
      +	commit = lookup_commit_reference(the_repository, result);
      +	if (!commit)
     -+		die(_("Could not parse object '%s'."), committish);
     ++		die(_("could not parse object '%s'."), committish);
      +	oidcpy(result, &commit->object.oid);
      +	return 0;
      +}
     @@ builtin/change.c (new)
      +static void resolve_commit_list(const struct string_list *commitsish_list,
      +	struct oid_array* result)
      +{
     -+	int i;
     -+	for (i = 0; i < commitsish_list->nr; i++) {
     -+		struct string_list_item *item = &commitsish_list->items[i];
     ++	struct string_list_item *item;
     ++
     ++	for_each_string_list_item(item, commitsish_list) {
      +		struct object_id next;
      +		resolve_commit(item->string, &next);
      +		oid_array_append(result, &next);
     @@ builtin/change.c (new)
      +	const struct update_state *state,
      +	struct strbuf *err)
      +{
     -+	struct metacommit_data metacommit;
     -+	struct change_table chtable;
     -+	struct string_list changes;
     ++	struct metacommit_data metacommit = METACOMMIT_DATA_INIT;
     ++	struct string_list changes = STRING_LIST_INIT_DUP;
      +	int ret;
     -+	int i;
     -+
     -+	change_table_init(&chtable);
     -+	change_table_add_all_visible(&chtable, repo);
     -+	string_list_init_dup(&changes);
     -+
     -+	init_metacommit_data(&metacommit);
     ++	struct string_list_item *item;
      +
      +	get_metacommit_from_command_line(state, &metacommit);
      +
     -+	ret = record_metacommit_withresult(repo, &chtable, &metacommit,
     -+		state->change, state->options, err, &changes);
     ++	ret = record_metacommit(
     ++		repo,
     ++		&metacommit,
     ++		state->change,
     ++		state->options,
     ++		err,
     ++		&changes);
      +
     -+	for (i = 0; i < changes.nr; i++) {
     -+		struct string_list_item *it = &changes.items[i];
     ++	for_each_string_list_item(item, &changes) {
      +
     -+		const char* name = lstrip_ref_components(it->string, 1);
     ++		const char* name = lstrip_ref_components(item->string, 1);
      +		if (!name)
     -+			die(_("Failed to remove `refs/` from %s"), it->string);
     ++			die(_("failed to remove `refs/` from %s"), item->string);
      +
     -+		if (it->util)
     -+			fprintf(stdout, N_("Updated change %s\n"), name);
     ++		if (item->util)
     ++			fprintf(stdout, _("Updated change %s"), name);
      +		else
     -+			fprintf(stdout, N_("Created change %s\n"), name);
     ++			fprintf(stdout, _("Created change %s"), name);
     ++		putchar('\n');
      +	}
      +
      +	string_list_clear(&changes, 0);
     -+	change_table_clear(&chtable);
      +	clear_metacommit_data(&metacommit);
      +
      +	return ret;
     @@ builtin/change.c (new)
      +static int change_update(int argc, const char **argv, const char* prefix)
      +{
      +	int result;
     -+	int force = 0;
     -+	int newchange = 0;
      +	struct strbuf err = STRBUF_INIT;
     -+	struct update_state state;
     ++	struct update_state state = UPDATE_STATE_INIT;
      +	struct option options[] = {
      +		{ OPTION_CALLBACK, 'r', "replace", &state, N_("commit"),
      +			N_("marks the given commit as being obsolete"),
     @@ builtin/change.c (new)
      +		{ OPTION_CALLBACK, 'o', "origin", &state, N_("commit"),
      +			N_("marks the given commit as being the origin of this commit"),
      +			0, update_option_parse_origin },
     -+		OPT_BOOL('F', "force", &force,
     -+			N_("overwrite an existing change of the same name")),
     ++
      +		OPT_STRING('c', "content", &state.content, N_("commit"),
      +				 N_("identifies the new content commit for the change")),
      +		OPT_STRING('g', "change", &state.change, N_("commit"),
      +				 N_("name of the change to update")),
     -+		OPT_BOOL('n', "new", &newchange,
     -+			N_("create a new change - do not append to any existing change")),
     ++		OPT_SET_INT_F('n', "new", &state.options,
     ++			      N_("create a new change - do not append to any existing change"),
     ++			      UPDATE_OPTION_NOAPPEND, 0),
     ++		OPT_SET_INT_F('F', "force", &state.options,
     ++			      N_("overwrite an existing change of the same name"),
     ++			      UPDATE_OPTION_FORCE, 0),
      +		OPT_END()
      +	};
      +
     -+	init_update_state(&state);
     -+
      +	argc = parse_options(argc, argv, prefix, options, builtin_update_usage, 0);
     -+
     -+	if (force) state.options |= UPDATE_OPTION_FORCE;
     -+	if (newchange) state.options |= UPDATE_OPTION_NOAPPEND;
     -+
      +	result = perform_update(the_repository, &state, &err);
      +
      +	if (result < 0) {
     @@ builtin/change.c (new)
      +
      +int cmd_change(int argc, const char **argv, const char *prefix)
      +{
     ++	parse_opt_subcommand_fn *fn = NULL;
      +	/* No options permitted before subcommand currently */
      +	struct option options[] = {
     ++		OPT_SUBCOMMAND("list", &fn, change_list),
     ++		OPT_SUBCOMMAND("update", &fn, change_update),
      +		OPT_END()
      +	};
     -+	int result = 1;
      +
      +	argc = parse_options(argc, argv, prefix, options, builtin_change_usage,
     -+		PARSE_OPT_STOP_AT_NON_OPTION);
     -+
     -+	if (argc < 1)
     -+		usage_with_options(builtin_change_usage, options);
     -+	else if (!strcmp(argv[0], "update"))
     -+		result = change_update(argc, argv, prefix);
     -+	else {
     -+		error(_("Unknown subcommand: %s"), argv[0]);
     -+		usage_with_options(builtin_change_usage, options);
     ++		PARSE_OPT_SUBCOMMAND_OPTIONAL);
     ++
     ++	if (!fn) {
     ++		if (argc) {
     ++			error(_("unknown subcommand: `%s'"), argv[0]);
     ++			usage_with_options(builtin_change_usage, options);
     ++		}
     ++		fn = change_list;
      +	}
      +
     -+	return result ? 1 : 0;
     ++	return !!fn(argc, argv, prefix);
      +}
      
       ## git.c ##
  9:  d087d467e3f !  8:  a0669fa63a1 evolve: add delete command
     @@ Commit message
      
       ## builtin/change.c ##
      @@
     + #include "parse-options.h"
       #include "metacommit.h"
     - #include "change-table.h"
       #include "config.h"
      +#include "refs.h"
       
       static const char * const builtin_change_usage[] = {
       	N_("git change list [<pattern>...]"),
     --	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
     -+	N_("git change update [--force] [--replace <treeish>...] [--origin <treeish>...] [--content <newtreeish>]"),
     + 	N_("git change update [--force] [--replace <treeish>...] "
     + 	   "[--origin <treeish>...] [--content <newtreeish>]"),
      +	N_("git change delete <change-name>..."),
       	NULL
       };
       
     -@@ builtin/change.c: static const char * const builtin_list_usage[] = {
     +@@ builtin/change.c: static const char * const builtin_update_usage[] = {
     + 	NULL
       };
       
     - static const char * const builtin_update_usage[] = {
     --	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
     -+	N_("git change update [--force] [--replace <treeish>...] [--origin <treeish>...] [--content <newtreeish>]"),
     ++static const char * const builtin_delete_usage[] = {
     ++	N_("git change delete <change-name>..."),
      +	NULL
      +};
      +
     -+static const char * const builtin_delete_usage[] = {
     -+	N_("git change delete <change-name>..."),
     - 	NULL
     - };
     - 
     + static int change_list(int argc, const char **argv, const char* prefix)
     + {
     + 	struct option options[] = {
      @@ builtin/change.c: static int change_update(int argc, const char **argv, const char* prefix)
       	return result;
       }
     @@ builtin/change.c: static int change_update(int argc, const char **argv, const ch
      +
       int cmd_change(int argc, const char **argv, const char *prefix)
       {
     - 	/* No options permitted before subcommand currently */
     + 	parse_opt_subcommand_fn *fn = NULL;
      @@ builtin/change.c: int cmd_change(int argc, const char **argv, const char *prefix)
     - 		result = change_list(argc, argv, prefix);
     - 	else if (!strcmp(argv[0], "update"))
     - 		result = change_update(argc, argv, prefix);
     -+	else if (!strcmp(argv[0], "delete"))
     -+		result = change_delete(argc, argv, prefix);
     - 	else {
     - 		error(_("Unknown subcommand: %s"), argv[0]);
     - 		usage_with_options(builtin_change_usage, options);
     + 	struct option options[] = {
     + 		OPT_SUBCOMMAND("list", &fn, change_list),
     + 		OPT_SUBCOMMAND("update", &fn, change_update),
     ++		OPT_SUBCOMMAND("delete", &fn, change_delete),
     + 		OPT_END()
     + 	};
     + 
 10:  811d516e5d2 =  9:  e67ff668fff evolve: add documentation for `git change`
  8:  b83a79beeb4 ! 10:  37042b58cda evolve: add the git change list command
     @@
       ## Metadata ##
     -Author: Stefan Xenos <sxenos@google.com>
     +Author: Chris Poucet <poucet@google.com>
      
       ## Commit message ##
     -    evolve: add the git change list command
     +    evolve: add tests for the git-change command
      
     -    This command lists the ongoing changes from the refs/metas
     -    namespace.
     -
     -    Signed-off-by: Stefan Xenos <sxenos@google.com>
     +    Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
          Signed-off-by: Chris Poucet <poucet@google.com>
      
     - ## builtin/change.c ##
     + ## t/t9990-changes.sh (new) ##
      @@
     - #include "config.h"
     - 
     - static const char * const builtin_change_usage[] = {
     -+	N_("git change list [<pattern>...]"),
     - 	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
     - 	NULL
     - };
     - 
     -+static const char * const builtin_list_usage[] = {
     -+	N_("git change list [<pattern>...]"),
     -+	NULL
     -+};
     ++#!/bin/sh
      +
     - static const char * const builtin_update_usage[] = {
     - 	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
     - 	NULL
     - };
     - 
     -+static int change_list(int argc, const char **argv, const char* prefix)
     -+{
     -+	struct option options[] = {
     -+		OPT_END()
     -+	};
     -+	struct ref_filter filter;
     -+	/* TODO: See below
     -+	struct ref_sorting *sorting;
     -+	struct string_list sorting_options = STRING_LIST_INIT_DUP; */
     -+	struct ref_format format = REF_FORMAT_INIT;
     -+	struct ref_array array;
     -+	int i;
     ++test_description='git change - low level meta-commit management'
      +
     -+	argc = parse_options(argc, argv, prefix, options, builtin_list_usage, 0);
     ++. ./test-lib.sh
      +
     -+	setup_ref_filter_porcelain_msg();
     ++. "$TEST_DIRECTORY"/lib-rebase.sh
      +
     -+	memset(&filter, 0, sizeof(filter));
     -+	memset(&array, 0, sizeof(array));
     ++test_expect_success 'setup commits and meta-commits' '
     ++       for c in one two three
     ++       do
     ++               test_commit $c &&
     ++               git change update --content $c >actual 2>err &&
     ++               echo "Created change metas/$c" >expect &&
     ++               test_cmp expect actual &&
     ++               test_must_be_empty err &&
     ++               test_cmp_rev refs/metas/$c $c || return 1
     ++       done
     ++'
      +
     -+	filter.kind = FILTER_REFS_CHANGES;
     -+	filter.name_patterns = argv;
     ++# Check a meta-commit has the correct parents Call with the object
     ++# name of the meta-commit followed by pairs of type and parent
     ++check_meta_commit () {
     ++       name=$1
     ++       shift
     ++       while test $# -gt 0
     ++       do
     ++               printf '%s %s\n' $1 $(git rev-parse --verify $2)
     ++               shift
     ++               shift
     ++       done | sort >expect
     ++       git cat-file commit $name >metacommit &&
     ++       # commit body should consist of parent-type
     ++           types="$(sed -n '/^$/ {
     ++                       :loop
     ++                       n
     ++                       s/^parent-type //
     ++                       p
     ++                       b loop
     ++                   }' metacommit)" &&
     ++       while read key value
     ++       do
     ++               # TODO: don't sort the first parent
     ++               if test "$key" = "parent"
     ++               then
     ++                       type="${types%% *}"
     ++                       test -n "$type" || return 1
     ++                       printf '%s %s\n' $type $value
     ++                       types="${types#?}"
     ++                       types="${types# }"
     ++               elif test "$key" = "tree"
     ++               then
     ++                       test_cmp_rev "$value" $EMPTY_TREE || return 1
     ++               elif test -z "$key"
     ++               then
     ++                       # only parse commit headers
     ++                       break
     ++               fi
     ++       done <metacommit >actual-unsorted &&
     ++       test -z "$types" &&
     ++       sort >actual <actual-unsorted &&
     ++       test_cmp expect actual
     ++}
      +
     -+	filter_refs(&array, &filter, FILTER_REFS_CHANGES);
     ++test_expect_success 'update meta-commits after rebase' '
     ++       (
     ++               set_fake_editor &&
     ++               FAKE_AMEND=edited &&
     ++               FAKE_LINES="reword 1 pick 2 fixup 3" &&
     ++               export FAKE_AMEND FAKE_LINES &&
     ++               git rebase -i --root
     ++       ) &&
      +
     -+	/* TODO: This causes a crash. It sets one of the atom_value handlers to
     -+	 * something invalid, which causes a crash later when we call
     -+	 * show_ref_array_item. Figure out why this happens and put back the sorting.
     -+	 *
     -+	 * sorting = ref_sorting_options(&sorting_options);
     -+	 * ref_array_sort(sorting, &array); */
     ++       # update meta-commits
     ++       git change update --replace tags/one --content HEAD~1 >out 2>err &&
     ++       echo "Updated change metas/one" >expect &&
     ++       test_cmp expect out &&
     ++       test_must_be_empty err &&
     ++       git change update --replace tags/two --content HEAD@{2} &&
     ++       oid=$(git rev-parse --verify metas/two) &&
     ++       git change update --replace HEAD@{2} --replace tags/three \
     ++               --content HEAD &&
      +
     -+	if (!format.format)
     -+		format.format = "%(refname:lstrip=1)";
     ++       # check meta-commits
     ++       check_meta_commit metas/one c HEAD~1 r tags/one &&
     ++       check_meta_commit $oid c HEAD@{2} r tags/two &&
     ++       # NB this checks that "git change update" uses the meta-commit ($oid)
     ++       #    corresponding to the replaces commit (HEAD@2 above) given on the
     ++       #    commandline.
     ++       check_meta_commit metas/two c HEAD r $oid r tags/three &&
     ++       check_meta_commit metas/three c HEAD r $oid r tags/three
     ++'
      +
     -+	if (verify_ref_format(&format))
     -+		die(_("unable to parse format string"));
     ++reset_meta_commits () {
     ++    for c in one two three
     ++    do
     ++       echo "update refs/metas/$c refs/tags/$c^0"
     ++    done | git update-ref --stdin
     ++}
      +
     -+	for (i = 0; i < array.nr; i++) {
     -+		struct strbuf output = STRBUF_INIT;
     -+		struct strbuf err = STRBUF_INIT;
     -+		if (format_ref_array_item(array.items[i], &format, &output, &err))
     -+			die("%s", err.buf);
     -+		fwrite(output.buf, 1, output.len, stdout);
     -+		putchar('\n');
     ++test_expect_success 'override change name' '
     ++       # TODO: builtin/change.c expects --change to be the full refname,
     ++       #       ideally it would prepend refs/metas to the string given by the
     ++       #       user.
     ++       git change update --change refs/metas/another-one --content one &&
     ++       test_cmp_rev metas/another-one one
     ++'
      +
     -+		strbuf_release(&err);
     -+		strbuf_release(&output);
     -+	}
     ++test_expect_success 'non-fast forward meta-commit update refused' '
     ++       test_must_fail git change update --change refs/metas/one --content two \
     ++               >out 2>err &&
     ++       echo "error: non-fast-forward update to ${SQ}refs/metas/one${SQ}" \
     ++               >expect &&
     ++       test_cmp expect err &&
     ++       test_must_be_empty out
     ++'
      +
     -+	ref_array_clear(&array);
     -+	/* TODO: see above
     -+	ref_sorting_release(sorting); */
     ++test_expect_success 'forced non-fast forward update succeeds' '
     ++       git change update --change refs/metas/one --content two --force \
     ++               >out 2>err &&
     ++       echo "Updated change metas/one" >expect &&
     ++       test_cmp expect out &&
     ++       test_must_be_empty err
     ++'
      +
     -+	return 0;
     -+}
     ++test_expect_success 'list changes' '
     ++       cat >expect <<-\EOF &&
     ++metas/another-one
     ++metas/one
     ++metas/three
     ++metas/two
     ++EOF
     ++       git change list >actual &&
     ++       test_cmp expect actual
     ++'
     ++
     ++test_expect_success 'delete change' '
     ++       git change delete metas/one &&
     ++       cat >expect <<-\EOF &&
     ++metas/another-one
     ++metas/three
     ++metas/two
     ++EOF
     ++       git change list >actual &&
     ++       test_cmp expect actual
     ++'
      +
     - struct update_state {
     - 	int options;
     - 	const char* change;
     -@@ builtin/change.c: int cmd_change(int argc, const char **argv, const char *prefix)
     - 
     - 	if (argc < 1)
     - 		usage_with_options(builtin_change_usage, options);
     -+	else if (!strcmp(argv[0], "list"))
     -+		result = change_list(argc, argv, prefix);
     - 	else if (!strcmp(argv[0], "update"))
     - 		result = change_update(argc, argv, prefix);
     - 	else {
     ++test_done

-- 
gitgitgadget

^ permalink raw reply	[flat|nested] 66+ messages in thread

* [PATCH v2 01/10] technical doc: add a design doc for the evolve command
  2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
@ 2022-10-05 14:59   ` Stefan Xenos via GitGitGadget
  2022-10-05 15:16     ` Chris Poucet
  2022-10-10 19:35     ` Victoria Dye
  2022-10-05 14:59   ` [PATCH v2 02/10] sha1-array: implement oid_array_readonly_contains Chris Poucet via GitGitGadget
                     ` (9 subsequent siblings)
  10 siblings, 2 replies; 66+ messages in thread
From: Stefan Xenos via GitGitGadget @ 2022-10-05 14:59 UTC (permalink / raw)
  To: git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Stefan Xenos

From: Stefan Xenos <sxenos@google.com>

This document describes what a change graph for
git would look like, the behavior of the evolve command,
and the changes planned for other commands.

It was originally proposed in 2018, see
https://public-inbox.org/git/20181115005546.212538-1-sxenos@google.com/

Signed-off-by: Stefan Xenos <sxenos@google.com>
Signed-off-by: Chris Poucet <poucet@google.com>
---
 Documentation/technical/evolve.txt | 1070 ++++++++++++++++++++++++++++
 1 file changed, 1070 insertions(+)
 create mode 100644 Documentation/technical/evolve.txt

diff --git a/Documentation/technical/evolve.txt b/Documentation/technical/evolve.txt
new file mode 100644
index 00000000000..2051ea77b8a
--- /dev/null
+++ b/Documentation/technical/evolve.txt
@@ -0,0 +1,1070 @@
+Evolve
+======
+
+Objective
+=========
+Create an "evolve" command to help users craft a high quality commit history.
+Users can improve commits one at a time and in any order, then run git evolve to
+rewrite their recent history to ensure everything is up-to-date. We track
+amendments to a commit over time in a change graph. Users can share their
+progress with others by exchanging their change graphs using the standard push,
+fetch, and format-patch commands.
+
+Status
+======
+This proposal has not been implemented yet.
+
+Background
+==========
+Imagine you have three sequential changes up for review and you receive feedback
+that requires editing all three changes. We'll define the word "change"
+formally later, but for the moment let's say that a change is a work-in-progress
+whose final version will be submitted as a commit in the future.
+
+While you're editing one change, more feedback arrives on one of the others.
+What do you do?
+
+The evolve command is a convenient way to work with chains of commits that are
+under review. Whenever you rebase or amend a commit, the repository remembers
+that the old commit is obsolete and has been replaced by the new one. Then, at
+some point in the future, you can run "git evolve" and the correct sequence of
+rebases will occur in the correct order such that no commit has an obsolete
+parent.
+
+Part of making the "evolve" command work involves tracking the edits to a commit
+over time, which is why we need an change graph. However, the change
+graph will also bring other benefits:
+
+- Users can view the history of a change directly (the sequence of amends and
+  rebases it has undergone, orthogonal to the history of the branch it is on).
+- It will be possible to quickly locate and list all the changes the user
+  currently has in progress.
+- It can be used as part of other high-level commands that combine or split
+  changes.
+- It can be used to decorate commits (in git log, gitk, etc) that are either
+  obsolete or are the tip of a work in progress.
+- By pushing and pulling the change graph, users can collaborate more
+  easily on changes-in-progress. This is better than pushing and pulling the
+  commits themselves since the change graph can be used to locate a more
+  specific merge base, allowing for better merges between different versions of
+  the same change.
+- It could be used to correctly rebase local changes and other local branches
+  after running git-filter-branch.
+- It can replace the change-id footer used by gerrit.
+
+Goals
+-----
+Legend: Goals marked with P0 are required. Goals marked with Pn should be
+attempted unless they interfere with goals marked with Pn-1.
+
+P0. All commands that modify commits (such as the normal commit --amend or
+    rebase command) should mark the old commit as being obsolete and replaced by
+    the new one. No additional commands should be required to keep the
+    change graph up-to-date.
+P0. Any commit that may be involved in a future evolve command should not be
+    garbage collected. Specifically:
+    - Commits that obsolete another should not be garbage collected until
+      user-specified conditions have occurred and the change has expired from
+      the reflog. User specified conditions for removing changes include:
+      - The user explicitly deleted the change.
+      - The change was merged into a specific branch.
+    - Commits that have been obsoleted by another should not be garbage
+      collected if any of their replacements are still being retained.
+P0. A commit can be obsoleted by more than one replacement (called divergence).
+P0. Users must be able to resolve divergence (convergence).
+P1. Users should be able to share chains of obsolete changes in order to
+    collaborate on WIP changes.
+P2. Such sharing should be at the user’s option. That is, it should be possible
+    to directly share a change without also sharing the file states or commit
+    comments from the obsolete changes that led up to it, and the choice not to
+    share those commits should not require changing any commit hashes.
+P2. It should be possible to discard part or all of the change graph
+    without discarding the commits themselves that are already present in
+    branches and the reflog.
+P2. Provide sufficient information to replace gerrit's Change-Id footers.
+
+Similar technologies
+--------------------
+There are some other technologies that address the same end-user problem.
+
+Rebase -i can be used to solve the same problem, but users can't easily switch
+tasks midway through an interactive rebase or have more than one interactive
+rebase going on at the same time. It can't handle the case where you have
+multiple changes sharing the same parent when that parent needs to be rebased
+and won't let you collaborate with others on resolving a complicated interactive
+rebase. You can think of rebase -i as a top-down approach and the evolve command
+as the bottom-up approach to the same problem.
+
+Revup amend (https://github.com/Skydio/revup/blob/main/docs/amend.md)
+allows insertion of cached changes into any commit in
+the current history, and then reapplies the rest of history on top of
+those changes. It uses a "git apply --cached" engine under the hood so
+doesn't touch the working directory (although it will soon use the new
+git merge-tree). When paired with "revup upload" which creates and
+pushes multiple branches in the background for you, its possible to
+work on a "graph" of changes on a single branch linearly, then have
+the true graph structure created at upload time.
+
+git-revise (https://github.com/mystor/git-revise) does some very
+similar things except it uses "git merge-file" combined with manually
+merging the resulting trees. git branchstack
+(https://github.com/krobelus/git-branchstack) can also create branches
+in the background with the same mechanism.
+
+These tools don't store any external state, but as such also don't
+provide any specific collaboration mechanism for individual changes.
+
+Several patch queue managers have been built on top of git (such as topgit,
+stgit, and quilt). They address the same user need. However they also rely on
+state managed outside git that needs to be kept in sync. Such state can be
+easily damaged when running a git native command that is unaware of the patch
+queue. They also typically require an explicit initialization step to be done by
+the user which creates workflow problems.
+
+Mercurial implements a very similar feature in its EvolveExtension. The behavior
+of the evolve command itself is very similar, but the storage format for the
+change graph differs. In the case of mercurial, each change set can have one or
+more obsolescence markers that point to other changesets that they replace. This
+is similar to the "Commit Headers" approach considered in the other options
+appendix. The approach proposed here stores obsolescence information in a
+separate metacommit graph, which makes exchanging of obsolescence information
+optional.
+
+Mercurial's default behavior makes it easy to find and switch between
+non-obsolete changesets that aren't currently on any branch. We introduce the
+notion of a new ref namespace that enables a similar workflow via a different
+mechanism. Mercurial has the notion of changeset phases which isn't present
+in git and creates new ways for a changeset to diverge. Git doesn't need
+to deal with these issues, but it has to deal with the problems of picking an
+upstream branch as a target for rebases and protecting obsolescence information
+from GC. We also introduce some additional transformations (see
+obsolescence-over-cherry-pick, below) that aren't present in the mercurial
+implementation.
+
+Semi-related work
+-----------------
+There are other technologies that address different problems but have some
+similarities with this proposal.
+
+Replacements (refs/replace) are superficially similar to obsolescences in that
+they describe that one commit should be replaced by another. However, they
+differ in both how they are created and how they are intended to be used.
+Obsolescences are created automatically by the commands a user runs, and they
+describe the user’s intent to perform a future rebase. Obsolete commits still
+appear in branches, logs, etc like normal commits (possibly with an extra
+decoration that marks them as obsolete). Replacements are typically created
+explicitly by the user, they are meant to be kept around for a long time, and
+they describe a replacement to be applied at read-time rather than as the input
+to a future operation. When a replaced commit is queried, it is typically hidden
+and swapped out with its replacement as though the replacement has already
+occurred.
+
+Git-imerge is a project to help make complicated merges easier, particularly
+when merging or rebasing long chains of patches. It is not an alternative to
+the change graph, but its algorithm of applying smaller incremental merges
+could be used as part of the evolve algorithm in the future.
+
+Overview
+========
+We introduce the notion of “meta-commits” which describe how one commit was
+created from other commits. A branch of meta-commits is known as a change.
+Changes are created and updated automatically whenever a user runs a command
+that creates a commit. They are used for locating obsolete commits, providing a
+list of a user’s unsubmitted work in progress, and providing a stable name for
+each unsubmitted change.
+
+Users can exchange edit histories by pushing and fetching changes.
+
+New commands will be introduced for manipulating changes and resolving
+divergence between them. Existing commands that create commits will be updated
+to modify the meta-commit graph and create changes where necessary.
+
+Example usage
+-------------
+# First create three dependent changes
+$ echo foo>bar.txt && git add .
+$ git commit -m "This is a test"
+created change metas/this_is_a_test
+$ echo foo2>bar2.txt && git add .
+$ git commit -m "This is also a test"
+created change metas/this_is_also_a_test
+$ echo foo3>bar3.txt && git add .
+$ git commit -m "More testing"
+created change metas/more_testing
+
+# List all our changes in progress
+$ git change list
+metas/this_is_a_test
+metas/this_is_also_a_test
+* metas/more_testing
+metas/some_change_already_merged_upstream
+
+# Now modify the earliest change, using its stable name
+$ git reset --hard metas/this_is_a_test
+$ echo morefoo>>bar.txt && git add . && git commit --amend --no-edit
+
+# Use git-evolve to fix up any dependent changes
+$ git evolve
+rebasing metas/this_is_also_a_test onto metas/this_is_a_test
+rebasing metas/more_testing onto metas/this_is_also_a_test
+Done
+
+# Use git-obslog to view the history of the this_is_a_test change
+$ git log --obslog
+93f110 metas/this_is_a_test@{0} commit (amend): This is a test
+930219 metas/this_is_a_test@{1} commit: This is a test
+
+# Now create an unrelated change
+$ git reset --hard origin/master
+$ echo newchange>unrelated.txt && git add .
+$ git commit -m "Unrelated change"
+created change metas/unrelated_change
+
+# Fetch the latest code from origin/master and use git-evolve
+# to rebase all dependent changes.
+$ git fetch origin master
+$ git evolve origin/master
+deleting metas/some_change_already_merged_upstream
+rebasing metas/this_is_a_test onto origin/master
+rebasing metas/this_is_also_a_test onto metas/this_is_a_test
+rebasing metas/more_testing onto metas/this_is_also_a_test
+rebasing metas/unrelated_change onto origin/master
+Conflict detected! Resolve it and then use git evolve --continue to resume.
+
+# Sort out the conflict
+$ git mergetool
+$ git evolve origin/master
+Done
+
+# Share the full history of edits for the this_is_a_test change
+# with a review server
+$ git push origin metas/this_is_a_test:refs/for/master
+# Share the lastest commit for “Unrelated change”, without history
+$ git push origin HEAD:refs/for/master
+
+Detailed design
+===============
+Obsolescence information is stored as a graph of meta-commits. A meta-commit is
+a specially-formatted merge commit that describes how one commit was created
+from others.
+
+Meta-commits look like this:
+
+$ git cat-file -p <example_meta_commit>
+tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
+parent aa7ce55545bf2c14bef48db91af1a74e2347539a
+parent d64309ee51d0af12723b6cb027fc9f195b15a5e9
+parent 7e1bbcd3a0fa854a7a9eac9bf1eea6465de98136
+author Stefan Xenos <sxenos@gmail.com> 1540841596 -0700
+committer Stefan Xenos <sxenos@gmail.com> 1540841596 -0700
+parent-type c r o
+
+This says “commit aa7ce555 makes commit d64309ee obsolete. It was created by
+cherry-picking commit 7e1bbcd3”.
+
+The tree for meta-commits is always the empty tree, but future versions of git
+may attach other trees here. For forward-compatibility fsck should ignore such
+trees if found on future repository versions. This will allow future versions of
+git to add metadata to the meta-commit tree without breaking forwards
+compatibility.
+
+The commit comment for a meta-commit is an auto-generated user-readable string
+describing the command that produced the meta commit. These strings are shown
+to the user when they view the obslog.
+
+Parent-type
+-----------
+The “parent-type” field in the commit header identifies a commit as a
+meta-commit and indicates the meaning for each of its parents. It is never
+present for normal commits. It contains a space-deliminated list of enum values
+whose order matches the order of the parents. Possible parent types are:
+
+- c: (content) the content parent identifies the commit that this meta-commit is
+  describing.
+- r: (replaced) indicates that this parent is made obsolete by the content
+  parent.
+- o: (origin) indicates that the content parent was generated by cherry-picking
+  this parent.
+- a: (abandoned) used in place of a content parent for abandoned changes. Points
+  to the final content commit for the change at the time it was abandoned.
+
+There must be exactly one content or abandoned parent for each meta-commit and
+it is always the first parent. The content commit will always be a normal commit
+and not a meta-commit. However, future versions of git may create meta-commits
+for other meta-commits and the fsck tool must be aware of this for forwards
+compatibility.
+
+A meta-commit can have zero or more replaced parents. An amend operation creates
+a single replaced parent. A merge used to resolve divergence (see divergence,
+below) will create multiple replaced parents. A meta-commit may have no
+replaced parents if it describes a cherry-pick or squash merge that copies one
+or more commits but does not replace them.
+
+A meta-commit can have zero or more origin parents. A cherry-pick creates a
+single origin parent. Certain types of squash merge will create multiple origin
+parents. Origin parents don't directly cause their origin to become obsolete,
+but are used when computing blame or locating a merge base. The section
+on obsolescence over cherry-picks describes how the evolve command uses
+origin parents.
+
+A replaced parent or origin parent may be either a normal commit (indicating
+the oldest-known version of a change) or another meta-commit (for a change that
+has already been modified one or more times).
+
+The parent-type field needs to go after the committer field since git's rules
+for forwards-compatibility require that new fields to be at the end of the
+header. Putting a new field in the middle of the header would break fsck.
+
+The presence of an abandoned parent indicates that the change should be pruned
+by the evolve command, and removed from the repository's history. Any follow-up
+changes should rebased onto the parent of the pruned commit. The abandoned
+parent points to the version of the change that should be restored if the user
+attempts to restore the change.
+
+Changes
+-------
+A branch of meta-commits describes how a commit was produced and what previous
+commits it is based on. It is also an identifier for a thing the user is
+currently working on. We refer to such a meta-branch as a change.
+
+Local changes are stored in the new refs/metas namespace. Remote changes are
+stored in the refs/remote/<remotename>/metas namespace.
+
+The list of changes in refs/metas is more than just a mechanism for the evolve
+command to locate obsolete commits. It is also a convenient list of all of a
+user’s work in progress and their current state - a list of things they’re
+likely to want to come back to.
+
+Strictly speaking, it is the presence of the branch in the refs/metas namespace
+that marks a branch as being a change, not the fact that it points to a
+metacommit. Metacommits are only created when a commit is amended or rebased, so
+in the case where a change points to a commit that has never been modified, the
+change points to that initial commit rather than a metacommit.
+
+Changes are also stored in the refs/hiddenmetas namespace. Hiddenmetas holds
+metadata for historical changes that are not currently in progress by the user.
+Commands like filter-branch and other bulk import commands create metadata in
+this namespace.
+
+Note that the changes in hiddenmetas get special treatment in several ways:
+
+- They are not cleaned up automatically once merged, since it is expected that
+  they refer to historical changes.
+- User commands that modify changes don't append to these changes as they would
+  to a change in refs/metas.
+- They are not displayed when the user lists their local changes.
+
+Obsolescence
+------------
+A commit is considered obsolete if it is reachable from the “replaces” edges
+anywhere in the history of a change and it isn’t the head of that change.
+Commits may be the content for 0 or more meta-commits. If the same commit
+appears in multiple changes, it is not obsolete if it is the head of any of
+those changes.
+
+Note that there is an exception to this rule. The metas namespace takes
+precedence over the hiddenmetas namespace for the purpose of obsolescence. That
+is, if a change appears in a replaces edge of a change in the metas namespace,
+it is obsolete even if it also appears as the head of a change in the
+hiddenmetas namespace.
+
+This special case prevents the hiddenmetas namespace from creating divergence
+with the user's work in progress, and allows the user to resolve historical
+divergence by creating new changes in the metas namespace.
+
+Divergence
+----------
+From the user’s perspective, two changes are divergent if they both ask for
+different replacements to the same commit. More precisely, a target commit is
+considered divergent if there is more than one commit at the head of a change in
+refs/metas that leads to the target commit via an unbroken chain of “replaces”
+parents.
+
+Much like a merge conflict, divergence is a situation that requires user
+intervention to resolve. The evolve command will stop when it encounters
+divergence and prompt the user to resolve the problem. Users can solve the
+problem in several ways:
+
+- Discard one of the changes (by deleting its change branch).
+- Merge the two changes (producing a single change branch).
+- Copy one of the changes (keep both commits, but one of them gets a new
+  metacommit appended to its history that is connected to its predecessor via an
+  origin edge rather than a replaces edge. That new change no longer obsoletes
+  the original.)
+
+Obsolescence across cherry-picks
+--------------------------------
+By default the evolve command will treat cherry-picks and squash merges as being
+completely separate from the original. Further amendments to the original commit
+will have no effect on the cherry-picked copy. However, this behavior may not be
+desirable in all circumstances.
+
+The evolve command may at some point support an option to look for cases where
+the source of a cherry-pick or squash merge has itself been amended, and
+automatically apply that same change to the cherry-picked copy. In such cases,
+it would traverse origin edges rather than ignoring them, and would treat a
+commit with origin edges as being obsolete if any of its origins were obsolete.
+
+Garbage collection
+------------------
+For GC purposes, meta-commits are normal commits. Just as a commit causes its
+parents and tree to be retained, a meta-commit also causes its parents to be
+retained.
+
+Change creation
+---------------
+Changes are created automatically whenever the user runs a command like “commit”
+that has the semantics of creating a new change. They also move forward
+automatically even if they’re not checked out. For example, whenever the user
+runs a command like “commit --amend” that modifies a commit, all branches in
+refs/metas that pointed to the old commit move forward to point to its
+replacement instead. This also happens when the user is working from a detached
+head.
+
+This does not mean that every commit has a corresponding change. By default,
+changes only exist for recent locally-created commits. Users may explicitly pull
+changes from other users or keep their changes around for a long time, but
+either behavior requires a user to opt-in. Code review systems like gerrit may
+also choose to keep changes around forever.
+
+Note that the changes in refs/metas serve a dual function as both a way to
+identify obsolete changes and as a way for the user to keep track of their work
+in progress. If we were only concerned with identifying obsolete changes, it
+would be sufficient to create the change branch lazily the first time a commit
+is obsoleted. Addressing the second use - of refs/metas as a mechanism for
+keeping track of work in progress - is the reason for eagerly creating the
+change on first commit.
+
+Change naming
+-------------
+When a change is first created, the only requirement for its name is that it
+must be unique. Good names would also serve as useful mnemonics and be easy to
+type. For example, a short word from the commit message containing no numbers or
+special characters and that shows up with low frequency in other commit messages
+would make a good choice.
+
+Different users may prefer different heuristics for their change names. For this
+reason a new hook will be introduced to compute change names. Git will invoke
+the hook for all newly-created changes and will append a numeric suffix if the
+name isn’t unique. The default heuristics are not specified by this proposal and
+may change during implementation.
+
+Change deletion
+---------------
+Changes are normally only interesting to a user while a commit is still in
+development and under review. Once the commit has submitted wherever it is
+going, its change can be discarded.
+
+The normal way of deleting changes makes this easy to do - changes are deleted
+by the evolve command when it detects that the change is present in an upstream
+branch. It does this in two ways: if the latest commit in a change either shows
+up in the branch history or the change becomes empty after a rebase, it is
+considered merged and the change is discarded. In this context, an “upstream
+branch” is any branch passed in as the upstream argument of the evolve command.
+
+In case this sometimes deletes a useful change, such automatic deletions are
+recorded in the reflog allowing them to be easily recovered.
+
+Sharing changes
+---------------
+Change histories are shared by pushing or fetching meta-commits and change
+branches. This provides users with a lot of control of what to share and
+repository implementations with control over what to retain.
+
+Users that only want to share the content of a commit can do so by pushing the
+commit itself as they currently would. Users that want to share an edit history
+for the commit can push its change, which would point to a meta-commit rather
+than the commit itself if there is any history to share. Note that multiple
+changes can refer to the same commits, so it’s possible to construct and push a
+different history for the same commit in order to remove sensitive or irrelevant
+intermediate states.
+
+Imagine the user is working on a change “mychange” that is currently the latest
+commit on master. They have two ways to share it:
+
+# User shares just a commit without its history
+> git push origin master
+
+# User shares the full history of the commit to a review system
+> git push origin metas/mychange:refs/for/master
+
+# User fetches a collaborator’s modifications to their change
+> git fetch remotename metas/mychange
+# Which updates the ref remote/remotename/metas/mychange
+
+This will cause more intermediate states to be shared with the server than would
+have been shared previously. A review system like gerrit would need to keep
+track of which states had been explicitly pushed versus other intermediate
+states in order to de-emphasize (or hide) the extra intermediate states from the
+user interface.
+
+Merge-base
+----------
+Merge-base will be changed to search the meta-commit graph for common ancestors
+as well as the commit graph, and will generally prefer results from the
+meta-commit graph over the commit graph. Merge-base will consider meta-commits
+from all changes, and will traverse both origin and obsolete edges.
+
+The reason for this is that - when merging two versions of the same commit
+together - an earlier version of that same commit will usually be much more
+similar than their common parent. This should make the workflow of collaborating
+on unsubmitted patches as convenient as the workflow for collaborating in a
+topic branch by eliminating repeated merges.
+
+Configuration
+-------------
+The core.enableChanges configuration variable enables the creation and update
+of change branches. This is enabled by default.
+
+User interface
+--------------
+All git porcelain commands that create commits are classified as having one of
+four behaviors: modify, create, copy, or import. These behaviors are discussed
+in more detail below.
+
+Modify commands
+---------------
+Modification commands (commit --amend, rebase) will mark the old commit as
+obsolete by creating a new meta-commit that references the old one as a
+replaced parent. In the event that multiple changes point to the same commit,
+this is done independently for every such change.
+
+More specifically, modifications work like this:
+
+1. Locate all existing changes for which the old commit is the content for the
+   head of the change branch. If no such branch exists, create one that points
+   to the old commit. Changes that include this commit in their history but not
+   at their head are explicitly not included.
+2. For every such change, create a new meta-commit that references the new
+   commit as its content and references the old head of the change as a
+   replaced parent.
+3. Move the change branch forward to point to the new meta-commit.
+
+Copy commands
+-------------
+Copy commands (cherry-pick, merge --squash) create a new meta-commit that
+references the old commits as origin parents. Besides the fact that the new
+parents are tagged differently, copy commands work the same way as modify
+commands.
+
+Create commands
+---------------
+Creation commands (commit, merge) create a new commit and a new change that
+points to that commit. The do not create any meta-commits.
+
+Import commands
+---------------
+Import commands (fetch, pull) do not create any new meta-commits or changes
+unless that is specifically what they are importing. For example, the fetch
+command would update remote/origin/metas/change35 and fetch all referenced
+meta-commits if asked to do so directly, but it wouldn’t create any changes or
+meta-commits for commits discovered on the master branch when running “git fetch
+origin master”.
+
+Other commands
+--------------
+Some commands don’t fit cleanly into one of the above categories.
+
+Semantically, filter-branch should be treated as a modify command, but doing so
+is likely to create a lot of irrelevant clutter in the changes namespace and the
+large number of extra change refs may introduce performance problems. We
+recommend treating filter-branch as an import command initially, but making it
+behave more like a modify command in future follow-up work. One possible
+solution may be to treat commits that are part of existing changes as being
+modified but to avoid creating changes for other rewritten changes. Another
+solution may be to record the modifications as changes in the hiddenmetas
+namespace.
+
+Once the evolve command can handle obsolescence across cherry-picks, such
+cherry-picks will result in a hybrid move-and-copy operation. It will create
+cherry-picks that replace other cherry-picks, which will have both origin edges
+(pointing to the new source commit being picked) and replacement edges (pointing
+to the previous cherry-pick being replaced).
+
+Evolve
+------
+The evolve command performs the correct sequence of rebases such that no change
+has an obsolete parent. The syntax looks like this:
+
+git evolve [upstream…]
+
+It takes an optional list of upstream branches. All changes whose parent shows
+up in the history of one of the upstream branches will be rebased onto the
+upstream branch before resolving obsolete parents.
+
+Any change whose latest state is found in an upstream branch (or that ends up
+empty after rebase) will be deleted. This is the normal mechanism for deleting
+changes. Changes are created automatically on the first commit, and are deleted
+automatically when evolve determines that they’ve been merged upstream.
+
+Orphan commits are commits with obsolete parents. The evolve command then
+repeatedly rebases orphan commits with non-orphan parents until there are either
+no orphan commits left, or a merge conflict is discovered. It will also
+terminate if it detects a divergent parent or a cycle that can't be resolved
+using any of the enabled transformations.
+
+When evolve discovers divergence, it will first check if it can resolve the
+divergence automatically using one of its enabled transformations. Supported
+transformations are:
+
+- Check if the user has already merged the divergent changes in a follow-up
+  change. That is, look for an existing merge in a follow-up change where all
+  the parents are divergent versions of the same change. Squash that merge with
+  its parents and use the result as the resolution for the divergence.
+
+- Attempt to auto-merge all the divergent changes (disabled by default).
+
+Each of the transformations can be enabled or disabled by command line options.
+
+Cycles can occur when two changes reference one another as parents. This can
+happen when both changes use an obsolete version of the other change as their
+parent. Although there are never cycles in the commit graph, users can create
+cycles in the change graph by rebasing changes onto obsolete commits. The evolve
+command has a transformation that will detect and break cycles by arbitrarily
+picking one of the changes to go first. If this generates a merge conflict,
+it tries each of the other changes in sequence to see if any ordering merges
+cleanly. If no possible ordering merges cleanly, it picks one and terminates
+to let the user resolve the merge conflict.
+
+If the working tree is dirty, evolve will attempt to stash the user's changes
+before applying the evolve and then reapply those changes afterward, in much
+the same way as rebase --autostash does.
+
+Checkout
+--------
+Running checkout on a change by name has the same effect as checking out a
+detached head pointing to the latest commit on that change-branch. There is no
+need to ever have HEAD point to a change since changes always move forward when
+necessary, no matter what branch the user has checked out
+
+Meta-commits themselves cannot be checked out by their hash.
+
+Reset
+-----
+Resetting a branch to a change by name is the same as resetting to the content
+(or abandoned) commit at that change’s head.
+
+Commit
+------
+Commit --amend gets modify semantics and will move existing changes forward. The
+normal form of commit gets create semantics and will create a new change.
+
+$ touch foo && git add . && git commit -m "foo" && git tag A
+$ touch bar && git add . && git commit -m "bar" && git tag B
+$ touch baz && git add . && git commit -m "baz" && git tag C
+
+This produces the following commits:
+A(tree=[foo])
+B(tree=[foo, bar], parent=A)
+C(tree=[foo, bar, baz], parent=B)
+
+...along with three changes:
+metas/foo = A
+metas/bar = B
+metas/baz = C
+
+Running commit --amend does the following:
+$ git checkout B
+$ touch zoom && git add . && git commit --amend -m "baz and zoom"
+$ git tag D
+
+Commits:
+A(tree=[foo])
+B(tree=[foo, bar], parent=A)
+C(tree=[foo, bar, baz], parent=B)
+D(tree=[foo, bar, zoom], parent=A)
+Dmeta(content=D, obsolete=B)
+
+Changes:
+metas/foo = A
+metas/bar = Dmeta
+metas/baz = C
+
+Merge
+-----
+Merge gets create, modify, or copy semantics based on what is being merged and
+the options being used.
+
+The --squash version of merge gets copy semantics (it produces a new change that
+is marked as a copy of all the original changes that were squashed into it).
+
+The “modify” version of merge replaces both of the original commits with the
+resulting merge commit. This is one of the standard mechanisms for resolving
+divergence. The parents of the merge commit are the parents of the two commits
+being merged. The resulting commit will not be a merge commit if both of the
+original commits had the same parent or if one was the parent of the other.
+
+The “create” version of merge creates a new change pointing to a merge commit
+that has both original commits as parents. The result is what merge produces now
+- a new merge commit. However, this version of merge doesn’t directly resolve
+divergence.
+
+To select between these two behaviors, merge gets new “--amend” and “--noamend”
+options which select between the “create” and “modify” behaviors respectively,
+with noamend being the default.
+
+For example, imagine we created two divergent changes like this:
+
+$ touch foo && git add . && git commit -m "foo" && git tag A
+$ touch bar && git add . && git commit -m "bar" && git tag B
+$ touch baz && git add . && git commit --amend -m "bar and baz"
+$ git tag C
+$ git checkout B
+$ touch bam && git add . && git commit --amend -m "bar and bam"
+$ git tag D
+
+At this point the commit graph looks like this:
+
+A(tree=[foo])
+B(tree=[bar], parent=A)
+C(tree=[bar, baz], parent=A)
+D(tree=[bar, bam], parent=A)
+Cmeta(content=C, obsoletes=B)
+Dmeta(content=D, obsoletes=B)
+
+There would be three active changes with heads pointing as follows:
+
+metas/changeA=A
+metas/changeB=Cmeta
+metas/changeB2=Dmeta
+
+ChangeB and changeB2 are divergent at this point. Lets consider what happens if
+perform each type of merge between changeB and changeB2.
+
+Merge example: Amend merge
+One way to resolve divergent changes is to use an amend merge. Recall that HEAD
+is currently pointing to D at this point.
+
+$ git merge --amend metas/changeB
+
+Here we’ve asked for an amend merge since we’re trying to resolve divergence
+between two versions of the same change. There are no conflicts so we end up
+with this:
+
+E(tree=[bar, baz, bam], parent=A)
+Emeta(content=E, obsoletes=[Cmeta, Dmeta])
+
+With the following branches:
+
+metas/changeA=A
+metas/changeB=Emeta
+metas/changeB2=Emeta
+
+Notice that the result of the “amend merge” is a replacement for C and D rather
+than a new commit with C and D as parents (as a normal merge would have
+produced). The parents of the amend merge are the parents of C and D which - in
+this case - is just A, so the result is not a merge commit. Also notice that
+changeB and changeB2 are now aliases for the same change.
+
+Merge example: Noamend merge
+Consider what would have happened if we’d used a noamend merge instead. Recall
+that HEAD was at D and our branches looked like this:
+
+metas/changeA=A
+metas/changeB=Cmeta
+metas/changeB2=Dmeta
+
+$ git merge --noamend metas/changeB
+
+That would produce the sort of merge we’d normally expect today:
+
+F(tree=[bar, baz, bam], parent=[C, D])
+
+And our changes would look like this:
+metas/changeA=A
+metas/changeB=Cmeta
+metas/changeB2=Dmeta
+metas/changeF=F
+
+In this case, changeB and changeB2 are still divergent and we’ve created a new
+change for our merge commit. However, this is just a temporary state. The next
+time we run the “evolve” command, it will discover the divergence but also
+discover the merge commit F that resolves it. Evolve will suggest converting F
+into an amend merge in order to resolve the divergence and will display the
+command for doing so.
+
+Rebase
+------
+In general the rebase command is treated as a modify command. When a change is
+rebased, the new commit replaces the original.
+
+Rebase --abort is special. Its intent is to restore git to the state it had
+prior to running rebase. It should move back any changes to point to the refs
+they had prior to running rebase and delete any new changes that were created as
+part of the rebase. To achieve this, rebase will save the state of all changes
+in refs/metas prior to running rebase and will restore the entire namespace
+after rebase completes (deleting any newly-created changes). Newly-created
+metacommits are left in place, but will have no effect until garbage collected
+since metacommits are only used if they are reachable from refs/metas.
+
+Change
+------
+The “change” command can be used to list, rename, reset or delete change. It has
+a number of subcommands.
+
+The "list" subcommand lists local changes. If given the -r argument, it lists
+remote changes.
+
+The "rename" subcommand renames a change, given its old and new name. If the old
+name is omitted and there is exactly one change pointing to the current HEAD,
+that change is renamed. If there are no changes pointing to the current HEAD,
+one is created with the given name.
+
+The "forget" subcommand deletes a change by deleting its ref from the metas/
+namespace. This is the normal way to delete extra aliases for a change if the
+change has more than one name. By default, this will refuse to delete the last
+alias for a change if there are any other changes that reference this change as
+a parent.
+
+The "update" subcommand adds a new state to a change. It uses the default
+algorithm for assigning change names. If the content commit is omitted, HEAD is
+used. If given the optional --force argument, it will overwrite any existing
+change of the same name. This latter form of "update" can be used to effectively
+reset changes.
+
+The "update" command can accept any number of --origin and --replace arguments.
+If any are present, the resulting change branch will point to a metacommit
+containing the given origin and replacement edges.
+
+The "abandon" command deletes a change using obsolescence markers. It marks the
+change as being obsolete and having been replaced by its parent. If given no
+arguments, it applies to the current commit. Running evolve will cause any
+abandoned changes to be removed from the branch. Any child changes will be
+reparented on top of the parent of the abandoned change. If the current change
+is abandoned, HEAD will move to point to its parent.
+
+The "restore" command restores a previously-abandoned change.
+
+The "prune" command deletes all obsolete changes and all changes that are
+present in the given branch. Note that such changes can be recovered from the
+reflog.
+
+Combined with the GC protection that is offered, this is intended to facilitate
+a workflow that relies on changes instead of branches. Users could choose to
+work with no local branches and use changes instead - both for mailing list and
+gerrit workflows.
+
+Log
+---
+When a commit is shown in git log that is part of a change, it is decorated with
+extra change information. If it is the head of a change, the name of the change
+is shown next to the list of branches. If it is obsolete, it is decorated with
+the text “obsolete, <n> commits behind <changename>”.
+
+Log gets a new --obslog argument indicating that the obsolescence graph should
+be followed instead of the commit graph. This also changes the default
+formatting options to make them more appropriate for viewing different
+iterations of the same commit.
+
+Pull
+----
+
+Pull gets an --evolve argument that will automatically attempt to run "evolve"
+on any affected branches after pulling.
+
+We also introduce an "evolve" enum value for the branch.<name>.rebase config
+value. When set, the evolve behavior will happen automatically for that branch
+after every pull even if the --evolve argument is not used.
+
+Next
+----
+
+The "next" command will reset HEAD to a non-obsolete commit that refers to this
+change as its parent. If there is more than one such change, the user will be
+prompted. If given the --evolve argument, the next commit will be evolved if
+necessary first.
+
+The "next" command can be thought of as the opposite of
+"git reset --hard HEAD^" in that it navigates to a child commit rather than a
+parent.
+
+Prev
+----
+
+The "prev" command will reset HEAD to the latest version of the parent change.
+If the parent change isn't obsolete, this is equivalent to
+"git reset --hard HEAD^". If the parent commit is obsolete, it resets to the
+latest replacement for the parent commit.
+
+Other options considered
+========================
+We considered several other options for storing the obsolescence graph. This
+section describes the other options and why they were rejected.
+
+Commit header
+-------------
+Add an “obsoletes” field to the commit header that points backwards from a
+commit to the previous commits it obsoletes.
+
+Pros:
+- Very simple
+- Easy to traverse from a commit to the previous commits it obsoletes.
+Cons:
+- Adds a cost to the storage format, even for commits where the change history
+  is uninteresting.
+- Unconditionally prevents the change history from being garbage collected.
+- Always causes the change history to be shared when pushing or pulling changes.
+
+Git notes
+---------
+Instead of storing obsolescence information in metacommits, the metacommit
+content could go in a new notes namespace - say refs/notes/metacommit. Each note
+would contain the list of obsolete and origin parents. An automerger could
+be supplied to make it easy to merge the metacommit notes from different remotes.
+
+Pros:
+- Easy to locate all commits obsoleted by a given commit (since there would only
+  be one metacommit for any given commit).
+Cons:
+- Wrong GC behavior (obsolete commits wouldn’t automatically be retained by GC)
+  unless we introduced a special case for these kinds of notes.
+- No way to selectively share or pull the metacommits for one specific change.
+  It would be all-or-nothing, which would be expensive. This could be addressed
+  by changes to the protocol, but this would be invasive.
+- Requires custom auto-merging behavior on fetch.
+
+Tags
+----
+Put the content of the metacommit in a message attached to tag on the
+replacement commit. This is very similar to the git notes approach and has the
+same pros and cons.
+
+Simple forward references
+-------------------------
+Record an edge from an obsolete commit to its replacement in this form:
+
+refs/obsoletes/<A>
+
+pointing to commit <B> as an indication that B is the replacement for the
+obsolete commit A.
+
+Pros:
+- Protects <B> from being garbage collected.
+- Fast lookup for the evolve operation, without additional search structures
+  (“what is the replacement for <A>?” is very fast).
+
+Cons:
+- Can’t represent divergence (which is a P0 requirement).
+- Creates lots of refs (which can be inefficient)
+- Doesn’t provide a way to fetch only refs for a specific change.
+- The obslog command requires a search of all refs.
+
+Complex forward references
+--------------------------
+Record an edge from an obsolete commit to its replacement in this form:
+
+refs/obsoletes/<change_id>/obs<A>_<B>
+
+Pointing to commit <B> as an indication that B is the replacement for obsolete
+commit A.
+
+Pros:
+- Permits sharing and fetching refs for only a specific change.
+- Supports divergence
+- Protects <B> from being garbage collected.
+
+Cons:
+- Creates lots of refs, which is inefficient.
+- Doesn’t provide a good lookup structure for lookups in either direction.
+
+Backward references
+-------------------
+Record an edge from a replacement commit to the obsolete one in this form:
+
+refs/obsolescences/<B>
+
+Cons:
+- Doesn’t provide a way to resolve divergence (which is a P0 requirement).
+- Doesn’t protect <B> from being garbage collected (which could be fixed by
+  combining this with a refs/metas namespace, as in the metacommit variant).
+
+Obsolescences file
+------------------
+Create a custom file (or files) in .git recording obsolescences.
+
+Pros:
+- Can store exactly the information we want with exactly the performance we want
+  for all operations. For example, there could be a disk-based hashtable
+  permitting constant time lookups in either direction.
+
+Cons:
+- Handling GC, pushing, and pulling would all require custom solutions. GC
+  issues could be addressed with a repository format extension.
+
+Squash points
+-------------
+We treat changes like topic branches, and use special squash points to mark
+places in the commit graph that separate changes.
+
+We create and update change branches in refs/metas at the same time we
+would have in the metacommit proposal. However, rather than pointing to a
+metacommit branch they point to normal commits and are treated as “squash
+points” - markers for sequences of commits intended to be squashed together on
+submission.
+
+Amends and rebases work differently than they do now. Rather than actually
+containing the desired state of a commit, they contain a delta from the previous
+version along with a squash point indicating that the preceding changes are
+intended to be squashed on submission. Specifically, amends would become new
+changes and rebases would become merge commits with the old commit and new
+parent as parents.
+
+When the changes are finally submitted, the squashes are executed, producing the
+final version of the commit.
+
+In addition to the squash points, git would maintain a set of “nosquash” tags
+for commits that were used as ancestors of a change that are not meant to be
+included in the squash.
+
+For example, if we have this commit graph:
+
+A(...)
+B(parent=A)
+C(parent=B)
+
+...and we amend B to produce D, we’d get:
+
+A(...)
+B(parent=A)
+C(parent=B)
+D(parent=B)
+
+...along with a new change branch indicating D should be squashed with its
+parents when submitted:
+
+metas/changeB = D
+metas/changeC = C
+
+We’d also create a nosquash tag for A indicating that A shouldn’t be included
+when changeB is squashed.
+
+If a user amends the change again, they’d get:
+
+A(...)
+B(parent=A)
+C(parent=B)
+D(parent=B)
+E(parent=D)
+
+metas/changeB = E
+metas/changeC = C
+
+Pros:
+- Good GC behavior.
+- Provides a natural way to share changes (they’re just normal branches).
+- Merge-base works automatically without special cases.
+- Rewriting the obslog would be easy using existing git commands.
+- No new data types needed.
+Cons:
+- No way to connect the squashed version of a change to the original, so no way
+  to automatically clean up old changes. This also means users lose all benefits
+  of the evolve command if they prematurely squash their commits. This may occur
+  if a user thinks a change is ready for submission, squashes it, and then later
+  discovers an additional change to make.
+- Histories would look very cluttered (users would see all previous edits to
+  their commit in the commit log, and all previous rebases would show up as
+  merges). Could be quite hard for users to tell what is going on. (Possible
+  fix: also implement a new smart log feature that displays the log as though
+  the squashes had occurred).
+- Need to change the current behavior of current commands (like amend and
+  rebase) in ways that will be unexpected to many users.
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v2 02/10] sha1-array: implement oid_array_readonly_contains
  2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
  2022-10-05 14:59   ` [PATCH v2 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
@ 2022-10-05 14:59   ` Chris Poucet via GitGitGadget
  2022-10-05 14:59   ` [PATCH v2 03/10] ref-filter: add the metas namespace to ref-filter Chris Poucet via GitGitGadget
                     ` (8 subsequent siblings)
  10 siblings, 0 replies; 66+ messages in thread
From: Chris Poucet via GitGitGadget @ 2022-10-05 14:59 UTC (permalink / raw)
  To: git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Chris Poucet

From: Chris Poucet <poucet@google.com>

Implement a "readonly_contains" function for oid_array that won't
sort the array if it is unsorted. This can be used to test containment in
the rare situations where the array order matters.

The function has intentionally been given a name that is more cumbersome
than the "lookup" function, which is what most callers will will want
in most situations.

Signed-off-by: Chris Poucet <poucet@google.com>
---
 oid-array.c               | 12 ++++++++++++
 oid-array.h               |  7 +++++++
 t/helper/test-oid-array.c |  6 ++++++
 t/t0064-oid-array.sh      | 22 ++++++++++++++++++++++
 4 files changed, 47 insertions(+)

diff --git a/oid-array.c b/oid-array.c
index 73ba76e9e9a..1e12651d245 100644
--- a/oid-array.c
+++ b/oid-array.c
@@ -28,6 +28,18 @@ static const struct object_id *oid_access(size_t index, const void *table)
 	return &array[index];
 }
 
+int oid_array_readonly_contains(const struct oid_array *array,
+				const struct object_id* oid) {
+	int i;
+
+	if (array->sorted)
+		return oid_pos(oid, array->oid, array->nr, oid_access) >= 0;
+	for (i = 0; i < array->nr; i++)
+		if (oideq(&array->oid[i], oid))
+			return 1;
+	return 0;
+}
+
 int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
 {
 	oid_array_sort(array);
diff --git a/oid-array.h b/oid-array.h
index f60f9af6741..e056eb61fa2 100644
--- a/oid-array.h
+++ b/oid-array.h
@@ -58,6 +58,13 @@ struct oid_array {
 
 #define OID_ARRAY_INIT { 0 }
 
+/**
+ * Sees whether an array contains an object ID. Optimized for when the array is
+ * sorted but does not require the array to be sorted.
+ */
+int oid_array_readonly_contains(const struct oid_array *array,
+				const struct object_id* oid);
+
 /**
  * Add an item to the set. The object ID will be placed at the end of the array
  * (but note that some operations below may lose this ordering).
diff --git a/t/helper/test-oid-array.c b/t/helper/test-oid-array.c
index d1324d086a2..0dbfc91ca8d 100644
--- a/t/helper/test-oid-array.c
+++ b/t/helper/test-oid-array.c
@@ -28,10 +28,16 @@ int cmd__oid_array(int argc, const char **argv)
 			if (get_oid_hex(arg, &oid))
 				die("not a hexadecimal oid: %s", arg);
 			printf("%d\n", oid_array_lookup(&array, &oid));
+		} else if (skip_prefix(line.buf, "readonly_contains ", &arg)) {
+			if (get_oid_hex(arg, &oid))
+				die("not a hexadecimal oid: %s", arg);
+			printf("%d\n", oid_array_readonly_contains(&array, &oid));
 		} else if (!strcmp(line.buf, "clear"))
 			oid_array_clear(&array);
 		else if (!strcmp(line.buf, "for_each_unique"))
 			oid_array_for_each_unique(&array, print_oid, NULL);
+		else if (!strcmp(line.buf, "for_each"))
+			oid_array_for_each(&array, print_oid, NULL);
 		else
 			die("unknown command: %s", line.buf);
 	}
diff --git a/t/t0064-oid-array.sh b/t/t0064-oid-array.sh
index 88c89e8f48a..aa677af132d 100755
--- a/t/t0064-oid-array.sh
+++ b/t/t0064-oid-array.sh
@@ -35,6 +35,28 @@ test_expect_success 'ordered enumeration with duplicate suppression' '
 	test_cmp expect actual
 '
 
+test_expect_success 'readonly_contains finds existing' '
+	echo 1 >expect &&
+	echoid "" 88 44 aa 55 >>expect &&
+	{
+		echoid append 88 44 aa 55 &&
+		echoid readonly_contains 55 &&
+		echo for_each
+	} | test-tool oid-array >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'readonly_contains non-existing query' '
+	echo 0 >expect &&
+	echoid "" 88 44 aa 55 >>expect &&
+	{
+		echoid append 88 44 aa 55 &&
+		echoid readonly_contains 33 &&
+		echo for_each
+	} | test-tool oid-array >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'lookup' '
 	{
 		echoid append 88 44 aa 55 &&
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v2 03/10] ref-filter: add the metas namespace to ref-filter
  2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
  2022-10-05 14:59   ` [PATCH v2 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
  2022-10-05 14:59   ` [PATCH v2 02/10] sha1-array: implement oid_array_readonly_contains Chris Poucet via GitGitGadget
@ 2022-10-05 14:59   ` Chris Poucet via GitGitGadget
  2022-10-05 14:59   ` [PATCH v2 04/10] evolve: add support for parsing metacommits Stefan Xenos via GitGitGadget
                     ` (7 subsequent siblings)
  10 siblings, 0 replies; 66+ messages in thread
From: Chris Poucet via GitGitGadget @ 2022-10-05 14:59 UTC (permalink / raw)
  To: git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Chris Poucet

From: Chris Poucet <poucet@google.com>

The metas namespace will contain refs for changes in progress. Add
support for searching this namespace.

Signed-off-by: Chris Poucet <poucet@google.com>
---
 ref-filter.c | 8 ++++++--
 ref-filter.h | 6 ++++--
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/ref-filter.c b/ref-filter.c
index fd1cb14b0f1..6a1789c623f 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -2200,7 +2200,8 @@ static int ref_kind_from_refname(const char *refname)
 	} ref_kind[] = {
 		{ "refs/heads/" , FILTER_REFS_BRANCHES },
 		{ "refs/remotes/" , FILTER_REFS_REMOTES },
-		{ "refs/tags/", FILTER_REFS_TAGS}
+		{ "refs/tags/", FILTER_REFS_TAGS},
+		{ "refs/metas/", FILTER_REFS_CHANGES }
 	};
 
 	if (!strcmp(refname, "HEAD"))
@@ -2218,7 +2219,8 @@ static int filter_ref_kind(struct ref_filter *filter, const char *refname)
 {
 	if (filter->kind == FILTER_REFS_BRANCHES ||
 	    filter->kind == FILTER_REFS_REMOTES ||
-	    filter->kind == FILTER_REFS_TAGS)
+	    filter->kind == FILTER_REFS_TAGS ||
+	    filter->kind == FILTER_REFS_CHANGES)
 		return filter->kind;
 	return ref_kind_from_refname(refname);
 }
@@ -2435,6 +2437,8 @@ int filter_refs(struct ref_array *array, struct ref_filter *filter, unsigned int
 			ret = for_each_fullref_in("refs/remotes/", ref_filter_handler, &ref_cbdata);
 		else if (filter->kind == FILTER_REFS_TAGS)
 			ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata);
+		else if (filter->kind == FILTER_REFS_CHANGES)
+			ret = for_each_fullref_in("refs/metas/", ref_filter_handler, &ref_cbdata);
 		else if (filter->kind & FILTER_REFS_ALL)
 			ret = for_each_fullref_in_pattern(filter, ref_filter_handler, &ref_cbdata);
 		if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD))
diff --git a/ref-filter.h b/ref-filter.h
index aa0eea4ecf5..db3ee44e4dc 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -16,9 +16,11 @@
 #define FILTER_REFS_TAGS           0x0002
 #define FILTER_REFS_BRANCHES       0x0004
 #define FILTER_REFS_REMOTES        0x0008
-#define FILTER_REFS_OTHERS         0x0010
+#define FILTER_REFS_CHANGES        0x0010
+#define FILTER_REFS_OTHERS         0x0040
 #define FILTER_REFS_ALL            (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
-				    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
+				    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS | \
+				    FILTER_REFS_CHANGES)
 #define FILTER_REFS_DETACHED_HEAD  0x0020
 #define FILTER_REFS_KIND_MASK      (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD)
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v2 04/10] evolve: add support for parsing metacommits
  2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
                     ` (2 preceding siblings ...)
  2022-10-05 14:59   ` [PATCH v2 03/10] ref-filter: add the metas namespace to ref-filter Chris Poucet via GitGitGadget
@ 2022-10-05 14:59   ` Stefan Xenos via GitGitGadget
  2022-10-05 14:59   ` [PATCH v2 05/10] evolve: add the change-table structure Stefan Xenos via GitGitGadget
                     ` (6 subsequent siblings)
  10 siblings, 0 replies; 66+ messages in thread
From: Stefan Xenos via GitGitGadget @ 2022-10-05 14:59 UTC (permalink / raw)
  To: git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Stefan Xenos

From: Stefan Xenos <sxenos@google.com>

This patch adds the get_metacommit_content method, which can classify
commits as either metacommits or normal commits, determine whether they
are abandoned, and extract the content commit's object id from the
metacommit.

Signed-off-by: Stefan Xenos <sxenos@google.com>
Signed-off-by: Chris Poucet <poucet@google.com>
---
 Makefile            |  1 +
 commit.c            | 13 ++++++
 commit.h            |  5 +++
 metacommit-parser.c | 97 +++++++++++++++++++++++++++++++++++++++++++++
 metacommit-parser.h | 19 +++++++++
 5 files changed, 135 insertions(+)
 create mode 100644 metacommit-parser.c
 create mode 100644 metacommit-parser.h

diff --git a/Makefile b/Makefile
index cac3452edb9..b2bcc00c289 100644
--- a/Makefile
+++ b/Makefile
@@ -999,6 +999,7 @@ LIB_OBJS += merge-ort.o
 LIB_OBJS += merge-ort-wrappers.o
 LIB_OBJS += merge-recursive.o
 LIB_OBJS += merge.o
+LIB_OBJS += metacommit-parser.o
 LIB_OBJS += midx.o
 LIB_OBJS += name-hash.o
 LIB_OBJS += negotiator/default.o
diff --git a/commit.c b/commit.c
index 89b8efc6116..3eabb66fb6b 100644
--- a/commit.c
+++ b/commit.c
@@ -623,6 +623,19 @@ struct commit_list *reverse_commit_list(struct commit_list *list)
 	return next;
 }
 
+struct commit *get_commit_by_index(struct commit_list *to_search, int index)
+{
+	while (to_search && index) {
+		to_search = to_search->next;
+		index--;
+	}
+
+	if (!to_search)
+		return NULL;
+
+	return to_search->item;
+}
+
 void free_commit_list(struct commit_list *list)
 {
 	while (list)
diff --git a/commit.h b/commit.h
index 21e4d25ce78..11861a5a78c 100644
--- a/commit.h
+++ b/commit.h
@@ -188,8 +188,13 @@ struct commit_list *copy_commit_list(struct commit_list *list);
 /* Modify list in-place to reverse it, returning new head; list will be tail */
 struct commit_list *reverse_commit_list(struct commit_list *list);
 
+/* Returns the commit at `index` or NULL if the index exceeds the `to_search`
+ * list */
+struct commit *get_commit_by_index(struct commit_list *to_search, int index);
+
 void free_commit_list(struct commit_list *list);
 
+
 struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
 
 int has_non_ascii(const char *text);
diff --git a/metacommit-parser.c b/metacommit-parser.c
new file mode 100644
index 00000000000..baccfb4dd5c
--- /dev/null
+++ b/metacommit-parser.c
@@ -0,0 +1,97 @@
+#include "cache.h"
+#include "metacommit-parser.h"
+#include "commit.h"
+
+/*
+ * Search the commit buffer for a line starting with the given key. Unlike
+ * find_commit_header, this also searches the commit message body.
+ */
+static const char *find_key(const char *msg, const char *key, size_t *out_len)
+{
+	int key_len = strlen(key);
+	const char *line = msg;
+
+	while (line) {
+		const char *eol = strchrnul(line, '\n');
+
+		if (eol - line > key_len && !memcmp(line, key, key_len) &&
+		    line[key_len] == ' ') {
+			*out_len = eol - line - key_len - 1;
+			return line + key_len + 1;
+		}
+		line = *eol ? eol + 1 : NULL;
+	}
+	return NULL;
+}
+
+/*
+ * Writes the index of the content parent to "result". Returns the metacommit
+ * type. See the METACOMMIT_TYPE_* constants.
+ */
+static enum metacommit_type index_of_content_commit(const char *buffer, int *result)
+{
+	int index = 0;
+	int ret = METACOMMIT_TYPE_NONE;
+	size_t parent_types_size;
+	const char *parent_types = find_key(buffer, "parent-type",
+		&parent_types_size);
+	const char *end;
+	const char *enum_start = parent_types;
+	int enum_length = 0;
+
+	if (!parent_types)
+		return METACOMMIT_TYPE_NONE;
+
+	end = &parent_types[parent_types_size];
+
+	while (1) {
+		char next = *parent_types;
+		if (next == ' ' || parent_types >= end) {
+			if (enum_length == 1) {
+				char type = *enum_start;
+				if (type == 'c') {
+					ret = METACOMMIT_TYPE_NORMAL;
+					break;
+				}
+				if (type == 'a') {
+					ret = METACOMMIT_TYPE_ABANDONED;
+					break;
+				}
+			}
+			if (parent_types >= end)
+				return METACOMMIT_TYPE_NONE;
+			enum_start = parent_types + 1;
+			enum_length = 0;
+			index++;
+		} else {
+			enum_length++;
+		}
+		parent_types++;
+	}
+
+	*result = index;
+	return ret;
+}
+
+/*
+ * Writes the content parent's object id to "content".
+ * Returns the metacommit type. See the METACOMMIT_TYPE_* constants.
+ */
+enum metacommit_type get_metacommit_content(struct commit *commit, struct object_id *content)
+{
+	const char *buffer = get_commit_buffer(commit, NULL);
+	int index = 0;
+	enum metacommit_type ret = index_of_content_commit(buffer, &index);
+	struct commit *content_parent;
+
+	if (ret == METACOMMIT_TYPE_NONE)
+		return ret;
+
+	content_parent = get_commit_by_index(commit->parents, index);
+
+	if (!content_parent)
+		return METACOMMIT_TYPE_NONE;
+
+	oidcpy(content, &(content_parent->object.oid));
+	return ret;
+}
diff --git a/metacommit-parser.h b/metacommit-parser.h
new file mode 100644
index 00000000000..ef4a121d433
--- /dev/null
+++ b/metacommit-parser.h
@@ -0,0 +1,19 @@
+#ifndef METACOMMIT_PARSER_H
+#define METACOMMIT_PARSER_H
+
+#include "commit.h"
+#include "hash.h"
+
+enum metacommit_type {
+	/* Indicates a normal commit (non-metacommit) */
+	METACOMMIT_TYPE_NONE = 0,
+	/* Indicates a metacommit with normal content (non-abandoned) */
+	METACOMMIT_TYPE_NORMAL = 1,
+	/* Indicates a metacommit with abandoned content */
+	METACOMMIT_TYPE_ABANDONED = 2,
+};
+
+enum metacommit_type get_metacommit_content(
+	struct commit *commit, struct object_id *content);
+
+#endif
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v2 05/10] evolve: add the change-table structure
  2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
                     ` (3 preceding siblings ...)
  2022-10-05 14:59   ` [PATCH v2 04/10] evolve: add support for parsing metacommits Stefan Xenos via GitGitGadget
@ 2022-10-05 14:59   ` Stefan Xenos via GitGitGadget
  2022-10-05 14:59   ` [PATCH v2 06/10] evolve: add support for writing metacommits Stefan Xenos via GitGitGadget
                     ` (5 subsequent siblings)
  10 siblings, 0 replies; 66+ messages in thread
From: Stefan Xenos via GitGitGadget @ 2022-10-05 14:59 UTC (permalink / raw)
  To: git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Stefan Xenos

From: Stefan Xenos <sxenos@google.com>

A change table stores a list of changes, and supports efficient lookup
from a commit hash to the list of changes that reference that commit
directly.

It can be used to look up content commits or metacommits at the head
of a change, but does not support lookup of commits referenced as part
of the commit history.

Signed-off-by: Stefan Xenos <sxenos@google.com>
Signed-off-by: Chris Poucet <poucet@google.com>
---
 Makefile       |   1 +
 change-table.c | 164 +++++++++++++++++++++++++++++++++++++++++++++++++
 change-table.h | 122 ++++++++++++++++++++++++++++++++++++
 3 files changed, 287 insertions(+)
 create mode 100644 change-table.c
 create mode 100644 change-table.h

diff --git a/Makefile b/Makefile
index b2bcc00c289..2b847e7e7de 100644
--- a/Makefile
+++ b/Makefile
@@ -913,6 +913,7 @@ LIB_OBJS += bulk-checkin.o
 LIB_OBJS += bundle-uri.o
 LIB_OBJS += bundle.o
 LIB_OBJS += cache-tree.o
+LIB_OBJS += change-table.o
 LIB_OBJS += cbtree.o
 LIB_OBJS += chdir-notify.o
 LIB_OBJS += checkout.o
diff --git a/change-table.c b/change-table.c
new file mode 100644
index 00000000000..1d3d64b36d8
--- /dev/null
+++ b/change-table.c
@@ -0,0 +1,164 @@
+#include "cache.h"
+#include "change-table.h"
+#include "commit.h"
+#include "ref-filter.h"
+#include "metacommit-parser.h"
+
+void change_table_init(struct change_table *table)
+{
+	memset(table, 0, sizeof(*table));
+	mem_pool_init(&table->memory_pool, 0);
+	oidmap_init(&table->oid_to_metadata_index, 0);
+	strmap_init(&table->refname_to_change_head);
+}
+
+static void change_list_clear(struct change_list *change_list) {
+	strset_clear(&change_list->refnames);
+}
+
+static void commit_change_list_entry_clear(
+	struct commit_change_list_entry *entry) {
+	change_list_clear(&entry->changes);
+}
+
+void change_table_clear(struct change_table *table)
+{
+	struct oidmap_iter iter;
+	struct commit_change_list_entry *next;
+	for (next = oidmap_iter_first(&table->oid_to_metadata_index, &iter);
+		next;
+		next = oidmap_iter_next(&iter)) {
+
+		commit_change_list_entry_clear(next);
+	}
+
+	oidmap_free(&table->oid_to_metadata_index, 0);
+	strmap_clear(&table->refname_to_change_head, 0);
+	mem_pool_discard(&table->memory_pool, 0);
+}
+
+static void add_head_to_commit(struct change_table *table,
+			       const struct object_id *to_add,
+			       const char *refname)
+{
+	struct commit_change_list_entry *entry;
+
+	entry = oidmap_get(&table->oid_to_metadata_index, to_add);
+	if (!entry) {
+		entry = mem_pool_calloc(&table->memory_pool, 1, sizeof(*entry));
+		oidcpy(&entry->entry.oid, to_add);
+		strset_init(&entry->changes.refnames);
+		oidmap_put(&table->oid_to_metadata_index, entry);
+	}
+	strset_add(&entry->changes.refnames, refname);
+}
+
+void change_table_add(struct change_table *table,
+		      const char *refname,
+		      struct commit *to_add)
+{
+	struct change_head *new_head;
+	int metacommit_type;
+
+	new_head = mem_pool_calloc(&table->memory_pool, 1, sizeof(*new_head));
+
+	oidcpy(&new_head->head, &to_add->object.oid);
+
+	metacommit_type = get_metacommit_content(to_add, &new_head->content);
+	/* If to_add is not a metacommit then the content is to_add itself,
+	 * otherwise it will have been set by the call to
+	 * get_metacommit_content.
+	 */
+	if (metacommit_type == METACOMMIT_TYPE_NONE)
+		oidcpy(&new_head->content, &to_add->object.oid);
+	new_head->abandoned = (metacommit_type == METACOMMIT_TYPE_ABANDONED);
+	new_head->remote = starts_with(refname, "refs/remote/");
+	new_head->hidden = starts_with(refname, "refs/hiddenmetas/");
+
+	strmap_put(&table->refname_to_change_head, refname, new_head);
+
+	if (!oideq(&new_head->content, &new_head->head)) {
+		/* We also remember to link between refname and the content oid */
+		add_head_to_commit(table, &new_head->content, refname);
+	}
+	add_head_to_commit(table, &new_head->head, refname);
+}
+
+static void change_table_add_matching_filter(struct change_table *table,
+					     struct repository* repo,
+					     struct ref_filter *filter)
+{
+	int i;
+	struct ref_array matching_refs = { 0 };
+
+	filter_refs(&matching_refs, filter, filter->kind);
+
+	/*
+	 * Determine the object id for the latest content commit for each change.
+	 * Fetch the commit at the head of each change ref. If it's a normal commit,
+	 * that's the commit we want. If it's a metacommit, locate its content parent
+	 * and use that.
+	 */
+
+	for (i = 0; i < matching_refs.nr; i++) {
+		struct ref_array_item *item = matching_refs.items[i];
+		struct commit *commit;
+
+		commit = lookup_commit_reference(repo, &item->objectname);
+		if (!commit) {
+			BUG("Invalid commit for refs/meta: %s", item->refname);
+		}
+		change_table_add(table, item->refname, commit);
+	}
+
+	ref_array_clear(&matching_refs);
+}
+
+void change_table_add_all_visible(struct change_table *table,
+	struct repository* repo)
+{
+	struct ref_filter filter = { 0 };
+	const char *name_patterns[] = {NULL};
+	filter.kind = FILTER_REFS_CHANGES;
+	filter.name_patterns = name_patterns;
+
+	change_table_add_matching_filter(table, repo, &filter);
+}
+
+static int return_true_callback(const char *refname, void *cb_data)
+{
+	return 1;
+}
+
+int change_table_has_change_referencing(struct change_table *table,
+	const struct object_id *referenced_commit_id)
+{
+	return for_each_change_referencing(table, referenced_commit_id,
+		return_true_callback, NULL);
+}
+
+int for_each_change_referencing(struct change_table *table,
+	const struct object_id *referenced_commit_id, each_change_fn fn, void *cb_data)
+{
+	int ret;
+	struct commit_change_list_entry *ccl_entry;
+	struct hashmap_iter iter;
+	struct strmap_entry *entry;
+
+	ccl_entry = oidmap_get(&table->oid_to_metadata_index,
+			       referenced_commit_id);
+	/* If this commit isn't referenced by any changes, it won't be in the map */
+	if (!ccl_entry)
+		return 0;
+	strset_for_each_entry(&ccl_entry->changes.refnames, &iter, entry) {
+		ret = fn(entry->key, cb_data);
+		if (ret != 0) break;
+	}
+	return ret;
+}
+
+struct change_head* get_change_head(struct change_table *table,
+	const char* refname)
+{
+	return strmap_get(&table->refname_to_change_head, refname);
+}
diff --git a/change-table.h b/change-table.h
new file mode 100644
index 00000000000..85c2fb80d18
--- /dev/null
+++ b/change-table.h
@@ -0,0 +1,122 @@
+#ifndef CHANGE_TABLE_H
+#define CHANGE_TABLE_H
+
+#include "oidmap.h"
+#include "strmap.h"
+
+struct commit;
+struct ref_filter;
+
+/**
+ * This struct holds a set of change refs.
+ */
+struct change_list {
+	/**
+	 * The refnames in this set.
+	 * This field is private. Use for_each_change_in to read.
+	 */
+	struct strset refnames;
+};
+
+/**
+ * Holds information about the head of a single change.
+ */
+struct change_head {
+	/**
+	 * The location pointed to by the head of the change. May be a commit or a
+	 * metacommit.
+	 */
+	struct object_id head;
+	/**
+	 * The content commit for the latest commit in the change. Always points to a
+	 * real commit, never a metacommit.
+	 */
+	struct object_id content;
+	/**
+	 * Abandoned: indicates that the content commit should be removed from the
+	 * history.
+	 *
+	 * Hidden: indicates that the change is an inactive change from the
+	 * hiddenmetas namespace. Such changes will be hidden from the user by
+	 * default.
+	 *
+	 * Deleted: indicates that the change has been removed from the repository.
+	 * That is the ref was deleted since the time this struct was created. Such
+	 * entries should be ignored.
+	 */
+	unsigned int abandoned:1,
+		hidden:1,
+		remote:1,
+		deleted:1;
+};
+
+/**
+ * Holds the list of change refs whose content points to a particular content
+ * commit.
+ */
+struct commit_change_list_entry {
+	struct oidmap_entry entry;
+	struct change_list changes;
+};
+
+/**
+ * Holds information about the heads of each change, and permits efficient
+ * lookup from a commit to the changes that reference it directly.
+ *
+ * All fields should be considered private. Use the change_table functions
+ * to interact with this struct.
+ */
+struct change_table {
+	/**
+	 * Memory pool for the objects allocated by the change table.
+	 */
+	struct mem_pool memory_pool;
+	/* Map object_id to commit_change_list_entry structs. */
+	struct oidmap oid_to_metadata_index;
+	/**
+	 * Map of refnames to change_head structure which are allocated from
+	 * memory_pool.
+	 */
+	struct strmap refname_to_change_head;
+};
+
+extern void change_table_init(struct change_table *table);
+extern void change_table_clear(struct change_table *table);
+
+/* Adds the given change head to the change_table struct */
+extern void change_table_add(struct change_table *table,
+			     const char *refname,
+			     struct commit *target);
+
+/**
+ * Adds the non-hidden local changes to the given change_table struct.
+ */
+extern void change_table_add_all_visible(struct change_table *table,
+					 struct repository *repo);
+
+typedef int each_change_fn(const char *refname, void *cb_data);
+
+extern int change_table_has_change_referencing(
+	struct change_table *table,
+	const struct object_id *referenced_commit_id);
+
+/**
+ * Iterates over all changes that reference the given commit. For metacommits,
+ * this is the list of changes that point directly to that metacommit.
+ * For normal commits, this is the list of changes that have this commit as
+ * their latest content.
+ */
+extern int for_each_change_referencing(
+	struct change_table *table,
+	const struct object_id *referenced_commit_id,
+	each_change_fn fn,
+	void *cb_data);
+
+/**
+ * Returns the change head for the given refname. Returns NULL if no such change
+ * exists.
+ */
+extern struct change_head* get_change_head(struct change_table *table,
+	const char* refname);
+
+#endif
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v2 06/10] evolve: add support for writing metacommits
  2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
                     ` (4 preceding siblings ...)
  2022-10-05 14:59   ` [PATCH v2 05/10] evolve: add the change-table structure Stefan Xenos via GitGitGadget
@ 2022-10-05 14:59   ` Stefan Xenos via GitGitGadget
  2022-10-05 14:59   ` [PATCH v2 07/10] evolve: implement the git change command Stefan Xenos via GitGitGadget
                     ` (4 subsequent siblings)
  10 siblings, 0 replies; 66+ messages in thread
From: Stefan Xenos via GitGitGadget @ 2022-10-05 14:59 UTC (permalink / raw)
  To: git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Stefan Xenos

From: Stefan Xenos <sxenos@google.com>

metacommit.c supports the creation of metacommits and
adds the API needed to create and update changes.

Create the "modify_change" function that can be called from modification
commands like "rebase" and "git amend" to record obsolescences in the
change graph.

Create the "record_metacommit" function for recording more complicated
commit relationships in the commit graph.

Create the "write_metacommit" function for low-level creation of
metacommits.

Signed-off-by: Stefan Xenos <sxenos@google.com>
Signed-off-by: Chris Poucet <poucet@google.com>
---
 Makefile     |   1 +
 metacommit.c | 410 +++++++++++++++++++++++++++++++++++++++++++++++++++
 metacommit.h |  75 ++++++++++
 3 files changed, 486 insertions(+)
 create mode 100644 metacommit.c
 create mode 100644 metacommit.h

diff --git a/Makefile b/Makefile
index 2b847e7e7de..68082ef94c7 100644
--- a/Makefile
+++ b/Makefile
@@ -1000,6 +1000,7 @@ LIB_OBJS += merge-ort.o
 LIB_OBJS += merge-ort-wrappers.o
 LIB_OBJS += merge-recursive.o
 LIB_OBJS += merge.o
+LIB_OBJS += metacommit.o
 LIB_OBJS += metacommit-parser.o
 LIB_OBJS += midx.o
 LIB_OBJS += name-hash.o
diff --git a/metacommit.c b/metacommit.c
new file mode 100644
index 00000000000..3c2e3ae1031
--- /dev/null
+++ b/metacommit.c
@@ -0,0 +1,410 @@
+#include "cache.h"
+#include "metacommit.h"
+#include "commit.h"
+#include "change-table.h"
+#include "refs.h"
+
+void clear_metacommit_data(struct metacommit_data *state)
+{
+	oidcpy(&state->content, null_oid());
+	oid_array_clear(&state->replace);
+	oid_array_clear(&state->origin);
+	state->abandoned = 0;
+}
+
+static void compute_default_change_name(struct commit *initial_commit,
+					struct strbuf* result)
+{
+	struct strbuf default_name = STRBUF_INIT;
+	const char *buffer;
+	const char *subject;
+	const char *eol;
+	size_t len;
+	buffer = get_commit_buffer(initial_commit, NULL);
+	find_commit_subject(buffer, &subject);
+	eol = strchrnul(subject, '\n');
+	for (len = 0; subject < eol && len < 10; subject++, len++) {
+		char next = *subject;
+		if (isspace(next))
+			continue;
+
+		strbuf_addch(&default_name, next);
+	}
+	sanitize_refname_component(default_name.buf, result);
+	unuse_commit_buffer(initial_commit, buffer);
+}
+
+/*
+ * Computes a change name for a change rooted at the given initial commit. Good
+ * change names should be memorable, unique, and easy to type. They are not
+ * required to match the commit comment.
+ */
+static void compute_change_name(struct commit *initial_commit, struct strbuf* result)
+{
+	struct strbuf default_name = STRBUF_INIT;
+	struct object_id unused;
+
+	if (initial_commit)
+		compute_default_change_name(initial_commit, &default_name);
+	else
+		BUG("initial commit is NULL");
+	strbuf_addstr(result, "refs/metas/");
+	strbuf_addbuf(result, &default_name);
+
+	/* If there is already a change of this name, append a suffix */
+	if (!read_ref(result->buf, &unused)) {
+		int suffix = 2;
+		size_t original_length = result->len;
+
+		while (1) {
+			strbuf_addf(result, "%d", suffix);
+			if (read_ref(result->buf, &unused))
+				break;
+			strbuf_remove(result, original_length,
+				      result->len - original_length);
+			++suffix;
+		}
+	}
+
+	strbuf_release(&default_name);
+}
+
+struct resolve_metacommit_context
+{
+	struct change_table* active_changes;
+	struct string_list *changes;
+	struct oid_array *heads;
+};
+
+static int resolve_metacommit_callback(const char *refname, void *cb_data)
+{
+	struct resolve_metacommit_context *data = cb_data;
+	struct change_head *chhead;
+
+	chhead = get_change_head(data->active_changes, refname);
+
+	if (data->changes)
+		string_list_append(data->changes, refname)->util = &chhead->head;
+	if (data->heads)
+		oid_array_append(data->heads, &(chhead->head));
+
+	return 0;
+}
+
+/*
+ * Produces the final form of a metacommit based on the current change refs.
+ */
+static void resolve_metacommit(
+	struct repository* repo,
+	struct change_table* active_changes,
+	const struct metacommit_data *to_resolve,
+	struct metacommit_data *resolved_output,
+	struct string_list *to_advance,
+	int allow_append)
+{
+	size_t i;
+	size_t len = to_resolve->replace.nr;
+	struct resolve_metacommit_context ctx = {
+		.active_changes = active_changes,
+		.changes = to_advance,
+		.heads = &resolved_output->replace
+	};
+	int old_change_list_length = to_advance->nr;
+	struct commit* content;
+
+	oidcpy(&resolved_output->content, &to_resolve->content);
+
+	/*
+	 * First look for changes that point to any of the replacement edges in the
+	 * metacommit. These will be the changes that get advanced by this
+	 * metacommit.
+	 */
+	resolved_output->abandoned = to_resolve->abandoned;
+
+	if (allow_append) {
+		for (i = 0; i < len; i++) {
+			int old_number = resolved_output->replace.nr;
+			for_each_change_referencing(
+				active_changes,
+				&(to_resolve->replace.oid[i]),
+				resolve_metacommit_callback,
+				&ctx);
+			/* If no changes were found, use the unresolved value. */
+			if (old_number == resolved_output->replace.nr)
+				oid_array_append(&(resolved_output->replace),
+						 &(to_resolve->replace.oid[i]));
+		}
+	}
+
+	ctx.changes = NULL;
+	ctx.heads = &(resolved_output->origin);
+
+	len = to_resolve->origin.nr;
+	for (i = 0; i < len; i++) {
+		int old_number = resolved_output->origin.nr;
+		for_each_change_referencing(
+			active_changes,
+			&(to_resolve->origin.oid[i]),
+			resolve_metacommit_callback,
+			&ctx);
+		if (old_number == resolved_output->origin.nr)
+			oid_array_append(&(resolved_output->origin),
+					 &(to_resolve->origin.oid[i]));
+	}
+
+	/*
+	 * If no changes were advanced by this metacommit, we'll need to create
+	 * a new one. */
+	if (to_advance->nr == old_change_list_length) {
+		struct strbuf change_name;
+
+		strbuf_init(&change_name, 80);
+
+		content = lookup_commit_reference_gently(
+			repo, &(to_resolve->content), 1);
+
+		compute_change_name(content, &change_name);
+		string_list_append(to_advance, change_name.buf);
+		strbuf_release(&change_name);
+	}
+}
+
+static void lookup_commits(
+	struct repository *repo,
+	struct oid_array *to_lookup,
+	struct commit_list **result)
+{
+	int i = to_lookup->nr;
+
+	while (--i >= 0) {
+		struct object_id *next = &(to_lookup->oid[i]);
+		struct commit *commit =
+			lookup_commit_reference_gently(repo, next, 1);
+		commit_list_insert(commit, result);
+	}
+}
+
+#define PARENT_TYPE_PREFIX "parent-type "
+
+int write_metacommit(struct repository *repo, struct metacommit_data *state,
+	struct object_id *result)
+{
+	struct commit_list *parents = NULL;
+	struct strbuf comment;
+	size_t i;
+	struct commit *content;
+
+	strbuf_init(&comment, strlen(PARENT_TYPE_PREFIX)
+		+ 1 + 2 * (state->origin.nr + state->replace.nr));
+	lookup_commits(repo, &state->origin, &parents);
+	lookup_commits(repo, &state->replace, &parents);
+	content = lookup_commit_reference_gently(repo, &state->content, 1);
+	if (!content) {
+		strbuf_release(&comment);
+		free_commit_list(parents);
+		return -1;
+	}
+	commit_list_insert(content, &parents);
+
+	strbuf_addstr(&comment, PARENT_TYPE_PREFIX);
+	strbuf_addstr(&comment, state->abandoned ? "a" : "c");
+	for (i = 0; i < state->replace.nr; i++)
+		strbuf_addstr(&comment, " r");
+
+	for (i = 0; i < state->origin.nr; i++)
+		strbuf_addstr(&comment, " o");
+
+	/* The parents list will be freed by this call. */
+	commit_tree(
+		comment.buf,
+		comment.len,
+		repo->hash_algo->empty_tree,
+		parents,
+		result,
+		NULL,
+		NULL);
+
+	strbuf_release(&comment);
+	return 0;
+}
+
+/*
+ * Returns true iff the given metacommit is abandoned, has one or more origin
+ * parents, or has one or more replacement parents.
+ */
+static int is_nontrivial_metacommit(struct metacommit_data *state)
+{
+	return state->replace.nr || state->origin.nr || state->abandoned;
+}
+
+/*
+ * Records the relationships described by the given metacommit in the
+ * repository.
+ *
+ * If override_change is NULL (the default), an attempt will be made
+ * to append to existing changes wherever possible instead of creating new ones.
+ * If override_change is non-null, only the given change ref will be updated.
+ *
+ * The changes list is filled in with the list of change refs that were updated,
+ * with the util pointers pointing to the old object IDS for those changes.
+ * The object ID pointers all point to objects owned by the change_table and
+ * will go out of scope when the change_table is destroyed.
+ *
+ * options is a bitwise combination of the UPDATE_OPTION_* flags.
+ */
+static int record_metacommit_withresult(
+	struct repository *repo,
+	struct change_table *chtable,
+	const struct metacommit_data *metacommit,
+	const char *override_change,
+	int options,
+	struct strbuf *err,
+	struct string_list *changes)
+{
+	static const char *msg = "updating change";
+	struct metacommit_data resolved_metacommit = METACOMMIT_DATA_INIT;
+	struct object_id commit_target;
+	struct ref_transaction *transaction = NULL;
+	struct change_head *overridden_head;
+	const struct object_id *old_head;
+
+	size_t i;
+	int ret = 0;
+	int force = (options & UPDATE_OPTION_FORCE);
+
+	resolve_metacommit(repo, chtable, metacommit, &resolved_metacommit, changes,
+		(options & UPDATE_OPTION_NOAPPEND) == 0);
+
+	if (override_change) {
+		string_list_clear(changes, 0);
+		overridden_head = get_change_head(chtable, override_change);
+		if (overridden_head) {
+			/* This is an existing change */
+			old_head = &overridden_head->head;
+			if (!force) {
+				if (!oid_array_readonly_contains(&(resolved_metacommit.replace),
+					&overridden_head->head)) {
+					/* Attempted non-fast-forward change */
+					strbuf_addf(err, _("non-fast-forward update to '%s'"),
+						override_change);
+					ret = -1;
+					goto cleanup;
+				}
+			}
+		} else
+			/* ...then this is a newly-created change */
+			old_head = null_oid();
+
+		/*
+		 * The expected "current" head of the change is stored in the
+		 * util pointer. Cast required because old_head is const*
+		 */
+		string_list_append(changes, override_change)->util = (void *)old_head;
+	}
+
+	if (is_nontrivial_metacommit(&resolved_metacommit)) {
+		/* If there are any origin or replacement parents, create a new metacommit
+		 * object. */
+		if (write_metacommit(repo, &resolved_metacommit, &commit_target) < 0) {
+			ret = -1;
+			goto cleanup;
+		}
+	} else {
+		/*
+		 * If the metacommit would only contain a content commit, point to the
+		 * commit itself rather than creating a trivial metacommit.
+		 */
+		oidcpy(&commit_target, &(resolved_metacommit.content));
+	}
+
+	/*
+	 * If a change already exists with this target and we're not forcing an
+	 * update to some specific override_change && change, there's nothing to do.
+	 */
+	if (!override_change
+		&& change_table_has_change_referencing(chtable, &commit_target))
+		/* Not an error */
+		goto cleanup;
+
+	transaction = ref_transaction_begin(err);
+
+	/* Update the refs for each affected change */
+	if (!transaction)
+		ret = -1;
+	else {
+		for (i = 0; i < changes->nr; i++) {
+			struct string_list_item *it = &changes->items[i];
+
+			/*
+			 * The expected current head of the change is stored in the util pointer.
+			 * It is null if the change should be newly-created.
+			 */
+			if (it->util) {
+				if (ref_transaction_update(transaction, it->string, &commit_target,
+					force ? NULL : it->util, 0, msg, err))
+
+					ret = -1;
+			} else {
+				if (ref_transaction_create(transaction, it->string,
+					&commit_target, 0, msg, err))
+
+					ret = -1;
+			}
+		}
+
+		if (!ret)
+			if (ref_transaction_commit(transaction, err))
+				ret = -1;
+	}
+
+cleanup:
+	ref_transaction_free(transaction);
+	clear_metacommit_data(&resolved_metacommit);
+
+	return ret;
+}
+
+int record_metacommit(
+	struct repository *repo,
+	const struct metacommit_data *metacommit,
+	const char *override_change,
+	int options,
+	struct strbuf *err,
+	struct string_list *changes)
+{
+		struct change_table chtable;
+		int result;
+
+		change_table_init(&chtable);
+		change_table_add_all_visible(&chtable, repo);
+
+		result = record_metacommit_withresult(
+			repo,
+			&chtable,
+			metacommit,
+			override_change,
+			options,
+			err,
+			changes);
+
+		change_table_clear(&chtable);
+		return result;
+}
+
+void modify_change(
+	struct repository *repo,
+	const struct object_id *old_commit,
+	const struct object_id *new_commit,
+	struct strbuf *err)
+{
+	struct string_list changes = STRING_LIST_INIT_DUP;
+	struct metacommit_data metacommit = METACOMMIT_DATA_INIT;
+
+	oidcpy(&(metacommit.content), new_commit);
+	oid_array_append(&(metacommit.replace), old_commit);
+
+	record_metacommit(repo, &metacommit, NULL, 0, err, &changes);
+
+	clear_metacommit_data(&metacommit);
+	string_list_clear(&changes, 0);
+}
diff --git a/metacommit.h b/metacommit.h
new file mode 100644
index 00000000000..45625cd0d02
--- /dev/null
+++ b/metacommit.h
@@ -0,0 +1,75 @@
+#ifndef METACOMMIT_H
+#define METACOMMIT_H
+
+#include "hash.h"
+#include "oid-array.h"
+#include "repository.h"
+#include "string-list.h"
+
+/* If specified, non-fast-forward changes are permitted. */
+#define UPDATE_OPTION_FORCE     0x0001
+/**
+ * If specified, no attempt will be made to append to existing changes.
+ * Normally, if a metacommit points to a commit in its replace or origin
+ * list and an existing change points to that same commit as its content, the
+ * new metacommit will attempt to append to that same change. This may replace
+ * the commit parent with one or more metacommits from the head of the appended
+ * changes. This option disables this behavior, and will always create a new
+ * change rather than reusing existing changes.
+ */
+#define UPDATE_OPTION_NOAPPEND  0x0002
+
+/* Metacommit Data */
+
+struct metacommit_data {
+	struct object_id content;
+	struct oid_array replace;
+	struct oid_array origin;
+	int abandoned;
+};
+
+#define METACOMMIT_DATA_INIT { 0 }
+
+extern void clear_metacommit_data(struct metacommit_data *state);
+
+/**
+ * Records the relationships described by the given metacommit in the
+ * repository.
+ *
+ * If override_change is NULL (the default), an attempt will be made
+ * to append to existing changes wherever possible instead of creating new ones.
+ * If override_change is non-null, only the given change ref will be updated.
+ *
+ * options is a bitwise combination of the UPDATE_OPTION_* flags.
+ */
+int record_metacommit(
+	struct repository *repo,
+	const struct metacommit_data *metacommit,
+	const char* override_change,
+	int options,
+	struct strbuf *err,
+	struct string_list *changes);
+
+/**
+ * Should be invoked after a command that has "modify" semantics - commands that
+ * create a new commit based on an old commit and treat the new one as a
+ * replacement for the old one. This method records the replacement in the
+ * change graph, such that a future evolve operation will rebase children of
+ * the old commit onto the new commit.
+ */
+void modify_change(
+	struct repository *repo,
+	const struct object_id *old_commit,
+	const struct object_id *new_commit,
+	struct strbuf *err);
+
+/**
+ * Creates a new metacommit object with the given content. Writes the object
+ * id of the newly-created commit to result.
+ */
+int write_metacommit(
+	struct repository *repo,
+	struct metacommit_data *state,
+	struct object_id *result);
+
+#endif
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v2 07/10] evolve: implement the git change command
  2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
                     ` (5 preceding siblings ...)
  2022-10-05 14:59   ` [PATCH v2 06/10] evolve: add support for writing metacommits Stefan Xenos via GitGitGadget
@ 2022-10-05 14:59   ` Stefan Xenos via GitGitGadget
  2022-10-05 14:59   ` [PATCH v2 08/10] evolve: add delete command Chris Poucet via GitGitGadget
                     ` (3 subsequent siblings)
  10 siblings, 0 replies; 66+ messages in thread
From: Stefan Xenos via GitGitGadget @ 2022-10-05 14:59 UTC (permalink / raw)
  To: git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Stefan Xenos

From: Stefan Xenos <sxenos@google.com>

Implement the git change update command, which
are sufficient for constructing change graphs.

For example, to create a new change (a stable name) that refers to HEAD:

git change update -c HEAD

To record a rebase or amend in the change graph:

git change update -c <new_commit> -r <old_commit>

To record a cherry-pick in the change graph:

git change update -c <new_commit> -o <original_commit>

Signed-off-by: Stefan Xenos <sxenos@google.com>
Signed-off-by: Chris Poucet <poucet@google.com>
---
 .gitignore       |   1 +
 Makefile         |   1 +
 builtin.h        |   1 +
 builtin/change.c | 253 +++++++++++++++++++++++++++++++++++++++++++++++
 git.c            |   1 +
 ref-filter.c     |   2 +-
 ref-filter.h     |   4 +
 7 files changed, 262 insertions(+), 1 deletion(-)
 create mode 100644 builtin/change.c

diff --git a/.gitignore b/.gitignore
index b3dcafcb331..a57fd8d8897 100644
--- a/.gitignore
+++ b/.gitignore
@@ -28,6 +28,7 @@
 /git-bugreport
 /git-bundle
 /git-cat-file
+/git-change
 /git-check-attr
 /git-check-ignore
 /git-check-mailmap
diff --git a/Makefile b/Makefile
index 68082ef94c7..82f68f13d9f 100644
--- a/Makefile
+++ b/Makefile
@@ -1142,6 +1142,7 @@ BUILTIN_OBJS += builtin/branch.o
 BUILTIN_OBJS += builtin/bugreport.o
 BUILTIN_OBJS += builtin/bundle.o
 BUILTIN_OBJS += builtin/cat-file.o
+BUILTIN_OBJS += builtin/change.o
 BUILTIN_OBJS += builtin/check-attr.o
 BUILTIN_OBJS += builtin/check-ignore.o
 BUILTIN_OBJS += builtin/check-mailmap.o
diff --git a/builtin.h b/builtin.h
index 8901a34d6bf..c10f20c972c 100644
--- a/builtin.h
+++ b/builtin.h
@@ -122,6 +122,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix);
 int cmd_bugreport(int argc, const char **argv, const char *prefix);
 int cmd_bundle(int argc, const char **argv, const char *prefix);
 int cmd_cat_file(int argc, const char **argv, const char *prefix);
+int cmd_change(int argc, const char **argv, const char *prefix);
 int cmd_checkout(int argc, const char **argv, const char *prefix);
 int cmd_checkout__worker(int argc, const char **argv, const char *prefix);
 int cmd_checkout_index(int argc, const char **argv, const char *prefix);
diff --git a/builtin/change.c b/builtin/change.c
new file mode 100644
index 00000000000..e4e8e15b768
--- /dev/null
+++ b/builtin/change.c
@@ -0,0 +1,253 @@
+#include "builtin.h"
+#include "ref-filter.h"
+#include "parse-options.h"
+#include "metacommit.h"
+#include "config.h"
+
+static const char * const builtin_change_usage[] = {
+	N_("git change list [<pattern>...]"),
+	N_("git change update [--force] [--replace <treeish>...] "
+	   "[--origin <treeish>...] [--content <newtreeish>]"),
+	NULL
+};
+
+static const char * const builtin_list_usage[] = {
+	N_("git change list [<pattern>...]"),
+	NULL
+};
+
+static const char * const builtin_update_usage[] = {
+	N_("git change update [--force] [--replace <treeish>...] "
+	"[--origin <treeish>...] [--content <newtreeish>]"),
+	NULL
+};
+
+static int change_list(int argc, const char **argv, const char* prefix)
+{
+	struct option options[] = {
+		OPT_END()
+	};
+	struct ref_filter filter = { 0 };
+	struct ref_sorting *sorting;
+	struct string_list sorting_options = STRING_LIST_INIT_DUP;
+	struct ref_format format = REF_FORMAT_INIT;
+	struct ref_array array = { 0 };
+	size_t i;
+
+	argc = parse_options(argc, argv, prefix, options, builtin_list_usage, 0);
+
+	setup_ref_filter_porcelain_msg();
+
+	filter.kind = FILTER_REFS_CHANGES;
+	filter.name_patterns = argv;
+
+	filter_refs(&array, &filter, FILTER_REFS_CHANGES);
+
+	/* TODO: This causes a crash. It sets one of the atom_value handlers to
+	 * something invalid, which causes a crash later when we call
+	 * show_ref_array_item. Figure out why this happens and put back the sorting.
+	 *
+	 * sorting = ref_sorting_options(&sorting_options);
+	 * ref_array_sort(sorting, &array); */
+
+	if (!format.format)
+		format.format = "%(refname:lstrip=1)";
+
+	if (verify_ref_format(&format))
+		die(_("unable to parse format string"));
+
+	sorting = ref_sorting_options(&sorting_options);
+	ref_array_sort(sorting, &array);
+
+
+	for (i = 0; i < array.nr; i++) {
+		struct strbuf output = STRBUF_INIT;
+		struct strbuf err = STRBUF_INIT;
+		if (format_ref_array_item(array.items[i], &format, &output, &err))
+			die("%s", err.buf);
+		fwrite(output.buf, 1, output.len, stdout);
+		putchar('\n');
+
+		strbuf_release(&err);
+		strbuf_release(&output);
+	}
+
+	ref_array_clear(&array);
+	ref_sorting_release(sorting);
+
+	return 0;
+}
+
+struct update_state {
+	int options;
+	const char* change;
+	const char* content;
+	struct string_list replace;
+	struct string_list origin;
+};
+
+#define UPDATE_STATE_INIT { \
+	.content = "HEAD", \
+	.replace = STRING_LIST_INIT_NODUP, \
+	.origin = STRING_LIST_INIT_NODUP \
+}
+
+static void clear_update_state(struct update_state *state)
+{
+	string_list_clear(&state->replace, 0);
+	string_list_clear(&state->origin, 0);
+}
+
+static int update_option_parse_replace(const struct option *opt,
+				       const char *arg, int unset)
+{
+	struct update_state *state = opt->value;
+	string_list_append(&state->replace, arg);
+	return 0;
+}
+
+static int update_option_parse_origin(const struct option *opt,
+				      const char *arg, int unset)
+{
+	struct update_state *state = opt->value;
+	string_list_append(&state->origin, arg);
+	return 0;
+}
+
+static int resolve_commit(const char *committish, struct object_id *result)
+{
+	struct commit *commit;
+	if (get_oid_committish(committish, result))
+		die(_("failed to resolve '%s' as a valid revision."), committish);
+	commit = lookup_commit_reference(the_repository, result);
+	if (!commit)
+		die(_("could not parse object '%s'."), committish);
+	oidcpy(result, &commit->object.oid);
+	return 0;
+}
+
+static void resolve_commit_list(const struct string_list *commitsish_list,
+	struct oid_array* result)
+{
+	struct string_list_item *item;
+
+	for_each_string_list_item(item, commitsish_list) {
+		struct object_id next;
+		resolve_commit(item->string, &next);
+		oid_array_append(result, &next);
+	}
+}
+
+/*
+ * Given the command-line options for the update command, fills in a
+ * metacommit_data with the corresponding changes.
+ */
+static void get_metacommit_from_command_line(
+	const struct update_state* commands, struct metacommit_data *result)
+{
+	resolve_commit(commands->content, &(result->content));
+	resolve_commit_list(&(commands->replace), &(result->replace));
+	resolve_commit_list(&(commands->origin), &(result->origin));
+}
+
+static int perform_update(
+	struct repository *repo,
+	const struct update_state *state,
+	struct strbuf *err)
+{
+	struct metacommit_data metacommit = METACOMMIT_DATA_INIT;
+	struct string_list changes = STRING_LIST_INIT_DUP;
+	int ret;
+	struct string_list_item *item;
+
+	get_metacommit_from_command_line(state, &metacommit);
+
+	ret = record_metacommit(
+		repo,
+		&metacommit,
+		state->change,
+		state->options,
+		err,
+		&changes);
+
+	for_each_string_list_item(item, &changes) {
+
+		const char* name = lstrip_ref_components(item->string, 1);
+		if (!name)
+			die(_("failed to remove `refs/` from %s"), item->string);
+
+		if (item->util)
+			fprintf(stdout, _("Updated change %s"), name);
+		else
+			fprintf(stdout, _("Created change %s"), name);
+		putchar('\n');
+	}
+
+	string_list_clear(&changes, 0);
+	clear_metacommit_data(&metacommit);
+
+	return ret;
+}
+
+static int change_update(int argc, const char **argv, const char* prefix)
+{
+	int result;
+	struct strbuf err = STRBUF_INIT;
+	struct update_state state = UPDATE_STATE_INIT;
+	struct option options[] = {
+		{ OPTION_CALLBACK, 'r', "replace", &state, N_("commit"),
+			N_("marks the given commit as being obsolete"),
+			0, update_option_parse_replace },
+		{ OPTION_CALLBACK, 'o', "origin", &state, N_("commit"),
+			N_("marks the given commit as being the origin of this commit"),
+			0, update_option_parse_origin },
+
+		OPT_STRING('c', "content", &state.content, N_("commit"),
+				 N_("identifies the new content commit for the change")),
+		OPT_STRING('g', "change", &state.change, N_("commit"),
+				 N_("name of the change to update")),
+		OPT_SET_INT_F('n', "new", &state.options,
+			      N_("create a new change - do not append to any existing change"),
+			      UPDATE_OPTION_NOAPPEND, 0),
+		OPT_SET_INT_F('F', "force", &state.options,
+			      N_("overwrite an existing change of the same name"),
+			      UPDATE_OPTION_FORCE, 0),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options, builtin_update_usage, 0);
+	result = perform_update(the_repository, &state, &err);
+
+	if (result < 0) {
+		error("%s", err.buf);
+		strbuf_release(&err);
+	}
+
+	clear_update_state(&state);
+
+	return result;
+}
+
+int cmd_change(int argc, const char **argv, const char *prefix)
+{
+	parse_opt_subcommand_fn *fn = NULL;
+	/* No options permitted before subcommand currently */
+	struct option options[] = {
+		OPT_SUBCOMMAND("list", &fn, change_list),
+		OPT_SUBCOMMAND("update", &fn, change_update),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options, builtin_change_usage,
+		PARSE_OPT_SUBCOMMAND_OPTIONAL);
+
+	if (!fn) {
+		if (argc) {
+			error(_("unknown subcommand: `%s'"), argv[0]);
+			usage_with_options(builtin_change_usage, options);
+		}
+		fn = change_list;
+	}
+
+	return !!fn(argc, argv, prefix);
+}
diff --git a/git.c b/git.c
index da411c53822..837b1abc53b 100644
--- a/git.c
+++ b/git.c
@@ -498,6 +498,7 @@ static struct cmd_struct commands[] = {
 	{ "bugreport", cmd_bugreport, RUN_SETUP_GENTLY },
 	{ "bundle", cmd_bundle, RUN_SETUP_GENTLY },
 	{ "cat-file", cmd_cat_file, RUN_SETUP },
+	{ "change", cmd_change, RUN_SETUP},
 	{ "check-attr", cmd_check_attr, RUN_SETUP },
 	{ "check-ignore", cmd_check_ignore, RUN_SETUP | NEED_WORK_TREE },
 	{ "check-mailmap", cmd_check_mailmap, RUN_SETUP },
diff --git a/ref-filter.c b/ref-filter.c
index 6a1789c623f..2d7a919d547 100644
--- a/ref-filter.c
+++ b/ref-filter.c
@@ -1557,7 +1557,7 @@ static inline char *copy_advance(char *dst, const char *src)
 	return dst;
 }
 
-static const char *lstrip_ref_components(const char *refname, int len)
+const char *lstrip_ref_components(const char *refname, int len)
 {
 	long remaining = len;
 	const char *start = xstrdup(refname);
diff --git a/ref-filter.h b/ref-filter.h
index db3ee44e4dc..193700694ad 100644
--- a/ref-filter.h
+++ b/ref-filter.h
@@ -145,4 +145,8 @@ struct ref_array_item *ref_array_push(struct ref_array *array,
 				      const char *refname,
 				      const struct object_id *oid);
 
+/* Strips `len` prefix components from the refname. */
+const char *lstrip_ref_components(const char *refname, int len);
+
+
 #endif /*  REF_FILTER_H  */
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v2 08/10] evolve: add delete command
  2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
                     ` (6 preceding siblings ...)
  2022-10-05 14:59   ` [PATCH v2 07/10] evolve: implement the git change command Stefan Xenos via GitGitGadget
@ 2022-10-05 14:59   ` Chris Poucet via GitGitGadget
  2022-10-05 14:59   ` [PATCH v2 09/10] evolve: add documentation for `git change` Chris Poucet via GitGitGadget
                     ` (2 subsequent siblings)
  10 siblings, 0 replies; 66+ messages in thread
From: Chris Poucet via GitGitGadget @ 2022-10-05 14:59 UTC (permalink / raw)
  To: git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Chris Poucet

From: Chris Poucet <poucet@google.com>

The delete command allows a user to delete one or more changes.
This effectively deletes the corresponding /refs/metas/foo ref.

Signed-off-by: Chris Poucet <poucet@google.com>
---
 builtin/change.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)

diff --git a/builtin/change.c b/builtin/change.c
index e4e8e15b768..12ea5f68197 100644
--- a/builtin/change.c
+++ b/builtin/change.c
@@ -3,11 +3,13 @@
 #include "parse-options.h"
 #include "metacommit.h"
 #include "config.h"
+#include "refs.h"
 
 static const char * const builtin_change_usage[] = {
 	N_("git change list [<pattern>...]"),
 	N_("git change update [--force] [--replace <treeish>...] "
 	   "[--origin <treeish>...] [--content <newtreeish>]"),
+	N_("git change delete <change-name>..."),
 	NULL
 };
 
@@ -22,6 +24,11 @@ static const char * const builtin_update_usage[] = {
 	NULL
 };
 
+static const char * const builtin_delete_usage[] = {
+	N_("git change delete <change-name>..."),
+	NULL
+};
+
 static int change_list(int argc, const char **argv, const char* prefix)
 {
 	struct option options[] = {
@@ -228,6 +235,75 @@ static int change_update(int argc, const char **argv, const char* prefix)
 	return result;
 }
 
+typedef int (*each_change_name_fn)(const char *name, const char *ref,
+				   const struct object_id *oid, void *cb_data);
+
+static int for_each_change_name(const char **argv, each_change_name_fn fn,
+				void *cb_data)
+{
+	const char **p;
+	struct strbuf ref = STRBUF_INIT;
+	int had_error = 0;
+	struct object_id oid;
+
+	for (p = argv; *p; p++) {
+		strbuf_reset(&ref);
+		/* Convenience functionality to avoid having to type `metas/` */
+		if (strncmp("metas/", *p, 5)) {
+			strbuf_addf(&ref, "refs/metas/%s", *p);
+		} else {
+			strbuf_addf(&ref, "refs/%s", *p);
+		}
+		if (read_ref(ref.buf, &oid)) {
+			error(_("change '%s' not found."), *p);
+			had_error = 1;
+			continue;
+		}
+		if (fn(*p, ref.buf, &oid, cb_data))
+			had_error = 1;
+	}
+	strbuf_release(&ref);
+	return had_error;
+}
+
+static int collect_changes(const char *name, const char *ref,
+			   const struct object_id *oid, void *cb_data)
+{
+	struct string_list *ref_list = cb_data;
+
+	string_list_append(ref_list, ref);
+	ref_list->items[ref_list->nr - 1].util = oiddup(oid);
+	return 0;
+}
+
+static int change_delete(int argc, const char **argv, const char* prefix) {
+	int result = 0;
+	struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
+	struct string_list_item *item;
+	struct option options[] = {
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options, builtin_delete_usage, 0);
+
+	result = for_each_change_name(argv, collect_changes, (void *)&refs_to_delete);
+	if (delete_refs(NULL, &refs_to_delete, REF_NO_DEREF))
+		result = 1;
+
+	for_each_string_list_item(item, &refs_to_delete) {
+		const char *name = item->string;
+		struct object_id *oid = item->util;
+		if (!ref_exists(name))
+			printf(_("Deleted change '%s' (was %s)\n"),
+				item->string + 5,
+				find_unique_abbrev(oid, DEFAULT_ABBREV));
+
+		free(oid);
+	}
+	string_list_clear(&refs_to_delete, 0);
+	return result;
+}
+
 int cmd_change(int argc, const char **argv, const char *prefix)
 {
 	parse_opt_subcommand_fn *fn = NULL;
@@ -235,6 +311,7 @@ int cmd_change(int argc, const char **argv, const char *prefix)
 	struct option options[] = {
 		OPT_SUBCOMMAND("list", &fn, change_list),
 		OPT_SUBCOMMAND("update", &fn, change_update),
+		OPT_SUBCOMMAND("delete", &fn, change_delete),
 		OPT_END()
 	};
 
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v2 09/10] evolve: add documentation for `git change`
  2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
                     ` (7 preceding siblings ...)
  2022-10-05 14:59   ` [PATCH v2 08/10] evolve: add delete command Chris Poucet via GitGitGadget
@ 2022-10-05 14:59   ` Chris Poucet via GitGitGadget
  2022-10-05 14:59   ` [PATCH v2 10/10] evolve: add tests for the git-change command Chris Poucet via GitGitGadget
  2022-10-10  9:23   ` [PATCH v2 00/10] RFC: Git Evolve / Change Phillip Wood
  10 siblings, 0 replies; 66+ messages in thread
From: Chris Poucet via GitGitGadget @ 2022-10-05 14:59 UTC (permalink / raw)
  To: git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Chris Poucet

From: Chris Poucet <poucet@google.com>

Signed-off-by: Chris Poucet <poucet@google.com>
---
 Documentation/git-change.txt | 55 ++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 Documentation/git-change.txt

diff --git a/Documentation/git-change.txt b/Documentation/git-change.txt
new file mode 100644
index 00000000000..ea9a8e619b9
--- /dev/null
+++ b/Documentation/git-change.txt
@@ -0,0 +1,55 @@
+git-change(1)
+=============
+
+NAME
+----
+git-change - Create, list, update or delete changes
+
+SYNOPSIS
+--------
+[verse]
+'git change' list [<pattern>...]
+'git change' update [-g <change-name> | -n] [--force] [--replace <treeish>...] [--origin <treeish>...] [--content <newtreeish>]
+'git change' delete <change-name>...
+
+DESCRIPTION
+-----------
+
+`git change list`: lists all existing <change-name>s.
+
+`git change delete`: deletes the given <change-name>s.
+
+`git change update`: creates or updates a <change-name>.
+
+If no arguments are given to `update` then a change is added to the
+`refs/metas/` directory, unless a change already exists for the given commit.
+
+A <change-name> starts with `metas/` and represents the current change that is
+being worked on.
+
+OPTIONS
+-------
+-c::
+--content::
+	Identifies the content commit for the change
+
+-o::
+--origin::
+	Marks the given commit as being the origin of this commit.
+
+-r::
+--replace::
+	Marks the given commit as being obsoleted by the new commit.
+
+-g::
+	<change-name> to update
+
+-n::
+	Indicates that the change is new and an existing change should not be updated.
+
+--force::
+	Overwite an existing change of the same name.
+
+GIT
+---
+Part of the linkgit:git[1] suite
-- 
gitgitgadget


^ permalink raw reply related	[flat|nested] 66+ messages in thread

* [PATCH v2 10/10] evolve: add tests for the git-change command
  2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
                     ` (8 preceding siblings ...)
  2022-10-05 14:59   ` [PATCH v2 09/10] evolve: add documentation for `git change` Chris Poucet via GitGitGadget
@ 2022-10-05 14:59   ` Chris Poucet via GitGitGadget
  2022-10-10  9:23   ` [PATCH v2 00/10] RFC: Git Evolve / Change Phillip Wood
  10 siblings, 0 replies; 66+ messages in thread
From: Chris Poucet via GitGitGadget @ 2022-10-05 14:59 UTC (permalink / raw)
  To: git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Chris Poucet

From: Chris Poucet <poucet@google.com>

Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
Signed-off-by: Chris Poucet <poucet@google.com>
---
 t/t9990-changes.sh | 148 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 148 insertions(+)
 create mode 100755 t/t9990-changes.sh

diff --git a/t/t9990-changes.sh b/t/t9990-changes.sh
new file mode 100755
index 00000000000..11fbd8ba49c
--- /dev/null
+++ b/t/t9990-changes.sh
@@ -0,0 +1,148 @@
+#!/bin/sh
+
+test_description='git change - low level meta-commit management'
+
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+test_expect_success 'setup commits and meta-commits' '
+       for c in one two three
+       do
+               test_commit $c &&
+               git change update --content $c >actual 2>err &&
+               echo "Created change metas/$c" >expect &&
+               test_cmp expect actual &&
+               test_must_be_empty err &&
+               test_cmp_rev refs/metas/$c $c || return 1
+       done
+'
+
+# Check a meta-commit has the correct parents Call with the object
+# name of the meta-commit followed by pairs of type and parent
+check_meta_commit () {
+       name=$1
+       shift
+       while test $# -gt 0
+       do
+               printf '%s %s\n' $1 $(git rev-parse --verify $2)
+               shift
+               shift
+       done | sort >expect
+       git cat-file commit $name >metacommit &&
+       # commit body should consist of parent-type
+           types="$(sed -n '/^$/ {
+                       :loop
+                       n
+                       s/^parent-type //
+                       p
+                       b loop
+                   }' metacommit)" &&
+       while read key value
+       do
+               # TODO: don't sort the first parent
+               if test "$key" = "parent"
+               then
+                       type="${types%% *}"
+                       test -n "$type" || return 1
+                       printf '%s %s\n' $type $value
+                       types="${types#?}"
+                       types="${types# }"
+               elif test "$key" = "tree"
+               then
+                       test_cmp_rev "$value" $EMPTY_TREE || return 1
+               elif test -z "$key"
+               then
+                       # only parse commit headers
+                       break
+               fi
+       done <metacommit >actual-unsorted &&
+       test -z "$types" &&
+       sort >actual <actual-unsorted &&
+       test_cmp expect actual
+}
+
+test_expect_success 'update meta-commits after rebase' '
+       (
+               set_fake_editor &&
+               FAKE_AMEND=edited &&
+               FAKE_LINES="reword 1 pick 2 fixup 3" &&
+               export FAKE_AMEND FAKE_LINES &&
+               git rebase -i --root
+       ) &&
+
+       # update meta-commits
+       git change update --replace tags/one --content HEAD~1 >out 2>err &&
+       echo "Updated change metas/one" >expect &&
+       test_cmp expect out &&
+       test_must_be_empty err &&
+       git change update --replace tags/two --content HEAD@{2} &&
+       oid=$(git rev-parse --verify metas/two) &&
+       git change update --replace HEAD@{2} --replace tags/three \
+               --content HEAD &&
+
+       # check meta-commits
+       check_meta_commit metas/one c HEAD~1 r tags/one &&
+       check_meta_commit $oid c HEAD@{2} r tags/two &&
+       # NB this checks that "git change update" uses the meta-commit ($oid)
+       #    corresponding to the replaces commit (HEAD@2 above) given on the
+       #    commandline.
+       check_meta_commit metas/two c HEAD r $oid r tags/three &&
+       check_meta_commit metas/three c HEAD r $oid r tags/three
+'
+
+reset_meta_commits () {
+    for c in one two three
+    do
+       echo "update refs/metas/$c refs/tags/$c^0"
+    done | git update-ref --stdin
+}
+
+test_expect_success 'override change name' '
+       # TODO: builtin/change.c expects --change to be the full refname,
+       #       ideally it would prepend refs/metas to the string given by the
+       #       user.
+       git change update --change refs/metas/another-one --content one &&
+       test_cmp_rev metas/another-one one
+'
+
+test_expect_success 'non-fast forward meta-commit update refused' '
+       test_must_fail git change update --change refs/metas/one --content two \
+               >out 2>err &&
+       echo "error: non-fast-forward update to ${SQ}refs/metas/one${SQ}" \
+               >expect &&
+       test_cmp expect err &&
+       test_must_be_empty out
+'
+
+test_expect_success 'forced non-fast forward update succeeds' '
+       git change update --change refs/metas/one --content two --force \
+               >out 2>err &&
+       echo "Updated change metas/one" >expect &&
+       test_cmp expect out &&
+       test_must_be_empty err
+'
+
+test_expect_success 'list changes' '
+       cat >expect <<-\EOF &&
+metas/another-one
+metas/one
+metas/three
+metas/two
+EOF
+       git change list >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'delete change' '
+       git change delete metas/one &&
+       cat >expect <<-\EOF &&
+metas/another-one
+metas/three
+metas/two
+EOF
+       git change list >actual &&
+       test_cmp expect actual
+'
+
+test_done
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 66+ messages in thread

* Re: [PATCH v2 01/10] technical doc: add a design doc for the evolve command
  2022-10-05 14:59   ` [PATCH v2 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
@ 2022-10-05 15:16     ` Chris Poucet
  2022-10-06 20:53       ` Glen Choo
  2022-10-10 19:35     ` Victoria Dye
  1 sibling, 1 reply; 66+ messages in thread
From: Chris Poucet @ 2022-10-05 15:16 UTC (permalink / raw)
  To: Stefan Xenos via GitGitGadget
  Cc: git, Jerry Zhang, Phillip Wood,
	Ævar Arnfjörð Bjarmason, Christophe Poucet, vdye,
	Junio C Hamano, Jonathan Tan, Glen Choo

One thing that is not clear to me is whether this is the desired
direction. I took at look at the git review notes but it was hard to
get a sense of where people are at.

Would love input on the design.


On Wed, Oct 5, 2022 at 4:59 PM Stefan Xenos via GitGitGadget
<gitgitgadget@gmail.com> wrote:
>
> From: Stefan Xenos <sxenos@google.com>
>
> This document describes what a change graph for
> git would look like, the behavior of the evolve command,
> and the changes planned for other commands.
>
> It was originally proposed in 2018, see
> https://public-inbox.org/git/20181115005546.212538-1-sxenos@google.com/
>
> Signed-off-by: Stefan Xenos <sxenos@google.com>
> Signed-off-by: Chris Poucet <poucet@google.com>
> ---
>  Documentation/technical/evolve.txt | 1070 ++++++++++++++++++++++++++++
>  1 file changed, 1070 insertions(+)
>  create mode 100644 Documentation/technical/evolve.txt
>
> diff --git a/Documentation/technical/evolve.txt b/Documentation/technical/evolve.txt
> new file mode 100644
> index 00000000000..2051ea77b8a
> --- /dev/null
> +++ b/Documentation/technical/evolve.txt
> @@ -0,0 +1,1070 @@
> +Evolve
> +======
> +
> +Objective
> +=========
> +Create an "evolve" command to help users craft a high quality commit history.
> +Users can improve commits one at a time and in any order, then run git evolve to
> +rewrite their recent history to ensure everything is up-to-date. We track
> +amendments to a commit over time in a change graph. Users can share their
> +progress with others by exchanging their change graphs using the standard push,
> +fetch, and format-patch commands.
> +
> +Status
> +======
> +This proposal has not been implemented yet.
> +
> +Background
> +==========
> +Imagine you have three sequential changes up for review and you receive feedback
> +that requires editing all three changes. We'll define the word "change"
> +formally later, but for the moment let's say that a change is a work-in-progress
> +whose final version will be submitted as a commit in the future.
> +
> +While you're editing one change, more feedback arrives on one of the others.
> +What do you do?
> +
> +The evolve command is a convenient way to work with chains of commits that are
> +under review. Whenever you rebase or amend a commit, the repository remembers
> +that the old commit is obsolete and has been replaced by the new one. Then, at
> +some point in the future, you can run "git evolve" and the correct sequence of
> +rebases will occur in the correct order such that no commit has an obsolete
> +parent.
> +
> +Part of making the "evolve" command work involves tracking the edits to a commit
> +over time, which is why we need an change graph. However, the change
> +graph will also bring other benefits:
> +
> +- Users can view the history of a change directly (the sequence of amends and
> +  rebases it has undergone, orthogonal to the history of the branch it is on).
> +- It will be possible to quickly locate and list all the changes the user
> +  currently has in progress.
> +- It can be used as part of other high-level commands that combine or split
> +  changes.
> +- It can be used to decorate commits (in git log, gitk, etc) that are either
> +  obsolete or are the tip of a work in progress.
> +- By pushing and pulling the change graph, users can collaborate more
> +  easily on changes-in-progress. This is better than pushing and pulling the
> +  commits themselves since the change graph can be used to locate a more
> +  specific merge base, allowing for better merges between different versions of
> +  the same change.
> +- It could be used to correctly rebase local changes and other local branches
> +  after running git-filter-branch.
> +- It can replace the change-id footer used by gerrit.
> +
> +Goals
> +-----
> +Legend: Goals marked with P0 are required. Goals marked with Pn should be
> +attempted unless they interfere with goals marked with Pn-1.
> +
> +P0. All commands that modify commits (such as the normal commit --amend or
> +    rebase command) should mark the old commit as being obsolete and replaced by
> +    the new one. No additional commands should be required to keep the
> +    change graph up-to-date.
> +P0. Any commit that may be involved in a future evolve command should not be
> +    garbage collected. Specifically:
> +    - Commits that obsolete another should not be garbage collected until
> +      user-specified conditions have occurred and the change has expired from
> +      the reflog. User specified conditions for removing changes include:
> +      - The user explicitly deleted the change.
> +      - The change was merged into a specific branch.
> +    - Commits that have been obsoleted by another should not be garbage
> +      collected if any of their replacements are still being retained.
> +P0. A commit can be obsoleted by more than one replacement (called divergence).
> +P0. Users must be able to resolve divergence (convergence).
> +P1. Users should be able to share chains of obsolete changes in order to
> +    collaborate on WIP changes.
> +P2. Such sharing should be at the user’s option. That is, it should be possible
> +    to directly share a change without also sharing the file states or commit
> +    comments from the obsolete changes that led up to it, and the choice not to
> +    share those commits should not require changing any commit hashes.
> +P2. It should be possible to discard part or all of the change graph
> +    without discarding the commits themselves that are already present in
> +    branches and the reflog.
> +P2. Provide sufficient information to replace gerrit's Change-Id footers.
> +
> +Similar technologies
> +--------------------
> +There are some other technologies that address the same end-user problem.
> +
> +Rebase -i can be used to solve the same problem, but users can't easily switch
> +tasks midway through an interactive rebase or have more than one interactive
> +rebase going on at the same time. It can't handle the case where you have
> +multiple changes sharing the same parent when that parent needs to be rebased
> +and won't let you collaborate with others on resolving a complicated interactive
> +rebase. You can think of rebase -i as a top-down approach and the evolve command
> +as the bottom-up approach to the same problem.
> +
> +Revup amend (https://github.com/Skydio/revup/blob/main/docs/amend.md)
> +allows insertion of cached changes into any commit in
> +the current history, and then reapplies the rest of history on top of
> +those changes. It uses a "git apply --cached" engine under the hood so
> +doesn't touch the working directory (although it will soon use the new
> +git merge-tree). When paired with "revup upload" which creates and
> +pushes multiple branches in the background for you, its possible to
> +work on a "graph" of changes on a single branch linearly, then have
> +the true graph structure created at upload time.
> +
> +git-revise (https://github.com/mystor/git-revise) does some very
> +similar things except it uses "git merge-file" combined with manually
> +merging the resulting trees. git branchstack
> +(https://github.com/krobelus/git-branchstack) can also create branches
> +in the background with the same mechanism.
> +
> +These tools don't store any external state, but as such also don't
> +provide any specific collaboration mechanism for individual changes.
> +
> +Several patch queue managers have been built on top of git (such as topgit,
> +stgit, and quilt). They address the same user need. However they also rely on
> +state managed outside git that needs to be kept in sync. Such state can be
> +easily damaged when running a git native command that is unaware of the patch
> +queue. They also typically require an explicit initialization step to be done by
> +the user which creates workflow problems.
> +
> +Mercurial implements a very similar feature in its EvolveExtension. The behavior
> +of the evolve command itself is very similar, but the storage format for the
> +change graph differs. In the case of mercurial, each change set can have one or
> +more obsolescence markers that point to other changesets that they replace. This
> +is similar to the "Commit Headers" approach considered in the other options
> +appendix. The approach proposed here stores obsolescence information in a
> +separate metacommit graph, which makes exchanging of obsolescence information
> +optional.
> +
> +Mercurial's default behavior makes it easy to find and switch between
> +non-obsolete changesets that aren't currently on any branch. We introduce the
> +notion of a new ref namespace that enables a similar workflow via a different
> +mechanism. Mercurial has the notion of changeset phases which isn't present
> +in git and creates new ways for a changeset to diverge. Git doesn't need
> +to deal with these issues, but it has to deal with the problems of picking an
> +upstream branch as a target for rebases and protecting obsolescence information
> +from GC. We also introduce some additional transformations (see
> +obsolescence-over-cherry-pick, below) that aren't present in the mercurial
> +implementation.
> +
> +Semi-related work
> +-----------------
> +There are other technologies that address different problems but have some
> +similarities with this proposal.
> +
> +Replacements (refs/replace) are superficially similar to obsolescences in that
> +they describe that one commit should be replaced by another. However, they
> +differ in both how they are created and how they are intended to be used.
> +Obsolescences are created automatically by the commands a user runs, and they
> +describe the user’s intent to perform a future rebase. Obsolete commits still
> +appear in branches, logs, etc like normal commits (possibly with an extra
> +decoration that marks them as obsolete). Replacements are typically created
> +explicitly by the user, they are meant to be kept around for a long time, and
> +they describe a replacement to be applied at read-time rather than as the input
> +to a future operation. When a replaced commit is queried, it is typically hidden
> +and swapped out with its replacement as though the replacement has already
> +occurred.
> +
> +Git-imerge is a project to help make complicated merges easier, particularly
> +when merging or rebasing long chains of patches. It is not an alternative to
> +the change graph, but its algorithm of applying smaller incremental merges
> +could be used as part of the evolve algorithm in the future.
> +
> +Overview
> +========
> +We introduce the notion of “meta-commits” which describe how one commit was
> +created from other commits. A branch of meta-commits is known as a change.
> +Changes are created and updated automatically whenever a user runs a command
> +that creates a commit. They are used for locating obsolete commits, providing a
> +list of a user’s unsubmitted work in progress, and providing a stable name for
> +each unsubmitted change.
> +
> +Users can exchange edit histories by pushing and fetching changes.
> +
> +New commands will be introduced for manipulating changes and resolving
> +divergence between them. Existing commands that create commits will be updated
> +to modify the meta-commit graph and create changes where necessary.
> +
> +Example usage
> +-------------
> +# First create three dependent changes
> +$ echo foo>bar.txt && git add .
> +$ git commit -m "This is a test"
> +created change metas/this_is_a_test
> +$ echo foo2>bar2.txt && git add .
> +$ git commit -m "This is also a test"
> +created change metas/this_is_also_a_test
> +$ echo foo3>bar3.txt && git add .
> +$ git commit -m "More testing"
> +created change metas/more_testing
> +
> +# List all our changes in progress
> +$ git change list
> +metas/this_is_a_test
> +metas/this_is_also_a_test
> +* metas/more_testing
> +metas/some_change_already_merged_upstream
> +
> +# Now modify the earliest change, using its stable name
> +$ git reset --hard metas/this_is_a_test
> +$ echo morefoo>>bar.txt && git add . && git commit --amend --no-edit
> +
> +# Use git-evolve to fix up any dependent changes
> +$ git evolve
> +rebasing metas/this_is_also_a_test onto metas/this_is_a_test
> +rebasing metas/more_testing onto metas/this_is_also_a_test
> +Done
> +
> +# Use git-obslog to view the history of the this_is_a_test change
> +$ git log --obslog
> +93f110 metas/this_is_a_test@{0} commit (amend): This is a test
> +930219 metas/this_is_a_test@{1} commit: This is a test
> +
> +# Now create an unrelated change
> +$ git reset --hard origin/master
> +$ echo newchange>unrelated.txt && git add .
> +$ git commit -m "Unrelated change"
> +created change metas/unrelated_change
> +
> +# Fetch the latest code from origin/master and use git-evolve
> +# to rebase all dependent changes.
> +$ git fetch origin master
> +$ git evolve origin/master
> +deleting metas/some_change_already_merged_upstream
> +rebasing metas/this_is_a_test onto origin/master
> +rebasing metas/this_is_also_a_test onto metas/this_is_a_test
> +rebasing metas/more_testing onto metas/this_is_also_a_test
> +rebasing metas/unrelated_change onto origin/master
> +Conflict detected! Resolve it and then use git evolve --continue to resume.
> +
> +# Sort out the conflict
> +$ git mergetool
> +$ git evolve origin/master
> +Done
> +
> +# Share the full history of edits for the this_is_a_test change
> +# with a review server
> +$ git push origin metas/this_is_a_test:refs/for/master
> +# Share the lastest commit for “Unrelated change”, without history
> +$ git push origin HEAD:refs/for/master
> +
> +Detailed design
> +===============
> +Obsolescence information is stored as a graph of meta-commits. A meta-commit is
> +a specially-formatted merge commit that describes how one commit was created
> +from others.
> +
> +Meta-commits look like this:
> +
> +$ git cat-file -p <example_meta_commit>
> +tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
> +parent aa7ce55545bf2c14bef48db91af1a74e2347539a
> +parent d64309ee51d0af12723b6cb027fc9f195b15a5e9
> +parent 7e1bbcd3a0fa854a7a9eac9bf1eea6465de98136
> +author Stefan Xenos <sxenos@gmail.com> 1540841596 -0700
> +committer Stefan Xenos <sxenos@gmail.com> 1540841596 -0700
> +parent-type c r o
> +
> +This says “commit aa7ce555 makes commit d64309ee obsolete. It was created by
> +cherry-picking commit 7e1bbcd3”.
> +
> +The tree for meta-commits is always the empty tree, but future versions of git
> +may attach other trees here. For forward-compatibility fsck should ignore such
> +trees if found on future repository versions. This will allow future versions of
> +git to add metadata to the meta-commit tree without breaking forwards
> +compatibility.
> +
> +The commit comment for a meta-commit is an auto-generated user-readable string
> +describing the command that produced the meta commit. These strings are shown
> +to the user when they view the obslog.
> +
> +Parent-type
> +-----------
> +The “parent-type” field in the commit header identifies a commit as a
> +meta-commit and indicates the meaning for each of its parents. It is never
> +present for normal commits. It contains a space-deliminated list of enum values
> +whose order matches the order of the parents. Possible parent types are:
> +
> +- c: (content) the content parent identifies the commit that this meta-commit is
> +  describing.
> +- r: (replaced) indicates that this parent is made obsolete by the content
> +  parent.
> +- o: (origin) indicates that the content parent was generated by cherry-picking
> +  this parent.
> +- a: (abandoned) used in place of a content parent for abandoned changes. Points
> +  to the final content commit for the change at the time it was abandoned.
> +
> +There must be exactly one content or abandoned parent for each meta-commit and
> +it is always the first parent. The content commit will always be a normal commit
> +and not a meta-commit. However, future versions of git may create meta-commits
> +for other meta-commits and the fsck tool must be aware of this for forwards
> +compatibility.
> +
> +A meta-commit can have zero or more replaced parents. An amend operation creates
> +a single replaced parent. A merge used to resolve divergence (see divergence,
> +below) will create multiple replaced parents. A meta-commit may have no
> +replaced parents if it describes a cherry-pick or squash merge that copies one
> +or more commits but does not replace them.
> +
> +A meta-commit can have zero or more origin parents. A cherry-pick creates a
> +single origin parent. Certain types of squash merge will create multiple origin
> +parents. Origin parents don't directly cause their origin to become obsolete,
> +but are used when computing blame or locating a merge base. The section
> +on obsolescence over cherry-picks describes how the evolve command uses
> +origin parents.
> +
> +A replaced parent or origin parent may be either a normal commit (indicating
> +the oldest-known version of a change) or another meta-commit (for a change that
> +has already been modified one or more times).
> +
> +The parent-type field needs to go after the committer field since git's rules
> +for forwards-compatibility require that new fields to be at the end of the
> +header. Putting a new field in the middle of the header would break fsck.
> +
> +The presence of an abandoned parent indicates that the change should be pruned
> +by the evolve command, and removed from the repository's history. Any follow-up
> +changes should rebased onto the parent of the pruned commit. The abandoned
> +parent points to the version of the change that should be restored if the user
> +attempts to restore the change.
> +
> +Changes
> +-------
> +A branch of meta-commits describes how a commit was produced and what previous
> +commits it is based on. It is also an identifier for a thing the user is
> +currently working on. We refer to such a meta-branch as a change.
> +
> +Local changes are stored in the new refs/metas namespace. Remote changes are
> +stored in the refs/remote/<remotename>/metas namespace.
> +
> +The list of changes in refs/metas is more than just a mechanism for the evolve
> +command to locate obsolete commits. It is also a convenient list of all of a
> +user’s work in progress and their current state - a list of things they’re
> +likely to want to come back to.
> +
> +Strictly speaking, it is the presence of the branch in the refs/metas namespace
> +that marks a branch as being a change, not the fact that it points to a
> +metacommit. Metacommits are only created when a commit is amended or rebased, so
> +in the case where a change points to a commit that has never been modified, the
> +change points to that initial commit rather than a metacommit.
> +
> +Changes are also stored in the refs/hiddenmetas namespace. Hiddenmetas holds
> +metadata for historical changes that are not currently in progress by the user.
> +Commands like filter-branch and other bulk import commands create metadata in
> +this namespace.
> +
> +Note that the changes in hiddenmetas get special treatment in several ways:
> +
> +- They are not cleaned up automatically once merged, since it is expected that
> +  they refer to historical changes.
> +- User commands that modify changes don't append to these changes as they would
> +  to a change in refs/metas.
> +- They are not displayed when the user lists their local changes.
> +
> +Obsolescence
> +------------
> +A commit is considered obsolete if it is reachable from the “replaces” edges
> +anywhere in the history of a change and it isn’t the head of that change.
> +Commits may be the content for 0 or more meta-commits. If the same commit
> +appears in multiple changes, it is not obsolete if it is the head of any of
> +those changes.
> +
> +Note that there is an exception to this rule. The metas namespace takes
> +precedence over the hiddenmetas namespace for the purpose of obsolescence. That
> +is, if a change appears in a replaces edge of a change in the metas namespace,
> +it is obsolete even if it also appears as the head of a change in the
> +hiddenmetas namespace.
> +
> +This special case prevents the hiddenmetas namespace from creating divergence
> +with the user's work in progress, and allows the user to resolve historical
> +divergence by creating new changes in the metas namespace.
> +
> +Divergence
> +----------
> +From the user’s perspective, two changes are divergent if they both ask for
> +different replacements to the same commit. More precisely, a target commit is
> +considered divergent if there is more than one commit at the head of a change in
> +refs/metas that leads to the target commit via an unbroken chain of “replaces”
> +parents.
> +
> +Much like a merge conflict, divergence is a situation that requires user
> +intervention to resolve. The evolve command will stop when it encounters
> +divergence and prompt the user to resolve the problem. Users can solve the
> +problem in several ways:
> +
> +- Discard one of the changes (by deleting its change branch).
> +- Merge the two changes (producing a single change branch).
> +- Copy one of the changes (keep both commits, but one of them gets a new
> +  metacommit appended to its history that is connected to its predecessor via an
> +  origin edge rather than a replaces edge. That new change no longer obsoletes
> +  the original.)
> +
> +Obsolescence across cherry-picks
> +--------------------------------
> +By default the evolve command will treat cherry-picks and squash merges as being
> +completely separate from the original. Further amendments to the original commit
> +will have no effect on the cherry-picked copy. However, this behavior may not be
> +desirable in all circumstances.
> +
> +The evolve command may at some point support an option to look for cases where
> +the source of a cherry-pick or squash merge has itself been amended, and
> +automatically apply that same change to the cherry-picked copy. In such cases,
> +it would traverse origin edges rather than ignoring them, and would treat a
> +commit with origin edges as being obsolete if any of its origins were obsolete.
> +
> +Garbage collection
> +------------------
> +For GC purposes, meta-commits are normal commits. Just as a commit causes its
> +parents and tree to be retained, a meta-commit also causes its parents to be
> +retained.
> +
> +Change creation
> +---------------
> +Changes are created automatically whenever the user runs a command like “commit”
> +that has the semantics of creating a new change. They also move forward
> +automatically even if they’re not checked out. For example, whenever the user
> +runs a command like “commit --amend” that modifies a commit, all branches in
> +refs/metas that pointed to the old commit move forward to point to its
> +replacement instead. This also happens when the user is working from a detached
> +head.
> +
> +This does not mean that every commit has a corresponding change. By default,
> +changes only exist for recent locally-created commits. Users may explicitly pull
> +changes from other users or keep their changes around for a long time, but
> +either behavior requires a user to opt-in. Code review systems like gerrit may
> +also choose to keep changes around forever.
> +
> +Note that the changes in refs/metas serve a dual function as both a way to
> +identify obsolete changes and as a way for the user to keep track of their work
> +in progress. If we were only concerned with identifying obsolete changes, it
> +would be sufficient to create the change branch lazily the first time a commit
> +is obsoleted. Addressing the second use - of refs/metas as a mechanism for
> +keeping track of work in progress - is the reason for eagerly creating the
> +change on first commit.
> +
> +Change naming
> +-------------
> +When a change is first created, the only requirement for its name is that it
> +must be unique. Good names would also serve as useful mnemonics and be easy to
> +type. For example, a short word from the commit message containing no numbers or
> +special characters and that shows up with low frequency in other commit messages
> +would make a good choice.
> +
> +Different users may prefer different heuristics for their change names. For this
> +reason a new hook will be introduced to compute change names. Git will invoke
> +the hook for all newly-created changes and will append a numeric suffix if the
> +name isn’t unique. The default heuristics are not specified by this proposal and
> +may change during implementation.
> +
> +Change deletion
> +---------------
> +Changes are normally only interesting to a user while a commit is still in
> +development and under review. Once the commit has submitted wherever it is
> +going, its change can be discarded.
> +
> +The normal way of deleting changes makes this easy to do - changes are deleted
> +by the evolve command when it detects that the change is present in an upstream
> +branch. It does this in two ways: if the latest commit in a change either shows
> +up in the branch history or the change becomes empty after a rebase, it is
> +considered merged and the change is discarded. In this context, an “upstream
> +branch” is any branch passed in as the upstream argument of the evolve command.
> +
> +In case this sometimes deletes a useful change, such automatic deletions are
> +recorded in the reflog allowing them to be easily recovered.
> +
> +Sharing changes
> +---------------
> +Change histories are shared by pushing or fetching meta-commits and change
> +branches. This provides users with a lot of control of what to share and
> +repository implementations with control over what to retain.
> +
> +Users that only want to share the content of a commit can do so by pushing the
> +commit itself as they currently would. Users that want to share an edit history
> +for the commit can push its change, which would point to a meta-commit rather
> +than the commit itself if there is any history to share. Note that multiple
> +changes can refer to the same commits, so it’s possible to construct and push a
> +different history for the same commit in order to remove sensitive or irrelevant
> +intermediate states.
> +
> +Imagine the user is working on a change “mychange” that is currently the latest
> +commit on master. They have two ways to share it:
> +
> +# User shares just a commit without its history
> +> git push origin master
> +
> +# User shares the full history of the commit to a review system
> +> git push origin metas/mychange:refs/for/master
> +
> +# User fetches a collaborator’s modifications to their change
> +> git fetch remotename metas/mychange
> +# Which updates the ref remote/remotename/metas/mychange
> +
> +This will cause more intermediate states to be shared with the server than would
> +have been shared previously. A review system like gerrit would need to keep
> +track of which states had been explicitly pushed versus other intermediate
> +states in order to de-emphasize (or hide) the extra intermediate states from the
> +user interface.
> +
> +Merge-base
> +----------
> +Merge-base will be changed to search the meta-commit graph for common ancestors
> +as well as the commit graph, and will generally prefer results from the
> +meta-commit graph over the commit graph. Merge-base will consider meta-commits
> +from all changes, and will traverse both origin and obsolete edges.
> +
> +The reason for this is that - when merging two versions of the same commit
> +together - an earlier version of that same commit will usually be much more
> +similar than their common parent. This should make the workflow of collaborating
> +on unsubmitted patches as convenient as the workflow for collaborating in a
> +topic branch by eliminating repeated merges.
> +
> +Configuration
> +-------------
> +The core.enableChanges configuration variable enables the creation and update
> +of change branches. This is enabled by default.
> +
> +User interface
> +--------------
> +All git porcelain commands that create commits are classified as having one of
> +four behaviors: modify, create, copy, or import. These behaviors are discussed
> +in more detail below.
> +
> +Modify commands
> +---------------
> +Modification commands (commit --amend, rebase) will mark the old commit as
> +obsolete by creating a new meta-commit that references the old one as a
> +replaced parent. In the event that multiple changes point to the same commit,
> +this is done independently for every such change.
> +
> +More specifically, modifications work like this:
> +
> +1. Locate all existing changes for which the old commit is the content for the
> +   head of the change branch. If no such branch exists, create one that points
> +   to the old commit. Changes that include this commit in their history but not
> +   at their head are explicitly not included.
> +2. For every such change, create a new meta-commit that references the new
> +   commit as its content and references the old head of the change as a
> +   replaced parent.
> +3. Move the change branch forward to point to the new meta-commit.
> +
> +Copy commands
> +-------------
> +Copy commands (cherry-pick, merge --squash) create a new meta-commit that
> +references the old commits as origin parents. Besides the fact that the new
> +parents are tagged differently, copy commands work the same way as modify
> +commands.
> +
> +Create commands
> +---------------
> +Creation commands (commit, merge) create a new commit and a new change that
> +points to that commit. The do not create any meta-commits.
> +
> +Import commands
> +---------------
> +Import commands (fetch, pull) do not create any new meta-commits or changes
> +unless that is specifically what they are importing. For example, the fetch
> +command would update remote/origin/metas/change35 and fetch all referenced
> +meta-commits if asked to do so directly, but it wouldn’t create any changes or
> +meta-commits for commits discovered on the master branch when running “git fetch
> +origin master”.
> +
> +Other commands
> +--------------
> +Some commands don’t fit cleanly into one of the above categories.
> +
> +Semantically, filter-branch should be treated as a modify command, but doing so
> +is likely to create a lot of irrelevant clutter in the changes namespace and the
> +large number of extra change refs may introduce performance problems. We
> +recommend treating filter-branch as an import command initially, but making it
> +behave more like a modify command in future follow-up work. One possible
> +solution may be to treat commits that are part of existing changes as being
> +modified but to avoid creating changes for other rewritten changes. Another
> +solution may be to record the modifications as changes in the hiddenmetas
> +namespace.
> +
> +Once the evolve command can handle obsolescence across cherry-picks, such
> +cherry-picks will result in a hybrid move-and-copy operation. It will create
> +cherry-picks that replace other cherry-picks, which will have both origin edges
> +(pointing to the new source commit being picked) and replacement edges (pointing
> +to the previous cherry-pick being replaced).
> +
> +Evolve
> +------
> +The evolve command performs the correct sequence of rebases such that no change
> +has an obsolete parent. The syntax looks like this:
> +
> +git evolve [upstream…]
> +
> +It takes an optional list of upstream branches. All changes whose parent shows
> +up in the history of one of the upstream branches will be rebased onto the
> +upstream branch before resolving obsolete parents.
> +
> +Any change whose latest state is found in an upstream branch (or that ends up
> +empty after rebase) will be deleted. This is the normal mechanism for deleting
> +changes. Changes are created automatically on the first commit, and are deleted
> +automatically when evolve determines that they’ve been merged upstream.
> +
> +Orphan commits are commits with obsolete parents. The evolve command then
> +repeatedly rebases orphan commits with non-orphan parents until there are either
> +no orphan commits left, or a merge conflict is discovered. It will also
> +terminate if it detects a divergent parent or a cycle that can't be resolved
> +using any of the enabled transformations.
> +
> +When evolve discovers divergence, it will first check if it can resolve the
> +divergence automatically using one of its enabled transformations. Supported
> +transformations are:
> +
> +- Check if the user has already merged the divergent changes in a follow-up
> +  change. That is, look for an existing merge in a follow-up change where all
> +  the parents are divergent versions of the same change. Squash that merge with
> +  its parents and use the result as the resolution for the divergence.
> +
> +- Attempt to auto-merge all the divergent changes (disabled by default).
> +
> +Each of the transformations can be enabled or disabled by command line options.
> +
> +Cycles can occur when two changes reference one another as parents. This can
> +happen when both changes use an obsolete version of the other change as their
> +parent. Although there are never cycles in the commit graph, users can create
> +cycles in the change graph by rebasing changes onto obsolete commits. The evolve
> +command has a transformation that will detect and break cycles by arbitrarily
> +picking one of the changes to go first. If this generates a merge conflict,
> +it tries each of the other changes in sequence to see if any ordering merges
> +cleanly. If no possible ordering merges cleanly, it picks one and terminates
> +to let the user resolve the merge conflict.
> +
> +If the working tree is dirty, evolve will attempt to stash the user's changes
> +before applying the evolve and then reapply those changes afterward, in much
> +the same way as rebase --autostash does.
> +
> +Checkout
> +--------
> +Running checkout on a change by name has the same effect as checking out a
> +detached head pointing to the latest commit on that change-branch. There is no
> +need to ever have HEAD point to a change since changes always move forward when
> +necessary, no matter what branch the user has checked out
> +
> +Meta-commits themselves cannot be checked out by their hash.
> +
> +Reset
> +-----
> +Resetting a branch to a change by name is the same as resetting to the content
> +(or abandoned) commit at that change’s head.
> +
> +Commit
> +------
> +Commit --amend gets modify semantics and will move existing changes forward. The
> +normal form of commit gets create semantics and will create a new change.
> +
> +$ touch foo && git add . && git commit -m "foo" && git tag A
> +$ touch bar && git add . && git commit -m "bar" && git tag B
> +$ touch baz && git add . && git commit -m "baz" && git tag C
> +
> +This produces the following commits:
> +A(tree=[foo])
> +B(tree=[foo, bar], parent=A)
> +C(tree=[foo, bar, baz], parent=B)
> +
> +...along with three changes:
> +metas/foo = A
> +metas/bar = B
> +metas/baz = C
> +
> +Running commit --amend does the following:
> +$ git checkout B
> +$ touch zoom && git add . && git commit --amend -m "baz and zoom"
> +$ git tag D
> +
> +Commits:
> +A(tree=[foo])
> +B(tree=[foo, bar], parent=A)
> +C(tree=[foo, bar, baz], parent=B)
> +D(tree=[foo, bar, zoom], parent=A)
> +Dmeta(content=D, obsolete=B)
> +
> +Changes:
> +metas/foo = A
> +metas/bar = Dmeta
> +metas/baz = C
> +
> +Merge
> +-----
> +Merge gets create, modify, or copy semantics based on what is being merged and
> +the options being used.
> +
> +The --squash version of merge gets copy semantics (it produces a new change that
> +is marked as a copy of all the original changes that were squashed into it).
> +
> +The “modify” version of merge replaces both of the original commits with the
> +resulting merge commit. This is one of the standard mechanisms for resolving
> +divergence. The parents of the merge commit are the parents of the two commits
> +being merged. The resulting commit will not be a merge commit if both of the
> +original commits had the same parent or if one was the parent of the other.
> +
> +The “create” version of merge creates a new change pointing to a merge commit
> +that has both original commits as parents. The result is what merge produces now
> +- a new merge commit. However, this version of merge doesn’t directly resolve
> +divergence.
> +
> +To select between these two behaviors, merge gets new “--amend” and “--noamend”
> +options which select between the “create” and “modify” behaviors respectively,
> +with noamend being the default.
> +
> +For example, imagine we created two divergent changes like this:
> +
> +$ touch foo && git add . && git commit -m "foo" && git tag A
> +$ touch bar && git add . && git commit -m "bar" && git tag B
> +$ touch baz && git add . && git commit --amend -m "bar and baz"
> +$ git tag C
> +$ git checkout B
> +$ touch bam && git add . && git commit --amend -m "bar and bam"
> +$ git tag D
> +
> +At this point the commit graph looks like this:
> +
> +A(tree=[foo])
> +B(tree=[bar], parent=A)
> +C(tree=[bar, baz], parent=A)
> +D(tree=[bar, bam], parent=A)
> +Cmeta(content=C, obsoletes=B)
> +Dmeta(content=D, obsoletes=B)
> +
> +There would be three active changes with heads pointing as follows:
> +
> +metas/changeA=A
> +metas/changeB=Cmeta
> +metas/changeB2=Dmeta
> +
> +ChangeB and changeB2 are divergent at this point. Lets consider what happens if
> +perform each type of merge between changeB and changeB2.
> +
> +Merge example: Amend merge
> +One way to resolve divergent changes is to use an amend merge. Recall that HEAD
> +is currently pointing to D at this point.
> +
> +$ git merge --amend metas/changeB
> +
> +Here we’ve asked for an amend merge since we’re trying to resolve divergence
> +between two versions of the same change. There are no conflicts so we end up
> +with this:
> +
> +E(tree=[bar, baz, bam], parent=A)
> +Emeta(content=E, obsoletes=[Cmeta, Dmeta])
> +
> +With the following branches:
> +
> +metas/changeA=A
> +metas/changeB=Emeta
> +metas/changeB2=Emeta
> +
> +Notice that the result of the “amend merge” is a replacement for C and D rather
> +than a new commit with C and D as parents (as a normal merge would have
> +produced). The parents of the amend merge are the parents of C and D which - in
> +this case - is just A, so the result is not a merge commit. Also notice that
> +changeB and changeB2 are now aliases for the same change.
> +
> +Merge example: Noamend merge
> +Consider what would have happened if we’d used a noamend merge instead. Recall
> +that HEAD was at D and our branches looked like this:
> +
> +metas/changeA=A
> +metas/changeB=Cmeta
> +metas/changeB2=Dmeta
> +
> +$ git merge --noamend metas/changeB
> +
> +That would produce the sort of merge we’d normally expect today:
> +
> +F(tree=[bar, baz, bam], parent=[C, D])
> +
> +And our changes would look like this:
> +metas/changeA=A
> +metas/changeB=Cmeta
> +metas/changeB2=Dmeta
> +metas/changeF=F
> +
> +In this case, changeB and changeB2 are still divergent and we’ve created a new
> +change for our merge commit. However, this is just a temporary state. The next
> +time we run the “evolve” command, it will discover the divergence but also
> +discover the merge commit F that resolves it. Evolve will suggest converting F
> +into an amend merge in order to resolve the divergence and will display the
> +command for doing so.
> +
> +Rebase
> +------
> +In general the rebase command is treated as a modify command. When a change is
> +rebased, the new commit replaces the original.
> +
> +Rebase --abort is special. Its intent is to restore git to the state it had
> +prior to running rebase. It should move back any changes to point to the refs
> +they had prior to running rebase and delete any new changes that were created as
> +part of the rebase. To achieve this, rebase will save the state of all changes
> +in refs/metas prior to running rebase and will restore the entire namespace
> +after rebase completes (deleting any newly-created changes). Newly-created
> +metacommits are left in place, but will have no effect until garbage collected
> +since metacommits are only used if they are reachable from refs/metas.
> +
> +Change
> +------
> +The “change” command can be used to list, rename, reset or delete change. It has
> +a number of subcommands.
> +
> +The "list" subcommand lists local changes. If given the -r argument, it lists
> +remote changes.
> +
> +The "rename" subcommand renames a change, given its old and new name. If the old
> +name is omitted and there is exactly one change pointing to the current HEAD,
> +that change is renamed. If there are no changes pointing to the current HEAD,
> +one is created with the given name.
> +
> +The "forget" subcommand deletes a change by deleting its ref from the metas/
> +namespace. This is the normal way to delete extra aliases for a change if the
> +change has more than one name. By default, this will refuse to delete the last
> +alias for a change if there are any other changes that reference this change as
> +a parent.
> +
> +The "update" subcommand adds a new state to a change. It uses the default
> +algorithm for assigning change names. If the content commit is omitted, HEAD is
> +used. If given the optional --force argument, it will overwrite any existing
> +change of the same name. This latter form of "update" can be used to effectively
> +reset changes.
> +
> +The "update" command can accept any number of --origin and --replace arguments.
> +If any are present, the resulting change branch will point to a metacommit
> +containing the given origin and replacement edges.
> +
> +The "abandon" command deletes a change using obsolescence markers. It marks the
> +change as being obsolete and having been replaced by its parent. If given no
> +arguments, it applies to the current commit. Running evolve will cause any
> +abandoned changes to be removed from the branch. Any child changes will be
> +reparented on top of the parent of the abandoned change. If the current change
> +is abandoned, HEAD will move to point to its parent.
> +
> +The "restore" command restores a previously-abandoned change.
> +
> +The "prune" command deletes all obsolete changes and all changes that are
> +present in the given branch. Note that such changes can be recovered from the
> +reflog.
> +
> +Combined with the GC protection that is offered, this is intended to facilitate
> +a workflow that relies on changes instead of branches. Users could choose to
> +work with no local branches and use changes instead - both for mailing list and
> +gerrit workflows.
> +
> +Log
> +---
> +When a commit is shown in git log that is part of a change, it is decorated with
> +extra change information. If it is the head of a change, the name of the change
> +is shown next to the list of branches. If it is obsolete, it is decorated with
> +the text “obsolete, <n> commits behind <changename>”.
> +
> +Log gets a new --obslog argument indicating that the obsolescence graph should
> +be followed instead of the commit graph. This also changes the default
> +formatting options to make them more appropriate for viewing different
> +iterations of the same commit.
> +
> +Pull
> +----
> +
> +Pull gets an --evolve argument that will automatically attempt to run "evolve"
> +on any affected branches after pulling.
> +
> +We also introduce an "evolve" enum value for the branch.<name>.rebase config
> +value. When set, the evolve behavior will happen automatically for that branch
> +after every pull even if the --evolve argument is not used.
> +
> +Next
> +----
> +
> +The "next" command will reset HEAD to a non-obsolete commit that refers to this
> +change as its parent. If there is more than one such change, the user will be
> +prompted. If given the --evolve argument, the next commit will be evolved if
> +necessary first.
> +
> +The "next" command can be thought of as the opposite of
> +"git reset --hard HEAD^" in that it navigates to a child commit rather than a
> +parent.
> +
> +Prev
> +----
> +
> +The "prev" command will reset HEAD to the latest version of the parent change.
> +If the parent change isn't obsolete, this is equivalent to
> +"git reset --hard HEAD^". If the parent commit is obsolete, it resets to the
> +latest replacement for the parent commit.
> +
> +Other options considered
> +========================
> +We considered several other options for storing the obsolescence graph. This
> +section describes the other options and why they were rejected.
> +
> +Commit header
> +-------------
> +Add an “obsoletes” field to the commit header that points backwards from a
> +commit to the previous commits it obsoletes.
> +
> +Pros:
> +- Very simple
> +- Easy to traverse from a commit to the previous commits it obsoletes.
> +Cons:
> +- Adds a cost to the storage format, even for commits where the change history
> +  is uninteresting.
> +- Unconditionally prevents the change history from being garbage collected.
> +- Always causes the change history to be shared when pushing or pulling changes.
> +
> +Git notes
> +---------
> +Instead of storing obsolescence information in metacommits, the metacommit
> +content could go in a new notes namespace - say refs/notes/metacommit. Each note
> +would contain the list of obsolete and origin parents. An automerger could
> +be supplied to make it easy to merge the metacommit notes from different remotes.
> +
> +Pros:
> +- Easy to locate all commits obsoleted by a given commit (since there would only
> +  be one metacommit for any given commit).
> +Cons:
> +- Wrong GC behavior (obsolete commits wouldn’t automatically be retained by GC)
> +  unless we introduced a special case for these kinds of notes.
> +- No way to selectively share or pull the metacommits for one specific change.
> +  It would be all-or-nothing, which would be expensive. This could be addressed
> +  by changes to the protocol, but this would be invasive.
> +- Requires custom auto-merging behavior on fetch.
> +
> +Tags
> +----
> +Put the content of the metacommit in a message attached to tag on the
> +replacement commit. This is very similar to the git notes approach and has the
> +same pros and cons.
> +
> +Simple forward references
> +-------------------------
> +Record an edge from an obsolete commit to its replacement in this form:
> +
> +refs/obsoletes/<A>
> +
> +pointing to commit <B> as an indication that B is the replacement for the
> +obsolete commit A.
> +
> +Pros:
> +- Protects <B> from being garbage collected.
> +- Fast lookup for the evolve operation, without additional search structures
> +  (“what is the replacement for <A>?” is very fast).
> +
> +Cons:
> +- Can’t represent divergence (which is a P0 requirement).
> +- Creates lots of refs (which can be inefficient)
> +- Doesn’t provide a way to fetch only refs for a specific change.
> +- The obslog command requires a search of all refs.
> +
> +Complex forward references
> +--------------------------
> +Record an edge from an obsolete commit to its replacement in this form:
> +
> +refs/obsoletes/<change_id>/obs<A>_<B>
> +
> +Pointing to commit <B> as an indication that B is the replacement for obsolete
> +commit A.
> +
> +Pros:
> +- Permits sharing and fetching refs for only a specific change.
> +- Supports divergence
> +- Protects <B> from being garbage collected.
> +
> +Cons:
> +- Creates lots of refs, which is inefficient.
> +- Doesn’t provide a good lookup structure for lookups in either direction.
> +
> +Backward references
> +-------------------
> +Record an edge from a replacement commit to the obsolete one in this form:
> +
> +refs/obsolescences/<B>
> +
> +Cons:
> +- Doesn’t provide a way to resolve divergence (which is a P0 requirement).
> +- Doesn’t protect <B> from being garbage collected (which could be fixed by
> +  combining this with a refs/metas namespace, as in the metacommit variant).
> +
> +Obsolescences file
> +------------------
> +Create a custom file (or files) in .git recording obsolescences.
> +
> +Pros:
> +- Can store exactly the information we want with exactly the performance we want
> +  for all operations. For example, there could be a disk-based hashtable
> +  permitting constant time lookups in either direction.
> +
> +Cons:
> +- Handling GC, pushing, and pulling would all require custom solutions. GC
> +  issues could be addressed with a repository format extension.
> +
> +Squash points
> +-------------
> +We treat changes like topic branches, and use special squash points to mark
> +places in the commit graph that separate changes.
> +
> +We create and update change branches in refs/metas at the same time we
> +would have in the metacommit proposal. However, rather than pointing to a
> +metacommit branch they point to normal commits and are treated as “squash
> +points” - markers for sequences of commits intended to be squashed together on
> +submission.
> +
> +Amends and rebases work differently than they do now. Rather than actually
> +containing the desired state of a commit, they contain a delta from the previous
> +version along with a squash point indicating that the preceding changes are
> +intended to be squashed on submission. Specifically, amends would become new
> +changes and rebases would become merge commits with the old commit and new
> +parent as parents.
> +
> +When the changes are finally submitted, the squashes are executed, producing the
> +final version of the commit.
> +
> +In addition to the squash points, git would maintain a set of “nosquash” tags
> +for commits that were used as ancestors of a change that are not meant to be
> +included in the squash.
> +
> +For example, if we have this commit graph:
> +
> +A(...)
> +B(parent=A)
> +C(parent=B)
> +
> +...and we amend B to produce D, we’d get:
> +
> +A(...)
> +B(parent=A)
> +C(parent=B)
> +D(parent=B)
> +
> +...along with a new change branch indicating D should be squashed with its
> +parents when submitted:
> +
> +metas/changeB = D
> +metas/changeC = C
> +
> +We’d also create a nosquash tag for A indicating that A shouldn’t be included
> +when changeB is squashed.
> +
> +If a user amends the change again, they’d get:
> +
> +A(...)
> +B(parent=A)
> +C(parent=B)
> +D(parent=B)
> +E(parent=D)
> +
> +metas/changeB = E
> +metas/changeC = C
> +
> +Pros:
> +- Good GC behavior.
> +- Provides a natural way to share changes (they’re just normal branches).
> +- Merge-base works automatically without special cases.
> +- Rewriting the obslog would be easy using existing git commands.
> +- No new data types needed.
> +Cons:
> +- No way to connect the squashed version of a change to the original, so no way
> +  to automatically clean up old changes. This also means users lose all benefits
> +  of the evolve command if they prematurely squash their commits. This may occur
> +  if a user thinks a change is ready for submission, squashes it, and then later
> +  discovers an additional change to make.
> +- Histories would look very cluttered (users would see all previous edits to
> +  their commit in the commit log, and all previous rebases would show up as
> +  merges). Could be quite hard for users to tell what is going on. (Possible
> +  fix: also implement a new smart log feature that displays the log as though
> +  the squashes had occurred).
> +- Need to change the current behavior of current commands (like amend and
> +  rebase) in ways that will be unexpected to many users.
> --
> gitgitgadget
>

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v2 01/10] technical doc: add a design doc for the evolve command
  2022-10-05 15:16     ` Chris Poucet
@ 2022-10-06 20:53       ` Glen Choo
  0 siblings, 0 replies; 66+ messages in thread
From: Glen Choo @ 2022-10-06 20:53 UTC (permalink / raw)
  To: Chris Poucet, Stefan Xenos via GitGitGadget
  Cc: git, Jerry Zhang, Phillip Wood,
	Ævar Arnfjörð Bjarmason, Christophe Poucet, vdye,
	Junio C Hamano, Jonathan Tan


Hi Chris!

Chris Poucet <poucet@google.com> writes:

> One thing that is not clear to me is whether this is the desired
> direction. I took at look at the git review notes but it was hard to
> get a sense of where people are at.

I'm really sorry, I meant to get back to this sooner with the takeaways
from Review Club. Hopefully this will still be useful.

You can find the Review Club notes here:

  https://docs.google.com/document/d/14L8BAumGTpsXpjDY8VzZ4rRtpAjuGrFSRqn3stCuS_w/edit?pli=1

> Would love input on the design.

Others have given a lot of input on the design, so instead, I'll focus
mostly on how to make the doc better on the mailing list.

>
> On Wed, Oct 5, 2022 at 4:59 PM Stefan Xenos via GitGitGadget
> <gitgitgadget@gmail.com> wrote:
>>
>> From: Stefan Xenos <sxenos@google.com>
>>
>> This document describes what a change graph for
>> git would look like, the behavior of the evolve command,
>> and the changes planned for other commands.
>>
>> It was originally proposed in 2018, see
>> https://public-inbox.org/git/20181115005546.212538-1-sxenos@google.com/

This doc is quite well-thought-out and surprisingly readable despite its
length. That said, it is a lot to review in one sitting, and a reviewer
might get easily fatigued. I suspect that reviewers will find it hard to
keep up with the discussion if they have to review the entire doc on
every iteration.

As Victoria suggested in Review Club, it might be helpful to split up
the design over multiple patches to make feedback more focused. I think
this will make it easier for you (and others) to get a sense of how we
feel about each part of the design. e.g. here's one way to split up the
doc:

- Motivation, Background, High level idea of how a user would use this. 

  (Roughly corresponding to the sections "Objective", "Status",
  "Background", "Goals", "Similar technologies", "Semi-related work")

- Local change tracking, Changes to existing commands, Meta-commits

  (The parts about the data format and their implications for GC,
  negotiation, etc. Maybe include the `change` subcommand if it helps
  reviewers visualize the impact.)

- How evolve works, e.g. convergence, divergence, merge base finding.
  CLI

- Sharing changes

Besides the design, here other sections that I would find useful:

- Glossary. I thought that terms like "change", "change branch" and
  "change graph" were underdefined. This would also be a useful
  reference during the implementation phase.

- Implementation Plan (you can find examples in
  Documentation/technical/bundle-uri.txt and
  Documentation/technical/sparse-index.txt). Making the concrete next
  steps visible has numerous benefits:
  - Reviewers of future patches know what problem is being tackled and
    value is being delivered.
  - The list gains confidence that the author can deliver the work being
    promised.
  - The shared direction makes it easier for others to contribute
    patches.

- Open questions (e.g. "Implementation questions" in [1]). It would be
  useful to know what questions can be answered later instead of right
  now. Also, since you are not the original author, perhaps you also
  have questions about the design that you want answered by reviewers.
  I also wouldn't mind this being in the cover letter or "---" section.

[1] https://lore.kernel.org/git/pull.1367.git.1664064588846.gitgitgadget@gmail.com

As mentioned earlier, I'll comment only very lightly on the design.

>> +Similar technologies
>> +--------------------

I'd personally love to see "git evolve". If it helps to consider some
other tools, I use the following tools that implement similar workflows:

- git-branchless [2] features anonymous heads, obsolescence tracking, 
  history manipulations and "git evolve". Having used this for a while,
  I'm of the opnion that having any of these features without the
  others is still very useful, and implementing them in phases 
  will still deliver value without having to complete all of the work
  (granted, each of these features is incrementally dependent on the
  others).

  Case in point: I don't use the "evolve" equivalent of git-branchless
  (IIRC "restack); being able to see obsolescence and manually
  manipulating history is good enough for me.

- Jujutsu [3] also features anonymous heads, obsolescence tracking and
  advanced history manipulations. Instead of "evolve", descendents of an
  obsolete commit are automatically rebased on the obsoleting commit.

[2] https://github.com/arxanas/git-branchless
[3] https://github.com/martinvonz/jj

>> +Changes
>> +-------
>> +A branch of meta-commits describes how a commit was produced and what previous
>> +commits it is based on. It is also an identifier for a thing the user is
>> +currently working on. We refer to such a meta-branch as a change.
>> +
>> +Local changes are stored in the new refs/metas namespace. Remote changes are
>> +stored in the refs/remote/<remotename>/metas namespace.

I find this terminology of "changes" and "metas" more confusing than
necessary. A glossary would help, but it might be even better to also
use an appropriate ref namespace. "refs/changes/" is an obvious
candidate, though I assume this wasn't mentioned because Gerrit uses
that namespace extensively.

Maybe `refs/changelists`, `refs/change-requests`, `refs/proposals`? Idk.

>> +Sharing changes
>> +---------------
>> +Change histories are shared by pushing or fetching meta-commits and change
>> +branches. This provides users with a lot of control of what to share and
>> +repository implementations with control over what to retain.
>> +
>> +Users that only want to share the content of a commit can do so by pushing the
>> +commit itself as they currently would. Users that want to share an edit history
>> +for the commit can push its change, which would point to a meta-commit rather
>> +than the commit itself if there is any history to share. Note that multiple
>> +changes can refer to the same commits, so it’s possible to construct and push a
>> +different history for the same commit in order to remove sensitive or irrelevant
>> +intermediate states.

I would not like to see the ability to share all intermediate states
with the server because this increases the risk of unintentional
disclosure by a lot.

How exactly we could tweak this can be an open discussion for later.
Some examples I can think of:
  - Asking the user to go through the obsolescence log and manually
    prune revisions (sounds too onerous for users IMO).
  - Push a truncated history consisting of only the latest version and
    commits that the server already knows (somewhat similar to Gerrit).

>> +Evolve
>> +------
>> +The evolve command performs the correct sequence of rebases such that no change
>> +has an obsolete parent. The syntax looks like this:
>> +
>> +git evolve [upstream…]
>> +
>> +It takes an optional list of upstream branches. All changes whose parent shows
>> +up in the history of one of the upstream branches will be rebased onto the
>> +upstream branch before resolving obsolete parents.
>> +

This CLI is an example of something that can be reviewed largely
independently of the implementing data structures.

>> +Merge
>> +-----
>> +
>> +To select between these two behaviors, merge gets new “--amend” and “--noamend”
>> +options which select between the “create” and “modify” behaviors respectively,
>> +with noamend being the default.

Ditto.


^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v2 00/10] RFC: Git Evolve / Change
  2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
                     ` (9 preceding siblings ...)
  2022-10-05 14:59   ` [PATCH v2 10/10] evolve: add tests for the git-change command Chris Poucet via GitGitGadget
@ 2022-10-10  9:23   ` Phillip Wood
  10 siblings, 0 replies; 66+ messages in thread
From: Phillip Wood @ 2022-10-10  9:23 UTC (permalink / raw)
  To: Christophe Poucet via GitGitGadget, git
  Cc: Jerry Zhang, Ævar Arnfjörð Bjarmason, Chris Poucet,
	Christophe Poucet

Hi Chris

On 05/10/2022 15:59, Christophe Poucet via GitGitGadget wrote:
> I'm reviving the original git evolve work that was started by
> sxenos@google.com
> (https://public-inbox.org/git/20190215043105.163688-1-sxenos@google.com/)
> 
> This work is intended to make it easier to deal with stacked changes.
> 
> The following set of patches introduces the design doc on the evolve command
> as well as the basics of the git change command.

Thanks for the new version. When you post a new version of a patch 
series it is helpful to give a brief outline of what you have changed 
since the last version. The overview should also explain the reasons for 
reordering or squashing patches. In this case the old patches 7 & 8 have 
been squashed together. I think it would have been better to leave them 
as separate patches and add the tests at the same time as the commands 
(that's why I provided separate fixups). The style is now closer to our 
normal style but there are still some deviations.

* Comments:

Single line comments should look like
	/* single line */

Multi-line comments should look like
	/*
	 * Multi-line
	 * comment.
	 */
Not
+	/* If to_add is not a metacommit then the content is to_add itself,
+	 * otherwise it will have been set by the call to
+	 * get_metacommit_content.
+	 */

API comments may be formatted as
	/**
	 * API docs
	 */

but then you should not mix styles. For example in patch 5 you have
+	/**
+	 * Memory pool for the objects allocated by the change table.
+	 */
+	struct mem_pool memory_pool;
+	/* Map object_id to commit_change_list_entry structs. */
+	struct oidmap oid_to_metadata_index;

* Functions

Wrap function arguments at 80 columns and indent the continuation lines 
to align with the opening parenthesis unless the function name is 
exceptionally long. There may be more than one function argument per 
line. For example

static void resolve_metacommit(struct repository* repo,
                                struct change_table* active_changes,
                                const struct metacommit_data *to_resolve,
                                struct metacommit_data *resolved_output,
                                struct string_list *to_advance, int 
allow_append)

Not

static void resolve_metacommit(
	struct repository* repo,
	struct change_table* active_changes,
	const struct metacommit_data *to_resolve,
	struct metacommit_data *resolved_output,
	struct string_list *to_advance,
	int allow_append)

You can use git-clang-format to get a fairly close approximation to the 
required style.

For variables and function arguments we generally prefer names to be 
nouns rather than verbs.

Commit messages are expected to explain the motivation the changes not 
just list the functions that are added.

Overall the changes are moving in the right direction, though it's a 
shame the fixup for sorting the output of "git change list" isn't 
included here.

As well as aligning the code style, I think we need to pin down the 
details in patch 1 as there seems to be some on-going discussion about 
the design.

Best Wishes

Phillip

> Chris Poucet (5):
>    sha1-array: implement oid_array_readonly_contains
>    ref-filter: add the metas namespace to ref-filter
>    evolve: add delete command
>    evolve: add documentation for `git change`
>    evolve: add tests for the git-change command
> 
> Stefan Xenos (5):
>    technical doc: add a design doc for the evolve command
>    evolve: add support for parsing metacommits
>    evolve: add the change-table structure
>    evolve: add support for writing metacommits
>    evolve: implement the git change command
> 
>   .gitignore                         |    1 +
>   Documentation/git-change.txt       |   55 ++
>   Documentation/technical/evolve.txt | 1070 ++++++++++++++++++++++++++++
>   Makefile                           |    4 +
>   builtin.h                          |    1 +
>   builtin/change.c                   |  330 +++++++++
>   change-table.c                     |  164 +++++
>   change-table.h                     |  122 ++++
>   commit.c                           |   13 +
>   commit.h                           |    5 +
>   git.c                              |    1 +
>   metacommit-parser.c                |   97 +++
>   metacommit-parser.h                |   19 +
>   metacommit.c                       |  410 +++++++++++
>   metacommit.h                       |   75 ++
>   oid-array.c                        |   12 +
>   oid-array.h                        |    7 +
>   ref-filter.c                       |   10 +-
>   ref-filter.h                       |   10 +-
>   t/helper/test-oid-array.c          |    6 +
>   t/t0064-oid-array.sh               |   22 +
>   t/t9990-changes.sh                 |  148 ++++
>   22 files changed, 2577 insertions(+), 5 deletions(-)
>   create mode 100644 Documentation/git-change.txt
>   create mode 100644 Documentation/technical/evolve.txt
>   create mode 100644 builtin/change.c
>   create mode 100644 change-table.c
>   create mode 100644 change-table.h
>   create mode 100644 metacommit-parser.c
>   create mode 100644 metacommit-parser.h
>   create mode 100644 metacommit.c
>   create mode 100644 metacommit.h
>   create mode 100755 t/t9990-changes.sh
> 
> 
> base-commit: 3dcec76d9df911ed8321007b1d197c1a206dc164
> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1356%2Fpoucet%2Fevolve-v2
> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1356/poucet/evolve-v2
> Pull-Request: https://github.com/gitgitgadget/git/pull/1356
> 
> Range-diff vs v1:
> 
>    1:  a0cf68f8ba2 !  1:  a5eb9325419 technical doc: add a design doc for the evolve command
>       @@ Documentation/technical/evolve.txt (new)
>        +rebase. You can think of rebase -i as a top-down approach and the evolve command
>        +as the bottom-up approach to the same problem.
>        +
>       ++Revup amend (https://github.com/Skydio/revup/blob/main/docs/amend.md)
>       ++allows insertion of cached changes into any commit in
>       ++the current history, and then reapplies the rest of history on top of
>       ++those changes. It uses a "git apply --cached" engine under the hood so
>       ++doesn't touch the working directory (although it will soon use the new
>       ++git merge-tree). When paired with "revup upload" which creates and
>       ++pushes multiple branches in the background for you, its possible to
>       ++work on a "graph" of changes on a single branch linearly, then have
>       ++the true graph structure created at upload time.
>       ++
>       ++git-revise (https://github.com/mystor/git-revise) does some very
>       ++similar things except it uses "git merge-file" combined with manually
>       ++merging the resulting trees. git branchstack
>       ++(https://github.com/krobelus/git-branchstack) can also create branches
>       ++in the background with the same mechanism.
>       ++
>       ++These tools don't store any external state, but as such also don't
>       ++provide any specific collaboration mechanism for individual changes.
>       ++
>        +Several patch queue managers have been built on top of git (such as topgit,
>        +stgit, and quilt). They address the same user need. However they also rely on
>        +state managed outside git that needs to be kept in sync. Such state can be
>    2:  84588312c1d =  2:  ed5106d6080 sha1-array: implement oid_array_readonly_contains
>    3:  54e559967df !  3:  c59066ebc10 ref-filter: add the metas namespace to ref-filter
>       @@ ref-filter.c: int filter_refs(struct ref_array *array, struct ref_filter *filter
>        
>         ## ref-filter.h ##
>        @@
>       + #define FILTER_REFS_TAGS           0x0002
>         #define FILTER_REFS_BRANCHES       0x0004
>         #define FILTER_REFS_REMOTES        0x0008
>       - #define FILTER_REFS_OTHERS         0x0010
>       -+#define FILTER_REFS_CHANGES        0x0040
>       +-#define FILTER_REFS_OTHERS         0x0010
>       ++#define FILTER_REFS_CHANGES        0x0010
>       ++#define FILTER_REFS_OTHERS         0x0040
>         #define FILTER_REFS_ALL            (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \
>        -				    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS)
>        +				    FILTER_REFS_REMOTES | FILTER_REFS_OTHERS | \

This looks good

>    4:  2e9a4a9bd81 !  4:  408941e7400 evolve: add support for parsing metacommits
>       @@ Makefile: LIB_OBJS += merge-ort.o
>         LIB_OBJS += name-hash.o
>         LIB_OBJS += negotiator/default.o
>        
>       + ## commit.c ##
>       +@@ commit.c: struct commit_list *reverse_commit_list(struct commit_list *list)
>       + 	return next;
>       + }
>       +
>       ++struct commit *get_commit_by_index(struct commit_list *to_search, int index)
>       ++{
>       ++	while (to_search && index) {
>       ++		to_search = to_search->next;
>       ++		index--;
>       ++	}
>       ++
>       ++	if (!to_search)
>       ++		return NULL;
>       ++
>       ++	return to_search->item;
>       ++}
>       ++
>       + void free_commit_list(struct commit_list *list)
>       + {
>       + 	while (list)
>       +
>       + ## commit.h ##
>       +@@ commit.h: struct commit_list *copy_commit_list(struct commit_list *list);
>       + /* Modify list in-place to reverse it, returning new head; list will be tail */
>       + struct commit_list *reverse_commit_list(struct commit_list *list);
>       +
>       ++/* Returns the commit at `index` or NULL if the index exceeds the `to_search`
>       ++ * list */
>       ++struct commit *get_commit_by_index(struct commit_list *to_search, int index);
>       ++
>       + void free_commit_list(struct commit_list *list);
>       +
>       ++
>       + struct rev_info; /* in revision.h, it circularly uses enum cmit_fmt */
>       +
>       + int has_non_ascii(const char *text);
>       +
>         ## metacommit-parser.c (new) ##
>        @@
>        +#include "cache.h"
>       @@ metacommit-parser.c (new)
>        +	return NULL;
>        +}
>        +
>       -+static struct commit *get_commit_by_index(struct commit_list *to_search, int index)
>       -+{
>       -+	while (to_search && index) {
>       -+		to_search = to_search->next;
>       -+		index--;
>       -+	}
>       -+
>       -+	if (!to_search)
>       -+		return NULL;
>       -+
>       -+	return to_search->item;
>       -+}
>       -+
>        +/*
>        + * Writes the index of the content parent to "result". Returns the metacommit
>        + * type. See the METACOMMIT_TYPE_* constants.
>        + */
>       -+static int index_of_content_commit(const char *buffer, int *result)
>       ++static enum metacommit_type index_of_content_commit(const char *buffer, int *result)
>        +{
>        +	int index = 0;
>        +	int ret = METACOMMIT_TYPE_NONE;
>       @@ metacommit-parser.c (new)
>        +		char next = *parent_types;
>        +		if (next == ' ' || parent_types >= end) {
>        +			if (enum_length == 1) {
>       -+				char first_char_in_enum = *enum_start;
>       -+				if (first_char_in_enum == 'c') {
>       ++				char type = *enum_start;
>       ++				if (type == 'c') {
>        +					ret = METACOMMIT_TYPE_NORMAL;
>        +					break;
>        +				}
>       -+				if (first_char_in_enum == 'a') {
>       ++				if (type == 'a') {
>        +					ret = METACOMMIT_TYPE_ABANDONED;
>        +					break;
>        +				}
>       @@ metacommit-parser.c (new)
>        + * Writes the content parent's object id to "content".
>        + * Returns the metacommit type. See the METACOMMIT_TYPE_* constants.
>        + */
>       -+int get_metacommit_content(struct commit *commit, struct object_id *content)
>       ++enum metacommit_type get_metacommit_content(struct commit *commit, struct object_id *content)
>        +{
>        +	const char *buffer = get_commit_buffer(commit, NULL);
>        +	int index = 0;
>       -+	int ret = index_of_content_commit(buffer, &index);
>       ++	enum metacommit_type ret = index_of_content_commit(buffer, &index);
>        +	struct commit *content_parent;
>        +
>        +	if (ret == METACOMMIT_TYPE_NONE)
>       @@ metacommit-parser.h (new)
>        +#include "commit.h"
>        +#include "hash.h"
>        +
>       -+/* Indicates a normal commit (non-metacommit) */
>       -+#define METACOMMIT_TYPE_NONE 0
>       -+/* Indicates a metacommit with normal content (non-abandoned) */
>       -+#define METACOMMIT_TYPE_NORMAL 1
>       -+/* Indicates a metacommit with abandoned content */
>       -+#define METACOMMIT_TYPE_ABANDONED 2
>       -+
>       -+struct commit;
>       ++enum metacommit_type {
>       ++	/* Indicates a normal commit (non-metacommit) */
>       ++	METACOMMIT_TYPE_NONE = 0,
>       ++	/* Indicates a metacommit with normal content (non-abandoned) */
>       ++	METACOMMIT_TYPE_NORMAL = 1,
>       ++	/* Indicates a metacommit with abandoned content */
>       ++	METACOMMIT_TYPE_ABANDONED = 2,
>       ++};
>        +
>       -+extern int get_metacommit_content(
>       ++enum metacommit_type get_metacommit_content(
>        +	struct commit *commit, struct object_id *content);
>        +
>        +#endif
>    5:  2b3a00a6702 !  5:  48cd92d35ef evolve: add the change-table structure
>       @@ change-table.c (new)
>        +#include "ref-filter.h"
>        +#include "metacommit-parser.h"
>        +
>       -+void change_table_init(struct change_table *to_initialize)
>       ++void change_table_init(struct change_table *table)
>        +{
>       -+	memset(to_initialize, 0, sizeof(*to_initialize));
>       -+	mem_pool_init(&to_initialize->memory_pool, 0);
>       -+	to_initialize->memory_pool.block_alloc = 4*1024 - sizeof(struct mp_block);
>       -+	oidmap_init(&to_initialize->oid_to_metadata_index, 0);
>       -+	string_list_init_dup(&to_initialize->refname_to_change_head);
>       ++	memset(table, 0, sizeof(*table));
>       ++	mem_pool_init(&table->memory_pool, 0);
>       ++	oidmap_init(&table->oid_to_metadata_index, 0);
>       ++	strmap_init(&table->refname_to_change_head);
>        +}
>        +
>       -+static void change_list_clear(struct change_list *to_clear) {
>       -+	string_list_clear(&to_clear->additional_refnames, 0);
>       ++static void change_list_clear(struct change_list *change_list) {
>       ++	strset_clear(&change_list->refnames);
>        +}
>        +
>        +static void commit_change_list_entry_clear(
>       -+	struct commit_change_list_entry *to_clear) {
>       -+	change_list_clear(&to_clear->changes);
>       ++	struct commit_change_list_entry *entry) {
>       ++	change_list_clear(&entry->changes);
>        +}
>        +
>       -+void change_table_clear(struct change_table *to_clear)
>       ++void change_table_clear(struct change_table *table)
>        +{
>        +	struct oidmap_iter iter;
>        +	struct commit_change_list_entry *next;
>       -+	for (next = oidmap_iter_first(&to_clear->oid_to_metadata_index, &iter);
>       ++	for (next = oidmap_iter_first(&table->oid_to_metadata_index, &iter);
>        +		next;
>        +		next = oidmap_iter_next(&iter)) {
>        +
>        +		commit_change_list_entry_clear(next);
>        +	}
>        +
>       -+	oidmap_free(&to_clear->oid_to_metadata_index, 0);
>       -+	string_list_clear(&to_clear->refname_to_change_head, 0);
>       -+	mem_pool_discard(&to_clear->memory_pool, 0);
>       ++	oidmap_free(&table->oid_to_metadata_index, 0);
>       ++	strmap_clear(&table->refname_to_change_head, 0);
>       ++	mem_pool_discard(&table->memory_pool, 0);
>        +}
>        +
>       -+static void add_head_to_commit(struct change_table *to_modify,
>       -+	const struct object_id *to_add, const char *refname)
>       ++static void add_head_to_commit(struct change_table *table,
>       ++			       const struct object_id *to_add,
>       ++			       const char *refname)
>        +{
>        +	struct commit_change_list_entry *entry;
>        +
>       -+	/**
>       -+	 * Note: the indices in the map are 1-based. 0 is used to indicate a missing
>       -+	 * element.
>       -+	 */
>       -+	entry = oidmap_get(&to_modify->oid_to_metadata_index, to_add);
>       ++	entry = oidmap_get(&table->oid_to_metadata_index, to_add);
>        +	if (!entry) {
>       -+		entry = mem_pool_calloc(&to_modify->memory_pool, 1,
>       -+			sizeof(*entry));
>       ++		entry = mem_pool_calloc(&table->memory_pool, 1, sizeof(*entry));
>        +		oidcpy(&entry->entry.oid, to_add);
>       -+		oidmap_put(&to_modify->oid_to_metadata_index, entry);
>       -+		string_list_init_nodup(&entry->changes.additional_refnames);
>       ++		strset_init(&entry->changes.refnames);
>       ++		oidmap_put(&table->oid_to_metadata_index, entry);
>        +	}
>       -+
>       -+	if (!entry->changes.first_refname)
>       -+		entry->changes.first_refname = refname;
>       -+	else
>       -+		string_list_insert(&entry->changes.additional_refnames, refname);
>       ++	strset_add(&entry->changes.refnames, refname);
>        +}
>        +
>       -+void change_table_add(struct change_table *to_modify, const char *refname,
>       -+	struct commit *to_add)
>       ++void change_table_add(struct change_table *table,
>       ++		      const char *refname,
>       ++		      struct commit *to_add)
>        +{
>        +	struct change_head *new_head;
>       -+	struct string_list_item *new_item;
>        +	int metacommit_type;
>        +
>       -+	new_head = mem_pool_calloc(&to_modify->memory_pool, 1,
>       -+		sizeof(*new_head));
>       ++	new_head = mem_pool_calloc(&table->memory_pool, 1, sizeof(*new_head));
>        +
>        +	oidcpy(&new_head->head, &to_add->object.oid);
>        +
>        +	metacommit_type = get_metacommit_content(to_add, &new_head->content);
>       ++	/* If to_add is not a metacommit then the content is to_add itself,
>       ++	 * otherwise it will have been set by the call to
>       ++	 * get_metacommit_content.
>       ++	 */
>        +	if (metacommit_type == METACOMMIT_TYPE_NONE)
>        +		oidcpy(&new_head->content, &to_add->object.oid);
>        +	new_head->abandoned = (metacommit_type == METACOMMIT_TYPE_ABANDONED);
>        +	new_head->remote = starts_with(refname, "refs/remote/");
>        +	new_head->hidden = starts_with(refname, "refs/hiddenmetas/");
>        +
>       -+	new_item = string_list_insert(&to_modify->refname_to_change_head, refname);
>       -+	new_item->util = new_head;
>       -+	/* Use pointers to the copy of the string we're retaining locally */
>       -+	refname = new_item->string;
>       -+
>       -+	if (!oideq(&new_head->content, &new_head->head))
>       -+		add_head_to_commit(to_modify, &new_head->content, refname);
>       -+	add_head_to_commit(to_modify, &new_head->head, refname);
>       -+}
>       -+
>       -+void change_table_add_all_visible(struct change_table *to_modify,
>       -+	struct repository* repo)
>       -+{
>       -+	struct ref_filter filter;
>       -+	const char *name_patterns[] = {NULL};
>       -+	memset(&filter, 0, sizeof(filter));
>       -+	filter.kind = FILTER_REFS_CHANGES;
>       -+	filter.name_patterns = name_patterns;
>       ++	strmap_put(&table->refname_to_change_head, refname, new_head);
>        +
>       -+	change_table_add_matching_filter(to_modify, repo, &filter);
>       ++	if (!oideq(&new_head->content, &new_head->head)) {
>       ++		/* We also remember to link between refname and the content oid */
>       ++		add_head_to_commit(table, &new_head->content, refname);
>       ++	}
>       ++	add_head_to_commit(table, &new_head->head, refname);
>        +}
>        +
>       -+void change_table_add_matching_filter(struct change_table *to_modify,
>       -+	struct repository* repo, struct ref_filter *filter)
>       ++static void change_table_add_matching_filter(struct change_table *table,
>       ++					     struct repository* repo,
>       ++					     struct ref_filter *filter)
>        +{
>       -+	struct ref_array matching_refs;
>        +	int i;
>       ++	struct ref_array matching_refs = { 0 };
>        +
>       -+	memset(&matching_refs, 0, sizeof(matching_refs));
>        +	filter_refs(&matching_refs, filter, filter->kind);
>        +
>       -+	/**
>       ++	/*
>        +	 * Determine the object id for the latest content commit for each change.
>        +	 * Fetch the commit at the head of each change ref. If it's a normal commit,
>        +	 * that's the commit we want. If it's a metacommit, locate its content parent
>       @@ change-table.c (new)
>        +
>        +	for (i = 0; i < matching_refs.nr; i++) {
>        +		struct ref_array_item *item = matching_refs.items[i];
>       -+		struct commit *commit = item->commit;
>       ++		struct commit *commit;
>        +
>       -+		commit = lookup_commit_reference_gently(repo, &item->objectname, 1);
>       -+
>       -+		if (commit)
>       -+			change_table_add(to_modify, item->refname, commit);
>       ++		commit = lookup_commit_reference(repo, &item->objectname);
>       ++		if (!commit) {
>       ++			BUG("Invalid commit for refs/meta: %s", item->refname);
>       ++		}
>       ++		change_table_add(table, item->refname, commit);
>        +	}
>        +
>        +	ref_array_clear(&matching_refs);
>        +}
>        +
>       ++void change_table_add_all_visible(struct change_table *table,
>       ++	struct repository* repo)
>       ++{
>       ++	struct ref_filter filter = { 0 };
>       ++	const char *name_patterns[] = {NULL};
>       ++	filter.kind = FILTER_REFS_CHANGES;
>       ++	filter.name_patterns = name_patterns;
>       ++
>       ++	change_table_add_matching_filter(table, repo, &filter);
>       ++}
>       ++
>        +static int return_true_callback(const char *refname, void *cb_data)
>        +{
>        +	return 1;
>        +}
>        +
>       -+int change_table_has_change_referencing(struct change_table *changes,
>       ++int change_table_has_change_referencing(struct change_table *table,
>        +	const struct object_id *referenced_commit_id)
>        +{
>       -+	return for_each_change_referencing(changes, referenced_commit_id,
>       ++	return for_each_change_referencing(table, referenced_commit_id,
>        +		return_true_callback, NULL);
>        +}
>        +
>        +int for_each_change_referencing(struct change_table *table,
>        +	const struct object_id *referenced_commit_id, each_change_fn fn, void *cb_data)
>        +{
>       -+	const struct change_list *changes;
>       -+	int i;
>       -+	int retvalue;
>       -+	struct commit_change_list_entry *entry;
>       ++	int ret;
>       ++	struct commit_change_list_entry *ccl_entry;
>       ++	struct hashmap_iter iter;
>       ++	struct strmap_entry *entry;
>        +
>       -+	entry = oidmap_get(&table->oid_to_metadata_index,
>       -+		referenced_commit_id);
>       ++	ccl_entry = oidmap_get(&table->oid_to_metadata_index,
>       ++			       referenced_commit_id);
>        +	/* If this commit isn't referenced by any changes, it won't be in the map */
>       -+	if (!entry)
>       ++	if (!ccl_entry)
>        +		return 0;
>       -+	changes = &entry->changes;
>       -+	if (!changes->first_refname)
>       -+		return 0;
>       -+	retvalue = fn(changes->first_refname, cb_data);
>       -+	for (i = 0; retvalue == 0 && i < changes->additional_refnames.nr; i++)
>       -+		retvalue = fn(changes->additional_refnames.items[i].string, cb_data);
>       -+	return retvalue;
>       ++	strset_for_each_entry(&ccl_entry->changes.refnames, &iter, entry) {
>       ++		ret = fn(entry->key, cb_data);
>       ++		if (ret != 0) break;
>       ++	}
>       ++	return ret;
>        +}
>        +
>       -+struct change_head* get_change_head(struct change_table *heads,
>       ++struct change_head* get_change_head(struct change_table *table,
>        +	const char* refname)
>        +{
>       -+	struct string_list_item *item = string_list_lookup(
>       -+		&heads->refname_to_change_head, refname);
>       -+
>       -+	if (!item)
>       -+		return NULL;
>       -+
>       -+	return (struct change_head *)item->util;
>       ++	return strmap_get(&table->refname_to_change_head, refname);
>        +}
>        
>         ## change-table.h (new) ##
>       @@ change-table.h (new)
>        +#define CHANGE_TABLE_H
>        +
>        +#include "oidmap.h"
>       ++#include "strmap.h"
>        +
>        +struct commit;
>        +struct ref_filter;
>        +
>        +/**
>       -+ * This struct holds a list of change refs. The first element is stored inline,
>       -+ * to optimize for small lists.
>       ++ * This struct holds a set of change refs.
>        + */
>        +struct change_list {
>        +	/**
>       -+	 * Ref name for the first change in the list, or null if none.
>       -+	 *
>       ++	 * The refnames in this set.
>        +	 * This field is private. Use for_each_change_in to read.
>        +	 */
>       -+	const char* first_refname;
>       -+	/**
>       -+	 * List of additional change refs. Note that this is empty if the list
>       -+	 * contains 0 or 1 elements.
>       -+	 *
>       -+	 * This field is private. Use for_each_change_in to read.
>       -+	 */
>       -+	struct string_list additional_refnames;
>       ++	struct strset refnames;
>        +};
>        +
>        +/**
>       @@ change-table.h (new)
>        +};
>        +
>        +/**
>       -+ * Holds information about the heads of each change, and permits effecient
>       ++ * Holds information about the heads of each change, and permits efficient
>        + * lookup from a commit to the changes that reference it directly.
>        + *
>        + * All fields should be considered private. Use the change_table functions
>       @@ change-table.h (new)
>        +	/* Map object_id to commit_change_list_entry structs. */
>        +	struct oidmap oid_to_metadata_index;
>        +	/**
>       -+	 * List of ref names. The util value points to a change_head structure
>       -+	 * allocated from memory_pool.
>       ++	 * Map of refnames to change_head structure which are allocated from
>       ++	 * memory_pool.
>        +	 */
>       -+	struct string_list refname_to_change_head;
>       ++	struct strmap refname_to_change_head;
>        +};
>        +
>       -+extern void change_table_init(struct change_table *to_initialize);
>       -+extern void change_table_clear(struct change_table *to_clear);
>       ++extern void change_table_init(struct change_table *table);
>       ++extern void change_table_clear(struct change_table *table);
>        +
>        +/* Adds the given change head to the change_table struct */
>       -+extern void change_table_add(struct change_table *to_modify,
>       -+	const char *refname, struct commit *target);
>       ++extern void change_table_add(struct change_table *table,
>       ++			     const char *refname,
>       ++			     struct commit *target);
>        +
>        +/**
>        + * Adds the non-hidden local changes to the given change_table struct.
>        + */
>       -+extern void change_table_add_all_visible(struct change_table *to_modify,
>       -+	struct repository *repo);
>       -+
>       -+/*
>       -+ * Adds all changes matching the given ref filter to the given change_table
>       -+ * struct.
>       -+ */
>       -+extern void change_table_add_matching_filter(struct change_table *to_modify,
>       -+	struct repository* repo, struct ref_filter *filter);
>       ++extern void change_table_add_all_visible(struct change_table *table,
>       ++					 struct repository *repo);
>        +
>        +typedef int each_change_fn(const char *refname, void *cb_data);
>        +
>       -+extern int change_table_has_change_referencing(struct change_table *changes,
>       ++extern int change_table_has_change_referencing(
>       ++	struct change_table *table,
>        +	const struct object_id *referenced_commit_id);
>        +
>        +/**
>       @@ change-table.h (new)
>        + * For normal commits, this is the list of changes that have this commit as
>        + * their latest content.
>        + */
>       -+extern int for_each_change_referencing(struct change_table *heads,
>       -+	const struct object_id *referenced_commit_id, each_change_fn fn, void *cb_data);
>       ++extern int for_each_change_referencing(
>       ++	struct change_table *table,
>       ++	const struct object_id *referenced_commit_id,
>       ++	each_change_fn fn,
>       ++	void *cb_data);
>        +
>        +/**
>        + * Returns the change head for the given refname. Returns NULL if no such change
>        + * exists.
>        + */
>       -+extern struct change_head* get_change_head(struct change_table *heads,
>       ++extern struct change_head* get_change_head(struct change_table *table,
>        +	const char* refname);
>        +
>        +#endif
>    6:  56c6770997b !  6:  353d97d0f38 evolve: add support for writing metacommits
>       @@ metacommit.c (new)
>        +#include "change-table.h"
>        +#include "refs.h"
>        +
>       -+void init_metacommit_data(struct metacommit_data *state)
>       -+{
>       -+	memset(state, 0, sizeof(*state));
>       -+}
>       -+
>        +void clear_metacommit_data(struct metacommit_data *state)
>        +{
>       ++	oidcpy(&state->content, null_oid());
>        +	oid_array_clear(&state->replace);
>        +	oid_array_clear(&state->origin);
>       ++	state->abandoned = 0;
>        +}
>        +
>        +static void compute_default_change_name(struct commit *initial_commit,
>       -+	struct strbuf* result)
>       ++					struct strbuf* result)
>        +{
>       -+	struct strbuf default_name;
>       ++	struct strbuf default_name = STRBUF_INIT;
>        +	const char *buffer;
>        +	const char *subject;
>        +	const char *eol;
>       -+	int len;
>       -+	strbuf_init(&default_name, 0);
>       ++	size_t len;
>        +	buffer = get_commit_buffer(initial_commit, NULL);
>        +	find_commit_subject(buffer, &subject);
>        +	eol = strchrnul(subject, '\n');
>       -+	for (len = 0;subject < eol && len < 10; ++subject, ++len) {
>       ++	for (len = 0; subject < eol && len < 10; subject++, len++) {
>        +		char next = *subject;
>        +		if (isspace(next))
>        +			continue;
>       @@ metacommit.c (new)
>        +		strbuf_addch(&default_name, next);
>        +	}
>        +	sanitize_refname_component(default_name.buf, result);
>       ++	unuse_commit_buffer(initial_commit, buffer);
>        +}
>        +
>       -+/**
>       ++/*
>        + * Computes a change name for a change rooted at the given initial commit. Good
>        + * change names should be memorable, unique, and easy to type. They are not
>        + * required to match the commit comment.
>        + */
>        +static void compute_change_name(struct commit *initial_commit, struct strbuf* result)
>        +{
>       -+	struct strbuf default_name;
>       ++	struct strbuf default_name = STRBUF_INIT;
>        +	struct object_id unused;
>        +
>       -+	strbuf_init(&default_name, 0);
>        +	if (initial_commit)
>        +		compute_default_change_name(initial_commit, &default_name);
>        +	else
>       -+		strbuf_addstr(&default_name, "change");
>       ++		BUG("initial commit is NULL");
>        +	strbuf_addstr(result, "refs/metas/");
>        +	strbuf_addbuf(result, &default_name);
>        +
>        +	/* If there is already a change of this name, append a suffix */
>        +	if (!read_ref(result->buf, &unused)) {
>        +		int suffix = 2;
>       -+		int original_length = result->len;
>       ++		size_t original_length = result->len;
>        +
>        +		while (1) {
>        +			strbuf_addf(result, "%d", suffix);
>        +			if (read_ref(result->buf, &unused))
>        +				break;
>       -+			strbuf_remove(result, original_length, result->len - original_length);
>       ++			strbuf_remove(result, original_length,
>       ++				      result->len - original_length);
>        +			++suffix;
>        +		}
>        +	}
>       @@ metacommit.c (new)
>        +	strbuf_release(&default_name);
>        +}
>        +
>       -+struct resolve_metacommit_callback_data
>       ++struct resolve_metacommit_context
>        +{
>        +	struct change_table* active_changes;
>        +	struct string_list *changes;
>       @@ metacommit.c (new)
>        +
>        +static int resolve_metacommit_callback(const char *refname, void *cb_data)
>        +{
>       -+	struct resolve_metacommit_callback_data *data = (struct resolve_metacommit_callback_data *)cb_data;
>       ++	struct resolve_metacommit_context *data = cb_data;
>        +	struct change_head *chhead;
>        +
>        +	chhead = get_change_head(data->active_changes, refname);
>        +
>        +	if (data->changes)
>       -+		string_list_append(data->changes, refname)->util = &(chhead->head);
>       ++		string_list_append(data->changes, refname)->util = &chhead->head;
>        +	if (data->heads)
>        +		oid_array_append(data->heads, &(chhead->head));
>        +
>        +	return 0;
>        +}
>        +
>       -+/**
>       ++/*
>        + * Produces the final form of a metacommit based on the current change refs.
>        + */
>        +static void resolve_metacommit(
>       @@ metacommit.c (new)
>        +	struct string_list *to_advance,
>        +	int allow_append)
>        +{
>       -+	int i;
>       -+	int len = to_resolve->replace.nr;
>       -+	struct resolve_metacommit_callback_data cbdata;
>       ++	size_t i;
>       ++	size_t len = to_resolve->replace.nr;
>       ++	struct resolve_metacommit_context ctx = {
>       ++		.active_changes = active_changes,
>       ++		.changes = to_advance,
>       ++		.heads = &resolved_output->replace
>       ++	};
>        +	int old_change_list_length = to_advance->nr;
>        +	struct commit* content;
>        +
>        +	oidcpy(&resolved_output->content, &to_resolve->content);
>        +
>       -+	/* First look for changes that point to any of the replacement edges in the
>       ++	/*
>       ++	 * First look for changes that point to any of the replacement edges in the
>        +	 * metacommit. These will be the changes that get advanced by this
>       -+	 * metacommit. */
>       ++	 * metacommit.
>       ++	 */
>        +	resolved_output->abandoned = to_resolve->abandoned;
>       -+	cbdata.active_changes = active_changes;
>       -+	cbdata.changes = to_advance;
>       -+	cbdata.heads = &(resolved_output->replace);
>        +
>        +	if (allow_append) {
>        +		for (i = 0; i < len; i++) {
>        +			int old_number = resolved_output->replace.nr;
>       -+			for_each_change_referencing(active_changes, &(to_resolve->replace.oid[i]),
>       -+				resolve_metacommit_callback, &cbdata);
>       ++			for_each_change_referencing(
>       ++				active_changes,
>       ++				&(to_resolve->replace.oid[i]),
>       ++				resolve_metacommit_callback,
>       ++				&ctx);
>        +			/* If no changes were found, use the unresolved value. */
>        +			if (old_number == resolved_output->replace.nr)
>       -+				oid_array_append(&(resolved_output->replace), &(to_resolve->replace.oid[i]));
>       ++				oid_array_append(&(resolved_output->replace),
>       ++						 &(to_resolve->replace.oid[i]));
>        +		}
>        +	}
>        +
>       -+	cbdata.changes = NULL;
>       -+	cbdata.heads = &(resolved_output->origin);
>       ++	ctx.changes = NULL;
>       ++	ctx.heads = &(resolved_output->origin);
>        +
>        +	len = to_resolve->origin.nr;
>        +	for (i = 0; i < len; i++) {
>        +		int old_number = resolved_output->origin.nr;
>       -+		for_each_change_referencing(active_changes, &(to_resolve->origin.oid[i]),
>       -+			resolve_metacommit_callback, &cbdata);
>       ++		for_each_change_referencing(
>       ++			active_changes,
>       ++			&(to_resolve->origin.oid[i]),
>       ++			resolve_metacommit_callback,
>       ++			&ctx);
>        +		if (old_number == resolved_output->origin.nr)
>       -+			oid_array_append(&(resolved_output->origin), &(to_resolve->origin.oid[i]));
>       ++			oid_array_append(&(resolved_output->origin),
>       ++					 &(to_resolve->origin.oid[i]));
>        +	}
>        +
>       -+	/* If no changes were advanced by this metacommit, we'll need to create a new
>       -+	 * one. */
>       ++	/*
>       ++	 * If no changes were advanced by this metacommit, we'll need to create
>       ++	 * a new one. */
>        +	if (to_advance->nr == old_change_list_length) {
>        +		struct strbuf change_name;
>        +
>        +		strbuf_init(&change_name, 80);
>       -+		content = lookup_commit_reference_gently(repo, &(to_resolve->content), 1);
>       ++
>       ++		content = lookup_commit_reference_gently(
>       ++			repo, &(to_resolve->content), 1);
>        +
>        +		compute_change_name(content, &change_name);
>        +		string_list_append(to_advance, change_name.buf);
>       @@ metacommit.c (new)
>        +
>        +	while (--i >= 0) {
>        +		struct object_id *next = &(to_lookup->oid[i]);
>       -+		struct commit *commit = lookup_commit_reference_gently(repo, next, 1);
>       ++		struct commit *commit =
>       ++			lookup_commit_reference_gently(repo, next, 1);
>        +		commit_list_insert(commit, result);
>        +	}
>        +}
>        +
>        +#define PARENT_TYPE_PREFIX "parent-type "
>        +
>       -+/**
>       -+ * Creates a new metacommit object with the given content. Writes the object
>       -+ * id of the newly-created commit to result.
>       -+ */
>        +int write_metacommit(struct repository *repo, struct metacommit_data *state,
>        +	struct object_id *result)
>        +{
>        +	struct commit_list *parents = NULL;
>        +	struct strbuf comment;
>       -+	int i;
>       ++	size_t i;
>        +	struct commit *content;
>        +
>        +	strbuf_init(&comment, strlen(PARENT_TYPE_PREFIX)
>       @@ metacommit.c (new)
>        +		strbuf_addstr(&comment, " o");
>        +
>        +	/* The parents list will be freed by this call. */
>       -+	commit_tree(comment.buf, comment.len, repo->hash_algo->empty_tree, parents,
>       -+		result, NULL, NULL);
>       ++	commit_tree(
>       ++		comment.buf,
>       ++		comment.len,
>       ++		repo->hash_algo->empty_tree,
>       ++		parents,
>       ++		result,
>       ++		NULL,
>       ++		NULL);
>        +
>        +	strbuf_release(&comment);
>        +	return 0;
>        +}
>        +
>       -+/**
>       ++/*
>        + * Returns true iff the given metacommit is abandoned, has one or more origin
>        + * parents, or has one or more replacement parents.
>        + */
>       @@ metacommit.c (new)
>        + * to append to existing changes wherever possible instead of creating new ones.
>        + * If override_change is non-null, only the given change ref will be updated.
>        + *
>       -+ * options is a bitwise combination of the UPDATE_OPTION_* flags.
>       -+ */
>       -+int record_metacommit(
>       -+	struct repository *repo,
>       -+	const struct metacommit_data *metacommit, const char *override_change,
>       -+	int options, struct strbuf *err)
>       -+{
>       -+		struct change_table chtable;
>       -+		struct string_list changes;
>       -+		int result;
>       -+
>       -+		change_table_init(&chtable);
>       -+		change_table_add_all_visible(&chtable, repo);
>       -+		string_list_init_dup(&changes);
>       -+
>       -+		result = record_metacommit_withresult(repo, &chtable, metacommit,
>       -+			override_change, options, err, &changes);
>       -+
>       -+		string_list_clear(&changes, 0);
>       -+		change_table_clear(&chtable);
>       -+		return result;
>       -+}
>       -+
>       -+/*
>       -+ * Records the relationships described by the given metacommit in the
>       -+ * repository.
>       -+ *
>       -+ * If override_change is NULL (the default), an attempt will be made
>       -+ * to append to existing changes wherever possible instead of creating new ones.
>       -+ * If override_change is non-null, only the given change ref will be updated.
>       -+ *
>        + * The changes list is filled in with the list of change refs that were updated,
>        + * with the util pointers pointing to the old object IDS for those changes.
>        + * The object ID pointers all point to objects owned by the change_table and
>       @@ metacommit.c (new)
>        + *
>        + * options is a bitwise combination of the UPDATE_OPTION_* flags.
>        + */
>       -+int record_metacommit_withresult(
>       ++static int record_metacommit_withresult(
>        +	struct repository *repo,
>        +	struct change_table *chtable,
>        +	const struct metacommit_data *metacommit,
>        +	const char *override_change,
>       -+	int options, struct strbuf *err,
>       ++	int options,
>       ++	struct strbuf *err,
>        +	struct string_list *changes)
>        +{
>        +	static const char *msg = "updating change";
>       -+	struct metacommit_data resolved_metacommit;
>       ++	struct metacommit_data resolved_metacommit = METACOMMIT_DATA_INIT;
>        +	struct object_id commit_target;
>        +	struct ref_transaction *transaction = NULL;
>        +	struct change_head *overridden_head;
>        +	const struct object_id *old_head;
>        +
>       -+	int i;
>       ++	size_t i;
>        +	int ret = 0;
>        +	int force = (options & UPDATE_OPTION_FORCE);
>        +
>       -+	init_metacommit_data(&resolved_metacommit);
>       -+
>        +	resolve_metacommit(repo, chtable, metacommit, &resolved_metacommit, changes,
>        +		(options & UPDATE_OPTION_NOAPPEND) == 0);
>        +
>        +	if (override_change) {
>        +		string_list_clear(changes, 0);
>        +		overridden_head = get_change_head(chtable, override_change);
>       -+		if (!overridden_head) {
>       ++		if (overridden_head) {
>        +			/* This is an existing change */
>        +			old_head = &overridden_head->head;
>        +			if (!force) {
>       @@ metacommit.c (new)
>        +			/* ...then this is a newly-created change */
>        +			old_head = null_oid();
>        +
>       -+		/* The expected "current" head of the change is stored in the util
>       -+		 * pointer. */
>       -+		string_list_append(changes, override_change)->util = (void*)old_head;
>       ++		/*
>       ++		 * The expected "current" head of the change is stored in the
>       ++		 * util pointer. Cast required because old_head is const*
>       ++		 */
>       ++		string_list_append(changes, override_change)->util = (void *)old_head;
>        +	}
>        +
>        +	if (is_nontrivial_metacommit(&resolved_metacommit)) {
>       @@ metacommit.c (new)
>        +			ret = -1;
>        +			goto cleanup;
>        +		}
>       -+	} else
>       -+		/**
>       ++	} else {
>       ++		/*
>        +		 * If the metacommit would only contain a content commit, point to the
>        +		 * commit itself rather than creating a trivial metacommit.
>        +		 */
>        +		oidcpy(&commit_target, &(resolved_metacommit.content));
>       ++	}
>        +
>       -+	/**
>       ++	/*
>        +	 * If a change already exists with this target and we're not forcing an
>        +	 * update to some specific override_change && change, there's nothing to do.
>        +	 */
>       @@ metacommit.c (new)
>        +		for (i = 0; i < changes->nr; i++) {
>        +			struct string_list_item *it = &changes->items[i];
>        +
>       -+			/**
>       ++			/*
>        +			 * The expected current head of the change is stored in the util pointer.
>        +			 * It is null if the change should be newly-created.
>        +			 */
>       @@ metacommit.c (new)
>        +	return ret;
>        +}
>        +
>       -+/**
>       -+ * Should be invoked after a command that has "modify" semantics - commands that
>       -+ * create a new commit based on an old commit and treat the new one as a
>       -+ * replacement for the old one. This method records the replacement in the
>       -+ * change graph, such that a future evolve operation will rebase children of
>       -+ * the old commit onto the new commit.
>       -+ */
>       ++int record_metacommit(
>       ++	struct repository *repo,
>       ++	const struct metacommit_data *metacommit,
>       ++	const char *override_change,
>       ++	int options,
>       ++	struct strbuf *err,
>       ++	struct string_list *changes)
>       ++{
>       ++		struct change_table chtable;
>       ++		int result;
>       ++
>       ++		change_table_init(&chtable);
>       ++		change_table_add_all_visible(&chtable, repo);
>       ++
>       ++		result = record_metacommit_withresult(
>       ++			repo,
>       ++			&chtable,
>       ++			metacommit,
>       ++			override_change,
>       ++			options,
>       ++			err,
>       ++			changes);
>       ++
>       ++		change_table_clear(&chtable);
>       ++		return result;
>       ++}
>       ++
>        +void modify_change(
>        +	struct repository *repo,
>        +	const struct object_id *old_commit,
>        +	const struct object_id *new_commit,
>        +	struct strbuf *err)
>        +{
>       -+	struct metacommit_data metacommit;
>       ++	struct string_list changes = STRING_LIST_INIT_DUP;
>       ++	struct metacommit_data metacommit = METACOMMIT_DATA_INIT;
>        +
>       -+	init_metacommit_data(&metacommit);
>        +	oidcpy(&(metacommit.content), new_commit);
>        +	oid_array_append(&(metacommit.replace), old_commit);
>        +
>       -+	record_metacommit(repo, &metacommit, NULL, 0, err);
>       ++	record_metacommit(repo, &metacommit, NULL, 0, err, &changes);
>        +
>        +	clear_metacommit_data(&metacommit);
>       ++	string_list_clear(&changes, 0);
>        +}
>        
>         ## metacommit.h (new) ##
>       @@ metacommit.h (new)
>        +#include "repository.h"
>        +#include "string-list.h"
>        +
>       -+
>       -+struct change_table;
>       -+
>        +/* If specified, non-fast-forward changes are permitted. */
>        +#define UPDATE_OPTION_FORCE     0x0001
>        +/**
>       @@ metacommit.h (new)
>        +	int abandoned;
>        +};
>        +
>       -+extern void init_metacommit_data(struct metacommit_data *state);
>       ++#define METACOMMIT_DATA_INIT { 0 }
>        +
>        +extern void clear_metacommit_data(struct metacommit_data *state);
>        +
>       -+extern int record_metacommit(struct repository *repo,
>       -+	const struct metacommit_data *metacommit,
>       -+	const char* override_change, int options, struct strbuf *err);
>       -+
>       -+extern int record_metacommit_withresult(
>       ++/**
>       ++ * Records the relationships described by the given metacommit in the
>       ++ * repository.
>       ++ *
>       ++ * If override_change is NULL (the default), an attempt will be made
>       ++ * to append to existing changes wherever possible instead of creating new ones.
>       ++ * If override_change is non-null, only the given change ref will be updated.
>       ++ *
>       ++ * options is a bitwise combination of the UPDATE_OPTION_* flags.
>       ++ */
>       ++int record_metacommit(
>        +	struct repository *repo,
>       -+	struct change_table *chtable,
>        +	const struct metacommit_data *metacommit,
>       -+	const char *override_change,
>       ++	const char* override_change,
>        +	int options,
>        +	struct strbuf *err,
>        +	struct string_list *changes);
>        +
>       -+extern void modify_change(struct repository *repo,
>       -+	const struct object_id *old_commit, const struct object_id *new_commit,
>       ++/**
>       ++ * Should be invoked after a command that has "modify" semantics - commands that
>       ++ * create a new commit based on an old commit and treat the new one as a
>       ++ * replacement for the old one. This method records the replacement in the
>       ++ * change graph, such that a future evolve operation will rebase children of
>       ++ * the old commit onto the new commit.
>       ++ */
>       ++void modify_change(
>       ++	struct repository *repo,
>       ++	const struct object_id *old_commit,
>       ++	const struct object_id *new_commit,
>        +	struct strbuf *err);
>        +
>       -+extern int write_metacommit(struct repository *repo, struct metacommit_data *state,
>       ++/**
>       ++ * Creates a new metacommit object with the given content. Writes the object
>       ++ * id of the newly-created commit to result.
>       ++ */
>       ++int write_metacommit(
>       ++	struct repository *repo,
>       ++	struct metacommit_data *state,
>        +	struct object_id *result);
>        +
>        +#endif
>    7:  91402834184 !  7:  f7a90700e0e evolve: implement the git change command
>       @@ builtin/change.c (new)
>        +#include "ref-filter.h"
>        +#include "parse-options.h"
>        +#include "metacommit.h"
>       -+#include "change-table.h"
>        +#include "config.h"
>        +
>        +static const char * const builtin_change_usage[] = {
>       -+	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
>       ++	N_("git change list [<pattern>...]"),
>       ++	N_("git change update [--force] [--replace <treeish>...] "
>       ++	   "[--origin <treeish>...] [--content <newtreeish>]"),
>       ++	NULL
>       ++};
>       ++
>       ++static const char * const builtin_list_usage[] = {
>       ++	N_("git change list [<pattern>...]"),
>        +	NULL
>        +};
>        +
>        +static const char * const builtin_update_usage[] = {
>       -+	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
>       ++	N_("git change update [--force] [--replace <treeish>...] "
>       ++	"[--origin <treeish>...] [--content <newtreeish>]"),
>        +	NULL
>        +};
>        +
>       ++static int change_list(int argc, const char **argv, const char* prefix)
>       ++{
>       ++	struct option options[] = {
>       ++		OPT_END()
>       ++	};
>       ++	struct ref_filter filter = { 0 };
>       ++	struct ref_sorting *sorting;
>       ++	struct string_list sorting_options = STRING_LIST_INIT_DUP;
>       ++	struct ref_format format = REF_FORMAT_INIT;
>       ++	struct ref_array array = { 0 };
>       ++	size_t i;
>       ++
>       ++	argc = parse_options(argc, argv, prefix, options, builtin_list_usage, 0);
>       ++
>       ++	setup_ref_filter_porcelain_msg();
>       ++
>       ++	filter.kind = FILTER_REFS_CHANGES;
>       ++	filter.name_patterns = argv;
>       ++
>       ++	filter_refs(&array, &filter, FILTER_REFS_CHANGES);
>       ++
>       ++	/* TODO: This causes a crash. It sets one of the atom_value handlers to
>       ++	 * something invalid, which causes a crash later when we call
>       ++	 * show_ref_array_item. Figure out why this happens and put back the sorting.
>       ++	 *
>       ++	 * sorting = ref_sorting_options(&sorting_options);
>       ++	 * ref_array_sort(sorting, &array); */
>       ++
>       ++	if (!format.format)
>       ++		format.format = "%(refname:lstrip=1)";
>       ++
>       ++	if (verify_ref_format(&format))
>       ++		die(_("unable to parse format string"));
>       ++
>       ++	sorting = ref_sorting_options(&sorting_options);
>       ++	ref_array_sort(sorting, &array);
>       ++
>       ++
>       ++	for (i = 0; i < array.nr; i++) {
>       ++		struct strbuf output = STRBUF_INIT;
>       ++		struct strbuf err = STRBUF_INIT;
>       ++		if (format_ref_array_item(array.items[i], &format, &output, &err))
>       ++			die("%s", err.buf);
>       ++		fwrite(output.buf, 1, output.len, stdout);
>       ++		putchar('\n');
>       ++
>       ++		strbuf_release(&err);
>       ++		strbuf_release(&output);
>       ++	}
>       ++
>       ++	ref_array_clear(&array);
>       ++	ref_sorting_release(sorting);
>       ++
>       ++	return 0;
>       ++}
>       ++
>        +struct update_state {
>        +	int options;
>        +	const char* change;
>       @@ builtin/change.c (new)
>        +	struct string_list origin;
>        +};
>        +
>       -+static void init_update_state(struct update_state *state)
>       -+{
>       -+	memset(state, 0, sizeof(*state));
>       -+	state->content = "HEAD";
>       -+	string_list_init_nodup(&state->replace);
>       -+	string_list_init_nodup(&state->origin);
>       ++#define UPDATE_STATE_INIT { \
>       ++	.content = "HEAD", \
>       ++	.replace = STRING_LIST_INIT_NODUP, \
>       ++	.origin = STRING_LIST_INIT_NODUP \
>        +}
>        +
>        +static void clear_update_state(struct update_state *state)
>       @@ builtin/change.c (new)
>        +{
>        +	struct commit *commit;
>        +	if (get_oid_committish(committish, result))
>       -+		die(_("Failed to resolve '%s' as a valid revision."), committish);
>       ++		die(_("failed to resolve '%s' as a valid revision."), committish);
>        +	commit = lookup_commit_reference(the_repository, result);
>        +	if (!commit)
>       -+		die(_("Could not parse object '%s'."), committish);
>       ++		die(_("could not parse object '%s'."), committish);
>        +	oidcpy(result, &commit->object.oid);
>        +	return 0;
>        +}
>       @@ builtin/change.c (new)
>        +static void resolve_commit_list(const struct string_list *commitsish_list,
>        +	struct oid_array* result)
>        +{
>       -+	int i;
>       -+	for (i = 0; i < commitsish_list->nr; i++) {
>       -+		struct string_list_item *item = &commitsish_list->items[i];
>       ++	struct string_list_item *item;
>       ++
>       ++	for_each_string_list_item(item, commitsish_list) {
>        +		struct object_id next;
>        +		resolve_commit(item->string, &next);
>        +		oid_array_append(result, &next);
>       @@ builtin/change.c (new)
>        +	const struct update_state *state,
>        +	struct strbuf *err)
>        +{
>       -+	struct metacommit_data metacommit;
>       -+	struct change_table chtable;
>       -+	struct string_list changes;
>       ++	struct metacommit_data metacommit = METACOMMIT_DATA_INIT;
>       ++	struct string_list changes = STRING_LIST_INIT_DUP;
>        +	int ret;
>       -+	int i;
>       -+
>       -+	change_table_init(&chtable);
>       -+	change_table_add_all_visible(&chtable, repo);
>       -+	string_list_init_dup(&changes);
>       -+
>       -+	init_metacommit_data(&metacommit);
>       ++	struct string_list_item *item;
>        +
>        +	get_metacommit_from_command_line(state, &metacommit);
>        +
>       -+	ret = record_metacommit_withresult(repo, &chtable, &metacommit,
>       -+		state->change, state->options, err, &changes);
>       ++	ret = record_metacommit(
>       ++		repo,
>       ++		&metacommit,
>       ++		state->change,
>       ++		state->options,
>       ++		err,
>       ++		&changes);
>        +
>       -+	for (i = 0; i < changes.nr; i++) {
>       -+		struct string_list_item *it = &changes.items[i];
>       ++	for_each_string_list_item(item, &changes) {
>        +
>       -+		const char* name = lstrip_ref_components(it->string, 1);
>       ++		const char* name = lstrip_ref_components(item->string, 1);
>        +		if (!name)
>       -+			die(_("Failed to remove `refs/` from %s"), it->string);
>       ++			die(_("failed to remove `refs/` from %s"), item->string);
>        +
>       -+		if (it->util)
>       -+			fprintf(stdout, N_("Updated change %s\n"), name);
>       ++		if (item->util)
>       ++			fprintf(stdout, _("Updated change %s"), name);
>        +		else
>       -+			fprintf(stdout, N_("Created change %s\n"), name);
>       ++			fprintf(stdout, _("Created change %s"), name);
>       ++		putchar('\n');
>        +	}
>        +
>        +	string_list_clear(&changes, 0);
>       -+	change_table_clear(&chtable);
>        +	clear_metacommit_data(&metacommit);
>        +
>        +	return ret;
>       @@ builtin/change.c (new)
>        +static int change_update(int argc, const char **argv, const char* prefix)
>        +{
>        +	int result;
>       -+	int force = 0;
>       -+	int newchange = 0;
>        +	struct strbuf err = STRBUF_INIT;
>       -+	struct update_state state;
>       ++	struct update_state state = UPDATE_STATE_INIT;
>        +	struct option options[] = {
>        +		{ OPTION_CALLBACK, 'r', "replace", &state, N_("commit"),
>        +			N_("marks the given commit as being obsolete"),
>       @@ builtin/change.c (new)
>        +		{ OPTION_CALLBACK, 'o', "origin", &state, N_("commit"),
>        +			N_("marks the given commit as being the origin of this commit"),
>        +			0, update_option_parse_origin },
>       -+		OPT_BOOL('F', "force", &force,
>       -+			N_("overwrite an existing change of the same name")),
>       ++
>        +		OPT_STRING('c', "content", &state.content, N_("commit"),
>        +				 N_("identifies the new content commit for the change")),
>        +		OPT_STRING('g', "change", &state.change, N_("commit"),
>        +				 N_("name of the change to update")),
>       -+		OPT_BOOL('n', "new", &newchange,
>       -+			N_("create a new change - do not append to any existing change")),
>       ++		OPT_SET_INT_F('n', "new", &state.options,
>       ++			      N_("create a new change - do not append to any existing change"),
>       ++			      UPDATE_OPTION_NOAPPEND, 0),
>       ++		OPT_SET_INT_F('F', "force", &state.options,
>       ++			      N_("overwrite an existing change of the same name"),
>       ++			      UPDATE_OPTION_FORCE, 0),
>        +		OPT_END()
>        +	};
>        +
>       -+	init_update_state(&state);
>       -+
>        +	argc = parse_options(argc, argv, prefix, options, builtin_update_usage, 0);
>       -+
>       -+	if (force) state.options |= UPDATE_OPTION_FORCE;
>       -+	if (newchange) state.options |= UPDATE_OPTION_NOAPPEND;
>       -+
>        +	result = perform_update(the_repository, &state, &err);
>        +
>        +	if (result < 0) {
>       @@ builtin/change.c (new)
>        +
>        +int cmd_change(int argc, const char **argv, const char *prefix)
>        +{
>       ++	parse_opt_subcommand_fn *fn = NULL;
>        +	/* No options permitted before subcommand currently */
>        +	struct option options[] = {
>       ++		OPT_SUBCOMMAND("list", &fn, change_list),
>       ++		OPT_SUBCOMMAND("update", &fn, change_update),
>        +		OPT_END()
>        +	};
>       -+	int result = 1;
>        +
>        +	argc = parse_options(argc, argv, prefix, options, builtin_change_usage,
>       -+		PARSE_OPT_STOP_AT_NON_OPTION);
>       -+
>       -+	if (argc < 1)
>       -+		usage_with_options(builtin_change_usage, options);
>       -+	else if (!strcmp(argv[0], "update"))
>       -+		result = change_update(argc, argv, prefix);
>       -+	else {
>       -+		error(_("Unknown subcommand: %s"), argv[0]);
>       -+		usage_with_options(builtin_change_usage, options);
>       ++		PARSE_OPT_SUBCOMMAND_OPTIONAL);
>       ++
>       ++	if (!fn) {
>       ++		if (argc) {
>       ++			error(_("unknown subcommand: `%s'"), argv[0]);
>       ++			usage_with_options(builtin_change_usage, options);
>       ++		}
>       ++		fn = change_list;
>        +	}
>        +
>       -+	return result ? 1 : 0;
>       ++	return !!fn(argc, argv, prefix);
>        +}
>        
>         ## git.c ##
>    9:  d087d467e3f !  8:  a0669fa63a1 evolve: add delete command
>       @@ Commit message
>        
>         ## builtin/change.c ##
>        @@
>       + #include "parse-options.h"
>         #include "metacommit.h"
>       - #include "change-table.h"
>         #include "config.h"
>        +#include "refs.h"
>         
>         static const char * const builtin_change_usage[] = {
>         	N_("git change list [<pattern>...]"),
>       --	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
>       -+	N_("git change update [--force] [--replace <treeish>...] [--origin <treeish>...] [--content <newtreeish>]"),
>       + 	N_("git change update [--force] [--replace <treeish>...] "
>       + 	   "[--origin <treeish>...] [--content <newtreeish>]"),
>        +	N_("git change delete <change-name>..."),
>         	NULL
>         };
>         
>       -@@ builtin/change.c: static const char * const builtin_list_usage[] = {
>       +@@ builtin/change.c: static const char * const builtin_update_usage[] = {
>       + 	NULL
>         };
>         
>       - static const char * const builtin_update_usage[] = {
>       --	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
>       -+	N_("git change update [--force] [--replace <treeish>...] [--origin <treeish>...] [--content <newtreeish>]"),
>       ++static const char * const builtin_delete_usage[] = {
>       ++	N_("git change delete <change-name>..."),
>        +	NULL
>        +};
>        +
>       -+static const char * const builtin_delete_usage[] = {
>       -+	N_("git change delete <change-name>..."),
>       - 	NULL
>       - };
>       -
>       + static int change_list(int argc, const char **argv, const char* prefix)
>       + {
>       + 	struct option options[] = {
>        @@ builtin/change.c: static int change_update(int argc, const char **argv, const char* prefix)
>         	return result;
>         }
>       @@ builtin/change.c: static int change_update(int argc, const char **argv, const ch
>        +
>         int cmd_change(int argc, const char **argv, const char *prefix)
>         {
>       - 	/* No options permitted before subcommand currently */
>       + 	parse_opt_subcommand_fn *fn = NULL;
>        @@ builtin/change.c: int cmd_change(int argc, const char **argv, const char *prefix)
>       - 		result = change_list(argc, argv, prefix);
>       - 	else if (!strcmp(argv[0], "update"))
>       - 		result = change_update(argc, argv, prefix);
>       -+	else if (!strcmp(argv[0], "delete"))
>       -+		result = change_delete(argc, argv, prefix);
>       - 	else {
>       - 		error(_("Unknown subcommand: %s"), argv[0]);
>       - 		usage_with_options(builtin_change_usage, options);
>       + 	struct option options[] = {
>       + 		OPT_SUBCOMMAND("list", &fn, change_list),
>       + 		OPT_SUBCOMMAND("update", &fn, change_update),
>       ++		OPT_SUBCOMMAND("delete", &fn, change_delete),
>       + 		OPT_END()
>       + 	};
>       +
>   10:  811d516e5d2 =  9:  e67ff668fff evolve: add documentation for `git change`
>    8:  b83a79beeb4 ! 10:  37042b58cda evolve: add the git change list command
>       @@
>         ## Metadata ##
>       -Author: Stefan Xenos <sxenos@google.com>
>       +Author: Chris Poucet <poucet@google.com>
>        
>         ## Commit message ##
>       -    evolve: add the git change list command
>       +    evolve: add tests for the git-change command
>        
>       -    This command lists the ongoing changes from the refs/metas
>       -    namespace.
>       -
>       -    Signed-off-by: Stefan Xenos <sxenos@google.com>
>       +    Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
>            Signed-off-by: Chris Poucet <poucet@google.com>
>        
>       - ## builtin/change.c ##
>       + ## t/t9990-changes.sh (new) ##
>        @@
>       - #include "config.h"
>       -
>       - static const char * const builtin_change_usage[] = {
>       -+	N_("git change list [<pattern>...]"),
>       - 	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
>       - 	NULL
>       - };
>       -
>       -+static const char * const builtin_list_usage[] = {
>       -+	N_("git change list [<pattern>...]"),
>       -+	NULL
>       -+};
>       ++#!/bin/sh
>        +
>       - static const char * const builtin_update_usage[] = {
>       - 	N_("git change update [--force] [--replace <treeish>...] [--origin <treesih>...] [--content <newtreeish>]"),
>       - 	NULL
>       - };
>       -
>       -+static int change_list(int argc, const char **argv, const char* prefix)
>       -+{
>       -+	struct option options[] = {
>       -+		OPT_END()
>       -+	};
>       -+	struct ref_filter filter;
>       -+	/* TODO: See below
>       -+	struct ref_sorting *sorting;
>       -+	struct string_list sorting_options = STRING_LIST_INIT_DUP; */
>       -+	struct ref_format format = REF_FORMAT_INIT;
>       -+	struct ref_array array;
>       -+	int i;
>       ++test_description='git change - low level meta-commit management'
>        +
>       -+	argc = parse_options(argc, argv, prefix, options, builtin_list_usage, 0);
>       ++. ./test-lib.sh
>        +
>       -+	setup_ref_filter_porcelain_msg();
>       ++. "$TEST_DIRECTORY"/lib-rebase.sh
>        +
>       -+	memset(&filter, 0, sizeof(filter));
>       -+	memset(&array, 0, sizeof(array));
>       ++test_expect_success 'setup commits and meta-commits' '
>       ++       for c in one two three
>       ++       do
>       ++               test_commit $c &&
>       ++               git change update --content $c >actual 2>err &&
>       ++               echo "Created change metas/$c" >expect &&
>       ++               test_cmp expect actual &&
>       ++               test_must_be_empty err &&
>       ++               test_cmp_rev refs/metas/$c $c || return 1
>       ++       done
>       ++'
>        +
>       -+	filter.kind = FILTER_REFS_CHANGES;
>       -+	filter.name_patterns = argv;
>       ++# Check a meta-commit has the correct parents Call with the object
>       ++# name of the meta-commit followed by pairs of type and parent
>       ++check_meta_commit () {
>       ++       name=$1
>       ++       shift
>       ++       while test $# -gt 0
>       ++       do
>       ++               printf '%s %s\n' $1 $(git rev-parse --verify $2)
>       ++               shift
>       ++               shift
>       ++       done | sort >expect
>       ++       git cat-file commit $name >metacommit &&
>       ++       # commit body should consist of parent-type
>       ++           types="$(sed -n '/^$/ {
>       ++                       :loop
>       ++                       n
>       ++                       s/^parent-type //
>       ++                       p
>       ++                       b loop
>       ++                   }' metacommit)" &&
>       ++       while read key value
>       ++       do
>       ++               # TODO: don't sort the first parent
>       ++               if test "$key" = "parent"
>       ++               then
>       ++                       type="${types%% *}"
>       ++                       test -n "$type" || return 1
>       ++                       printf '%s %s\n' $type $value
>       ++                       types="${types#?}"
>       ++                       types="${types# }"
>       ++               elif test "$key" = "tree"
>       ++               then
>       ++                       test_cmp_rev "$value" $EMPTY_TREE || return 1
>       ++               elif test -z "$key"
>       ++               then
>       ++                       # only parse commit headers
>       ++                       break
>       ++               fi
>       ++       done <metacommit >actual-unsorted &&
>       ++       test -z "$types" &&
>       ++       sort >actual <actual-unsorted &&
>       ++       test_cmp expect actual
>       ++}
>        +
>       -+	filter_refs(&array, &filter, FILTER_REFS_CHANGES);
>       ++test_expect_success 'update meta-commits after rebase' '
>       ++       (
>       ++               set_fake_editor &&
>       ++               FAKE_AMEND=edited &&
>       ++               FAKE_LINES="reword 1 pick 2 fixup 3" &&
>       ++               export FAKE_AMEND FAKE_LINES &&
>       ++               git rebase -i --root
>       ++       ) &&
>        +
>       -+	/* TODO: This causes a crash. It sets one of the atom_value handlers to
>       -+	 * something invalid, which causes a crash later when we call
>       -+	 * show_ref_array_item. Figure out why this happens and put back the sorting.
>       -+	 *
>       -+	 * sorting = ref_sorting_options(&sorting_options);
>       -+	 * ref_array_sort(sorting, &array); */
>       ++       # update meta-commits
>       ++       git change update --replace tags/one --content HEAD~1 >out 2>err &&
>       ++       echo "Updated change metas/one" >expect &&
>       ++       test_cmp expect out &&
>       ++       test_must_be_empty err &&
>       ++       git change update --replace tags/two --content HEAD@{2} &&
>       ++       oid=$(git rev-parse --verify metas/two) &&
>       ++       git change update --replace HEAD@{2} --replace tags/three \
>       ++               --content HEAD &&
>        +
>       -+	if (!format.format)
>       -+		format.format = "%(refname:lstrip=1)";
>       ++       # check meta-commits
>       ++       check_meta_commit metas/one c HEAD~1 r tags/one &&
>       ++       check_meta_commit $oid c HEAD@{2} r tags/two &&
>       ++       # NB this checks that "git change update" uses the meta-commit ($oid)
>       ++       #    corresponding to the replaces commit (HEAD@2 above) given on the
>       ++       #    commandline.
>       ++       check_meta_commit metas/two c HEAD r $oid r tags/three &&
>       ++       check_meta_commit metas/three c HEAD r $oid r tags/three
>       ++'
>        +
>       -+	if (verify_ref_format(&format))
>       -+		die(_("unable to parse format string"));
>       ++reset_meta_commits () {
>       ++    for c in one two three
>       ++    do
>       ++       echo "update refs/metas/$c refs/tags/$c^0"
>       ++    done | git update-ref --stdin
>       ++}
>        +
>       -+	for (i = 0; i < array.nr; i++) {
>       -+		struct strbuf output = STRBUF_INIT;
>       -+		struct strbuf err = STRBUF_INIT;
>       -+		if (format_ref_array_item(array.items[i], &format, &output, &err))
>       -+			die("%s", err.buf);
>       -+		fwrite(output.buf, 1, output.len, stdout);
>       -+		putchar('\n');
>       ++test_expect_success 'override change name' '
>       ++       # TODO: builtin/change.c expects --change to be the full refname,
>       ++       #       ideally it would prepend refs/metas to the string given by the
>       ++       #       user.
>       ++       git change update --change refs/metas/another-one --content one &&
>       ++       test_cmp_rev metas/another-one one
>       ++'
>        +
>       -+		strbuf_release(&err);
>       -+		strbuf_release(&output);
>       -+	}
>       ++test_expect_success 'non-fast forward meta-commit update refused' '
>       ++       test_must_fail git change update --change refs/metas/one --content two \
>       ++               >out 2>err &&
>       ++       echo "error: non-fast-forward update to ${SQ}refs/metas/one${SQ}" \
>       ++               >expect &&
>       ++       test_cmp expect err &&
>       ++       test_must_be_empty out
>       ++'
>        +
>       -+	ref_array_clear(&array);
>       -+	/* TODO: see above
>       -+	ref_sorting_release(sorting); */
>       ++test_expect_success 'forced non-fast forward update succeeds' '
>       ++       git change update --change refs/metas/one --content two --force \
>       ++               >out 2>err &&
>       ++       echo "Updated change metas/one" >expect &&
>       ++       test_cmp expect out &&
>       ++       test_must_be_empty err
>       ++'
>        +
>       -+	return 0;
>       -+}
>       ++test_expect_success 'list changes' '
>       ++       cat >expect <<-\EOF &&
>       ++metas/another-one
>       ++metas/one
>       ++metas/three
>       ++metas/two
>       ++EOF
>       ++       git change list >actual &&
>       ++       test_cmp expect actual
>       ++'
>       ++
>       ++test_expect_success 'delete change' '
>       ++       git change delete metas/one &&
>       ++       cat >expect <<-\EOF &&
>       ++metas/another-one
>       ++metas/three
>       ++metas/two
>       ++EOF
>       ++       git change list >actual &&
>       ++       test_cmp expect actual
>       ++'
>        +
>       - struct update_state {
>       - 	int options;
>       - 	const char* change;
>       -@@ builtin/change.c: int cmd_change(int argc, const char **argv, const char *prefix)
>       -
>       - 	if (argc < 1)
>       - 		usage_with_options(builtin_change_usage, options);
>       -+	else if (!strcmp(argv[0], "list"))
>       -+		result = change_list(argc, argv, prefix);
>       - 	else if (!strcmp(argv[0], "update"))
>       - 		result = change_update(argc, argv, prefix);
>       - 	else {
>       ++test_done
> 


^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v2 01/10] technical doc: add a design doc for the evolve command
  2022-10-05 14:59   ` [PATCH v2 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
  2022-10-05 15:16     ` Chris Poucet
@ 2022-10-10 19:35     ` Victoria Dye
  2022-10-11  8:59       ` Phillip Wood
  1 sibling, 1 reply; 66+ messages in thread
From: Victoria Dye @ 2022-10-10 19:35 UTC (permalink / raw)
  To: Stefan Xenos via GitGitGadget, git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Stefan Xenos

Stefan Xenos via GitGitGadget wrote:
> From: Stefan Xenos <sxenos@google.com>
> 
> This document describes what a change graph for
> git would look like, the behavior of the evolve command,
> and the changes planned for other commands.
> 
> It was originally proposed in 2018, see
> https://public-inbox.org/git/20181115005546.212538-1-sxenos@google.com/
> 
> Signed-off-by: Stefan Xenos <sxenos@google.com>
> Signed-off-by: Chris Poucet <poucet@google.com>
> ---
>  Documentation/technical/evolve.txt | 1070 ++++++++++++++++++++++++++++
>  1 file changed, 1070 insertions(+)
>  create mode 100644 Documentation/technical/evolve.txt
> 
> diff --git a/Documentation/technical/evolve.txt b/Documentation/technical/evolve.txt
> new file mode 100644
> index 00000000000..2051ea77b8a
> --- /dev/null
> +++ b/Documentation/technical/evolve.txt
> @@ -0,0 +1,1070 @@
> +Evolve
> +======
> +
> +Objective
> +=========
> +Create an "evolve" command to help users craft a high quality commit history.
> +Users can improve commits one at a time and in any order, then run git evolve to
> +rewrite their recent history to ensure everything is up-to-date. We track
> +amendments to a commit over time in a change graph. Users can share their
> +progress with others by exchanging their change graphs using the standard push,
> +fetch, and format-patch commands.
> +
> +Status
> +======
> +This proposal has not been implemented yet.
> +
> +Background
> +==========
> +Imagine you have three sequential changes up for review and you receive feedback
> +that requires editing all three changes. We'll define the word "change"
> +formally later, but for the moment let's say that a change is a work-in-progress
> +whose final version will be submitted as a commit in the future.
> +
> +While you're editing one change, more feedback arrives on one of the others.
> +What do you do?

For the sake of providing additional perspectives, I can say that I'd:

$ git stash
# Make changes based on new feedback
$ git add . & git commit --fixup <target commit>
$ git stash pop
# Continue working

or something along those lines.

> +
> +The evolve command is a convenient way to work with chains of commits that are
> +under review. Whenever you rebase or amend a commit, the repository remembers
> +that the old commit is obsolete and has been replaced by the new one. Then, at
> +some point in the future, you can run "git evolve" and the correct sequence of
> +rebases will occur in the correct order such that no commit has an obsolete
> +parent.
> +
> +Part of making the "evolve" command work involves tracking the edits to a commit
> +over time, which is why we need an change graph. However, the change
> +graph will also bring other benefits:
> +
> +- Users can view the history of a change directly (the sequence of amends and
> +  rebases it has undergone, orthogonal to the history of the branch it is on).
> +- It will be possible to quickly locate and list all the changes the user
> +  currently has in progress.
> +- It can be used as part of other high-level commands that combine or split
> +  changes.
> +- It can be used to decorate commits (in git log, gitk, etc) that are either
> +  obsolete or are the tip of a work in progress.
> +- By pushing and pulling the change graph, users can collaborate more
> +  easily on changes-in-progress. This is better than pushing and pulling the
> +  commits themselves since the change graph can be used to locate a more
> +  specific merge base, allowing for better merges between different versions of
> +  the same change.
> +- It could be used to correctly rebase local changes and other local branches
> +  after running git-filter-branch.
> +- It can replace the change-id footer used by gerrit.

While the first part of the "Background" section is good (talking about a
user scenario), I don't think this description of 'git evolve' belongs here.
I'd expect the background to talk about what you wish Git could do (but it
hard/impossible to do now), or what prompted you to (re-)submit this
proposal. By prescribing the 'git evolve' command as the solution up-front,
this doc makes it difficult to interpret the underlying "why" of the
proposal.

> +
> +Goals
> +-----
> +Legend: Goals marked with P0 are required. Goals marked with Pn should be
> +attempted unless they interfere with goals marked with Pn-1.
> +
> +P0. All commands that modify commits (such as the normal commit --amend or
> +    rebase command) should mark the old commit as being obsolete and replaced by
> +    the new one. No additional commands should be required to keep the
> +    change graph up-to-date.

You elaborate on it more later, but this design is proposing that this
"change" workflow becomes the default for everyone, with a config option
('core.enableChanges') to opt out. This would be massively disruptive (and
confusing) to the huge swath of users that have no desire to use this
particular workflow.

> +P0. Any commit that may be involved in a future evolve command should not be
> +    garbage collected. Specifically:
> +    - Commits that obsolete another should not be garbage collected until
> +      user-specified conditions have occurred and the change has expired from
> +      the reflog. User specified conditions for removing changes include:
> +      - The user explicitly deleted the change.
> +      - The change was merged into a specific branch.
> +    - Commits that have been obsoleted by another should not be garbage
> +      collected if any of their replacements are still being retained.

If the creation of these linkages is passive, but requires active user
intervention to clean up, this requirement could result in creating an
enormous amount of cruft in repositories. I might rebase a branch 10+ times
between pushes to make little tweaks to phrasing in commit messages, or fix
typos, etc. It sounds like I'd be pushing an order of magnitude more objects
than I am now, let alone the fact that they wouldn't be GC'd automatically.

> +P0. A commit can be obsoleted by more than one replacement (called divergence).
> +P0. Users must be able to resolve divergence (convergence).
> +P1. Users should be able to share chains of obsolete changes in order to
> +    collaborate on WIP changes.
> +P2. Such sharing should be at the user’s option. That is, it should be possible
> +    to directly share a change without also sharing the file states or commit
> +    comments from the obsolete changes that led up to it, and the choice not to
> +    share those commits should not require changing any commit hashes.
> +P2. It should be possible to discard part or all of the change graph
> +    without discarding the commits themselves that are already present in
> +    branches and the reflog.
> +P2. Provide sufficient information to replace gerrit's Change-Id footers.

A general comment on the "Goals" section (similar to the one on "Background)
- all of the listed goals are written assuming you've already decided on the
solution, rather than elaborating on the problems you're trying to solve
(e.g., "I want to create a persistent linkage between iterations of a commit
and be able to query that linkage"). Talking about the features you want
(and why) will not only make it much easier to understand the proposal, it
could inspire other contributors to suggest solutions you may not have
considered.

> +
> +Similar technologies
> +--------------------
> +There are some other technologies that address the same end-user problem.
> +
> +Rebase -i can be used to solve the same problem, but users can't easily switch
> +tasks midway through an interactive rebase or have more than one interactive
> +rebase going on at the same time. It can't handle the case where you have
> +multiple changes sharing the same parent when that parent needs to be rebased
> +and won't let you collaborate with others on resolving a complicated interactive
> +rebase. You can think of rebase -i as a top-down approach and the evolve command
> +as the bottom-up approach to the same problem.

I think it's worth considering whether 'rebase' can be updated to handle
these cases (since it might simplify and/or pare down your proposed design).

1. Can't easily switch tasks midway through an interactive rebase
   - I could imagine us introducing a 'git rebase pause' that does this,
     although it would require changes to how rebases are tracked
     internally.
2. Can't have more than one interactive rebase going on at the same time
   - Do you mean nested rebases, or just separate ones? I think both of them
     could be possible (with the changes to rebase tracking in #1), but
     nested ones might be tough to mentally keep track of.
3. Can't handle multiple changes sharing the same parent when the parent
   needs to be rebased
   - Since the introduction of '--update-refs' [1], this is technically
     possible (although it needs a UI for the use case you mentioned).
4. Won't let you collaborate with others on resolving a complicated
   interactive rebase
   - This is an interesting one, since it requires being able to push a
     mid-merge state. However, if you're planning on solving that for 'git
     evolve', a similar solution could probably be used for 'rebase'.
     Pushing a whole rebase script, though, would be more complicated.

The "top-down"/"bottom-up" analogy is a bit lost on me, I'm afraid. Could
you clarify what you mean by that?

[1] https://lore.kernel.org/git/pull.1247.v5.git.1658255624.gitgitgadget@gmail.com/

> +Overview
> +========
> +We introduce the notion of “meta-commits” which describe how one commit was
> +created from other commits. A branch of meta-commits is known as a change.
> +Changes are created and updated automatically whenever a user runs a command
> +that creates a commit. They are used for locating obsolete commits, providing a
> +list of a user’s unsubmitted work in progress, and providing a stable name for
> +each unsubmitted change.

The term "change" is overly generic; I and others already use it as a
general term for anything from "part of a patch" to "an entire patch
series". Maybe "evolution" (in keeping with 'git evolve' being the command
that updates all of the meta-commit branches)? Or "iteration" (although that
might also be overloaded with how we colloquially refer to [PATCH vN]
submissions)?

Also, I'm not sure what you mean by "unsubmitted". Un-pushed? 

> +
> +Users can exchange edit histories by pushing and fetching changes.
> +
> +New commands will be introduced for manipulating changes and resolving
> +divergence between them. Existing commands that create commits will be updated
> +to modify the meta-commit graph and create changes where necessary.
> +
> +Example usage
> +-------------

nit: please include information about where HEAD starts (I think you start
on 'metas/some_change_already_merged_upstream'?).

> +# First create three dependent changes
> +$ echo foo>bar.txt && git add .
> +$ git commit -m "This is a test"
> +created change metas/this_is_a_test
> +$ echo foo2>bar2.txt && git add .
> +$ git commit -m "This is also a test"
> +created change metas/this_is_also_a_test
> +$ echo foo3>bar3.txt && git add .
> +$ git commit -m "More testing"
> +created change metas/more_testing
> +
> +# List all our changes in progress
> +$ git change list
> +metas/this_is_a_test
> +metas/this_is_also_a_test
> +* metas/more_testing
> +metas/some_change_already_merged_upstream

If this is a list of all of the unmerged commits/changes you have, it's
going to be exceptionally long for people with multiple local branches.
Unless the results of 'git change list' are scoped to those reachable from
the meta-commit pointing at HEAD?

> +
> +# Now modify the earliest change, using its stable name
> +$ git reset --hard metas/this_is_a_test
> +$ echo morefoo>>bar.txt && git add . && git commit --amend --no-edit
> +
> +# Use git-evolve to fix up any dependent changes
> +$ git evolve
> +rebasing metas/this_is_also_a_test onto metas/this_is_a_test
> +rebasing metas/more_testing onto metas/this_is_also_a_test
> +Done
> +
Where is HEAD at this point? Still 'metas/this_is_a_test'? 

> +# Use git-obslog to view the history of the this_is_a_test change
> +$ git log --obslog
> +93f110 metas/this_is_a_test@{0} commit (amend): This is a test
> +930219 metas/this_is_a_test@{1} commit: This is a test
> +
> +# Now create an unrelated change
> +$ git reset --hard origin/master
> +$ echo newchange>unrelated.txt && git add .
> +$ git commit -m "Unrelated change"
> +created change metas/unrelated_change
> +
> +# Fetch the latest code from origin/master and use git-evolve
> +# to rebase all dependent changes.
> +$ git fetch origin master
> +$ git evolve origin/master
> +deleting metas/some_change_already_merged_upstream
> +rebasing metas/this_is_a_test onto origin/master
> +rebasing metas/this_is_also_a_test onto metas/this_is_a_test
> +rebasing metas/more_testing onto metas/this_is_also_a_test
> +rebasing metas/unrelated_change onto origin/master
> +Conflict detected! Resolve it and then use git evolve --continue to resume.
> +
> +# Sort out the conflict
> +$ git mergetool
> +$ git evolve origin/master
> +Done

You ran 'git evolve origin/master', but the message said to use 'git evolve
--continue'. Is that a typo, or do they actually do something different
after resolving a conflict?

> +
> +# Share the full history of edits for the this_is_a_test change
> +# with a review server
> +$ git push origin metas/this_is_a_test:refs/for/master
> +# Share the lastest commit for “Unrelated change”, without history
> +$ git push origin HEAD:refs/for/master

It would be nice to also show here how a change is "finalized" (unlinked
from previous iterations, allowing them to be garbage collected). I think
you add this detail later in the doc, but it'd be nice to have up-front to
show the full end-to-end workflow.

> +
> +Detailed design
> +===============
> +Obsolescence information is stored as a graph of meta-commits. A meta-commit is
> +a specially-formatted merge commit that describes how one commit was created
> +from others.
> +
> +Meta-commits look like this:
> +
> +$ git cat-file -p <example_meta_commit>
> +tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904
> +parent aa7ce55545bf2c14bef48db91af1a74e2347539a
> +parent d64309ee51d0af12723b6cb027fc9f195b15a5e9
> +parent 7e1bbcd3a0fa854a7a9eac9bf1eea6465de98136
> +author Stefan Xenos <sxenos@gmail.com> 1540841596 -0700
> +committer Stefan Xenos <sxenos@gmail.com> 1540841596 -0700
> +parent-type c r o
> +
> +This says “commit aa7ce555 makes commit d64309ee obsolete. It was created by
> +cherry-picking commit 7e1bbcd3”.
> +
> +The tree for meta-commits is always the empty tree, but future versions of git
> +may attach other trees here. For forward-compatibility fsck should ignore such
> +trees if found on future repository versions. This will allow future versions of
> +git to add metadata to the meta-commit tree without breaking forwards
> +compatibility.
> +
> +The commit comment for a meta-commit is an auto-generated user-readable string
> +describing the command that produced the meta commit. These strings are shown
> +to the user when they view the obslog.
> +

The rest of this section provides (very detailed) descriptions of what you
want to do with each command. I appreciate the detail, but I wanted to focus
the review/discussion on the high-level approach before diving into
implementation details.

My overall take on this proposal is that, while (I think) I understand how
your proposed solution works, I'm unsure as to whether it's the best way to
solve the problems you're hoping to solve. Reworking the "Background" and
"Goals" sections to focus more on pain points/what you want to get out of
the new workflow would help substantially with figuring that out, so please
consider updating those in your next re-roll.

As far as the described approach, I have some concerns with both the
user-facing side of things and the internal architecture. In terms of
user impact:

- Regardless of the decided approach, I don't think this workflow should be
  made the default for all users; it's just too different from typical
  workflows that Git users follow.
- Even if users do "opt-in" to using this workflow, I think it'd be valuable
  to have an "iterate only when I ask you to" option for users that don't
  want every single fixup be saved as a persistent "version" of a change.
- The term "change" is already a loose/overloaded concept in the context of
  Git, I'd strongly suggest picking something else. Relatedly, a
  "terminology" subsection (for things like "meta-commit", "evolve", etc.)
  under the "Overview" section  would help a lot with reading through this.

The backend concerns are mostly related to massively increasing the number
of pushed objects and the reachability of those objects. An "iterate only
when I ask you to" approach would help with this, but I don't know whether
that fits your needs or not.

Thanks!
- Victoria

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v2 01/10] technical doc: add a design doc for the evolve command
  2022-10-10 19:35     ` Victoria Dye
@ 2022-10-11  8:59       ` Phillip Wood
  2022-10-11 16:59         ` Victoria Dye
  0 siblings, 1 reply; 66+ messages in thread
From: Phillip Wood @ 2022-10-11  8:59 UTC (permalink / raw)
  To: Victoria Dye, Stefan Xenos via GitGitGadget, git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Stefan Xenos

On 10/10/2022 20:35, Victoria Dye wrote:
> Stefan Xenos via GitGitGadget wrote:
>> From: Stefan Xenos <sxenos@google.com>
>>
>> This document describes what a change graph for
>> git would look like, the behavior of the evolve command,
>> and the changes planned for other commands.
>>
>> It was originally proposed in 2018, see
>> https://public-inbox.org/git/20181115005546.212538-1-sxenos@google.com/
>>
>> Signed-off-by: Stefan Xenos <sxenos@google.com>
>> Signed-off-by: Chris Poucet <poucet@google.com>
>> ---
>>   Documentation/technical/evolve.txt | 1070 ++++++++++++++++++++++++++++
>>   1 file changed, 1070 insertions(+)
>>   create mode 100644 Documentation/technical/evolve.txt
>>
>> diff --git a/Documentation/technical/evolve.txt b/Documentation/technical/evolve.txt
>> new file mode 100644
>> index 00000000000..2051ea77b8a
>> --- /dev/null
>> +++ b/Documentation/technical/evolve.txt

...

>> +P0. Any commit that may be involved in a future evolve command should not be
>> +    garbage collected. Specifically:
>> +    - Commits that obsolete another should not be garbage collected until
>> +      user-specified conditions have occurred and the change has expired from
>> +      the reflog. User specified conditions for removing changes include:
>> +      - The user explicitly deleted the change.
>> +      - The change was merged into a specific branch.
>> +    - Commits that have been obsoleted by another should not be garbage
>> +      collected if any of their replacements are still being retained.
> 
> If the creation of these linkages is passive, but requires active user
> intervention to clean up, this requirement could result in creating an
> enormous amount of cruft in repositories. I might rebase a branch 10+ times
> between pushes to make little tweaks to phrasing in commit messages, or fix
> typos, etc. It sounds like I'd be pushing an order of magnitude more objects
> than I am now, let alone the fact that they wouldn't be GC'd automatically.

That's an interesting point. When we push we only really need to push a 
map of "commits we pulled" to "commits we're pushing", we don't need to 
send all the intermediate changes. That would also help to address 
Glen's review club concerns about accidentally pushing secret information.

One of the things which I hope comes out of having all the intermediate 
changes tracked locally is a way to view the history of a particular 
commit. If I make a mistake when rebasing and don't notice it for a 
while it would be really helpful to be able to view the history and 
figure out which change introduced the mistake (You can do something 
similar with "git rev-list -g $branch | git log -p --stdin 
^${branch}@{upstream}" but you have to wade through all the commits on 
$branch).

...

>> +Similar technologies
>> +--------------------
>> +There are some other technologies that address the same end-user problem.
>> +
>> +Rebase -i can be used to solve the same problem, but users can't easily switch
>> +tasks midway through an interactive rebase or have more than one interactive
>> +rebase going on at the same time. It can't handle the case where you have
>> +multiple changes sharing the same parent when that parent needs to be rebased
>> +and won't let you collaborate with others on resolving a complicated interactive
>> +rebase. You can think of rebase -i as a top-down approach and the evolve command
>> +as the bottom-up approach to the same problem.
> 
> I think it's worth considering whether 'rebase' can be updated to handle
> these cases (since it might simplify and/or pare down your proposed design).
> 
> 1. Can't easily switch tasks midway through an interactive rebase
>     - I could imagine us introducing a 'git rebase pause' that does this,
>       although it would require changes to how rebases are tracked
>       internally.

I'm not sure how much of a problem this is in practice as one can use 
"git worktree add" to work on a different branch or is the idea to be 
able to start several rebases on the same branch? - That sounds like a 
recipe for conflicts that cannot be resolved automatically unless the 
user is very disciplined.

> 2. Can't have more than one interactive rebase going on at the same time
>     - Do you mean nested rebases, or just separate ones? I think both of them
>       could be possible (with the changes to rebase tracking in #1), but
>       nested ones might be tough to mentally keep track of.
> 3. Can't handle multiple changes sharing the same parent when the parent
>     needs to be rebased
>     - Since the introduction of '--update-refs' [1], this is technically
>       possible (although it needs a UI for the use case you mentioned).

'--update-refs' is more limited though I think. With evolve if I have

                   D (topic-2)
                  /
	A - B - C (topic-1)
                  \
                   E (topic-3)

then if I checkout topic-1 and amend one of the commits I can run "git 
evolve" to automatically rebase topic-2 & topic-3. One cannot do that 
with "rebase --update-refs". We could extend rebase (or have a new 
command) so that users can say "amend commit X and rebase all the 
branches that contain it".

> 4. Won't let you collaborate with others on resolving a complicated
>     interactive rebase
>     - This is an interesting one, since it requires being able to push a
>       mid-merge state. However, if you're planning on solving that for 'git
>       evolve', a similar solution could probably be used for 'rebase'.
>       Pushing a whole rebase script, though, would be more complicated.
> 
> The "top-down"/"bottom-up" analogy is a bit lost on me, I'm afraid. Could
> you clarify what you mean by that?

I was confused by that as well

> [1] https://lore.kernel.org/git/pull.1247.v5.git.1658255624.gitgitgadget@gmail.com/

Best Wishes

Phillip

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v2 01/10] technical doc: add a design doc for the evolve command
  2022-10-11  8:59       ` Phillip Wood
@ 2022-10-11 16:59         ` Victoria Dye
  2022-10-12 19:19           ` Phillip Wood
  0 siblings, 1 reply; 66+ messages in thread
From: Victoria Dye @ 2022-10-11 16:59 UTC (permalink / raw)
  To: phillip.wood, Stefan Xenos via GitGitGadget, git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Stefan Xenos

Phillip Wood wrote:
> On 10/10/2022 20:35, Victoria Dye wrote:
>> Stefan Xenos via GitGitGadget wrote:
>> 3. Can't handle multiple changes sharing the same parent when the parent
>>     needs to be rebased
>>     - Since the introduction of '--update-refs' [1], this is technically
>>       possible (although it needs a UI for the use case you mentioned).
> 
> '--update-refs' is more limited though I think. With evolve if I have
> 
>                   D (topic-2)
>                  /
>     A - B - C (topic-1)
>                  \
>                   E (topic-3)
> 
> then if I checkout topic-1 and amend one of the commits I can run "git
> evolve" to automatically rebase topic-2 & topic-3. One cannot do that with
> "rebase --update-refs". We could extend rebase (or have a new command) so
> that users can say "amend commit X and rebase all the branches that contain
> it".

Sorry, let me clarify what I mean. The 'update-ref' command in a
'rebase-todo' script (not the '--update-refs' option) can be used to create
a rebase script that does what's described in your example:

  label onto # A

  reset onto
  pick 1342ab B
  fixup 8a7f3e fixup! B
  label branch-point-1

  pick 90d7fc C
  label topic-1
  update-ref refs/heads/topic-1

  reset branch-point-1
  pick 42f92b D
  label topic-2
  update-ref refs/heads/topic-2

  reset branch-point-1
  pick 06d8ec E
  label topic-3
  update-ref refs/heads/topic-3

So, while it'd need a less manual UI (e.g., a 'rebase --evolve' option) to
generate that script, the 'update-ref' command makes this functionality
possible in a rebase.

^ permalink raw reply	[flat|nested] 66+ messages in thread

* Re: [PATCH v2 01/10] technical doc: add a design doc for the evolve command
  2022-10-11 16:59         ` Victoria Dye
@ 2022-10-12 19:19           ` Phillip Wood
  0 siblings, 0 replies; 66+ messages in thread
From: Phillip Wood @ 2022-10-12 19:19 UTC (permalink / raw)
  To: Victoria Dye, Stefan Xenos via GitGitGadget, git
  Cc: Jerry Zhang, Phillip Wood, Ævar Arnfjörð Bjarmason,
	Chris Poucet, Christophe Poucet, Stefan Xenos

Hi Victoria

On 11/10/2022 17:59, Victoria Dye wrote:
> Phillip Wood wrote:
>> On 10/10/2022 20:35, Victoria Dye wrote:
>>> Stefan Xenos via GitGitGadget wrote:
>>> 3. Can't handle multiple changes sharing the same parent when the parent
>>>      needs to be rebased
>>>      - Since the introduction of '--update-refs' [1], this is technically
>>>        possible (although it needs a UI for the use case you mentioned).
>>
>> '--update-refs' is more limited though I think. With evolve if I have
>>
>>                    D (topic-2)
>>                   /
>>      A - B - C (topic-1)
>>                   \
>>                    E (topic-3)
>>
>> then if I checkout topic-1 and amend one of the commits I can run "git
>> evolve" to automatically rebase topic-2 & topic-3. One cannot do that with
>> "rebase --update-refs". We could extend rebase (or have a new command) so
>> that users can say "amend commit X and rebase all the branches that contain
>> it".
> 
> Sorry, let me clarify what I mean. The 'update-ref' command in a
> 'rebase-todo' script (not the '--update-refs' option) can be used to create
> a rebase script that does what's described in your example:
> 
>    label onto # A
> 
>    reset onto
>    pick 1342ab B
>    fixup 8a7f3e fixup! B
>    label branch-point-1
> 
>    pick 90d7fc C
>    label topic-1
>    update-ref refs/heads/topic-1
> 
>    reset branch-point-1
>    pick 42f92b D
>    label topic-2
>    update-ref refs/heads/topic-2
> 
>    reset branch-point-1
>    pick 06d8ec E
>    label topic-3
>    update-ref refs/heads/topic-3
> 
> So, while it'd need a less manual UI (e.g., a 'rebase --evolve' option) to
> generate that script, the 'update-ref' command makes this functionality
> possible in a rebase.

Ah, I'd misunderstood what you meant, that makes sense - thanks for 
clarifying.

Best Wishes

Phillip

^ permalink raw reply	[flat|nested] 66+ messages in thread

end of thread, other threads:[~2022-10-12 19:19 UTC | newest]

Thread overview: 66+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-23 18:55 [PATCH 00/10] Add the Git Change command Christophe Poucet via GitGitGadget
2022-09-23 18:55 ` [PATCH 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
2022-09-23 19:59   ` Jerry Zhang
2022-09-28 21:26   ` Junio C Hamano
2022-09-28 22:20   ` Junio C Hamano
2022-09-29  9:17     ` Phillip Wood
2022-09-29 19:57   ` Jonathan Tan
2022-09-23 18:55 ` [PATCH 02/10] sha1-array: implement oid_array_readonly_contains Chris Poucet via GitGitGadget
2022-09-26 13:08   ` Phillip Wood
2022-09-23 18:55 ` [PATCH 03/10] ref-filter: add the metas namespace to ref-filter Chris Poucet via GitGitGadget
2022-09-26 13:13   ` Phillip Wood
2022-10-04  9:50     ` Chris P
2022-09-23 18:55 ` [PATCH 04/10] evolve: add support for parsing metacommits Stefan Xenos via GitGitGadget
2022-09-26 13:27   ` Phillip Wood
2022-10-04 11:21     ` Chris P
2022-10-04 14:10       ` Phillip Wood
2022-09-23 18:55 ` [PATCH 05/10] evolve: add the change-table structure Stefan Xenos via GitGitGadget
2022-09-27 13:27   ` Phillip Wood
2022-09-27 13:50     ` Ævar Arnfjörð Bjarmason
2022-09-27 14:13       ` Phillip Wood
2022-09-27 15:28         ` Ævar Arnfjörð Bjarmason
2022-09-28 14:33           ` Phillip Wood
2022-09-28 15:14             ` Ævar Arnfjörð Bjarmason
2022-09-28 15:59             ` Junio C Hamano
2022-09-27 14:18     ` Phillip Wood
2022-10-04 14:48     ` Chris P
2022-09-23 18:55 ` [PATCH 06/10] evolve: add support for writing metacommits Stefan Xenos via GitGitGadget
2022-09-28 14:27   ` Phillip Wood
2022-10-05  9:40     ` Chris P
2022-10-05 11:09       ` Phillip Wood
2022-09-23 18:55 ` [PATCH 07/10] evolve: implement the git change command Stefan Xenos via GitGitGadget
2022-09-25  9:10   ` Phillip Wood
2022-09-26  8:23     ` Ævar Arnfjörð Bjarmason
2022-09-26  8:25   ` Ævar Arnfjörð Bjarmason
2022-10-05 12:30     ` Chris P
2022-09-23 18:55 ` [PATCH 08/10] evolve: add the git change list command Stefan Xenos via GitGitGadget
2022-09-23 18:55 ` [PATCH 09/10] evolve: add delete command Chris Poucet via GitGitGadget
2022-09-26  8:38   ` Ævar Arnfjörð Bjarmason
2022-09-26  9:10     ` Chris Poucet
2022-09-23 18:55 ` [PATCH 10/10] evolve: add documentation for `git change` Chris Poucet via GitGitGadget
2022-09-25  8:41   ` Phillip Wood
2022-09-25  8:39 ` [PATCH 00/10] Add the Git Change command Phillip Wood
2022-10-04  9:33   ` Chris P
2022-10-04 14:24 ` Phillip Wood
2022-10-04 15:19   ` Chris P
2022-10-04 15:55     ` Chris P
2022-10-04 16:00       ` Phillip Wood
2022-10-04 15:57     ` Phillip Wood
2022-10-05 14:59 ` [PATCH v2 00/10] RFC: Git Evolve / Change Christophe Poucet via GitGitGadget
2022-10-05 14:59   ` [PATCH v2 01/10] technical doc: add a design doc for the evolve command Stefan Xenos via GitGitGadget
2022-10-05 15:16     ` Chris Poucet
2022-10-06 20:53       ` Glen Choo
2022-10-10 19:35     ` Victoria Dye
2022-10-11  8:59       ` Phillip Wood
2022-10-11 16:59         ` Victoria Dye
2022-10-12 19:19           ` Phillip Wood
2022-10-05 14:59   ` [PATCH v2 02/10] sha1-array: implement oid_array_readonly_contains Chris Poucet via GitGitGadget
2022-10-05 14:59   ` [PATCH v2 03/10] ref-filter: add the metas namespace to ref-filter Chris Poucet via GitGitGadget
2022-10-05 14:59   ` [PATCH v2 04/10] evolve: add support for parsing metacommits Stefan Xenos via GitGitGadget
2022-10-05 14:59   ` [PATCH v2 05/10] evolve: add the change-table structure Stefan Xenos via GitGitGadget
2022-10-05 14:59   ` [PATCH v2 06/10] evolve: add support for writing metacommits Stefan Xenos via GitGitGadget
2022-10-05 14:59   ` [PATCH v2 07/10] evolve: implement the git change command Stefan Xenos via GitGitGadget
2022-10-05 14:59   ` [PATCH v2 08/10] evolve: add delete command Chris Poucet via GitGitGadget
2022-10-05 14:59   ` [PATCH v2 09/10] evolve: add documentation for `git change` Chris Poucet via GitGitGadget
2022-10-05 14:59   ` [PATCH v2 10/10] evolve: add tests for the git-change command Chris Poucet via GitGitGadget
2022-10-10  9:23   ` [PATCH v2 00/10] RFC: Git Evolve / Change Phillip Wood

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).