git.cljs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. (ns frontend.handler.git
  2. (:require [frontend.util :as util :refer-macros [profile]]
  3. [promesa.core :as p]
  4. [lambdaisland.glogi :as log]
  5. [frontend.state :as state]
  6. [frontend.db :as db]
  7. [frontend.git :as git]
  8. [frontend.date :as date]
  9. [frontend.handler.notification :as notification]
  10. [frontend.handler.route :as route-handler]
  11. [frontend.handler.common :as common-handler]
  12. [frontend.config :as config]
  13. [cljs-time.local :as tl]))
  14. (defn- set-git-status!
  15. [repo-url value]
  16. (db/set-key-value repo-url :git/status value)
  17. (state/set-git-status! repo-url value))
  18. (defn- set-git-last-pulled-at!
  19. [repo-url]
  20. (db/set-key-value repo-url :git/last-pulled-at
  21. (date/get-date-time-string (tl/local-now))))
  22. (defn- set-git-error!
  23. [repo-url value]
  24. (db/set-key-value repo-url :git/error (if value (str value))))
  25. (defn git-add
  26. ([repo-url file]
  27. (git-add repo-url file true))
  28. ([repo-url file update-status?]
  29. (when (and (not (config/local-db? repo-url))
  30. (not (util/electron?)))
  31. (-> (p/let [result (git/add repo-url file)]
  32. (when update-status?
  33. (common-handler/check-changed-files-status)))
  34. (p/catch (fn [error]
  35. (println "git add '" file "' failed: " error)
  36. (js/console.error error)))))))
  37. (defn commit-and-force-push!
  38. [commit-message pushing?]
  39. (when-let [repo (frontend.state/get-current-repo)]
  40. (->
  41. (p/let [remote-oid (common-handler/get-remote-ref repo)
  42. commit-oid (git/commit repo commit-message (array remote-oid))
  43. result (git/write-ref! repo commit-oid)
  44. token (common-handler/get-github-token repo)
  45. push-result (git/push repo token true)]
  46. (reset! pushing? false)
  47. (notification/clear! nil)
  48. (route-handler/redirect! {:to :home}))
  49. (p/catch (fn [error]
  50. (log/error :git/commit-and-force-push! error))))))
  51. (defn git-set-username-email!
  52. [repo-url {:keys [name email]}]
  53. (when (and name email)
  54. (git/set-username-email
  55. (config/get-repo-dir repo-url)
  56. name
  57. email)))