shrub/pkg/arvo/ted/ddiff.hoon

305 lines
8.3 KiB
Plaintext
Raw Normal View History

2021-03-02 23:05:27 +03:00
/- spider
/+ strandio
=, strand=strand:spider
=, clay
^- thread:spider
|= arg=vase
=/ m (strand ,vase)
^- form:m
|^
2021-03-06 01:09:24 +03:00
:: workaround to make the shallow flag optional. if it's specified we
:: do require a non-empty path - however this shouldn't be called with
:: empty paths to begin with.
=+ !<([~ =a=path b=$~([/hi &] $^([(lest @ta) flag] path))] arg)
=/ [b-path=path shallow=flag] ?:(?=([^ *] b) b [`path`b |])
2021-03-02 23:05:27 +03:00
=/ a-beam (need (de-beam a-path))
=/ b-beam (need (de-beam b-path))
;< a-dome=dome bind:m (get-from-clay a-beam dome %v)
;< b-dome=dome bind:m (get-from-clay b-beam dome %v)
2021-03-06 01:09:24 +03:00
;< diffs=(list diff-type) bind:m (diff-beams a-beam b-beam)
2021-03-02 23:05:27 +03:00
%- pure:m
!> ^- tang
2021-03-06 01:09:24 +03:00
:: our tang is built in reverse order
%- flop
2021-03-02 23:05:27 +03:00
?: shallow
(format-shallow diffs q.a-beam q.b-beam)
(format-deep diffs q.a-beam q.b-beam)
::
:: $diff-type: type for diffs produced.
:: 1. %txt-diff is a standard diff (i.e. for hoon and txt files)
:: 2. %directory-diff shows unique files between two directories
:: 3. %other is for files that don't use txt-diff - we just take
:: the mug of the files
+$ diff-type
2021-03-06 01:09:24 +03:00
:: paths of the diffed beams
$: a=path
b=path
2021-03-02 23:05:27 +03:00
$% [%txt-diff diff=(urge cord)]
[%directory-diff p=(list path) q=(list path)]
[%other p=@ q=@]
==
==
:: +diff-is-empty: check if a diff is empty (for two identical files)
::
++ diff-is-empty
|= d=diff-type
2021-03-06 01:09:24 +03:00
^- flag
?: ?=([%txt-diff *] +.+.d)
:: levy produces & on empty lists
%+ levy
diff.d
|= u=(unce cord)
2021-03-02 23:05:27 +03:00
^- flag
2021-03-06 01:09:24 +03:00
-:u
?: ?=([%directory-diff *] +.+.d)
2021-03-02 23:05:27 +03:00
=(p.d q.d)
2021-03-06 01:09:24 +03:00
=(p.d q.d)
2021-03-02 23:05:27 +03:00
:: +get-file: retrieve a cage of a file from clay
::
++ get-file
|= =beam
=/ m (strand ,cage)
^- form:m
;< =riot:clay bind:m
(warp:strandio p.beam q.beam ~ %sing %x r.beam s.beam)
?~ riot
(strand-fail:strandio %file-not-found >s.beam< ~)
(pure:m r.u.riot)
:: +get-from-clay: retrieve other data from clay based on care
::
++ get-from-clay
|* [=beam mol=mold =care]
=/ m (strand ,mol)
^- form:m
;< =riot:clay bind:m
(warp:strandio p.beam q.beam ~ %sing care r.beam s.beam)
?~ riot
(strand-fail:strandio %file-not-found >s.beam< ~)
(pure:m !<(mol q.r.u.riot))
:: +diff-beams: recursively diff two beams. produces a vase
:: of (list diff-type)
::
++ diff-beams
=<
2021-03-06 01:09:24 +03:00
|= [a=beam b=beam]
=/ m (strand ,(list diff-type))
2021-03-02 23:05:27 +03:00
^- form:m
;< hash-a=@ bind:m (get-from-clay a @ %z)
;< hash-b=@ bind:m (get-from-clay b @ %z)
:: if the recursive hashes for each beam are the same we bail early
?: =(hash-a hash-b)
2021-03-06 01:09:24 +03:00
(pure:m *(list diff-type))
;< a-arch=arch bind:m (get-from-clay a arch %y)
;< b-arch=arch bind:m (get-from-clay b arch %y)
;< file-diff=(unit diff-type) bind:m (diff-file-contents a a-arch b b-arch)
2021-03-02 23:05:27 +03:00
:: get distinct files along with shared files
2021-03-06 01:09:24 +03:00
=/ a-keys=(set @t) ~(key by dir.a-arch)
=/ b-keys=(set @t) ~(key by dir.b-arch)
2021-03-02 23:05:27 +03:00
:: unique children
=/ a-unique-children=(set @t) (~(dif in a-keys) b-keys)
=/ b-unique-children=(set @t) (~(dif in b-keys) a-keys)
2021-03-06 01:09:24 +03:00
;< a-unique=(list path) bind:m (format-unique-children a a-arch a-unique-children)
;< b-unique=(list path) bind:m (format-unique-children b b-arch b-unique-children)
=/ unique-diff=diff-type [s.a s.b %directory-diff a-unique b-unique]
2021-03-02 23:05:27 +03:00
:: shared children
=/ find-common-diffs
|.
^- form:m
=| acc=(list diff-type)
=/ common-children=(list @t) ~(tap in (~(int in a-keys) b-keys))
|-
=* loop $
^- form:m
?~ common-children
2021-03-06 01:09:24 +03:00
(pure:m acc)
2021-03-02 23:05:27 +03:00
=/ child=@t i.common-children
=/ new-a=beam a(s (snoc s.a child))
=/ new-b=beam b(s (snoc s.b child))
2021-03-06 01:09:24 +03:00
;< diffs=(list diff-type) bind:m
(diff-beams new-a new-b)
2021-03-02 23:05:27 +03:00
:: ;< introduces another $ so we use "loop" instead.
%= loop
2021-03-06 01:09:24 +03:00
acc (weld diffs acc)
2021-03-02 23:05:27 +03:00
common-children t.common-children
==
2021-03-06 01:09:24 +03:00
;< common-diffs=(list diff-type) bind:m (find-common-diffs)
2021-03-02 23:05:27 +03:00
%- pure:m
2021-03-06 01:09:24 +03:00
^- (list diff-type)
2021-03-02 23:05:27 +03:00
%+ skip
;: weld
2021-03-06 01:09:24 +03:00
(drop file-diff)
2021-03-02 23:05:27 +03:00
[unique-diff ~]
2021-03-06 01:09:24 +03:00
common-diffs
2021-03-02 23:05:27 +03:00
==
diff-is-empty
|%
:: +format-unique-children: produce list of paths representing
:: files that are unique within a directory.
::
++ format-unique-children
2021-03-06 01:09:24 +03:00
|= [bem=beam ark=arch children=(set @t)]
=/ m (strand ,(list path))
^- form:m
=/ children=(list @t) ~(tap in children)
=| acc=(list path)
|-
=* loop $
^- form:m
?~ children
(pure:m acc)
:: the %t care gives all paths with the specified prefix
;< res=(list path) bind:m (get-from-clay bem(s (snoc s.bem i.children)) (list path) %t)
%= loop
acc (weld res acc)
children t.children
==
2021-03-02 23:05:27 +03:00
:: +diff-file-contents: diff two files at specified beams,
:: producing a vase of (unit diff-type)
++ diff-file-contents
=<
2021-03-06 01:09:24 +03:00
|= [a=beam a-arch=arch b=beam b-arch=arch]
=/ m (strand ,(unit diff-type))
2021-03-02 23:05:27 +03:00
^- form:m
2021-03-06 01:09:24 +03:00
?: =(fil.a-arch fil.b-arch)
(pure:m *(unit diff-type))
?~ fil.a-arch
2021-03-02 23:05:27 +03:00
:: only b has contents
%- pure:m
2021-03-06 01:09:24 +03:00
^- (unit diff-type)
2021-03-02 23:05:27 +03:00
%- some
2021-03-06 01:09:24 +03:00
:^ s.a
s.b
2021-03-02 23:05:27 +03:00
%txt-diff
:_ ~
^- (unce cord)
:+ %|
~
~[(format-file-content-missing s.b q.b)]
2021-03-06 01:09:24 +03:00
?~ fil.b-arch
2021-03-02 23:05:27 +03:00
:: only a has contents
%- pure:m
2021-03-06 01:09:24 +03:00
^- (unit diff-type)
2021-03-02 23:05:27 +03:00
%- some
2021-03-06 01:09:24 +03:00
:^ s.a
s.b
2021-03-02 23:05:27 +03:00
%txt-diff
:_ ~
^- (unce cord)
:+ %|
~[(format-file-content-missing s.a q.a)]
~
2021-03-06 01:09:24 +03:00
:: have two file contents - check that they have
:: the same mark.
2021-03-02 23:05:27 +03:00
=/ mar=mark -:(flop s.a)
2021-03-06 01:09:24 +03:00
?: !=(mar -:(flop s.b))
(strand-fail:strandio %files-not-same-type >s.a< >s.b< ~)
2021-03-02 23:05:27 +03:00
;< =a=cage bind:m (get-file a)
;< =b=cage bind:m (get-file b)
;< =dais:clay bind:m (build-mark:strandio -.a mar)
:: for txt-diff we produce an actual diff with type (urge cord).
:: for all other marks we just take the mug)
%- pure:m
?: =(form:dais %txt-diff)
2021-03-06 01:09:24 +03:00
^- (unit diff-type)
2021-03-02 23:05:27 +03:00
%- some
2021-03-06 01:09:24 +03:00
:^ s.a
s.b
2021-03-02 23:05:27 +03:00
%txt-diff
!<((urge cord) (~(diff dais q.a-cage) q.b-cage))
2021-03-06 01:09:24 +03:00
^- (unit diff-type)
2021-03-02 23:05:27 +03:00
%- some
2021-03-06 01:09:24 +03:00
:^ s.a
s.b
2021-03-02 23:05:27 +03:00
%other
:: For some reason, vases for identical files on different desks
:: can sometimes have different types. for this reason, we only
:: take the mug of the data.
[(mug q.q.a-cage) (mug q.q.b-cage)]
|%
++ format-file-content-missing
|= [p=path d=desk]
^- cord
%- crip
;: weld
"only "
<p>
" in desk "
<d>
" has file contents"
==
--
--
2021-03-06 01:09:24 +03:00
:: +format-paths: helper to combine two paths into a tape
::
++ format-paths
|= [a=path b=path]
^- tape
;:(weld <a> " " <b>)
2021-03-02 23:05:27 +03:00
:: +format-directory-diff: helper for producing a tank based on
:: a %directory-diff
::
++ format-directory-diff
|= [paths=(list path) =desk]
^- tang
=/ prefix=tape ;:(weld "only in " (trip desk) ": ")
(turn paths |=(p=path >(weld prefix <p>)<))
:: +format-shallow: converts a list of diff-type generated
:: between desks a and b into a tang in a shallow manner (just
:: listing files that differ).
::
++ format-shallow
|= [diffs=(list diff-type) a=desk b=desk]
^- tang
%+ reel
diffs
|= [d=diff-type acc=tang]
^- tang
2021-03-06 01:09:24 +03:00
?: ?=([%txt-diff *] +.+.d)
[leaf+(format-paths a.d b.d) acc]
?: ?=([%other *] +.+.d)
[leaf+(format-paths a.d b.d) acc]
?: ?=([%directory-diff *] +.+.d)
2021-03-02 23:05:27 +03:00
;: weld
(format-directory-diff p.d a)
(format-directory-diff q.d b)
acc
==
!!
:: +format-deep: converts a list of diff-type generated
:: between desks a and b into a tang in a deep manner (preserving
:: diff information for files)
++ format-deep
|= [diffs=(list diff-type) a=desk b=desk]
^- tang
%+ reel
diffs
|= [d=diff-type acc=tang]
^- tang
2021-03-06 01:09:24 +03:00
?: ?=([%txt-diff *] +.+.d)
:+ leaf+(format-paths a.d b.d)
>diff.d<
2021-03-02 23:05:27 +03:00
acc
2021-03-06 01:09:24 +03:00
?: ?=([%directory-diff *] +.+.d)
2021-03-02 23:05:27 +03:00
;: weld
(format-directory-diff p.d a)
(format-directory-diff q.d b)
acc
==
2021-03-06 01:09:24 +03:00
?: ?=([%other *] +.+.d)
2021-03-02 23:05:27 +03:00
:_ acc
:- %leaf
;: weld
"file "
2021-03-06 01:09:24 +03:00
<a.d>
2021-03-02 23:05:27 +03:00
" has mug "
<p.d>
" on desk "
<a>
" but mug "
<q.d>
" on desk "
<b>
==
!!
--