fix(pull_request): compare in right order when in same repository
+ 63
- 70
@@ -1,5 +1,5 @@
 {
-  "_generatedAtUnix": 1665610187489,
+  "_generatedAtUnix": 1665610935850,
   "_hashAlgorithm": "sha1",
   "_version": 2,
   "islands": {

...
@@ -64,7 +64,7 @@
       "pathSourceMap": "./public/.islands/RepositoryInitialSetup.bundle.js.map"
     },
     "RepositoryPullRequestCreateForm": {
-      "hash": "e35581cdc53a2ea39b27c25637a181db891a386c",
+      "hash": "6ac90711334641e84db75f910431e086fc0d1e00",
       "pathSource": "./app/islands/RepositoryPullRequestCreateForm.tsx",
       "pathBundle": "./public/.islands/RepositoryPullRequestCreateForm.bundle.js",
       "pathSourceMap": "./public/.islands/RepositoryPullRequestCreateForm.bundle.js.map"

app/controllers/repositoryPullRequests/getRepositoryPullRequestCreateView.ts
@@ -18,6 +18,7 @@ import {
 import RepositoryPullRequestCreateView, {
   RepositoryPullRequestCreateViewProps,
 } from "../../views/repositoryPullRequests/RepositoryPullRequestCreateView";
+import { Organization } from "@prisma/client";
 
 const getRepositoryPullRequestCreateView: ReqHandler = async (
   request,

...
@@ -79,30 +80,56 @@ const getRepositoryPullRequestCreateView: ReqHandler = async (
     true // personalOnly
   );
 
+  const sources: {
+    [orgSlug: string]: {
+      org: Organization;
+      repos: {
+        [repoKey: string]: {
+          repo: RepositoryWithParentAndForkedFromRepos;
+          branches: string[];
+        };
+      };
+    };
+  } = {
+    // target repo's source
+    [parentOrg.slug]: {
+      org: parentOrg,
+      repos: {
+        [`${parentOrg.slug}/${repo.slug}`]: {
+          repo: { ...repo, organization: parentOrg },
+          branches: branches.sort((a, b) => {
+            if (
+              a === Const.PRIMARY_BRANCH_REF &&
+              b !== Const.PRIMARY_BRANCH_REF
+            ) {
+              return -1;
+            }
+            if (
+              a !== Const.PRIMARY_BRANCH_REF &&
+              b === Const.PRIMARY_BRANCH_REF
+            ) {
+              return 1;
+            }
+            return 0;
+          }),
+        },
+      },
+    },
+    // In case user has forks on its own, add its personal org (where forks **should** be)
+    ...(currentUserForks.length >= 1 && {
+      [personalOrg.slug]: {
+        org: personalOrg,
+        repos: currentUserForksRepos,
+      },
+    }),
+  };
+
   let variant: RepositoryPullRequestCreateFormVariant;
   if (from_branch == null) {
     variant = {
       state: PullRequestFormState.CONFIGURE,
       data: {
-        sources: {
-          // target repo's source
-          [parentOrg.slug]: {
-            org: parentOrg,
-            repos: {
-              [`${parentOrg.slug}/${repo.slug}`]: {
-                repo: { ...repo, organization: parentOrg },
-                branches,
-              },
-            },
-          },
-          // In case user has forks on its own, add its personal org (where forks **should** be)
-          ...(currentUserForks.length >= 1 && {
-            [personalOrg.slug]: {
-              org: personalOrg,
-              repos: currentUserForksRepos,
-            },
-          }),
-        },
+        sources,
         target: {
           branch: from_branch || Const.PRIMARY_BRANCH_REF,
           parentOrg,

...
@@ -111,13 +138,8 @@ const getRepositoryPullRequestCreateView: ReqHandler = async (
       },
     };
   } else {
-    let source: RepositoryPullRequestTarget = {
-      branch: from_branch,
-      parentOrg,
-      repo,
-    };
     let target: RepositoryPullRequestTarget = {
-      branch: Const.PRIMARY_BRANCH_REF,
+      branch: from_branch || Const.PRIMARY_BRANCH_REF,
       parentOrg,
       repo,
     };

...
@@ -139,46 +161,20 @@ const getRepositoryPullRequestCreateView: ReqHandler = async (
         return reply.status(404).callNotFound();
       }
 
-      const fileDiffs = await repoService.getRepositoryRefDiff(
-        repo,
-        Const.PRIMARY_BRANCH_REF,
-        targetFromBranch
-      );
-
       target = {
-        branch: from_branch,
+        branch: targetFromBranch,
         parentOrg: targetParentOrg,
         repo: targetRepo,
       };
-
-      variant = {
-        state: PullRequestFormState.COMPARE,
-        data: {
-          source,
-          target,
-          fileDiffs,
-        },
-      };
-    } else {
-      const fileDiffs = await repoService.getRepositoryRefDiff(
-        repo,
-        Const.PRIMARY_BRANCH_REF,
-        from_branch
-      );
-
-      variant = {
-        state: PullRequestFormState.COMPARE,
-        data: {
-          source: {
-            branch: Const.PRIMARY_BRANCH_REF,
-            parentOrg,
-            repo,
-          },
-          target,
-          fileDiffs,
-        },
-      };
     }
+
+    variant = {
+      state: PullRequestFormState.CONFIGURE,
+      data: {
+        sources,
+        target,
+      },
+    };
   }
 
   const reqHandler = reply.makeRequestHandler(request, reply);

app/islands/RepositoryPullRequestCreateForm.tsx
@@ -227,13 +227,10 @@ const RepositoryPullRequestCreateForm: ReactIsland<RepositoryPullRequestCreateFo
                     <PullRequestSourceSelect
                       namePrefix={"source"}
                       defaultSource={{
-                        org: Object.values(data.sources)[0].org,
-                        repo: Object.values(
-                          Object.values(data.sources)[0].repos
-                        )[0].repo,
-                        branch: Object.values(
-                          Object.values(data.sources)[0].repos
-                        )[0].branches[0],
+                        org: data.target.parentOrg,
+                        repo: data.target
+                          .repo as RepositoryWithParentAndForkedFromRepos,
+                        branch: data.target.branch,
                       }}
                       sources={data.sources}
                       onSelectionChange={onSourceSelectionChange}

app/services/repository/getRepositoryRefDiff.ts
@@ -41,7 +41,7 @@ const makeGetRepositoryRefDiff: ServiceMethodFactory<
 
       const gitDiffRefsProcess = spawn(
         "git",
-        ["diff", `${refA}${refB != null ? `..${refB}` : ""}`],
+        ["diff", `${refB}${refA != null ? `..${refA}` : ""}`],
         {
           cwd: repoPath,
         }