 
 (defn- clone-repo
   "Clones the latest version of the specified repo from GIT."
-  ([branch repo-name]
+  ([repo-name branch]
      (info "First ensuring that repository directory does not exist")
-     (rm "-rf" (repo-path repo-name))
-     (info "Cloning repository to" (repo-path repo-name))
+     (rm "-rf" (repo-path repo-name branch))
+     (info "Cloning repository to" (repo-path repo-name branch))
      (->
       (Git/cloneRepository)
       (.setURI (repo-url repo-name))
-      (.setDirectory (as-file (repo-path repo-name)))
+      (.setDirectory (as-file (repo-path repo-name branch)))
       (.setRemote "origin")
       (.setBranch branch)
       (.setBare false)

  (str base-git-url repo-name))

(defn- repo-path
  [name]
  (str base-git-path name))

(defn- repo-branch
  "Always look for the master branch for the environment default configuration file"

        loader (.open repo (.getObjectId twalk 0))]
    (slurp (.openStream loader))))

(defn- repo-exists?
  [repo-name branch]
  (.exists (as-file (repo-path repo-name branch))))

(defn- ensure-repo-up-to-date
  "Gets or updates the specified repo from GIT"
  [repo-name branch]
  (if (repo-exists? repo-name branch)
    (pull-repo repo-name branch)
    (do
      (info (str "Repo '" repo-name "' not found - attempting to clone"))
      (clone-repo repo-name branch))))

(defn get-data
  "Fetches the data corresponding to the given application from GIT"
  [env application readonly]
  (try
    (let [branch (repo-branch env application readonly)]
      (ensure-repo-up-to-date application branch)
      (get-head application branch))
    (catch InvalidRemoteException e
      (info (str "Can't communicate with remote repo '" application  "': " e))
      nil)
