Fix handling of extra_query_params in Business::OnlinePayment wrapper.
[interchange.git] / code / UI_Tag / diffmerge.coretag
1 # Copyright 2002-2007 Interchange Development Group and others
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.  See the LICENSE file for details.
7
8 # $Id: diffmerge.coretag,v 1.4 2007-03-30 23:40:54 pajamian Exp $
9
10 # This tag uses GNU diff3 to merge two texts blocks that were
11 # modified from the same ancestral text together, and marks
12 # conflicts that may appear. This is similar to CVS's merging
13 # and conflict marking. The names the diff3 manpage uses are:
14 #
15 #        older
16 #         / \
17 #        /   \
18 #       /     \
19 #    mine    yours
20 #
21 # You supply pointers to three text blocks, either as file names or
22 # database fields in the form Table::Column::Key. 'mine' can instead
23 # be provided in the body, between the opening and closing tags.
24 #
25 # The tag returns the merged text. You can find out whether a
26 # conflict was detected by providing the name of a scratch variable
27 # in the 'result' option where the return code from diff3 will be placed.
28 #
29 # Set the 'ascii' option to allow for different newline types and
30 # ignore whether the last line of the file has a newline.
31 #
32 # Set the 'safe_data' option to allow raw data to be pulled from the
33 # database without escaping left brackets (turning [ into [).
34 #
35 # Examples:
36 #
37 # [diffmerge /tmp/abcd2 /tmp/abcd1 /tmp/abcd3]
38 #
39 # [diffmerge
40 #     yours="content::pagebody::00001"
41 #     older="backup::pagebody::00001"
42 #     ascii=1
43 #     result=diff_result
44 #     safe_data=1
45 # ][scratch new_pagebody][/diffmerge]
46
47 UserTag diffmerge Interpolate   1
48 UserTag diffmerge hasEndTag
49 UserTag diffmerge addAttr
50 UserTag diffmerge Version       $Revision: 1.4 $
51
52 # These designations come from the diff3 manpage.
53 # It seemed easier to use their names than to make up new ones.
54 UserTag diffmerge Order yours older mine
55
56 # But here I try to make up new ones anyway. :)
57 UserTag diffmerge attrAlias     <<EOA
58         current         mine
59         curr            mine
60         previous        yours
61         prev            yours
62         old                     older
63 EOA
64
65 UserTag diffmerge Routine       <<EOR
66 sub {
67     my ($yours, $older, $mine, $opt, $body) = @_;
68
69     unless ($opt->{flags} =~ /^[-\s\w.]*$/) {
70         Log("diffmerge tag: Security violation with flags: $opt->{flags}");
71         return "Security violation with flags: $opt->{flags}. Logged.";
72     }
73
74         my ($minefn, $yoursfn, $olderfn, $cmd, $merge);
75         my $tmpbasename = "tmp/$Vend::SessionName";
76
77         my $data_opt = {};
78         $data_opt->{safe_data} = 1 if $opt->{safe_data};
79
80         my $asciifix = sub {
81                 local $_ = shift;
82                 if ($opt->{ascii}) {
83                         s/\r\n?/\n/g;
84                         $_ .= "\n" unless substr($_, -1, 1) eq "\n";
85                 }
86                 return $_;
87         };
88
89         my $putfile = sub {
90                 my ($name, $passed, $fn) = @_;
91             if ($$passed =~ /^(\w+)::(.*?)::(.*)/) {
92                 my ($table, $col, $key) = ($1, $2, $3);
93                         my $data = $asciifix->( tag_data($table, $col, $key, $data_opt) );
94                 $$fn = "$tmpbasename.$name";
95                 Vend::Util::writefile(">$$fn", $data);
96             }
97             else {
98                 $$fn = $$passed;
99             }
100         };
101
102         if ($body) {
103                 $body = $asciifix->($body);
104                 $minefn = "tmp/$Vend::SessionName.mine";
105                 Vend::Util::writefile(">$minefn", $body);
106         }
107         elsif ($mine) {
108                 $putfile->('mine', \$mine, \$minefn);
109         }
110
111         $putfile->('yours', \$yours, \$yoursfn);
112         $putfile->('older', \$older, \$olderfn);
113
114     $cmd = "diff3 -m $opt->{flags} $minefn $olderfn $yoursfn";
115 #Debug("diffmerge command: '$cmd'");
116     $merge = `$cmd`;
117
118         if (defined $opt->{result}) {
119                 unless ($opt->{result} =~ /\W/) {
120                         $Scratch->{$opt->{result}} = $? >> 8;
121 #Debug("diffmerge put $Scratch->{$opt->{result}} into scratch $opt->{result}");
122                 }
123                 else {
124                         Log("diffmerge tag: Invalid 'result' option given; must be a valid name for a scratch variable");
125                 }
126         }
127
128         return $merge;
129 }
130 EOR