This Node.js application clones git repositories, compiles the content of non-binary files, and provides commit history and diffs.
The application uses Express.js to create an API that processes git repositories, retrieves file contents, and compiles their histories. It employs several utility modules to handle git operations, file processing, and caching.
-
Express Server (server.js):
- Initializes the Express server.
- Sets up middleware for JSON processing.
- Starts the server and creates a tunnel for external access.
- Ensures the cache directory exists.
-
Routes (routes.js):
- Defines the primary route to handle repository processing.
- Validates incoming requests.
- Clones or updates the specified repository.
- Uses caching to speed up repeated requests.
- Retrieves file contents and histories based on request parameters.
-
Utilities:
- cacheUtils.js: Functions to generate cache keys, read from, and write to cache.
- contentUtils.js: Functions to compile file content and histories.
- fileUtils.js: Functions to retrieve files from directories, excluding specified directories like
.git
,.idea
, and.vscode
. - gitUtils.js: Functions to handle git operations like cloning and updating repositories.
- serverUtils.js: Function to check the availability of a port.
The fileUtils.js
file defines an array of directories to be excluded during file retrieval, which includes .git
, .idea
, and .vscode
. If you need to exclude more directories, such as .next
, you can add them to the EXCLUDED_DIRS
array.
Here is an updated version of fileUtils.js
to include any other directories you need to exclude:
To use the application, start the server using Node.js. The primary endpoint will handle POST requests to clone or update repositories, process the files, and return the desired content.
A POST request to the server might look like this:
{
"repo": "username/repository",
"provider": "github",
"page": 1,
"filePath": "src/index.js",
"returnType": "all",
"filesPerPage": 10
}
This request would trigger the server to clone or update the specified GitHub repository, process the file src/index.js
, and return its content along with the commit history and diffs.
-
Clone the repository:
git clone https://github.com/mauriciobellon/get-git-files.git cd get-git-files
-
Install dependencies:
npm install
-
Start the server:
node server.js
Feel free to ask if you have more specific questions or need further customization.