Code map construction method, device, electronic device and computer storage medium
By building code graphs, obtaining and managing the directory tree and file entity relationships of the code repository, the problem of code repository construction and management in the existing technology is solved, and a global overview and rapid retrieval of the code repository is realized, and a variety of AI applications are supported.
Patent Information
- Application Number
- CN202411617322.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-13
- Publication Date
- 2025-03-25
- Estimated Expiration
- 2044-11-13
AI Technical Summary
The existing technology is difficult to effectively build and manage code bases, which limits the expansion space for AI to apply downstream of code databases.
By building a code map, obtaining the directory tree and file entity relationship of the target code repository, and achieving an overall overview and cross-file understanding of the code repository.
It facilitates developers to quickly grasp the content and hierarchy of the code repository, and supports downstream applications such as code retrieval, analysis, recommendation and Q&A.
Smart Images

Figure CN119149757B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a code graph construction method, device, electronic device and computer storage medium. Background Art
[0002] As a kind of knowledge asset, the construction of code bases and applications based on code bases are becoming increasingly important in order to give full play to the role of existing code assets. By building a better code storage and code management database, AI applications downstream of the code database have a wide range of expansion space, such as code retrieval, analysis, recommendation, question and answer, etc. Summary of the invention
[0003] The embodiments of the present application provide a code graph construction method, device, electronic device and computer storage medium. By constructing a code graph, an overall overview and cross-file understanding of the target code repository are achieved, which facilitates developers to grasp the content and hierarchical structure of the target code repository more quickly and provides a basis for downstream applications.
[0004] In a first aspect, an embodiment of the present application provides a method for constructing a code graph, including:
[0005] Obtain a target code repository; the target code repository includes a target directory and a target file; generate a target directory tree based on the target directory; the target directory tree is used to characterize the relationship between the target directory and the target file; extract the first target entity and the first target entity relationship in the target file; construct a target code graph corresponding to the target code repository based on the target directory tree, the first target entity and the first target entity relationship.
[0006] In a possible implementation, the target directory tree is generated according to the target directory, including: obtaining a second target entity and a relationship between the second target entity according to the target directory; the second target entity includes at least one of the following: a directory name, a file name; the second target entity relationship includes: an inclusion relationship; and generating a target directory tree according to the second target entity and the second target entity relationship.
[0007] In a possible implementation, the target file includes a target code file and / or a target other file; the first target entity includes at least one of the following: a target code entity corresponding to the target code file and a target other entity corresponding to the target other file; the first target entity relationship includes at least one of the following: a target code entity relationship corresponding to the target code file and a target other entity relationship corresponding to the target other file.
[0008] In a possible implementation, the above-mentioned extraction of the first target entity and the first target entity relationship in the above-mentioned target file includes: when the above-mentioned target file includes the above-mentioned target code file, parsing the above-mentioned target code file to obtain the target syntax tree, and extracting the above-mentioned target code entity and the above-mentioned target code entity relationship from the above-mentioned target syntax tree; when the above-mentioned target file includes the above-mentioned target other files, using the target big model to extract the above-mentioned target other entities and the above-mentioned target other entity relationships from the above-mentioned target other files.
[0009] In a possible implementation, the above-mentioned use of the target big model to extract the above-mentioned target other entities and the above-mentioned target other entity relationships from the above-mentioned target other files includes: slicing the above-mentioned target other files to obtain multiple target other sub-files; inputting the above-mentioned target other sub-files into the target big model, and outputting the above-mentioned target other entities and the above-mentioned target other entity relationships in the above-mentioned target other sub-files.
[0010] In a possible implementation, before parsing the target code file to obtain the target syntax tree, the method further includes: when the target file includes the target code file, determining whether the target code file supports parsing; if the target code file supports parsing, executing the step of parsing the target code file to obtain the target syntax tree.
[0011] In a possible implementation, when the target file includes the target code file, after determining whether the target code file supports parsing, the method further includes: if the target code file does not support parsing, extracting the target code entity and the target code entity relationship from the target code file using the target macro model.
[0012] In a possible implementation, the target code graph corresponding to the target code repository is constructed based on the target directory tree, the first target entity and the first target entity relationship, including: constructing a target directory sub-graph based on the target directory tree; constructing a target file sub-graph corresponding to each of the target files based on the first target entity in each of the target files and the first target entity relationship; and constructing a target code graph corresponding to the target code repository based on the target directory sub-graph and the target file sub-graph.
[0013] In a possible implementation, before extracting the first target entity and the first target entity relationship in the target file, the method further includes: determining whether there is a corresponding cached target file sub-graph for the target file; if there is no corresponding cached target file sub-graph for the target file, executing the step of extracting the first target entity and the first target entity relationship in the target file.
[0014] In a possible implementation, the target code graph is composed of multiple target sub-graphs, and the multiple target sub-graphs include a target directory sub-graph corresponding to the target directory and a target file sub-graph corresponding to each of the target files in the target code repository; after constructing the target code graph corresponding to the target code repository according to the target directory tree, the first target entity and the relationship between the first target entities, the method further includes: after detecting that the target code repository is updated, updating the specified target sub-graph according to the target update information corresponding to the target code repository; and updating the target code graph according to the updated specified target sub-graph.
[0015] In one possible implementation, the programming language used by the above-mentioned target code file includes at least one of the following: elm, javascript, ql, ocaml, elixir, typescript, go, c, elisp, php, r, python, java, ruby, rust, c++, c#.
[0016] In a possible implementation, the target code entity includes at least one of the following: a class, a function, and a variable; and the target code entity relationship includes a calling relationship.
[0017] In a possible implementation, after constructing the target code map corresponding to the target code repository according to the target directory tree, the first target entity and the relationship between the first target entities, the method further includes: converting the target code map into target base data; and saving the target base data in a local Sqlite database.
[0018] In a possible implementation, the above-mentioned obtaining of the target code repository includes: obtaining a code repository path; when the above-mentioned code repository path is a local code path, determining whether the code repository corresponding to the above-mentioned code repository path is a git repository; if the code repository corresponding to the above-mentioned code repository path is a git repository, determining the above-mentioned code repository as the target code repository.
[0019] In a possible implementation, when the code repository path is a local code path, after determining whether the code repository corresponding to the code repository path is a git repository, the method further includes: if the code repository corresponding to the code repository path is not a git repository, initializing the code repository corresponding to the code repository path to obtain the target code repository.
[0020] In a possible implementation, the obtaining of the target code repository includes: obtaining a code repository path; and when the code repository path is a remote repository path, cloning the code repository path to obtain the target code repository.
[0021] In a possible implementation, the method further includes: receiving a target instruction; the target instruction carries target task information related to the target code repository; in response to the target instruction, executing the target task corresponding to the target task information based on the target code graph to obtain a target execution result.
[0022] In a second aspect, an embodiment of the present application provides a code graph construction device, including:
[0023] Get the module and get the target code repository; the target code repository includes the target directory and target files;
[0024] A generating module generates a target directory tree according to the target directory; the target directory tree is used to represent the relationship between the target directory and the target file;
[0025] An extraction module, extracting the first target entity and the first target entity relationship in the target file;
[0026] A construction module is provided to construct a target code graph corresponding to the target code repository according to the target directory tree, the first target entity and the relationship between the first target entities.
[0027] In a third aspect, an embodiment of the present application provides an electronic device, comprising: a processor and a memory; wherein the memory stores executable program code, and the processor runs a program corresponding to the executable program code by reading the executable program code stored in the memory, so as to execute the method steps provided in the first aspect of the embodiment of the present application.
[0028] In a fourth aspect, an embodiment of the present application provides a computer storage medium, wherein the computer storage medium stores a plurality of instructions, and the instructions are suitable for being loaded by a processor and executing the method steps provided in the first aspect of the embodiment of the present application.
[0029] The beneficial effects brought about by the technical solutions provided by some embodiments of the present application include at least:
[0030] In an embodiment of the present application, on the one hand, by generating a target directory tree according to the target directory in the target code repository, and using the target directory tree to show the relationship between the target directory and the target file, it is possible to extract the information of the target code repository from a macro perspective; on the other hand, by performing a refined analysis of the target files in the target code repository, the target entities and target relationships in the target files are extracted, and the detailed content information of the target code repository is extracted from a local perspective; on the other hand, a target code map is constructed based on the above two aspects, and an overall overview and cross-file understanding of the target code repository are achieved through the target code map, and information of different granularities of the target code repository is obtained and analyzed and integrated from a macro and local perspective, respectively, so that developers can grasp the content and hierarchical structure of the target code repository more quickly, and achieve a global overview and quick retrieval of the target code repository. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0032] Figure 1 A schematic diagram of the architecture of a code graph construction system provided for an exemplary embodiment of the present application;
[0033] Figure 2 A flowchart of a code map construction method provided for an exemplary embodiment of the present application;
[0034] Figure 3 A schematic diagram of a target directory tree diagram provided for an exemplary embodiment of the present application;
[0035] Figure 4 A schematic diagram of a target code map provided for an exemplary embodiment of the present application;
[0036] Figure 5 A flowchart of another code map construction method provided for an exemplary embodiment of the present application;
[0037] Figure 6 A flowchart of another code map construction method provided for an exemplary embodiment of the present application;
[0038] Figure 7 A schematic diagram of the application process of a code knowledge graph provided for an exemplary embodiment of the present application;
[0039] Figure 8 A schematic diagram of an implementation process of a code map construction method provided by an exemplary embodiment of the present application;
[0040] Fig. 9 A schematic diagram of the structure of a code map construction device provided by an exemplary embodiment of the present application;
[0041] Fig.10 A schematic structural diagram of an electronic device provided as an exemplary embodiment of the present application. DETAILED DESCRIPTION
[0042] In order to make the features and advantages of the present application more obvious and easy to understand, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present application.
[0043] The terms "first", "second", "third", etc. in the specification and claims of this application and the above-mentioned drawings are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but optionally includes steps or units that are not listed, or optionally includes other steps or units inherent to these processes, methods, products or devices.
[0044] Please refer to the following Figure 1 , which is a schematic diagram of the architecture of a code graph construction system provided by an exemplary embodiment of this specification. Figure 1 As shown, the code graph construction system may include: a terminal 110 and a server 120. Among them:
[0045] The terminal 110 can interact with the server 120 through the network to receive messages from the server 120 or send messages to the server 120. The terminal 110 can be hardware or software. When the terminal 110 is hardware, it can be various electronic devices, including but not limited to smart watches, smart phones, tablet computers, laptop portable computers and desktop computers. When the terminal 110 is software, it can be installed in the electronic devices listed above, which can be implemented as multiple software or software modules (for example: used to provide distributed services), or it can be implemented as a single software or software module, which is not specifically limited here.
[0046] In the embodiment of the present specification, the terminal 110 may be a user terminal corresponding to a user such as a developer who needs to construct a code graph. When a user wants to construct a code graph for a target code repository, the user may first obtain the target code repository through the terminal 110, but is not limited to the target code repository including a target directory and a target file; then, a target directory tree is generated according to the target directory, and the target directory tree is used to represent the relationship between the target directory and the target file; and the first target entity and the first target entity relationship in the target file are extracted; finally, the target code graph corresponding to the target code repository is constructed according to the target directory tree, the first target entity and the first target entity relationship.
[0047] The server 120 may be a server that provides various code graph building functions. It should be noted that the server 120 may be hardware or software. When the server 120 is hardware, it may be implemented as a distributed server cluster consisting of multiple servers, or it may be implemented as a single server. When the server 120 is software, it may be implemented as multiple software or software modules (for example, used to provide distributed services), or it may be implemented as a single software or software module, which is not specifically limited here. The server 120 may be, but is not limited to, a hardware server, a virtual server, a cloud server, etc.
[0048] Optionally, after receiving the code graph construction instruction sent by the terminal 110 through the network, the server 120 can also construct a target code graph corresponding to the target code repository through the code graph construction method provided in the embodiment of the present application.
[0049] It can be understood that the code graph construction method provided in the embodiment of the present application can be executed by the terminal 110 alone, by the server 120 alone, or by the terminal 110 and the server 120 together, and the embodiment of the present application is not limited to this.
[0050] The network may be a medium providing a communication link between the terminal 110 and the server 120, or may be the Internet including network devices and transmission media, but is not limited thereto. The transmission media may be a wired link, such as but not limited to coaxial cable, optical fiber, and digital subscriber line (DSL), or a wireless link, such as but not limited to wireless fidelity (WIFI), Hypertext Transfer Protocol (HTTP), Bluetooth, and mobile device network, etc.
[0051] Understandably, Figure 1The number of terminals 110 and servers 120 in the code graph construction system shown is only an example. In a specific implementation, the code graph construction system may include any number of terminals 110 and servers 120.
[0052] Next, combine Figure 1 , taking the server 120 executing the code map construction as an example, the code map construction method provided by an exemplary embodiment of the present application is introduced. For details, please refer to Figure 2 , which exemplarily shows a flow chart of a code map construction method provided in an embodiment of the present application. Figure 2 As shown, the code graph construction method may include the following steps:
[0053] S201, obtaining a target code repository, where the target code repository includes a target directory and a target file.
[0054] Specifically, the above-mentioned target code warehouse can be a local code warehouse pre-stored in the user terminal or the server designated by the user, or it can be a remote code warehouse received through the network, or it can be a code warehouse corresponding to the target application that the user (developer) is currently developing or using or testing or wants to perform a refined analysis on. The number of the above-mentioned target code warehouses can be one or more, and the embodiment of the present application does not limit this. When there are multiple target code warehouses, the code map construction method provided in the embodiment of the present application can be used to simultaneously or successively construct the target code maps corresponding to multiple target code warehouses. The embodiment of the present application does not limit this. The above-mentioned target code warehouse is used to manage and store the source code of the target software (target application project).
[0055] The target code repository contains target directories and target files. The target directory is used to represent the core structure and status of the target code repository. It includes each target subdirectory (target folder) and target file in the target code repository. The target file is the core content of the target code repository, which can include but is not limited to code files, operation logs, configuration information and other core files. The above-mentioned target subdirectories (target folders) and target files together constitute the skeleton of the target code repository, making it possible to efficiently manage various code versions and branches in the target code repository.
[0056] Optionally, when the developer wants to build a target code map corresponding to the target code repository or wants to better apply the knowledge in the target code repository, the corresponding code repository path can be sent to the server through the terminal. At the same time, the server can receive the code repository path sent by the terminal or other servers through the network. Then, the target code repository is obtained according to the code repository path. The code repository path can be divided into a local code path and a remote repository path. The target code repository is obtained in different ways according to different paths. In the case where the above-mentioned code repository path is a local code path, it can be determined first whether the code repository corresponding to the above-mentioned code repository path is a git repository. For example, but not limited to, it can be checked whether there are git-specific files and directories under the code repository path.
[0057] If there are git-specific files and directories under the code repository path (that is, the code repository corresponding to the above code repository path is a git repository), the above code repository is determined as the target code repository. The git repository (Gitrepository) is used to store project version information, and can record all submission history, branches, tags and other information. The root directory of the git repository usually contains a .git directory, which contains all metadata, configurations, object databases, etc. of the warehouse. In the embodiment of the present application, by using the git repository (target code repository) to construct a code map, it is beneficial to track code changes in the target code repository, roll back to a previous version, or compare differences between different versions, etc., and it is also beneficial for team collaboration and data sharing for the target code repository.
[0058] If the code repository corresponding to the above code repository path is not a git repository, the code repository corresponding to the above code repository path can be initialized, for example but not limited to, by running the "git init" command in the code repository corresponding to the code repository path to initialize a new git repository, create a .git directory to store the metadata of the code repository, and obtain the target code repository after the code repository is initialized. In the embodiment of the present application, when the code repository corresponding to the code repository path is not a git repository, the code repository is first initialized and converted into a git code repository (target code repository), and then a code map is constructed for the initialized git code repository (target code repository), which is conducive to team collaboration management and data sharing of the target code map corresponding to the code repository.
[0059] Optionally, after obtaining the code repository path to be used for code graph construction, if the code repository path is a remote repository path, the target code repository can be cloned according to the code repository path. If the code repository path is a remote repository path, then the git clone command can be used to clone (copy) the code repository stored at the network location pointed to by the remote repository path, such as but not limited to the URL of a hosting service such as GitHub, GitLab, Bitbucket, or an SSH address, and store it in the local database as the target code repository.
[0060] S202, generating a target directory tree according to the target directory.
[0061] Specifically, the target directory tree (also called the target file tree or target directory hierarchy) is used to represent the organizational structure of the target files and target folders (target subdirectories) in the target code repository. This structure can be displayed in the form of a tree diagram, where each node represents a file or directory (folder), and the lines between the nodes represent the inclusion (parent-child) relationship between them. The root node of the target directory tree usually represents the root directory of the entire target code repository, while the other nodes are expanded downward step by step to form a tree-like hierarchical structure. The target directory tree can clearly reflect the hierarchy and dependency relationship between the target files in the target code repository.
[0062] Optionally, after obtaining the target code repository, for the target directory part in the target code repository, you can first traverse the target directory of the target code repository and obtain the second target entity and the second target entity relationship based on the above target directory. The above second target entity includes at least one of the following: directory name (may include but is not limited to folder name), file name, and the above second target entity relationship may include but is not limited to: containment relationship. The above containment relationship refers to the hierarchical relationship between the parent directory and the child directory, and the hierarchical relationship is determined by the nested structure of the folder. That is, a folder can contain multiple subfolders or files, and these subfolders or files constitute the child nodes of the folder. After obtaining the second target entity and the second target entity relationship corresponding to the target directory, the target directory tree corresponding to the target directory can be generated according to each second target entity and the second target entity relationship between each second target entity.
[0063] Exemplarily, traversing the target directory corresponding to the target code repository can be implemented, but not limited to, by a traversal algorithm (such as a depth-first search DFS or a breadth-first search BFS). During the traversal process, the full path and name of each file or folder can be recorded. Based on the information obtained by the traversal, a node object is constructed for each file or folder. The node object may include, but is not limited to, the name of the file or folder (the second target entity), a unique identifier, and a list for storing child nodes. Then, according to the inclusion relationship information (the second target entity relationship) between the nodes, each child node is added to the child node list of its corresponding parent node. Since the root directory of the target directory has no parent node, its parent node list can be empty or set to a specific value (such as but not limited to null or None). Then, by passing the parent-child node list data to a visualization tool, the visualization tool may be, but is not limited to, a graphics library (such as Graphviz, D3.js), a specialized tree map library (such as jsTree, a tree map component in ECharts), or any visualization tool that can process hierarchical data. The tree map generation function or method is called by the visualization tool to generate a tree map of the target directory corresponding to the target code repository.
[0064] For example, Figure 3 A schematic diagram of a target directory tree diagram provided in an embodiment of the present application. Figure 3 As shown in the figure, the target directory tree diagram includes a root node, a parent node, and a child node. The line between the nodes indicates the inclusion relationship between the two nodes. The root node includes the repository name corresponding to the target code repository, for example Figure 3 The parent node includes the directory name or folder name, such as "utils", "data", and "test_data" in the figure. The child node includes the file name, such as "__init__.py", "autonet.py" and other target code file names with the suffix .py, "README.md", "tree.txt", "test1.txt" and other target file names.
[0065] S203: Extract the first target entity and the first target entity relationship in the target file.
[0066] Optionally, in order to obtain the content of the target file and build a comprehensive and complete target code map, a target model (for example, but not limited to a named entity recognition model based on deep learning, a relationship extraction model based on supervised learning, etc.) can be used to automatically extract the first target entity and the first target entity relationship in the target file. The embodiment of the present application does not limit the target model.
[0067] In a possible embodiment, the target file may include but is not limited to target code files and / or target other files; the first target entity may include but is not limited to at least one of the following: target code entities corresponding to the target code files, target other entities corresponding to the target other files; the first target entity relationship may include but is not limited to at least one of the following: target code entity relationships corresponding to the target code files, target other entity relationships corresponding to the target other files. The target code entity may include but is not limited to at least one of the following: classes, functions, variables; the target code entity relationships include: call relationships. The programming language used in the target code file may include but is not limited to at least one of the following: elm, javascript, ql, ocaml, elixir, typescript, go, c, elisp, php, r, python, java, ruby, rust, c++, c#.
[0068] Optionally, in the case where the target file includes a target code file, a code parsing tool can be used to parse the target code file to obtain a target syntax tree. Each node in the target syntax tree represents a specific syntax structure, such as an identifier, a keyword, an operator, a constant, etc., and these nodes together constitute the overall structure of the target code file. Then, the target code entity and the target code entity relationship can be extracted from the target syntax tree. The target syntax tree can be traversed by writing a program script, and the nodes representing the target code entity and the node connection lines representing the target code entity relationship can be identified.
[0069] In the case where the target file includes target other files, the target other entities and target other entity relationships can be extracted from the target other files using the target big model. Target other files (such as but not limited to README, documents, comments, etc.) usually contain text information that is essential for understanding the purpose, usage, architecture, etc. of the target code repository. By inputting the target other files into the target big model (such as a pre-trained natural language processing big model or a deep learning big model in a specific field), the target other entities and target other entity relationships (association relationships) corresponding to the target other files can be output.
[0070] It should be noted that the above S202 and S203 can be executed successively or simultaneously, and the embodiment of the present application is not limited to this.
[0071] S204: construct a target code graph corresponding to the target code repository according to the target directory tree, the first target entity and the first target entity relationship.
[0072] Specifically, after obtaining the target directory tree according to S202 and obtaining the first target entity and the first target entity relationship according to S203, a target code graph corresponding to the target code repository can be constructed using a graph modeling tool (such as but not limited to NetworkX) or a graph processing framework (such as Apache Jena) based on the attributes of the entities in the target directory tree, the first target entity and the first target entity relationship (such as name, type, content, etc.) and the type of entity relationship (such as call, containment, association, etc.).
[0073] Optionally, the target directory entities and target directory entity relationships can be extracted from the target directory tree, and then a graph construction tool can be used to construct a target directory sub-graph based on the target directory entities and target directory entity relationships. At the same time, a graph construction tool can also be used to construct a target file sub-graph based on the first target entity and the first target entity relationship. Each target file corresponds to a target file sub-graph. Then, the target directory sub-graph and each target file sub-graph are merged to form a target code graph corresponding to a complete target code repository.
[0074] For example, Figure 4 As shown, the target code map can be obtained by combining the target directory sub-map 410 and the target file sub-map. The target directory sub-map 410 can be composed of the target directory entity (e.g. Figure 4 The warehouse name, folder a, other files 1, code file 1) and the target directory entity relationship (for example Figure 4 The target file sub-graph may include the target code file sub-graph 420 and / or the target other file sub-graph 430. The target code file sub-graph 420 may be composed of the target code entities (e.g. Figure 4 class a, function x, and variable m, etc.) as well as the target code entity relationships (e.g. Figure 4 The target other file sub-graph 430 can be constructed by the target other entities in other files 1 (for example Figure 4 Other entities a, other entities b) as well as target other entity relationships (e.g. Figure 4 Each node in the target code map displays the entity name, and the node attributes include: name, type, and content. Figure 4As shown, the warehouse name of the target code warehouse can be used as the root node of the target code map. The root node contains two child nodes, namely folder a and other files 1. The connection line between the root node and the child nodes represents the inclusion relationship. The information of each node is stored in the attributes of each node. For example, the attribute of the node where folder a is located is <name: folder a, type: directory, content: empty>, the attribute of the node where code file 1 is located is <name: code file 1, type: file, content: empty>, and the attribute of the node where variable m is located is <name: a, type: variable, content: 1>.
[0075] In the embodiments of the present application, on the one hand, by generating a target directory tree according to the target directory in the target code repository, and using the target directory tree to show the relationship between the target directory and the target file, it is possible to extract the information of the target code repository from a macro perspective; on the other hand, by performing a refined analysis of the target files in the target code repository, the target entities and target relationships in the target files are extracted, and the detailed content information of the target code repository is extracted from a local perspective; on the other hand, a target code map is constructed based on the above two aspects, and an overall overview and cross-file understanding of the target code repository are achieved through the target code map, and different granularities of information of the target code repository are obtained and analyzed and integrated from macro and local perspectives, respectively, so that developers can more quickly grasp the content and hierarchical structure of the target code repository and achieve a global overview and rapid retrieval of the target code repository.
[0076] Please refer to the following Figure 5 , which exemplarily shows a flow chart of another code map construction method provided in an embodiment of the present application. Figure 5 As shown, the code graph construction method may include the following steps:
[0077] S501, obtaining a target code repository, where the target code repository includes a target directory and a target file.
[0078] Specifically, S501 is consistent with S201 and will not be described in detail here.
[0079] S502: Generate a target directory tree according to the target directory.
[0080] Specifically, S502 is consistent with S202 and will not be described in detail here.
[0081] S503, determining whether there is a corresponding cached target file sub-graph for the target file.
[0082] Specifically, in order to ensure the consistency of the target file and its target file subgraph, it can be determined whether there is a corresponding cached target file subgraph for each target file in the target code repository by checking whether the update time of each target file in the target code repository is consistent with the creation time of the target file subgraph corresponding to each target file. The nodes of the target file subgraph include the first target entity (such as class, variable, function, etc.) and its attributes (such as name, type, content, etc.) in the target file, and the edges in the target file subgraph are used to represent the first target entity relationship (such as call, association, etc.) in the target file.
[0083] It can be understood that one target file corresponds to one target file sub-graph. When there is no update of the target file, the update time of the target file can also be the creation time of the target file. The creation time of the target file sub-graph can be updated as the update time of the target file sub-graph is updated, or it can be empty, that is, the target file sub-graph corresponding to the target file has not yet been created.
[0084] It should be noted that the above S502 and S503 can be executed successively or simultaneously, and this embodiment of the present application does not limit this.
[0085] S504: If a target file sub-graph corresponding to the cache exists for the target file, a target code graph corresponding to the target code repository is constructed according to the target file sub-graph and the target directory tree.
[0086] Specifically, if the update time of the target file is consistent with the creation time of its corresponding target file sub-graph, that is, the target file has a corresponding cached target file sub-graph, which means that the target file sub-graph at this time is the latest version (consistent with the content of the target file) and does not need to be updated, and there is no need to perform the extraction operation of the first target entity and the first target entity relationship. In other words, when each target file in the target code repository has a corresponding cached target file sub-graph, the target code graph corresponding to the target code repository can be directly constructed using the cached target file sub-graph and the target directory tree corresponding to each target file. For example, but not limited to, the cached target file sub-graph and the target directory tree corresponding to each target file can be directly merged to obtain the target code graph.
[0087] Optionally, if each target file in the target code repository has a corresponding cached target file sub-map, it means that the target file has not been updated after the target code map is created, and the construction of the target code map can be terminated to avoid repeated creation of target code maps with the same content. If the target code map already exists, during the process of supplementing or repairing the target code map, if it is detected that all target files in the target code repository have corresponding cached target file sub-maps, it means that the target code map has been supplemented or repaired, and the construction or update of the target code map can be terminated.
[0088] Please continue to refer to Figure 5 ,like Figure 5 As shown, after determining whether there is a corresponding cached target file sub-graph in the above S503, the code graph construction method further includes:
[0089] S505, if the target file does not have a corresponding cached target file sub-graph, then in the case where the target file includes target other files, the target other entities and target other entity relationships are extracted from the target other files using the target large model.
[0090] Specifically, if the update time of the target file is inconsistent with the creation time of its corresponding target file sub-graph, that is, the target file does not have a corresponding cached target file sub-graph, which means that the target file sub-graph at this time is not the latest version (inconsistent with the content of the target file) or the target file sub-graph corresponding to the target file has not been created. At this time, in order to ensure the accuracy of the target code graph, it is necessary to extract the first target entity and the first target entity relationship in the updated target file, update its corresponding target file sub-graph, and the target code graph corresponding to the target code repository will also be updated accordingly.
[0091] When there is no corresponding cached target file sub-graph including target other files, the target big model can be used to extract target other entities and target other entity relationships from the target other files. For example, but not limited to, the target big model (such as a pre-trained natural language processing model or a deep learning model in a specific field) can be used to identify the target other entities in the target other files and the association relationship between the target other entities.
[0092] Exemplarily, when the target code repository includes target other files and there is no corresponding cached target file subgraph for the target other files, the target other files can be first cleaned to remove unnecessary formats, HTML tags, special characters, etc. Then, the cleaned text is segmented and part-of-speech tagged (POSTagging) can be performed to better identify entities and relationships. Finally, a model fine-tuned on a large-scale corpus (such as but not limited to BERT, BiTE, etc.) can be used to identify target other entities and target other entity relationships (association relationships) in the text. Since there may be multiple mentions of the same target other entity or target other entity relationship in the text, after extracting the target other entities and target other entity relationships in the target other files, deduplication and merging can also be performed.
[0093] Optionally, in the case where the target file includes other target files, in order to avoid the problem that the other target files are too large and affect the processing speed of the target large model, the other target files can be sliced first to obtain multiple other target sub-files. For example, but not limited to, a segmentation program can be written to write the content of a other target file into multiple new files according to the set segmentation logic (for example, but not limited to the number of lines, content, byte size, etc.) to obtain multiple other target sub-files. Then, each other target sub-file is input into the target large model, and the other target entities and the other target entity relationships in each other target sub-file are output. In the embodiment of the present application, by slicing the other target files, complex other target files can be decomposed into smaller other target sub-files, avoiding the problem that the other target files are too large and the target large model cannot be processed or affect the processing speed of the target large model, and significantly improving the extraction efficiency of the target large model.
[0094] Please continue to refer to Figure 5 ,like Figure 5 As shown, after determining whether there is a corresponding cached target file sub-graph in the above S503, the code graph construction method further includes:
[0095] S506: If the target file does not have a corresponding cached target file sub-graph, then if the target file includes a target code file, determine whether the target code file supports parsing.
[0096] Specifically, when the target code repository includes a target code file and the target code file does not have a corresponding cached target file sub-graph, it is necessary to first determine whether the above target code file supports parsing. Since the programming languages used by the target code files are different, the corresponding parsing methods and parsing tools are also different. That is, whether the above target code file supports parsing can be determined by whether the programming language used by the target code file is in the preset programming language type; if the programming language used by the target code file is in the preset programming language type, it means that the target code file supports parsing, and the code parsing tool can be used to parse the target code file; if the programming language used by the target code file is not in the preset programming language type, it means that the target code file does not support parsing.
[0097] It should be noted that the above S505 and S506 can be executed simultaneously or one after another, and this embodiment of the present application does not limit this.
[0098] S507: If the target code file supports parsing, the target code file is parsed to obtain a target syntax tree.
[0099] Specifically, the target syntax tree shows the hierarchical relationship of expressions, statements, declarations and other elements in the code in a tree structure, making the structure of the code more intuitive and easy to understand. Each node in the target syntax tree represents a specific syntax structure, such as identifiers, keywords, operators, constants, etc. You can use the code parsing tool Tree-sitter to parse the target code file, build a specific syntax tree for the target code file, and effectively update the syntax tree when editing the target code file.
[0100] Exemplarily, all files in the target code repository can be traversed through a written program script to obtain the content of the target code file. The target code file can be parsed using, but is not limited to, the code parsing tool Tree-sitter. Tree-sitter provides syntax definitions for a variety of programming languages (such as C, C++, JavaScript, Python, etc.). The target code file can be loaded and parsed using Tree-sitter's API, by reading the content of the target code file and using Tree-sitter's parsing function to convert the content into a syntax tree. During the parsing process, Tree-sitter will identify the lexical units and grammatical structures in the code based on the syntax definition, and construct a target syntax tree corresponding to the target code file.
[0101] S508, extracting target code entities and target code entity relationships in the target code file from the target syntax tree.
[0102] Specifically, the target entity nodes can be identified by traversing the target syntax tree, and the target code entity information can be extracted from the target entity nodes. The edges in the target syntax tree represent the grammatical relationships between the target entity nodes, and by analyzing these relationships, the relationships between the target code entities can be inferred.
[0103] Exemplarily, after obtaining the target syntax tree, the entire target syntax tree is first traversed, which can be implemented by, but not limited to, algorithms such as depth-first search (DFS) or breadth-first search (BFS). During the traversal process, each target entity node can be accessed and its type can be checked. According to the type of the target entity node, which target entity nodes represent the target code entities are identified. For example, in the syntax tree of a programming language, there may be target entity nodes representing variable declarations, target entity nodes representing function definitions, target entity nodes representing class definitions, etc. For each target entity node, information about the target entity, such as name, type, content, etc., can be extracted. This information is usually stored in the attributes of the target entity node, or can be inferred through the relationship between the child target entity node and the parent target entity node of the target entity node. After identifying the target code entity, it is necessary to further identify the relationship between the target code entities. The edges in the target syntax tree represent the grammatical relationship between the target entity nodes. By analyzing these relationships, the relationship between the target code entities can be inferred. For example, a class node may contain multiple function nodes, and there is a call relationship between these function nodes and the class node.
[0104] Please continue to refer to Figure 5 ,like Figure 5 As shown, in the above S506, if the target file does not have a corresponding cached target file sub-graph, then in the case where the target file includes a target code file, after determining whether the target code file supports parsing, the code graph construction method further includes:
[0105] S509: If the target code file does not support parsing, the target code entities and target code entity relationships in the target code file are extracted using the target macro model.
[0106] Specifically, if the target code file does not support parsing, it means that the target code entities and target code entity relationships in the target code file cannot be extracted by parsing the target code file. However, in order not to miss the content of the target code file (target code entities and target code entity relationships) and to ensure the integrity of the target code graph construction, the target large model can be used to extract the target code entities and target code entity relationships from the target code file. The specific extraction process can refer to the description of S505, which will not be repeated here.
[0107] S510, constructing a target code graph corresponding to the target code repository according to the target directory tree, target code entities, target code entity relationships, target other entities and target other entity relationships.
[0108] Specifically, the implementation process of S510 is similar to that of S204, and will not be repeated here.
[0109] S511, save the target code map in the local Sqlite database.
[0110] Specifically, after constructing the target code graph, the target code graph can be serialized into a storable binary format (for example but not limited to binary format), and then multiple tables can be designed in the local Sqlite database to store the nodes and edges of the target code graph respectively, and then the serialized target code graph data can be inserted into the above tables for storage.
[0111] In an embodiment of the present application, by storing the target code map in a local Sqlite database, it is possible to access the target code map without transmitting data over the network, thereby reducing the time and delay of data transmission. In addition, in an environment without a network or an unstable network, the target code map stored in the local database can still be accessed and operated, ensuring fast access and high availability of the target code map data.
[0112] Please refer to the following Figure 6 , which exemplarily shows a flow chart of another code map construction method provided in an embodiment of the present application. Figure 6 As shown, the code graph construction method may include:
[0113] S601, obtaining a target code repository, where the target code repository includes a target directory and a target file.
[0114] Specifically, S601 is consistent with S201 and will not be described in detail here.
[0115] S602: Generate a target directory tree according to the target directory.
[0116] Specifically, S602 is consistent with S202 and will not be repeated here.
[0117] S603: Extract the first target entity and the first target entity relationship in the target file.
[0118] Specifically, S603 is consistent with S203 and will not be repeated here.
[0119] It should be noted that the above S602 and S603 can be executed successively or simultaneously, and this embodiment of the present application does not limit this.
[0120] S604, constructing a target directory sub-map according to the target directory tree.
[0121] Specifically, after obtaining the target directory tree, all target directory entities (such as file names, directory names) and target directory entity relationships (inclusion relationships) can be identified from the target directory tree. These inclusion relationships can be represented by the lines between the nodes in the target directory subgraph. Then, a graph construction tool is used to construct the target directory subgraph based on the above target directory entities and target directory entity relationships.
[0122] S605: construct a target file sub-graph corresponding to each target file according to the first target entity and the first target entity relationship in each target file.
[0123] Specifically, a graph construction tool can be used to construct a target file sub-graph corresponding to each target file based on the first target entity and the first target entity relationship in each target file. One target file corresponds to one target file sub-graph, and the update time of the target file is the creation time of the target file sub-graph. The target file sub-graph can include, but is not limited to, the attributes of the first target entity (such as name, type, content, etc.) and the type of the first target entity relationship (such as call, association, etc.).
[0124] It should be noted that the above S604 and S605 can be executed successively or simultaneously, and this embodiment of the present application does not limit this.
[0125] S606, constructing a target code graph corresponding to the target code repository according to the target directory sub-graph and the target file sub-graph.
[0126] Specifically, the target directory sub-graph and the target file sub-graph corresponding to each target file in the target code warehouse are merged to form a complete target code graph. It is possible but not limited to first cleaning the target directory sub-graph and the target file sub-graph corresponding to each of the multiple target files in the target code warehouse to remove duplicate, erroneous or inconsistent information and convert it into a unified format; then, using entity recognition, similarity calculation and other technologies, align the same entities of different target file sub-graphs, and use the graph construction tool to generate a complete and accurate target code graph, thereby solving the conflicts and inconsistencies between entities, relationships and attributes between different target file sub-graphs, and ensuring the consistency and accuracy of the fused target code graph. The target directory sub-graph represents the information on the macro structure of the target code warehouse, and the target file sub-graph can represent the specific details of the target files in the target code warehouse, showing the content of the target code warehouse from a micro perspective.
[0127] S607: After detecting that the target code repository is updated, the specified target sub-graph is updated according to the target update information corresponding to the target code repository.
[0128] Specifically, the designated target sub-graph may be a target directory sub-graph corresponding to the target directory updated this time in the target code repository, or a target file sub-graph corresponding to the target file updated this time in the target code repository, which may be set according to the actual update situation of the target code repository, and the embodiment of the present application does not limit this. The number of the above-mentioned designated target sub-graphs may be one or more, and the embodiment of the present application does not limit this.
[0129] Optionally, the update time of the target file is the creation time of the target file sub-graph. After detecting that the target code repository has been updated, the updated target file can be determined by judging whether the update time of the target file in the target code repository is consistent with the creation time of the target file sub-graph. If they are inconsistent, it means that the target file has updated information but its corresponding target file sub-graph (specified target sub-graph) has not been updated, then it is necessary to extract the first target entity and the first target entity relationship in the updated target file again to update the target file sub-graph (specified target sub-graph). That is, in the case where only an update of a target file in the target code repository is detected, updating the specified target sub-graph may include updating the target file sub-graph corresponding to the updated target file.
[0130] Optionally, when it is detected that the target directory in the target code repository is updated, such as but not limited to deleting a file or changing a file name, it is necessary to update the target directory tree, and update the corresponding target directory sub-map (specified target sub-map) based on the updated target directory tree. That is, when it is detected that only the target directory in the target code repository is updated, updating the specified target sub-map may include updating the target directory sub-map of the target directory corresponding to the target code repository.
[0131] Optionally, when it is detected that a new target file is created in the target code repository, it is necessary to perform content detection on the new target file, extract the first target entity and the first target entity relationship therein, and create its corresponding target file sub-graph (specified target sub-graph). At the same time, it is necessary to obtain the inclusion relationship between the new target file and the target directory, and update the target directory sub-graph (specified target sub-graph) corresponding to the target directory based on the inclusion relationship. That is, when it is detected that a new target file is created in the target code repository, updating the specified target sub-graph can include both constructing the target file sub-graph corresponding to the new target file and updating the target directory sub-graph of the target directory corresponding to the target code repository.
[0132] S608, updating the target code graph according to the updated designated target sub-graph.
[0133] Specifically, after all the specified target sub-graphs are updated, the specified target sub-graphs corresponding to the updated target directories and / or target files in the target code graph can be replaced with the updated specified target sub-graphs, or the target code graph corresponding to the target code repository can be rebuilt based on the target sub-graphs corresponding to the target directories and / or target files that have not been updated and the updated specified target sub-graphs. This ensures the consistency between the target code graph and the target code repository, avoids the problem of constructing a large number of duplicate target sub-graphs that results in a long time required to update the target code graph and a waste of computing resources, and ensures the update efficiency of the target code graph.
[0134] In some possible embodiments, Figure 7 As shown, after the target code graph corresponding to the target code repository is built, it can also include but is not limited to:
[0135] S701, receiving a target instruction, where the target instruction carries target task information related to a target code repository.
[0136] Specifically, in the application of the target code map, the target instruction from the terminal or other server can be received first, and the target instruction contains all the information required to perform the target task. After receiving the target instruction, it needs to be parsed to extract the key target task information, such as but not limited to finding a function call, checking the specific value of a parameter, etc.
[0137] S702, in response to the target instruction, executing the target task corresponding to the target task information based on the target code map to obtain a target execution result.
[0138] Specifically, for the received target instruction, after parsing the key target task information related to the target code repository, the target instruction can be responded to and the corresponding target task can be executed based on the target code map according to the parsed task type and parameters. The above target tasks can include but are not limited to code retrieval, question and answer, query and other tasks. After the target task is executed, the corresponding target execution result can also be fed back to the triggering end of the target task.
[0139] In an embodiment of the present application, by constructing a target code graph corresponding to a target code repository and applying the target code graph to downstream tasks (such as but not limited to code question answering, code retrieval, etc.), it is convenient to combine AI with the code field, which can assist developers in developing software more quickly and efficiently.
[0140] Please refer to the following Figure 8 , which exemplarily shows a schematic diagram of the implementation process of a code map construction method provided in an embodiment of the present application. Figure 8As shown in the figure, when the developer wants to build the target code map corresponding to the target code repository, first, the target code repository can be obtained according to the target code repository path. If the target code repository path is the local code repository path, after obtaining the code repository according to the local code repository path, it can be determined whether the code repository is a git repository. If so, the code repository corresponding to the local code repository path is determined as the target code repository (git code repository); if not, the code repository can be initialized to obtain the target code repository (git code repository). If the target code repository path is a remote code repository path, the git clone command is used to clone and save it to obtain the target code repository (git code repository).
[0141] Secondly, after obtaining the target code repository (git code repository), the target repository name and attributes of the target code repository (git code repository) can be extracted as the root node of the target code map; and the instruction (seedir) is used to obtain the inclusion relationship between the target directory and the target file of the target code repository (git code repository) to obtain the target directory tree. The target file can be but is not limited to target code files, operation logs, configuration information and other files.
[0142] At the same time, extract the file content of the target code warehouse (git code warehouse), that is, you can use but not limited to the written program script to traverse multiple target files in the target code warehouse (git code warehouse) to determine whether each target file has a corresponding target file sub-graph. The target file sub-graph is a sub-graph composed of the first target entity and the relationship between the first target entity in each target file. One target file corresponds to one target file sub-graph. For the specific judgment process, please refer to S503, which will not be repeated here. If all target files in the target code warehouse (git code warehouse) have corresponding target file sub-graphs, then the construction of the target code graph corresponding to the target code warehouse is terminated. If there is a target file in the target code warehouse (git code warehouse) that does not have a corresponding target file sub-graph, then determine whether the target file that does not have a corresponding target file sub-graph is a target code file. Usually, in the target code warehouse, the source code files (target code files) have specific suffixes, and the suffix of the target file can be used to determine whether the target file is a target code file.
[0143] If the target file is not a target code file, it is sliced and then input into the target large model for analysis, and the target other entities and target other entity relationships (other file entities and relationships) in the target other files are extracted. If the target file is a target code file, it is further determined whether the target code file supports parsing. For the specific judgment method, please refer to S506. If the target code file supports parsing, the code parsing tool (Tree-Sitter) is used to parse the target code file to obtain the target syntax tree, and then the target code entities (variables, classes, functions, etc.) and target code entity relationships in the target syntax tree are extracted. If the target code file does not support parsing, the target code file is sliced and then input into the target large model for analysis, and the target code entities (variables, classes, functions, etc.) and target code entity relationships in the target code file are extracted.
[0144] Finally, according to the above target directory tree, target warehouse name, target code entities and target code entity relationships and target other entities and target other entity relationships (code files and other file entities and relationships), a graph building tool (such as NetworkX) is used to build the target code graph, and the target code graph is saved in the local sqlite database to complete the construction of the target code graph.
[0145] Please refer to the following Fig. 9 , which exemplarily shows a schematic diagram of the structure of a code map construction device provided in an embodiment of the present application. Fig. 9 As shown, the code graph construction device 900 may include:
[0146] The acquisition module 910 acquires a target code repository; the target code repository includes a target directory and a target file;
[0147] A generating module 920 generates a target directory tree according to the target directory; the target directory tree is used to represent the relationship between the target directory and the target file;
[0148] An extraction module 930 extracts the first target entity and the first target entity relationship in the target file;
[0149] A construction module 940 constructs a target code graph corresponding to the target code repository according to the target directory tree, the first target entity and the relationship between the first target entities.
[0150] In some possible embodiments, the above-mentioned generation module 920 is specifically used to: obtain the second target entity and the second target entity relationship based on the above-mentioned target directory; the above-mentioned second target entity includes at least one of the following: directory name, file name; the above-mentioned second target entity relationship includes: inclusion relationship; generate a target directory tree based on the above-mentioned second target entity and the above-mentioned second target entity relationship.
[0151] In some possible embodiments, the above-mentioned target file includes a target code file and / or a target other file; the above-mentioned first target entity includes at least one of the following: a target code entity corresponding to the above-mentioned target code file, and a target other entity corresponding to the above-mentioned target other file; the above-mentioned first target entity relationship includes at least one of the following: a target code entity relationship corresponding to the above-mentioned target code file, and a target other entity relationship corresponding to the above-mentioned target other file.
[0152] In some possible embodiments, the extraction module 930 is specifically used for: when the target file includes the target code file, parsing the target code file to obtain a target syntax tree, and extracting the target code entities and the target code entity relationships from the target syntax tree; when the target file includes the target other files, extracting the target other entities and the target other entity relationships from the target other files using a target large model.
[0153] In some possible embodiments, the extraction module 930 includes:
[0154] A slicing unit, configured to slice the target other files to obtain a plurality of target other sub-files when the target file includes the target other files;
[0155] The output unit is used to input the above-mentioned target other sub-files into the target big model, and output the above-mentioned target other entities and the above-mentioned target other entity relationships in the above-mentioned target other sub-files.
[0156] In some possible embodiments, the code graph construction device 900 further includes:
[0157] A first judgment module, used for judging whether the target code file supports parsing when the target file includes the target code file;
[0158] The first execution module is used to parse the target code file to obtain a target syntax tree if the target code file supports parsing.
[0159] In some possible embodiments, the extraction module 930 is further used to: if the target code file does not support parsing, extract the target code entity and the target code entity relationship from the target code file using the target macro model.
[0160] In some possible embodiments, the construction module 940 is specifically used to: construct a target directory sub-graph based on the target directory tree; construct a target file sub-graph corresponding to each target file based on the first target entity and the first target entity relationship in each target file; and construct a target code graph corresponding to the target code repository based on the target directory sub-graph and the target file sub-graph.
[0161] In some possible embodiments, the code graph construction device 900 further includes:
[0162] The second judgment module judges whether the target file has a corresponding cached target file sub-graph;
[0163] The second execution module is used to execute the step of extracting the first target entity and the first target entity relationship in the target file when there is no corresponding cached target file sub-graph in the target file.
[0164] In some possible embodiments, the target code graph is composed of a plurality of target sub-graphs, wherein the plurality of target sub-graphs include a target directory sub-graph corresponding to the target directory and a target file sub-graph corresponding to each of the target files in the target code repository; the code graph construction device 900 further includes:
[0165] The update module is used to update the above-mentioned specified target sub-graph according to the target update information corresponding to the above-mentioned target code warehouse after detecting that the above-mentioned target code warehouse is updated; and update the above-mentioned target code graph according to the updated above-mentioned specified target sub-graph.
[0166] In some possible embodiments, the programming language used by the above-mentioned target code file includes at least one of the following: elm, javascript, ql, ocaml, elixir, typescript, go, c, elisp, php, r, python, java, ruby, rust, c++, c#.
[0167] In some possible embodiments, the above-mentioned target code entity includes at least one of the following: a class, a function, and a variable; and the above-mentioned target code entity relationship includes: a calling relationship.
[0168] In some possible embodiments, the code graph construction device 900 further includes:
[0169] A conversion module, configured to convert the target code map into target binary data after the construction module 940 constructs the target code map corresponding to the target code repository according to the target directory tree, the first target entity and the first target entity relationship;
[0170] The saving module is used to save the above target binary data in the local Sqlite database.
[0171] In some possible embodiments, the acquisition module 910 includes:
[0172] Get path unit, used to get the code repository path;
[0173] A judging unit, configured to judge whether the code repository corresponding to the code repository path is a git repository when the code repository path is a local code path;
[0174] The determination unit is used to determine the code repository as a target code repository if the code repository corresponding to the code repository path is a git repository.
[0175] In some possible embodiments, the code graph construction device 900 further includes:
[0176] The initialization module is used to initialize the code repository corresponding to the code repository path after the judgment unit to obtain the target code repository if the code repository corresponding to the code repository path is not a git repository.
[0177] In some possible embodiments, the acquisition module 910 includes:
[0178] Get the path unit and get the code repository path;
[0179] The cloning unit clones the target code repository according to the code repository path when the code repository path is a remote repository path.
[0180] In some possible embodiments, the code graph construction device 900 further includes:
[0181] A receiving instruction module receives a target instruction; the target instruction carries target task information related to the target code repository;
[0182] The task execution module responds to the target instruction and executes the target task corresponding to the target task information based on the target code map to obtain the target execution result.
[0183] The division of each module in the above-mentioned code map construction device is only for illustration. In other embodiments, the code map construction device can be divided into different modules as needed to complete all or part of the functions of the above-mentioned code map construction device. The implementation of each module in the code map construction device provided in the embodiment of this specification can be in the form of a computer program. The computer program can be run on a terminal or a server. The program modules constituted by the computer program can be stored in the memory of the terminal or the server. When the computer program is executed by the processor, all or part of the steps of the code map construction method described in the embodiment of this specification are implemented.
[0184] See next Fig.10 , which is a schematic diagram of the structure of an electronic device provided by an exemplary embodiment of this specification. Fig.10 As shown, the electronic device 1000 may include: at least one processor 1010 , at least one communication bus 1020 , a user interface 1030 , at least one network interface 1040 , and a memory 1050 .
[0185] The communication bus 1020 may be used to realize the connection and communication among the above-mentioned components.
[0186] The user interface 1030 may include buttons, and the optional user interface may also include a standard wired interface or a wireless interface.
[0187] The network interface 1040 may optionally include a Bluetooth module, an NFC module, a Wi-Fi module, etc.
[0188] Among them, the processor 1010 may include one or more processing cores. The processor 1010 uses various interfaces and lines to connect the various parts of the entire electronic device 1000, and runs or executes instructions, programs, code sets or instruction sets stored in the memory 1050, and calls the data stored in the memory 1050, and the various functions and processing data of the electronic device 1000. Optionally, the processor 1010 can be implemented in at least one hardware form of DSP, FPGA, and PLA. The processor 1010 can integrate one or a combination of CPU, GPU, modem, etc. Among them, the CPU mainly processes the operating system, user interface, and application programs; the GPU is responsible for rendering and drawing the content to be displayed on the display screen; the modem is used to process wireless communications. It can be understood that the above-mentioned modem may not be integrated into the processor 1010, and it can be implemented separately through a chip.
[0189] Among them, the memory 1050 may include RAM and may also include ROM. Optionally, the memory 1050 includes a non-transitory computer-readable medium. The memory 1050 can be used to store instructions, programs, codes, code sets or instruction sets. The memory 1050 may include a program storage area and a data storage area, wherein the program storage area may store instructions for implementing an operating system, instructions for at least one function (such as obtaining a target code repository, generating a target directory tree, building a target code map corresponding to a target code repository, etc.), instructions for implementing the above-mentioned various method embodiments, etc.; the data storage area may store data involved in the above-mentioned various method embodiments, etc. The memory 1050 may optionally also be at least one storage device located away from the aforementioned processor 1010. As Fig.10 As shown, the memory 1050 as a computer storage medium may include an operating system, a network communication module, a user interface module, and program instructions.
[0190] In some possible embodiments, the electronic device 1000 may be the aforementioned Fig. 9 In the code graph construction device 900 shown, the processor 1010 can be used to call the program instructions stored in the memory 1050, and specifically perform the following operations: obtain the target code repository; the above-mentioned target code repository contains a target directory and a target file; generate a target directory tree according to the above-mentioned target directory; the above-mentioned target directory tree is used to characterize the relationship between the above-mentioned target directory and the above-mentioned target file; extract the first target entity and the first target entity relationship in the above-mentioned target file; and construct the target code graph corresponding to the above-mentioned target code repository according to the above-mentioned target directory tree, the above-mentioned first target entity and the above-mentioned first target entity relationship.
[0191] In some possible embodiments, when the processor 1010 executes the above-mentioned generation of a target directory tree according to the target directory, it is specifically used to execute: obtaining a second target entity and a second target entity relationship according to the target directory; the second target entity includes at least one of the following: a directory name, a file name; the second target entity relationship includes: an inclusion relationship; generating a target directory tree according to the second target entity and the second target entity relationship.
[0192] In some possible embodiments, the above-mentioned target file includes a target code file and / or a target other file; the above-mentioned first target entity includes at least one of the following: a target code entity corresponding to the above-mentioned target code file, and a target other entity corresponding to the above-mentioned target other file; the above-mentioned first target entity relationship includes at least one of the following: a target code entity relationship corresponding to the above-mentioned target code file, and a target other entity relationship corresponding to the above-mentioned target other file.
[0193] In some possible embodiments, when the processor 1010 executes the extraction of the first target entity and the first target entity relationship in the target file, it is specifically used to execute: when the target file includes the target code file, the target code file is parsed to obtain a target syntax tree, and the target code entity and the target code entity relationship are extracted from the target syntax tree; when the target file includes the target other files, the target other entities and the target other entity relationships are extracted from the target other files using a target large model.
[0194] In some possible embodiments, when the processor 1010 executes the above-mentioned use of the target big model to extract the target other entities and the target other entity relationships from the target other files, it is specifically used to execute: slicing the target other files to obtain multiple target other sub-files; inputting the target other sub-files into the target big model, and outputting the target other entities and the target other entity relationships in the target other sub-files.
[0195] In some possible embodiments, before the processor 1010 executes the parsing of the target code file to obtain the target syntax tree, it is also used to execute: when the target file includes the target code file, determine whether the target code file supports parsing; if the target code file supports parsing, execute the parsing of the target code file to obtain the target syntax tree.
[0196] In some possible embodiments, after the processor 1010 executes the above-mentioned determination of whether the target code file supports parsing when the target file includes the target code file, it is also used to execute: if the target code file does not support parsing, extracting the target code entity and the target code entity relationship from the target code file using the target macro model.
[0197] In some possible embodiments, when the processor 1010 executes the target code graph corresponding to the target code repository based on the target directory tree, the first target entity and the first target entity relationship, it is specifically used to execute: construct a target directory sub-graph based on the target directory tree; construct a target file sub-graph corresponding to each of the target files based on the first target entity in each of the target files and the first target entity relationship; construct a target code graph corresponding to the target code repository based on the target directory sub-graph and the target file sub-graph.
[0198] In some possible embodiments, before executing the above-mentioned extraction of the first target entity and the first target entity relationship in the above-mentioned target file, the above-mentioned processor 1010 is also used to execute: determining whether there is a corresponding cached target file sub-graph for the above-mentioned target file; if there is no corresponding cached target file sub-graph for the above-mentioned target file, then executing the above-mentioned step of extracting the first target entity and the first target entity relationship in the above-mentioned target file.
[0199] In some possible embodiments, the target code graph is composed of a plurality of target sub-graphs, and the plurality of target sub-graphs include a target directory sub-graph corresponding to the target directory and a target file sub-graph corresponding to each of the target files in the target code repository;
[0200] After executing the above-mentioned construction of the target code graph corresponding to the above-mentioned target code repository based on the above-mentioned target directory tree, the above-mentioned first target entity and the above-mentioned first target entity relationship, the above-mentioned processor 1010 is also used to execute: after detecting that the above-mentioned target code repository is updated, updating the above-mentioned specified target sub-graph according to the target update information corresponding to the above-mentioned target code repository; updating the above-mentioned target code graph according to the updated above-mentioned specified target sub-graph.
[0201] In some possible embodiments, the programming language used by the above-mentioned target code file includes at least one of the following: elm, javascript, ql, ocaml, elixir, typescript, go, c, elisp, php, r, python, java, ruby, rust, c++, c#.
[0202] In some possible embodiments, the above-mentioned target code entity includes at least one of the following: a class, a function, and a variable; and the above-mentioned target code entity relationship includes: a calling relationship.
[0203] In some possible embodiments, after executing the above-mentioned construction of the target code graph corresponding to the target code repository according to the above-mentioned target directory tree, the above-mentioned first target entity and the above-mentioned first target entity relationship, the above-mentioned processor 1010 is further configured to execute:
[0204] Convert the target code map into target base data; and save the target base data in a local Sqlite database.
[0205] In some possible embodiments, when the processor 1010 executes the above-mentioned acquisition of the target code repository, it is specifically used to execute: obtaining the code repository path; when the code repository path is a local code path, determining whether the code repository corresponding to the code repository path is a git repository; if the code repository corresponding to the code repository path is a git repository, determining the code repository as the target code repository.
[0206] In some possible embodiments, after the processor 1010 executes the above-mentioned determination of whether the code repository corresponding to the code repository path is a git repository when the code repository path is a local code path, it is also used to execute: if the code repository corresponding to the code repository path is not a git repository, the code repository corresponding to the code repository path is initialized to obtain the target code repository.
[0207] In some possible embodiments, when the processor 1010 executes the above-mentioned acquisition of the target code repository, it is specifically used to execute: acquiring a code repository path; and when the code repository path is a remote repository path, cloning according to the code repository path to obtain the target code repository.
[0208] In some possible embodiments, the processor 1010 is further used to execute: receiving a target instruction; the target instruction carries target task information related to the target code repository; in response to the target instruction, executing the target task corresponding to the target task information based on the target code map to obtain a target execution result.
[0209] The embodiment of the present application also provides a computer storage medium, which stores instructions, and when the instructions are executed on a computer or a processor, the computer or the processor executes one or more steps in any of the above methods. If the components of the above code map construction device are implemented in the form of software functional units and sold or used as independent products, they can be stored in the above storage medium.
[0210] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The above computer program product includes one or more computer instructions. When the above computer program instructions are loaded and executed on a computer, the above process or function according to the embodiment of the present application is generated in whole or in part. The above computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The above computer instructions may be stored in a computer-readable storage medium or transmitted through the above computer-readable storage medium. The above computer instructions may be transmitted from a website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The above computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The above available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid state disk (SSD)), etc.
[0211] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing the relevant hardware through a computer program, and the program can be stored in a computer-readable storage medium. When the program is executed, it can include the processes of the embodiments of the above-mentioned methods. The aforementioned storage medium includes: ROM, RAM, magnetic disk or optical disk and other media that can store program codes. In the absence of conflict, the technical features in this embodiment and the implementation scheme can be combined arbitrarily.
[0212] The above-mentioned embodiments are merely preferred embodiments of the present application and are not intended to limit the scope of the present application. Without departing from the design spirit of the present application, various modifications and improvements made to the technical solutions of the present application by ordinary technicians in this field should fall within the protection scope determined by the claims of the present application.
Claims
1. A code graph construction method, characterized in that: include: Get the target code repository; The target code repository includes a target directory and a target file; Generate a target directory tree according to the target directory; The target directory tree is used to represent the relationship between the target directory and the target file; Extracting a first target entity and a first target entity relationship from the target file; Constructing a target code graph corresponding to the target code repository according to the target directory tree, the first target entity and the relationship between the first target entities; The extracting the first target entity and the first target entity relationship in the target file includes: In the case where the target file includes a target code file, parsing the target code file to obtain a target syntax tree, and extracting target code entities and target code entity relationships from the target syntax tree; In the case where the target file includes target other files, extracting target other entities and target other entity relationships from the target other files using the target big model; the target other files include text information, and the target other entity relationships include association relationships between the target other entities; The method of extracting target other entities and target other entity relationships from the target other files by using the target big model includes: slicing the target other files to obtain multiple target other sub-files; inputting the target other sub-files into the target big model, and outputting the target other entities and the target other entity relationships in the target other sub-files.
2. The method according to claim 1, characterized in that The target file includes a target code file and / or a target other file; the first target entity includes at least one of the following: a target code entity corresponding to the target code file, a target other entity corresponding to the target other file; the first target entity relationship includes at least one of the following: a target code entity relationship corresponding to the target code file, a target other entity relationship corresponding to the target other file.
3. The method according to claim 1, characterized in that Before parsing the target code file to obtain the target syntax tree, the method further includes: In the case where the target file includes the target code file, determining whether the target code file supports parsing; If the target code file supports parsing, the step of parsing the target code file to obtain a target syntax tree is performed.
4. The method according to claim 1, characterized in that The step of constructing a target code graph corresponding to the target code repository according to the target directory tree, the first target entity and the relationship between the first target entities includes: Construct a target directory sub-map according to the target directory tree; Constructing a target file sub-graph corresponding to each target file according to the first target entity in each target file and the relationship between the first target entity; A target code map corresponding to the target code repository is constructed according to the target directory sub-map and the target file sub-map.
5. The method according to claim 1, characterized in that Before extracting the first target entity and the first target entity relationship in the target file, the method further includes: Determine whether there is a corresponding cached target file sub-graph for the target file; If the target file does not have a corresponding cached target file sub-graph, the step of extracting the first target entity and the first target entity relationship in the target file is performed.
6. The method according to claim 1, characterized in that The target code graph is composed of a plurality of target sub-graphs, wherein the plurality of target sub-graphs include a target directory sub-graph corresponding to the target directory and a target file sub-graph corresponding to each of the target files in the target code repository; After constructing the target code graph corresponding to the target code repository according to the target directory tree, the first target entity and the first target entity relationship, the method further includes: After detecting that the target code repository is updated, updating the specified target sub-graph according to the target update information corresponding to the target code repository; The target code map is updated according to the updated designated target sub-map.
7. A code map construction device, characterized in that: include: Acquisition module, used to obtain the target code repository; The target code repository includes a target directory and a target file; A generating module, used for generating a target directory tree according to the target directory; The target directory tree is used to represent the relationship between the target directory and the target file; An extraction module, used for extracting a first target entity and a first target entity relationship in the target file; A construction module, configured to construct a target code graph corresponding to the target code repository according to the target directory tree, the first target entity and the relationship between the first target entities; The extraction module is specifically used for: when the target file includes a target code file, parsing the target code file to obtain a target syntax tree, and extracting target code entities and target code entity relationships from the target syntax tree; when the target file includes target other files, extracting target other entities and target other entity relationships from the target other files using a target macro model; The target other files include text information, and the target other entity relationships include association relationships between the target other entities; The extraction module includes: a slicing unit, which is used to slice the target other files to obtain multiple target other sub-files when the target file includes the target other files; The output unit is used to input the target other sub-files into the target big model, and output the target other entities and the target other entity relationships in the target other sub-files.
8. An electronic device, characterized in that: include: A processor and a memory; wherein the memory stores an executable program code, and the processor runs a program corresponding to the executable program code by reading the executable program code stored in the memory, so as to execute the method according to any one of claims 1 to 6.
9. A computer storage medium, characterized in that The computer storage medium stores a plurality of instructions, which are suitable for being loaded by a processor and executing the method steps according to any one of claims 1-6.
Citation Information
Patent Citations
Project knowledge tree construction and retrieval method
CN111368090A
Knowledge graph construction and query method and device, equipment, storage medium and product
CN115827487A