Code review method and device, electronic equipment, storage medium and program product
By converting the code into a syntax tree and comparing the syntax tree before and after the change, identifying logical differences and generating code review results, the inefficiency problem caused by format changes in code review is solved, and the efficiency and accuracy of code review is improved.
Patent Information
- Application Number
- CN202411170655.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-23
- Publication Date
- 2024-12-17
AI Technical Summary
In code review, codes such as Javascript or Typescript lack uniform format requirements, which makes it take a lot of time to change the code format, which reduces the efficiency of code review.
By converting the code into a syntax tree, comparing the syntax tree before and after the change to identify logical differences, generating code review results, ignoring format changes, and prioritizing the logical change part.
It improves the efficiency of code review, reduces the workload of code reviewers, saves comparison time, and ensures the accuracy of comparison results.
Smart Images

Figure CN119149408A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a code review method, device, electronic device, computer storage medium and computer program product. Background Art
[0002] With the development of software development, software testing has become an indispensable process. The underlying logic of software operation relies on the implementation of code, so code review is particularly important.
[0003] In the related art, code reviewers usually review the code based on the code review page of git diff. The code review page will show the difference of each line of code before and after the change, including code format changes such as spaces and line breaks, as well as code logic changes. At this time, the code reviewer will review the code segments with differences line by line.
[0004] However, there is no uniform requirement for code format for codes such as Javascript or Typescript, that is, the same function can be implemented using different code formats; if code reviewers use the above method to review, they will need to spend a lot of time on code format changes, which will increase the workload of code reviewers and lead to low efficiency of code review. Summary of the invention
[0005] In view of the above problems, embodiments of the present application are proposed to provide a code review method, device, electronic device, computer storage medium and computer program product that overcome the above problems or at least partially solve the above problems.
[0006] The technical solution of this application is implemented as follows:
[0007] The present application provides a code review method, the method comprising:
[0008] Obtain the first version code before the change and the second version code after the change;
[0009] Parsing the first version code and the second version code respectively to obtain a first syntax tree and a second syntax tree;
[0010] Determine a first difference code segment and a second difference code segment that are different between the first version code and the second version code, where the first difference code segment corresponds to the second difference code segment;
[0011] Traversing and screening the first syntax tree according to the start and end line numbers of the first difference code segment to obtain a first candidate syntax tree, and traversing and screening the second syntax tree according to the start and end line numbers of the second difference code segment to obtain a second candidate syntax tree;
[0012] The first candidate syntax tree and the second candidate syntax tree are compared to obtain a comparison result; and a code review result is generated according to the comparison result.
[0013] The present application also provides a code review device, which includes:
[0014] An acquisition module, used to acquire a first version code before the change and a second version code after the change;
[0015] A parsing module, used to parse the first version code and the second version code respectively to obtain a first syntax tree and a second syntax tree;
[0016] A traversal and screening module is used to determine a first difference code segment and a second difference code segment where the first version code and the second version code differ, the first difference code segment corresponding to the second difference code segment; traverse and screen the first syntax tree according to the start and end line numbers of the first difference code segment to obtain a first candidate syntax tree, and traverse and screen the second syntax tree according to the start and end line numbers of the second difference code segment to obtain a second candidate syntax tree;
[0017] A comparison module is used to compare the first candidate syntax tree and the second candidate syntax tree to obtain a comparison result; and generate a code review result according to the comparison result.
[0018] An embodiment of the present application provides an electronic device, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the code review method provided by one or more of the aforementioned technical solutions when executing the program.
[0019] An embodiment of the present application provides a computer storage medium, which stores a computer program; after the computer program is executed by a processor, it can implement the code review method provided by one or more of the above-mentioned technical solutions.
[0020] The present application provides a computer program product, including a computer program, which, when executed by a processor, implements the code review method provided by one or more of the aforementioned technical solutions.
[0021] An embodiment of the present application proposes a code review method, device, electronic device, computer storage medium and computer program product, the method comprising: obtaining a first version code before the change and a second version code after the change; parsing the first version code and the second version code respectively to obtain a first syntax tree and a second syntax tree; determining a first difference code segment and a second difference code segment in which the first version code and the second version code are different, the first difference code segment corresponding to the second difference code segment; traversing and filtering the first syntax tree according to the start and end line numbers of the first difference code segment to obtain a first candidate syntax tree, traversing and filtering the second syntax tree according to the start and end line numbers of the second difference code segment to obtain a second candidate syntax tree; comparing the first candidate syntax tree and the second candidate syntax tree to obtain a comparison result; generating a code review result according to the comparison result.
[0022] It can be seen that in the embodiment of the present application, after obtaining the two versions of the code before and after the change, the two versions of the code will be parsed into syntax trees; because the syntax tree can more intuitively reflect the code logic and distinguish the code format well compared to the code, the syntax trees corresponding to the codes before and after the change are compared, which can ensure that the comparison results accurately reflect the logical changes of the code and ignore the changes in the code format, thereby guiding the code reviewers to give priority to reviewing the code segments with logical changes. In this way, the workload of the code reviewers can be reduced and the efficiency of code review can be improved. In addition, after obtaining the syntax trees corresponding to the codes before and after the change, the two syntax trees are traversed and screened by the start and end line numbers of the difference code segments before and after the change, which can save comparison time and further improve processing efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] Figure 1 It is a flowchart of a code review method in an embodiment of the present application;
[0024] Figure 2 is a schematic diagram of a process for determining a candidate syntax tree in an embodiment of the present application;
[0025] Figure 3 It is a flowchart of another code review method in an embodiment of the present application;
[0026] Figure 4 It is a schematic diagram of the composition structure of the code review device in an embodiment of the present application;
[0027] Figure 5 It is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0028] The present application will be further described in detail below in conjunction with the accompanying drawings and Examples. It should be understood that the embodiments provided herein are only used to explain the present application and are not intended to limit the present application. In addition, the embodiments provided below are partial embodiments for implementing the present application, rather than providing all embodiments for implementing the present application. In the absence of conflict, the technical solutions recorded in the embodiments of the present application can be implemented in any combination.
[0029] It should be noted that, in the embodiments of the present application, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a method or apparatus including a series of elements includes not only the elements explicitly recorded, but also includes other elements not explicitly listed, or also includes elements inherent to the implementation of the method or apparatus. In the absence of further restrictions, an element defined by the sentence "includes a ..." does not exclude the presence of other related elements (such as steps in a method or units in a device, for example, a unit may be a portion of a circuit, a portion of a processor, a portion of a program or software, etc.) in the method or apparatus including the element.
[0030] The term "and / or" herein is only a description of the association relationship of the associated objects, indicating that there may be three relationships. For example, I and / or J can represent the three situations that I exists alone, I and J exist at the same time, and J exists alone. In addition, the term "at least one" herein represents any combination of at least two of any one or more of a plurality of. For example, including at least one of I, J, and R can represent including any one or more elements selected from the set consisting of I, J, and R.
[0031] For example, the code review method provided in the embodiment of the present application includes a series of steps, but the code review method provided in the embodiment of the present application is not limited to the recorded steps. Similarly, the code review device provided in the embodiment of the present application includes a series of modules, but the code review device provided in the embodiment of the present application is not limited to including the modules explicitly recorded, and may also include modules required to obtain relevant task data or perform processing based on task data.
[0032] The embodiments of the present application can be applied to a computer system composed of a terminal device and a server, and can operate with many other general or special computing system environments or configurations. Here, the terminal device can be a thin client, a thick client, a handheld or laptop device, a microprocessor-based system, a set-top box, a programmable consumer electronic product, a network personal computer, a small computer system, etc., and the server can be a small computer system, a large computer system, and a distributed cloud computing technology environment including any of the above systems, etc.
[0033] Electronic devices such as terminal devices and servers can implement corresponding functions through the execution of program modules. Generally, program modules can include routines, programs, target programs, components, logic, data structures, etc., which perform specific tasks or implement specific abstract data types. Computer systems / servers can be implemented in a distributed cloud computing environment, in which tasks are performed by remote processing devices linked through a communication network. In a distributed cloud computing environment, program modules can be located on local or remote computing system storage media including storage devices.
[0034] In the related art, Javascript / Typescript and other codes have no unified requirements for code formats such as spaces, line breaks and symbols, that is, the same function can be implemented using different code formats. For example, ab is equivalent to a["b"], and is also equivalent to a['b']. In addition, some codes have no requirements for order. For example, the import statement is used to introduce the definition of object properties, class method definitions and variable declarations. If a developer maintains old code and introduces some new formatting rules during the development process, such as changes in indentation, whether a semicolon is required at the end of a statement, and the order of import from far to near and in alphabetical order, etc., the format of the old code is changed. Git diff will truthfully record the changes in each line of code, causing code reviewers to spend a lot of time on code format changes. This will increase the workload of code reviewers and lead to low efficiency of code review.
[0035] In view of the above technical problems, the following embodiments are proposed.
[0036] In some embodiments of the present application, the code review method can be implemented using a processor in a code review device, and the above-mentioned processor can be at least one of an application-specific integrated circuit (ASIC), a digital signal processor (DSP), a digital signal processing device (DSPD), a programmable logic device (PLD), a field programmable gate array (FPGA), a central processing unit (CPU), a controller, a microcontroller, and a microprocessor.
[0037] Figure 1 is a flowchart of a code review method in an embodiment of the present application, such as Figure 1As shown, the method comprises the following steps:
[0038] Step 100: Obtain a first version code before the change and a second version code after the change.
[0039] In an embodiment of the present application, the code review method can be applied to a scenario where a browser plug-in installed on a terminal is used to accelerate code review by reviewers. The method can be executed by a code review device, which can be configured in an electronic device.
[0040] Exemplarily, the terminal provides a code review page; when the terminal detects a selection operation performed by the reviewer on the file to be reviewed, the code review page is displayed; wherein the file to be reviewed includes a first version code and a second version code. Here, there is no limitation on the code type of the file to be reviewed, for example, it can be a code type with relatively loose code format requirements such as Javascript or Typescript.
[0041] Here, there is no limitation on the execution method of the selected operation. For example, the selected operation may be executed by the git diff command or by other methods. The git diff command is used as an example for explanation below.
[0042] In an embodiment of the present application, the code review page may include relevant information of the first version of the code and relevant information of the second version of the code; illustratively, the relevant information of the first version of the code may include the first version of the code with a review mark and the file path, commit number and other information of the first version of the code; the relevant information of the second version of the code may include the second version of the code with a review mark and the file path, commit number and other information of the second version of the code.
[0043] Exemplarily, the second version code is the code obtained after changing the first version code, and the corresponding file paths of the two are the same; wherein the first version code and the second version code may include multiple code segments, each code segment includes one or more code lines; the multiple code segments of the first version code include different code segments from the second version code; correspondingly, the multiple code segments of the second version code include different code segments from the first version code.
[0044] In an embodiment of the present application, the difference code segment included in the first version code can be referred to as the first difference code segment before the change, and the difference code segment included in the second version code can be referred to as the second difference code segment after the change; it should be noted that the number of first difference code segments and second difference code segments can be one or more, and the two correspond one to one; for example, when the number of first difference code segments and second difference code segments are both one, the second difference code is the code segment obtained after the first difference code segment is changed.
[0045] For example, the commit number refers to a code hash value, which can uniquely identify each changed code; the code usually undergoes multiple changes during the development process, and each change generates a new version of the code; wherein the hash values of the two versions of the code before and after the change are different.
[0046] In the embodiment of the present application, the first version code and the second version code may be adjacent versions of code, wherein the first version code is the previous version of code and the second version code is the current version of code, and the two are distinguished by a code hash value.
[0047] Exemplarily, the review mark is an identification of the difference code segments between the first version of the code and the second version of the code. Here, the content of the review mark is not limited. For example, the review mark may include a highlight mark for the difference code segment and a line number mark for the difference code segment. These marks are built into the code review page displayed based on the git diff command.
[0048] Exemplarily, a browser plug-in is a tool that adds new functions based on the original functions of the browser. After the browser plug-in is installed in the terminal, the browser plug-in can identify the file paths of the first version code and the second version code, as well as their respective commit numbers, from the code review page, and then obtain the first version code before the change and the second version code after the change from the code library according to the file paths and their respective commit numbers.
[0049] Step 101: parse the first version code and the second version code respectively to obtain a first syntax tree and a second syntax tree.
[0050] In an embodiment of the present application, after obtaining the first version code before the change and the second version code after the change according to the above steps, a syntax parsing tool can be used to parse the first version code and the second version code respectively to obtain a first syntax tree and a second syntax tree.
[0051] Exemplarily, a syntax parsing tool is a software tool for parsing input code into a syntax tree or other data structure for more in-depth analysis and processing. Here, the type of syntax parsing tool is not specifically limited, for example, it can be a Babel tool, a Bison tool, or an ANTLR tool.
[0052] In the embodiment of the present application, if the Babel tool is used as a syntax parsing tool, the first syntax tree and the second syntax tree may be abstract syntax trees (Abstract Syntax Tree, AST) in JavaScript Object Notation (JSON) format.
[0053] Exemplarily, taking the first version of the code as an example, the parser part in the Babel tool can be used to decompose the first version of the code into smaller, more manageable parts, and build an AST node for each part; these nodes represent an abstract grammatical structure in the first version of the code, such as variable names, keywords, operators, and function calls, etc. The relationship between the nodes describes the hierarchy and dependencies between them. The reason why the grammar is "abstract" is that the grammar does not represent every detail that appears in the real code. For example, nested parentheses are implicit in the tree structure, and some non-critical characters such as semicolons or parentheses usually do not appear in the AST.
[0054] Step 102: Determine a first difference code segment and a second difference code segment where differences exist between the first version code and the second version code; traverse and filter the first syntax tree according to the start and end line numbers of the first difference code segment to obtain a first candidate syntax tree, and traverse and filter the second syntax tree according to the start and end line numbers of the second difference code segment to obtain a second candidate syntax tree.
[0055] In an embodiment of the present application, the first difference code segment corresponds to the second difference code segment; after obtaining the first syntax tree corresponding to the first version code and the second syntax tree corresponding to the second version code according to the above steps, the first difference code segment and the second difference code segment in which the first version code and the second version code differ can be determined, and then, the first syntax tree is traversed and filtered according to the start and end line numbers of the first difference code segment, and the second syntax tree is traversed and filtered according to the start and end line numbers of the second difference code segment, to obtain a first candidate syntax tree and a second candidate syntax tree.
[0056] Exemplarily, according to the above content, the first difference code segment is the difference code segment in the first version code, the second difference code segment is the difference code segment in the second version code, and the second difference code segment is the code segment obtained after changing the first difference code segment; wherein, the number of the first difference code segment and the second difference code segment can be one or more, and the two correspond one to one; in the case where the number is multiple, the processing method for each first difference code segment and the corresponding second difference code segment is similar; for the convenience of description, the following illustrative explanation is given by taking the case where the number of both is one as an example.
[0057] Exemplarily, the start and end line numbers of the first difference code segment include the start line number and the end line number of the first difference code segment; the start and end line numbers of the second difference code segment include the start line number and the end line number of the second difference code segment; according to the above content, it can be seen that the review identifier in the code review page includes the line number identifier for the difference code segment; here, the line number identifier may include the start and end line numbers of the first difference code segment and the second version of the code, respectively; therefore, in addition to being able to identify the file paths of the first version of the code and the second version of the code, and their respective commit numbers from the code review page, the browser plug-in can also identify the start and end line numbers of the first difference code segment and the second difference code segment, respectively.
[0058] In some embodiments, traversing and filtering the first syntax tree according to the start and end line numbers of the first difference code segment to obtain a first candidate syntax tree may include: in the process of traversing the first syntax tree, determining whether the code line numbers covered by the current traversal node include the start and end line numbers of the first difference code segment; if it is determined to be yes, continuing to determine the next traversal node until a first target node that does not include the start and end line numbers of the first difference code segment is filtered out; and determining the first candidate syntax tree based on each child node contained in the previous traversal node of the first target node.
[0059] In the embodiment of the present application, the first syntax tree includes a plurality of nodes having a hierarchical relationship; the nodes in the first syntax tree can be traversed and screened in order from high to low according to the hierarchical relationship to obtain a first target node, and then, a first candidate syntax tree is determined according to the first target node. Figure 2 This process is described exemplarily.
[0060] For example, see Figure 2 In the process of traversing the nodes in the first syntax tree, determine whether the code line number covered by the current traversal node includes the start and end line numbers of the first difference code segment; if it is determined to be yes, continue to judge the next traversal node until the first target node that does not include the start and end line numbers of the first difference code segment is screened out; if it is determined to be no, determine the current traversal node as the first target node.
[0061] Here, the first target node does not contain the starting and ending line numbers of the first difference code segment, that is, the starting line number of the code line numbers covered by the first target node is greater than the starting line number of the first difference code segment, or the ending line number of the code line numbers covered by the first target node is less than the ending line number of the first difference code segment.
[0062] Further, after obtaining the first target node, the previous traversal node of the first target node can be determined to be the most specific node containing the start and end line numbers of the first difference code segment, that is, the minimum range node. At this time, each child node contained in the previous traversal node can be traversed, and then the first candidate syntax tree can be determined based on these child nodes.
[0063] In some embodiments, traversing and filtering the second syntax tree according to the start and end line numbers of the second difference code segment to obtain a second candidate syntax tree may include: in the process of traversing the second syntax tree, determining whether the code line numbers covered by the current traversal node include the start and end line numbers of the second difference code segment; if it is determined to be yes, continuing to determine the next traversal node until a second target node that does not include the start and end line numbers of the second difference code segment is filtered out; and determining the second candidate syntax tree based on each child node contained in the previous traversal node of the second target node.
[0064] It should be noted that the process of traversing and screening the second syntax tree to obtain the second candidate syntax tree is similar to the process of traversing and screening the first syntax tree to obtain the first candidate syntax tree, and will not be repeated here to avoid repeated description.
[0065] It can be seen that in the embodiment of the present application, after obtaining the syntax trees corresponding to the two versions of the code before and after the change, the two syntax trees are traversed and screened to obtain the first candidate syntax tree and the second candidate syntax tree of the minimum coverage difference code segment. In this way, the comparison time between subsequent syntax trees can be saved, and the influence of other unchanged code segments on the comparison results can be avoided, thereby significantly improving the comparison performance.
[0066] Step 103: Compare the first candidate syntax tree and the second candidate syntax tree to obtain a comparison result; and generate a code review result according to the comparison result.
[0067] In an embodiment of the present application, after obtaining the first candidate syntax tree and the second candidate syntax tree according to the above steps, the first candidate syntax tree and the second candidate syntax tree can be compared to obtain a comparison result between the two, and then, a code review result is generated according to the comparison result.
[0068] In some embodiments, a first candidate syntax tree and a second candidate syntax tree are compared to obtain a comparison result, including: performing non-critical node elimination processing on the first candidate syntax tree and the second candidate syntax tree respectively to obtain a first target syntax tree and a second target syntax tree; and comparing the first target syntax tree and the second target syntax tree to obtain a comparison result.
[0069] Here, non-essential nodes include at least one of the following: format nodes, custom nodes to be removed; illustratively, format nodes include but are not limited to nodes with key values of start, end or loc for blank lines, spaces and line breaks, and nodes with field values of extra for single quotes, double quotes, etc.; custom nodes to be removed may include other nodes entered by code reviewers that need to be additionally removed.
[0070] It can be seen that in the embodiment of the present application, before comparing the first candidate syntax tree and the second candidate syntax tree, by removing non-critical nodes from the two candidate syntax trees, it can be ensured that the comparison results no longer reflect the format changes between the codes before and after the change and the code segments that the code reviewers do not want to pay attention to. In this way, the workload of the code reviewers can be reduced.
[0071] Exemplarily, comparing the first target syntax tree and the second target syntax tree to obtain a comparison result may include: converting the first target syntax tree and the second target syntax tree into first string data and second string data, respectively; and comparing the first string data and the second string data to obtain a comparison result.
[0072] In the embodiment of the present application, after obtaining the first target syntax tree and the second target syntax tree according to the above steps, the first target syntax tree and the second target syntax tree can be converted into first string data and second string data respectively, and then the two string data are compared to obtain a comparison result. The comparison result indicates whether the first string data and the second string data are completely consistent.
[0073] Here, there is no limitation on the implementation method of converting the syntax tree. For example, a syntax parser in a Babel tool may be used to convert the first target syntax tree and the second target syntax tree into first string data and second string data, respectively.
[0074] In some embodiments, a code review result is generated based on the comparison result, including: when the comparison result indicates that the first string data and the second string data are completely consistent, adding a target review identifier, and generating the code review result based on the newly added target review identifier; when the comparison result indicates that the first string data and the second string data are not completely consistent, determining the similarity between the first target syntax tree and the second target syntax tree; and generating the code review result based on the similarity.
[0075] Exemplarily, if the comparison result indicates that the first string data and the second string data are completely consistent, a target review identifier can be added based on the original review identifier, and a code review result can be generated based on the newly added target review identifier; wherein the code review result includes the original review identifier and the target review identifier, and the target review identifier is used to indicate that no logical changes have occurred between the first difference code segment and the second difference code segment; for example, the newly added target review identifier can be: 100% no logical changes.
[0076] In an embodiment of the present application, there is no specific limitation on the position of the target review identifier in the code review result. For example, the target review identifier can be located before or after the line number identifier of the difference code segment; assuming that the original line number identifiers of the first difference code segment and the second difference code segment are: @@-1, 4+1, 5@@, where @@ represents the start and end of the change position of the difference code segment, -1 represents the first line of the first difference code segment, and 4 represents 4 consecutive lines, that is, the first difference code segment is 4 consecutive lines starting from the first line; +1 represents the first line of the second difference code segment, and 5 represents 5 consecutive lines, that is, the second difference code segment is 5 consecutive lines starting from the first line; if a target review identifier "100% no logical change" is added on the basis of the above-mentioned original line number identifier, the current review identifier can be: @@-1, 4+1, 5@@100% no logical change. At this time, the code reviewer can directly determine that no logical change has occurred between the first difference code segment and the second difference code segment based on the current review identifier, thereby realizing a rapid review of the difference code segments between the first version code and the second version code.
[0077] It can be understood that if the first string data and the second string data are completely consistent, by adding a review mark that can indicate that no logical changes have occurred between the difference code segments, it is convenient for code reviewers to prioritize the parts where code logic changes have occurred, thereby saving review time.
[0078] Exemplarily, if the comparison result indicates that the first string data and the second string data are not completely consistent, it means that there is a possibility of a logical change between the first difference code segment and the second difference code segment. At this time, the similarity between the first target syntax tree and the second target syntax tree can be further determined; then, the code review result is generated based on the similarity.
[0079] Here, the similarity can represent the possibility of a logic change between the first difference code segment and the second difference code segment; if the similarity between the two is higher, the possibility of a logic change is lower, and vice versa, if the similarity between the two is lower, the possibility of a logic change is higher. The process of determining the similarity is exemplified below.
[0080] Exemplarily, a preset traversal method can be used to traverse each node of the first target syntax tree and the second target syntax tree respectively, and then all the traversed nodes are flattened into a one-dimensional array in turn to obtain a one-dimensional array corresponding to the first target syntax tree and the second target syntax tree respectively.
[0081] Here, there is no specific limitation on the type of the preset traversal method, for example, it can be a depth-first traversal or a breadth-first traversal.
[0082] Furthermore, after obtaining the one-dimensional arrays corresponding to the first target syntax tree and the second target syntax tree respectively, each node in the two one-dimensional arrays can be compared in turn to obtain a comparison result, and the similarity between the first target syntax tree and the second target syntax tree can be determined based on the comparison result; here, the comparison result represents whether each node in the first target syntax tree and the second target syntax tree is the same.
[0083] For example, the similarity can be calculated by formula (1):
[0084] Similarity = number of identical nodes / total number of nodes * 100% (1)
[0085] Here, the number of identical nodes refers to the number of identical nodes in the first target syntax tree and the second target syntax tree, and the total number of nodes can be the total number of nodes in the first target syntax tree or the total number of nodes in the second target syntax tree, which is not specifically limited here.
[0086] In an embodiment of the present application, when determining the similarity between the first target syntax tree and the second target syntax tree, the similarity can be added as a new review identifier to the original review identifier to generate a code review result, provide a reference basis for code reviewers, and achieve a quick review of the difference code segments between the first version code and the second version code.
[0087] In order to better reflect the purpose of the present application, further examples are given based on the above embodiments of the present application.
[0088] Figure 3 It is a flowchart of another code review method in an embodiment of the present application, which is applied to a browser plug-in. The browser plug-in includes a parsing module and a comparison and recognition module. Figure 3 As shown, the method comprises the following steps:
[0089] First, the browser plug-in obtains input information from the code review page, which are the file path, the commit numbers before and after the change, the start and end line numbers of the first difference code segment, and the start and end line numbers of the second difference code segment.
[0090] Then, the first version code before the change and the second version code after the change are found according to the file path and the commit numbers before and after the change.
[0091] Next, the parsing module uses the Babel tool to parse the obtained first version code and second version code into a first syntax tree and a second syntax tree in JSON format respectively.
[0092] Finally, the first syntax tree, the second syntax tree, the start and end line numbers of the first difference code segment, and the start and end line numbers of the second difference code segment are input into the recognition and comparison module. The module performs the above-mentioned traversal and screening on the two syntax trees in combination with the start and end line numbers of the two difference code segments, and performs operations such as non-critical node elimination, string conversion, and comparison on the two candidate syntax trees obtained by the screening, outputs a review mark to indicate whether a logical change has occurred in the difference code segment, and adds the review mark to the original review mark to generate a code review result; here, the difference code segment includes the above-mentioned first difference code segment and the second difference code segment, and the review mark output by the recognition and comparison module includes the above-mentioned target review mark or the review mark corresponding to the similarity.
[0093] It can be seen that in an embodiment of the present application, a browser plug-in can be used to obtain code-related data of the code review page, identify and extract candidate syntax trees corresponding to the difference code segments before and after the change based on the code-related data, and determine the possibility of logical changes in the difference code segments through comparison operations, thereby helping code reviewers save review time.
[0094] For ease of understanding, Figure 3 Based on this, the process of using the browser plug-in is further explained, and the process may include the following steps:
[0095] Step A1: Install the browser plug-in in the terminal.
[0096] Step A2: The code reviewer opens the code review page.
[0097] Step A3: The browser plug-in identifies the file path of the file to be reviewed, the commit numbers before and after the change, and the start and end line numbers of the difference code segments before and after the change.
[0098] Step A4: The browser plug-in searches for the first version of the code before the change and the second version of the code after the change according to the file path and the commit numbers before and after the change.
[0099] Step A5: With the help of the third-party syntax parsing tool Babel, the first version code and the second version code are parsed into a first syntax tree and a second syntax tree.
[0100] Step A6: The identification and comparison module processes the first syntax tree and the second syntax tree according to the start and end line numbers of the difference code segments before and after the change, and outputs a review mark to indicate whether a logic change occurs in the difference code segments before and after the change.
[0101] Step A7: The browser plug-in adds a review mark output by the recognition comparison module on the basis of the original review mark, and generates a code review result; the code review result is used to explain whether the difference code segment before and after the change has a logical change and the possibility of a logical change.
[0102] It should be noted that, when the original review mark includes a highlight mark of a difference code segment, if the newly added review mark indicates that the difference code segment has not undergone any logical changes, the code reviewer is allowed to customize the highlight mark of the difference code segment to be turned off. At this time, the code reviewer can give priority to reviewing the code portion where the code logic has changed, thereby reducing the workload of the code reviewer.
[0103] Figure 4 Schematic diagram of the structure of the code review device in the embodiment of the present application. Figure 4 As shown, the device includes: an acquisition module 300, a parsing module 301, a traversal and screening module 302 and a comparison module 303, wherein:
[0104] An acquisition module 300 is used to acquire a first version code before the change and a second version code after the change;
[0105] A parsing module 301, used to parse the first version code and the second version code respectively to obtain a first syntax tree and a second syntax tree;
[0106] The traversal and screening module 302 is used to determine the first difference code segment and the second difference code segment where the first version code and the second version code are different, and the first difference code segment corresponds to the second difference code segment; traverse and screen the first syntax tree according to the start and end line numbers of the first difference code segment to obtain a first candidate syntax tree, and traverse and screen the second syntax tree according to the start and end line numbers of the second difference code segment to obtain a second candidate syntax tree;
[0107] The comparison module 303 is used to compare the first candidate syntax tree with the second candidate syntax tree to obtain a comparison result; and generate a code review result according to the comparison result.
[0108] In some embodiments, the traversal screening module 302 is further configured to:
[0109] In the process of traversing the first syntax tree, determining whether the code line number covered by the current traversal node includes the start and end line numbers of the first difference code segment;
[0110] If it is determined to be yes, continue to judge the next traversal node until the first target node that does not contain the start and end line numbers of the first difference code segment is screened out;
[0111] A first candidate syntax tree is determined according to each child node included in the last traversal node of the first target node.
[0112] In some embodiments, the traversal screening module 302 is further configured to:
[0113] In the process of traversing the second syntax tree, determining whether the code line number covered by the current traversal node includes the start and end line numbers of the second difference code segment;
[0114] If it is determined to be yes, continue to judge the next traversal node until a second target node that does not contain the start and end line numbers of the second difference code segment is screened out;
[0115] A second candidate syntax tree is determined according to each child node included in the last traversal node of the second target node.
[0116] In some embodiments, the comparison module 303 is further configured to:
[0117] The first candidate syntax tree and the second candidate syntax tree are respectively subjected to non-key node elimination processing to obtain a first target syntax tree and a second target syntax tree; the non-key nodes include at least one of the following: a format node and a user-defined node to be eliminated;
[0118] The first target syntax tree and the second target syntax tree are compared to obtain a comparison result.
[0119] In some embodiments, the comparison module 303 is further configured to:
[0120] Converting the first target syntax tree and the second target syntax tree into first string data and second string data respectively;
[0121] The first character string data and the second character string data are compared to obtain a comparison result.
[0122] In some embodiments, the comparison module 303 is further configured to:
[0123] When the comparison result indicates that the first character string data and the second character string data are completely consistent, a target review mark is added;
[0124] Generate code review results based on the newly added target review identifier;
[0125] When the comparison result indicates that the first character string data and the second character string data are not completely consistent, determining the similarity between the first target syntax tree and the second target syntax tree;
[0126] Generate code review results based on similarity.
[0127] In practical applications, the acquisition module 300, the parsing module 301, the traversal and screening module 302 and the comparison module 303 can all be implemented by a processor located in an electronic device, and the processor can be at least one of an ASIC, a DSP, a DSPD, a PLD, a FPGA, a CPU, a controller, a microcontroller, and a microprocessor.
[0128] In addition, each functional module in this embodiment can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The above integrated unit can be implemented in the form of hardware or software functional modules.
[0129] If the integrated unit is implemented in the form of a software function module and is not sold or used as an independent commodity, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this embodiment is essentially or the part that contributes to the relevant technology or the whole or part of the technical solution can be embodied in the form of a software commodity. The computer software commodity is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, server, or network device, etc.) or a processor to perform all or part of the steps of the method of this embodiment. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk, etc. Various media that can store program codes.
[0130] Specifically, a computer program instruction corresponding to a code review method in this embodiment can be stored on a storage medium such as a CD, a hard disk, or a USB flash drive. When the computer program instruction corresponding to a code review method in the storage medium is read or executed by an electronic device, any one of the code review methods in the aforementioned embodiments is implemented.
[0131] Based on the same technical concept as the above embodiments, see Figure 5 , which shows an electronic device 400 provided in an embodiment of the present application, which may include: a memory 401 and a processor 402; wherein,
[0132] Memory 401, used for storing computer programs and data;
[0133] The processor 402 is used to execute the computer program stored in the memory to implement any one of the code review methods in the foregoing embodiments.
[0134] In practical applications, the memory 401 may be a volatile memory, such as RAM; or a non-volatile memory, such as ROM, flash memory, hard disk drive (HDD) or solid-state drive (SSD); or a combination of the above types of memory, and provide instructions and data to the processor 402.
[0135] The processor 402 may be at least one of ASIC, DSP, DSPD, PLD, FPGA, CPU, controller, microcontroller, and microprocessor. It is understandable that for different electronic devices, the electronic device used to implement the processor function may also be other, which is not specifically limited in the embodiments of the present application.
[0136] The present application provides a computer program product, including a computer program, which implements any one of the code review methods in the aforementioned embodiments when executed by a processor.
[0137] In some embodiments, the functions or modules included in the device provided in the embodiments of the present application can be used to execute the method described in the above method embodiments. The specific implementation can refer to the description of the above method embodiments. For the sake of brevity, it will not be repeated here.
[0138] The above description of various embodiments tends to emphasize the differences between the various embodiments. The same or similar aspects can be referenced to each other, and for the sake of brevity, they will not be repeated herein.
[0139] The methods disclosed in the various method embodiments provided in this application can be arbitrarily combined without conflict to obtain new method embodiments.
[0140] The features disclosed in the various commodity embodiments provided in this application can be arbitrarily combined without conflict to obtain new commodity embodiments.
[0141] The features disclosed in the various method or device embodiments provided in this application can be arbitrarily combined without conflict to obtain new method embodiments or device embodiments.
[0142] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of hardware embodiments, software embodiments, or embodiments in combination with software and hardware. Moreover, the present application may adopt the form of computer program products implemented on one or more computer-usable storage media (including but not limited to disk storage and optical storage, etc.) that contain computer-usable program codes.
[0143] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0144] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0145] The above are only preferred embodiments of the present application and are not intended to limit the protection scope of the present application.
Claims
1. A code review method, characterized in that: The method comprises: Obtain the first version code before the change and the second version code after the change; Parsing the first version code and the second version code respectively to obtain a first syntax tree and a second syntax tree; Determine a first difference code segment and a second difference code segment that are different between the first version code and the second version code, where the first difference code segment corresponds to the second difference code segment; Traversing and screening the first syntax tree according to the start and end line numbers of the first difference code segment to obtain a first candidate syntax tree, and traversing and screening the second syntax tree according to the start and end line numbers of the second difference code segment to obtain a second candidate syntax tree; The first candidate syntax tree and the second candidate syntax tree are compared to obtain a comparison result; and a code review result is generated according to the comparison result.
2. The method according to claim 1, characterized in that The traversing and screening the first syntax tree according to the start and end line numbers of the first difference code segment to obtain a first candidate syntax tree includes: In the process of traversing the first syntax tree, determining whether the code line number covered by the current traversal node includes the start and end line numbers of the first difference code segment; If it is determined to be yes, continue to judge the next traversal node until a first target node that does not contain the start and end line numbers of the first difference code segment is screened out; The first candidate syntax tree is determined according to each child node included in the last traversal node of the first target node.
3. The method according to claim 1, characterized in that The traversing and screening the second syntax tree according to the start and end line numbers of the second difference code segment to obtain a second candidate syntax tree includes: In the process of traversing the second syntax tree, determining whether the code line number covered by the current traversal node includes the start and end line numbers of the second difference code segment; If it is determined to be yes, continue to judge the next traversal node until a second target node that does not contain the start and end line numbers of the second difference code segment is screened out; The second candidate syntax tree is determined according to each child node included in the last traversal node of the second target node.
4. The method according to any one of claims 1 to 3, characterized in that: The comparing the first candidate syntax tree and the second candidate syntax tree to obtain a comparison result includes: The first candidate syntax tree and the second candidate syntax tree are respectively subjected to non-key node elimination processing to obtain a first target syntax tree and a second target syntax tree; the non-key nodes include at least one of the following: a format node and a user-defined node to be eliminated; The first target syntax tree and the second target syntax tree are compared to obtain the comparison result.
5. The method according to claim 4, characterized in that The comparing the first target syntax tree and the second target syntax tree to obtain the comparison result includes: Convert the first target syntax tree and the second target syntax tree into first character string data and second character string data respectively; The first character string data and the second character string data are compared to obtain the comparison result.
6. The method according to claim 5, characterized in that Generating a code review result according to the comparison result includes: When the comparison result indicates that the first character string data and the second character string data are completely consistent, adding a target review mark; Generate code review results based on the newly added target review identifier; If the comparison result indicates that the first character string data and the second character string data are not completely consistent, determining the similarity between the first target syntax tree and the second target syntax tree; A code review result is generated according to the similarity.
7. A code review device, characterized in that: The device comprises: An acquisition module, used to acquire a first version code before the change and a second version code after the change; A parsing module, used to parse the first version code and the second version code respectively to obtain a first syntax tree and a second syntax tree; A traversal and screening module is used to determine a first difference code segment and a second difference code segment where the first version code and the second version code differ, the first difference code segment corresponding to the second difference code segment; traverse and screen the first syntax tree according to the start and end line numbers of the first difference code segment to obtain a first candidate syntax tree, and traverse and screen the second syntax tree according to the start and end line numbers of the second difference code segment to obtain a second candidate syntax tree; A comparison module is used to compare the first candidate syntax tree and the second candidate syntax tree to obtain a comparison result; and generate a code review result according to the comparison result.
8. An electronic device, characterized in that: The electronic device comprises a memory, a processor and a computer program stored in the memory and executable on the processor, and the processor implements the method according to any one of claims 1 to 6 when executing the program.
9. A computer storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the method according to any one of claims 1 to 6 is implemented.
Citation Information
Cited By
New code generation method and device based on historical code library, storage medium and electronic equipment
CN121387255A
