25#include <nlohmann/json.hpp>
56 throw std::invalid_argument(
"Unknown PROP_RES: " + aStr );
82 if( aStr ==
"delete" )
86 throw std::invalid_argument(
"Unknown ITEM_RES: " + aStr );
93void indexChangeTree(
const ITEM_CHANGE& aChange,
94 std::map<KIID_PATH, const ITEM_CHANGE*>& aIndex )
96 aIndex[aChange.id] = &aChange;
98 for(
const ITEM_CHANGE& child : aChange.children )
102 if( !child.id.empty() )
103 indexChangeTree( child, aIndex );
112 std::map<KIID_PATH, const ITEM_CHANGE*>
index;
115 indexChangeTree( c,
index );
123 std::map<wxString, const PROPERTY_DELTA*>
index;
138 if( aOurs && !aTheirs )
144 if( !aOurs && aTheirs )
155 wxASSERT( aOurs || aTheirs );
157 if( !aOurs && !aTheirs )
179 const bool baselinesMatch = aOurs->
before == aTheirs->
before;
222 r.
name = aJson.at(
"name" ).get<wxString>();
225 if( aJson.contains(
"custom" ) )
241 j[
"id"] =
id.AsString();
244 nlohmann::json arr = nlohmann::json::array();
247 arr.push_back( p.ToJson() );
249 j[
"props"] = std::move( arr );
257 r.
id =
KIID_PATH( aJson.at(
"id" ).get<wxString>() );
260 for(
const auto& p : aJson.at(
"props" ) )
271 nlohmann::json arr = nlohmann::json::array();
274 arr.push_back( a.ToJson() );
276 j[
"actions"] = std::move( arr );
278 nlohmann::json unr = nlohmann::json::array();
281 unr.push_back( u.AsString() );
283 j[
"unresolved"] = std::move( unr );
294 for(
const auto& a : aJson.at(
"actions" ) )
297 for(
const auto& u : aJson.at(
"unresolved" ) )
314 std::set<KIID_PATH> processed;
316 auto noteSideEffects = [&](
const ITEM_CHANGE& aChange )
319 plan.requiresZoneRefill =
true;
322 plan.requiresConnectivityRebuild =
true;
326 for(
const auto& [
id, ourChange] : ourIndex )
328 auto it = theirIndex.find(
id );
330 if( it == theirIndex.end() )
334 processed.insert(
id );
335 noteSideEffects( *ourChange );
336 noteSideEffects( *theirChange );
345 plan.actions.push_back( std::move( r ) );
353 plan.unresolved.push_back(
id );
357 plan.actions.push_back( std::move( r ) );
364 plan.unresolved.push_back(
id );
368 plan.actions.push_back( std::move( r ) );
379 plan.unresolved.push_back(
id );
383 plan.actions.push_back( std::move( r ) );
395 plan.unresolved.push_back(
id );
399 plan.actions.push_back( std::move( r ) );
408 if( ourChange->properties.empty() && theirChange->
properties.empty()
409 && ourChange->children.empty() && theirChange->
children.empty() )
411 plan.unresolved.push_back(
id );
415 plan.actions.push_back( std::move( r ) );
427 std::set<wxString> allNames;
429 for(
const auto& [n,
_] : ourProps ) allNames.insert( n );
430 for(
const auto& [n,
_] : theirProps ) allNames.insert( n );
432 bool hasUnresolvedProp =
false;
434 for(
const wxString&
name : allNames )
436 auto ourIt = ourProps.find(
name );
437 auto theirIt = theirProps.find(
name );
439 const PROPERTY_DELTA* ourDelta = ourIt != ourProps.end() ? ourIt->second :
nullptr;
440 const PROPERTY_DELTA* theirDelta = theirIt != theirProps.end() ? theirIt->second :
nullptr;
446 p.
kind = outcome.kind;
447 r.
props.push_back( std::move( p ) );
449 if( outcome.isUnresolved )
450 hasUnresolvedProp =
true;
453 if( hasUnresolvedProp )
454 plan.unresolved.push_back(
id );
456 plan.actions.push_back( std::move( r ) );
461 plan.unresolved.push_back(
id );
465 plan.actions.push_back( std::move( r ) );
469 for(
const auto& [
id, ourChange] : ourIndex )
471 if( processed.count(
id ) )
474 noteSideEffects( *ourChange );
479 switch( ourChange->kind )
491 plan.unresolved.push_back(
id );
495 plan.actions.push_back( std::move( r ) );
498 for(
const auto& [
id, theirChange] : theirIndex )
500 if( processed.count(
id ) || ourIndex.count(
id ) )
503 noteSideEffects( *theirChange );
508 switch( theirChange->kind )
520 plan.unresolved.push_back(
id );
524 plan.actions.push_back( std::move( r ) );
static DIFF_VALUE FromJson(const nlohmann::json &aJson)
MERGE_PLAN Plan(const DOCUMENT_DIFF &aAncestorOurs, const DOCUMENT_DIFF &aAncestorTheirs) const
Plan the merge given the canonical pair of diffs.
static std::vector< PENDING_PROPERTY > plan(const EDA_ITEM &aSource, const EDA_ITEM &aTarget, const std::set< wxString > &aEnabledKeys)
Target properties to write, paired with values read off the source.
bool ChangeInvalidatesZone(const ITEM_CHANGE &aChange)
Whether a change to an item of the given type invalidates any overlapping filled zones.
ITEM_RES
Resolution kind for a whole item.
const char * PropResToString(PROP_RES aRes)
Canonical lower-case spellings for PROP_RES used inside the JSON serialization of PROPERTY_RESOLUTION...
PROP_RES PropResFromString(const std::string &aStr)
PROPERTY_RESOLUTION_OUTCOME ResolvePropertyConflict(const PROPERTY_DELTA *aOurs, const PROPERTY_DELTA *aTheirs, const KICAD_MERGE_ENGINE::OPTIONS &aOptions)
Decide how to resolve a single property edit between two sides.
ITEM_RES ItemResFromString(const std::string &aStr)
PROP_RES
Resolution kind for a single property of a single item.
bool ChangeRequiresConnectivityRebuild(const ITEM_CHANGE &aChange)
Whether a change to an item of the given type requires the connectivity graph to be rebuilt.
std::map< KIID_PATH, const ITEM_CHANGE * > IndexChangesByKiid(const DOCUMENT_DIFF &aDiff)
Flatten a DOCUMENT_DIFF's ITEM_CHANGE tree into a KIID_PATH -> ITEM_CHANGE* map, recursing into child...
const char * ItemResToString(ITEM_RES aRes)
Canonical snake_case spellings used in MERGE_PLAN JSON serialization (take_ours / take_theirs / take_...
std::map< wxString, const PROPERTY_DELTA * > IndexPropertiesByName(const ITEM_CHANGE &aChange)
Index property deltas inside one ITEM_CHANGE by property name.
The full set of changes between two parsed documents of one type.
std::vector< ITEM_CHANGE > changes
One change record on a single item.
std::vector< PROPERTY_DELTA > properties
std::vector< ITEM_CHANGE > children
std::vector< PROPERTY_RESOLUTION > props
static ITEM_RESOLUTION FromJson(const nlohmann::json &aJson)
bool operator==(const ITEM_RESOLUTION &aOther) const
nlohmann::json ToJson() const
bool preferAutoMerge
When true, property-orthogonal edits auto-merge silently.
bool autoResolveEqualValues
When true and a property edit conflicts but the side values are equal, the resolution is automaticall...
Result of planning a 3-way merge.
bool requiresConnectivityRebuild
std::vector< ITEM_RESOLUTION > actions
nlohmann::json ToJson() const
std::vector< KIID_PATH > unresolved
static MERGE_PLAN FromJson(const nlohmann::json &aJson)
Single (name, before, after) triple for one mutated property on an item.
Per-property merge decision result.
static PROPERTY_RESOLUTION FromJson(const nlohmann::json &aJson)
nlohmann::json ToJson() const
bool operator==(const PROPERTY_RESOLUTION &aOther) const