1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
35 #include <QtCore/QRegExp>
37 FileDataList splitDiffToFiles(const QByteArray &data)
40 QString strData = data;
41 QString splitExpression;
43 if (data.contains("==== ") && data.contains(" ====\n")) {
45 splitExpression = "==== ([^\\n\\r]+) - ([^\\n\\r]+) ====";
47 } else if (data.contains("--- ") && data.contains("\n+++ ")) {
48 // Unified contextual diff
49 splitExpression = "\\-\\-\\- ([^\\n\\r]*)"
50 "\\n\\+\\+\\+ ([^\\n\\r]*)";
52 } else if (data.contains("*** ") && data.contains("\n--- ")) {
53 // Copied contextual diff
54 splitExpression = "\\*\\*\\* ([^\\n\\r]*) [0-9\\-]* [0-9:\\.]*[^\\n\\r]*"
55 "\\n\\-\\-\\- ([^\\n\\r]*) [0-9\\-]* [0-9:\\.]*[^\\n\\r]*";
58 return FileDataList();
61 int splitIndex = 0, previousSplit = -1;
62 QRegExp splitExpr(splitExpression);
63 QString filename, content;
64 // The algorithm works like this:
65 // On the first match we only get the filename of the first patch part
66 // On the second match (if any) we get the diff content, and the name of the next file patch
68 while (-1 != (splitIndex = splitExpr.indexIn(strData,splitIndex))) {
69 if (!filename.isEmpty()) {
70 QString content = strData.mid(previousSplit, splitIndex - previousSplit);
71 ret.append(FileData(filename, content.toLatin1()));
74 // If the first index in not at the beginning of the file, then we know there's content
75 // we're about to skip, which is common in commit diffs, so we get that content and give it
77 if (previousSplit == -1 && splitIndex > 0 && filename.isEmpty()) {
78 QString content = strData.left(splitIndex);
79 ret.append(FileData("<Header information>", content.toLatin1()));
82 filename = splitExpr.cap(1);
83 previousSplit = splitIndex;
86 // Append the last patch content
87 if (!filename.isEmpty()) {
88 QString content = strData.mid(previousSplit);
89 ret.append(FileData(filename, content.toLatin1()));