A commit query method for querying relevant code in a code repository
By using Lucene's incremental indexing method, we can compare the commitIDs before and after a version update, mark and update newly added, deleted, or modified files in the code repository, solve the problem of wasted time and cost in code repository version updates, and improve the work efficiency of programmers.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- XUANCAI INTERACTIVE NETWORK SCI & TECH
- Filing Date
- 2023-03-23
- Publication Date
- 2026-06-23
AI Technical Summary
Existing technologies require re-traversing all code files when updating code repository versions, resulting in wasted time and costs and reduced programmers' work efficiency.
An incremental indexing method based on Lucene is adopted. By comparing the commit IDs of the previous and next commits, the code files added, deleted or modified in the version update are marked and updated in the incremental index, avoiding the need to re-traverse all files.
Effectively manage code file changes during code repository version updates, saving time and operational costs, and improving programmers' work efficiency.
Smart Images

Figure CN116360843B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer technology, specifically a commit query method applicable to code repositories for querying related code. Background Technology
[0002] With the rapid development of the internet and the continuous improvement of network technology, the market demand for programming talent is constantly growing. When developing software, programmers typically utilize numerous existing code modules, modifying and combining them to complete the required code project. Therefore, code repositories have emerged. Programmers can better utilize existing code segments in repositories, greatly improving their programming efficiency and code utilization. Furthermore, code repositories effectively manage and store submitted code, significantly contributing to the advancement of both coding and software engineering.
[0003] As software, code repositories also undergo version updates and iterations. After each code repository version update, code files will be added, deleted, or modified between different code repository versions. This makes the management of open-source code files in the code repository a rather complex and tedious task. How to handle and manage these added, deleted, or modified code files plays a crucial role in subsequent version updates and system optimizations by programmers. Simple and efficient management helps save programmers the cost and time of searching for code files and improves the efficiency of completing version update tasks.
[0004] Document CN 108829432 A discloses a code synchronization method and system based on a code manager. This method compiles multiple source code files in a source code repository into multiple corresponding binary code files and stores the binary code files in the binary code repository. When an update is detected in the source code files in the source code repository, the updated source code files are compiled into updated binary code files and stored in the binary code repository. This method is complex, time-consuming, and wastes a lot of time and resources, reducing the work efficiency of programmers. Summary of the Invention
[0005] To address the aforementioned issues, this invention discloses a commit query method applicable to code repositories for querying related code.
[0006] An effective solution to the management of newly added code files in code repository version updates is to use an incremental indexing method based on Lucene. When the initial version of the code repository is indexed, all code files are traversed and added to the index one by one. In each subsequent version update, the method compares the commit IDs of the two commits to find the newly added, deleted, or modified code files in different versions, and updates these modified files to the incremental index. This avoids retracing all code files in the code repository, saving on the amount of operations in version updates. It ensures that all code files in each new version are added to the index, making it easier for programmers to search and manage these code files, saving significant time and improving the efficiency of code repository version updates.
[0007] This invention primarily utilizes Lucene to implement incremental indexing functionality. It marks newly added, deleted, and modified code files during code repository version updates and updates the corresponding incremental index. This facilitates subsequent version updates by programmers using the index to search, query, and manage the required code files, effectively assisting them in completing search and query operations and better fulfilling development tasks.
[0008] The technical solution of this invention is: a commit query method applicable to code repositories for querying related code, comprising the following steps:
[0009] Step 1: Use the features provided by Lucene to build an incremental index for the commits of code files in the code repository. For all subsequent code file commits that are traversed, build corresponding indexes for the commit information such as COMMIT_INDEX_VERSION, COMMIT_HASH, and META.
[0010] Step 2: In the initial version of the code repository, iterate through all the commits of the code files in the repository. During the iteration, record the commit information of the current code file and update the corresponding COMMIT_HASH index to know which commits have been indexed. Record the commit ID of the last commit so that when the code repository version is updated, it can index which subsequent commits have changed the code files. Step 3: When the code repository version is updated, obtain the commit ID of the latest commit after the update and the index version.
[0011] Step 4: Compare the information submitted in Step 3 with the previously updated submission information in the index to determine if the two pieces of information are the same. Obtain the newly added, deleted, or modified code files and update them in the incremental index. Update the index for the modified code files.
[0012] Step 5: Use incremental indexing to search, query, and manage different code files in the code repository.
[0013] Furthermore, in step 1, we first need to use Lucene to build an index. We reference the IndexWriter class in the Lucene package and set its isEmpty value to true, which means building an incremental index. We then write the required code file commit information into the index, such as: commit index version COMMIT_INDEX_VERSION, commit partition COMMIT_HASH, meta index META, latest committed index LAST_COMMIT, latest committed index version LAST_COMMIT_INDEX_VERSION, and latest committed partition LAST_COMMIT_HASH, thus completing the construction of the required index.
[0014] Furthermore, in step 2, RevWalk reads the repository logs and TreeWalk is used to traverse the code files in the repository. First, the revTree corresponding to the revision is obtained, which is the file node tree for this version. Then, the entry file corresponding to the revTree is obtained to perform traversal. In the initial version of the repository, the repository needs to be traversed. Each code file obtained by traversal is marked as read, indicating that the code file has been traversed. The commit ID of the latest commit is recorded, such as commitID and COMMIT_INDEX_VERSION, and updated to the COMMIT_HASH index to know which commits have been indexed. The commit ID of the last commit is recorded, and its commit information is updated to the LAST_COMMIT_INDEX_VERSION and LAST_COMMIT_HASH indexes to know which subsequent commits need to be indexed when the repository version is updated.
[0015] Furthermore, in step 3, Lucene needs to be used to build an index first, and the isEmpty value is set to false, that is, to build an incremental index. The required code files are marked and written into the index. After the index is built, all the code files marked in step 2 are added to the incremental index, and the corresponding LAST_COMMIT_INDEX_VERSION and LAST_COMMIT_HASH index contents are updated.
[0016] Furthermore, in step 4, when the code repository version is updated, the commitID of the latest commit after the version update is first obtained, and it is extracted together with the commitID before the version update. The commit information of the two commits is compared to obtain the code files that were added, deleted and modified before and after the version update, and the changes are updated in the incremental index.
[0017] Furthermore, newly added code files have their corresponding incremental indexes added, deleted code files have their corresponding incremental indexes deleted, and modified code files have their original incremental indexes deleted and new incremental indexes added for their new version files, thus completing the incremental index update.
[0018] Furthermore, step 5 completes the final search, query, and management of code files. First, when a programmer queries the corresponding code file, the query text they input is obtained, which is accomplished using the QueryContext class. Second, the incremental index is traversed to obtain the code file that meets the search criteria and has the highest matching degree, and then it is returned to the user.
[0019] The advantages of this invention are as follows: 1. The incremental indexing method based on Lucene for managing code files during code repository version updates uses incremental indexing to record and manage newly added, deleted, or modified code files during code repository version updates. This effectively addresses code repository version update-related issues, better completes the task of marking and querying modified code files during version updates, and better manages code files in different versions of the code repository during version updates.
[0020] 2. In this invention, Lucene is used to index the code files. In practice, it is impossible for the program to re-traverse all code files in every code repository version update. This would greatly affect the efficiency and cost of the update. Therefore, this invention adopts incremental indexing. Instead, it compares the code files before and after the version update and updates the incremental index accordingly to the newly added, deleted, and modified code files. This corresponds to the series of operations on the code files mentioned above. In the initial version, all code files are added to the index. In subsequent version updates, the changed code files are updated to the incremental index, ensuring that all files are recorded, saving a lot of time and operational costs.
[0021] 3. This invention utilizes incremental indexing to index each code file, enabling the searching, querying, and management of different code files within the code repository. When all code files in the code repository are recorded in the incremental index, programmers can use the index to search and query for the code files they need. This greatly assists in daily code repository management, programmers' application of the code repository, and subsequent version updates, improving programming efficiency and saving time and costs. Attached Figure Description
[0022] Figure 1 This is a flowchart of the present invention. Detailed Implementation
[0023] The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings.
[0024] like Figure 1 As shown, a commit query method applicable to retrieving related code in a code repository includes the following steps:
[0025] Step 1: Use the features provided by Lucene to build incremental indexes for the commits of code files in the code repository. For all subsequent code file commits, create corresponding indexes for the commit information such as COMMIT_INDEX_VERSION, COMMIT_HASH, and META. First, use Lucene to build the indexes by referencing the IndexWriter class in the Lucene package and setting its isEmpty value to true. This creates the incremental index. Then, write the required code file commit information into the index, such as: COMMIT_INDEX_VERSION, COMMIT_HASH, META, LAST_COMMIT, LAST_COMMIT_INDEX_VERSION, and LAST_COMMIT_HASH. This completes the construction of the required indexes.
[0026] Step 2: In the initial version of the code repository, iterate through all commits of the code files in the repository. During the iteration, record the commit information of the current code file and update the corresponding COMMIT_HASH index to know which commits have been indexed. Record the commit ID of the last commit so that when the code repository version is updated, it is necessary to index the code files that have been changed by subsequent commits. Step 3: When the code repository version is updated, obtain the commit ID and index version of the latest commit after the update. In Step 3, it is necessary to first use Lucene to build an index and set the isEmpty value to false, that is, to build an incremental index. Mark the required code files and write them into the index. After the index is built, add all the code files marked in Step 2 to the incremental index. Update the corresponding LAST_COMMIT_INDEX_VERSION and LAST_COMMIT_HASH index contents.
[0027] RevWalk reads the repository logs, and TreeWalk is used to traverse the code files in the repository. First, the revTree corresponding to the revision is obtained, which is the file node tree for this version. Then, the entry file corresponding to the revTree is obtained to perform traversal. In the initial version of the code repository, the code repository needs to be traversed. Each code file obtained by traversal is marked as read, indicating that the code file has been traversed. The commit ID of the latest commit is recorded, such as commitID and COMMIT_INDEX_VERSION, and updated to the COMMIT_HASH index to know which commits have been indexed. The commit ID of the last commit is recorded, and its commit information is updated to the LAST_COMMIT_INDEX_VERSION and LAST_COMMIT_HASH indexes to know which subsequent commits need to be indexed when the code repository version is updated.
[0028] Step 4: Compare the information submitted in Step 3 with the previously updated submission information in the index to determine if the two sets of information are the same. Obtain the newly added, deleted, or modified code files and update them in the incremental index. Update the index for the modified code files. When the code repository version is updated, first obtain the commit ID of the latest submission after the version update, extract it together with the commit ID before the version update, compare the two submissions to obtain the newly added, deleted, and modified code files before and after the version update, and update the changes in the incremental index. When the code repository version is updated, first obtain the commit ID of the latest submission after the version update, extract it together with the commit ID before the version update, compare the two submissions to obtain the newly added, deleted, and modified code files before and after the version update, and update the changes in the incremental index. For newly added code files, add a corresponding incremental index for them; for deleted code files, delete their corresponding incremental index; for modified code files, delete their original incremental index and add a corresponding incremental index for their new version file. This completes the update of the incremental index.
[0029] Step 5: Utilize incremental indexing to search, query, and manage different code files in the code repository. This involves first obtaining the query text input by the programmer when they search for the corresponding code file (using the QueryContext class), then traversing the incremental index to find the code file with the highest matching score that meets the search criteria, and returning it to the user.
[0030] Those skilled in the art should understand that the embodiments of the present invention shown in the above description are merely examples and do not limit the present invention; the objectives of the present invention have been fully and effectively achieved. The functions and structural principles of the present invention have been demonstrated and described in the embodiments, and any modifications or variations of the embodiments of the present invention may be made without departing from the stated principles.
Claims
1. A commit query method applicable to retrieving related code in a code repository, characterized in that, Includes the following steps: Step 1: Use the features provided by Lucene to build an incremental index for the commits of code files in the code repository. For all subsequent code file commits that are traversed, build corresponding indexes for the commit information such as COMMIT_INDEX_VERSION, COMMIT_HASH, and META. Step 2: In the initial version of the code repository, iterate through all the commits of the code files in the repository. During the iteration, record the commit information of the current code file and update the corresponding COMMIT_HASH index to know which commits have been indexed. Record the commit ID of the last commit so that when the code repository version is updated, it can index which subsequent commits have changed the code files. Step 3: When the code repository version is updated, obtain the commit ID of the latest commit after the update and the index version. Step 4: Compare the information submitted in Step 3 with the previously updated submission information in the index to determine if the two pieces of information are the same. Obtain the newly added, deleted, or modified code files and update them in the incremental index. Update the index for the modified code files. Step 5: Use incremental indexing to search, query, and manage different code files in the code repository.
2. The commit query method for querying related code in a code repository according to claim 1, characterized in that: In step 1, you first need to use Lucene to create an index. Refer to the IndexWriter class in the Lucene package and set its isEmpty value to true, which means creating an incremental index and writing the required code file commit information into the index.
3. The commit query method for querying related code in a code repository according to claim 1, characterized in that: In step 2, RevWalk reads the repository logs and TreeWalk is used to traverse the code files in the repository. First, the revTree corresponding to the revision is obtained, which is the file node tree for this version. Then, the entry file corresponding to the revTree is obtained to perform traversal. In the initial version of the repository, the repository needs to be traversed. Each code file obtained by traversal is marked as read, indicating that the code file has been traversed. The commitID of the latest commit is recorded so as to know which subsequent commits need to index the modified code files when the repository version is updated.
4. The commit query method for querying related code in a code repository according to claim 1, characterized in that: In step 3, Lucene is first used to build an index, and the isEmpty value is set to false, which means an incremental index is built. The required code files are marked and written into the index. After the index is built, all the code files marked in step 2 are added to the incremental index.
5. A commit query method for querying related code in a code repository according to claim 1, characterized in that: In step 4, when the code repository version is updated, the commitID of the latest commit after the version update is first obtained, and it is extracted together with the commitID before the version update. The commit information of the two commits is compared to obtain the code files that were added, deleted and modified before and after the version update, and the changes are updated in the incremental index.
6. A commit query method for querying related code in a code repository according to claim 5, characterized in that: The newly added code file has its corresponding incremental index added; the deleted code file has its corresponding incremental index deleted; the modified code file has its original incremental index deleted and a corresponding incremental index added for its new version file, thus completing the incremental index update.
7. A commit query method for querying related code in a code repository according to claim 1, characterized in that: Step 5 completes the final search, query, and management of code files. First, when a programmer queries a corresponding code file, the query text they input is obtained, which is accomplished using the QueryContext class. Second, the incremental index is traversed to obtain the code file that meets the search criteria and has the highest matching degree, and then it is returned to the user.