简体   繁体   English

如何将具有差异内容的 2 个文件合并为 json 对象

[英]how to merge 2 files with diff contents as json objects

在此处输入图像描述

I'm trying to merge these two file contents into a new file.我正在尝试将这两个文件内容合并到一个新文件中。 I already have the left and rights contents as JSON objects with line numbers and line content.我已经将左右内容作为 JSON 具有行号和行内容的对象。

For instance, I want to select the left part and leave the right part in a new file after the merge.例如,我想 select 左侧部分并在合并后将右侧部分保留在新文件中。 Since the line numbers are different.由于行号不同。 How are we going to add the left content from the 214th line?我们如何从第 214 行添加左边的内容?

All I want is a final JSON object capturing merge decisions with line numbers.我想要的只是一个最终的 JSON object 捕获带有行号的合并决策。 Once I get the merge JSON.一旦我得到合并 JSON。 I'll use any programming language to create a new file and these changes.我将使用任何编程语言来创建一个新文件和这些更改。

NOTE: When Merging, I'm taking the right file as the main reference.注意:合并时,我将正确的文件作为主要参考。 I'll take a copy of the right file and add left contents as and when selected.我将复制正确的文件并在选择时添加左侧内容。

As Aleksey Tsalolikhin commented , what you're showing is XML, not JSON (as encoded into text).正如Aleksey Tsalolikhin 评论的那样,您显示的是 XML,而不是 JSON(编码为文本)。 That's not such a big deal: both just provide means of encoding tree-structured data.这没什么大不了的:两者都只是提供了编码树结构数据的方法。

Git does not understand any of this. Git 对此一无所知。 Git's merge code treats files as lines (of text). Git 的合并代码将文件视为(文本)。 That's OK, since you plan to do the merge yourself (with code or manually or whatever).没关系,因为您打算自己进行合并(使用代码或手动或其他方式)。

Note that diffing or merging tree-structured data, regardless of its encoding, is inherently complex.请注意,区分或合并树结构数据,无论其编码如何,本质上都是复杂的。 It should not use line numbers.它不应该使用行号。 It probably should see if subtrees match, and if so, consider them moved, even if they have changed depth.它可能应该查看子树是否匹配,如果匹配,则认为它们已移动,即使它们已更改深度。

If you've done the merge yourself, and the result should be encoded in text, encode it in text and insert it where it goes and give Git the result as a text file.如果您自己完成了合并,并且结果应该以文本编码,则将其编码为文本并将其插入到它所在的位置,并将结果作为文本文件提供给 Git。 All git merge needs is the final text file.所有git merge需要的是最终的文本文件。 The merge's inputs were three text files—one from the merge base commit, the second from the current commit, and the third from the other commit being merged—and what Git needs is a new text file.合并的输入是三个文本文件——一个来自合并基础提交,第二个来自当前提交,第三个来自另一个正在合并的提交——Git 需要一个新的文本文件。

So take your merge result, encode it as text, and you're basically done: write it to the appropriate file (or appropriate portion of the appropriate file) and tell Git this is the correct result of the merge .因此,将您的合并结果编码为文本,您基本上就完成了:将其写入适当的文件(或适当文件的适当部分)并告诉 Git这是合并的正确结果 In other words, write out the result and run git add path/to/file .换句话说,写出结果并运行git add path/to/file

NOTE: When Merging, I'm taking the right file as the main reference.注意:合并时,我将正确的文件作为主要参考。 I'll take a copy of the right file and add left contents as and when selected.我将复制正确的文件并在选择时添加左侧内容。

There are three inputs to a merge.合并有三个输入。 There isn't "left and right": there are merge base , ours , and theirs .没有“左右”:有合并基础我们的和他们的 The delta from base to ours represents what we changed.从 base 到我们的 delta 代表了我们所做的更改。 The delta from base to theirs represents what they changed.从 base 到他们的 delta 代表他们改变了什么。 Your job is to combine these changes .你的工作就是结合这些变化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM