]> gitweb.factorcode.org Git - factor.git/commitdiff
extra: Add cli.git and github.sync.
authorDoug Coleman <doug.coleman@gmail.com>
Thu, 3 Aug 2017 23:43:41 +0000 (18:43 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Sun, 6 Aug 2017 01:40:57 +0000 (20:40 -0500)
cli.git is a command-line git wrapper.
github.sync is a way to sync an organization to local disk.

extra/cli/git/authors.txt [new file with mode: 0644]
extra/cli/git/git.factor [new file with mode: 0644]
extra/web-services/github/github.factor [new file with mode: 0644]

diff --git a/extra/cli/git/authors.txt b/extra/cli/git/authors.txt
new file mode 100644 (file)
index 0000000..7c1b2f2
--- /dev/null
@@ -0,0 +1 @@
+Doug Coleman
diff --git a/extra/cli/git/git.factor b/extra/cli/git/git.factor
new file mode 100644 (file)
index 0000000..a05ebc0
--- /dev/null
@@ -0,0 +1,37 @@
+! Copyright (C) 2017 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: arrays concurrency.combinators concurrency.semaphores fry
+io.directories io.files.info io.launcher io.pathnames kernel
+math namespaces sequences splitting system-info ;
+IN: cli.git
+
+SYMBOL: cli-git-num-parallel
+cli-git-num-parallel [ cpus 2 * ] initialize
+
+: git-clone-as ( ssh-url path -- process )
+    [ { "git" "clone" } ] 2dip 2array append run-process ;
+
+: git-clone ( ssh-url -- process )
+    [ { "git" "clone" } ] dip suffix run-process ;
+
+: git-pull ( path -- process )
+    [ { "git" "pull" } run-process ] with-directory ;
+
+: git-repository? ( directory -- ? )
+    ".git" append-path current-directory get prepend-path
+    ?file-info dup [ directory? ] when ;
+
+: repository-url>name ( string -- string' )
+    file-name ".git" ?tail drop ;
+
+: update-repository ( url -- process )
+    dup repository-url>name git-repository?
+    [ repository-url>name git-pull ] [ git-clone ] if ;
+
+: sync-repositories ( directory urls -- )
+    '[
+        _ cli-git-num-parallel get <semaphore> '[
+            _ [ update-repository ] with-semaphore
+        ] parallel-each
+    ] with-ensure-directory ;
+
diff --git a/extra/web-services/github/github.factor b/extra/web-services/github/github.factor
new file mode 100644 (file)
index 0000000..6eaa9d2
--- /dev/null
@@ -0,0 +1,33 @@
+! Copyright (C) 2017 Doug Coleman.
+! See http://factorcode.org/license.txt for BSD license.
+USING: assocs cli.git concurrency.combinators
+concurrency.semaphores formatting fry http.client io
+io.directories json.reader kernel locals math namespaces
+sequences ;
+IN: github
+
+SYMBOL: github-username
+SYMBOL: github-token
+
+:: get-organization-repositories-with-credentials ( organization username token -- seq )
+    0 [ dup ] [
+        1 + dup
+        [ username token organization ] dip
+        "https://%s:%s@api.github.com/orgs/%s/repos?per_page=100&page=%d" sprintf http-get nip json>
+        dup empty? [ 2drop f f ] [ ] if
+    ] produce nip concat ;
+
+: get-organization-repositories ( organization -- seq )
+    github-username get
+    github-token get
+    get-organization-repositories-with-credentials ;
+
+: sync-organization-with-credentials ( directory organization username token -- )
+    get-organization-repositories-with-credentials
+    [ "ssh_url" of ] map sync-repositories ;
+
+: sync-organization ( directory organization -- )
+    github-username get
+    github-token get
+    sync-organization-with-credentials ;
+