Here's how I show last commit & history of directories / files #7486

Closed
opened 2025-11-02 07:27:03 -06:00 by GiteaMirror · 0 comments
Owner

Originally created by @lchrennew on GitHub (Jun 19, 2021).

  • Gitea version (or commit ref): 1.14.2
  • Git version:
  • Operating system:
  • Database (use [x]):
    • PostgreSQL
    • MySQL
    • MSSQL
    • SQLite
  • Can you reproduce the bug at https://try.gitea.io:
    • Yes (provide example URL)
    • No
  • Log gist:

Description

...

Hello there,

I'm sharing my implementation for showing last commit and commits of directories and files via api.

I do this by myself because the APIs of current version of Gitea not provide these. (expecting new version will).

I use redis to build indices:

  1. Build last commit sha index for all files of each commit.
  2. Build commit history index for each file changed in each commit.
  3. Build commit summary index for each commit.

last commit sha index

key:

`{files:${owner}:${repo}}:${ref}` // ref is sha of commit or refs/tags/... or refs/heads/...

value: Hash<filename, sha>, every path visible, including the root path, are tracked.

When a commit is pushed to a branch:

  1. if the branch does not exist before(the key doesn't exist), copy the files of the before commit to this branch. using redis COPY command.
  2. update all paths affected by files changed on the branch. using redis HMSET command.
  3. copy the files of the branch to the after commit. usinig redis COPY command.

To ls a path, you could combine the response of GET /repos/{owner}/{repo}/contents/{filepath} API and the result of HMGET command.

commit history index:

key:

`{commits:${owner}:${repo}}:${sha}:${filename}`

value: List<sha>, all commits' sha are tracked in newer to older order.

When a commit is pushed to a branch:

  1. if the branch does not exist before(the key doesn't exist), push the sha of the before commit to all paths affected by files changed on this branch. using redis LPUSH command.
  2. push the sha of the after commit to all paths affected by files changed on this branch. using redis LPUSH command.
  3. copy all paths updated on this branch to the after commit. using redis COPY command.
  4. create commit summary: HMSET {commit:$owner:$repo}:$sha message '$message' timestamp '$timestamp' author '$author'

To query history of a path, you could use LRANGE to get the SHA list and then use MGET to get commit summaries.

Screenshots

Originally created by @lchrennew on GitHub (Jun 19, 2021). <!-- NOTE: If your issue is a security concern, please send an email to security@gitea.io instead of opening a public issue --> <!-- 1. Please speak English, this is the language all maintainers can speak and write. 2. Please ask questions or configuration/deploy problems on our Discord server (https://discord.gg/gitea) or forum (https://discourse.gitea.io). 3. Please take a moment to check that your issue doesn't already exist. 4. Make sure it's not mentioned in the FAQ (https://docs.gitea.io/en-us/faq) 5. Please give all relevant information below for bug reports, because incomplete details will be handled as an invalid report. --> - Gitea version (or commit ref): 1.14.2 - Git version: - Operating system: <!-- Please include information on whether you built gitea yourself, used one of our downloads or are using some other package --> <!-- Please also tell us how you are running gitea, e.g. if it is being run from docker, a command-line, systemd etc. ---> <!-- If you are using a package or systemd tell us what distribution you are using --> - Database (use `[x]`): - [ ] PostgreSQL - [x] MySQL - [ ] MSSQL - [ ] SQLite - Can you reproduce the bug at https://try.gitea.io: - [ ] Yes (provide example URL) - [ ] No - Log gist: <!-- It really is important to provide pertinent logs --> <!-- Please read https://docs.gitea.io/en-us/logging-configuration/#debugging-problems --> <!-- In addition, if your problem relates to git commands set `RUN_MODE=dev` at the top of app.ini --> ## Description <!-- If using a proxy or a CDN (e.g. CloudFlare) in front of gitea, please disable the proxy/CDN fully and connect to gitea directly to confirm the issue still persists without those services. --> ... Hello there, I'm sharing my implementation for showing last commit and commits of directories and files via api. I do this by myself because the APIs of current version of Gitea not provide these. (expecting new version will). I use redis to build indices: 1. Build last commit sha index for all files of each commit. 2. Build commit history index for each file changed in each commit. 3. Build commit summary index for each commit. ### last commit sha index key: ```javascript `{files:${owner}:${repo}}:${ref}` // ref is sha of commit or refs/tags/... or refs/heads/... ``` value: `Hash<filename, sha>`, every path visible, including the root path, are tracked. When a commit is pushed to a branch: 1. if the branch does not exist before(the key doesn't exist), copy the files of the `before` commit to this branch. using redis `COPY` command. 2. update all paths affected by files changed on the branch. using redis `HMSET` command. 3. copy the files of the branch to the `after` commit. usinig redis `COPY` command. To `ls` a path, you could combine the response of `GET /repos/{owner}/{repo}/contents/{filepath}` API and the result of `HMGET` command. ### commit history index: key: ```javascript `{commits:${owner}:${repo}}:${sha}:${filename}` ``` value: `List<sha>`, all commits' sha are tracked in newer to older order. When a commit is pushed to a branch: 1. if the branch does not exist before(the key doesn't exist), push the sha of the `before` commit to all paths affected by files changed on this branch. using redis `LPUSH` command. 2. push the sha of the `after` commit to all paths affected by files changed on this branch. using redis `LPUSH` command. 3. copy all paths updated on this branch to the `after` commit. using redis `COPY` command. 4. create commit summary: `HMSET {commit:$owner:$repo}:$sha message '$message' timestamp '$timestamp' author '$author'` To query history of a path, you could use `LRANGE` to get the SHA list and then use `MGET` to get commit summaries. ## Screenshots <!-- **If this issue involves the Web Interface, please include a screenshot** -->
GiteaMirror added the modifies/api label 2025-11-02 07:27:03 -06:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/gitea#7486