From patchwork Sun Jan 27 19:44:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Stefan Xenos X-Patchwork-Id: 10783017 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 7D60191E for ; Sun, 27 Jan 2019 19:44:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6F4BC29351 for ; Sun, 27 Jan 2019 19:44:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 634ED2A671; Sun, 27 Jan 2019 19:44:25 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 64D5429351 for ; Sun, 27 Jan 2019 19:44:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726878AbfA0ToV (ORCPT ); Sun, 27 Jan 2019 14:44:21 -0500 Received: from mail-it1-f201.google.com ([209.85.166.201]:44148 "EHLO mail-it1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726794AbfA0ToU (ORCPT ); Sun, 27 Jan 2019 14:44:20 -0500 Received: by mail-it1-f201.google.com with SMTP id x82so10685468ita.9 for ; Sun, 27 Jan 2019 11:44:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc :content-transfer-encoding; bh=bTsyWxTGmxhjRmFi7Ad9s71mORqPQ2y8n1hCB5N7FDc=; b=isAbCZLCXDZSEg6uzylQ8ldywFuxvDYzeBoYBGI+JNcgEZsgUuKQKACm85sMvTtcv5 dEdOoYiK4nALecTNZ3Ex86AJS2eGr2gWw9SqaOJGMZ+ddlgo2De17x4P5nA71gb8HuOJ 65PZdKbrMlYiML9q4+YciJFTK0gQwyjeBb1uaq1sUXdWJrSjhTvs8OFF32gHGp6LngUl oHKVFFk5TczdRMYI3BHlNeLB4PCcNbNR0rUklKEgEEA5Q++AyeaSu9F7IchjkWH7t4F6 EkNV+4x2w0sPYZRvJEcOvTu7TNDJEfhg6RfWqvoluIblSKuYQYY1WGT9FyV9UXakDsAK BFsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc :content-transfer-encoding; bh=bTsyWxTGmxhjRmFi7Ad9s71mORqPQ2y8n1hCB5N7FDc=; b=iG3Xa2w/TMKrP1KFLMYKrzosPtJ4UQtDGzaOv+lHdPIGry2NykqtOzAdUWJuV3HgxE Xd11duxKL0rhaB9IlxZfXnmXClT+5J4H5lVe0oDAe8Ltm/DcP8jMexAwFgklN2p9sPlS hkKVqyJOp2KXzLjg2rYkGBlpBy4nA/i1GpHhZi+Nw91oRW3a3MuA5urzNdKw8qZqq98T rjVVU6drH1/5pX7vnLf1EvHKutX1rWCeVIssvZf32H3eF4Z+lso37RSeSFiC9J43mKGo +d7EdvPx6SbvesZeHdOVTPlSARHcJJ9Du1pwMNSiZLxaadL5koHMBdgKbuBE3S0yU2Sk VHHw== X-Gm-Message-State: AJcUukccI4rlLdrXflaGtz2PJAShfq4+i7SAkgVUNjks8tZ53ZJp4vWB m5aIG00jEvls9ZKqxQY5JJQocU77LdEgS6dhlrPe2P8uGpw4avZJmzhyzRyfrDTejnMnjB35HJT aNh0+q6vcYc9WPyRh79VqAx7ZzmGpwEX/dP2iO81CAr+BmZ7jzEcVEtCYKw== X-Google-Smtp-Source: ALg8bN4ST2vYQD0hfSbKi54+PoC986Kt1OKA25dDiXiKJQqwM1RYAItDWmxj3q4k3JJj7/w4foQ0yWgKAhM= X-Received: by 2002:a05:660c:4e:: with SMTP id p14mr11848074itk.31.1548618258506; Sun, 27 Jan 2019 11:44:18 -0800 (PST) Date: Sun, 27 Jan 2019 11:44:08 -0800 Message-Id: <20190127194415.171035-1-sxenos@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.20.1.495.gaa96b0ce6b-goog Subject: [PATCH v3 1/8] technical doc: add a design doc for the evolve command From: sxenos@google.com To: git@vger.kernel.org Cc: Stefan Xenos Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Stefan Xenos 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. Signed-off-by: Stefan Xenos --- Documentation/technical/evolve.txt | 1034 ++++++++++++++++++++++++++++ 1 file changed, 1034 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 0000000000..7967c73e5d --- /dev/null +++ b/Documentation/technical/evolve.txt @@ -0,0 +1,1034 @@ +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 + changes 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. 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 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 --continue +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 +tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904 +parent aa7ce55545bf2c14bef48db91af1a74e2347539a +parent d64309ee51d0af12723b6cb027fc9f195b15a5e9 +parent 7e1bbcd3a0fa854a7a9eac9bf1eea6465de98136 +author Stefan Xenos 1540841596 -0700 +committer Stefan Xenos 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 whose hash matches +4b825dc642cb6eb9a060e54bf8d69288fbee4904 exactly, but future versions of git may +attach other trees here. For forward-compatibility fsck should ignore such trees +if found on future repository versions. Similarly, current versions of git +should always fill in an empty commit comment and tools like fsck should ignore +the content of the commit comment if present in a future repository version. +This will allow future versions of git to add metadata to the meta-commit +comments or tree without breaking forwards compatibility. + +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 this parent was generated from the given commit. +- 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. 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//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 exeption 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 “obsolete” +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 an obsolete 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. + +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 obsolete 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 [--abort][--continue][--quit] [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, a merge conflict is discovered, or a divergent parent is +discovered. + +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. + +The --abort option returns all changes to the state they were in prior to +invoking evolve, and the --quit option terminates the current evolution without +changing the current state. + +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 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 "replace" command records a replacement in the obsolescence graph, given a +list of obsolete commits or metacommits followed by their replacement. This +behaves like a normal "modify" command, except that the replacement is an +existing commit. If an obsolete commit points to a metacommit, only a change +branch pointing to exactly that metacommit moves forward. If an obsolete commit +points to a normal commit, all change branches pointing to that commit move +forward. If no change branches moved forward, a new change branch is created +using the default name. + +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, commits behind ”. + +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..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. + +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, and 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/ + +pointing to commit as an indication that B is the replacement for the +obsolete commit A. + +Pros: +- Protects from being garbage collected. +- Fast lookup for the evolve operation, without additional search structures + (“what is the replacement for ?” 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//obs_ + +Pointing to commit 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 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/ + +Cons: +- Doesn’t provide a way to resolve divergence (which is a P0 requirement). +- Doesn’t protect 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 create and update change branches in refs/metas them at the same time we +would 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. From patchwork Sun Jan 27 19:44:09 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Xenos X-Patchwork-Id: 10783015 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6AE4191E for ; Sun, 27 Jan 2019 19:44:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5050B2A5DB for ; Sun, 27 Jan 2019 19:44:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3E29E2A6DC; Sun, 27 Jan 2019 19:44:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 205F12A5DB for ; Sun, 27 Jan 2019 19:44:23 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726920AbfA0ToW (ORCPT ); Sun, 27 Jan 2019 14:44:22 -0500 Received: from mail-it1-f202.google.com ([209.85.166.202]:37989 "EHLO mail-it1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726794AbfA0ToV (ORCPT ); Sun, 27 Jan 2019 14:44:21 -0500 Received: by mail-it1-f202.google.com with SMTP id m128so10716514itd.3 for ; Sun, 27 Jan 2019 11:44:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=vkxspNQERKQtD2zbfXs9u9ZH5PO+G1KSGyGwY6+Rpn8=; b=W7Ltftzm2WiLwZT4/VybnDpx9WYilg2gAMLDLHo48lNB4rC3QgdpLVcGeO+jIj6ygO R8BWlJJwQaUEURTAyT4rkXO/sWia2ldh+VhNURIOa5HJJjxpob+73Oo6uE8AonzxOHYy TO4SImi73MQmbNzzbIWKJTK3mbFOUIG5xVB8OmdxWFXCTS5T8hHkIXto5g4rhipBkYd8 v/DmnR1HpqZrZwFI2Y94HqFVw/Atd7qQRKkVSYrlH8/23vweYmoWNOvoLIZss3xdh272 SdbevdNeOaeZhegRCNSXeH0n9/ydU7p1JNK0xAmCFw+ma+eSsp3xAW2EPfq09K6kDu2b RM8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=vkxspNQERKQtD2zbfXs9u9ZH5PO+G1KSGyGwY6+Rpn8=; b=asL+t0sGbbPA7P6uzLKh6ats10MhbaIQknH3G3PS03yKadifNdUyLmBx9euloV0yiZ c/Uw/qkWJ5ZW5QPXlx9uB874d4pHvvfSxLMPZT9ELeeQI/4yyGe1cct8fu+Fd5Wp38Vb j9rUI6qiX9jwSWvyCj0VFrY8ok2BqmVQqlMFIC/aJTmC2OMo8LrhaNoH7kTWGedK4/NC x8qSC80ZyicbH6GH8EqKHDbAbeBpcqe9MJnjJNYJGB4ASMbyFetT2hOAwrw6YxjWwcpz XhMoSdjeL1Av3LtXL2aqi/eMLopbSsysbkcsqZBS8Td8S0vmk5OOIO3CwnCrW48xYlFA o7bg== X-Gm-Message-State: AJcUukcPOtN66G+yiYgclzamTphgy0A1Ao+YYgaZr1c+z8KI9+ZQ1lAo I6hQ5tNCTjtOBVRnEyOsqyiSZVAdOmh26q/zSzcCN+zHtmSaQaooera/m23K9qsHxZj22hp9mOe ggjTLYuJjoeAWjrabhcVWMKx7BncGc9cbP3vW8iv8ckVhv9JgnEp5oVbjWw== X-Google-Smtp-Source: ALg8bN7kew+dlymTL2qgDbychMXBJfMUvJ0/j2MC5VUH2cL/IDLQofckVd4TAGc9gf+biDBLe1CfyLzjiC4= X-Received: by 2002:a24:5311:: with SMTP id n17mr11916576itb.39.1548618260832; Sun, 27 Jan 2019 11:44:20 -0800 (PST) Date: Sun, 27 Jan 2019 11:44:09 -0800 In-Reply-To: <20190127194415.171035-1-sxenos@google.com> Message-Id: <20190127194415.171035-2-sxenos@google.com> Mime-Version: 1.0 References: <20190127194415.171035-1-sxenos@google.com> X-Mailer: git-send-email 2.20.1.495.gaa96b0ce6b-goog Subject: [PATCH v3 2/8] sha1-array: Implement oid_array_readonly_contains From: sxenos@google.com To: git@vger.kernel.org Cc: Stefan Xenos , Stefan Xenos Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Stefan Xenos 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: Stefan Xenos --- sha1-array.c | 15 +++++++++++++++ sha1-array.h | 2 ++ t/helper/test-sha1-array.c | 6 ++++++ t/t0064-sha1-array.sh | 22 ++++++++++++++++++++++ 4 files changed, 45 insertions(+) diff --git a/sha1-array.c b/sha1-array.c index b94e0ec0f5..071fce7e90 100644 --- a/sha1-array.c +++ b/sha1-array.c @@ -26,6 +26,21 @@ static const unsigned char *sha1_access(size_t index, void *table) return array[index].hash; } +int oid_array_readonly_contains(const struct oid_array* array, + const struct object_id* oid) +{ + int i; + if (array->sorted) { + return sha1_pos(oid->hash, array->oid, array->nr, sha1_access) >= 0; + } + for (i = 0; i < array->nr; i++) { + if (hashcmp(array->oid[i].hash, oid->hash) == 0) { + return 1; + } + } + return 0; +} + int oid_array_lookup(struct oid_array *array, const struct object_id *oid) { if (!array->sorted) diff --git a/sha1-array.h b/sha1-array.h index 232bf95017..7273bd5151 100644 --- a/sha1-array.h +++ b/sha1-array.h @@ -13,6 +13,8 @@ struct oid_array { void oid_array_append(struct oid_array *array, const struct object_id *oid); int oid_array_lookup(struct oid_array *array, const struct object_id *oid); void oid_array_clear(struct oid_array *array); +int oid_array_readonly_contains(const struct oid_array* array, + const struct object_id* oid); typedef int (*for_each_oid_fn)(const struct object_id *oid, void *data); diff --git a/t/helper/test-sha1-array.c b/t/helper/test-sha1-array.c index ad5e69f9d3..fefb1c984f 100644 --- a/t/helper/test-sha1-array.c +++ b/t/helper/test-sha1-array.c @@ -25,10 +25,16 @@ int cmd__sha1_array(int argc, const char **argv) if (get_oid_hex(arg, &oid)) die("not a hexadecimal SHA1: %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 SHA1: %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-sha1-array.sh b/t/t0064-sha1-array.sh index 5dda570b9a..c1bac6fcdd 100755 --- a/t/t0064-sha1-array.sh +++ b/t/t0064-sha1-array.sh @@ -32,6 +32,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 sha1-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 sha1-array >actual && + test_cmp expect actual +' + test_expect_success 'lookup' ' { echoid append 88 44 aa 55 && From patchwork Sun Jan 27 19:44:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Xenos X-Patchwork-Id: 10783019 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 86F8191E for ; Sun, 27 Jan 2019 19:44:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7B8C429351 for ; Sun, 27 Jan 2019 19:44:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 701F12A671; Sun, 27 Jan 2019 19:44:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 18D4329351 for ; Sun, 27 Jan 2019 19:44:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726960AbfA0ToZ (ORCPT ); Sun, 27 Jan 2019 14:44:25 -0500 Received: from mail-yw1-f74.google.com ([209.85.161.74]:37254 "EHLO mail-yw1-f74.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726794AbfA0ToY (ORCPT ); Sun, 27 Jan 2019 14:44:24 -0500 Received: by mail-yw1-f74.google.com with SMTP id t192so8563410ywe.4 for ; Sun, 27 Jan 2019 11:44:23 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=W7uFzqyjRWxwkCF3xj4LWTSLlruZYya9JwjsYbq/8mE=; b=V5vXOxS9XpfCpIWVeFxgCRdZoKLAYbS/D//tHEfYD/oom8ZOuTN7opYSBxK+wlQ5wC BCV36zheyGq7x+f1H0zaEn95hVKcMPEw+Odolesb7NDx2K8GnwNZuqb4I3oqfgRWqUVc w7vLLpbtXL7gIHhFIjd7DN4Qp0bX2YcF4+f4UThFzIum4L1v4ufbyhlttdDmNvi1FX19 6WxdN7zR7rE915KSqPquF8VsVSDGpFKIdkQywLKYUH7VShqEmoAST7waB9lu+nGS5Zo4 Wr5jOd/0UOTWxwLEtqE5/OCDcw5JIferxh8MnoyWCpRAvouRMz3FECtdqhPi0iCFPJOK O+Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=W7uFzqyjRWxwkCF3xj4LWTSLlruZYya9JwjsYbq/8mE=; b=QXQxnLEgHffI5o09kzUhmZBzZaAFEpV+CYk4/5LfcCV39L5C1WzRCtt6P4BxAtscGb 6AVV8EUaD2rULcAzQSJtfYbuPYXftUZ935/HZyZT26RPKY2nb2Y9RKD0ne+2I3tROGf1 GlA4EqNyjP8MHHasbwq4cRmoMNh9i3Rx0UTTM/cFNwi7ZJ9Vb5keSBlbAusSU/mmkoU2 AS//vTlKoIkQFAuqT4b3G+eaw9Vx9Znsag11hQF8Gg30CJTNdQEaYf4nGtKBVH83O+v5 PjYWXlG4CHtRqXSGOA4Q1jg54SQG6kIMtV6KpXP5B0TrhXpMJsdlKEKvC2wr6/qf1PlJ ePSQ== X-Gm-Message-State: AJcUukf9CEpm6XDlNW45eXiK2P9rqVA7WmK5LJvHYc3Yn4XuHWS0dCBm lgXhQEA53unG6Og5cHwJiEfw0HtVAAt75P6FQJW3mV4OpGtYWrgRAMVQkrLOpLae7LqTbirFk9C jRBMYUevgrtI+uS3ef1+9Oy/23Avrh97Nfh89DobQnYlNwaKIKQVVpm4aqg== X-Google-Smtp-Source: ALg8bN7b7I4LBhnwPFgI4Oq4HewL14kTA1d1U7ggVzja9pIuiy5dxgGEvOg38Y9EPIuGQRhH5rvOe6CwSA8= X-Received: by 2002:a25:818b:: with SMTP id p11mr8991536ybk.51.1548618263408; Sun, 27 Jan 2019 11:44:23 -0800 (PST) Date: Sun, 27 Jan 2019 11:44:10 -0800 In-Reply-To: <20190127194415.171035-1-sxenos@google.com> Message-Id: <20190127194415.171035-3-sxenos@google.com> Mime-Version: 1.0 References: <20190127194415.171035-1-sxenos@google.com> X-Mailer: git-send-email 2.20.1.495.gaa96b0ce6b-goog Subject: [PATCH v3 3/8] ref-filter: Add the metas namespace to ref-filter From: sxenos@google.com To: git@vger.kernel.org Cc: Stefan Xenos Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Stefan Xenos The metas namespace will contain refs for changes in progress. Add support for searching this namespace. Signed-off-by: Stefan Xenos --- ref-filter.c | 8 ++++++-- ref-filter.h | 5 +++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/ref-filter.c b/ref-filter.c index 422a9c9ae3..4d7bd06880 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -1925,7 +1925,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")) @@ -1943,7 +1944,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); } @@ -2128,6 +2130,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, broken); else if (filter->kind == FILTER_REFS_TAGS) ret = for_each_fullref_in("refs/tags/", ref_filter_handler, &ref_cbdata, broken); + else if (filter->kind == FILTER_REFS_CHANGES) + ret = for_each_fullref_in("refs/metas/", ref_filter_handler, &ref_cbdata, broken); else if (filter->kind & FILTER_REFS_ALL) ret = for_each_fullref_in_pattern(filter, ref_filter_handler, &ref_cbdata, broken); if (!ret && (filter->kind & FILTER_REFS_DETACHED_HEAD)) diff --git a/ref-filter.h b/ref-filter.h index 85c8ebc3b9..19a3e57845 100644 --- a/ref-filter.h +++ b/ref-filter.h @@ -18,9 +18,10 @@ #define FILTER_REFS_BRANCHES 0x0004 #define FILTER_REFS_REMOTES 0x0008 #define FILTER_REFS_OTHERS 0x0010 -#define FILTER_REFS_ALL (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \ - FILTER_REFS_REMOTES | FILTER_REFS_OTHERS) #define FILTER_REFS_DETACHED_HEAD 0x0020 +#define FILTER_REFS_CHANGES 0X0040 +#define FILTER_REFS_ALL (FILTER_REFS_TAGS | FILTER_REFS_BRANCHES | \ + FILTER_REFS_REMOTES | FILTER_REFS_CHANGES | FILTER_REFS_OTHERS) #define FILTER_REFS_KIND_MASK (FILTER_REFS_ALL | FILTER_REFS_DETACHED_HEAD) struct atom_value; From patchwork Sun Jan 27 19:44:11 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Xenos X-Patchwork-Id: 10783021 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 04C8691E for ; Sun, 27 Jan 2019 19:44:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id ECD1629351 for ; Sun, 27 Jan 2019 19:44:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E161F2A671; Sun, 27 Jan 2019 19:44:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7AB7729351 for ; Sun, 27 Jan 2019 19:44:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726984AbfA0To1 (ORCPT ); Sun, 27 Jan 2019 14:44:27 -0500 Received: from mail-yb1-f201.google.com ([209.85.219.201]:35059 "EHLO mail-yb1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726794AbfA0To1 (ORCPT ); Sun, 27 Jan 2019 14:44:27 -0500 Received: by mail-yb1-f201.google.com with SMTP id g14so692996ybd.2 for ; Sun, 27 Jan 2019 11:44:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=zEDGAhBnNEmeIfIQLormecozVD4IiiP9quqwE/o+Csw=; b=e5MpS0WtvnDPiaZd0O6J7eg6JRzOHVKhUAZV7g/lJnMcbLkv2nmEE92fdfg4QVmVgq LjUNp+BQW01O3mPBfxmKjtqYPpl6aMq86HWTGAfyd7f5syj2IpLAaTq70P31SzIRoYGB wZ3+QSC5uwh/emNc5n1ySWjJCmDrMVz0caykX/Ve882MoDy4jTXVYm+B6DjQGcfiVddd leZQUbblBl+Oc/hIt3RsNNMmrNXxdQLEqhmlxJgm9AI533OgEvGtEOdC4EhH9uPTzytJ oLlRAa7iHyaeCU2IGiTOlPQuvuugiLimiSaF/Ul9+OXBYIvn/YqTlFU7qZzrr0DWk6Uj 4frA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=zEDGAhBnNEmeIfIQLormecozVD4IiiP9quqwE/o+Csw=; b=fNIFU+PsgppnuiUgvCCiTwySmo6C8HQdXkAWPFWn0L/UcM2Xa9vWzszfapkh/HygRP RPoxqYK2aXL4nWJ1WtdST/ttI8J837A+vey1bp57n0vjWNaiJK5CtKi1Nz4wGcFqE3wu 6w8fMHvy4Ha3Uz/oN4Pq7WuAmiVPFz+obmTkK76/umI0/8MVITtE63uq2QWeHTt0bB4l NyNI231BhpZFJaZ5/tpb02K4F0S2LiTmTLhe7XsupqXXFLesu2WzH9b67dKe7C2qXPR+ eQg/SvWae9fgL8fJZA4XkP9FaU/Y6+0w9K1w77sWZq2ti2K+E7lbdr51QL30CsINwq6R AwSg== X-Gm-Message-State: AJcUukckP823oZB3z4L8frk5qVolPMixncMgN9cKXSsu+NyrlFry3ygk qaktpVvofZTKHY14ahYpGYi65pe1uSiK4pHqYqmqa9pI91hZUE39PbC/YjySVqvgNTpZLOTbFcq vOpmALso2QjiuPbL+Ja/jWSPevyBqL+5LaoL7HLkTmLYl9g9G1fxvbxE/gQ== X-Google-Smtp-Source: ALg8bN4xXZg5prMFa/NS6SCESOdXH2BqxIQxkFhLdbfO8iAucvHMz3EZvAaaFGxK3WK/Mzx07Gl5s3cIrwk= X-Received: by 2002:a25:7355:: with SMTP id o82mr4948004ybc.84.1548618265777; Sun, 27 Jan 2019 11:44:25 -0800 (PST) Date: Sun, 27 Jan 2019 11:44:11 -0800 In-Reply-To: <20190127194415.171035-1-sxenos@google.com> Message-Id: <20190127194415.171035-4-sxenos@google.com> Mime-Version: 1.0 References: <20190127194415.171035-1-sxenos@google.com> X-Mailer: git-send-email 2.20.1.495.gaa96b0ce6b-goog Subject: [PATCH v3 4/8] evolve: Add support for parsing metacommits From: sxenos@google.com To: git@vger.kernel.org Cc: Stefan Xenos Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Stefan Xenos 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 --- Makefile | 1 + metacommit-parser.c | 87 +++++++++++++++++++++++++++++++++++++++++++++ metacommit-parser.h | 16 +++++++++ 3 files changed, 104 insertions(+) create mode 100644 metacommit-parser.c create mode 100644 metacommit-parser.h diff --git a/Makefile b/Makefile index 1a44c811aa..7ffc383f2b 100644 --- a/Makefile +++ b/Makefile @@ -919,6 +919,7 @@ LIB_OBJS += merge.o LIB_OBJS += merge-blobs.o LIB_OBJS += merge-recursive.o LIB_OBJS += mergesort.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 0000000000..5013a108a3 --- /dev/null +++ b/metacommit-parser.c @@ -0,0 +1,87 @@ +#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 && + !strncmp(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; + } + + return to_search->item; +} + +/* + * 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); + size_t parent_types_size; + const char *parent_types = find_key(buffer, "parent-type", + &parent_types_size); + const char *end; + int index = 0; + int ret; + struct commit *content_parent; + + if (!parent_types) { + return METACOMMIT_TYPE_NONE; + } + + end = &(parent_types[parent_types_size]); + + while (1) { + char next = *parent_types; + if (next == ' ') { + index++; + } + if (next == 'c') { + ret = METACOMMIT_TYPE_NORMAL; + break; + } + if (next == 'a') { + ret = METACOMMIT_TYPE_ABANDONED; + break; + } + parent_types++; + if (parent_types >= end) { + return METACOMMIT_TYPE_NONE; + } + } + + 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 0000000000..e546f5a7e7 --- /dev/null +++ b/metacommit-parser.h @@ -0,0 +1,16 @@ +#ifndef METACOMMIT_PARSER_H +#define METACOMMIT_PARSER_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 From patchwork Sun Jan 27 19:44:12 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Xenos X-Patchwork-Id: 10783023 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 416551390 for ; Sun, 27 Jan 2019 19:44:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3286A29351 for ; Sun, 27 Jan 2019 19:44:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 26DA52A671; Sun, 27 Jan 2019 19:44:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4F00C29351 for ; Sun, 27 Jan 2019 19:44:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727016AbfA0Toa (ORCPT ); Sun, 27 Jan 2019 14:44:30 -0500 Received: from mail-yw1-f73.google.com ([209.85.161.73]:40200 "EHLO mail-yw1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726794AbfA0To3 (ORCPT ); Sun, 27 Jan 2019 14:44:29 -0500 Received: by mail-yw1-f73.google.com with SMTP id l69so8398796ywb.7 for ; Sun, 27 Jan 2019 11:44:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=7cMyXIzIL94D6VTnKdFBV3rrJTxL/qORHkyq0bTsvjU=; b=EhCKDk8KR0nXud2Hklm2CXxcBeP6Kpog1pl6M0z8qJFeWh9MAQSO7N8i2f5OQlF16z RYEqKpAjRuE8yliC2em1yXpUqM6E8InjIUq0OWf0B+re9j9XNLTKLoUuvWNQqCT98UBz g9fSo0pKO9phiKHbSeNGk/h0BW+X+4ZljGD7fJWywRYxL6AKZYJ3j9TYvK67iGjsOkXS xdai7C6JNlH3Xi5uoTC0V9FjKowfIngitflgMLJfp92uiDXy4GhNbIcdkoFjb4iGHGbc mzog8Kl5NIhZsRzgUi01lrKa7mGpQ9+IBIzhax/UQThM5effU4t9Y4o5VFFdYp6D1xkK rZYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=7cMyXIzIL94D6VTnKdFBV3rrJTxL/qORHkyq0bTsvjU=; b=Pq3awdNmO4fnQAxsXEM0riKhubf3Dk8/j7XOcvQJBdjfHkXgpIpfgMacoxJt6Bc2dL 2JxdmPrUTaPA4YnF0XxpeowHXyEX8GaanmXu/vEE7nT0Mf8RrJwExGP1l4nZcyWs20zT SXlnxIFYmrpp7gRCxtwry/ErWjFfuM6NtXhlZJg8eamqF14xOTXT9hat/o5ZNFWODcg6 Us9/9QNCX5qvHKG0D89WLz3/yzOdqOrdRpkJr8VPVnk/R9DqgCPY/zLiXpCWozjxRVAI HMl9wlNgizU3c46l9U2jK2ONHs9wxk8yOO7k4spt4GIDtql1sFUCSozi1/uEVBwliSD1 yfPg== X-Gm-Message-State: AHQUAubHoTwGTcjaDf1bayUSUghfHEpaZ9LzW2+LjKzkY3qdzRW5RmMj guXq5UKV9LejZXLtvykvffA90U23pM3bqR8J5aSp/Dh9Rd1Ph+XaQGHsNuGhkwDmFQ/5LKLXVfv foxQB6TYqOfVbj/yBV4vs9Dr/0UxwJ3B3oPGi7t5EYtfa9YfxXr6NBw4wDw== X-Google-Smtp-Source: AHgI3IZm90Q1fkabuRI+wrTqy26z/FlM8mVdVSg4bQ+v7E+CXKdK5ZYTItz2eYF4ZQ+t0amBwIlYXFKJp7g= X-Received: by 2002:a5b:890:: with SMTP id e16mr1580477ybq.101.1548618268566; Sun, 27 Jan 2019 11:44:28 -0800 (PST) Date: Sun, 27 Jan 2019 11:44:12 -0800 In-Reply-To: <20190127194415.171035-1-sxenos@google.com> Message-Id: <20190127194415.171035-5-sxenos@google.com> Mime-Version: 1.0 References: <20190127194415.171035-1-sxenos@google.com> X-Mailer: git-send-email 2.20.1.495.gaa96b0ce6b-goog Subject: [PATCH v3 5/8] evolve: Add the change-table structure From: sxenos@google.com To: git@vger.kernel.org Cc: Stefan Xenos Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Stefan Xenos 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 --- Makefile | 1 + change-table.c | 207 +++++++++++++++++++++++++++++++++++++++++++++++++ change-table.h | 138 +++++++++++++++++++++++++++++++++ 3 files changed, 346 insertions(+) create mode 100644 change-table.c create mode 100644 change-table.h diff --git a/Makefile b/Makefile index 7ffc383f2b..09cfd3ef1b 100644 --- a/Makefile +++ b/Makefile @@ -844,6 +844,7 @@ LIB_OBJS += branch.o LIB_OBJS += bulk-checkin.o LIB_OBJS += bundle.o LIB_OBJS += cache-tree.o +LIB_OBJS += change-table.o LIB_OBJS += chdir-notify.o LIB_OBJS += checkout.o LIB_OBJS += color.o diff --git a/change-table.c b/change-table.c new file mode 100644 index 0000000000..6daff5f58c --- /dev/null +++ b/change-table.c @@ -0,0 +1,207 @@ +#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(&(to_initialize->refname_to_change_head), 1); +} + +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)); +} + +static void change_head_array_clear(struct change_head_array *to_clear) +{ + FREE_AND_NULL(to_clear->array); +} + +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); + change_head_array_clear(&to_clear->heads); + mem_pool_discard(to_clear->memory_pool, 0); +} + +/* + * Appends a new, empty, change_head struct to the end of the given array. + * Returns the index of the newly-added struct. + */ +static int change_head_array_append(struct change_head_array *to_add) +{ + int index = to_add->nr++; + struct change_head *new_head; + ALLOC_GROW(to_add->array, to_add->nr, to_add->alloc); + new_head = &(to_add->array[index]); + memset(new_head, 0, sizeof(*new_head)); + return index; +} + +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(&(entry->changes.additional_refnames), 0); + } + + if (entry->changes.first_refname == NULL) { + 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; + long index; + int metacommit_type; + + index = change_head_array_append(&to_modify->heads); + new_head = &(to_modify->heads.array[index]); + + 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 = (void*)index; + // 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 != NULL) { + 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 == NULL) { + 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); + long index; + + if (!item) { + return NULL; + } + + index = (long)item->util; + return &(heads->heads.array[index]); +} + diff --git a/change-table.h b/change-table.h new file mode 100644 index 0000000000..85bb19c3bf --- /dev/null +++ b/change-table.h @@ -0,0 +1,138 @@ +#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. + */ + int abandoned:1, + hidden:1, + remote:1, + deleted:1; +}; + +/* + * An array of change_head. + */ +struct change_head_array { + struct change_head* array; + int nr; + int alloc; +}; + +/* + * 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 is an int index into change_metadata + * array. + */ + struct string_list refname_to_change_head; + /* change_head structures for each head */ + struct change_head_array heads; +}; + +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 From patchwork Sun Jan 27 19:44:13 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Xenos X-Patchwork-Id: 10783025 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id E940D1390 for ; Sun, 27 Jan 2019 19:44:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DA05229351 for ; Sun, 27 Jan 2019 19:44:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CB79E2A671; Sun, 27 Jan 2019 19:44:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BCD2829351 for ; Sun, 27 Jan 2019 19:44:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727040AbfA0Toc (ORCPT ); Sun, 27 Jan 2019 14:44:32 -0500 Received: from mail-oi1-f201.google.com ([209.85.167.201]:50745 "EHLO mail-oi1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726794AbfA0Toc (ORCPT ); Sun, 27 Jan 2019 14:44:32 -0500 Received: by mail-oi1-f201.google.com with SMTP id u63so7818635oie.17 for ; Sun, 27 Jan 2019 11:44:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=7jSLY1UHQhyX9VCJusx0gdlbIjr+7kwX88S0gteETU4=; b=FXrqCKutcBNrluxhTLUrUk3xi1WU0BX5CGojCwzLAW0zjYnum9Hp89Xy/rcYDRMrCm UD108GwFdcOF9yqmD+fvWeempCbOGcsfchMSARNUu0RTdiMpLkDjclgA172Hm1j1fgrq YWzMt0XwQr4JDt9SQNdOoM9HB1xYvEpEe7DbWyBlS9yfa20bt+THlR4jgaVxO16CA62m n80OgGJly1FTWlOMn6GE80VrNSDs0jTVuKDBJ5qOHJ/7GmpCS9lP/7mZkVdF6cFUiROC 5TRDuCqm4BPLNhLDK5y4fdxJHFy2DEnZAr034ZthVP9qE5LdywrC5ZWyq6+8CGsabSDU tHbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=7jSLY1UHQhyX9VCJusx0gdlbIjr+7kwX88S0gteETU4=; b=FIrsl2ljaHHvohrVWMUIchmnNCh/WWOphOoX0tqoJF94trUZv9VvNVmsrKKGKLqRmo FM4YFhU+ksZQF6iy7BKYeXrDvwXe3apoAYyu/uA1Gb197eJDnIHRYRMlOpzWw9iGWQ7w FSHNz4efkXqyvrlZ2mCvOVipOWkfzcDwABNAdFEZfbjIpOlorf85A3jnVPqBFHAsDcFS xR5oR2d2vsgBcPuTF+NqUvQa7WZ7GiMGMA4tdcizTqBZyjzGGuVHwMKTVHkC/lQFSQPb lCXlJwILZRc2U6Ei74FUu+P0Ft8Lj4c4vNaY9gWP5Kii5uu0am62I/cfGad4ErECJGGL B8pw== X-Gm-Message-State: AJcUukcXQwnwiOeBo4PrNX7LPPjLLvCHg/Rbpe5lF1wk0gJJ46hw7jt8 ef4W5ucQxkXcx1otX4Rrkk3kWyFK/moIoCfl189oGnf2sV4eqrVAX01JAqnGwcw89L5d8IC0YDn VM6anewb9/p+XYWdpSvf/shfl42eDt0ifnQEkxcO1QVMf8g7HdYUqXx/UJw== X-Google-Smtp-Source: ALg8bN4FThH0KSnc+Ld536aS7+noZiMggdGj7nfLPrN5Yv6R2aKK84N/pr8uVEFBxlbiHdT+p72HjMmI5G8= X-Received: by 2002:aca:4ccc:: with SMTP id z195mr15238047oia.11.1548618270939; Sun, 27 Jan 2019 11:44:30 -0800 (PST) Date: Sun, 27 Jan 2019 11:44:13 -0800 In-Reply-To: <20190127194415.171035-1-sxenos@google.com> Message-Id: <20190127194415.171035-6-sxenos@google.com> Mime-Version: 1.0 References: <20190127194415.171035-1-sxenos@google.com> X-Mailer: git-send-email 2.20.1.495.gaa96b0ce6b-goog Subject: [PATCH v3 6/8] evolve: Add support for writing metacommits From: sxenos@google.com To: git@vger.kernel.org Cc: Stefan Xenos Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Stefan Xenos 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 --- Makefile | 1 + metacommit.c | 370 +++++++++++++++++++++++++++++++++++++++++++++++++++ metacommit.h | 39 ++++++ 3 files changed, 410 insertions(+) create mode 100644 metacommit.c create mode 100644 metacommit.h diff --git a/Makefile b/Makefile index 09cfd3ef1b..a6be1780c5 100644 --- a/Makefile +++ b/Makefile @@ -920,6 +920,7 @@ LIB_OBJS += merge.o LIB_OBJS += merge-blobs.o LIB_OBJS += merge-recursive.o LIB_OBJS += mergesort.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 0000000000..409c2fa03f --- /dev/null +++ b/metacommit.c @@ -0,0 +1,370 @@ +#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) +{ + const char *buffer = get_commit_buffer(initial_commit, NULL); + const char *subject; + const char *eol; + int len; + 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(result, next); + } +} + +/* + * 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_addstr(result, default_name.buf); + + // 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) +{ + static const char *msg = "updating change"; + struct metacommit_data resolved_metacommit; + struct string_list changes; + struct object_id commit_target; + struct ref_transaction *transaction = NULL; + struct object_id old_head_working; + const struct object_id *old_head; + struct change_table chtable; + int i; + int ret = 0; + int force = (options & UPDATE_OPTION_FORCE); + + init_metacommit_data(&resolved_metacommit); + string_list_init(&changes, 1); + + change_table_init(&chtable); + + change_table_add_all_visible(&chtable, repo); + + resolve_metacommit(repo, &chtable, metacommit, &resolved_metacommit, &changes, + (options & UPDATE_OPTION_NOAPPEND) == 0); + + if (override_change) { + old_head = &old_head_working; + string_list_clear(&changes, 0); + if (get_oid_committish(override_change, &old_head_working)) { + // ...then this is a newly-created change + old_head = &null_oid; + } else if (!force) { + if (!oid_array_readonly_contains(&(resolved_metacommit.replace), + &old_head_working)) { + // Attempted non-fast-forward change + strbuf_addf(err, _("non-fast-forward update to '%s'"), + override_change); + ret = -1; + goto cleanup; + } + } + // 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); + string_list_clear(&changes, 0); + clear_metacommit_data(&resolved_metacommit); + change_table_clear(&chtable); + 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 0000000000..1d4be9cdfb --- /dev/null +++ b/metacommit.h @@ -0,0 +1,39 @@ +#ifndef METACOMMIT_H +#define METACOMMIT_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; +}; + +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 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 From patchwork Sun Jan 27 19:44:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Xenos X-Patchwork-Id: 10783027 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 75A4B91E for ; Sun, 27 Jan 2019 19:44:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 697B329351 for ; Sun, 27 Jan 2019 19:44:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 5D8C02A671; Sun, 27 Jan 2019 19:44:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3441C29351 for ; Sun, 27 Jan 2019 19:44:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727059AbfA0Tog (ORCPT ); Sun, 27 Jan 2019 14:44:36 -0500 Received: from mail-it1-f202.google.com ([209.85.166.202]:39252 "EHLO mail-it1-f202.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726794AbfA0Tof (ORCPT ); Sun, 27 Jan 2019 14:44:35 -0500 Received: by mail-it1-f202.google.com with SMTP id k133so10721772ite.4 for ; Sun, 27 Jan 2019 11:44:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=hbRrxRg4+mfGHIRGwfnl3uO8TpiBKJKWPkCqPRz1nO4=; b=u5QkOhEK1HwHElxaK+/RNA52NYqSNFkC3Chqh0Dcvivli5tbvrRfLKTuNLvlNGYMI5 i01o5qpibzo+Ozbukh7j4f7J6Hp2EDqzCsMCRHJ0epHYKf6JRT4AG0l4e4KDn1P6JlTh UFopDT/6dU3/q2uei+RYE+sTAy5PbNOR/8vvpby3RmlL/BBHEu3JI/LHoXsKQHpRxXVx vZAWWiGCKlVewnU9jZdbUXupIjs6tbl0Dx5TKpg9zqhg6CG+bIsar7a5ZYFgUYQwQt8U mqsarB+tnbaIzDE5JMKzKyXGI34k3pmLWFxv/sXip6F46fS4vTk76EzEhSm74ADICwQT FP3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=hbRrxRg4+mfGHIRGwfnl3uO8TpiBKJKWPkCqPRz1nO4=; b=WYBkcGHZpoXuzDHAaQmE4LaKznbN4VaXJKq6cdUpz35OejmhHv5ws6RV0pEcFtI0kG cKa4u8c2YP4TSnYhCb5QY2coko2hKrPaPdoZuLQHwD42LeqXJ1E8D718HoPaRR+vzTVS T6UY8ODU1R3lKMLbD02o9g0YOu7AbdAQ7Q0Bn0oK4die6XwHvb4DhMUJDum7qpk4GIt+ Pur0BFPF4mKulOJdbVqd6MnpwB1odgqUlYg9XT8ManbrINB15LC3I4cADDUsK/WgqBcQ 2IMjA5rWHe5XIYnSfEWTfo7VoKdhS+NYlCVv6VtsAzv1WJS6ovLwhUv8XBLXgBG4Dtv1 0WFA== X-Gm-Message-State: AHQUAuZtTzavFQppx4GcXMi8q2qO1JN4rWiVoG/ZNEHzbFMy8f5QpQ1d 53YmgsKCDr6KLsRXwdyO93MkT2NRKSx+dmZWgkgrlu4cU7jBKKa+b6YxjPvXdGkupvToSgGp8TH N/Q5/mru4/0lTDelV1DQqJ2e1u9O4IV1ZbHvXTbs0J/8hTPnsDzy53OYCMA== X-Google-Smtp-Source: ALg8bN7H0i8ouxLyy8xnuRLEb0CG35JHqkG+NtB04ykCr1mxz0iuhhd/4iiL+YglrtX9x7wUZrfyDn7Jg54= X-Received: by 2002:a24:5e92:: with SMTP id h140mr12326451itb.17.1548618273423; Sun, 27 Jan 2019 11:44:33 -0800 (PST) Date: Sun, 27 Jan 2019 11:44:14 -0800 In-Reply-To: <20190127194415.171035-1-sxenos@google.com> Message-Id: <20190127194415.171035-7-sxenos@google.com> Mime-Version: 1.0 References: <20190127194415.171035-1-sxenos@google.com> X-Mailer: git-send-email 2.20.1.495.gaa96b0ce6b-goog Subject: [PATCH v3 7/8] evolve: Implement the git change command From: sxenos@google.com To: git@vger.kernel.org Cc: Stefan Xenos Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Stefan Xenos 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 -r To record a cherry-pick in the change graph: git change update -c -o Signed-off-by: Stefan Xenos --- .gitignore | 1 + Makefile | 1 + builtin.h | 1 + builtin/change.c | 175 +++++++++++++++++++++++++++++++++++++++++++++++ git.c | 1 + 5 files changed, 179 insertions(+) create mode 100644 builtin/change.c diff --git a/.gitignore b/.gitignore index 0d77ea5894..8a084ac38b 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ /git-branch /git-bundle /git-cat-file +/git-change /git-check-attr /git-check-ignore /git-check-mailmap diff --git a/Makefile b/Makefile index a6be1780c5..d6fab30eca 100644 --- a/Makefile +++ b/Makefile @@ -1035,6 +1035,7 @@ BUILTIN_OBJS += builtin/blame.o BUILTIN_OBJS += builtin/branch.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 6538932e99..d2d39d9da8 100644 --- a/builtin.h +++ b/builtin.h @@ -137,6 +137,7 @@ extern int cmd_blame(int argc, const char **argv, const char *prefix); extern int cmd_branch(int argc, const char **argv, const char *prefix); extern int cmd_bundle(int argc, const char **argv, const char *prefix); extern int cmd_cat_file(int argc, const char **argv, const char *prefix); +extern int cmd_change(int argc, const char **argv, const char *prefix); extern int cmd_checkout(int argc, const char **argv, const char *prefix); extern int cmd_checkout_index(int argc, const char **argv, const char *prefix); extern int cmd_check_attr(int argc, const char **argv, const char *prefix); diff --git a/builtin/change.c b/builtin/change.c new file mode 100644 index 0000000000..ff7eb3b113 --- /dev/null +++ b/builtin/change.c @@ -0,0 +1,175 @@ +#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 update [--force] [--replace ...] [--origin ...] [--content ]"), + NULL +}; + +static const char * const builtin_update_usage[] = { + N_("git change update [--force] [--replace ...] [--origin ...] [--content ]"), + 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(&state->replace, 0); + string_list_init(&state->origin, 0); +} + +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; + int ret; + + init_metacommit_data(&metacommit); + + get_metacommit_from_command_line(state, &metacommit); + + ret = record_metacommit(repo, &metacommit, state->change, state->options, err); + + 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 0ce0e13f0f..f59f887238 100644 --- a/git.c +++ b/git.c @@ -453,6 +453,7 @@ static struct cmd_struct commands[] = { { "branch", cmd_branch, RUN_SETUP | DELAY_PAGER_CONFIG }, { "bundle", cmd_bundle, RUN_SETUP_GENTLY | NO_PARSEOPT }, { "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 }, From patchwork Sun Jan 27 19:44:15 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefan Xenos X-Patchwork-Id: 10783029 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 040DB1390 for ; Sun, 27 Jan 2019 19:44:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EC8EB29351 for ; Sun, 27 Jan 2019 19:44:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E0E632A671; Sun, 27 Jan 2019 19:44:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-15.5 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, USER_IN_DEF_DKIM_WL autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BC26329351 for ; Sun, 27 Jan 2019 19:44:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727067AbfA0Toh (ORCPT ); Sun, 27 Jan 2019 14:44:37 -0500 Received: from mail-pl1-f201.google.com ([209.85.214.201]:44220 "EHLO mail-pl1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726794AbfA0Tog (ORCPT ); Sun, 27 Jan 2019 14:44:36 -0500 Received: by mail-pl1-f201.google.com with SMTP id b24so10233057pls.11 for ; Sun, 27 Jan 2019 11:44:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=jHgC/objxk+ZaFKXZ0Ouk5mjEPMOghW/siqZWVSdiHM=; b=BivzZpnTZPYnU/gBAT7OBAwlvQx0nqdotSXjNj9fyoLOcndzhxxLdUjUrkndShIAFZ SpZeqn0EQJrdPK9UW7uHsrl/RQAh2cWsGKXUDz4tO3T2DQhBBeweYDfHWSnyDsLLG3TQ hmbVk7xHa9Jqv3HztvdIDHZBAZLr6XyJiwvDftMgQBEOp/z/dPgmgHKBwubifT0JA6V/ /DqtrKH/DGXNIZBc7x0OkMSvloeU5hxd7jxETrROoqutNf/zrLz87J8v1mGIJIzHboDi k7Avp3LpUpUqAiXqywa55PBRAzB3xqcUYn0GGMk/qBSRjHgpp6HN15/OtGQJtXUQkVO+ JI9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=jHgC/objxk+ZaFKXZ0Ouk5mjEPMOghW/siqZWVSdiHM=; b=k8KG5bBEhT4K9leLy7Wamj/V2DMjlq9d9wGSxZIKgZhL64/CktiWUKge2AGHIHzzxv 3tKrRXvA2sSa4isC7mR3M+W24g3xfsjQ5P5d6PJFetKpfvcVqwD5ALe9aNIJ8ynBHEtm P2bzUTiqU9+plKKpqOtXO4Ibz1QnCYLc+jDhYfXTMr6qFas0HKKkP+hnbqzrybUJ5g4q lWGlzL+QuKTtxHFYTHknMW1akOQC7kK1GqrlOrDOnuhlcp50BFq5d4dnJ2IsJNpOGSe3 3iyhHHLrmVIZw3CAqlFIBVjkw08ViKc9X9W29zofiaQvz7nFY98B/zJRg2iehMCM1oK8 QzCw== X-Gm-Message-State: AJcUukfs08bP9FdaRdYMxEyVS3rQZ2BbcXEky0uuZWD2lSsam0s9ieaQ LUwiKDMC3TCrBN/HeXoClkgbYXo2POWLSCxxJQ2WtlWWs3Xhgfh+VL7Ykk6Ktn+/WvfPkFp3SSQ DuYqdBjPodnKazVZ3yLQjE3Ye5qWIdGMjBAaZqFOC60++XXIUaLkaH+r6Lw== X-Google-Smtp-Source: ALg8bN5X4lvD2MFg6gotJip9SnyvSlAwkf02yyIBsTh4qQR8FWo+1xRshuNnCquZ2CalNf2jMstzunHoOmI= X-Received: by 2002:a62:860c:: with SMTP id x12mr8405125pfd.39.1548618275728; Sun, 27 Jan 2019 11:44:35 -0800 (PST) Date: Sun, 27 Jan 2019 11:44:15 -0800 In-Reply-To: <20190127194415.171035-1-sxenos@google.com> Message-Id: <20190127194415.171035-8-sxenos@google.com> Mime-Version: 1.0 References: <20190127194415.171035-1-sxenos@google.com> X-Mailer: git-send-email 2.20.1.495.gaa96b0ce6b-goog Subject: [PATCH v3 8/8] evolve: Add the git change list command From: sxenos@google.com To: git@vger.kernel.org Cc: Stefan Xenos Sender: git-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: git@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Stefan Xenos This command lists the ongoing changes from the refs/metas namespace. Signed-off-by: Stefan Xenos --- builtin/change.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/builtin/change.c b/builtin/change.c index ff7eb3b113..b63fe98665 100644 --- a/builtin/change.c +++ b/builtin/change.c @@ -5,15 +5,66 @@ #include "config.h" static const char * const builtin_change_usage[] = { + N_("git change list [...]"), N_("git change update [--force] [--replace ...] [--origin ...] [--content ]"), NULL }; +static const char * const builtin_list_usage[] = { + N_("git change list [...]"), + NULL +}; + static const char * const builtin_update_usage[] = { N_("git change update [--force] [--replace ...] [--origin ...] [--content ]"), NULL }; +static int change_list(int argc, const char **argv, const char* prefix) +{ + struct option options[] = { + OPT_END() + }; + struct ref_filter filter; + // TODO: Sorting temporarily disabled. See comments, below. + //struct ref_sorting *sorting = ref_default_sorting(); + 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. + //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++) { + show_ref_array_item(array.items[i], &format); + } + + ref_array_clear(&array); + + return 0; +} + struct update_state { int options; const char* change; @@ -164,6 +215,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 {