Method and related device for code analysis
By acquiring and analyzing the attribute information of code blocks in a distributed system, conflicting code blocks can be identified and alerted, solving the problem of poor code conflict detection in existing technologies, improving the efficiency and accuracy of code merging, and supporting conflict management in distributed development.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- HUAWEI CLOUD COMPUTING TECHNOLOGIES CO LTD
- Filing Date
- 2020-12-16
- Publication Date
- 2026-06-12
AI Technical Summary
In distributed program development, the detection and merging of code conflicts is a complex process. Existing AI-based methods are not ideal and are difficult to apply to real-world scenarios, leading to difficulties in version merging.
By acquiring the attribute information of the modified code block, it is determined whether it overlaps with the target code block in the base version, and warnings are issued on the terminal, including pop-up reminders, IDE prompts, and highlighting of conflicting code blocks. Conflict analysis and warnings are performed using the collection agent module and server in the distributed system.
It improves the efficiency and accuracy of code conflict detection, helps developers quickly identify conflicting code blocks, reduces the complexity of version merging and conflict accumulation, and enhances the coordination of collaborative code development.
Smart Images

Figure CN114637510B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of software development technology, and more specifically, to methods and related equipment for code analysis. Background Technology
[0002] Modern software development is often based on distributed environments and is completed collaboratively by multiple people. During collaborative development, programmers working independently and not synchronizing with the main branch for extended periods can lead to a large accumulation of conflicts, which greatly hinders the merging and conflict resolution of later versions.
[0003] To address this issue, the industry has proposed a code conflict detection method based on artificial intelligence (AI). This method trains a random forest binary classification model and collects information from the code (such as the number of files modified simultaneously, the number of commits between earlier and latest commits, and the number of files added, deleted, renamed, modified, and copied in a branch). Using the collected information and the trained random forest model, it determines whether merging two branches will cause a conflict.
[0004] However, this method is not very effective in practical applications and is difficult to apply in real-world scenarios. Summary of the Invention
[0005] This application provides a code analysis method and related equipment that can help developers quickly discover modified code, so that developers can determine whether the modified code will affect the programs they have written.
[0006] In a first aspect, embodiments of this application provide a method for processing code blocks, comprising: obtaining attribute information of a modified code block, the attribute information of the modified code block including the identity information of a modified file and the position of the modified code block in the modified file, wherein the modified code block is located in a terminal of a distributed system, the distributed system including multiple terminals for providing code writing; determining a base version of the modified file based on the identity information of the modified file; determining whether the base version of the modified file includes a target code block whose position overlaps with that of the modified code block based on the position of the modified code block in the modified file; and if the base version of the modified file includes the target code block, issuing an alert to at least one terminal in the distributed system.
[0007] The above technical solution issues a warning in the terminal when it detects an overlap between the location of a code block under development (i.e., the modified code block) and the location of an already developed code block (i.e., the target code block). This allows a programmer (let's call them programmer A) to be aware that a code block has been modified by another programmer (let's call them programmer B). Programmer A can then determine whether the modified code block will affect other parts of the code and decide how to handle it based on the assessment. For example, if the modified code block does not affect other parts of the code, it can be preserved; if it does affect other parts, programmer A can consult with programmer B to determine whether to cancel the modification or assess whether to modify the affected parts of the code.
[0008] The alert can be sent to all terminals involved in code development within the distributed system. This way, all participants can be informed of code modifications.
[0009] The alert can also target a subset of terminals in a distributed system. For example, it could be the terminal used by the author of the target code block. Or, it could be the terminal used by all authors involved in writing the change file.
[0010] Warnings about modifications to the target code block can be displayed via pop-up notifications or as a fixed notification window within the integrated development environment (IDE). The warning content can either present the entire modified code block or only show the location of the modified code block, displaying the entire modified code block when the warning content is selected. Additionally, differences between the modified code block and the target code block can be highlighted.
[0011] In conjunction with the first aspect, in one possible implementation, the terminal in the distributed system provides an alert by: saving the modified code block to a modified code conflict list; and pushing the modified code conflict list to at least one terminal in the distributed system.
[0012] The above technical solution stores conflicting code blocks in a list. This allows for a more intuitive identification of conflicting code blocks when multiple code blocks are plotted in a graph.
[0013] In conjunction with the first aspect, in one possible implementation, the attribute information of the modified code block also includes the author of the modified code block; if the base version includes the target code block, then a warning is issued to the terminal in the distributed system, including: determining that the author of the modified code block is different from the author of the target code block, sending a first warning message to a first target terminal in the distributed system, the first target terminal being a terminal used by the author of the target code block, the first warning message being used to indicate that the target code block has been modified.
[0014] The above technical solution further determines whether the code block has been modified by its original author. If not, it alerts the original author that their code block has been modified by another programmer. This allows the original author to stay informed about the latest developments in the code. Furthermore, since the original author of the target code block is more familiar with it, they can more quickly determine whether modifications made by other programmers have caused problems and / or whether they will affect other parts of the code.
[0015] In conjunction with the first aspect, in one possible implementation, the method further includes: determining whether the target code block overlaps with a specific code block in a code analysis library, the specific code block including a code block for defining functions or a code block for defining variables; if the target code block overlaps with the specific code block, then sending a second warning message to a second target terminal in the distributed system, the code written by the second target terminal including the functions or variables defined by the specific code block, the second warning message being used to indicate that the functions or variables defined by the specific code block have changed.
[0016] Problems with functions and variables can have a significant impact on the entire codebase. Therefore, the above-described technical solution can alert the author of a function or variable when the code block defining that function or variable is modified, indicating a change. This allows the original author to stay informed about the latest code updates. Furthermore, since the author of the original code block (i.e., the target code block) is more familiar with it, they can more quickly determine whether modifications made by other programmers to that code block have caused problems, and / or whether they will affect other parts of the code.
[0017] In conjunction with the first aspect, in one possible implementation, when the target code block overlaps with the specific code block, the method further includes: analyzing the modified code block to determine key information of a first object defined by the modified code block; determining key information of a second object defined by the target code block, wherein the first object and the second object are functions, or the first object and the second object are variables; comparing the key information of the first object and the key information of the second object to determine difference information between the key information of the first object and the key information of the second object, wherein the second warning information is further used to indicate the difference information.
[0018] The above technical solution identifies key information about code modifications by analyzing code blocks. This allows the original author of the code block to be informed of only the key information. The original author can more intuitively identify the modified key information without spending extra time locating the changed code.
[0019] In conjunction with the first aspect, in one possible implementation, when the first object and the second object are functions, the key information includes one or more of the following: the name of the function, the parameters of the function, the return value of the function, the start line of the function, the end line of the function, the class to which the function belongs, the file name of the class to which the function belongs, the function name of another function called by the function, the parameters of the other function, the start line of the other function, the end line of the other function, the class to which the other function belongs, the file name of the class to which the other function belongs, and the type name of the caller of the function; when the first object and the second object are variables, the key information includes one or more of the following: the name of the variable, the start line of the variable, the end line of the variable, the type of the variable, and the value of the variable.
[0020] In conjunction with the first aspect, in one possible implementation, obtaining the attribute information of the modified code block includes: obtaining the attribute information of the modified code block sent by the acquisition agent module of the terminal located in the distributed system.
[0021] Secondly, embodiments of this application provide a computer device that includes units for implementing the first aspect or any possible implementation of the first aspect.
[0022] Thirdly, embodiments of this application provide a computer device including a processor for coupling with a memory to read and execute instructions and / or program code in the memory to perform the first aspect or any possible implementation of the first aspect.
[0023] Fourthly, embodiments of this application provide a computer program product, which includes computer program code. When the computer program code is run on a computer, it causes the computer to perform the first aspect or any possible implementation of the first aspect.
[0024] Fifthly, embodiments of this application provide a computer-readable storage medium storing program code, which, when run on a computer, executes the first aspect or any possible implementation thereof. Attached Figure Description
[0025] Figure 1 This is a system schematic diagram of an embodiment of the present application.
[0026] Figure 2 This is a schematic diagram illustrating an implementation of an embodiment of this application.
[0027] Figure 3 This is a schematic diagram of the data collection agent module.
[0028] Figure 4 This is a schematic flowchart of a method for processing code blocks according to an embodiment of this application.
[0029] Figure 5 This is a schematic structural block diagram of a computer device provided according to an embodiment of this application.
[0030] Figure 6 This is a schematic structural block diagram of a computer device provided according to an embodiment of this application. Detailed Implementation
[0031] The technical solutions in this application will now be described with reference to the accompanying drawings.
[0032] In the embodiments of this application, "program" can refer to software, games, applications (apps), web pages, etc., created by developers using programming languages to write code.
[0033] The system architecture of the distributed development environment described in this application, as well as the commands or functions used to implement various functions, are all described using Git as an example. Git is a free and open-source distributed version control system. Git adopts the approach of distributed version control, which does not require server-side software to operate version control, making the release and exchange of source code extremely convenient.
[0034] It is understandable that, in addition to Git, the technical solution of this application can also be applied to other distributed version control systems that can achieve the same or similar functions as Git.
[0035] Figure 1 This is a schematic diagram of the system architecture applied to an embodiment of this application. For example... Figure 1 As shown, system 100 includes terminal 101, terminal 102, terminal 103, terminal 104, terminal 105, code management device 110, and server 120.
[0036] The following is combined with, for example Figure 1 The system 100 shown provides a brief introduction to the collaborative development process in a distributed environment.
[0037] The code management device 110 stores a code repository (also called a code repository). This code repository stores the code files of the developed programs. Different programmers clone code files from the code repository in the code management device 110 to their terminals through different terminals, and then write code on the files locally.
[0038] For example, suppose programmer 1 uses terminal 101 to write code. In this case, all code files in the code repository of code management device 110 can be cloned to the code repository of terminal 101, and coding can be performed on terminal 101. Suppose programmer 2 uses terminal 102 to write code. In this case, all code files in the code repository of code management device 110 can be cloned to the code repository of terminal 102, and coding can be performed on terminal 102, and so on.
[0039] The code management device 110 can be a device with data storage function, such as a server, a terminal, or other form of computer equipment.
[0040] To distinguish between the code repository on the terminal and the code repository on the code management device, the code repository on the terminal can be called the local repository, and the code repository on the code management device can be called the central code repository or the remote repository.
[0041] In addition to the local repository, the terminal also includes a workspace and a cache.
[0042] Code writing all takes place in the working directory. The cache is where change files added to the working directory are stored. These change files can then be saved to the local repository and pushed to the central code repository.
[0043] For example, programmer 1 writes code for file 1 using terminal 101. Programmer 1 modifies file 1 in the working directory (e.g., adding new code, deleting parts of code, and / or modifying parts of code), resulting in a modified file 1. For clarity, file 1 before modification can be called file 1-a, and the modified file 1 can be called file 1-b. At this point, file 1 in the working directory is file 1-b, file 1 in the local repository is file 1-a, and file 1 in the remote repository is also file 1-a. Programmer 1 can use the `git add` command to add file 1-b to the staging area, then use the `git commit` command to commit file 1-b from the staging area to the local repository, and finally use `git push` to push the contents of the local repository to the central code repository. At this point, file 1 in both the local and central code repositories is updated to file 1-b. After this, if other programmers synchronize with the central code repository, the file 1 in the synchronized local repository will be file 1-b.
[0044] A file may have a current version and a base version. The current version is the version in the working directory, and the base version is the version preceding the current version. Adding, deleting, or modifying code on the base version will yield the current version. Taking file 1 as an example again, the base version of file 1 is file 1-a, and the current version is file 1-b.
[0045] Figure 1 The server 120 shown can perform code analysis to provide early warnings of potential code conflicts that may occur when writing code on various terminals.
[0046] Figure 2 Is it like this? Figure 1 The diagram shows the structure of server 120. Figure 2 As shown, server 120 includes code author analysis module 121, function definition / call relationship analysis module 122, variable definition / call relationship analysis module 123, information collection and storage module 124, conflict early warning analysis module 125, and storage module 126.
[0047] The code author analysis module 121 is used to analyze code files in the central code repository (for example, using the git blame command), determine the author information of each code block in these code files, and store the author information of the code blocks in the code analysis repository.
[0048] It should be understood that in some embodiments, the aforementioned central code repository may also be stored on server 120 for code conflict warning, that is, server 120 both stores the code repository and performs code conflict warning. In this case, the code author analysis module 121 of server 120 directly analyzes the code files in the local code repository.
[0049] In other embodiments, the code repository may also be stored on other devices or in other environments, such as... Figure 1 The code management device 110 is shown. At this time, the code author analysis module 121 of the server 120 accesses the code repository remotely and analyzes the code files in the code repository.
[0050] In some embodiments, the code in a file may be written by multiple programmers, and in this case, each programmer's piece of code has corresponding author information.
[0051] For example, if file 1 contains 20,000 lines of code, with lines 1 to 5,000 written by programmer A, lines 501 to 15,000 written by programmer B, and lines 15,001 to 20,000 written by programmer C, then there can be author information for three code blocks.
[0052] In other embodiments, the code in a file may be written by a single programmer. In this case, the file includes the programmer's author information.
[0053] In other embodiments, the code in multiple files may be written by a single programmer. In this case, the multiple files may have only one author, which is the author information of the programmer who wrote the multiple files.
[0054] The code analysis library stores author information for code blocks that includes not only the author's identity information (such as email address, author's name, or author's mobile phone number, etc.), but also the location information of the code written by that author.
[0055] For example, suppose file 1 contains 20,000 lines of code, where lines 1 to 5,000 were written by programmer A, lines 501 to 15,000 by programmer B, and lines 15,001 to 20,000 by programmer C. Then the author information stored in the code analysis repository could be:
[0056] [file 1;[1:5000];ID A];[file 1;[5001:15000];ID B];[file 1;[15001:20000];ID C],
[0057] Where [file 1; [1:5000]; ID A] means that the author's ID is A for lines 1 to 5000 of file 1; [file 1; [5001:15000]; ID B] means that the author's ID is B for lines 5001 to 15000 of file 1; [file 1; [15001:20000]; ID C] means that the author's ID is C for lines 15001 to 20000 of file 1.
[0058] For example, suppose file 2 was written by programmer A; and files 3 and 4 were written by programmer B. Then the author information stored in the code analysis repository could be:
[0059] [file 2; ID A]; [file 3; ID B]; [file 4; ID C],
[0060] Where [file 2; ID A] represents the author ID of file 2 as A; [file 3; ID B] represents the author ID of file 3 as B; [file 4; ID C] represents the author ID of file 4 as C.
[0061] For example, assuming files 2 and 3 were written by programmer A, and files 4 and 5 were written by programmer B, then the author information stored in the code analysis repository could be:
[0062] [file 2,file 3;;ID A];[file 4,file 5;;ID B];,
[0063] Where [file 2, file 3; ID A] means that the author ID of file 2 and file 3 is A; [file 4, file 5; ID B] means that the author ID of file 4 and file 5 is B.
[0064] In other embodiments, the author information may also include one or more of the following: Commit ID, Repo ID, or Repo Name.
[0065] The function definition / call relationship analysis module 122 is used to perform function relationship analysis on code files in the central code repository, determine key information of functions, and save the determined key information of functions to the code analysis library.
[0066] Similarly, if the central code repository is stored on server 120, the function definition / call relationship analysis module 122 can directly analyze the code files in the local code repository. If the central code repository is stored on code management device 110, the function definition / call relationship analysis module 122 can access the code repository remotely and analyze the code files in the code repository.
[0067] For example, the function definition / call relationship analysis module 122 can use a code parser (such as JDT / CDT) to analyze code files in the central code repository, obtain the corresponding function declarations and function calls in each file, and record key information about the functions. The recorded key information is stored in the function declaration list (FuncDeclList).
[0068] Key information about a function may include one or more of the following: the function's name, the function's parameters, the function's return value, the function's location information (e.g., the function's start and end lines, the function's start line and offset, or the function's end line and offset), the function's class, and the filename of the class to which the function belongs.
[0069] For example, the function definition / call relationship analysis module 122 can also perform type analysis on the caller of the function call, obtain the type name of the caller, and store it in the function call list (FuncInvokeList, function invokinglist). The type name of the caller of the function can also serve as key information about the function.
[0070] For example, the function definition / call relationship analysis module 122 can also traverse the FuncDeclList and FuncInvokeList lists respectively, determine the relationship between function definitions and function calls through function names, parameters, and function caller types, and store this information in the function declaration-call matching information. This information can also serve as key information about the function.
[0071] For example, function A calls function B. Then the function definition / call analysis module 122 can also obtain one or more of the following information as key information for function A: function name of function B, parameters of function B, location information of function B, class of function B, and filename of the class containing function B. Similarly, if function A calls function B, and function B calls function C, then some related information about function C (such as function name, parameters, location information, class, and filename of the class containing function C) can also serve as key information for function A. For ease of description, the information about the functions called above can be collectively referred to as call information.
[0072] In some embodiments, the depth of the call information can be set as needed. For example, suppose function A calls function B, function B calls function C, and function C calls function B. If the depth is 0, then it is not necessary to determine the relevant information of functions B, C, and D (function name, parameters, location information, class, file name of the class, etc.) as key information for function A; if the depth is 1, then the key information of function A can include the relevant information of function B, but does not need to include the relevant information of functions C and D; if the depth is 2, then the key information of function A can include the relevant information of functions B and C, but does not need to include the relevant information of function D; if the depth is 3, then the key information of function A can include the relevant information of functions B, C, and D.
[0073] The analysis results obtained by the function definition / call relationship analysis module 122, such as key information of the function, function declaration list, function call list, and function declaration call matching information, are stored in the code analysis library.
[0074] The variable definition / call relationship analysis module 123 is used to perform variable analysis on code files in the central code repository, determine the key information of the variables, and save the determined key information of the variables to the code analysis library.
[0075] Similarly, if the central code repository is stored on server 120, the variable definition / call relationship analysis module 123 can directly analyze the code files in the local code repository. If the central code repository is stored on code management device 110, the variable definition / call relationship analysis module 123 can access the code repository remotely and analyze the code files in the code repository.
[0076] Key information about a variable may include one or more of the following: the variable's name, the variable's location information (e.g., the variable's start and end lines, the variable's start line and offset, or the variable's end line and offset), the variable's type, or the variable's value.
[0077] Key information about functions and variables can be obtained from an abstract syntax tree (AST).
[0078] The data collection and storage module 124 can be used to obtain the attribute information of the modified code block and save the attribute information of the modified code block to the data collection database.
[0079] The conflict warning analysis module 125 is used to periodically or based on specific events to trigger conflict warning analysis, analyze whether there is a conflict between the content stored in the information collection database and the content in the code analysis database, generate warning information, and send the generated warning information to the corresponding terminal.
[0080] Storage module 126 can be used to store code analysis libraries and collected information libraries. If the central code repository is stored in server 120, then storage module 126 can also be used to store code files in the central code repository.
[0081] In some embodiments, the content stored in storage module 126 may also be stored on other storage devices or storage systems. For example, code analysis libraries and / or data collection libraries may be stored in an object storage service (OBS) in the cloud.
[0082] like Figure 1 Each terminal in the system 100 shown includes a data acquisition agent module that acquires the code in the file being written; determines whether the code in the file being written has changed compared to the code in the corresponding file in the base version; and if a change has occurred, reports it back to the server 120.
[0083] The data collection agent module can collect the code from the file being written according to preset rules.
[0084] In some embodiments, the preset rule may be to collect the code in the file being written at time intervals.
[0085] For example, the data acquisition module can start when the application (e.g., an integrated development environment, IDE) is launched or when the file being written is opened, and start a timer. This timer can have a preset time period. When the timer reaches the preset time period, the code in the file is acquired, and the acquired code is compared with the code in the corresponding file to determine if any changes have occurred.
[0086] In other embodiments, the preset rule may be to collect code from the file being written based on line number.
[0087] For example, the acquisition agent module can detect the number of lines of code being modified in the file being written. If the number of lines of code being modified is equal to a preset line number threshold, the code in the file is acquired, and the acquired code is compared with the code in the corresponding file to determine whether any changes have occurred.
[0088] For example, the data collection agent module can also collect the code in a file when the file being written is closed, compare the collected code with the code in the corresponding file, and determine whether any changes have occurred.
[0089] For example, programmers can also manually trigger the data collection agent module to perform code collection and comparison. For instance, programmers can trigger the agent module to collect code from a file by entering a command or clicking an icon in the IDE, and then compare the collected code with the code in the corresponding file to determine if any changes have occurred.
[0090] Figure 3 This is a schematic diagram of the data acquisition agent module. (For example...) Figure 3 As shown, the acquisition agent module 300 may include a code change extraction unit 301 and a sending unit 302.
[0091] The code change extraction unit 301 in the data acquisition agent module is used to determine whether the acquired code has changed.
[0092] The sending unit 302 is used to send information related to the code change to the server 120 when it is determined that the code has changed.
[0093] Figure 4 This is a schematic flowchart of a code analysis method provided according to an embodiment of this application. Figure 4 The illustrated embodiment assumes that terminal 101's code repository contains base versions of three files, named file A.go, file B.go, and file C.go. Terminal 110's workspace contains current versions of these three files, named file A.go, file B.go, and file C.go. To distinguish between files in the base versions and files in the current versions, we will use A_base to represent the base version file file A.go, B_base to represent the base version file file B.go, C_base to represent the base version file file C.go, A_curr to represent the current version file file A.go, B_cur to represent the current version file file B.go, and C_curr to represent the current version file file C.go.
[0094] Assume that B_cur is being written. For ease of description, we will refer to the file being written as the first object file, the file in the base version corresponding to the first object file as the second object file, and the code block that will be changed as the modified code block. Therefore, the first object file is B_cur, and the second object file is B_base.
[0095] Suppose the contents of the second target file are as follows:
[0096]
[0097] Assume the contents of the first target file are as follows:
[0098]
[0099]
[0100] 401. Server 120 analyzes the code files in the central code repository and obtains the analysis results. For details on the specific analysis process of the code files by Server 120, please refer to... Figure 2 For the sake of brevity, the relevant descriptions in the document will not be repeated here.
[0101] 402, The acquisition agent module 300 in terminal 110 determines the change code module.
[0102] For ease of description, in the description Figure 4 In the illustrated embodiment, the data acquisition agent module 300 mentioned refers to the data acquisition agent module in the terminal 110.
[0103] The acquisition agent module 300, by comparing the first target file and the second target file, can determine that lines 4 and 5 of the code in the first target file are different from lines 4 and 5 of the code in the second target file. Therefore, the acquisition agent module 300 can determine that the changed code block includes lines 4 and 5 of the code in the first target file.
[0104] Furthermore, if a new line of code is inserted between lines 4 and 5 of the first target file, then the changed code block determined by the acquisition agent module 300 includes lines 5 to 22 of the first target file.
[0105] Comparing code between different files can be done using existing comparison methods. For example, the `diff` function in Git can be used to compare the differences between code in two files.
[0106] 403, the data acquisition agent module 300 sends the attribute information of the modified code block back to the server 120. The attribute information of the modified code block may include the identity information of the modified file and the location of the modified code block within that file. The modified file is the file containing the modified code block. In the example above, the modified file is the first target file.
[0107] The identity information of a modified file can include the filename and the path where the modified file is saved. In the example above, the filename is file B.go.
[0108] In some embodiments, the path information of the modified file can be the full path of the folder in terminal 110 where the modified file is stored. For example, if the full path of file B.go in terminal 110 is C:\Git\Study\Project 1\branch1\file B.go, then the full path of the folder where file B.go is stored is C:\Git\Study\Project1\branch 1\, and the path information of the modified file can include the above full path.
[0109] In other embodiments, the path information for the modified file can be a relative path to the folder where the modified file is stored. For example, suppose the full path of the folder where file B.go is stored is C:\Git\Study\Project 1\branch1\, and all files in the code repository containing file B.go are stored in the following path: C:\Git\Study\Project 1. The full path of the code files in this code repository can be divided into two parts: a common path and a relative path. All code files in this code repository are stored in the same common path, but the relative paths of different code files are different. For example, the full path of the folder where file B.go is stored is C:\Git\Study\Project 1\branch 2\. Therefore, the full paths of file A.go and file B.go both include C:\Git\Study\Project 1, C:\Git\Study\Project 1 can be called the common path, the relative path of file A.go is branch 2\, and the relative path of file B.go is branch1\.
[0110] Including the path information of the changed file in the identity information can reduce the occurrence of judgment errors caused by files with the same name.
[0111] The location of the modified code block in the modified file can be indicated in different ways.
[0112] For example, the location of a modified code block within a modified file can be indicated by the start and end lines of that modified code block. Thus, the location of the modified code block can be directly determined based on the start and end lines. Taking the first and second target files mentioned above as examples, if the location of the modified code block is indicated by the start and end lines, the indication information could be [file B.go; C:\Git\Study\Project 1\branch 1\; 4:5]. [file B.go; C:\Git\Study\Project 1\branch 1\; 4:5] indicates that the modified code block is located on lines 4 and 5 of the file file B.go in the path C:\Git\Study\Project 1\branch1. It can be understood that if the modified code is only one line, then the start and end lines in the indication information used to indicate the location of the modified code block are the same.
[0113] For example, the location of a modified code block within a modified file can be indicated by the start line and offset of the modified code block. The sum of the start line and offset is the end line of the modified code block. Thus, the location of the modified code block can be determined using the start line and offset. Taking the first and second target files again as examples, if the location of the modified code block is indicated by the start line and offset, the indication information could be [file B.go; C:\Git\Study\Project 1\branch 1\; 4,1]. [file B.go; C:\Git\Study\Project 1\branch 1\file B.go; 4,1] indicates that the modified code block is located at line 4 to line 4+1 of the file file B.go in the path C:\Git\Study\Project 1\branch 1. It can be understood that if the modified code is only one line long, then the offset value in the indication information used to indicate the location of the modified code block is 0.
[0114] For example, the position of a modified code block in the modified file can also be indicated by the end line and offset value of the modified code block. The method of indicating the position of a modified code block by the end line and offset value is similar to the method of indicating the position of a modified code block by the start line and offset value, and will not be elaborated here for the sake of simplicity.
[0115] It is understood that the formats of the information in the embodiments of this application are illustrative and not specific limitations on the formats.
[0116] In some embodiments, the attribute information of the modified code block may also include the author information of the modified code block. This author information indicates the author of the modified code block. The author information of the modified code block may be author identification information, such as one or more of the following: an email address, the author's name, or the author's mobile phone number.
[0117] For example, the information that the data collection agent module 300 sends back to the server 120 may include the following: DiffHunk = {Hunk info: file B.go; C:\Git\Study\Project 1\branch 1\file B.go; HunkID: 1001; StartLine: 109; EndLine: 120; Version(Base / Current): 1.01 / 1.02; Author: ID A}, where DiffHunk represents the attribute information of the modified code block; Hunk info: file B.go; C:\Git\Study\Project 1\branch 1\file B.go; indicates that the identity information of the modified code block is the file file located at the path C:\Git\Study\Project 1\branch 1. B.go; HunkID:1001 indicates that the identifier (ID) of the code block is changed to 1001; StartLine:109 indicates that the line number of the beginning line of the code block is changed to 109; EndLine:120 indicates that the line number of the end line of the code block is changed to 120; Version(Base / Current):1.01 / 1.02 indicates that the base version number is 1.01 and the current version number is 1.02; Author:ID A indicates that the author ID of the code block is changed to ID A.
[0118] In other embodiments, server 120 can independently determine the modified code block, its location, and author. For example, if the acquisition agent module 300 finds that the contents of the first target file and the second target file are different, it can directly send the first target file to server 120. Server 120 stores files from the base version or obtains the base version file stored in code management device 110 via remote access. Server 120 can determine the second target file from the base version file based on the received first target file, and then determine the modified code block and its location based on the first and second target files. Additionally, the first target file carries the author information of the first target file. Therefore, server 120 can also determine the author of the modified code block based on the author information of the first target file.
[0119] 404. The server 120 obtains the attribute information of the modified code block and determines whether an alert needs to be issued on the terminal in the distributed system.
[0120] In some embodiments, server 120 may determine whether the corresponding file in the base version (i.e., the second target file) includes a target code block that overlaps with the location of the changed code block.
[0121] Positional overlap can occur when the modified code block is a subset of the second object file. In other words, the start and end line numbers of the modified code block are both less than the maximum number of lines of code in the second object file.
[0122] For example, the modified code block starts at line 4 and ends at line 5. The second object file has a total of 22 lines of code. Therefore, it can be determined that the second object file contains an object code block that overlaps with the location of the modified code block.
[0123] Location overlap can also occur when the target code block intersects with the second target file. In other words, the starting line number of the modified code block is less than the maximum line number of the code in the second target file, but the ending line number of the modified code block can be greater than the maximum line number of the code in the second target file.
[0124] For example, the modified code block starts at line 932 and ends at line 1016. The second object file has a total of 1000 lines of code. Therefore, it can be determined that the second object file contains an object code block whose location overlaps with the modified code block.
[0125] In some embodiments, if the server 120 determines that there are target code blocks with overlapping locations, it can push an alert to the terminals in the distributed system.
[0126] In some embodiments, server 120 may save the modified code block to an updated code conflict list and push the updated code conflict list to the terminal. In other embodiments, server 120 may save the location and / or author of the modified code block to the updated code conflict list and push the updated code conflict list to the terminal. In other words, in some embodiments, the updated code conflict list includes the code in the updated code block; in other embodiments, the updated code conflict list may only include the location and / or author of the updated code block.
[0127] In some embodiments, server 120 can push the updated code conflict list to all terminals in the distributed system. This way, all programmers involved in developing the software can know which piece of code has been modified by a particular programmer.
[0128] In other embodiments, server 120 can compare the author of the updated code block with the author of the target code block. If the author of the updated code block and the author of the target code block are different, a first warning message can be sent to the terminal used by the author of the target code block to write the code (hereinafter referred to as the first target terminal). This first warning message indicates that the target code block has been modified. The first warning message may include any one or more of the code in the updated code block, the location of the updated code block, or the author. In this way, the author of the target code block can know that the code he / she wrote has been updated by someone else.
[0129] In other embodiments, server 120 can determine whether the updated code block overlaps with a specific code block based on key information stored in the code analysis library.
[0130] If the code analysis library only stores the key information of functions, then a specific code block is a function in the code analysis library; if the code analysis library only stores the key information of variables, then a specific code block is a variable in the code analysis library; if the code analysis library stores both the key information of functions and the key information of variables, then a specific code block is either a function or a variable in the code analysis library.
[0131] Similarly, whether an update block overlaps with a specific block can be either a complete overlap or a partial overlap.
[0132] For example, the code block that updates is located at lines 719 to 730 of the second object file. The file containing the class of function A is the second object file, and function A is located at lines 719 to 730 of the second object file. Therefore, it can be determined that the code block that updates overlaps with a specific code block.
[0133] For example, the updated code block is located at lines 719 to 730 of the second object file. The file containing the class of function A is the second object file, and function A is located at lines 719 to 830 of the second object file. Therefore, it can be determined that the updated code block overlaps with a specific code block.
[0134] If the updated code block overlaps with a specific code block, it can be determined that an object (function or variable) defined in that specific code block has been modified. In this case, a second warning message can be sent to a second target terminal in the distributed system to indicate that the function or variable defined in that specific code block has changed. This second target terminal may include terminals whose written code includes the function or variable defined in that specific code block. The second target terminal may also include terminals that use and / or reference the function or variable defined in that specific code block.
[0135] In some embodiments, if the updated code block overlaps with a specific code block, the changed code block can be analyzed to obtain key information about the object defined by the changed code block (hereinafter referred to as first key information); key information about the object defined by the specific code block can be obtained from the code analysis library (hereinafter referred to as second key information). The differences between the first key information and the second key information are compared, and the differences are sent to the second target terminal.
[0136] For example, suppose both the modified code block definition and a specific code block define a function, and the modified code block changes the function name and adds a new return value. Then the difference information could include the updated function name and the added return value. In some embodiments, the difference information could also include the function name before the update.
[0137] Figure 5 This is a schematic structural block diagram of a computer device provided according to an embodiment of this application. Figure 5 The computer equipment shown can be, for example, Figure 1 Server 120 in system 100 shown. For example... Figure 5 As shown, the computer device 500 includes an acquisition unit 501, a processing unit 502, and an early warning unit 503.
[0138] The acquisition unit 501 is used to acquire the attribute information of the modified code block. The attribute information of the modified code block includes the identity information of the modified file and the position of the modified code block in the modified file. The modified code block is located in the terminal of the distributed system, which includes multiple terminals for providing code writing.
[0139] Processing unit 502 is used to determine the base version of the change file based on the identity information of the change file.
[0140] The processing unit 502 is further configured to determine, based on the position of the changed code block in the changed file, whether the base version of the changed file includes a target code block whose position overlaps with that of the changed code block.
[0141] The early warning unit 503 is used to issue an early warning to at least one terminal in the distributed system when the processing unit 502 determines that the base version of the change file includes the target code block.
[0142] The specific functions and beneficial effects of the acquisition unit 501, processing unit 502 and early warning unit 503 can be found in the description of the above embodiments, and will not be repeated here for the sake of brevity.
[0143] Figure 6This is a schematic structural block diagram of a computer device according to an embodiment of this application. The computer device 600 includes a bus 601, a processor 602, a communication interface 603, and a memory 604. The processor 602, the memory 604, and the communication interface 603 communicate via the bus 601. The processor 602 may be a field-programmable gate array (FPGA), an application-specific integrated circuit (ASIC), a system-on-chip (SoC), a central processing unit (CPU), a network processor (NP), a digital signal processor (DSP), a microcontroller unit (MCU), a programmable logic device (PLD), other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or other integrated chips. The memory 604 stores executable code included in the pathological image processing system, and the processor 602 reads the executable code from the memory 604 using the method shown in the above embodiment. The memory 604 may also include other software modules required for running processes, such as an operating system. The operating system can be Linux. TM UNIX TM WINDOWS TM wait.
[0144] This application also provides a chip system, including: a logic circuit, the logic circuit being coupled to an input / output interface, through which data is transmitted to perform actions such as... Figure 4 The method described.
[0145] In implementation, each step of the above method can be completed by integrated logic circuits in the processor's hardware or by instructions in software. The steps of the method disclosed in the embodiments of this application can be directly implemented by a hardware processor, or by a combination of hardware and software modules in the processor. The software modules can reside in random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, or other mature storage media in the art. This storage medium is located in memory, and the processor reads information from the memory and, in conjunction with its hardware, completes the steps of the above method. To avoid repetition, detailed descriptions are omitted here.
[0146] It should be noted that the processor in the embodiments of this application can be an integrated circuit chip with signal processing capabilities. In implementation, each step of the above method embodiments can be completed by the integrated logic circuitry in the processor's hardware or by instructions in software form. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the method disclosed in the embodiments of this application can be directly implemented by a hardware decoding processor, or by a combination of hardware and software modules in the decoding processor. The software modules can reside in random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, or other mature storage media in the art. This storage medium is located in memory; the processor reads information from the memory and, in conjunction with its hardware, completes the steps of the above method.
[0147] It is understood that the memory in the embodiments of this application can be volatile memory or non-volatile memory, or may include both volatile and non-volatile memory. The non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. The volatile memory can be random access memory (RAM), which is used as an external cache. By way of example, but not limitation, many forms of RAM are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous linked dynamic random access memory (SLDRAM), and direct rambus RAM (DR RAM). It should be noted that the memory used in the systems and methods described herein is intended to include, but is not limited to, these and any other suitable types of memory.
[0148] According to the method provided in the embodiments of this application, this application also provides a computer program product, which includes: computer program code, which, when run on a computer, causes the computer to execute... Figure 4The method of the illustrated embodiment.
[0149] According to the method provided in the embodiments of this application, this application also provides a computer-readable medium storing program code, which, when run on a computer, causes the computer to perform... Figure 4 The method of the illustrated embodiment.
[0150] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0151] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0152] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.
[0153] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0154] In addition, the functional units in the various embodiments of this application 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.
[0155] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0156] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A method for code analysis, characterized in that, include: Obtain the attribute information of the modified code block, the attribute information of the modified code block including the identity information of the modified file and the position of the modified code block in the modified file, wherein the modified code block is located in the terminal of the distributed system, and the distributed system includes multiple terminals for providing code writing; The base version of the changed file is determined based on the identity information of the changed file; Based on the position of the modified code block in the modified file, determine whether the base version of the modified file includes a target code block that overlaps with the position of the modified code block; If the base version of the modified file includes the target code block, an alert is issued to at least one terminal in the distributed system, wherein issuing an alert to at least one terminal in the distributed system includes: Save the modified code block to the modified code conflict list; The list of conflicting change codes is pushed to at least one terminal in the distributed system.
2. The method as described in claim 1, characterized in that, The attribute information of the modified code block also includes the author of the modified code block; If the base version of the modified file includes the target code block, a warning is issued on the terminal in the distributed system, including: If it is determined that the author of the modified code block is different from the author of the target code block, a first warning message is sent to a first target terminal in the distributed system. The first target terminal is a terminal used by the author of the target code block, and the first warning message is used to indicate that the target code block has been modified.
3. The method as described in claim 1 or 2, characterized in that, The method further includes: Determine whether the target code block overlaps with a specific code block in the code analysis library, the specific code block including a code block for defining a function or a code block for defining a variable; If the target code block overlaps with the specific code block, a second warning message is sent to the second target terminal in the distributed system. The code written by the second target terminal includes functions or variables defined by the specific code block. The second warning message is used to indicate that the functions or variables defined by the specific code block have changed.
4. The method as described in claim 3, characterized in that, In the case where the target code block overlaps with the specific code block, the method further includes: Analyze the modified code block to determine the key information of the first object defined by the modified code block; Determine the key information of the second object defined in the target code block, wherein the first object and the second object are functions, or the first object and the second object are variables; By comparing the key information of the first object and the key information of the second object, the difference information between the key information of the first object and the key information of the second object is determined, wherein the second warning information is also used to indicate the difference information.
5. The method as described in claim 4, characterized in that, When the first object and the second object are functions, the key information includes any one or more of the following: the name of the function, the parameters in the function, the return value of the function, the start line of the function, the end line of the function, the class to which the function belongs, the file name of the class to which the function belongs, the function name of another function called by the function, the parameters in the other function, the start line of the other function, the end line of the other function, the class to which the other function belongs, the file name of the class to which the other function belongs, and the type name of the caller of the function. When the first object and the second object are variables, the key information includes one or more of the following: the name of the variable, the start line of the variable, the end line of the variable, and the type of the variable.
6. The method as described in claim 1 or 2, characterized in that, The process of obtaining the attribute information of the modified code block includes: Obtain the attribute information of the modified code block sent by the acquisition agent module of the terminal located in the distributed system.
7. A computer device, characterized in that, The computer device includes: An acquisition unit is used to acquire attribute information of a modified code block. The attribute information of the modified code block includes the identity information of the modified file and the position of the modified code block in the modified file. The modified code block is located in a terminal of a distributed system, and the distributed system includes multiple terminals for providing code writing. The processing unit is configured to determine the base version of the changed file based on the identity information of the changed file; The processing unit is further configured to determine, based on the position of the modified code block in the modified file, whether the base version of the modified file includes a target code block whose position overlaps with that of the modified code block; An early warning unit is configured to issue an early warning to at least one terminal in the distributed system when the processing unit determines that the base version of the modified file includes the target code block. The warning unit is specifically used to save the modified code block to a modified code conflict list and push the modified code conflict list to at least one terminal in the distributed system.
8. The computer device as claimed in claim 7, characterized in that, The attribute information of the modified code block also includes the author of the modified code block; The processing unit is further configured to determine whether the author of the modified code block is the same as the author of the target code block; The early warning unit is configured to send a first early warning message to a first target terminal in the distributed system when the processing unit determines that the author of the modified code block is different from the author of the target code block. The first target terminal is a terminal used by the author of the target code block, and the first early warning message is used to indicate that the target code block has been modified.
9. The computer device as claimed in claim 8, characterized in that, The processing unit is configured to determine whether the target code block overlaps with a specific code block in the code analysis library when the author of the modified code block is different from the author of the target code block. The specific code block includes a code block for defining a function or a code block for defining a variable. The warning unit is configured to send a second warning message to a second target terminal in the distributed system when the processing unit determines that the target code block overlaps with the specific code block. The code written by the second target terminal includes functions or variables defined by the specific code block, and the second warning message is used to indicate that the functions or variables defined by the specific code block have changed.
10. The computer device as claimed in claim 9, characterized in that, The processing unit is configured to analyze the modified code block and determine key information of the first object defined by the modified code block when the target code block overlaps with the specific code block. Determine the key information of the second object defined in the target code block, wherein the first object and the second object are functions, or the first object and the second object are variables; By comparing the key information of the first object and the key information of the second object, the difference information between the key information of the first object and the key information of the second object is determined, wherein the second warning information is also used to indicate the difference information.
11. The computer device as claimed in claim 10, characterized in that, When the first object and the second object are functions, the key information includes any one or more of the following: the name of the function, the parameters in the function, the return value of the function, the start line of the function, the end line of the function, the class to which the function belongs, the file name of the class to which the function belongs, the function name of another function called by the function, the parameters in the other function, the start line of the other function, the end line of the other function, the class to which the other function belongs, the file name of the class to which the other function belongs, and the type name of the caller of the function. When the first object and the second object are variables, the key information includes one or more of the following: the name of the variable, the start line of the variable, the end line of the variable, the type of the variable, and the value of the variable.
12. The computer device as claimed in any one of claims 7 to 11, characterized in that, The acquisition unit is specifically used to acquire the attribute information of the modified code block sent by the acquisition agent module of the terminal located in the distributed system.
13. A computer device, characterized in that, include: A processor configured to be coupled to memory, read and execute instructions and / or program code in the memory to perform the method as described in any one of claims 1-6.
14. A computer program product, characterized in that, The computer program product includes: computer program code, which, when run on a computer, enables the computer to perform the method as described in any one of claims 1-6.
15. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer program code, which, when executed on a computer, performs the method as described in any one of claims 1-6.
Citation Information
Patent Citations
Intelligent code publishing alarm method based on zookeeper
CN109144562A
Pending merge conflict notification
US20190339964A1
Cited By
Code analysis method and related device
WO2022127557A1