💾 Archived View for lists.flounder.online › patches › threads › 20210320000714.11299-4-johann@qwertq… captured on 2022-04-28 at 19:24:45. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

[PATCH mygit 4/4] handle nonexistent repos

[PATCH mygit 4/4] handle nonexistent repos

From: johann@qwertqwefsday.eu

Date: Sat, 20 Mar 2021 01:07:14 +0100

Message-Id: 20210320000714.11299-4-johann@qwertqwefsday.eu

To: <~aw/patches@lists.sr.ht>

In-Reply-To: 20210320000714.11299-1-johann@qwertqwefsday.eu

Cc: "Johann150" <johann@qwertqwefsday.eu>

Reply

Export

--------------------------------------

From: Johann150 <johann@qwertqwefsday.eu>

---

src/main.rs | 49 ++++++++++++++++++++++++++++++++++++++++++-------

1 file changed, 42 insertions(+), 7 deletions(-)

diff --git a/src/main.rs b/src/main.rs

index cc4b9af..beeb305 100644

--- a/src/main.rs

+++ b/src/main.rs

@@ -168,7 +168,13 @@ async fn repo_home(req: Request<()>) -> tide::Result {

Markdown,

}

- let repo = repo_from_request(&req.param("repo_name")?)?;

+ let repo = if let Ok(repo) = repo_from_request(&req.param("repo_name")?) {

+ repo

+ } else {

+ // this repository does not exist

+ return not_found(req.url().path());

+ };

+

if repo.is_empty().unwrap() {

// we would not be able to find HEAD

return Ok(RepoEmptyTemplate { repo }.into());

@@ -228,7 +234,12 @@ struct RepoLogTemplate<'a> {

}

