Skip to content

Commit

Permalink
ADDED: lib/patch.pl and tests for merging conflicting edits
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Wielemaker committed Nov 3, 2016
1 parent ae3a3a6 commit 2b14740
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
118 changes: 118 additions & 0 deletions lib/patch.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/* Part of SWISH
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2014-2016, VU University Amsterdam
CWI Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

:- module(patch,
[ patch/4 % +Data, +Diff, -Merged, +Options
]).
:- use_module(library(process)).
:- use_module(library(option)).

/** <module> Run patch program
This library uses the GNU patch(1) program to merge changes.
*/

:- predicate_options(patch/4, 4,
[ status(-compound),
stderr(-string)
]).

%% patch(+Orig:string, +Diff:string, -Merged:string, +Options)
%
% Patch the string Orig using Diff. Options:
%
% - status(-Status)
% Unify Status with the completion status of patch. This
% is exit(0) for smooth completion and exit(1) if there are
% merge conflicts.
% - stderr(-String)
% Unify String with the data patch(1) sent to standard error.

patch(Orig, Diff, Merged, Options) :-
setup_call_cleanup(
tmp_file_stream(utf8, TmpFile, Out),
( call_cleanup(format(Out, '~s', [Orig]),
close(Out)),
run_patch(TmpFile, Diff, Merged, Options)
),
delete_file(TmpFile)).

run_patch(File, Diff, Merged, Options) :-
thread_self(Me),
setup_call_cleanup(
process_create(path(patch),
[ '--force', '--merge', '--silent',
'--output=-', file(File) ],
[ stdin(pipe(In)),
stdout(pipe(Out)),
stderr(pipe(Err)),
process(PID)
]),
( set_stream(In, encoding(utf8)),
set_stream(Out, encoding(utf8)),
set_stream(Err, encoding(utf8)),
thread_create(copy_diff(Diff, In), _, [detached(true)]),
thread_create(read_stderr(Me, Err), ErrThread, []),
read_string(Out, _, Merged)
),
( close(Out),
process_wait(PID, Status)
)),
get_errors(ErrThread, Options),
( option(status(VarStat), Options)
-> VarStat = Status
; true
).

copy_diff(Diff, To) :-
call_cleanup(
format(To, '~s', [Diff]),
close(To)).

read_stderr(Master, Stderr) :-
read_string(Stderr, _, Errors),
thread_send_message(Master, patch_errors(Errors)).

get_errors(Thread, Options) :-
thread_join(Thread, Status),
( Status == true
-> thread_get_message(patch_errors(Errors))
; Status == exception(Error)
-> throw(Error)
),
( option(stderr(Var), Options)
-> Var = Errors
; true
).
5 changes: 5 additions & 0 deletions test/diff/f0.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Line 10
Line 20
Line 30
Line 40
Line 50
6 changes: 6 additions & 0 deletions test/diff/f1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Line 10
Line 20
Line 30
Line 35
Line 40
Line 50
6 changes: 6 additions & 0 deletions test/diff/f2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Line 10
Line 20
Line 30
Line 37
Line 40
Line 50
72 changes: 72 additions & 0 deletions test/test_patch.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Part of SWISH
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2014-2016, VU University Amsterdam
CWI Amsterdam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

:- module(test_patch, [ test_patch/0 ]).
:- use_module(library(plunit)).
:- use_module(library(process)).
:- use_module(library(aggregate)).
:- use_module(library(debug)).
:- use_module(library(readutil)).
:- use_module('../lib/patch').

test_patch :-
run_tests([patch]).

:- begin_tests(patch).

test(simple, Merged+Status == Final+exit(0)) :-
run(diff('-u', file('diff/f0.txt'), file('diff/f1.txt')), Diff, _),
read_file_to_string('diff/f0.txt', Orig, []),
read_file_to_string('diff/f1.txt', Final, []),
patch(Orig, Diff, Merged, [status(Status)]).
test(conflict, Status+Failures == exit(1)+1) :-
run(diff('-u', file('diff/f0.txt'), file('diff/f1.txt')), Diff, _),
read_file_to_string('diff/f2.txt', Orig, []),
patch(Orig, Diff, Merged, [status(Status), stderr(Errors)]),
aggregate_all(count, sub_string(Errors, _,_,_, 'Hunk #'), Failures),
assertion(sub_string(Merged, _, _, _, "<<<<<")),
assertion(sub_string(Merged, _, _, _, ">>>>>")),
debug(patch,
'Diff:~n~s~nMerged:~n~s~nStatus: ~p', [Diff, Merged, Status]).

:- end_tests(patch).

run(Command, Output, Status) :-
Command =.. [Exe|Argv],
setup_call_cleanup(
process_create(path(Exe), Argv, [stdout(pipe(Out)), process(Pid)]),
read_string(Out, _, Output),
close(Out)),
process_wait(Pid, Status).

0 comments on commit 2b14740

Please sign in to comment.