Code retrieval method and device

By scanning and matching code branches and keywords of the code repository on the server, the problem of permission management of code retrieval is solved, and efficient and reliable display of code retrieval results is achieved.

CN120386807APending Publication Date: 2025-07-29BEIJING BAILONG MAYUN TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510322888.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-18
Publication Date
2025-07-29

AI Technical Summary

Technical Problem

In the prior art, code retrieval requires R&D personnel to have code access rights for the project, resulting in high management costs and affecting work efficiency.

Method used

By obtaining scanning instructions, scan the target code branch in the code repository to the project directory of the server, and receive code search instructions, obtain relevant code segments related to the keyword to be matched, generate search results and display them in the graphical user interface, supporting fuzzy, precise and regular matching methods.

Benefits of technology

Improves the efficiency and reliability of code retrieval, reduces the complexity and latency of permission management, and improves the user experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120386807A_ABST
    Figure CN120386807A_ABST
Patent Text Reader

Abstract

The embodiment of the invention relates to the field of data processing, and discloses a code retrieval method and device, and the method comprises the steps: obtaining a scanning instruction for scanning a target item; scanning a target code branch in a code warehouse to an item directory of a server based on the scanning instruction; receiving a code retrieval instruction for retrieving the target item; obtaining a related code segment related to a to-be-matched keyword in the code retrieval instruction under the item directory; and based on the related code segment, generating a retrieval result corresponding to the code retrieval instruction under the item directory, and displaying the retrieval result at a target position of the graphical user interface. By applying the technical scheme of the invention, the efficiency and reliability of code retrieval can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] Embodiments of the present invention relate to the field of data processing, and particularly to a code retrieval method and apparatus. Background Art

[0002] With the rapid development of information technology, the scale of software development has been expanding day by day, and the amount of code has also increased sharply. During the software development and maintenance process, code retrieval has become a crucial link. When performing code retrieval in related technologies, it is often required that developers must have the code access permission of the project to view and retrieve the content of the code library. Therefore, when developers perform code retrieval, they need to go through a strict approval process to obtain the corresponding code access permission, which increases the management cost and may affect the work efficiency due to the delay in permission allocation. Summary of the Invention

[0003] In view of the above problems, embodiments of the present invention provide a code retrieval method and apparatus.

[0004] According to one aspect of the embodiments of the present invention, a code retrieval method is provided. The method includes: obtaining a scan instruction for scanning a target project; scanning a target code branch in a code repository to a project directory on a server based on the scan instruction; receiving a code retrieval instruction for retrieving the target project; obtaining relevant code segments related to the keyword to be matched in the code retrieval instruction in the project directory; generating a retrieval result corresponding to the code retrieval instruction in the project directory based on the relevant code segments, and displaying the retrieval result at a target position in a graphical user interface. Through the above process, the efficiency and reliability of code retrieval can be improved.

[0005] In an alternative embodiment, scanning a target code branch in a code repository to a project directory on a server based on the scan instruction includes:

[0006] obtaining the number of projects and the project types of the target project based on the scan instruction;

[0007] if the number of projects is single or multiple target projects all belong to the same project type, obtaining all code branch information associated with the target project in the code repository based on the project type;

[0008] screening out a first code branch that meets the scan condition according to a first scan condition specified in the scan instruction, and scanning the first code branch to the project directory on the server, where the target code branch includes the first code branch.

[0009] In an alternative embodiment, scanning a target code branch in a code repository to a project directory on a server based on the scan instruction further includes:

[0010] If multiple target projects do not belong to the same project type, group the multiple target projects based on the project type to obtain project category groups;

[0011] For any project category group, respectively obtain all code branch information associated with the project category group in the code repository;

[0012] According to the second scanning condition specified in the scanning instruction, filter out the second code branches that meet the scanning conditions, and scan the second code branches to the corresponding project directory on the server side. The target code branches include the second code branches.

[0013] In an alternative implementation, obtain relevant code segments related to the keywords to be matched in the code retrieval instruction under the project directory, including:

[0014] Obtain the project name and project content of the project to be retrieved carried by the code retrieval instruction;

[0015] Based on the project name, obtain the project directory corresponding to the project to be retrieved;

[0016] Based on the project content, perform keyword matching on the target code branches under the project directory to filter out relevant code segments related to the keywords to be matched in the code retrieval instruction.

[0017] In an alternative implementation, based on the project content, perform keyword matching on the target code branches under the project directory, including:

[0018] Obtain one or more keywords to be matched carried in the project content;

[0019] Perform fuzzy keyword matching on the target code branches under the project directory based on the keywords to be matched. Fuzzy keyword matching is used to indicate that matching is performed when some characters of the keywords to be matched are similar or close to the code segment characters in the target code branches; or,

[0020] Perform exact keyword matching on the target code branches under the project directory based on the keywords to be matched. Exact keyword matching is used to indicate that matching is performed when all characters and character positions of the keywords to be matched are exactly the same as the code segment characters and character positions in the target code branches; or,

[0021] Perform regular matching on the target code branches under the project directory based on the keywords to be matched, and send an exception notification when the matching fails. Regular matching is used to indicate that matching is performed when the code segment character combination of the keywords to be matched in the target code branches conforms to the preset regular expression rule.

[0022] In an alternative embodiment, based on the relevant code segments, retrieval results corresponding to the code retrieval instruction are generated under the project directory and displayed at the target position in the graphical user interface, including:

[0023] Obtain the location information of the relevant code segments in the project directory and the code content of the relevant code segments;

[0024] Generate display data for the retrieval results according to the location information and the code content;

