Disclosure of Invention
The present invention is directed to solving at least one of the problems of the prior art or the related art.
To this end, an object of the present invention is to provide an automatic source code merging method.
Another objective of the present invention is to provide an apparatus for automatically merging source codes.
To achieve at least one of the above objects, according to an embodiment of the first aspect of the present invention, there is provided a source code automatic merging method, including: finding suspected incompatible points in source codes of two different versions; judging whether the suspected incompatible points in the two source codes are code blocks with substantial conflict or not according to a preset compatibility condition; if the suspected incompatible points in the two source codes are code blocks with conflict substantially, selecting a code branch according to the compatibility condition so as to use the selected code blocks in the code branch for merging; and if the suspected incompatible points in the two source codes are code blocks without conflict substantially, selecting any one of the suspected incompatible points for merging.
In the foregoing technical solution, preferably, the step of finding suspected incompatible points in two different versions of source code includes: comparing the code content of corresponding lines between the source codes of two different versions; and if the code content of the corresponding line is different, taking the code content of the corresponding line as the suspected incompatible point.
In any of the above technical solutions, preferably, the compatibility condition includes one or more combinations of a keyword, a merge mode, a code modification time, and a value of an expression.
In any one of the above technical solutions, preferably, when the compatibility condition includes a keyword, the step of determining whether the suspected incompatible point in the two source codes is a code block with a substantial conflict includes: if the suspected incompatible point meets the keyword query condition, determining that the suspected incompatible point is a code block with substantial conflict; if the suspected incompatible point does not satisfy the keyword query condition, determining that the suspected incompatible point is not a code block with substantial conflict, wherein the keyword query condition includes: finding a first type keyword in one code block in the suspected incompatible point, and finding a second type keyword in another code block in the suspected incompatible point; or the first type key word is found in one code block in the suspected incompatible point, and the second type key word is not found in the other code block in the suspected incompatible point; or the second type key word is found in one code block in the suspected incompatible point, and the first type key word is not found in the other code block in the suspected incompatible point.
In any one of the above technical solutions, preferably, when the compatibility condition includes a merge mode, the step of determining whether the suspected incompatibility point in the two source codes is a code block with a substantial conflict includes: judging whether the suspected incompatible point has only one variable of a code block for display initialization; if the suspected incompatible point is judged to have only one code block variable for display initialization, the suspected incompatible point is judged to be a code block with collision substantially; if the suspected incompatible point is judged to be not the variable with only one code block for display initialization, judging whether the suspected incompatible point has only one key statement of the code block and then checking a return value; if the suspected incompatible point is judged to have a return value check after the key statement of only one code block, the suspected incompatible point is judged to be a code block with a substantial conflict, and if the suspected incompatible point is judged to have a return value check after the key statement of only one code block is not judged to have the collision, the suspected incompatible point is judged to be not a code block with a substantial conflict.
In any of the above technical solutions, preferably, the method for automatically merging source codes further includes: detecting whether the compatibility condition is correct; and if the compatibility condition is detected to be incorrect, prompting the user that the compatibility condition is incorrect and/or prompting the user to reset the compatibility condition.
According to an embodiment of the second aspect of the present invention, there is provided a source code automatic merging apparatus, including: the searching unit is used for searching suspected incompatible points in the source codes of the two different versions; a judging unit, configured to judge, according to a preset compatibility condition, whether the suspected incompatible point in the two source codes is a code block with a substantial conflict; a merging unit, configured to select a code branch according to the compatibility condition if the determining unit determines that the suspected incompatible point in the two source codes is a code block with a substantial conflict, so as to merge the code blocks in the selected code branch; and the merging unit is further configured to select any one of the suspected incompatible points for merging if the determining unit determines that the suspected incompatible points in the two source codes are code blocks with substantially no conflict.
In the above technical solution, preferably, the search unit includes: the comparison subunit is used for comparing the code content of the corresponding line between the source codes of the two different versions; and the determining subunit is configured to, if the code content of the corresponding line compared by the comparing subunit is different, take the code content of the corresponding line as the suspected incompatible point.
In any of the above technical solutions, preferably, the compatibility condition includes one or more combinations of a keyword, a merge mode, a code modification time, and a value of an expression.
In any one of the foregoing technical solutions, preferably, in a case that the compatibility condition includes a keyword, the determining unit is specifically configured to determine that the suspected incompatible point is a code block with a substantial conflict if the suspected incompatible point satisfies a keyword query condition; if the suspected incompatible point does not satisfy the keyword query condition, determining that the suspected incompatible point is not a code block with substantial conflict, wherein the keyword query condition includes: finding a first type keyword in one code block in the suspected incompatible point, and finding a second type keyword in another code block in the suspected incompatible point; or the first type key word is found in one code block in the suspected incompatible point, and the second type key word is not found in the other code block in the suspected incompatible point; or the second type key word is found in one code block in the suspected incompatible point, and the first type key word is not found in the other code block in the suspected incompatible point.
In any one of the foregoing technical solutions, preferably, when the compatibility condition includes a merge mode, the determining unit is specifically configured to determine whether only one variable of a code block is present in the suspected incompatible point for initialization; if the suspected incompatible point is judged to have only one code block variable for display initialization, the suspected incompatible point is judged to be a code block with collision substantially; if the suspected incompatible point is judged to be not the variable with only one code block for display initialization, judging whether the suspected incompatible point has only one key statement of the code block and then checking a return value; if the suspected incompatible point is judged to have a return value check after the key statement of only one code block, the suspected incompatible point is judged to be a code block with a substantial conflict, and if the suspected incompatible point is judged to have a return value check after the key statement of only one code block is not judged to have the collision, the suspected incompatible point is judged to be not a code block with a substantial conflict.
In any one of the above technical solutions, preferably, the source code automatic merging apparatus further includes: the detection unit is used for detecting whether the compatibility condition is correct or not; and the prompting unit is used for prompting the user that the compatibility condition is incorrect and/or prompting the user to reset the compatibility condition if the detection unit detects that the compatibility condition is incorrect.
By the technical scheme of the invention, under the condition of not depending on a merging tool to merge the source codes, the problem of code incompatibility during source code merging is solved, and meanwhile, the merging work of the source codes is avoided being interfered manually, so that the merging efficiency of the source codes is improved.
Detailed Description
So that the manner in which the above recited objects, features and advantages of the present invention can be understood in detail, a more particular description of the invention, briefly summarized above, may be had by reference to the embodiments thereof which are illustrated in the appended drawings. It should be noted that the embodiments and features of the embodiments of the present application may be combined with each other without conflict.
In the following description, numerous specific details are set forth in order to provide a thorough understanding of the present invention, however, the present invention may be practiced in other ways than those specifically described herein, and therefore the scope of the present invention is not limited by the specific embodiments disclosed below.
Fig. 1 shows a flow diagram of a source code automatic merging method according to an embodiment of the present invention.
As shown in fig. 1, the method for automatically merging source codes according to an embodiment of the present invention includes:
at step 102, suspected incompatibility points in two different versions of source code are found.
Preferably, step 102 comprises: comparing the code content of corresponding lines between the source codes of two different versions; and if the code content of the corresponding line is different, taking the code content of the corresponding line as the suspected incompatible point.
If the code content of the corresponding line between the two source codes is the same, it is indicated that the code content of the corresponding line is a compatible point, and if the code content of the corresponding line between the two source codes is different, it is indicated that the code content of the corresponding line may be an incompatible point, i.e. a suspected incompatible point.
The suspected incompatible point is a plurality of lines of codes, for example, developers a and B submit a source code named test.c, the source code submitted by a has 80 lines, the source code submitted by B has 100 lines, if the code content of the first 30 lines of the source code submitted by a and the code content of the first 30 lines of the source code submitted by B are the same, the code content of the lines 31 to 50 in the source code submitted by a and the code content of the lines 31 to 70 in the source code submitted by B are different, the code content of the lines 51 to 80 in the source code submitted by a and the code content of the lines 71 to 100 in the source code submitted by B are the same, and the codes of the lines 31 to 50 in the source code submitted by a and the codes of the lines 31 to 70 in the source code submitted by B are regarded as a suspected incompatible point.
The following further describes the suspected incompatibility point by two examples.
For example, a is 5 in the source code developed by the developer a, and a is 4+1 in the corresponding line in the source code developed by the developer B, and it can be seen that the code contents of "a is 5" and "a is 4+ 1" are different, that is, "a is 5" and "a is 4+ 1" are suspected incompatible points.
For another example, the source code developed by the developer a has a value of 5, the source code developed by the developer B has a value of B equal to 5, and the code content of "a equal to 5" and "B equal to 5" are different, that is, "a equal to 5" and "B equal to 5" are suspected incompatible points.
Step 104, according to a preset compatibility condition, determining whether the suspected incompatible point in the two source codes is a code block with substantial conflict, if it is determined that the suspected incompatible point in the two source codes is a code block with substantial conflict, executing step 106, and if it is determined that the suspected incompatible point in the two source codes is a code block without substantial conflict, executing step 108.
Preferably, the compatibility condition includes one or more combinations of keywords, merge patterns, code modification time, and values of expressions.
By the compatibility condition, whether the suspected incompatible point is a code block with substantial conflict or not can be accurately judged.
In one embodiment, in the case that the compatibility condition includes a keyword, step 104 includes: if the suspected incompatible point meets the keyword query condition, determining that the suspected incompatible point is a code block with substantial conflict; if the suspected incompatible point does not satisfy the keyword query condition, determining that the suspected incompatible point is not a code block with substantial conflict, wherein the keyword query condition includes: finding a first type keyword in one code block in the suspected incompatible point, and finding a second type keyword in another code block in the suspected incompatible point; or the first type key word is found in one code block in the suspected incompatible point, and the second type key word is not found in the other code block in the suspected incompatible point; or the second type key word is found in one code block in the suspected incompatible point, and the first type key word is not found in the other code block in the suspected incompatible point.
The first type keywords and the second type keywords are preset keywords, for example, the first type keywords are expected keywords, and the second type keywords are undesired keywords. How to determine whether the suspected incompatible point is a code block with substantial conflict according to the keyword is further described below with reference to fig. 2. The method specifically comprises the following steps:
step 202, setting compatibility conditions as keywords, wherein the keywords comprise expected keywords and unexpected keywords. The desired keyword and the undesired keyword may be one or more keywords. For example, the desired keyword may be set to "ToprowMQ" and the undesired keyword may be set to "WebSphereMQ".
And step 204, comparing the two code blocks of the suspected incompatible point.
Step 206, judging whether the expected keyword is found in one code block of the two code blocks suspected to be incompatible points, and finding the unexpected keyword in the other code block, if so, executing step 208, and if not, executing step 210.
In step 208, the suspected incompatible point is determined to be a code block with substantial conflict.
Step 210, determining whether the expected keyword is found in one of the two code blocks of the suspected incompatible point, and the unexpected keyword is not found in the other code block, if yes, executing step 208, and if no, executing step 212.
In step 212, it is determined whether an unexpected keyword is found in one of the two code blocks of the suspected incompatible point, and the other code block does not find the expected keyword, if the determination result is yes, step 208 is executed, and if the determination result is no, step 214 is executed.
In step 214, the suspected incompatible point is determined as a code block where there is substantially no collision
And step 216, determining whether all the suspected incompatible points are determined, if yes, ending the process, and if no, re-executing step 204 to perform the above-mentioned steps on the next suspected incompatible point.
In one embodiment, in the case that the compatibility condition includes a merge mode, step 104 includes: judging whether the suspected incompatible point has only one variable of a code block for display initialization; if the suspected incompatible point is judged to have only one code block variable for display initialization, the suspected incompatible point is judged to be a code block with collision substantially; if the suspected incompatible point is judged to be not the variable with only one code block for display initialization, judging whether the suspected incompatible point has only one key statement of the code block and then checking a return value; if the suspected incompatible point is judged to have a return value check after the key statement of only one code block, the suspected incompatible point is judged to be a code block with a substantial conflict, and if the suspected incompatible point is judged to have a return value check after the key statement of only one code block is not judged to have the collision, the suspected incompatible point is judged to be not a code block with a substantial conflict.
How to determine whether the suspected incompatible point is a code block with substantial conflict according to the merge mode is further described below with reference to fig. 3. The method specifically comprises the following steps:
step 302, set the compatibility condition as a merge mode.
Step 304, comparing the two code blocks of the suspected incompatible point.
Step 306, judging that only one variable of the two code blocks of the suspected incompatible point is displayed and initialized, if the judgment result is yes, the step 308 is carried out, and if the judgment result is no, the step 310 is carried out.
In step 308, the suspected incompatible point is determined to be a code block with substantial conflict.
In step 310, a return value check is performed after the key statement of only one code block in the two code blocks of the suspected incompatible point is judged, if the judgment result is yes, the step 308 is performed, and if the judgment result is no, the step 312 is performed.
At step 312, the suspected incompatible point is determined to be a code block where there is substantially no collision.
And step 314, determining whether all the suspected incompatible points are determined, if yes, ending the process, and if no, re-executing step 304 to perform the processing of the above steps on the next suspected incompatible point.
In one embodiment, step 104 includes: if the positions of the two code blocks of the suspected incompatible point are different, the suspected incompatible point is determined to be a code block with substantial conflict.
In general, multiple pieces of code can be written within a line in any language (e.g., C, Java), such as:
int a;
a=5;a=a+1;
the above code block and the following code block are equivalent:
int a;
a=5;
a=a+1;
although the above two code blocks have the same meaning, the positions of the two code blocks are different from each other, and it is determined that the point of suspected incompatibility is a code block in which there is a substantial collision.
In one embodiment, where the compatibility condition comprises a code modification time, step 104 comprises: and if the modification time of the two code blocks of the suspected incompatible point is different, determining that the suspected incompatible point is a code block with substantial conflict. Even if the contents of two code blocks of a suspected incompatible point are completely identical, if the modification times are not identical, the suspected incompatible point is considered to be a code block in which there is a substantial conflict.
In one embodiment, where the compatibility condition comprises a value of an expression, step 104 comprises: and respectively calculating expressions in two code blocks in the suspected incompatible point, judging whether the values of the expressions in the two calculated code blocks are consistent, if so, judging that the suspected incompatible point is a code block without substantial conflict, and if not, judging that the suspected incompatible point is a code block with substantial conflict.
For example, one code block in a suspected incompatible point is int number-5; the other code block is 4+ 1; the values of these two expressions are identical, i.e., the suspected incompatibility point is considered to be a code block for which there is substantially no conflict.
And 106, selecting a code branch according to the compatibility condition so as to use the code block in the selected code branch for merging.
And if the suspected incompatible points in the two source codes are judged to be code blocks with conflict substantially, selecting a code branch according to the compatibility condition so as to use the code blocks in the selected code branch for combination. For example, when the compatibility condition is the code modification time, a code block with a new code modification time or an old code modification time may be selected; when the compatibility condition is a keyword, a code block containing "desired keyword" or a code block not containing "undesired keyword" may be selected; when the compatibility condition is a merge mode, a code block of a strict mode or a relaxed mode may be selected. For example, developers a and B both submit source code named test.c, the code content of lines 31 to 50 in the source code submitted by a is different from the code content of lines 31 to 70 in the source code submitted by B, only some operations are performed in lines 31 to 50 in the source code of a, and not only some operations but also return value check after a critical statement is performed in lines 31 to 70 in the source code of B. It can be seen that the code blocks of lines 31-70 in the source code of B belong to the strict mode, and the code blocks of lines 31-50 in the source code of a belong to the relaxed mode. When merging source codes, a code block may be selected according to whether a merging mode is a strict mode or a loose mode.
And step 108, selecting any code block in the suspected incompatible points for combination.
If the suspected incompatible points in the two source codes are determined to be code blocks with substantially no conflict, any one of the suspected incompatible points can be selected for merging.
In the technical solution, if a suspected incompatible point exists in two different versions of source codes, it is further determined that the suspected incompatible point is a code block that substantially has a conflict, a corresponding code branch is selected according to a preset compatibility condition, so as to use the code blocks in the selected code branch for merging. According to the scheme, under the condition that source code merging is carried out without depending on a merging tool, the problem of code incompatibility during source code merging is solved, meanwhile, merging work of manual intervention source codes is avoided, errors caused by manual intervention during source code merging are avoided, and therefore merging efficiency and merging accuracy of the source codes are improved.
The "source code", "code block", and "code branch" are explained below. "Source code" refers to all programs in a submission; "code block" refers to a portion of source code, e.g., lines of source code; "code branching" refers to different versions of a source code submission where a block of code is part of the source code.
In the foregoing technical solution, preferably, the method for automatically merging source codes further includes: detecting whether the compatibility condition is correct; and if the compatibility condition is detected to be incorrect, prompting the user that the compatibility condition is incorrect and/or prompting the user to reset the compatibility condition.
Because the set compatibility condition may be incorrect, it is necessary to detect whether the compatibility condition is correct, and further ensure the accuracy and reliability of the source code merging.
For example, the compatibility condition is a keyword, the item name of the value of the keyword is "ABC", but the keyword "ABC" is not present in the source code of two different versions, which indicates that the compatibility condition is set incorrectly. In addition, after the compatibility condition is set correctly, the source code merging executed thereafter generally does not have the condition that the compatibility condition is incorrect.
Fig. 4 is a schematic structural diagram of an automatic source code merging apparatus according to an embodiment of the present invention.
As shown in fig. 4, the source code automatic merging apparatus 400 according to an embodiment of the present invention includes: a lookup unit 402, a judgment unit 404 and a merging unit 406.
A finding unit 402 for finding suspected incompatible points in two different versions of the source code.
Preferably, the searching unit 402 comprises: a comparing subunit 4022, configured to compare code contents of a corresponding line between the source codes of the two different versions; a determining subunit 4024, configured to, if the code content of the corresponding line compared by the comparing subunit 4022 is different, take the code content of the corresponding line as the suspected incompatible point.
If the code content of the corresponding line between the two source codes is the same, it is indicated that the code content of the corresponding line is a compatible point, and if the code content of the corresponding line between the two source codes is different, it is indicated that the code content of the corresponding line may be an incompatible point, i.e. a suspected incompatible point.
A determining unit 404, configured to determine, according to a preset compatibility condition, whether the suspected incompatible point in the two source codes is a code block with a substantial conflict.
Preferably, the compatibility condition includes: the keyword, merge pattern code modification time, or value of an expression.
Preferably, in a case that the compatibility condition includes a keyword, the determining unit 404 is specifically configured to determine that the suspected incompatible point is a code block with a substantial conflict if the suspected incompatible point satisfies the keyword query condition; if the suspected incompatible point does not satisfy the keyword query condition, determining that the suspected incompatible point is not a code block with substantial conflict, wherein the keyword query condition includes: finding a first type keyword in one code block in the suspected incompatible point, and finding a second type keyword in another code block in the suspected incompatible point; or the first type key word is found in one code block in the suspected incompatible point, and the second type key word is not found in the other code block in the suspected incompatible point; or the second type key word is found in one code block in the suspected incompatible point, and the first type key word is not found in the other code block in the suspected incompatible point.
Preferably, when the compatibility condition includes a merge mode, the determining unit 404 is specifically configured to determine whether only one variable of a code block is present in the suspected incompatible point for initialization of display; if the suspected incompatible point is judged to have only one code block variable for display initialization, the suspected incompatible point is judged to be a code block with collision substantially; if the suspected incompatible point is judged to be not the variable with only one code block for display initialization, judging whether the suspected incompatible point has only one key statement of the code block and then checking a return value; if the suspected incompatible point is judged to have a return value check after the key statement of only one code block, the suspected incompatible point is judged to be a code block with a substantial conflict, and if the suspected incompatible point is judged to have a return value check after the key statement of only one code block is not judged to have the collision, the suspected incompatible point is judged to be not a code block with a substantial conflict.
A merging unit 406, configured to, if the determining unit 404 determines that the suspected incompatible points in the two source codes are code blocks with substantial conflicts, select a code branch according to the compatibility condition, so as to merge the code blocks in the selected code branch; and the merging unit 406 is further configured to select any one code block of the suspected incompatible points for merging if the determining unit 404 determines that the suspected incompatible points in the two source codes are code blocks with substantially no conflict.
In the technical solution, if a suspected incompatible point exists in two different versions of source codes, it is further determined that the suspected incompatible point is a code block that substantially has a conflict, a corresponding code branch is selected according to a preset compatibility condition, so as to use the code blocks in the selected code branch for merging. According to the scheme, under the condition that source code merging is carried out without depending on a merging tool, the problem of code incompatibility during source code merging is solved, meanwhile, merging work of manual intervention source codes is avoided, errors caused by manual intervention during source code merging are avoided, and therefore merging efficiency and merging accuracy of the source codes are improved.
In any of the above technical solutions, preferably, the apparatus 400 for automatically merging source codes further includes: a detecting unit 408, configured to detect whether the compatibility condition is correct; a prompting unit 410, configured to prompt the user that the compatibility condition is incorrect and/or prompt the user to reset the compatibility condition if the detecting unit 408 detects that the compatibility condition is incorrect.
Because the principle of solving the problem of the automatic source code merging device is similar to that of the automatic source code merging method, the implementation of the device can refer to the implementation of the method, and repeated details are not repeated.
The technical scheme of the invention is explained in detail in the above with reference to the attached drawings, and by the technical scheme of the invention, under the condition of not depending on a merging tool to merge the source codes, the problem of code incompatibility during source code merging is solved, and meanwhile, the merging work of manual intervention source codes is avoided, so that the merging efficiency of the source codes is improved.
In the present invention, the terms "first" and "second" are used for descriptive purposes only and are not to be construed as indicating or implying relative importance. The specific meanings of the above terms in the present invention can be understood by those skilled in the art according to specific situations.
The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention, and various modifications and changes may be made by those skilled in the art. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.