Selaa lähdekoodia

fix: git add unstaged files before committing

Yukun Guo 4 vuotta sitten
vanhempi
sitoutus
a1ff34f4b1
3 muutettua tiedostoa jossa 33 lisäystä ja 4 poistoa
  1. 5 1
      resources/js/worker.js
  2. 25 0
      src/main/frontend/git.cljs
  3. 3 3
      src/main/frontend/handler/repo.cljs

+ 5 - 1
resources/js/worker.js

@@ -282,7 +282,11 @@ if (detect() === 'Worker') {
       })
     },
     statusMatrix: async function (dir) {
-      await git.statusMatrix({ fs, dir });
+      return git.statusMatrix({ fs, dir });
+    },
+    statusMatrixChanged: async function (dir) {
+      return (await git.statusMatrix({ fs, dir }))
+        .filter(([_, head, workDir, stage]) => !(head == 1 && workDir == 1 && stage == 1));
     },
     getChangedFiles: async function (dir) {
       try {

+ 25 - 0
src/main/frontend/git.cljs

@@ -106,6 +106,31 @@
                                     email
                                     parent))))
 
+(defn add-all
+  "Equivalent to `git add --all`. Returns changed files."
+  [repo-url]
+  (p/let [repo-dir (util/get-repo-dir repo-url)
+          status-matrix (js/window.workerThread.statusMatrixChanged repo-dir)
+          changed-files (for [[file head work-dir _stage] status-matrix
+                              :when (not= head work-dir)]
+                          file)
+          unstaged-files (for [[file _head work-dir stage] status-matrix
+                               :when (not= work-dir stage)]
+                           file)
+          _ (p/all (for [file unstaged-files]
+                     (add repo-url file)))]
+    changed-files))
+
+(defn commit-non-empty
+  "Equivalent to `git add --all` and then `git commit` without `--allow-empty`."
+  ([repo-url message]
+   (commit-non-empty repo-url message nil))
+  ([repo-url message parent]
+   (p/let [changed-files (add-all repo-url)]
+     (if (not-empty changed-files)
+       (commit repo-url message parent)
+       (p/resolved nil)))))
+
 (defn read-commit
   [repo-url oid]
   (js/window.workerThread.readCommit (util/get-repo-dir repo-url)

+ 3 - 3
src/main/frontend/handler/repo.cljs

@@ -139,8 +139,8 @@
                file-exists? (fs/create-if-not-exists repo-url repo-dir file-path content)]
          (when-not file-exists?
            (file-handler/reset-file! repo-url path content)
-           (ui-handler/re-render-root!))
-         (git-handler/git-add repo-url path))))))
+           (ui-handler/re-render-root!)
+           (git-handler/git-add repo-url path)))))))
 
 (defn create-today-journal!
   []
@@ -427,7 +427,7 @@
          (state/input-idle? repo-url)
          (or (not= status :pushing)
              custom-commit?))
-      (-> (p/let [files (js/window.workerThread.getChangedFiles (util/get-repo-dir (state/get-current-repo)))]
+      (-> (p/let [files (git/add-all (state/get-current-repo))]
             (when (or (seq files) merge-push-no-diff?)
               ;; auto commit if there are any un-committed changes
               (let [commit-message (if (string/blank? commit-message)