Created
October 11, 2020 10:00
-
-
Save FieryCod/4d74a00cb75595b23fc3c5880eeea044 to your computer and use it in GitHub Desktop.
Copies content of one repo to another using commit sha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
#_( | |
#_DEPS is same format as deps.edn. Multiline is okay. | |
DEPS=' | |
{:deps {}} | |
' | |
#_You can put other options here | |
OPTS=' | |
-J-Xms256m -J-Xmx256m -J-client | |
' | |
exec clojure $OPTS -Sdeps "$DEPS" -M "$0" "$@" | |
) | |
(require '[clojure.java.shell :as shell :refer [sh]]) | |
(require '[clojure.java.io :as io]) | |
(require '[clojure.string :as s]) | |
(defn sh-shim | |
[& args] | |
(let [{:keys [reverse-out-err] :as opts} | |
(if (map? (last args)) | |
(last args) | |
nil) | |
{:keys [out err]} | |
(let [result (apply sh (if opts (butlast args) args))] | |
(if-not reverse-out-err | |
result | |
{:err (:out result) | |
:out (:err result)}))] | |
(if (s/blank? err) | |
(do | |
(println "[OUT]: " (s/trim out)) | |
out) | |
(do (println "[ERR]: " (s/trim err)) | |
(System/exit 1))))) | |
(def args (into {:from "Bank" | |
:to "bank" | |
:message nil | |
:commit nil} | |
(map (fn [[k v]] [(keyword (subs k 1)) v]) (partition 2 *command-line-args*)))) | |
(println "[ENTRY ARGS]" args) | |
(when-not (:commit args) | |
(println "Commit sha should be specified. Exiting") | |
(System/exit 1)) | |
(defn path | |
[& [x]] | |
(.getAbsolutePath (if (or (string? x) (nil? x)) | |
(io/file (or x "")) | |
x))) | |
(defn join | |
[& args] | |
(path (apply io/file args))) | |
(shell/with-sh-dir (join (path) (:from args)) | |
(sh-shim "git" "reset" "--hard" (:commit args)) | |
(def message (or (:message args) | |
(sh-shim "git" "show-branch" "--no-name" "HEAD"))) | |
(sh-shim "mv" ".git" "git-clone")) | |
(shell/with-sh-dir (join (path) (:from args)) | |
(sh-shim "cp" "-r" "." (str (join (path) (:to args)))) | |
(sh-shim "mv" "git-clone" ".git")) | |
(shell/with-sh-dir (join (path) (:to args)) | |
(sh-shim "rm" "-Rf" "git-clone") | |
(sh-shim "git" "add" ".") | |
(sh-shim "git" "commit" "-m" message "--no-edit") | |
(sh-shim "git" "push" "origin" "master" {:reverse-out-err true})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment