git.cljs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. (ns frontend.git
  2. (:refer-clojure :exclude [clone])
  3. (:require [promesa.core :as p]
  4. [frontend.util :as util]
  5. [clojure.string :as string]))
  6. ;; only support Github now
  7. (defn auth
  8. [token]
  9. {:username token
  10. :password "x-oauth-basic"})
  11. (defn set-username-email
  12. [dir username email]
  13. (prn {:dir dir
  14. :username username
  15. :email email})
  16. (util/p-handle (js/git.config (clj->js
  17. {:dir dir
  18. :path "user.name"
  19. :value username}))
  20. (fn [result]
  21. (js/git.config (clj->js
  22. {:dir dir
  23. :path "user.email"
  24. :value email})))
  25. (fn [error]
  26. (prn "error:" error))))
  27. (defn with-auth
  28. [token m]
  29. (clj->js
  30. (merge (auth token)
  31. m)))
  32. (defn get-repo-dir
  33. [repo-url]
  34. (str "/" (last (string/split repo-url #"/"))))
  35. (defn clone
  36. [repo-url token]
  37. (js/git.clone (with-auth token
  38. {:dir (get-repo-dir repo-url)
  39. :url repo-url
  40. :corsProxy "https://cors.isomorphic-git.org"
  41. :singleBranch true
  42. :depth 1})))
  43. (defn list-files
  44. [repo-url]
  45. (js/git.listFiles (clj->js
  46. {:dir (get-repo-dir repo-url)
  47. :ref "HEAD"})))
  48. (defn fetch
  49. [repo-url token]
  50. (js/git.fetch (with-auth token
  51. {:dir (get-repo-dir repo-url)
  52. :ref "master"
  53. :singleBranch true})))
  54. (defn log
  55. [repo-url token depth]
  56. (js/git.log (with-auth token
  57. {:dir (get-repo-dir repo-url)
  58. :ref "master"
  59. :depth depth
  60. :singleBranch true})))
  61. (defn pull
  62. [repo-url token]
  63. (js/git.pull (with-auth token
  64. {:dir (get-repo-dir repo-url)
  65. :ref "master"
  66. :singleBranch true})))
  67. (defn add
  68. [repo-url file]
  69. (js/git.add (clj->js
  70. {:dir (get-repo-dir repo-url)
  71. :filepath file})))
  72. ;; TODO: cache email and name
  73. (defn commit
  74. [repo-url message]
  75. (js/git.commit (clj->js
  76. {:dir (get-repo-dir repo-url)
  77. :author {:name "Orgnote"
  78. :email "[email protected]"}
  79. :message message})))
  80. (defn push
  81. [repo-url token]
  82. (js/git.push (with-auth token
  83. {:dir (get-repo-dir repo-url)
  84. :remote "origin"
  85. :ref "master"
  86. })))
  87. (defn add-commit-push
  88. [repo-url file message token push-ok-handler push-error-handler]
  89. (util/p-handle
  90. (let [files (if (coll? file) file [file])]
  91. (doseq [file files]
  92. (add repo-url file)))
  93. (fn [_]
  94. (util/p-handle
  95. (commit repo-url message)
  96. (fn [_]
  97. (push repo-url token)
  98. (push-ok-handler))
  99. push-error-handler))))
  100. (defn add-commit
  101. [repo-url file message commit-ok-handler commit-error-handler]
  102. (let [get-seconds (fn []
  103. (/ (.getTime (js/Date.)) 1000))]
  104. (let [t1 (get-seconds)]
  105. (util/p-handle
  106. (add repo-url file)
  107. (fn [_]
  108. (let [t2 (get-seconds)]
  109. (prn "Add time: " (- t2 t1))
  110. (util/p-handle
  111. (commit repo-url message)
  112. (fn []
  113. (let [t3 (get-seconds)]
  114. (prn "Commit time: " (- t3 t2)))
  115. (prn "Commited")
  116. (commit-ok-handler))
  117. (fn [error]
  118. (commit-error-handler error))))
  119. )))
  120. ))