|
| 1 | +#lang racket |
| 2 | +(require racket/runtime-path |
| 3 | + rackunit) |
| 4 | + |
| 5 | +(define-runtime-module-path-index main "../main.rkt") |
| 6 | + |
| 7 | +(define work-dir (make-temporary-file "~a-git-slice" |
| 8 | + 'directory)) |
| 9 | + |
| 10 | +(define (reset-dir dir) |
| 11 | + (delete-directory/files #:must-exist? #f dir) |
| 12 | + (make-directory* dir)) |
| 13 | + |
| 14 | +(define repo-dir (build-path work-dir "repo")) |
| 15 | +(reset-dir repo-dir) |
| 16 | + |
| 17 | +(define commit-counts (hash)) |
| 18 | + |
| 19 | +;; ---------------------------------------- |
| 20 | +;; Script a repository with interesting moves, branching, etc. |
| 21 | + |
| 22 | +(parameterize ([current-directory repo-dir]) |
| 23 | + (system "git init") |
| 24 | + |
| 25 | + (define (slices->msg line l) |
| 26 | + (~s (apply ~a #:separator " " "CHANGED:" line l))) |
| 27 | + (define (merge-counts a b) |
| 28 | + (for/fold ([a a]) ([(k v) (in-hash b)]) |
| 29 | + (hash-update a k (lambda (n) (+ n v)) 0))) |
| 30 | + (define (increment-counts counts slices) |
| 31 | + (for/fold ([ht counts]) ([slice (in-list slices)]) |
| 32 | + (hash-update ht slice add1 0))) |
| 33 | + |
| 34 | + (define n 32) |
| 35 | + (define (create p) |
| 36 | + ;; Create a file that will not be considered a copy |
| 37 | + ;; of any other file. |
| 38 | + (make-directory* (path-only p)) |
| 39 | + (call-with-output-file* |
| 40 | + p |
| 41 | + (lambda (o) |
| 42 | + (for ([i 100]) |
| 43 | + (display (random 100) o) |
| 44 | + (displayln (make-bytes n 60) o)) |
| 45 | + (set! n (add1 n))))) |
| 46 | + (define (modify p) |
| 47 | + (define s (file->bytes p)) |
| 48 | + (call-with-output-file* |
| 49 | + #:exists 'update |
| 50 | + p |
| 51 | + (lambda (o) |
| 52 | + (displayln (bytes-append |
| 53 | + (subbytes s 0 n) |
| 54 | + (make-bytes 100 n) |
| 55 | + (subbytes s n)) |
| 56 | + o) |
| 57 | + (set! n (add1 n))))) |
| 58 | + (define (move s d) |
| 59 | + (make-directory* (path-only d)) |
| 60 | + (system (~a "git mv " s " " d))) |
| 61 | + (define (copy s d) |
| 62 | + (make-directory* (path-only d)) |
| 63 | + (copy-file s d)) |
| 64 | + (define (do-commit line . slices) |
| 65 | + (set! commit-counts (increment-counts commit-counts slices)) |
| 66 | + (system (~a "git add . && git commit -m " (slices->msg line slices)))) |
| 67 | + (define-syntax (commit stx) |
| 68 | + (syntax-case stx () |
| 69 | + [(_ slice ...) |
| 70 | + #`(do-commit #,(syntax-line stx) slice ...)])) |
| 71 | + (define (fork a b) |
| 72 | + (define old-counts commit-counts) |
| 73 | + (set! commit-counts (hash)) |
| 74 | + (system "git branch left && git checkout left") |
| 75 | + (a) |
| 76 | + (define left-counts commit-counts) |
| 77 | + (set! commit-counts (hash)) |
| 78 | + (system "git checkout master") |
| 79 | + (b) |
| 80 | + (define right-counts commit-counts) |
| 81 | + (define both-counts (merge-counts left-counts right-counts)) |
| 82 | + (define slices (hash-keys both-counts)) |
| 83 | + (system (~a "git merge -m " (slices->msg 0 slices) " left" |
| 84 | + " && git branch -d left")) |
| 85 | + (set! commit-counts (merge-counts old-counts both-counts))) |
| 86 | + |
| 87 | + ;; ------------------------------------------------------------ |
| 88 | + ;; Files with names ending in "..._Alpha" will end up in "alpha", |
| 89 | + ;; etc., and each of those directories corresponds to a slice to |
| 90 | + ;; try. Each `commit` names the end slices that should include |
| 91 | + ;; the commit. |
| 92 | + |
| 93 | + (create "a/x_Alpha") |
| 94 | + (commit "Alpha") |
| 95 | + (modify "a/x_Alpha") |
| 96 | + (create "a/y_Alpha") |
| 97 | + (commit "Alpha") |
| 98 | + (create "b/x_Beta") |
| 99 | + (create "b/y_Beta") |
| 100 | + (commit "Beta") |
| 101 | + (move "a/x_Alpha" "a/z_Alpha") |
| 102 | + (move "a/y_Alpha" "c/y_Alpha") |
| 103 | + (commit "Alpha") |
| 104 | + (copy "b/x_Beta" "b/z_Beta") |
| 105 | + (commit "Beta") |
| 106 | + |
| 107 | + (fork |
| 108 | + (lambda () |
| 109 | + (create "c/x_Gamma") |
| 110 | + (commit "Gamma") |
| 111 | + (modify "a/z_Alpha") |
| 112 | + (commit "Alpha")) |
| 113 | + (lambda () |
| 114 | + (create "d/x_Delta") |
| 115 | + (commit "Delta") |
| 116 | + (modify "b/z_Beta") |
| 117 | + (move "b/y_Beta" "c/y_Beta") |
| 118 | + (commit "Beta"))) |
| 119 | + |
| 120 | + ;; Move all into place: |
| 121 | + (move "a/z_Alpha" "alpha/x_Alpha") |
| 122 | + (move "c/y_Alpha" "alpha/y_Alpha") |
| 123 | + (move "b/x_Beta" "beta/x_Beta") |
| 124 | + (move "c/y_Beta" "beta/y_Beta") |
| 125 | + (move "b/z_Beta" "beta/z_Beta") |
| 126 | + (move "c/x_Gamma" "gamma/x_Gamma") |
| 127 | + (move "d/x_Delta" "delta/x_Delta") |
| 128 | + (commit "Alpha" "Beta" "Gamma" "Delta")) |
| 129 | + |
| 130 | +;; ---------------------------------------- |
| 131 | +;; Extract and check slices |
| 132 | + |
| 133 | +(define (slice slice) |
| 134 | + (define slice-dir (build-path work-dir "slice")) |
| 135 | + (reset-dir slice-dir) |
| 136 | + (parameterize ([current-directory work-dir]) |
| 137 | + (system "git clone repo slice")) |
| 138 | + |
| 139 | + (define dir (string-foldcase slice)) |
| 140 | + |
| 141 | + (parameterize ([current-directory slice-dir] |
| 142 | + [current-command-line-arguments (vector dir)] |
| 143 | + [current-namespace (make-base-namespace)]) |
| 144 | + (define-values (name base) (module-path-index-split main)) |
| 145 | + (dynamic-require (module-path-index-join name base) #f)) |
| 146 | + |
| 147 | + (check-equal? (directory-list slice-dir) |
| 148 | + (map string->path (list ".git" dir))) |
| 149 | + |
| 150 | + (parameterize ([current-directory slice-dir]) |
| 151 | + (define o (open-output-bytes)) |
| 152 | + (parameterize ([current-output-port o]) |
| 153 | + (system "git log")) |
| 154 | + (define commit-count |
| 155 | + (for/fold ([count 0]) ([l (in-lines (open-input-string |
| 156 | + (get-output-string o)))]) |
| 157 | + (cond |
| 158 | + [(regexp-match? #rx"^commit " l) |
| 159 | + (add1 count)] |
| 160 | + [(regexp-match? #rx"^CHANGED: " l) |
| 161 | + (check-true (member slice (string-split l)))] |
| 162 | + [else count]))) |
| 163 | + (check-equal? commit-count (hash-ref commit-counts slice)) |
| 164 | + (unless (equal? commit-count (hash-ref commit-counts slice)) |
| 165 | + (exit)) |
| 166 | + )) |
| 167 | + |
| 168 | +(slice "Alpha") |
| 169 | +(slice "Beta") |
| 170 | +(slice "Gamma") |
| 171 | +(slice "Delta") |
| 172 | + |
| 173 | +;; ---------------------------------------- |
| 174 | + |
| 175 | +(delete-directory/files work-dir) |
0 commit comments