async fn repo_log(req: Request<()>) -> tide::Result {

- let repo = repo_from_request(&req.param("repo_name")?)?;

+ let repo = if let Ok(repo) = repo_from_request(&req.param("repo_name")?) {

+ repo

+ } else {

+ // this repository does not exist

+ return not_found(req.url().path());

+ };

if repo.is_empty().unwrap() {

// redirect to start page of repo

let mut url = req.url().clone();

@@ -276,7 +287,13 @@ struct RepoRefTemplate<'a> {

tags: Vec<Reference<'a>>,

}

async fn repo_refs(req: Request<()>) -> tide::Result {

- let repo = repo_from_request(&req.param("repo_name")?)?;

+ let repo = if let Ok(repo) = repo_from_request(&req.param("repo_name")?) {

+ repo

+ } else {

+ // this repository does not exist

+ return not_found(req.url().path());

+ };

+

if repo.is_empty().unwrap() {

// redirect to start page of repo

let mut url = req.url().clone();

@@ -311,7 +328,13 @@ struct RepoTreeTemplate<'a> {

}

async fn repo_tree(req: Request<()>) -> tide::Result {

// TODO handle subtrees

- let repo = repo_from_request(&req.param("repo_name")?)?;

+ let repo = if let Ok(repo) = repo_from_request(&req.param("repo_name")?) {

+ repo

+ } else {

+ // this repository does not exist

+ return not_found(req.url().path());

+ };

+

if repo.is_empty().unwrap() {

// redirect to start page of repo

let mut url = req.url().clone();

@@ -343,7 +366,13 @@ struct RepoCommitTemplate<'a> {

}

async fn repo_commit(req: Request<()>) -> tide::Result {

- let repo = repo_from_request(req.param("repo_name")?)?;

+ let repo = if let Ok(repo) = repo_from_request(&req.param("repo_name")?) {

+ repo

+ } else {

+ // this repository does not exist

+ return not_found(req.url().path());

+ };

+

let commit = repo

.revparse_single(req.param("commit")?)?

.peel_to_commit()?;

@@ -376,8 +405,14 @@ struct RepoFileTemplate<'a> {

}

async fn repo_file(req: Request<()>) -> tide::Result {

- // TODO renmae for clarity

- let repo = repo_from_request(req.param("repo_name")?)?;

+ // TODO rename for clarity

+ let repo = if let Ok(repo) = repo_from_request(&req.param("repo_name")?) {

+ repo

+ } else {

+ // this repository does not exist

+ return not_found(req.url().path());

+ };

+

// If directory -- show tree TODO

let head = repo.head()?;

let spec = req.param("ref").ok().or_else(|| head.shorthand()).unwrap();

--

2.20.1

Re: [PATCH mygit 4/4] handle nonexistent repos

From: alex@alexwennerberg.com

Date: Sat, 20 Mar 2021 10:46:11 -0700

Message-Id: CA2D6Z1XYRI9.154UHFQ2ZHGKQ@debian-alex

To: "Johann Galle" <johann@qwertqwefsday.eu>, <~aw/patches@lists.sr.ht>

In-Reply-To: 20210320000714.11299-4-johann@qwertqwefsday.eu

Reply

Export

--------------------------------------

I wonder if there is a way to reduce code duplication here -- maybe

through middleware or something?

On Fri Mar 19, 2021 at 5:07 PM PDT, Johann Galle wrote:

From: Johann150 <johann@qwertqwefsday.eu>
---
src/main.rs | 49 ++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 42 insertions(+), 7 deletions(-)
diff --git a/src/main.rs b/src/main.rs
index cc4b9af..beeb305 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -168,7 +168,13 @@ async fn repo_home(req: Request<()>) ->
tide::Result {
Markdown,
}
- let repo = repo_from_request(&req.param("repo_name")?)?;
+ let repo = if let Ok(repo) =
repo_from_request(&req.param("repo_name")?) {
+ repo
+ } else {
+ // this repository does not exist
+ return not_found(req.url().path());
+ };
+
if repo.is_empty().unwrap() {
// we would not be able to find HEAD
return Ok(RepoEmptyTemplate { repo }.into());
@@ -228,7 +234,12 @@ struct RepoLogTemplate<'a> {
}
async fn repo_log(req: Request<()>) -> tide::Result {
- let repo = repo_from_request(&req.param("repo_name")?)?;
+ let repo = if let Ok(repo) =
repo_from_request(&req.param("repo_name")?) {
+ repo
+ } else {
+ // this repository does not exist
+ return not_found(req.url().path());
+ };
if repo.is_empty().unwrap() {
// redirect to start page of repo
let mut url = req.url().clone();
@@ -276,7 +287,13 @@ struct RepoRefTemplate<'a> {
tags: Vec<Reference<'a>>,
}
async fn repo_refs(req: Request<()>) -> tide::Result {
- let repo = repo_from_request(&req.param("repo_name")?)?;
+ let repo = if let Ok(repo) =
repo_from_request(&req.param("repo_name")?) {
+ repo
+ } else {
+ // this repository does not exist
+ return not_found(req.url().path());
+ };
+
if repo.is_empty().unwrap() {
// redirect to start page of repo
let mut url = req.url().clone();
@@ -311,7 +328,13 @@ struct RepoTreeTemplate<'a> {
}
async fn repo_tree(req: Request<()>) -> tide::Result {
// TODO handle subtrees
- let repo = repo_from_request(&req.param("repo_name")?)?;
+ let repo = if let Ok(repo) =
repo_from_request(&req.param("repo_name")?) {
+ repo
+ } else {
+ // this repository does not exist
+ return not_found(req.url().path());
+ };
+
if repo.is_empty().unwrap() {
// redirect to start page of repo
let mut url = req.url().clone();
@@ -343,7 +366,13 @@ struct RepoCommitTemplate<'a> {
}
async fn repo_commit(req: Request<()>) -> tide::Result {
- let repo = repo_from_request(req.param("repo_name")?)?;
+ let repo = if let Ok(repo) =
repo_from_request(&req.param("repo_name")?) {
+ repo
+ } else {
+ // this repository does not exist
+ return not_found(req.url().path());
+ };
+
let commit = repo
.revparse_single(req.param("commit")?)?
.peel_to_commit()?;
@@ -376,8 +405,14 @@ struct RepoFileTemplate<'a> {
}
async fn repo_file(req: Request<()>) -> tide::Result {
- // TODO renmae for clarity
- let repo = repo_from_request(req.param("repo_name")?)?;
+ // TODO rename for clarity
+ let repo = if let Ok(repo) =
repo_from_request(&req.param("repo_name")?) {
+ repo
+ } else {
+ // this repository does not exist
+ return not_found(req.url().path());
+ };
+
// If directory -- show tree TODO
let head = repo.head()?;
let spec = req.param("ref").ok().or_else(|| head.shorthand()).unwrap();
--
2.20.1

Re: [PATCH mygit 4/4] handle nonexistent repos

From: johann@qwertqwefsday.eu

Date: Sat, 20 Mar 2021 21:47:38 +0100

Message-Id: fa0c9e30-b7a9-9f18-148b-357148b98ca3@qwertqwefsday.eu

To: "alex wennerberg" <alex@alexwennerberg.com>, <~aw/patches@lists.sr.ht>

In-Reply-To: CA2D6Z1XYRI9.154UHFQ2ZHGKQ@debian-alex

Reply

Export

--------------------------------------

On 20.03.2021 18:46, alex wennerberg wrote:

I wonder if there is a way to reduce code duplication here -- maybe
through middleware or something?

It should probably be possible to make the not_found call inside repo_from_request and return it as an Err variant maybe. I'll try that.

Re: [PATCH mygit 4/4] handle nonexistent repos

From: johann@qwertqwefsday.eu

Date: Sat, 20 Mar 2021 22:19:02 +0100

Message-Id: cc25b7a9-f36c-3be8-e16a-3392cd9b72e7@qwertqwefsday.eu

To: "alex wennerberg" <alex@alexwennerberg.com>, <~aw/patches@lists.sr.ht>

In-Reply-To: fa0c9e30-b7a9-9f18-148b-357148b98ca3@qwertqwefsday.eu

Reply

Export

--------------------------------------

This is possible, but because we want flow control in this instance

(early function return), it would't really reduce duplication much.

Even when implementing it as an Err variant, we could not use the

try operator, because we would have to "convert" the Err variant to

a Ok variant. In combination with an early return, this would require

some restructuring, i.e. an if let.