[0025] Pass the display data to the graphical user interface to display the retrieval results at the target position.

[0026] In an alternative embodiment, obtaining the relevant code segments related to the keyword to be matched in the code retrieval instruction under the project directory further includes:

[0027] Obtain the retrieval type corresponding to the code retrieval instruction;

[0028] If the retrieval type is project dependency retrieval, obtain the file paths of the main entry file and the routing file of the target project based on the dependency retrieval engine;

[0029] Use the abstract syntax tree to parse the file paths to obtain the dependency relationship between the main entry file and the routing file;

[0030] Based on the flattened list corresponding to the dependency relationship, search for the relevant code segments related to the keyword to be matched.

[0031] In an alternative embodiment, obtaining the relevant code segments related to the keyword to be matched in the code retrieval instruction under the project directory further includes:

[0032] If the retrieval type is project library directory retrieval, obtain all code branches under the project directory based on the directory retrieval engine;

[0033] Traverse all code branches to locate the code files containing the keyword to be matched;

[0034] Based on the file content matching algorithm, search for the relevant code segments that exactly match or are fuzzy matched with the keyword to be matched in the code files.

[0035] In an alternative embodiment, the above method further includes:

[0036] Periodically or aperiodically, scan the new code branches of the target project and the new code of the target code branches from the code repository to the project directory on the server;

[0037] For the target project with the retrieval type of project dependency retrieval, generate an abstract syntax tree cache file based on the new code branches and the new code of the target code branches;

[0038] When receiving a code retrieval instruction for project dependency retrieval of a target project, the abstract syntax tree cache file is preferentially retrieved.

[0039] According to another aspect of the embodiments of the present invention, there is provided a code retrieval device, including: a project scanning and obtaining module, configured to obtain a scanning instruction for scanning a target project; a code scanning module, configured to scan a target code branch in a code repository to a project directory on a server based on the scanning instruction; a code retrieval module, configured to receive a code retrieval instruction for retrieving the target project; a code segment obtaining module, configured to obtain relevant code segments related to a keyword to be matched in the code retrieval instruction under the project directory; and a result generating module, configured to generate a retrieval result corresponding to the code retrieval instruction under the project directory based on the relevant code segments and display it at a target position in a graphical user interface. Through the above modules, the efficiency and reliability of code retrieval can be improved.

[0040] According to another aspect of the embodiments of the present invention, there is provided a computer device, including: a processor, a memory, a communication interface, and a communication bus. The processor, the memory, and the communication interface complete communication with each other through the communication bus; the memory is used to store at least one executable instruction, and the executable instruction causes the processor to execute the operations of the foregoing code retrieval method.

[0041] According to yet another aspect of the embodiments of the present invention, there is provided a computer-readable storage medium, in which at least one executable instruction is stored, and the executable instruction causes a computer device / device to execute the operations of the foregoing code retrieval method.

[0042] According to yet another aspect of the embodiments of the present invention, there is provided a computer program product, including computer instructions, and the computer instructions are used to cause a computer to execute the operations of the code retrieval method in the first aspect or any corresponding embodiment thereof described above.

[0043] The above description is only an overview of the technical solutions of the embodiments of the present invention. In order to be able to understand the technical means of the embodiments of the present invention more clearly, it can be implemented according to the content of the specification. And in order to make the above and other purposes, features, and advantages of the embodiments of the present invention more obvious and understandable, the following specifically illustrates the embodiments of the present invention. Description of the Drawings

[0044] The drawings are only used to illustrate the embodiments and are not considered to be a limitation of the present invention. Moreover, throughout the drawings, the same reference numerals are used to represent the same components. In the drawings:

[0045] Figure 1 A schematic flowchart of a code retrieval method provided by the present invention is shown;

[0046] Figure 2Shows another schematic flowchart of a code retrieval method provided by the present invention;

[0047] Figure 3 Shows a schematic diagram of generating a scan instruction for a code retrieval method provided by the present invention;

[0048] Figure 4 Shows a schematic diagram of a first retrieval result for a code retrieval method provided by the present invention;

[0049] Figure 5 Shows a schematic diagram of a second retrieval result for a code retrieval method provided by the present invention;

[0050] Figure 6 Shows a schematic structural diagram of a code retrieval device provided by the present invention;

[0051] Figure 7 Shows a schematic structural diagram of a computer device provided by the present invention. Detailed implementation

[0052] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present invention. Apparently, the described embodiments are some, but not all, of the embodiments of the present invention. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.

[0053] Figure 1 Shows a flowchart of a first embodiment of a code retrieval method of the present invention. As Figure 1 shown, the method includes the following steps:

[0054] Step 110, obtaining a scan instruction for scanning a target project.

[0055] As above, by obtaining a scan instruction for scanning a target project, various code files stored in the code library for the target project can be comprehensively searched based on this scan instruction, including but not limited to source code, configuration files, and script files, etc. The trigger of the scan instruction can come from the direct operation of the user or be automatically executed based on a preset rule.

[0056] Specifically, please refer to Figure 2, the above scanning instructions may include the project type, project name, gitlab HTTP (gitlab address), branch, project name, pnpm multi-package management project, packageName, scanning method, and scanning conditions scanRules: scanRules of the newly added projects in the project set. Among them, scanRules can be further refined into various specific scanning rules, such as filtering by file type, searching according to keywords, or sorting priorities based on code change frequency, etc. The setting of these scanning rules enables users to flexibly customize the scope and depth of scanning according to their actual needs, so as to retrieve the required code information more efficiently.

[0057] In addition, information such as the project type and project name in the scanning instructions helps the server accurately locate the target project, ensuring the accuracy and pertinence of the scanning. Parameters such as gitlab HTTP, branch, and project name provide the necessary information for connecting to the target project's code repository, enabling the system to successfully access and search various code files in the code repository. For pnpm multi-package management projects, the corresponding packageName parameter can be specified based on their multi-package management characteristics, so as to scan a specific package in the project accurately, further improving the accuracy and efficiency of the scanning.

[0058] Step 120, scan the target code branch in the code repository to the project directory on the server based on the scanning instructions.

[0059] Among them, when scanning the target code branch in the code repository to the project directory on the server based on the scanning instructions, the rapid migration and centralized management of the code can be realized. The code branch in the server project directory is not only convenient for subsequent retrieval and analysis, but also can effectively prevent the code from being lost or tampered with.

[0060] In some alternative embodiments, when scanning the target code branch in the code repository to the project directory on the server based on the scan instruction, the number of projects and the project types of the target projects can be obtained based on the scan instruction; if the number of projects is single or multiple target projects all belong to the same project type, all code branch information associated with the target project in the code repository can be obtained based on the project type; according to the first scan condition specified in the scan instruction, the first code branches that meet the scan condition are filtered out, and the first code branches are scanned to the project directory on the server, and the target code branch includes the first code branches. If multiple target projects do not belong to the same project type, the multiple target projects are grouped based on the project type to obtain project category groups; for any project category group, all code branch information associated with the project category group in the code repository is obtained respectively; according to the second scan condition specified in the scan instruction, the second code branches that meet the scan condition are filtered out, and the second code branches are scanned to the corresponding project directory on the server, and the target code branch includes the second code branches.

[0061] Further, when filtering out the code branches that meet the scan condition, the first scan condition and the second scan condition can include but are not limited to the creation time, update frequency, code quality score, etc. of the code branches. For example, it can be set to only scan the code branches created or updated within the last month, or only scan the code branches with a code quality score higher than a certain threshold. In addition, when scanning the filtered code branches to the project directory on the server, the scanning process can be further monitored and recorded. For example, information such as the scanning time, scanning result (success or failure), and failure reason of each code branch can be recorded to facilitate subsequent traceability and analysis of the scanning process. At the same time, to improve the scanning efficiency and accuracy, a parallel scanning method can also be adopted. That is, multiple scanning tasks are started simultaneously, and each task is responsible for scanning one or more code branches. In this way, the scanning time can be significantly shortened, and the response speed of code retrieval can be improved.

[0062] Step 130, receive a code retrieval instruction for retrieving the target project.

[0063] As above, by receiving a code retrieval instruction for retrieving the target project, code retrieval of the target project is performed based on the code retrieval instruction.

[0064] Specifically, the code retrieval instruction can include various retrieval conditions, such as keywords, code types, code functions, etc. According to these retrieval conditions, it is possible to search in the code branches that have been scanned and stored in the server project directory to find the code branches that meet the conditions. The retrieval results can be displayed in a list form, including key information such as the name of the code branch, creation time, update frequency, code quality score, etc., so as to facilitate users to quickly locate the required code resources. In addition, in order to improve the user experience, functions such as code preview and code comparison can also be provided to help users more intuitively understand the code content and differences.

[0065] Step 140, obtain relevant code segments in the project directory that are related to the keywords to be matched in the code retrieval instruction.

[0066] As described above, by obtaining relevant code segments in the project directory that are related to the keywords to be matched in the code retrieval instruction, the retrieval results can be further refined. Specifically, according to the keywords to be matched input by the user, it is possible to match in the code segments in the project directory to find the code segments that contain these keywords. These relevant code segments can be highlighted for the user to quickly identify. At the same time, context information of the code segments, such as the file where they are located, the function where they are located, the class where they are located, etc., can also be provided to help users better understand the specific application scenarios of the code segments. In addition, in order to improve the accuracy and efficiency of the retrieval, a matching algorithm based on semantic analysis can also be adopted to more accurately match the keywords to be matched input by the user and the relevant code segments.

[0067] In some alternative embodiments, when obtaining relevant code segments in the project directory that are related to the keywords to be matched in the code retrieval instruction, the project name and project content (scanned content) of the project to be retrieved carried by the code retrieval instruction can be obtained; based on the project name, obtain the project directory corresponding to the project to be retrieved; based on the project content, perform keyword matching (keyword scanning) on the target code branches in the project directory to filter out relevant code segments that are related to the keywords to be matched in the code retrieval instruction.

[0068] In some alternative embodiments, when performing keyword matching on the target code branch under the project directory based on the project content, one or more keywords to be matched carried in the project content can be obtained first; fuzzy keyword matching is performed on the target code branch under the project directory based on the keywords to be matched, where fuzzy keyword matching is used to indicate that matching is performed when some characters of the keywords to be matched are similar or close to the characters of the code segment in the target code branch; or, exact keyword matching is performed on the target code branch under the project directory based on the keywords to be matched, where exact keyword matching is used to indicate that matching is performed when all characters and character positions of the keywords to be matched are exactly the same as the characters and character positions of the code segment in the target code branch; or, regular matching is performed on the target code branch under the project directory based on the keywords to be matched, and an exception notification is given when the matching fails, where regular matching is used to indicate that matching is performed when the character combination of the keywords to be matched in the code segment of the target code branch conforms to a preset regular expression rule.

