Skip to content

Commit

Permalink
Flatten nested paths with only one child
Browse files Browse the repository at this point in the history
  • Loading branch information
w4 committed Oct 16, 2024
1 parent 6a451a4 commit 98383dc
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 247 deletions.
1 change: 0 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
./src
./statics
./templates
./grammar
./themes
./build.rs
];
Expand Down
197 changes: 0 additions & 197 deletions grammar/fortran/highlights.scm

This file was deleted.

20 changes: 0 additions & 20 deletions grammar/html/highlights.scm

This file was deleted.

10 changes: 0 additions & 10 deletions grammar/html/injections.scm

This file was deleted.

62 changes: 44 additions & 18 deletions src/git.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
use std::{
borrow::Cow,
collections::{BTreeMap, VecDeque},
ffi::OsStr,
fmt::{self, Arguments, Write},
io::ErrorKind,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
time::Duration,
};

use anyhow::{anyhow, Context, Result};
use axum::response::IntoResponse;
use bytes::{buf::Writer, BufMut, Bytes, BytesMut};
Expand All @@ -26,7 +14,19 @@ use gix::{
url::Scheme,
ObjectId, ThreadSafeRepository, Url,
};
use itertools::Itertools;
use moka::future::Cache;
use std::borrow::Cow;
use std::{
collections::{BTreeMap, VecDeque},
ffi::OsStr,
fmt::{self, Arguments, Write},
io::ErrorKind,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
time::Duration,
};
use tar::Builder;
use time::{OffsetDateTime, UtcOffset};
use tracing::{error, instrument, warn};
Expand Down Expand Up @@ -198,7 +198,7 @@ impl OpenRepository {
| EntryKind::Blob
| EntryKind::BlobExecutable
| EntryKind::Link => {
let object = item
let mut object = item
.object()
.context("Expected item in tree to be object but it wasn't")?;

Expand All @@ -209,11 +209,36 @@ impl OpenRepository {
path,
name: item.filename().to_string(),
}),
Kind::Tree => TreeItem::Tree(Tree {
mode: item.mode().0,
path,
name: item.filename().to_string(),
}),
Kind::Tree => {
let mut children = PathBuf::new();

// if the tree only has one child, flatten it down
while let Ok(Some(Ok(item))) = object
.try_into_tree()
.iter()
.map(gix::Tree::iter)

Check warning on line 219 in src/git.rs

View workflow job for this annotation

GitHub Actions / Clippy

called `map(..).flatten()` on `Iterator`
.flatten()
.at_most_one()
{
let nested_object = item.object().context(
"Expected item in tree to be object but it wasn't",
)?;

if nested_object.kind != Kind::Tree {
break;
}

object = nested_object;
children.push(item.filename().to_path_lossy());
}

TreeItem::Tree(Tree {
mode: item.mode().0,
path,
children,
name: item.filename().to_string(),
})
}
_ => continue,
});
}
Expand Down Expand Up @@ -602,6 +627,7 @@ pub struct Submodule {
pub struct Tree {
pub mode: u16,
pub name: String,
pub children: PathBuf,
pub path: PathBuf,
}

Expand Down
1 change: 1 addition & 0 deletions src/methods/repo/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{

use askama::Template;
use axum::{extract::Query, response::IntoResponse, Extension};
use itertools::Itertools;
use serde::Deserialize;

use crate::{
Expand Down
6 changes: 5 additions & 1 deletion templates/repo/tree.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
{% match item -%}
{%- when crate::git::TreeItem::Tree with (tree) -%}
<td><pre>{{ tree.mode|file_perms }}</pre></td>
<td><pre><a class="nested-tree" href="/{{ repo.display() }}/tree/{{ tree.path.display() }}{{ query }}">{{ tree.name }}</a></pre></td>
<td><pre><a class="nested-tree" href="/{{ repo.display() }}/tree/{{ tree.path.display() }}{{ query }}">{{ tree.name }}</a>
{%- for child in tree.children.ancestors().collect_vec().into_iter().rev() -%}
{%- if let Some(file_name) = child.file_name() %} / <a class="nested-tree" href="/{{ repo.display() }}/tree/{{ tree.path.display() }}/{{ child.display() }}{{ query }}">{{ file_name.to_string_lossy() }}</a>{%- endif -%}
{%- endfor -%}
</pre></td>
<td></td>
<td></td>

Expand Down

0 comments on commit 98383dc

Please sign in to comment.