feat(repository): make it possible to either setup (clone) repo or see its details/files/head depending on its HEAD state
+ 54
- 18
@@ -1,5 +1,5 @@
 {
-  "_generatedAtUnix": 1663515661976,
+  "_generatedAtUnix": 1663515923444,
   "_hashAlgorithm": "sha1",
   "_version": 2,
   "islands": {

...
@@ -48,7 +48,7 @@
       "pathSource": "./app/views/repository/RepositoryCreateView.tsx"
     },
     "RepositoryDetailsView": {
-      "hash": "6e77564a663cddb61df8a61a03d3b45d15e6df3f",
+      "hash": "671bd0202822bb20af517af1bfee98e138cd61ac",
       "pathSource": "./app/views/repository/RepositoryDetailsView.tsx"
     },
     "RepositoryExploreView": {

app/controllers/repository/getRepositoryDetailsView.ts
@@ -60,6 +60,8 @@ const getRepositoryDetailsView: ReqHandler = async (request, reply) => {
           repoFiles: [],
         }
       );
+    } else {
+      throw err;
     }
   }
 };

app/services/repository/getRepositoryHead.ts
@@ -11,8 +11,14 @@ import { Env } from "../../env";
 import type { RepositoryHead } from "../../types";
 import type { RepositoryServiceDeps } from "./types";
 
-const GIT_CAT_FILE_REGEXP =
-  /^tree[\s]+(.*)\nparent[\s]+(.*)\nauthor[\s]+(.*)[\s]+<(.*)>[\s]+([\d]+)[\s]+([\+\d]+)\ncommitter[\s]+(.*)[\s]+<(.*)>[\s]+([\d]+)[\s]+([\+\d]+)\n\n(.*)\n$/im;
+const GIT_TREE_ID_REGEXP = /tree[\s]+(.*)\n/im;
+const GIT_PARENT_ID_REGEXP = /parent[\s]+(.*)\n/im;
+const GIT_AUTHOR_REGEXP =
+  /author[\s]+(.*)[\s]+<(.*)>[\s]+([\d]+)[\s]+([\+\d]+)\n/im;
+const GIT_COMMITTER_REGEXP =
+  /committer[\s]+(.*)[\s]+<(.*)>[\s]+([\d]+)[\s]+([\+\d]+)\n/im;
+const GIT_COMMITT_MESSAGE_REGEXP =
+  /committer[\s]+.*[\s]+<.*>[\s]+[\d]+[\s]+[\+\d]+\n\n(.*)\n/im;
 
 const makeGetRepositoryHead: ServiceMethodFactory<
   RepositoryServiceDeps,

...
@@ -52,25 +58,52 @@ const makeGetRepositoryHead: ServiceMethodFactory<
       });
     });
 
-    const matches = GIT_CAT_FILE_REGEXP.exec(gitCatFileResult);
-    if (matches == null || Array.isArray(matches) === false) {
-      throw new Error("Invalid HEAD.");
+    console.log("gitCatFileResult:", gitCatFileResult);
+
+    const treeIdMatches = GIT_TREE_ID_REGEXP.exec(gitCatFileResult);
+    const parentIdMatches = GIT_PARENT_ID_REGEXP.exec(gitCatFileResult);
+    const authorMatches = GIT_AUTHOR_REGEXP.exec(gitCatFileResult);
+    const committerMatches = GIT_COMMITTER_REGEXP.exec(gitCatFileResult);
+    const commitMessageMatches =
+      GIT_COMMITT_MESSAGE_REGEXP.exec(gitCatFileResult);
+
+    let treeId: null | string = null;
+    if (treeIdMatches != null || Array.isArray(treeIdMatches)) {
+      treeId = treeIdMatches[1];
+    }
+
+    let parentId: null | string = null;
+    if (parentIdMatches != null && Array.isArray(parentIdMatches)) {
+      parentId = parentIdMatches[1];
+    }
+
+    if (authorMatches == null || Array.isArray(authorMatches) === false) {
+      throw new Error("Invalid HEAD, missing author informations.");
+    }
+
+    const [_, authorName, authorEmail, authorTimestamp, authorTimezone] =
+      authorMatches;
+
+    if (committerMatches == null || Array.isArray(committerMatches) === false) {
+      throw new Error("Invalid HEAD, missing committer informations.");
     }
 
     const [
-      _,
-      treeId,
-      parentId,
-      authorName,
-      authorEmail,
-      authorTimestamp,
-      authorTimezone,
+      __,
       committerName,
       committerEmail,
       committerTimestamp,
       committerTimezone,
-      commitMessage,
-    ] = matches;
+    ] = committerMatches;
+
+    if (
+      commitMessageMatches == null ||
+      Array.isArray(commitMessageMatches) === false
+    ) {
+      throw new Error("Invalid HEAD, missing commit message.");
+    }
+
+    const commitMessage = commitMessageMatches[1];
 
     return {
       treeId,

@@ -82,7 +82,7 @@ export type CommonProps = { commonProps: CommonViewProps };
 
 export interface RepositoryHead {
   treeId: string;
-  parentId: string;
+  parentId: null | string;
   author: {
     name: string;
     email: string;

app/views/repository/RepositoryDetailsView.tsx
@@ -60,7 +60,8 @@ $ git config user.email "${currentUser.email}"`
 $ echo "# ${repo.displayName || repo.slug}" > README.md
 $ echo "The MIT License" > LICENSE
 
-# Commit and send to GitFOSS remote repository
+# Track files, commit and send to GitFOSS remote repository
+$ git add .
 $ git commit -am 'feat: initial commit'
 $ git push
 `}</pre>