A semi-structured statement-level code merging method and system
Patent Information
- Application Number
- CN202510467112.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-15
- Publication Date
- 2026-10-23
AI Technical Summary
该算法即使是注释、格式等代码无关的调整导致行内容发生变化时,在这些工具看来代码也发生了变化,这会导致潜在的不必要冲突
本发明提供了一种半结构化的语句级别代码合并方法及系统,通过封装自定义代码图,将AST中的节点信息进行结构化处理,并补全可能丢失的文本信息和风格特征,确保合并后文件中的信息完整性,包括注释、格式和其他非语法信息。此外,自定义代码图精简了节点数量和层级结构,减少了冗余和复杂度,进而提高了后续节点匹配和处理的效率。这种方法不仅提升了代码合并过程的自动化水平和精确度,还能减少手动干预和错误发生的可能性,确保最终合并结果的高质量和可维护性,尤其适用于多人协作开发和大规模软件项目的场景。
Smart Images

Figure CN122965429A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of software code merging technology, and in particular to a semi-structured statement-level code merging method and system. Background Technology
[0002] In modern software development environments, as modern software projects increase functionality and technology improve, the code size gradually expands. Many software projects can no longer be coded by a single person, inevitably facing the challenge of multi-person collaborative development. Currently, this is primarily addressed through version control systems. When a developer completes their individual branch's development task, they can attempt to merge that branch's updates with the stable main branch to complete the task. However, during the merging process, multiple developers often encounter situations where they are simultaneously updating the same part of the code, resulting in a merge conflict. This requires manual intervention from the developers, known as conflict resolution.
[0003] Merge conflicts are a common cause of pull request failures, continuous integration build failures, and potential software defects in large projects. Currently, mainstream distributed version control systems like Git use line-by-line merging algorithms for code merging. This algorithm treats even minor changes unrelated to code, such as comments or formatting, as changes to line content, leading to potential unnecessary conflicts. Furthermore, some code changes are merely syntactic, remaining semantically unchanged, yet these tools still report conflicts. Both of these situations result in false positives for conflicting code in line-merging tools like Git during practical applications. Therefore, the ability to detect potentially conflicting code blocks as quickly and accurately as possible is crucial.
[0004] Based on these issues, numerous related works have attempted to reduce code conflict rates by resolving more unnecessary conflicts and to decrease the false positive rate of merge tasks, preventing erroneous code from flowing into downstream stages. Existing tools can be broadly categorized into two types based on their conflict resolution methods. One type attempts to structure text data. These works analyze and process semantics and syntax through structured code, reducing false positives caused by the limitations of line-by-line text itself, and improving merge accuracy by reducing the granularity of code merging. The other type of work uses machine learning or large models to reduce conflict rates. It utilizes pre-trained models or large model prompting learning methods to allow the model to predict conflict resolution strategies to automatically repair conflicts generated during merging, thereby completing the conflict resolution process. Some works also attempt to combine these two methods, using structured data to slice and divide code, and then training and outputting large models to improve the merge success rate. Summary of the Invention
[0005] To address the aforementioned problems, this invention proposes a semi-structured statement-level code merging method and system, providing an efficient solution for resolving statement-level code merging conflicts while ensuring the integrity of code information. The above-mentioned objective of this invention is achieved through the following technical solutions: This invention provides a semi-structured statement-level code merging method, comprising the following steps: Step S1: Respond to the code merging request; Step S2: Based on the code block to be merged in the code merge request, use the AST tool to parse the original code block and at least two branch modified code blocks of the code block to be merged to generate a structured AST model and encapsulate it into the corresponding code diagram; Step S3: Traverse the base nodes of the base code graph corresponding to the original code block, establish the correspondence between the base nodes and the branch nodes in the code graph of the modified code block by matching the nodes through the node association information, perform conflict change detection in the code merging process according to the correspondence, and update the content of the base code graph. Step S4: Convert the updated base code graph into code text, extract the content through the line number information of the base node, and reconstruct the code text according to the corresponding hierarchical structure to complete the code merging request.
[0006] Further, step S2 includes: Step S21: Use the AST tool to parse the code block to be merged and generate the corresponding AST model; start traversing from the root node of the AST model and select nodes at the statement level and above in the AST node information to process; Step S22: Initialize the AST node information in the AST model, including assigning unique identifiers to AST nodes, recording node type and syntax attributes, marking frame nodes, and configuring parent and child node pointers. Based on each node selected during traversal, obtain the starting line number start of the node and the ending line number end of the previous node, calculate the difference between start and end, and determine whether the difference is greater than or equal to 1. If the difference is not greater than 1, determine that the current node does not contain text comments; otherwise, preliminarily determine that the current node contains text comments, and obtain the content from start to end of the original text to determine whether it contains comment text, and encapsulate the content of the text comment into the code graph of the current node. Step S23: Encapsulate the current node into the code graph according to the node type. For non-block type nodes, encapsulate them directly as independent nodes; for block type nodes, encapsulate them as composite graph nodes containing at least one child node. For each block type composite graph node, recursively traverse all child nodes of the composite graph node and repeat steps S22 and S23 to construct a complete subgraph for each composite graph node and integrate them to construct a complete code graph.
[0007] Furthermore, in step S3, the node association information includes content similarity, sibling node information, and parent node information; the correspondence relationships include complete match, update, move, move and update, insertion, and deletion.
[0008] Further, in step S3, the content similarity calculation includes: The code was reconstructed using AST tools to standardize the format of node content; Determine whether the length of the content of two nodes exceeds a preset threshold, and calculate the content similarity. When the content of two nodes does not exceed a preset threshold, the ratio of the length of the longest common substring of the content of the two nodes to the maximum length of the content of the two nodes is used as the content similarity. When the content of two nodes exceeds a preset threshold, the ratio of the Levenstein distance between the content of the two nodes to the maximum length of the content of the two nodes is used as the content similarity.
[0009] Further, in step S3, node matching is performed using node association information to establish the correspondence between the base node and the branch nodes in the code graph of the branch modification code block, including: Calculate the content similarity between the base node and the branch nodes, and perform node matching by combining the information of sibling nodes and parent nodes; When the content of the base node and the branch node are the same, check whether the information of the sibling node and the parent node are consistent. If the information of the sibling node and the parent node are consistent, the correspondence is a complete match; otherwise, if the information of the sibling node and the parent node are inconsistent, the correspondence is a move. Determine whether the content similarity between the base node and the branch nodes exceeds a set threshold, and determine whether the information of the sibling node is consistent with the information of the parent node; When the content similarity exceeds the set threshold, and the sibling node information and the parent node information are consistent, the corresponding relationship is updated; When the content similarity exceeds the set threshold and the sibling node information is inconsistent with the parent node information, the corresponding relationship is updated by moving. If the content similarity does not exceed the set threshold, and a branch node cannot match any base node, the branch node is inserted, and the base node is deleted.
[0010] Furthermore, in step S3, establishing the correspondence between the base node and the branch nodes in the code graph of the branch modification code block through node association information also includes: When the correspondence between the base node and the branch node is a move or move update, the node pair is split and processed, with the base node marked as deleted and the branch node marked as inserted.
[0011] Furthermore, in step S3, the correspondence between the base node and the branch node in the code graph of the branch modification code block is established by matching nodes through node association information. This also includes: checking all branch nodes marked as inserted; when the nodes are sibling nodes under the same parent node, the sibling nodes are merged into one node; for the identified child nodes under the same parent node, the parent node is used as the representative of the child node, and the parent node is retained in the code graph while all individual child node insertions are removed.
[0012] Further, in step S3, conflict change detection includes: Based on the correspondence between the base node and at least one branch node, determine whether the node is a conflicting change; When a branch node completely matches the base node, or when branch nodes delete the same node at the same time, the change is a non-conflicting change. A change is considered a conflicting change when a change to the base node is inconsistent with a change to any branch node and / or when at least two of the branch nodes are inserted in the same position.
[0013] Furthermore, in step S3, updating the content of the base code graph includes: For nodes involving non-conflict changes, nodes marked as insertions are inserted between their corresponding predecessor and successor nodes in the base code graph, based on their context position; nodes marked as updates are updated in the corresponding nodes in the base code graph; and nodes marked as deletions are deleted by finding the corresponding nodes in the code graph. For nodes that have undergone conflict changes, the nodes marked as inserted are constructed using the git conflict format in the base code graph according to the node's context position. This conflict content is then encapsulated into the graph node, and the conflicting node is inserted between its corresponding predecessor and successor nodes. For changed nodes, the corresponding node is found in the base code graph, and its content is updated using the git conflict format.
[0014] Based on the same inventive concept, this invention also provides a semi-structured statement-level code merging system, which performs the statement-level code merging method described above, including: The response module is used to respond to code merging requests; The data processing module is used to parse the original code block and at least two branch modification code blocks of the code block to be merged in the code merge request, generate a structured AST model, and encapsulate it into a corresponding code graph. It traverses the base nodes of the base code graph corresponding to the original code block, establishes the correspondence between the base nodes and the branch nodes in the code graph of the branch modification code blocks by matching the nodes through the node association information, performs conflict change detection in the code merging process according to the correspondence, and updates the content of the base code graph. The output module is used to convert the updated base code graph into code text, extract content through the line number information of the base node, and reconstruct the code text according to the corresponding hierarchical structure to complete the code merging request.
[0015] Compared with the prior art, the present invention has at least one of the following beneficial effects: This invention provides a semi-structured statement-level code merging method and system. By encapsulating a custom code graph, it structures the node information in the Abstract Syntax Tree (AST) and completes any potentially lost text information and style features, ensuring the integrity of the information in the merged file, including comments, formatting, and other non-syntactic information. Furthermore, the custom code graph simplifies the number of nodes and hierarchical structure, reducing redundancy and complexity, thereby improving the efficiency of subsequent node matching and processing. This method not only enhances the automation and accuracy of the code merging process but also reduces the possibility of manual intervention and errors, ensuring the high quality and maintainability of the final merged result. It is particularly suitable for scenarios involving multi-person collaborative development and large-scale software projects. Attached Figure Description
[0016] Figure 1 This is a flowchart illustrating the steps of the statement-level code merging method based on AST partitioning and pruning in this invention. Figure 2 This is a schematic diagram illustrating the working principle of the statement-level code merging method based on AST partitioning and pruning in this invention. Figure 3 This is a schematic diagram illustrating the implementation process of the statement-level code merging method based on AST partitioning and pruning in this invention. Detailed Implementation
[0017] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0018] Those skilled in the art will understand that, unless specifically stated otherwise, the singular forms “a,” “an,” “the,” and “the” used herein may also include the plural forms. It should be further understood that the term “comprising” as used in this specification means the presence of the stated features, integers, steps, operations, elements, and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and / or groups thereof.
[0019] First Embodiment In existing technologies, traditional code merging methods mainly rely on line-level comparisons. While this approach can be effective in certain scenarios, it also has several problems. For example, minor changes in code style, comments, or formatting can lead to false conflict reports from merging tools, especially in collaborative development environments where multiple developers may simultaneously make different modifications to the same code, making it difficult to automatically handle merge conflicts. These false conflict reports not only increase the burden on developers but may also introduce unnecessary errors.
[0020] To address the aforementioned problems, the inventors of this invention, after in-depth consideration, proposed a novel code merging method based on semi-structured statement-level syntax. This method utilizes an AST tool to parse at least two code blocks to be merged, generating corresponding ASTs. Based on the AST information and the source files, the code blocks are transformed into a custom graph structure, hereinafter referred to as the code graph. Subsequently, node matching is performed on this code graph, and the graph node content is updated and conflicts are marked according to the matching results, achieving efficient statement-level code merging while ensuring the integrity of the code information.
[0021] The advantages and practical applications of the present invention are further demonstrated through the description of specific embodiments: such as Figure 1 As shown, this invention provides a semi-structured statement-level code merging method, mainly used to provide an efficient solution to statement-level code merging conflicts while ensuring the integrity of code information. The method includes the following steps: Step S1: Respond to the code merging request; Step S2: Based on the code block to be merged in the code merge request, use the AST tool to parse the original code block and at least two branch modified code blocks of the code block to be merged to generate a structured AST model and encapsulate it into the corresponding code diagram; It should be noted that, assuming there are three code blocks to be merged, taken from the original file, the modified file evolved from branch a, and the modified file evolved from branch b, respectively, the original file will be referred to as code block base, the modified file evolved from branch a as code block a, and the modified file evolved from branch b as code block b. The three code blocks are parsed using an AST tool to generate their respective AST information, i.e., abstract syntax trees. The three AST information generated above, i.e., the three abstract syntax trees, are parsed and combined with the code block content to encapsulate a code graph. In the process of generating the graph, in addition to the code statements, the AST node information and the original text content are combined to complete any missing comment text in the AST, ensuring that the code graph can completely represent the original code block content.
[0022] Step S3: Traverse the base nodes of the base code graph corresponding to the original code block, establish the correspondence between the base nodes and the branch nodes in the code graph of the modified code block by matching the nodes through the node association information, perform conflict change detection in the code merging process according to the correspondence, and update the content of the base code graph. Specifically, the code graph's base nodes are traversed, and a correspondence is established between the base nodes and branch nodes (a and b) based on content similarity, contextual relationships, and parent nodes. This correspondence includes ALL_MATCH, UPDATE, INSERT, DELETE, MOVE, and MOVE_UPDATE. To ensure the atomicity and indirectness of this correspondence, the relationships between nodes are further simplified and integrated, resulting in ALL_MATCH, UPDATE, INSERT, and DELETE correspondences. The code graph's base nodes are then traversed, and based on the established correspondences between the base nodes and branch nodes, it is determined whether the node has undergone changes or conflicts. If a change has occurred without conflict, it is considered a non-conflicting change; if a change has occurred and a conflict exists, it is considered a conflicting change. Finally, the code graph's base nodes are updated based on the obtained non-conflicting, conflicting, and conflicting changes.
[0023] Step S4: Convert the updated base code graph into code text, extract the content through the line number information of the base node, and reconstruct the code text according to the corresponding hierarchical structure to complete the code merging request.
[0024] Research revealed that because Abstract Syntax Trees (ASTs) only focus on the syntactic structure of code, plain text information such as comments and style features are lost. Therefore, although AST-based code merging methods can usually resolve code conflicts at a finer-grained level, they have limitations due to the aforementioned reasons. This invention combines AST information with code block content to complete the missing text information and style features during code graph generation, ensuring that the code graph can fully represent the code block content and overcoming the limitations of the aforementioned AST-based merging methods.
[0025] Further, step S2 includes: Step S21: Use the AST tool to parse the code block to be merged to generate the corresponding AST model; start traversing from the root node of the AST model and select nodes at the statement level and above in the AST node information to process; Step S22: Initialize the AST node information in the AST model, including assigning unique identifiers to AST nodes, recording node type and syntax attributes, marking frame nodes, and configuring parent and child node pointers. Based on each node selected during traversal, obtain the starting line number start of the node and the ending line number end of the previous node, calculate the difference between start and end, and determine whether the difference is greater than or equal to 1. If the difference is not greater than 1, determine that the current node does not contain text comments; otherwise, preliminarily determine that the current node contains text comments, and obtain the content from start to end of the original text to determine whether it contains comment text, and encapsulate the content of the text comment into the code graph of the current node. Step S23: Encapsulate the current node into the code graph according to the node type. For non-block type nodes, encapsulate them directly as independent nodes; for block type nodes, encapsulate them as composite graph nodes containing at least one child node. For each block type composite graph node, recursively traverse all child nodes of the composite graph node and repeat steps S22 and S23 to construct a complete subgraph for each composite graph node and integrate them to construct a complete code graph.
[0026] In this process, non-block type nodes are those without sub-statements, such as variable definitions and function calls; block type nodes are those with sub-statements, such as if, while, and try. The sub-ASTs are traversed, repeating the steps starting from S21, and the resulting code graph is recorded as a sub-graph, i.e., a child node of the parent. Through this method, the generated code graph possesses the original hierarchical structure of the code, such as if statements and their sub-statements, and also has complete text information. The text comments extracted from the code blocks ensure the restoration of the syntax structure and the integrity of the text comments when the code graph is subsequently converted into code text.
[0027] Furthermore, in step S3, the node association information includes content similarity, sibling node information, and parent node information; the correspondence relationships include complete match, update, move, move and update, insertion, and deletion.
[0028] Further, in step S3, the content similarity calculation includes: The code was reconstructed using AST tools to standardize the format of node content; Determine whether the length of the content of two nodes exceeds a preset threshold, and calculate the content similarity. When the content of two nodes does not exceed a preset threshold, the ratio of the length of the longest common substring of the content of the two nodes to the maximum length of the content of the two nodes is used as the content similarity. When the content of two nodes exceeds a preset threshold, the ratio of the Levenstein distance between the content of the two nodes to the maximum length of the content of the two nodes is used as the content similarity.
[0029] For example, code can be reconstructed using AST tools to standardize the format of node content and reduce the impact of code style, indentation, and other factors on similarity calculation. 2. For nodes with content lengths ≤ 64, the similarity is calculated as the ratio of the longest common substring length to the maximum length of the two node contents; for nodes with content lengths > 64, the similarity is calculated as the ratio of the Levenstein distance to the maximum length of the two node contents.
[0030] Further, in step S3, node matching is performed using node association information to establish the correspondence between the base node and the branch nodes in the code graph of the branch modification code block, including: Calculate the content similarity between the base node and the branch nodes, and perform node matching by combining the information of sibling nodes and parent nodes; When the content of the base node and the branch node are the same, check whether the information of the sibling node and the parent node are consistent. If the information of the sibling node and the parent node are consistent, the correspondence is a complete match; otherwise, if the information of the sibling node and the parent node are inconsistent, the correspondence is a move. Determine whether the content similarity between the base node and the branch nodes exceeds a set threshold, and determine whether the information of the sibling node is consistent with the information of the parent node; When the content similarity exceeds the set threshold, and the sibling node information and the parent node information are consistent, the corresponding relationship is updated; When the content similarity exceeds the set threshold and the sibling node information is inconsistent with the parent node information, the corresponding relationship is updated by moving. If the content similarity does not exceed the set threshold, and a branch node cannot match any base node, the branch node is inserted, and the base node is deleted.
[0031] Furthermore, in step S3, establishing the correspondence between the base node and the branch nodes in the code graph of the branch modification code block through node association information also includes: When the correspondence between the base node and the branch node is a move or move update, the node pair is split and processed, with the base node marked as deleted and the branch node marked as inserted.
[0032] Furthermore, in step S3, the correspondence between the base node and the branch node in the code graph of the branch modification code block is established by matching nodes through node association information. This also includes: checking all branch nodes marked as inserted; when the nodes are sibling nodes under the same parent node, the sibling nodes are merged into one node; for the identified child nodes under the same parent node, the parent node is used as the representative of the child node, and the parent node is retained in the code graph while all individual child node insertions are removed.
[0033] For example, the specific rules for establishing the correspondence between the base node and branch node a / branch node b are as follows, taking branch node a as an example: If the base node and node a have the same content and the same context (sibling node and parent node), it is ALL_MATCH (complete match). If the similarity between the base node and branch node a exceeds the threshold s (generally 0.5), and they have the same context and parent node, it is UPDATE (update). If the base node and branch node a have the same content, but different context or parent node, it is MOVE (move). If the similarity between the base node and branch node a exceeds the threshold s, but different context or parent node, it is MOVE_UPDATE (move update). Branch node a that does not meet the above criteria is considered INSERT (insertion). Base node that does not meet the above criteria is considered DELETE (deletion).
[0034] Therefore, the further simplification and integration of the correspondence between the base node and branch node a / branch node b is as follows, taking branch node a as an example: If there are node pairs with a correspondence of MOVE_UPDATE or MOVE, they are split into base node - DELETE and a node - INSERT to ensure the atomicity of the correspondence. If there are several branch nodes a belonging to INSERT that are sibling nodes, they are combined as one node. If there are several branch nodes a belonging to INSERT that share the same parent node, the parent node is used to represent its child nodes, and the remaining child nodes are removed, further simplifying the code diagram and improving the efficiency of subsequent processing.
[0035] Further, in step S3, conflict change detection includes: Based on the correspondence between the base node and at least one branch node, determine whether the node is a conflicting change; When a branch node completely matches the base node, or when branch nodes delete the same node at the same time, the change is a non-conflicting change. A change is considered a conflicting change when a change to the base node is inconsistent with a change to any branch node and / or when at least two of the branch nodes are inserted in the same position.
[0036] In other words, when two branches, namely file a and file b, make different modifications to the same base node, i.e., the original code block, this situation is considered a conflicting change. Additionally, if both branches insert code blocks at the same location, that is, between two identical base nodes, this situation is also considered a conflicting change. Examples of these two situations are as follows: ① If two branches make different modifications to the same base node, for example, if the original code block's base node is a function body, branch a modifies the function's implementation, while branch b makes a different modification, a conflict will occur because both branches have changed the content of the same node, preventing automatic merging.
[0037] ② Both branches insert code at the same location. For example, branch A inserts a code block at a certain location in the original code, i.e., between the base nodes, and branch B also inserts a code block at the same location. In this case, because the code insertions on both sides occur on the same line or in the same area, this is also considered a conflict, because automatic merging cannot determine which inserted code should be included at that location.
[0038] Therefore, when two branches make different modifications or insertions to the same node or in the same location, they are considered conflicting changes and cannot be automatically merged, requiring manual intervention.
[0039] Specifically, the methods for determining conflicting and non-conflicting changes are as follows: 1. If one of them is ALL_MATCH, it is a non-conflicting change. 2. If both are DELETE, it is a non-conflicting change. 3. For INSERT nodes, if branch a node and branch b node have the same context, i.e., they are added at the same location, it is considered a conflicting change. 4. Other combinations, including UPDATE-UPDATE and UPDATE-DELETE, are considered conflicting changes.
[0040] Furthermore, in step S3, updating the content of the base code graph includes: For nodes involving non-conflict changes, nodes marked as insertions are inserted between their corresponding predecessor and successor nodes in the base code graph, based on their context position; nodes marked as updates are updated in the corresponding nodes in the base code graph; and nodes marked as deletions are deleted by finding the corresponding nodes in the code graph. For nodes that have undergone conflict changes, the nodes marked as inserted are constructed using the git conflict format in the base code graph according to the node's context position. This conflict content is then encapsulated into the graph node, and the conflicting node is inserted between its corresponding predecessor and successor nodes. For changed nodes, the corresponding node is found in the base code graph, and its content is updated using the git conflict format.
[0041] Specifically, the method for updating the code graph base is as follows: 1. For INSERT nodes that do not involve conflicts, denoted as Node based on their context. prev Node next Find the node in the code graph base. prev Node next Corresponding Node base-prev Node base-next In Node base-prev Node base-next 1. Insert INSERT nodes between the given conditions. 2. For UPDATE nodes that do not involve conflicts, locate the corresponding node in the code graph base and update its content directly. 3. For DELETE nodes that do not involve conflicts, locate the corresponding node in the code graph base and delete it. 4. For INSERT nodes that involve conflicts, denote them as Nodes based on their context. prev Node next Find the node in the code graph base. prev Node next The corresponding nodes Nodebase-prev and Nodebase-next construct conflict content according to the git conflict format, encapsulate it into the graph nodes, and then... base-prev Node base-next Insert the conflicting node between them. For the remaining conflicting change nodes, find the corresponding Node in the code graph base. base Update Node.js with conflict content structured according to git conflict format. base .
[0042] Second Embodiment Based on the same inventive concept, this invention also provides a semi-structured statement-level code merging system, which performs the statement-level code merging method described above, including: The response module is used to respond to code merging requests; The data processing module is used to parse the original code block and at least two branch modification code blocks of the code block to be merged in the code merge request, generate a structured AST model, and encapsulate it into a corresponding code graph. It traverses the base nodes of the base code graph corresponding to the original code block, establishes the correspondence between the base nodes and the branch nodes in the code graph of the branch modification code blocks by matching the nodes through the node association information, performs conflict change detection in the code merging process according to the correspondence, and updates the content of the base code graph. The output module is used to convert the updated base code graph into code text, extract content through the line number information of the base node, and reconstruct the code text according to the corresponding hierarchical structure to complete the code merging request.
[0043] Third Embodiment In collaborative software development environments, code merging has always been a challenge, especially when multiple branches are simultaneously modifying the same functionality. Traditional code merging methods primarily rely on line-level comparison, which can lead to unnecessary false alarms and information loss, particularly when comments or formatting change; merging tools are prone to ignoring or mishandling these subtle changes. To effectively address these issues, this invention proposes a semi-structured statement-level code merging method based on AST parsing and a custom code graph. This method can accurately identify syntactic and semantic changes in the code, significantly improving merging accuracy and efficiency.
[0044] In this embodiment, we take the merging process of function f as an example to detail how to achieve efficient code merging using the method of this invention. First, an AST parsing tool is used to parse the original code block base and the modified code blocks a and b of the two branches, generating corresponding AST models. Then, by encapsulating a custom code graph and supplementing any potentially lost text information, such as comments, we can preserve the complete code context and style features when comparing code graphs. Next, node matching and conflict detection mechanisms are used to identify and handle potential conflicts in the code merging process. Finally, the merged code text is generated by reconstructing the updated code graph, achieving an accurate and complete merging result. Specifically: First, such as Figure 2 As shown, Figure 2This diagram illustrates the working principle of the present invention, specifically showcasing its core process, which encompasses six main steps: generating an Abstract Syntax Tree (AST), encapsulating a custom code graph, establishing correspondences between nodes in the graph, detecting conflicts based on node correspondences, updating the code graph content and marking conflicts, and reconstructing the final code text. The specific operations and implementation methods for each step have been detailed in the first embodiment and will not be repeated here. This process aims to provide an efficient and accurate statement-level code merging solution, ensuring the accuracy and completeness of the merged code.
[0045] Secondly, such as Figure 3 As shown, Figure 3 This is a schematic diagram illustrating the actual process of the third embodiment, showing the complete merging of block functions f in the code. The specific implementation is as follows: This invention first uses an AST parser to parse code blocks base, a, and b, generating corresponding AST-base, AST-a, and AST-b. Then, based on the node information in the ASTs, the function f is encapsulated into a custom code graph structure. During this process, any missing text comments " / / comment" in the ASTs are completed and encapsulated into the code graph. Next, the three code graphs are compared to establish the correspondence between nodes. For example, there are corresponding node pairs with the relationship UPDATE in code graph base and code graph a.<if(a == 2){}, if(a == 1){}> There exist corresponding node pairs with the relationship UPDATE in code graph base and code graph b.<if(a == 2){}, if(a == 3){}> , and the corresponding node pair with the INSERT relationship.<null, / / comment> Next, conflicts are detected by comparing the node correspondences in the base-a and base-b graphs. A node in the base graph (if(a == 2){}) has an UPDATE correspondence with nodes in the base graph (if(a == 1){}) and the base graph (if(a == 3){}), respectively, indicating an UPDATE-UPDATE relationship. Therefore, this node is considered to have a conflicting change. As for the node ( / / comment) in the base graph, its context...<foo(),......> Since it did not overlap with other INSERT nodes, it was a non-conflicting change. Subsequently, the code graph base was updated. For conflicting change nodes (if(a == 2){}), the conflict node content was constructed according to the git conflict format as shown in the figure, and then updated to the code graph base. For non-conflicting change nodes ( / / comment), its corresponding context node was found and inserted into it. Finally, the updated code graph base was converted into code text, i.e., the merged result of function f.
[0046] The advantage of this approach is that by encapsulating a custom code graph, it can complete the text information and style features that may be lost in the AST, thus ensuring the integrity of the information in the merged file. At the same time, the custom code graph has fewer nodes and a more concise structure, making the subsequent matching process more efficient.
[0047] Although the present invention has been disclosed above with reference to preferred embodiments, it is not intended to limit the present invention. Any person skilled in the art can make possible changes and modifications to the technical solutions of the present invention by utilizing the methods and techniques disclosed above without departing from the spirit and scope of the present invention. Therefore, any simple modifications, equivalent changes and alterations made to the above embodiments based on the technical essence of the present invention without departing from the content of the technical solutions of the present invention shall fall within the protection scope of the technical solutions of the present invention.
Claims
1. A semi-structured statement-level code merging method, characterized in that, Includes the following steps: Step S1: Respond to the code merging request; Step S2: Based on the code block to be merged in the code merging request, use the AST tool to parse the original code block and at least two branch modified code blocks of the code block to be merged to generate a structured AST model and encapsulate it into the corresponding code graph; Step S3: Traverse the base nodes of the base code graph corresponding to the original code block, establish the correspondence between the base nodes and the branch nodes in the code graph of the modified code block by matching the nodes through the node association information, perform conflict change detection in the code merging process according to the correspondence, and update the content of the base code graph; Step S4: Convert the updated base code graph into code text, extract the content through the line number information of the base node, and reconstruct the code text according to the corresponding hierarchical structure to complete the code merging request.
2. The statement-level code merging method according to claim 1, characterized in that, Step S2 includes: Step S21: Use the AST tool to parse the code block to be merged to generate the corresponding AST model; start traversing from the root node of the AST model and select nodes at the statement level and above in the AST node information to process; Step S22: Initialize the AST node information in the AST model, including assigning a unique identifier to the AST node, recording the node type and syntax attributes, marking the frame node, and configuring parent and child node pointers. Based on each node selected during traversal, obtain the starting line number start of the node and the ending line number end of the previous node, calculate the difference between start and end, and determine whether the difference is greater than or equal to 1. If the difference is not greater than 1, then the current node is determined not to contain a text comment. Otherwise, it is initially determined that the current node contains the text annotation, and the content of the start to end lines is obtained from the original text to determine whether it contains the annotation text. The content of the text annotation is then encapsulated into the code graph of the current node. Step S23: Encapsulate the current node into the code graph according to the node type. For non-block type nodes, encapsulate them directly as independent nodes; for block type nodes, encapsulate them as composite graph nodes containing at least one child node. For each block type composite graph node, recursively traverse all the child nodes of the composite graph node, and repeat steps S22 and S23 to construct a complete subgraph for each composite graph node and integrate them to construct a complete code graph.
3. The statement-level code merging method according to claim 2, characterized in that, In step S3, the node association information includes content similarity, sibling node information, and parent node information; the correspondence relationship includes complete match, update, move, move and update, insertion, and deletion.
4. The statement-level code merging method according to claim 3, characterized in that, In step S3, the content similarity calculation includes: The AST tool is used to reconstruct the code to unify the format of node content; Determine whether the length of the content of two nodes exceeds a preset threshold, and then calculate the content similarity. When the content of the two nodes does not exceed the preset threshold, the ratio of the length of the longest common substring of the content of the two nodes to the maximum length of the content of the two nodes is calculated as the content similarity. When the content of the two nodes exceeds the preset threshold, the ratio of the Levinstein distance of the content of the two nodes to the maximum value of the length of the content of the two nodes is calculated as the content similarity.
5. The statement-level code merging method according to claim 4, characterized in that, In step S3, node matching is performed using node association information to establish the correspondence between the base node and the branch nodes in the code graph of the branch modification code block, including: Calculate the content similarity between the base node and the branch node, and perform node matching by combining the sibling node information and the parent node information; When the content of the base node and the branch node are the same, it is determined whether the sibling node information and the parent node information are consistent; if the sibling node information and the parent node information are consistent, the correspondence is a complete match; otherwise, if the sibling node information and the parent node information are inconsistent, the correspondence is a move. Determine whether the content similarity between the base node and the branch node exceeds a set threshold, and determine whether the sibling node information and the parent node information are consistent; When the content similarity exceeds the set threshold, and the sibling node information and the parent node information are consistent, the correspondence is updated; When the content similarity exceeds the set threshold, and the sibling node information and the parent node information are inconsistent, the correspondence is the mobile update; If the content similarity does not exceed the set threshold, the branch node cannot match any of the base nodes, the branch node is the insertion node, and the base node is the deletion node.
6. The statement-level code merging method according to claim 5, characterized in that, In step S3, establishing the correspondence between the base node and the branch node in the code graph of the branch modification code block by matching nodes through node association information, further includes: When the correspondence between the base node and the branch node is the move or the move update, the node pair is split and processed, the base node is marked as the deletion, and the branch node is marked as the insertion.
7. The statement-level code merging method according to claim 6, characterized in that, In step S3, the correspondence between the base node and the branch node in the code graph of the branch modification code block is established by matching nodes through node association information. This also includes: checking all the branch nodes marked as inserted; if the nodes are sibling nodes under the same parent node, the sibling nodes are merged into one node; for the identified child nodes under the same parent node, the parent node is used as the child node representative, and the parent node is retained in the code graph while all individual child node insertions are removed.
8. The statement-level code merging method according to claim 7, characterized in that, In step S3, conflict change detection includes: Based on the correspondence between the base node and at least one of the branch nodes, determine whether the node is a conflicting change; When the branch node completely matches the base node, or when the branch nodes delete the same node at the same time, the change is a non-conflicting change. A change is a conflicting change when a change to the base node is inconsistent with a change to any of the branch nodes and / or when at least two of the branch nodes are inserted in the same position.
9. The statement-level code merging method according to claim 8, characterized in that, In step S3, updating the content of the base code graph includes: Based on the nodes involved in non-conflict changes, the nodes marked as inserted are inserted between the corresponding predecessor and successor nodes in the base code graph according to the context position of the nodes; the nodes marked as updated are updated in the corresponding nodes in the base code graph; the nodes marked as deleted are deleted by finding the corresponding nodes in the code graph. Based on the node that has undergone conflict changes, the node marked as the inserted node is constructed in the base code graph using the git conflict format according to the context position of the node, the conflict content is encapsulated into the graph node, and the conflict node is inserted between the corresponding predecessor node and the successor node; for the changed node, the corresponding node is found in the base code graph, and the node content is updated using the git conflict format.
10. A semi-structured statement-level code merging system, executing the statement-level code merging method as described in any one of claims 1 to 9, characterized in that, include: The response module is used to respond to code merging requests; The data processing module is used to, based on the code block to be merged in the code merging request, parse the original code block and at least two branch modification code blocks of the code block to be merged using an AST tool to generate a structured AST model and encapsulate it into a corresponding code graph; traverse the base nodes of the base code graph corresponding to the original code block, establish the correspondence between the base nodes and the branch nodes in the code graph of the branch modification code blocks by matching nodes through node association information, perform conflict change detection in the code merging process according to the correspondence, and update the content of the base code graph; The result output module is used to convert the updated base code graph into code text, extract the content through the line number information of the base node, and reconstruct the code text according to the corresponding hierarchical structure to complete the code merging request.