コード例 #1
0
ファイル: Differ.cs プロジェクト: lysannschlegel/sharpdiff
        internal static Diff NewFileDiff(string content, string path)
        {
            var fileOne = new NullFile();
            var fileTwo = new File('b', path);
            var header = new DiffHeader(new DiffFormatType("generated"), new IFile[] { fileOne, fileTwo });

            var lines = content.SplitIntoLines()
                .Select(x => (ILine)new AdditionLine(x));
            var range = new ChunkRange(new ChangeRange(0, 0), new ChangeRange(1, lines.Count()));
            var snippet = new AdditionSnippet(lines);
            var chunk = new Chunk(range, new[] { snippet });

            return new Diff(header, new IHeader[0], new ChunksHeader(fileOne, fileTwo), new[] { chunk });
        }
コード例 #2
0
            public IList<IFile> GetDiffFilesFromBinaryFiles(string binaryFilesRawFileDefs)
            {
                List<IFile> files = new List<IFile>(2);

                string rawFileDefs = binaryFilesRawFileDefs;

                // rawFileDefs has the following format:
                //   '<file def> and <file def> differ'
                // See IsValidFileDef for valid values of <file def>.

                // 1. remove ' differ' from the end
                int index = rawFileDefs.IndexOf(" differ");
                if(index != -1) {
                    rawFileDefs = rawFileDefs.Substring(0, index);
                } else {
                    System.Diagnostics.Trace.TraceWarning("Missing string ' differ' at end of BinaryFiles raw line: '{0}'", binaryFilesRawFileDefs);
                }

                // 2. repeatedly split at ' and ' positions, and find the ones where we have valid file names
                var candidates = new List<KeyValuePair<string, string>>(1);
                index = 0;
                while(true) {
                    index = rawFileDefs.IndexOf(" and ", index);
                    if(index == -1) break;
                    string filename1 = rawFileDefs.Substring(0, index);
                    string filename2 = rawFileDefs.Substring(index + 5 /* " and ".Length */);
                    if(AreValidFileDefs(filename1, filename2)) {
                        candidates.Add(new KeyValuePair<string, string>(filename1, filename2));
                    }
                }

                // 3. find the right candidate
                if(candidates.Count == 0) {
                    System.Diagnostics.Trace.TraceWarning("Missing file names in BinaryFiles raw line '{0}'", binaryFilesRawFileDefs);
                } else if(candidates.Count == 1) {
                    files[0] = FileFromFileDef(candidates[0].Key);
                    files[1] = FileFromFileDef(candidates[0].Value);
                } else {
                    // We got at least one file name with ' and b/' or ' and /dev/null' in its path ...
                    // We can consult the diff header's raw string, which looks like this:
                    //   '<file def> <file def>'
                    // Try to find a matching pair of candidates.
                    foreach(var candidate in candidates) {
                        if(this.headerSplitCandidates.Any(headerCandidate => CandidatesMatch(candidate.Key, headerCandidate.Key, candidate.Value, headerCandidate.Value))) {
                            files[0] = FileFromFileDef(candidate.Key);
                            files[1] = FileFromFileDef(candidate.Value);
                            break;
                        }
                    }
                    if(files.Count < 2) {
                        System.Diagnostics.Trace.TraceWarning("Could not find any pair of file names in BinaryFiles header that match the diff header file names.");
                    }
                }

                // 4. check if we found anything, and choose a default if not
                if(files.Count < 2) {
                    if(candidates.Count > 0) {
                        System.Diagnostics.Trace.TraceWarning("Defaulting to first candidate.");
                        files[0] = FileFromFileDef(candidates[0].Key);
                        files[1] = FileFromFileDef(candidates[0].Value);
                    } else {
                        System.Diagnostics.Trace.TraceWarning("Defaulting to NullFiles.");
                        files[0] = new NullFile();
                        files[1] = new NullFile();
                    }
                }
                return files;
            }