[0069] Furthermore, to improve the efficiency and accuracy of code retrieval, the target code branch under the project directory can also be preprocessed before performing keyword matching. The preprocessing steps can include: unifying the encoding format of the code files under the project directory to avoid matching errors caused by inconsistent encoding formats; cleaning the comments and whitespace characters in the code files to reduce the interference of irrelevant information on the matching results; standardizing identifiers such as variable names and function names in the code files, for example, converting uppercase letters to lowercase letters, or replacing identifiers under a specific naming rule with a unified identifier, to improve the flexibility of matching.

[0070] In addition, when implementing keyword matching, the matching strategy can also be flexibly selected according to actual needs. For example, in the initial screening stage, fuzzy keyword matching can be adopted to quickly locate the code segments that may contain the keywords to be matched; in the exact screening stage, exact keyword matching can be adopted to ensure the accuracy of the matching results; when complex patterns or rules need to be matched, regular matching can be adopted, such as specifying which file types, directories, or specific code segments need to be scanned, as well as the keywords or regular expression patterns to be searched, to more accurately locate potential security hazards or code quality issues, reduce unnecessary scanning overhead, and improve scanning efficiency. At the same time, to enhance the user experience, detailed exception notifications can also be provided when the matching fails, including the reasons for the matching failure, possible solutions, etc., so that users can quickly locate the problems and make repairs.

[0071] In specific implementation, when performing keyword matching on the target code branch under the project directory based on the project content, it is defaulted to fuzzy keyword matching, which means that as long as the keyword appears regardless of its position, and the corresponding expression is new RegExp(`(${match})`,"g"). Exact keyword matching means that only when the entire field is the same as the search keyword can it be matched, and the corresponding expression is new RegExp(`\\b(${match})\\b`,"g").

[0072] Step 150: Generate a retrieval result corresponding to the code retrieval instruction under the project directory based on the relevant code segments and display it at the target position on the graphical user interface.

[0073] As above, by generating a retrieval result corresponding to the code retrieval instruction under the project directory based on the relevant code segments and displaying it at the target position on the graphical user interface, it is convenient for users to intuitively view and understand the retrieved code information. In addition, in order to improve the retrieval efficiency, the retrieval result can be further sorted, for example, sorted according to factors such as code occurrence frequency, code modification time, or code relevance, so that users can find the required code segments more quickly.

[0074] In some alternative implementation manners, when generating a retrieval result corresponding to the code retrieval instruction under the project directory based on the relevant code segments and displaying it at the target position on the graphical user interface, the position information of the relevant code segments in the project directory and the code content of the relevant code segments can be obtained first; according to the position information and the code content, display data of the retrieval result is generated; the display data is assembled into an interface response body and passed to the graphical user interface to display the retrieval result at the target position. Among them, the retrieval result can be exported in multiple formats (such as CSV, JSON, Excel) by calling an open-source plugin, which is convenient for users to further analyze and archive, and improves work efficiency.

[0075] Furthermore, in order to enhance the user experience, some additional information can also be attached to the display data, such as comment information of the code segment, author information of the code segment, modification history of the code segment, etc. These information can help users better understand the background and function of the code segment, so as to perform code retrieval and utilization more effectively. In addition, in order to improve the accuracy and efficiency of code retrieval, the retrieval result can also be filtered, for example, filtered according to factors such as the language type of the code segment and the project to which the code segment belongs, so as to further narrow the retrieval scope and improve the accuracy and efficiency of retrieval.

[0076] In some alternative embodiments, when generating a search result corresponding to a code search instruction under a project directory based on relevant code segments and displaying it at a target position in a graphical user interface, the search result can also be presented in a list form, with each list item corresponding to a matched target code branch. The name, path, and the position of the matched keyword of the target code branch are displayed in the list item, as Figure 3 shown. The search result can also be presented in a tree structure, where the nodes of the tree structure correspond to different levels of the project directory, and the leaf nodes correspond to the matched target code branches. The name, path, and the position of the matched keyword of the target code branch are displayed in the leaf nodes. The search result can also be presented in the form of a tab page in a graphical interface. Each tab page corresponds to a keyword to be matched, and all the target code branches matched with the keyword to be matched, as well as the name, path, and the position of the matched keyword of each target code branch, are displayed in the tab page. The matched keyword is highlighted in the relevant code segments of the target code branch, as Figure 4 shown. That is, the element tags are obtained through the document.getElementsByTagName API, the text nodes are collected through the document.createTreeWalker API, the text range of the keyword is obtained, and a highlighting instance is created using the Highlight API. Finally, the highlighting style is customized through the CSS.highlights static property.

[0077] In some alternative embodiments, when generating a search result corresponding to a code search instruction under a project directory based on relevant code segments and displaying it at a target position in a graphical user interface, the position information of the relevant code segments in the project directory and the content summary of the relevant code segments can also be obtained; based on the position information and the content summary, display data of the search result is generated; and the display data is passed to the graphical user interface to display the search result at the target position. The target position can be a display area preset by the user or a system default display area, facilitating the user to view and analyze the relevant code segments.

[0078] The code search method according to the embodiments of the present invention can improve the efficiency and reliability of code search by obtaining a scan instruction for scanning a target project to scan the target code branches in a code repository to a project directory on a server based on the scan instruction; receiving a code search instruction for searching the target project to obtain relevant code segments related to the keyword to be matched in the code search instruction under the project directory; and generating a search result corresponding to the code search instruction under the project directory based on the relevant code segments and displaying it at a target position in a graphical user interface.

[0079] Figure 5 shows a flowchart of another embodiment of the code search method of the present invention. AsFigure 5 As shown in the figure, the method includes the following steps:

[0080] Step 510: Obtain a scan instruction for scanning a target project.

[0081] For details, please refer to Figure 1 Step 110 of the embodiment shown, which will not be elaborated here.

[0082] Step 520: Based on the scan instruction, scan the target code branch in the code repository to the project directory of the server.

[0083] For details, please refer to Figure 1 Step 120 of the embodiment shown, which will not be elaborated here.

[0084] Step 530: Receive a code retrieval instruction for retrieving the target project.

[0085] For details, please refer to Figure 1 Step 130 of the embodiment shown, which will not be elaborated here.

[0086] Step 540: Obtain relevant code segments in the project directory that are related to the keywords to be matched in the code retrieval instruction.

[0087] Specifically, the above step 540 includes:

[0088] Step 5401: Obtain the retrieval type corresponding to the code retrieval instruction.

[0089] As above, by obtaining the retrieval type corresponding to the code retrieval instruction, the matching range of the keywords to be matched is determined, such as project dependency retrieval, project library directory retrieval, etc. According to the determined matching range, relevant code segments related to the keywords to be matched are searched in the target code branch in the project directory. In this way, the code segments required by the user can be more accurately located, improving the efficiency and accuracy of code retrieval.

[0090] Step 5402: If the retrieval type is project dependency retrieval, obtain the file paths of the main entry file and the routing file of the target project based on the dependency retrieval engine.

[0091] As above, if the retrieval type is project dependency retrieval, obtain the file paths of the main entry file and the routing file of the target project based on the dependency retrieval engine to parse the dependency relationships in the main entry file and the routing file.

[0092] Step 5403: Use an abstract syntax tree to parse the file paths to obtain the dependency relationships between the main entry file and the routing file.

[0093] As described above, the file path is parsed through the abstract syntax tree to obtain the dependency relationship between the main entry file and the routing file, and the parsed dependency relationship can be used to construct the dependency graph of the project, so as to visually display the dependencies between various files, modules or components in the project. Such a dependency graph helps developers better understand the structure of the project, as well as the associations and dependencies between various parts, providing strong support for subsequent code maintenance, optimization or refactoring.

[0094] Step 5404, based on the flattened list corresponding to the dependency relationship, search for relevant code segments related to the keyword to be matched.

[0095] As described above, by searching for relevant code segments related to the keyword to be matched based on the flattened list corresponding to the dependency relationship, the target code can be located more efficiently, reducing the time cost of manually flipping through the code. In the flattened list, each dependency relationship is clearly displayed, and developers only need to search for the relevant code segments according to the keyword to be matched. This search method not only improves the efficiency of code retrieval but also reduces the risk of code omission caused by human negligence, providing great convenience for the development and maintenance of the project.

[0096] In some alternative embodiments, when obtaining relevant code segments related to the keyword to be matched in the code retrieval instruction under the project directory, if the retrieval type is project library directory retrieval, all code branches under the project directory are obtained based on the directory retrieval engine; all code branches are traversed to locate the code file containing the keyword to be matched; based on the file content matching algorithm, relevant code segments that exactly match or are fuzzy-matched with the keyword to be matched are searched in the code file.

[0097] After locating the code file containing the keyword to be matched, in order to improve the accuracy of matching, the code file can be further preprocessed, such as removing comments, blank lines and formatting the code, etc., to reduce the interference of irrelevant information. Then, using the file content matching algorithm, such as regular expression matching, string matching or semantic matching, etc., relevant code segments that exactly match or are fuzzy-matched with the keyword to be matched are searched in the preprocessed code file. Exact match means that the keyword to be matched is exactly the same as a certain string in the code file; while fuzzy match allows a certain degree of difference, such as partial match of the keyword, synonym match or context-based match, etc., to meet the retrieval requirements in different scenarios. In this way, the target code segment can be located more accurately, improving the accuracy and efficiency of code retrieval.

[0098] In some alternative embodiments, the newly added code branches of the target project and the newly added code of the target code branch can also be scanned from the code repository to the project directory on the server periodically or aperiodically; for the target project with the retrieval type of project dependency retrieval, an abstract syntax tree cache file is generated based on the newly added code branches and the newly added code of the target code branch; when receiving a code retrieval instruction for project dependency retrieval of the target project, the abstract syntax tree cache file is preferentially retrieved. By scanning and caching the newly added code in a timely manner, it can be ensured that the results of project dependency retrieval are always up-to-date, reducing retrieval delays or omissions caused by code updates. When generating the abstract syntax tree cache file, efficient data structures and storage methods can be adopted to enable quick access and matching during subsequent retrieval processes. When receiving a code retrieval instruction for project dependency retrieval of the target project, it will first check whether there is a corresponding abstract syntax tree cache file. If it exists, the retrieval is directly performed in this cache file to obtain dependency information related to the target project. This method can significantly improve the retrieval speed, reduce the retrieval cost, and at the same time ensure the accuracy and integrity of the retrieval results. In addition, the abstract syntax tree cache file can be updated and maintained regularly or irregularly according to actual needs to ensure that it always remains consistent with the latest code in the code repository.

[0099] Furthermore, during the process of keyword retrieval, the keywords retrieved by the user can also be automatically collected to understand the user's usage trends and optimize the system. At the same time, monitor the execution status of the scheduled tasks and the error reports during scanning, and push exception notifications in real time through the DingTalk message robot to ensure that problems are discovered and solved in a timely manner and to guarantee the user experience. Report operation behaviors, data cleaning, information statistics, and frequencies. Among them, the automatic collection function can report user information and the retrieved keywords by accessing the user's internal buried-point SDK when making an interface request. When a scheduled task fails to execute, the API interface capabilities provided by DingTalk are called to push exception notifications through the group robot.

[0100] Step 550, generate a retrieval result corresponding to the code retrieval instruction in the project directory based on the relevant code segments and display it at the target position in the graphical user interface.

[0101] For details, please refer to Figure 1 Step 150 of the illustrated embodiment, which will not be elaborated here.

[0102] In summary, the code retrieval method according to the embodiment of the present invention obtains a scan instruction for scanning a target project, and scans a target code branch in a code repository to a project directory on a server based on the scan instruction; receives a code retrieval instruction for retrieving the target project to obtain relevant code segments related to the keywords to be matched in the code retrieval instruction in the project directory; generates a retrieval result corresponding to the code retrieval instruction in the project directory based on the relevant code segments and displays it at a target position in a graphical user interface, which can improve the efficiency and reliability of code retrieval.

[0103] Figure 6 FIG. shows a schematic structural diagram of an embodiment of a code retrieval device according to the present invention. As Figure 6 shown, the device includes:

[0104] A project scanning module 610, configured to obtain a scan instruction for scanning a target project;

[0105] A code scanning module 620, configured to scan a target code branch in a code repository to a project directory on a server based on the scan instruction;

[0106] A code retrieval module 630, configured to receive a code retrieval instruction for retrieving the target project;

[0107] A code segment obtaining module 640, configured to obtain relevant code segments related to the keywords to be matched in the code retrieval instruction in the project directory;

[0108] A result generating module 650, configured to generate a retrieval result corresponding to the code retrieval instruction in the project directory based on the relevant code segments and display it at a target position in a graphical user interface.

[0109] In an optional implementation manner, the code scanning module 620 includes:

[0110] A data obtaining sub-module, configured to obtain the number of projects and the project types of the target project based on the scan instruction;

[0111] An information obtaining sub-module, configured to, if the number of projects is single or multiple target projects all belong to the same project type, obtain all code branch information associated with the target project in the code repository based on the project type;

[0112] A first scanning sub-module, configured to screen out a first code branch that meets the scanning conditions according to a first scanning condition specified in the scan instruction, and scan the first code branch to a project directory on a server, where the target code branch includes the first code branch.

[0113] In an optional implementation manner, the code scanning module 620 further includes:

[0114] A target project grouping sub-module, which is used to group multiple target projects based on project types to obtain project category groups if the multiple target projects do not belong to the same project type;

[0115] A branch information acquisition sub-module, which is used to respectively obtain all code branch information associated with a project category group in a code repository for any project category group;

[0116] A second scanning sub-module, which is used to screen out second code branches that meet the scanning conditions according to the second scanning conditions specified in the scanning instruction, and scan the second code branches to the corresponding project directory on the server side. The target code branches include the second code branches.

[0117] In an alternative embodiment, the code segment acquisition module 640 includes:

[0118] A retrieved project acquisition sub-module, which is used to obtain the project name and project content of the project to be retrieved carried by a code retrieval instruction;

[0119] A project directory acquisition sub-module, which is used to obtain the corresponding project directory of the project to be retrieved based on the project name;

[0120] A keyword matching sub-module, which is used to perform keyword matching on the target code branches in the project directory based on the project content to screen out relevant code segments related to the keywords to be matched in the code retrieval instruction.

[0121] In an alternative embodiment, the keyword matching sub-module includes:

[0122] A keyword to be matched acquisition unit, which is used to obtain one or more keywords to be matched carried in the project content;

[0123] A fuzzy keyword matching unit, which is used to perform fuzzy keyword matching on the target code branches in the project directory based on the keyword to be matched. The fuzzy keyword matching is used to indicate that matching is performed when some characters of the keyword to be matched are similar or close to the code segment characters in the target code branch; or,

[0124] An exact keyword matching unit, which is used to perform exact keyword matching on the target code branches in the project directory based on the keyword to be matched. The exact keyword matching is used to indicate that matching is performed when all characters and character positions of the keyword to be matched are exactly the same as the code segment characters and character positions in the target code branch; or,

[0125] A regular matching unit, which is used to perform regular matching on the target code branches in the project directory based on the keyword to be matched and send an exception notification when the matching fails. The regular matching is used to indicate that matching is performed when the code segment character combination of the keyword to be matched in the target code branch conforms to a preset regular expression rule.

[0126] In an alternative embodiment, the result generation module 650 includes:

[0127] A code segment information acquisition sub-module, configured to acquire the location information of a relevant code segment in the project directory and the code content of the relevant code segment;

[0128] A display data generation sub-module, configured to generate display data of the retrieval result according to the location information and the code content;

[0129] A retrieval result display sub-module, configured to pass the display data to the graphical user interface to display the retrieval result at the target location.

[0130] In an alternative embodiment, the code segment acquisition module 640 further includes:

[0131] A retrieval type acquisition sub-module, configured to acquire the retrieval type corresponding to the code retrieval instruction;

[0132] A file path acquisition sub-module, configured to, if the retrieval type is project dependency retrieval, acquire the file paths of the main entry file and the routing file of the target project based on the dependency retrieval engine;

[0133] A dependency relationship acquisition sub-module, configured to parse the file paths by using an abstract syntax tree to acquire the dependency relationship between the main entry file and the routing file;

[0134] A relevant code segment search sub-module, configured to search for relevant code segments related to the keyword to be matched based on the flattened list corresponding to the dependency relationship.

[0135] In an alternative embodiment, the code segment acquisition module 640 further includes:

[0136] A code branch acquisition sub-module, configured to, if the retrieval type is project library directory retrieval, acquire all code branches under the project directory based on the directory retrieval engine;

