Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 299fcfa480e925002d78dce408ecbc686e767e14 Takahiro Hozumi committed Feb 22, 2011
Showing with 260 additions and 0 deletions.
  1. +25 −0 .gitignore
  2. +13 −0 README
  3. +8 −0 project.clj
  4. +9 −0 src/visugit/core.clj
  5. +105 −0 src/visugit/git.clj
  6. +94 −0 src/visugit/shell_out.clj
  7. +6 −0 test/visugit/test/core.clj
@@ -0,0 +1,25 @@
+build
+target
+lib
+*.dot
+
+# use glob syntax.
+syntax: glob
+creds.clj
+Manifest.txt
+pom.xml
+aws.clj
+*.ser
+*.class
+*.jar
+*~
+*.bak
+*.off
+*.old
+.DS_Store
+*.#*
+*#*
+*.classpath
+*.project
+*.settings
+*.pyc
@@ -0,0 +1,13 @@
+# visugit
+
+FIXME: write description
+
+## Usage
+
+FIXME: write
+
+## License
+
+Copyright (C) 2010 FIXME
+
+Distributed under the Eclipse Public License, the same as Clojure.
@@ -0,0 +1,8 @@
+(defproject visugit "1.0.0-SNAPSHOT"
+ :description "FIXME: write"
+ :warn-on-reflection true,
+ :dependencies [[org.clojure/clojure "1.2.0"]
+ [org.clojure/clojure-contrib "1.2.0"]
+ [org.apache.commons/commons-exec "1.1"]
+ [org.clojars.hozumi/clj-glob "0.1.0"]]
+ :dev-dependencies [[swank-clojure "1.3.0-SNAPSHOT"]])
@@ -0,0 +1,9 @@
+(ns visugit.core
+ (:require [visugit.shell-out :as shell]
+ [visugit.git :as git]
+ [org.satta.glob :as glob]
+ [clojure.java.io :as jio]
+ [clojure.string :as cstr]
+ [clojure.contrib.string :as ccstr])
+ (:import [java.io File]))
+
@@ -0,0 +1,105 @@
+(ns visugit.git
+ (:require [visugit.shell-out :as shell]
+ [org.satta.glob :as glob]
+ [clojure.java.io :as jio]
+ [clojure.string :as cstr]
+ [clojure.contrib.string :as ccstr])
+ (:import [java.io File]))
+
+(def dir (atom nil))
+
+(defn heads []
+ (glob/glob (str @dir ".git/refs/heads/*")))
+
+(defn get-branches []
+ (let [branch-files (glob/glob (str @dir ".git/refs/heads/*"))]
+ (reduce conj {} (map (fn [^File f]
+ [(.getName f) (cstr/trim-newline (slurp f))])
+ branch-files))))
+
+(defn get-refs []
+ (into {} (for [^String line (-> (shell/sh "git" "show-ref" "--head" {:dir @dir})
+ deref
+ :out
+ cstr/split-lines)]
+ (let [[sha1 nam] (.split line "\\s+")]
+ [nam sha1]))))
+
+(defn get-type [obj-id]
+ (-> (shell/sh "git" "cat-file" "-t" obj-id {:dir @dir})
+ deref :out cstr/trim-newline))
+
+(defn get-type-batch [obj-ids]
+ (let [m (into {}
+ (map (fn [^String line]
+ (let [[id type] (.split line "\\s")]
+ [id (keyword type)]))
+ (-> (shell/sh "git" "cat-file" "--batch-check"
+ {:dir @dir, :encode "utf-8",
+ :in (apply str (interleave obj-ids (repeat \newline)))})
+ deref :out cstr/split-lines)))]
+ (group-by m obj-ids)))
+
+(defn get-tree-id [line]
+ (let [[_ id] (re-find #"tree (.*)$" line)]
+ id))
+;; (get-tree-id "tree 47975e7102c8933c2b0ddf62bc9f92055a50167e")
+;; "47975e7102c8933c2b0ddf62bc9f92055a50167e"
+
+(defn get-parent-ids [lines]
+ (map (fn [line] (second (re-find #"parent (.*)$" line)))
+ (take-while (fn [^String line] (.startsWith line "parent")) lines)))
+
+(defn commit-obj [obj-id]
+ (let [[tree-line & lines] (-> (shell/sh "git" "cat-file" "-p" obj-id {:dir @dir})
+ deref :out cstr/split-lines)
+ tree-id (get-tree-id tree-line)
+ parent-ids (get-parent-ids lines)]
+ {:tree tree-id, :parents parent-ids}))
+
+(defn commit-map* [obj-id-queue acc]
+ (if-let [id (first obj-id-queue)]
+ (if (acc id)
+ (recur (subvec obj-id-queue 1) acc)
+ (let [co (commit-obj id)
+ added-acc (assoc acc id co)]
+ (recur (into (subvec obj-id-queue 1) (:parents co))
+ added-acc)))
+ acc))
+
+(defn commit-map []
+ (-> (get-refs) vals get-type-batch :commit (commit-map* {})))
+
+(defn tree-obj [obj-id]
+ (let [lines (-> (shell/sh "git" "cat-file" "-p" obj-id {:dir @dir})
+ deref :out cstr/split-lines)]
+ (into {}
+ (map (fn [^String line]
+ (let [[typenum type id file] (.split line "\\s+")]
+ [id {:type type :file file}]))
+ lines))))
+;;(catch Exception e (.printStackTrace e))))
+
+(defn tree? [{type :type}]
+ (= "tree" type))
+
+(defn tree-map [obj-id-queue acc]
+ (if-let [id (first obj-id-queue)]
+ (if (acc id)
+ (recur (subvec obj-id-queue 1) acc)
+ (let [t (tree-obj id)
+ added-acc (assoc acc id t)]
+ (recur (into (subvec obj-id-queue 1)
+ (keys (filter #(-> % val tree?) t)))
+ added-acc)))
+ acc))
+
+(defn tree-map-by-comits [commit-map]
+ (tree-map (into [] (map :tree (vals commit-map))) {}))
+
+;;(defn commit-map [acc commit-objs]
+;; (
+
+;;(get-type "67025a4d86c6fc87c4c5c8f9ce38c22b3019d79a" "../clj-http/")
+;;"commit"
+
@@ -0,0 +1,94 @@
+(ns visugit.shell-out
+ (:require [clojure.java.io :as jio :only [file]])
+ (:import [java.io ByteArrayOutputStream
+ ByteArrayInputStream
+ InputStream OutputStream]
+ [org.apache.commons.exec
+ CommandLine
+ DefaultExecutor
+ DefaultExecuteResultHandler
+ ExecuteException
+ ExecuteResultHandler
+ ExecuteWatchdog
+ PumpStreamHandler
+ ShutdownHookProcessDestroyer
+ Watchdog]
+ [org.apache.commons.exec.environment EnvironmentUtils]))
+
+(defn parse-args [args]
+ (split-with string? args))
+
+(defn- all-close [^InputStream in,
+ ^OutputStream out,
+ ^OutputStream err, opts]
+ (when (and in (not (:no-close-in opts)))
+ (.close in))
+ (when (not (:no-close-out opts))
+ (.close out))
+ (when (not (:no-close-err opts))
+ (.close err)))
+
+(defn- conv [st ^String enc]
+ (when (instance? ByteArrayOutputStream st)
+ (let [b (.toByteArray ^ByteArrayOutputStream st)]
+ (cond (nil? (seq b)) nil
+ (= enc :byte) b
+ (string? enc) (String. b enc)
+ :else (String. b (System/getProperty "file.encoding"))))))
+
+(defn response [in out err opts exit-value]
+ (let [enc (:encode opts)
+ o (conv out enc)
+ e (conv err enc)]
+ (all-close in out err opts)
+ {:exit exit-value,
+ :out o,
+ :err e}))
+
+(defrecord MyResultHandler [ret in out err opts]
+ ExecuteResultHandler
+ (onProcessComplete
+ [_ exit-value]
+ (deliver ret (response in out err opts exit-value)))
+ (onProcessFailed
+ [_ e]
+ (deliver ret
+ (assoc (response in out err opts (.getExitValue e))
+ :fail e))))
+
+(defn string->input-stream [^String s ^String encode]
+ (ByteArrayInputStream. (.getBytes s (or encode (System/getProperty "file.encoding")))))
+
+(defn sh [& args-and-opts]
+ (let [[[^String comm & args] [opts]] (parse-args args-and-opts)
+ command (CommandLine. comm)
+ in (when-let [i (:in opts)]
+ (if (string? i) (string->input-stream i (:encode opts)) i))
+ out (or (:out opts) (ByteArrayOutputStream.))
+ err (or (:err opts) (ByteArrayOutputStream.))
+ stream-handler (PumpStreamHandler. out err in)
+ executor (DefaultExecutor.)
+ ret (promise)
+ ^ExecuteResultHandler result-handler (MyResultHandler. ret in out err opts)]
+ (doseq [arg args]
+ (.addArgument command arg))
+ (when-let [dir (:dir opts)]
+ (.setWorkingDirectory executor (jio/file dir)))
+ (when-let [success (:as-success opts)]
+ (.setExitValue executor success))
+ (when-let [successes (:as-successes opts)]
+ (.setExitValues executor (into-array Integer/TYPE successes)))
+ (when-let [ms (:watchdog opts)]
+ (.setWatchdog executor (ExecuteWatchdog. ms)))
+ (when-not (:no-destroy opts)
+ (.setProcessDestroyer executor (ShutdownHookProcessDestroyer.)))
+ (.setStreamHandler executor stream-handler)
+ (if-let [env (:env opts)]
+ (.execute executor command env result-handler)
+ (if-let [add-env (:add-env opts)]
+ (let [env (EnvironmentUtils/getProcEnvironment)]
+ (doseq [[k v] add-env]
+ (.put env k v))
+ (.execute executor command env result-handler))
+ (.execute executor command result-handler)))
+ ret))
@@ -0,0 +1,6 @@
+(ns visugit.test.core
+ (:use [visugit.core] :reload)
+ (:use [clojure.test]))
+
+(deftest replace-me ;; FIXME: write
+ (is false "No tests have been written."))

0 comments on commit 299fcfa

Please sign in to comment.