   "fns for interacting with git."
   (:import java.io.File)
   (:require [clojure.string :as str])
+  (:use [circle.util.except :only (throw-if-not)])
   (:use [clojure.tools.logging :only (infof)])
   (:use [arohner.utils :only (inspect)])
   (:require [circle.sh :as sh]))

(defn latest-local-commit
  "Returns the most recent commit id, on the current branch."
  [repo]
  (->
   (git-fn* (sh/quasiquote (git log -1 "--pretty=format:%H")))
   :out))

(defn latest-remote-commit
  "Returns the most recent on origin/master. Does not fetch."
  [repo]
  (->
   (git-fn* (sh/quasiquote (git log -1 "--pretty=format:%H" "origin/master")))
   :out))

(defn committer-email
  "Returns the email address of the committer"
  [repo commit-id]
  (->
   (git-fn* (sh/quasiquote (git log ~commit-id -1 "--format=%ae")))
   :out
   (str/trim)))
  "Takes a seq of stevedore code. Executes it locally, with GIT_SSH and GIT_SSH_KEY set up."
  [steve & {:keys [ssh-key repo]}]
  (with-temp-ssh-key-file [f ssh-key]
    (let [result (sh/sh steve
                        :environment (when ssh-key
                                       {"GIT_SSH" git-ssh-path
                                        "GIT_SSH_KEY" f})
                        :pwd repo)]
      (throw-if-not (zero? (-> result :exit)) "git command %s returned %s: %s" (first steve) (-> result :exit) (-> result :err))
      result)))

(defn clone
  "Clone a git repo at url, writing the directory to path"