[0137] A code file location sub-module, configured to traverse all code branches to locate the code file containing the keyword to be matched;

[0138] A relevant code segment search sub-module, configured to search for relevant code segments that exactly match or ambiguously match the keyword to be matched in the code file based on the file content matching algorithm.

[0139] In an alternative embodiment, the code retrieval module 630 is further configured to periodically or aperiodically scan the newly added code branches of the target project and the newly added code of the target code branch from the code repository to the project directory on the server; for the target project with the retrieval type of project dependency retrieval, generate an abstract syntax tree cache file based on the newly added code branches and the newly added code of the target code branch; and when receiving a code retrieval instruction for performing project dependency retrieval on the target project, preferentially retrieve the abstract syntax tree cache file.

[0140] The further functional descriptions of the above-mentioned modules and units are the same as those in the corresponding method embodiments described above, and will not be repeated here.

[0141] Through the above device and its components, the technical solution provided by the embodiments of the present invention has the following advantages:

[0142] The code retrieval device according to the embodiments of the present invention obtains a scan instruction for scanning a target project through a project scanning acquisition module; a code scanning module scans the target code branch in the code repository to the project directory on the server based on the scan instruction; a code retrieval module receives a code retrieval instruction for retrieving the target project; a code segment acquisition module obtains relevant code segments related to the keyword to be matched in the code retrieval instruction in the project directory; and a result generation module generates a retrieval result corresponding to the code retrieval instruction in the project directory based on the relevant code segments and displays it at a target position on the graphical user interface. Through the above modules, the efficiency and reliability of code retrieval can be improved.

[0143] Please refer to Figure 7 , Figure 7 which is a schematic structural diagram of a computer device provided by an alternative embodiment of the present invention. As Figure 7 shown, the computer device includes: one or more processors 710, a memory 720, and interfaces for connecting various components, including a high-speed interface and a low-speed interface. Each component communicates with each other using different buses and can be installed on a common main board or installed in other ways as needed. The processor can process instructions executed within the computer device, including instructions stored in the memory or on the memory to display graphical information of the GUI on an external input / output device (such as a display device coupled to the interface). In some alternative embodiments, if necessary, multiple processors and / or multiple buses can be used together with multiple memories and multiple memories. Similarly, multiple computer devices can be connected, and each device provides some necessary operations (for example, as a server array, a set of blade servers, or a multi-processor system). Figure 7 In

[0144] The processor 710 may be a central processing unit, a network processor, or a combination thereof. Among them, the processor 710 may further include a hardware chip. The above-mentioned hardware chip may be an application-specific integrated circuit, a programmable logic device, or a combination thereof. The above-mentioned programmable logic device may be a complex programmable logic device, a field-programmable gate array, a generic array logic, or any combination thereof.

[0145] Among them, the memory 720 stores instructions that can be executed by at least one processor 710, so that at least one processor 710 executes the method shown in the above embodiments.

[0146] The memory 720 may include a program storage area and a data storage area. Among them, the program storage area may store an operating system and application programs required for at least one function; the data storage area may store data created according to the use of a computer device presented by a kind of landing page of a small program, etc. In addition, the memory 720 may include a high-speed random access memory, and may also include a non-transitory memory, such as at least one disk storage device, a flash memory device, or other non-transitory solid-state storage devices. In some alternative embodiments, the memory 720 may optionally include a memory remotely provided with respect to the processor 710, and these remote memories may be connected to the computer device through a network. Examples of the above-mentioned network include but are not limited to the Internet, an enterprise intranet, a server cluster, a mobile communication network, and combinations thereof.

[0147] The memory 720 may include a volatile memory, for example, a random access memory; the memory may also include a non-volatile memory, for example, a flash memory, a hard disk, or a solid-state drive; the memory 720 may also include a combination of the above types of memories.

[0148] The computer device further includes a communication interface 730 for the computer device to communicate with other devices or communication networks.

[0149] An embodiment of the present invention also provides a computer-readable storage medium, and the storage medium stores at least one executable instruction. When the executable instruction runs on a computer device / code retrieval device, the computer device / code retrieval device executes the code retrieval method in any of the above method embodiments.

[0150] An embodiment of the present invention also provides a computer program product, including computer instructions for causing a computer to execute the code retrieval method in the first aspect or any corresponding embodiment thereof.

[0151] The algorithms or displays provided herein are not inherently related to any particular computer, virtual system, or other device. In addition, embodiments of the present invention are not directed to any particular programming language.

[0152] In the description provided herein, numerous specific details are set forth. It will be understood, however, that embodiments of the invention may be practiced without these specific details. Similarly, in order to streamline the present invention and assist in understanding one or more of the various inventive aspects, in the foregoing description of exemplary embodiments of the invention, various features of the embodiments of the invention are sometimes grouped together in a single embodiment, figure, or description thereof. Among them, the claims following the specific implementation manner are hereby expressly incorporated into the specific implementation manner, where each claim itself serves as a separate embodiment of the present invention.

[0153] Those skilled in the art will appreciate that the modules in the devices in the embodiments can be adaptively changed and arranged in one or more devices different from the embodiments. The modules or units or components in the embodiments can be combined into one module or unit or component, and in addition, they can be divided into multiple sub-modules or sub-units or sub-components. Except that at least some of such features and / or processes or units are mutually exclusive.

[0154] It should be noted that the above embodiments illustrate the present invention rather than limit the present invention, and those skilled in the art can design alternative embodiments without departing from the scope of the appended claims. In the claims, any reference signs placed between parentheses shall not be construed as limiting the claim. The word "comprising" does not exclude the presence of elements or steps not listed in the claim. The word "a" or "an" preceding an element does not exclude the presence of a plurality of such elements. The present invention can be implemented by means of hardware including several different elements and by means of a suitably programmed computer. In a unit claim listing several devices, several of these devices may be embodied by the same item of hardware. The use of the words first, second, and third, etc. does not denote any order. These words can be interpreted as names. The steps in the above embodiments, unless otherwise specified, should not be construed as limiting the order of execution.

Claims

1. A code retrieval method, characterized in that, The method comprises: Obtain a scanning instruction for scanning a target item; Scan the target code branch in the code repository to the project directory of the server based on the scanning instruction; receiving a code search instruction for searching the target item; Obtain relevant code segments under the project directory that are related to the keywords to be matched in the code search instruction; Based on the relevant code segments, a search result corresponding to the code search instruction is generated under the project directory and displayed at a target location in a graphical user interface.

2. The method according to claim 1, wherein Scanning the target code branch in the code repository to the project directory of the server based on the scanning instruction includes: Acquire the item quantity and item type of the target item based on the scanning instruction; If the number of projects is single or multiple target projects all belong to the same project type, then obtaining all code branch information associated with the target project in the code repository based on the project type; According to the first scanning condition specified in the scanning instruction, a first code branch that meets the scanning condition is screened out, and the first code branch is scanned into a project directory on the server, and the target code branch includes the first code branch.

3. The method according to claim 2, wherein Scanning the target code branch in the code repository to the project directory of the server based on the scanning instruction also includes: If the multiple target projects do not belong to the same project type, grouping the multiple target projects based on the project type to obtain a project category group; For any of the project category groups, respectively obtain all code branch information associated with the project category group in the code repository; According to the second scanning condition specified in the scanning instruction, a second code branch that meets the scanning condition is screened out, and the second code branch is scanned into a project directory corresponding to the server, and the target code branch includes the second code branch.

4. The method according to claim 1, wherein The step of obtaining the relevant code segments in the project directory that are related to the keywords to be matched in the code search instruction includes: Obtain the project name and project content of the project to be searched carried by the code search instruction; Obtaining a project directory corresponding to the project to be retrieved based on the project name; Based on the project content, keyword matching is performed on the target code branches under the project directory to screen out relevant code segments related to the to-be-matched keywords in the code retrieval instruction.

5. The method according to claim 4, wherein The keyword matching of the target code branches under the project directory based on the project content includes: Obtain one or more keywords to be matched carried in the project content; Perform fuzzy keyword matching on the target code branch under the project directory based on the keyword to be matched, wherein the fuzzy keyword matching is used to match when some characters of the keyword to be matched are similar or close to the code segment characters in the target code branch; or Performing precise keyword matching on the target code branch under the project directory based on the keyword to be matched, wherein the precise keyword matching is used to indicate that the matching is performed when all characters and character positions of the keyword to be matched are completely consistent with the characters and character positions of the code segment in the target code branch; or Based on the keyword to be matched, the target code branch under the project directory is matched regularly, and an exception notification is issued when the match fails. The regular matching is used to characterize that the code segment character combination of the keyword to be matched in the target code branch meets the preset regular expression rules.

6. The method according to claim 1, wherein The generating, based on the relevant code segments, a search result corresponding to the code search instruction in the project directory and displaying it at a target location in a graphical user interface includes: Obtaining location information of the relevant code segment in the project directory and code content of the relevant code segment; Generating display data of the search results according to the location information and code content; The presentation data is transmitted to a graphical user interface to display the search results at the target location.

7. The method according to claim 1, characterized in that The step of obtaining the relevant code segments in the project directory that are related to the keywords to be matched in the code search instruction further includes: Obtaining the search type corresponding to the code search instruction; If the search type is project dependency search, the file paths of the main entry file and routing file of the target project are obtained based on the dependency search engine; Parsing the file path using an abstract syntax tree to obtain a dependency relationship between the main entry file and the routing file; Based on the flattened list corresponding to the dependency relationship, relevant code segments related to the keyword to be matched are searched.

8. The method according to claim 7, characterized in that The step of obtaining the relevant code segments in the project directory that are related to the keywords to be matched in the code search instruction further includes: If the search type is a project library directory search, all code branches under the project directory are obtained based on the directory search engine; Traversing all the code branches to locate the code file containing the keyword to be matched; Based on a file content matching algorithm, the code file is searched for relevant code segments that fully match or fuzzily match the keyword to be matched.

9. The method according to claim 7, characterized in that: The method further comprises: Periodically or aperiodically, scanning the newly added code branch of the target project and the newly added code of the target code branch from the code repository to the project directory of the server; For the target project whose search type is project dependency search, generating an abstract syntax tree cache file based on the newly added code branch and the newly added code of the target code branch; When receiving a code retrieval instruction for performing a project dependency search on the target project, the abstract syntax tree cache file is preferentially retrieved.

10. A code retrieval device, characterized in that, The device comprises: A project scanning module is used to obtain a scanning instruction for scanning a target project; A code scanning module is used to scan the target code branch in the code repository to the project directory of the server based on the scanning instruction; A code retrieval module, configured to receive a code retrieval instruction for retrieving the target item; A code segment acquisition module, configured to acquire relevant code segments related to the keyword to be matched in the code retrieval instruction under the project directory; A result generation module, configured to generate a retrieval result corresponding to the code retrieval instruction under the project directory based on the relevant code segments, and display the result at a target position in the graphical user interface.