index : gitrs
A lightweight web interface for browsing Git repositories, implemented in Rust
about summary refs log tree commit diff

Diffstat

-rw-r--r-- src/time.rs 4
-rw-r--r-- static/style.css 42
-rw-r--r-- templates/base.html 79
-rw-r--r-- templates/macros.html 11
-rw-r--r-- templates/repositorys.html 21
-rw-r--r-- src/git/commit.rs 11
-rw-r--r-- src/routes/mod.rs 10
-rw-r--r-- templates/repository/tree.html 2
 8 files changed, 162 insertions(+), 18 deletions(-)

diff --git a/src/time.rs b/src/time.rs
index 0eb01bc..53a33d4 100644
--- a/src/time.rs
+++ b/src/time.rs
@@ -31,6 +31,10 @@
        Self { gix }
    }

    pub fn seconds(&self) -> i64 {
        self.gix.seconds
    }

    pub fn format(&self) -> String {
        let dt: DateTime<Utc> = Utc.timestamp_opt(self.gix.seconds, 0).unwrap();
        let offset = FixedOffset::east_opt(self.gix.offset).unwrap();
diff --git a/static/style.css b/static/style.css
index 2e7440d..ca1b995 100644
--- a/static/style.css
+++ b/static/style.css
@@ -147,6 +147,48 @@
  border-bottom: 1px solid var(--surface1);
}

main table thead th {
  cursor: pointer;
  user-select: none;
  position: relative;
  transition: background-color 0.1s ease, color 0.1s ease;
}

main table thead th:hover {
  background: var(--one_bg);
  color: var(--rosewater);
}

main table thead th::after {
  content: ' ⇅';
  font-size: 0.75em;
  color: var(--overlay0);
  opacity: 0;
  margin-left: 0.35rem;
}

main table thead th:hover::after {
  opacity: 0.75;
}

main table thead th.sort-asc,
main table thead th.sort-desc {
  background: var(--one_bg);
  border-bottom: 2px solid var(--peach);
}

main table thead th.sort-asc::after {
  content: ' ▲';
  color: var(--green);
  opacity: 1;
}

main table thead th.sort-desc::after {
  content: ' ▼';
  color: var(--red);
  opacity: 1;
}

td {
  text-align: left;
  white-space: nowrap;
diff --git a/templates/base.html b/templates/base.html
index 3bff0a4..5315b50 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -29,5 +29,84 @@
          </main>
          {% endblock %}
        </div>
      <script>
        document.querySelectorAll("main table").forEach((table) => {
          const headers = table.querySelectorAll("thead th");
          const tbody = table.querySelector("tbody");
          if (!headers.length || !tbody) return;

          const cellValue = (row, idx) => {
            const cell = row.children[idx];
            if (!cell) return { num: NaN, text: "" };
            const rawSort = cell.getAttribute("data-sort");
            if (rawSort !== null) {
              return { num: Number(rawSort), text: rawSort };
            }
            const text = cell.textContent.trim();
            return { num: NaN, text };
          };

          headers.forEach((th, colIdx) => {
            th.setAttribute("tabindex", "0");
            th.setAttribute("role", "columnheader");
            if (!th.hasAttribute("aria-sort")) {
              th.setAttribute("aria-sort", "none");
            }

            const applySort = () => {
              // 1st click -> ascending (top-down), 2nd click -> descending (bottom-up)
              const nextDir = th.classList.contains("sort-asc") ? "desc" : "asc";
              const factor = nextDir === "asc" ? 1 : -1;

              headers.forEach((h) => {
                h.classList.remove("sort-asc", "sort-desc");
                h.setAttribute("aria-sort", "none");
              });
              th.classList.add(nextDir === "asc" ? "sort-asc" : "sort-desc");
              th.setAttribute(
                "aria-sort",
                nextDir === "asc" ? "ascending" : "descending"
              );

              const allRows = Array.from(tbody.querySelectorAll(":scope > tr"));
              const topPinned = allRows.filter((r) =>
                r.classList.contains("tree-parent")
              );
              const bottomPinned = allRows.filter((r) =>
                r.classList.contains("no-background")
              );
              const sortableRows = allRows.filter(
                (r) =>
                  !r.classList.contains("tree-parent") &&
                  !r.classList.contains("no-background")
              );

              sortableRows.sort((rowA, rowB) => {
                const a = cellValue(rowA, colIdx);
                const b = cellValue(rowB, colIdx);
                if (!Number.isNaN(a.num) && !Number.isNaN(b.num)) {
                  return (a.num - b.num) * factor;
                }
                return (
                  a.text.localeCompare(b.text, undefined, {
                    numeric: true,
                    sensitivity: "base",
                  }) * factor
                );
              });

              tbody.append(...topPinned, ...sortableRows, ...bottomPinned);
            };

            th.addEventListener("click", applySort);
            th.addEventListener("keydown", (e) => {
              if (e.key === "Enter" || e.key === " ") {
                e.preventDefault();
                applySort();
              }
            });
          });
        });
      </script>
  </body>
</html>
diff --git a/templates/macros.html b/templates/macros.html
index b172639..3d4c916 100644
--- a/templates/macros.html
+++ b/templates/macros.html
@@ -111,7 +111,7 @@
            {{ branch.author.name }}
          </td>
          {% let time = branch.last_commit.committer.time.format_relative() %}
          <td class="{{ time.1 }}" title="{{ branch.last_commit.committer.time.format() }}">{{ time.0 }}</td>
          <td class="{{ time.1 }}" data-sort="-{{ branch.last_commit.committer.time.seconds() }}" title="{{ branch.last_commit.committer.time.format() }}">{{ time.0 }}</td>
        </tr>
      {% endfor %}
    <tr class="no-background">
@@ -142,7 +142,7 @@
{% macro tags(tags, tags_has_more, has_prev, page, mode) %}
    <thead>
      <tr>
        <th>Tag</th>
        <th class="sort-desc" aria-sort="descending">Tag</th>
        <th>Download</th>
        <th>Author</th>
        <th>Age</th>
@@ -165,8 +165,7 @@
            {% endif %}
            </td>
            {% let time = tag.time.format_relative() %}
            <td class="
              {{ time.1 }}" title="{{ tag.time.format() }}">{{ time.0 }}</td>
            <td class="{{ time.1 }}" data-sort="-{{ tag.time.seconds() }}" title="{{ tag.time.format() }}">{{ time.0 }}</td>
        </tr>
      {% endfor %}
      <tr class="no-background">
@@ -197,7 +196,7 @@
{% macro commits(commits, commits_has_more, has_prev, page, mode) %}
    <thead>
      <tr>
        <th>Age</th>
        <th class="sort-asc" aria-sort="ascending">Age</th>
        <th>Commit message</th>
        <th>Author</th>
      </tr>
@@ -206,7 +205,7 @@
      {% for commit in commits %}
        <tr>
          {% let time = commit.committer.time.format_relative() %}
          <td class="{{ time.1 }}" title="{{ commit.committer.time.format() }}">{{ time.0 }}</td>
          <td class="{{ time.1 }}" data-sort="-{{ commit.committer.time.seconds() }}" title="{{ commit.committer.time.format() }}">{{ time.0 }}</td>
          <td>
            <a href="/{{ repo.name }}/commit/?id={{ commit.id }}">{{ commit.summary()? | truncate(80)}}</a>
            {% if commit.is_head(repo)? %}<a href="/{{ repo.name }}/commit/?id={{ commit.id }}" class="badge_head">HEAD</a>{% endif %}
diff --git a/templates/repositorys.html b/templates/repositorys.html
index 0646bf8..93c40c7 100644
--- a/templates/repositorys.html
+++ b/templates/repositorys.html
@@ -28,7 +28,7 @@
          <th>Name</th>
          <th>Description</th>
          <th>Owner</th>
          <th>Idle</th>
          <th class="sort-asc" aria-sort="ascending">Idle</th>
          <th>Links</th>
        </tr>
    </thead>
@@ -38,16 +38,17 @@
            <td><a href="/{{ repo.name }}/">{{ repo.name }}</a></td>
            <td><a href="/{{ repo.name }}/">{{ repo.description }}</a></td>
            <td>{% if let Some(owner) = repo.owner %}{{ owner }}{% endif %}</td>
            <td>
              {% let gix_repo = repo.gix() %}
              {% if let Some(last_commit) = repo.last_commit(gix_repo)? %}
                {% let time = last_commit.time.format_relative() %}
                <span class="{{ time.1 }}" title="{{ last_commit.time.format() }}">{{ time.0 }}</span>
              {% else %}
            {% let gix_repo = repo.gix() %}
            {% if let Some(last_commit) = repo.last_commit(gix_repo)? %}
              {% let time = last_commit.committer.time.format_relative() %}
              <td data-sort="-{{ last_commit.committer.time.seconds() }}">
                <span class="{{ time.1 }}" title="{{ last_commit.committer.time.format() }}">{{ time.0 }}</span>
              </td>
            {% else %}
              <td data-sort="999999999999">
                <span class="stale">Unknown</span>
              {% endif %}

            </td>
              </td>
            {% endif %}
            <td><a href="/{{ repo.name }}/">summary</a><a href="/{{ repo.name }}/log/">log</a><a href="/{{ repo.name }}/tree/">tree</a></td>
        </tr>
        {% endfor %}
diff --git a/src/git/commit.rs b/src/git/commit.rs
index e446207..a6cd67e 100644
--- a/src/git/commit.rs
+++ b/src/git/commit.rs
@@ -89,6 +89,17 @@
        Ok(None)
    }

    /// Returns the committer timestamp (in seconds) of the repository's latest commit,
    /// or `i64::MIN` if the repository has no commits yet.
    pub fn last_commit_timestamp(&self) -> i64 {
        let gix_repo = self.gix();
        self.last_commit(&gix_repo)
            .ok()
            .flatten()
            .map(|c| c.committer.time.seconds())
            .unwrap_or(i64::MIN)
    }

    pub fn branch_tips_by_commit(&self) -> Result<HashMap<String, Vec<String>>, Error> {
        let gix_repo = self.gix();
        let references = gix_repo.references()?;
diff --git a/src/routes/mod.rs b/src/routes/mod.rs
index 0c6a5f9..9ec1eb7 100644
--- a/src/routes/mod.rs
+++ b/src/routes/mod.rs
@@ -86,7 +86,15 @@

        if let (qt, Some(value)) = (query.query_type.as_deref(), query.value.as_deref()) {
            let value_lower = value.to_lowercase();
            repos.sort_by_key(|a| relevance(a, qt, &value_lower));
            repos.sort_by_key(|a| {
                (
                    relevance(a, qt, &value_lower),
                    std::cmp::Reverse(a.last_commit_timestamp()),
                )
            });
        } else {
            // Auto-sort repositories by Idle/Age (most recently updated first)
            repos.sort_by_key(|a| std::cmp::Reverse(a.last_commit_timestamp()));
        }
        ReposPage { repos }.render()
    };
diff --git a/templates/repository/tree.html b/templates/repository/tree.html
index 66f3624..6c34e24 100644
--- a/templates/repository/tree.html
+++ b/templates/repository/tree.html
@@ -59,7 +59,7 @@
                    <a href="/{{ repo.name }}/tree/{{ entry.path }}{{ parameters }}">{{ entry.name }}</a>
                {% endmatch %}
              </td>
              <td class="size">{{ entry.size_display() }}</td>
              <td class="size" data-sort="{{ entry.size.unwrap_or(0) }}">{{ entry.size_display() }}</td>
            </tr>
          {% endfor %}
        </tbody>