25
25
*/
26
26
final class PhabricatorDifferenceEngine {
27
27
28
+
28
29
private $ ignoreWhitespace ;
30
+ private $ oldName ;
31
+ private $ newName ;
29
32
30
33
31
34
/* -( Configuring the Engine )--------------------------------------------- */
@@ -44,20 +47,46 @@ public function setIgnoreWhitespace($ignore_whitespace) {
44
47
}
45
48
46
49
50
+ /**
51
+ * Set the name to identify the old file with. Primarily cosmetic.
52
+ *
53
+ * @param string Old file name.
54
+ * @return this
55
+ * @task config
56
+ */
57
+ public function setOldName ($ old_name ) {
58
+ $ this ->oldName = $ old_name ;
59
+ return $ this ;
60
+ }
61
+
62
+
63
+ /**
64
+ * Set the name to identify the new file with. Primarily cosmetic.
65
+ *
66
+ * @param string New file name.
67
+ * @return this
68
+ * @task config
69
+ */
70
+ public function setNewName ($ new_name ) {
71
+ $ this ->newName = $ new_name ;
72
+ return $ this ;
73
+ }
74
+
75
+
47
76
/* -( Generating Diffs )--------------------------------------------------- */
48
77
49
78
50
79
/**
51
- * Generate an @{class:DifferentialChangeset} from two raw files. This is
52
- * principally useful because you can feed the output to
53
- * @{class:DifferentialChangesetParser} in order to render it .
80
+ * Generate a raw diff from two raw files. This is a lower-level API than
81
+ * @{method:generateChangesetFromFileContent}, but may be useful if you need
82
+ * to use a custom parser configuration, as with Diffusion .
54
83
*
55
84
* @param string Entire previous file content.
56
85
* @param string Entire current file content.
57
- * @return @{class:DifferentialChangeset} Synthetic changeset .
86
+ * @return string Raw diff between the two files .
58
87
* @task diff
59
88
*/
60
- public function generateChangesetFromFileContent ($ old , $ new ) {
89
+ public function generateRawDiffFromFileContent ($ old , $ new ) {
61
90
62
91
$ options = array ();
63
92
if ($ this ->ignoreWhitespace ) {
@@ -67,6 +96,14 @@ public function generateChangesetFromFileContent($old, $new) {
67
96
// Generate diffs with full context.
68
97
$ options [] = '-U65535 ' ;
69
98
99
+ $ old_name = nonempty ($ this ->oldName , '/dev/universe ' ).' 9999-99-99 ' ;
100
+ $ new_name = nonempty ($ this ->newName , '/dev/universe ' ).' 9999-99-99 ' ;
101
+
102
+ $ options [] = '-L ' ;
103
+ $ options [] = $ old_name ;
104
+ $ options [] = '-L ' ;
105
+ $ options [] = $ new_name ;
106
+
70
107
$ old_tmp = new TempFile ();
71
108
$ new_tmp = new TempFile ();
72
109
@@ -78,13 +115,14 @@ public function generateChangesetFromFileContent($old, $new) {
78
115
$ old_tmp ,
79
116
$ new_tmp );
80
117
81
- if (!strlen ( $ diff ) ) {
118
+ if (!$ err ) {
82
119
// This indicates that the two files are the same (or, possibly, the
83
120
// same modulo whitespace differences, which is why we can't do this
84
121
// check trivially before running `diff`). Build a synthetic, changeless
85
122
// diff so that we can still render the raw, unchanged file instead of
86
123
// being forced to just say "this file didn't change" since we don't have
87
124
// the content.
125
+
88
126
$ entire_file = explode ("\n" , $ old );
89
127
foreach ($ entire_file as $ k => $ line ) {
90
128
$ entire_file [$ k ] = ' ' .$ line ;
@@ -93,12 +131,29 @@ public function generateChangesetFromFileContent($old, $new) {
93
131
$ entire_file = implode ("\n" , $ entire_file );
94
132
95
133
// This is a bit hacky but the diff parser can handle it.
96
- $ diff = "--- ignored 9999-99-99 \n" .
97
- "+++ ignored 9999-99-99 \n" .
134
+ $ diff = "--- { $ old_name } \n" .
135
+ "+++ { $ new_name } \n" .
98
136
"@@ -1, {$ len } +1, {$ len } @@ \n" .
99
137
$ entire_file ."\n" ;
100
138
}
101
139
140
+ return $ diff ;
141
+ }
142
+
143
+
144
+ /**
145
+ * Generate an @{class:DifferentialChangeset} from two raw files. This is
146
+ * principally useful because you can feed the output to
147
+ * @{class:DifferentialChangesetParser} in order to render it.
148
+ *
149
+ * @param string Entire previous file content.
150
+ * @param string Entire current file content.
151
+ * @return @{class:DifferentialChangeset} Synthetic changeset.
152
+ * @task diff
153
+ */
154
+ public function generateChangesetFromFileContent ($ old , $ new ) {
155
+ $ diff = $ this ->generateRawDiffFromFileContent ($ old , $ new );
156
+
102
157
$ changes = id (new ArcanistDiffParser ())->parseDiff ($ diff );
103
158
$ diff = DifferentialDiff::newFromRawChanges ($ changes );
104
159
return head ($ diff ->getChangesets ());
0 commit comments