KiCad PCB EDA Suite
Loading...
Searching...
No Matches
auto_resolution.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 * or you may search the http://www.gnu.org website for the version 3 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25
26#include <nlohmann/json.hpp>
27
28#include <wx/intl.h>
29
30#include <set>
31
32
33namespace KICAD_DIFF
34{
35
36AUTO_RESOLUTION_PARSE_RESULT ParseAutoResolutionJson( const std::string& aJsonContent )
37{
39
40 nlohmann::json j;
41
42 try
43 {
44 j = nlohmann::json::parse( aJsonContent );
45 }
46 catch( const nlohmann::json::exception& e )
47 {
49 out.errorContext = wxString::FromUTF8( e.what() );
50 return out;
51 }
52
53 if( !j.is_object() )
54 {
56 return out;
57 }
58
59 for( const auto& [key, val] : j.items() )
60 {
61 const wxString keyStr = wxString::FromUTF8( key );
62
63 if( !val.is_string() )
64 {
66 out.errorContext = keyStr;
67 out.resolutions.clear(); // contract: populated only on OK
68 return out;
69 }
70
72
73 try
74 {
75 kind = ItemResFromString( val.get<std::string>() );
76 }
77 catch( const std::invalid_argument& )
78 {
80 out.errorContext = keyStr;
81 out.resolutions.clear();
82 return out;
83 }
84
85 // Engine-internal kinds (KEEP / DELETE / MERGE_PROPS) make no sense
86 // as a scripted resolution — only the TAKE_* family represents a
87 // user-equivalent choice of side.
88 if( kind != ITEM_RES::TAKE_OURS && kind != ITEM_RES::TAKE_THEIRS
89 && kind != ITEM_RES::TAKE_ANCESTOR )
90 {
92 out.errorContext = keyStr;
93 out.resolutions.clear();
94 return out;
95 }
96
97 out.resolutions.emplace( keyStr, kind );
98 }
99
101 return out;
102}
103
104
105APPLY_AUTO_RESOLUTIONS_RESULT
107 const std::vector<std::size_t>& aConflictActionIndices,
108 const std::map<wxString, ITEM_RES>& aResolutions )
109{
111
112 if( aConflictActionIndices.empty() )
113 {
115 return out;
116 }
117
118 // Stage the writes so a partial-coverage failure leaves the plan
119 // untouched — the dialog promises that on PARTIAL the caller can
120 // still observe the original plan, e.g. for fallback reporting.
121 std::vector<std::pair<std::size_t, ITEM_RES>> staged;
122 staged.reserve( aConflictActionIndices.size() );
123
124 const std::set<KIID_PATH> unresolved( aPlan.unresolved.begin(), aPlan.unresolved.end() );
125
126 for( std::size_t actionIdx : aConflictActionIndices )
127 {
128 if( actionIdx >= aPlan.actions.size() )
129 {
130 // Stale/bad index — treat as PARTIAL so the caller bails out
131 // rather than silently dropping a conflict.
133 return out;
134 }
135
136 const ITEM_RESOLUTION& action = aPlan.actions[actionIdx];
137
138 auto it = aResolutions.find( action.id.AsString() );
139
140 if( it == aResolutions.end() )
141 {
142 // The engine's own property merge stands unless it left a property conflict
143 if( action.kind == ITEM_RES::MERGE_PROPS && !unresolved.contains( action.id ) )
144 continue;
145
147 out.firstMissingId = action.id;
148 return out;
149 }
150
151 staged.emplace_back( actionIdx, it->second );
152 }
153
154 for( const auto& [actionIdx, kind] : staged )
155 aPlan.actions[actionIdx].kind = kind;
156
157 aPlan.unresolved.clear();
159 out.appliedCount = staged.size();
160 return out;
161}
162
163
164std::vector<CONFLICT_LIST_ENTRY> BuildConflictList( const MERGE_PLAN& aPlan )
165{
166 std::vector<CONFLICT_LIST_ENTRY> out;
167
168 std::set<KIID_PATH> unresolvedSet( aPlan.unresolved.begin(), aPlan.unresolved.end() );
169
170 for( std::size_t i = 0; i < aPlan.actions.size(); ++i )
171 {
172 const ITEM_RESOLUTION& action = aPlan.actions[i];
173
174 if( !unresolvedSet.count( action.id ) && action.kind != ITEM_RES::MERGE_PROPS )
175 continue;
176
177 wxString label = action.id.AsString();
178
179 // Long KIID_PATH strings (multi-segment, on-disk-style) overflow the
180 // dialog's list control — keep the right side of the path because
181 // that's where the disambiguating segment lives.
182 if( label.Length() > 40 )
183 label = wxS( "…" ) + label.Right( 39 );
184
185 out.push_back( { i, label, action.id } );
186 }
187
188 return out;
189}
190
191
192std::vector<KIID_PATH>
194 const std::vector<std::size_t>& aConflictActionIndices )
195{
196 std::vector<KIID_PATH> out;
197
198 for( std::size_t actionIdx : aConflictActionIndices )
199 {
200 // Bounds-check the index — the function is public and callers can
201 // pass arbitrary indices. The dialog's own conflict-action list is
202 // built from plan.actions so this never fires for it, but a future
203 // mergetool consumer could feed in something stale.
204 if( actionIdx >= aPlan.actions.size() )
205 continue;
206
207 const ITEM_RESOLUTION& a = aPlan.actions[actionIdx];
208
211 {
212 out.push_back( a.id );
213 }
214 }
215
216 return out;
217}
218
219
220void SettleLibraryMerges( MERGE_PLAN& aPlan, const std::function<bool( const KIID_PATH& )>& aCanCombine,
221 const std::function<std::set<wxString>( const KIID_PATH& )>& aEditedNames )
222{
223 std::set<KIID_PATH> unresolved( aPlan.unresolved.begin(), aPlan.unresolved.end() );
224
225 for( ITEM_RESOLUTION& action : aPlan.actions )
226 {
227 if( action.kind != ITEM_RES::MERGE_PROPS )
228 continue;
229
230 if( aCanCombine( action.id ) )
231 {
232 unresolved.erase( action.id );
233 continue;
234 }
235
236 if( !aEditedNames )
237 continue;
238
239 action.kind = ITEM_RES::KEEP;
240 action.props.clear();
241
242 // Named for the details, which would otherwise omit field and pin edits
243 for( const wxString& name : aEditedNames( action.id ) )
244 {
246 prop.name = name;
247 action.props.push_back( std::move( prop ) );
248 }
249
250 if( unresolved.insert( action.id ).second )
251 aPlan.unresolved.push_back( action.id );
252 }
253
254 std::erase_if( aPlan.unresolved,
255 [&]( const KIID_PATH& aId )
256 {
257 return !unresolved.contains( aId );
258 } );
259}
260
261
262std::set<wxString> EditedNames( const ITEM_CHANGE& aChange )
263{
264 std::set<wxString> names;
265
266 for( const PROPERTY_DELTA& delta : aChange.properties )
267 names.insert( delta.name );
268
269 for( const ITEM_CHANGE& child : aChange.children )
270 {
271 // A bare pin number could collide with a field of that name
272 if( child.refdes && child.typeName == _( "Pin" ) )
273 names.insert( wxString::Format( _( "Pin %s" ), *child.refdes ) );
274 else
275 names.insert( child.refdes.value_or( child.typeName ) );
276 }
277
278 return names;
279}
280
281
282std::function<std::set<wxString>( const KIID_PATH& )> EditedNamesIn( const DOCUMENT_DIFF& aOurs,
283 const DOCUMENT_DIFF& aTheirs )
284{
285 return [ours = IndexChangesByKiid( aOurs ), theirs = IndexChangesByKiid( aTheirs )]( const KIID_PATH& aId )
286 {
287 std::set<wxString> edited;
288
289 for( const std::map<KIID_PATH, const ITEM_CHANGE*>* changes : { &ours, &theirs } )
290 {
291 if( auto it = changes->find( aId ); it != changes->end() )
292 edited.merge( EditedNames( *it->second ) );
293 }
294
295 return edited;
296 };
297}
298
299
300std::optional<BOX2I>
302 const std::map<KIID_PATH, BOX2I>& aPrimary,
303 const std::map<KIID_PATH, BOX2I>& aOurs,
304 const std::map<KIID_PATH, BOX2I>& aTheirs,
305 const std::map<KIID_PATH, BOX2I>& aAncestor )
306{
307 auto find = []( const std::map<KIID_PATH, BOX2I>& aMap,
308 const KIID_PATH& aLookup ) -> std::optional<BOX2I>
309 {
310 auto it = aMap.find( aLookup );
311
312 if( it == aMap.end() )
313 return std::nullopt;
314
315 // Treat a zero-size bbox (regardless of origin) as absence so a
316 // side with a placeholder entry falls through to the next side.
317 // A 1xN sliver is still usable — the highlight rectangle just
318 // gets narrow.
319 if( it->second.GetWidth() <= 0 && it->second.GetHeight() <= 0 )
320 return std::nullopt;
321
322 return it->second;
323 };
324
325 if( auto b = find( aPrimary, aId ); b.has_value() ) return b;
326 if( auto b = find( aOurs, aId ); b.has_value() ) return b;
327 if( auto b = find( aTheirs, aId ); b.has_value() ) return b;
328 if( auto b = find( aAncestor, aId ); b.has_value() ) return b;
329
330 return std::nullopt;
331}
332
333
334wxString BuildConflictDetailText( const ITEM_RESOLUTION& aResolution )
335{
336 wxString text;
337 text << _( "Current resolution: " );
338
339 switch( aResolution.kind )
340 {
341 case ITEM_RES::TAKE_OURS: text << _( "Take ours" ); break;
342 case ITEM_RES::TAKE_THEIRS: text << _( "Take theirs" ); break;
343 case ITEM_RES::TAKE_ANCESTOR: text << _( "Take ancestor" ); break;
344 case ITEM_RES::MERGE_PROPS: text << _( "Property-level merge" ); break;
345 case ITEM_RES::DELETE_ITEM: text << _( "Delete" ); break;
346 case ITEM_RES::KEEP: text << _( "Keep (default)" ); break;
347 }
348
349 text << wxS( "\n\n" );
350 text << wxString::Format( wxPLURAL( "%zu property edit in this resolution.\n",
351 "%zu property edits in this resolution.\n", aResolution.props.size() ),
352 aResolution.props.size() );
353
354 // A count alone does not say which edit a side would lose
355 wxString names;
356
357 for( const PROPERTY_RESOLUTION& prop : aResolution.props )
358 {
359 if( !names.IsEmpty() )
360 names << wxS( ", " );
361
362 names << prop.name;
363 }
364
365 if( !names.IsEmpty() )
366 text << wxString::Format( _( "Changed on either side: %s\n" ), names );
367
368 return text;
369}
370
371} // namespace KICAD_DIFF
const char * name
wxString AsString() const
Definition kiid.cpp:423
#define _(s)
wxString BuildConflictDetailText(const ITEM_RESOLUTION &aResolution)
Build the human-readable detail text shown in the merge dialog's resolution panel for a given ITEM_RE...
void SettleLibraryMerges(MERGE_PLAN &aPlan, const std::function< bool(const KIID_PATH &)> &aCanCombine, const std::function< std::set< wxString >(const KIID_PATH &)> &aEditedNames)
Settle the MERGE_PROPS of a library merge, where an item is combined whole or not at all.
AUTO_RESOLUTION_PARSE_RESULT ParseAutoResolutionJson(const std::string &aJsonContent)
Parse a KICAD_MERGETOOL_AUTO-format JSON object mapping item IDs (KIID_PATH strings) to ITEM_RES spel...
@ BAD_VALUE
at least one entry's value wasn't a string
@ NOT_OBJECT
JSON parsed but the root was not an object.
@ INVALID_JSON
input could not be parsed as JSON
@ UNKNOWN_KIND
at least one entry's string wasn't a known ITEM_RES
@ OK
parsed cleanly; resolutions is populated
@ ENGINE_INTERNAL_KIND
at least one entry asked for an engine-internal kind (KEEP / DELETE / MERGE_PROPS); only the TAKE_* f...
ITEM_RES
Resolution kind for a whole item.
std::vector< CONFLICT_LIST_ENTRY > BuildConflictList(const MERGE_PLAN &aPlan)
std::optional< BOX2I > ResolveConflictBBox(const KIID_PATH &aId, const std::map< KIID_PATH, BOX2I > &aPrimary, const std::map< KIID_PATH, BOX2I > &aOurs, const std::map< KIID_PATH, BOX2I > &aTheirs, const std::map< KIID_PATH, BOX2I > &aAncestor)
Resolve the best bounding box for a conflicted item across the three sides of a 3-way merge.
APPLY_AUTO_RESOLUTIONS_RESULT ApplyAutoResolutions(MERGE_PLAN &aPlan, const std::vector< std::size_t > &aConflictActionIndices, const std::map< wxString, ITEM_RES > &aResolutions)
Apply a parsed auto-resolution map to a MERGE_PLAN's conflicts.
ITEM_RES ItemResFromString(const std::string &aStr)
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...
std::function< std::set< wxString >(const KIID_PATH &)> EditedNamesIn(const DOCUMENT_DIFF &aOurs, const DOCUMENT_DIFF &aTheirs)
What either side edits in an item, for SettleLibraryMerges. Both diffs must outlive the result.
std::set< wxString > EditedNames(const ITEM_CHANGE &aChange)
The properties and elements aChange edits, by the names a user would know them by.
std::vector< KIID_PATH > CollectUnresolvedConflicts(const MERGE_PLAN &aPlan, const std::vector< std::size_t > &aConflictActionIndices)
Return the conflict actions that still have no decision.
APPLY_AUTO_RESOLUTIONS_STATUS status
KIID_PATH firstMissingId
Set only when status == PARTIAL AND the partial result was caused by a missing entry in the auto-reso...
Result of ParseAutoResolutionJson.
std::map< wxString, ITEM_RES > resolutions
The full set of changes between two parsed documents of one type.
One change record on a single item.
std::vector< PROPERTY_DELTA > properties
std::optional< wxString > refdes
std::vector< ITEM_CHANGE > children
std::vector< PROPERTY_RESOLUTION > props
Result of planning a 3-way merge.
std::vector< ITEM_RESOLUTION > actions
std::vector< KIID_PATH > unresolved
Single (name, before, after) triple for one mutated property on an item.
int delta