|
1 | 1 | Include (%occInclude, %occErrors, %occKeyword, %occReference, %occSAX)
|
2 | 2 |
|
3 |
| -Class SourceControl.Git.Util.ProductionConflictResolver Extends %RegisteredObject |
| 3 | +Class SourceControl.Git.Util.ProductionConflictResolver Extends SourceControl.Git.Util.XMLConflictResolver |
4 | 4 | {
|
5 | 5 |
|
6 |
| -Property logStream As %Stream.Object [ Private ]; |
| 6 | +Parameter ExpectedConflictTag = "</Item>"; |
7 | 7 |
|
8 |
| -Property productionFile As %String [ Private ]; |
| 8 | +Parameter OutputIndent = " "; |
9 | 9 |
|
10 |
| -Property productionClassname As %Dictionary.CacheClassname [ Private ]; |
11 |
| - |
12 |
| -Property errorStatus As %Status [ InitialExpression = 1, Private ]; |
13 |
| - |
14 |
| -/// API property: whether or not the conflict was resolved |
15 |
| -Property resolved As %Boolean [ InitialExpression = 0 ]; |
16 |
| - |
17 |
| -/// API property: error message if resolved is false |
18 |
| -Property errorMessage As %String [ Calculated ]; |
19 |
| - |
20 |
| -Method errorMessageGet() As %String |
21 |
| -{ |
22 |
| - If $$$ISERR(..errorStatus) { |
23 |
| - Do $System.Status.DecomposeStatus(..errorStatus,.components) |
24 |
| - If $Get(components(1,"code")) = $$$GeneralError { |
25 |
| - Quit $Get(components(1,"param",1)) |
26 |
| - } Else { |
27 |
| - Set ex = ##class(%Exception.StatusException).CreateFromStatus(..errorStatus) |
28 |
| - Do ex.Log() |
29 |
| - Quit "an internal error occurred and has been logged." |
30 |
| - } |
31 |
| - } Else { |
32 |
| - Quit "" |
33 |
| - } |
34 |
| -} |
35 |
| - |
36 |
| -ClassMethod FromLog(pOutStream As %Stream.Object) As SourceControl.Git.Util.ProductionConflictResolver |
37 |
| -{ |
38 |
| - Set inst = ..%New() |
39 |
| - Try { |
40 |
| - Set inst.logStream = pOutStream |
41 |
| - Do inst.ConsumeStream() |
42 |
| - Do inst.Resolve() |
43 |
| - } Catch e { |
44 |
| - Set inst.resolved = 0 |
45 |
| - Set inst.errorStatus = e.AsStatus() |
46 |
| - } |
47 |
| - Do inst.logStream.Rewind() // Finally |
48 |
| - Quit inst |
49 |
| -} |
50 |
| - |
51 |
| -Method ConsumeStream() [ Private ] |
52 |
| -{ |
53 |
| - Set conflicts = 0 |
54 |
| - Do ..logStream.Rewind() |
55 |
| - Do ..logStream.ReadLine() |
56 |
| - while '..logStream.AtEnd { |
57 |
| - Set conflictLine = ..logStream.ReadLine() |
58 |
| - If $Extract(conflictLine,1,8) = "CONFLICT" { |
59 |
| - Set conflicts($i(conflicts)) = $Piece(conflictLine,"Merge conflict in ",2) |
60 |
| - } |
61 |
| - } |
62 |
| - If (conflicts = 0) { |
63 |
| - $$$ThrowStatus($$$ERROR($$$GeneralError,"Message did not reflect merge conflict on a single file.")) |
64 |
| - } |
65 |
| - If conflicts '= 1 { |
66 |
| - $$$ThrowStatus($$$ERROR($$$GeneralError,"Multiple files had merge conflicts; cannot resolve intelligently.")) |
67 |
| - } |
68 |
| - Set ..productionFile = conflicts(1) |
69 |
| - Set internalName = ##class(SourceControl.Git.Utils).NameToInternalName(..productionFile) |
70 |
| - If ($Piece(internalName,".",*) '= "CLS") { |
71 |
| - $$$ThrowStatus($$$ERROR($$$GeneralError,"File with conflict is not a class.")) |
72 |
| - } |
73 |
| - Set ..productionClassname = $Piece(internalName,".",1,*-1) |
74 |
| - If '($$$comClassDefined(..productionClassname) && $ClassMethod(..productionClassname,"%Extends","Ens.Production")) { |
75 |
| - $$$ThrowStatus($$$ERROR($$$GeneralError,"File with conflict is not an interoperability production.")) |
76 |
| - } |
77 | 10 | }
|
78 | 11 |
|
79 |
| -Method Resolve() [ Private ] |
80 |
| -{ |
81 |
| - Set filePath = ##class(SourceControl.Git.Utils).TempFolder()_..productionFile |
82 |
| - Set file = ##class(%Stream.FileCharacter).%OpenId(filePath,,.sc) |
83 |
| - $$$ThrowOnError(sc) |
84 |
| - |
85 |
| - Do ..ResolveStream(file) // Throws exception on failure |
86 |
| - |
87 |
| - $$$ThrowOnError(##class(SourceControl.Git.Utils).ImportItem(..productionClassname_".CLS",1)) |
88 |
| - $$$ThrowOnError($System.OBJ.Compile(..productionClassname,"ck")) |
89 |
| - |
90 |
| - // TODO: if we add multiple resolvers, move this to the end. |
91 |
| - set code = ##class(SourceControl.Git.Utils).RunGitWithArgs(.errStream, .outStream, "add", ..productionFile) |
92 |
| - if (code '= 0) { |
93 |
| - $$$ThrowStatus($$$ERROR($$$GeneralError,"git add reported failure")) |
94 |
| - } |
95 |
| - set code = ##class(SourceControl.Git.Utils).RunGitWithArgs(.errStream, .outStream, "commit", "--no-edit") |
96 |
| - if (code '= 0) { |
97 |
| - $$$ThrowStatus($$$ERROR($$$GeneralError,"git commit reported failure")) |
98 |
| - } |
99 |
| - |
100 |
| - set code = ##class(SourceControl.Git.Utils).RunGitWithArgs(.errStream, .outStream, "rebase", "--continue") |
101 |
| - if (code '= 0) { |
102 |
| - $$$ThrowStatus($$$ERROR($$$GeneralError,"git rebase --continue reported failure")) |
103 |
| - } |
104 |
| - |
105 |
| - set ..resolved = 1 |
106 |
| -} |
107 |
| - |
108 |
| -/// Non-private to support unit testing |
109 |
| -ClassMethod ResolveStream(stream As %Stream.Object) |
110 |
| -{ |
111 |
| - // File may have: |
112 |
| - /* |
113 |
| - <<<<<<< HEAD |
114 |
| - <Item Name="Demo7" Category="" ClassName="EnsLib.CloudStorage.BusinessOperation" PoolSize="1" Enabled="false" Foreground="false" Comment="" LogTraceEvents="false" Schedule=""> |
115 |
| - ======= |
116 |
| - <Item Name="Demo5" Category="" ClassName="EnsLib.AmazonCloudWatch.MetricAlarmOperation" PoolSize="1" Enabled="false" Foreground="false" Comment="" LogTraceEvents="false" Schedule=""> |
117 |
| - >>>>>>> 607d1f6 (modified src/HCC/Connect/Production.cls add Demo5) |
118 |
| - </Item> |
119 |
| - */ |
120 |
| - |
121 |
| - // If: |
122 |
| - // * We have one such marker (<<<<<<< / ======= / >>>>>>>) |
123 |
| - // * The line after >>>>>> is "</Item>" |
124 |
| - // Then: |
125 |
| - // * We can replace ======= with "</Item>" |
126 |
| - |
127 |
| - Set copy = ##class(%Stream.TmpCharacter).%New() |
128 |
| - Set markerCount = 0 |
129 |
| - Set postCloseMarker = 0 |
130 |
| - While 'stream.AtEnd { |
131 |
| - Set line = stream.ReadLine() |
132 |
| - Set start = $Extract(line,1,7) |
133 |
| - If start = "<<<<<<<" { |
134 |
| - Set markerCount = markerCount + 1 |
135 |
| - Continue |
136 |
| - } ElseIf (start = ">>>>>>>") { |
137 |
| - Set postCloseMarker = 1 |
138 |
| - Continue |
139 |
| - } ElseIf (start = "=======") { |
140 |
| - Do copy.WriteLine(" </Item>") |
141 |
| - Continue |
142 |
| - } ElseIf postCloseMarker { |
143 |
| - If $ZStrip(line,"<>W") '= "</Item>" { |
144 |
| - $$$ThrowStatus($$$ERROR($$$GeneralError,"The type of conflict encountered is not handled; user must resolve manually.")) |
145 |
| - } |
146 |
| - Set postCloseMarker = 0 |
147 |
| - } |
148 |
| - Do copy.WriteLine(line) |
149 |
| - } |
150 |
| - |
151 |
| - If markerCount > 1 { |
152 |
| - $$$ThrowStatus($$$ERROR($$$GeneralError,"Multiple conflicts found, cannot resolve automatically.")) |
153 |
| - } ElseIf markerCount = 0 { |
154 |
| - $$$ThrowStatus($$$ERROR($$$GeneralError,"No conflict markers found in file")) |
155 |
| - } |
156 |
| - |
157 |
| - $$$ThrowOnError(stream.CopyFromAndSave(copy)) |
158 |
| - |
159 |
| - Quit 1 |
160 |
| -} |
161 |
| - |
162 |
| -} |
0 commit comments