Created
November 8, 2016 14:53
-
-
Save xificurC/f4de1993b3218a50dd8936dfc0ec16f2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env factor-vm | |
USING: kernel io namespaces sequences io.launcher io.directories io.encodings.utf8 io.files io.files.info io.pathnames concurrency.messaging threads math tools.threads accessors calendar ; | |
IN: script | |
: git-clean? ( path -- t/f ) | |
[ "git status --untracked-files=no --porcelain" utf8 [ read1 not ] | |
with-process-reader* ] with-directory nip 0 = and ; | |
: git-pull ( path -- ) | |
[ "git pull" run-process ] with-directory drop ; | |
: git-fetch ( path -- ) | |
[ "git fetch" run-process ] with-directory drop ; | |
: git-directory? ( path -- t/f ) | |
".git" append-path dup exists? [ file-info directory? ] [ drop f ] if ; | |
SYMBOLS: pull fetch ; | |
: git-thread ( main-thread path -- ) | |
dup git-clean? | |
[ git-pull pull swap send ] | |
[ git-fetch fetch swap send ] | |
if ; | |
! this implementation finishes without waiting for the threads to complete | |
! home "git" append-path [ | |
! ! now we have the entries list on the stack | |
! ! keep only git directories | |
! [ git-directory? ] filter | |
! ! count them | |
! dup length | |
! ! spawn the thread that will print information about the updates | |
! [ [ receive fetch = [ "FETCHED" print ] [ "PULLED" print ] if ] times ] curry "receiver" spawn | |
! ! for each git directory launch an update thread | |
! swap over [ over [ git-thread ] 2curry swap spawn drop ] curry each | |
! ! now we should wait until the receiver thread finishes | |
! [ dup state>> ] [ 50 milliseconds sleep ] while drop | |
! ] with-directory-files | |
! while this version does wait the receiver part only kicks off | |
! when everyone is finished. | |
! Its purpose is to show progress live. | |
home "git" append-path [ | |
! now we have the entries list on the stack | |
! keep only git directories | |
[ git-directory? ] filter | |
! count them and push the count at the bottom of the stack | |
[ length ] keep | |
! for each git directory launch an update thread | |
[ dup self swap [ git-thread ] 2curry swap spawn drop ] each | |
! we are left with the count on the stack | |
! so let's wait for that many messages and print something | |
[ receive fetch = [ "FETCHED" print ] [ "PULLED" print ] if ] times | |
] with-directory-files | |
! threads. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment