mergedvfs.cpp 8.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/***************************************************************************
 *   Copyright Simon Persson                                               *
 *   simonpersson1@gmail.com                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

21
#include "mergedvfs.h"
22
#include "vfshelpers.h"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

#include <QDebug>
#include <KLocale>

#include <git2/branch.h>
#include <sys/stat.h>

typedef QMap<QString, MergedNode *> NameMap;
typedef QMapIterator<QString, MergedNode *> NameMapIterator;

git_repository *MergedNode::mRepository = NULL;

bool mergedNodeLessThan(const MergedNode *a, const MergedNode *b) {
	if(a->isDirectory() != b->isDirectory()) {
		return a->isDirectory();
	}
	return a->objectName() < b->objectName();
}

42 43 44 45
bool versionGreaterThan(const VersionData *a, const VersionData *b) {
	return a->mModifiedDate > b->mModifiedDate;
}

46 47 48 49 50 51 52 53 54

MergedNode::MergedNode(QObject *pParent, const QString &pName, uint pMode)
   :QObject(pParent)
{
	mSubNodes = NULL;
	setObjectName(pName);
	mMode = pMode;
}

55 56
void MergedNode::getBupUrl(int pVersionIndex, KUrl *pComplete, QString *pRepoPath,
                           QString *pBranchName, quint64 *pCommitTime, QString *pPathInRepo) const {
57 58 59 60 61 62 63
	QList<const MergedNode *> lStack;
	const MergedNode *lNode = this;
	while(lNode != NULL) {
		lStack.append(lNode);
		lNode = qobject_cast<const MergedNode *>(lNode->parent());
	}
	const MergedRepository *lRepo = qobject_cast<const MergedRepository *>(lStack.takeLast());
64 65 66 67
	if(pComplete) {
		pComplete->setProtocol(QLatin1String("bup"));
		pComplete->addPath(lRepo->objectName());
		pComplete->addPath(lRepo->mBranchName);
68
		pComplete->addPath(vfsTimeToString(mVersionList.at(pVersionIndex)->mCommitTime));
69 70 71 72 73 74 75 76
	}
	if(pRepoPath) {
		*pRepoPath = lRepo->objectName();
	}
	if(pBranchName) {
		*pBranchName = lRepo->mBranchName;
	}
	if(pCommitTime) {
77
		*pCommitTime = mVersionList.at(pVersionIndex)->mCommitTime;
78 79 80 81
	}
	if(pPathInRepo) {
		pPathInRepo->clear();
	}
82
	while(!lStack.isEmpty()) {
83 84 85 86 87 88 89 90
		QString lPathComponent = lStack.takeLast()->objectName();
		if(pComplete) {
			pComplete->addPath(lPathComponent);
		}
		if(pPathInRepo) {
			pPathInRepo->append(QLatin1Char('/'));
			pPathInRepo->append(lPathComponent);
		}
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
	}
}

MergedNodeList &MergedNode::subNodes() {
	if(mSubNodes == NULL) {
		mSubNodes = new MergedNodeList();
		if(S_ISDIR(mMode)) {
			generateSubNodes();
		}
	}
	return *mSubNodes;
}

void MergedNode::generateSubNodes() {
	NameMap lSubNodeMap;
106
	foreach(VersionData *lCurrentVersion, mVersionList) {
107
		git_tree *lTree;
108
		if(0 != git_tree_lookup(&lTree, mRepository, &lCurrentVersion->mOid)) {
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
			continue;
		}
		git_blob *lMetadataBlob = NULL;
		VintStream *lMetadataStream = NULL;
		const git_tree_entry *lTreeEntry = git_tree_entry_byname(lTree, ".bupm");
		if(lTreeEntry != NULL && 0 == git_blob_lookup(&lMetadataBlob, mRepository, git_tree_entry_id(lTreeEntry))) {
			lMetadataStream = new VintStream(git_blob_rawcontent(lMetadataBlob), git_blob_rawsize(lMetadataBlob), this);
			Metadata lMetadata;
			readMetadata(*lMetadataStream, lMetadata); // the first entry is metadata for the directory itself, discard it.
		}

		uint lEntryCount = git_tree_entrycount(lTree);
		for(uint i = 0; i < lEntryCount; ++i) {
			uint lMode;
			const git_oid *lOid;
			QString lName;
			bool lChunked;
			const git_tree_entry *lTreeEntry = git_tree_entry_byindex(lTree, i);
			getEntryAttributes(lTreeEntry, lMode, lChunked, lOid, lName);
			if(lName == QLatin1String(".bupm")) {
				continue;
			}

			MergedNode *lSubNode = lSubNodeMap.value(lName, NULL);
			if(lSubNode == NULL) {
				lSubNode = new MergedNode(this, lName, lMode);
				lSubNodeMap.insert(lName, lSubNode);
				mSubNodes->append(lSubNode);
			} else if((S_IFMT & lMode) != (S_IFMT & lSubNode->mMode)) {
				if(S_ISDIR(lMode)) {
139
					lName.append(i18nc("added after folder name in some cases", " (folder)"));
140
				} else if(S_ISLNK(lMode)) {
141
					lName.append(i18nc("added after file name in some cases", " (symlink)"));
142
				} else {
143
					lName.append(i18nc("added after file name in some cases", " (file)"));
144 145 146 147 148 149 150 151
				}
				lSubNode = lSubNodeMap.value(lName, NULL);
				if(lSubNode == NULL) {
					lSubNode = new MergedNode(this, lName, lMode);
					lSubNodeMap.insert(lName, lSubNode);
					mSubNodes->append(lSubNode);
				}
			}
152 153 154 155 156 157 158 159 160 161 162 163 164
			bool lAlreadySeen = false;
			foreach(VersionData *lVersion, lSubNode->mVersionList) {
				if(lVersion->mOid == *lOid) {
					lAlreadySeen = true;
					break;
				}
			}
			if(S_ISDIR(lMode)) {
				if(!lAlreadySeen) {
					lSubNode->mVersionList.append(new VersionData(lOid, lCurrentVersion->mCommitTime,
					                                              lCurrentVersion->mModifiedDate, 0));
				}
			} else {
165
				quint64 lModifiedDate;
166 167 168
				Metadata lMetadata;
				if(lMetadataStream != NULL && 0 == readMetadata(*lMetadataStream, lMetadata)) {
					lModifiedDate = lMetadata.mMtime;
169
				} else {
170 171 172 173 174 175
					lModifiedDate = lCurrentVersion->mModifiedDate;
				}
				if(!lAlreadySeen) {
					lSubNode->mVersionList.append(new VersionData(lChunked, lOid,
					                                              lCurrentVersion->mCommitTime,
					                                              lModifiedDate));
176 177 178 179 180 181 182 183 184 185
				}
			}
		}
		if(lMetadataStream != NULL) {
			delete lMetadataStream;
			git_blob_free(lMetadataBlob);
		}
		git_tree_free(lTree);
	}
	qSort(mSubNodes->begin(), mSubNodes->end(), mergedNodeLessThan);
186
	foreach(MergedNode *lNode, *mSubNodes) {
187
		qSort(lNode->mVersionList.begin(), lNode->mVersionList.end(), versionGreaterThan);
188
	}
189 190 191 192 193 194 195 196
}

MergedRepository::MergedRepository(QObject *pParent, const QString &pRepositoryPath, const QString &pBranchName)
   : MergedNode(pParent, pRepositoryPath, DEFAULT_MODE_DIRECTORY), mBranchName(pBranchName)
{
	if(!objectName().endsWith(QLatin1Char('/'))) {
		setObjectName(objectName() + QLatin1Char('/'));
	}
197 198 199 200 201 202 203 204 205 206 207
}

MergedRepository::~MergedRepository() {
	if(mRepository != NULL) {
		git_repository_free(mRepository);
	}
}

bool MergedRepository::open() {
	if(0 != git_repository_open(&mRepository, objectName().toLocal8Bit())) {
		qWarning() << "could not open repository " << objectName();
208
		mRepository = NULL;
209
		return false;
210
	}
211 212 213 214 215 216 217 218 219 220 221
	return true;
}

bool MergedRepository::readBranch() {
	if(mRepository == NULL) {
		return false;
	}
	git_revwalk *lRevisionWalker;
	if(0 != git_revwalk_new(&lRevisionWalker, mRepository)) {
		qWarning() << "could not create a revision walker in repository " << objectName();
		return false;
222 223 224 225
	}

	QString lCompleteBranchName = QString::fromLatin1("refs/heads/");
	lCompleteBranchName.append(mBranchName);
226 227 228 229
	if(0 != git_revwalk_push_ref(lRevisionWalker, lCompleteBranchName.toLocal8Bit())) {
		qWarning() << "Unable to read branch \"" << mBranchName << "\" in repository " << objectName();
		git_revwalk_free(lRevisionWalker);
		return false;
230
	}
231
	bool lEmptyList = true;
232
	git_oid lOid;
233
	while(0 == git_revwalk_next(&lOid, lRevisionWalker)) {
234 235 236 237 238
		git_commit *lCommit;
		if(0 != git_commit_lookup(&lCommit, mRepository, &lOid)) {
			continue;
		}
		git_time_t lTime = git_commit_time(lCommit);
239
		mVersionList.append(new VersionData(git_commit_tree_id(lCommit), lTime, lTime, 0));
240
		lEmptyList = false;
241 242
		git_commit_free(lCommit);
	}
243 244
	git_revwalk_free(lRevisionWalker);
	return !lEmptyList;
245 246 247 248 249 250 251 252 253 254 255 256
}

uint qHash(git_oid pOid) {
	return qHash(QByteArray::fromRawData((char *)pOid.id, GIT_OID_RAWSZ));
}


bool operator ==(const git_oid &pOidA, const git_oid &pOidB) {
	QByteArray a = QByteArray::fromRawData((char *)pOidA.id, GIT_OID_RAWSZ);
	QByteArray b = QByteArray::fromRawData((char *)pOidB.id, GIT_OID_RAWSZ);
	return a == b;
}
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276


quint64 VersionData::size() {
	if(mSizeIsValid) {
		return mSize;
	}
	if(mChunkedFile) {
		mSize = calculateChunkFileSize(&mOid, MergedNode::mRepository);
	} else {
		git_blob *lBlob;
		if(0 == git_blob_lookup(&lBlob, MergedNode::mRepository, &mOid)) {
			mSize = git_blob_rawsize(lBlob);
			git_blob_free(lBlob);
		} else {
			mSize = 0;
		}
	}
	mSizeIsValid = true;
	return mSize;
}