Skip to content

Commit e7548a0

Browse files
Vampiretnyblom
authored andcommitted
Maintain branch notes throughout incremental runs
1 parent e51772b commit e7548a0

File tree

3 files changed

+70
-13
lines changed

3 files changed

+70
-13
lines changed

src/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ int main(int argc, char **argv)
211211
repositories.insert(rule.name, repo);
212212

213213
int repo_next = repo->setupIncremental(cutoff);
214+
repo->restoreBranchNotes();
214215

215216
/*
216217
* cutoff < resume_from => error exit eventually
@@ -282,6 +283,7 @@ int main(int argc, char **argv)
282283

283284
foreach (Repository *repo, repositories) {
284285
repo->finalizeTags();
286+
repo->saveBranchNotes();
285287
delete repo;
286288
}
287289
Stats::instance()->printStats();

src/repository.cpp

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ class FastImportRepository : public Repository
6464
void deleteFile(const QString &path);
6565
QIODevice *addFile(const QString &path, int mode, qint64 length);
6666

67-
void commitNote(const QByteArray &noteText, bool append,
67+
bool commitNote(const QByteArray &noteText, bool append,
6868
const QByteArray &commit = QByteArray());
6969
};
7070
FastImportRepository(const Rules::Repository &rule);
7171
int setupIncremental(int &cutoff);
72+
void restoreBranchNotes();
7273
void restoreLog();
7374
~FastImportRepository();
7475

@@ -82,6 +83,7 @@ class FastImportRepository : public Repository
8283
const QByteArray &author, uint dt,
8384
const QByteArray &log);
8485
void finalizeTags();
86+
void saveBranchNotes();
8587
void commit();
8688

8789
bool branchExists(const QString& branch) const;
@@ -98,7 +100,6 @@ class FastImportRepository : public Repository
98100
int created;
99101
QVector<int> commits;
100102
QVector<int> marks;
101-
QByteArray note;
102103
};
103104
struct AnnotatedTag
104105
{
@@ -111,6 +112,7 @@ class FastImportRepository : public Repository
111112
};
112113

113114
QHash<QString, Branch> branches;
115+
QHash<QString, QByteArray> branchNotes;
114116
QHash<QString, AnnotatedTag> annotatedTags;
115117
QString name;
116118
QString prefix;
@@ -175,14 +177,15 @@ class ForwardingRepository : public Repository
175177
QIODevice *addFile(const QString &path, int mode, qint64 length)
176178
{ return txn->addFile(prefix + path, mode, length); }
177179

178-
void commitNote(const QByteArray &noteText, bool append,
180+
bool commitNote(const QByteArray &noteText, bool append,
179181
const QByteArray &commit)
180182
{ return txn->commitNote(noteText, append, commit); }
181183
};
182184

183185
ForwardingRepository(const QString &n, Repository *r, const QString &p) : name(n), repo(r), prefix(p) {}
184186

185187
int setupIncremental(int &) { return 1; }
188+
void restoreBranchNotes() {}
186189
void restoreLog() {}
187190

188191
void reloadBranches() { return repo->reloadBranches(); }
@@ -204,6 +207,7 @@ class ForwardingRepository : public Repository
204207
const QByteArray &log)
205208
{ repo->createAnnotatedTag(name, svnprefix, revnum, author, dt, log); }
206209
void finalizeTags() { /* loop that called this will invoke it on 'repo' too */ }
210+
void saveBranchNotes() { /* loop that called this will invoke it on 'repo' too */ }
207211
void commit() { repo->commit(); }
208212

209213
bool branchExists(const QString& branch) const
@@ -267,6 +271,13 @@ static QString marksFileName(QString name)
267271
return name;
268272
}
269273

274+
static QString branchNotesFileName(QString name)
275+
{
276+
name.replace('/', '_');
277+
name.prepend("branchNotes-");
278+
return name;
279+
}
280+
270281
FastImportRepository::FastImportRepository(const Rules::Repository &rule)
271282
: name(rule.name), prefix(rule.forwardTo), fastImport(name), commitCount(0), outstandingTransactions(0),
272283
last_commit_mark(0), next_file_mark(maxMark - 1), processHasStarted(false)
@@ -451,6 +462,17 @@ int FastImportRepository::setupIncremental(int &cutoff)
451462
return cutoff;
452463
}
453464

465+
void FastImportRepository::restoreBranchNotes()
466+
{
467+
QFile branchNotesFile(name + "/" + branchNotesFileName(name));
468+
if (!branchNotesFile.exists())
469+
return;
470+
branchNotesFile.open(QIODevice::ReadOnly);
471+
QDataStream branchNotesStream(&branchNotesFile);
472+
branchNotesStream >> branchNotes;
473+
branchNotesFile.close();
474+
}
475+
454476
void FastImportRepository::restoreLog()
455477
{
456478
QString file = logFileName(name);
@@ -578,7 +600,7 @@ int FastImportRepository::createBranch(const QString &branch, int revnum,
578600
qDebug() << "Creating branch:" << branch << "from" << branchFrom << "(" << branchRevNum << branchFromDesc << ")";
579601

580602
// Preserve note
581-
branches[branch].note = branches.value(branchFrom).note;
603+
branchNotes[branch] = branchNotes.value(branchFrom);
582604

583605
return resetBranch(branch, revnum, mark, branchFromRef, branchFromDesc);
584606
}
@@ -748,10 +770,10 @@ void FastImportRepository::finalizeTags()
748770
Repository::Transaction *txn = newTransaction(tag.supportingRef, tag.svnprefix, tag.revnum);
749771
txn->setAuthor(tag.author);
750772
txn->setDateTime(tag.dt);
751-
txn->commitNote(formatMetadataMessage(tag.svnprefix, tag.revnum, tagName.toUtf8()), true);
773+
bool written = txn->commitNote(formatMetadataMessage(tag.svnprefix, tag.revnum, tagName.toUtf8()), true);
752774
delete txn;
753775

754-
if (!fastImport.waitForBytesWritten(-1))
776+
if (written && !fastImport.waitForBytesWritten(-1))
755777
qFatal("Failed to write to process: %s", qPrintable(fastImport.errorString()));
756778
}
757779

@@ -765,6 +787,17 @@ void FastImportRepository::finalizeTags()
765787
printf("\n");
766788
}
767789

790+
void FastImportRepository::saveBranchNotes()
791+
{
792+
if (branchNotes.isEmpty())
793+
return;
794+
795+
QFile branchNotesFile(name + "/" + branchNotesFileName(name));
796+
branchNotesFile.open(QIODevice::WriteOnly);
797+
QDataStream branchNotesStream(&branchNotesFile);
798+
branchNotesStream << branchNotes;
799+
branchNotesFile.close();
800+
}
768801

769802
QByteArray
770803
FastImportRepository::msgFilter(QByteArray msg)
@@ -834,13 +867,13 @@ bool FastImportRepository::branchExists(const QString& branch) const
834867

835868
const QByteArray FastImportRepository::branchNote(const QString& branch) const
836869
{
837-
return branches.value(branch).note;
870+
return branchNotes.value(branch);
838871
}
839872

840873
void FastImportRepository::setBranchNote(const QString& branch, const QByteArray& noteText)
841874
{
842875
if (branches.contains(branch))
843-
branches[branch].note = noteText;
876+
branchNotes[branch] = noteText;
844877
}
845878

846879
bool FastImportRepository::hasPrefix() const
@@ -944,20 +977,37 @@ QIODevice *FastImportRepository::Transaction::addFile(const QString &path, int m
944977
return &repository->fastImport;
945978
}
946979

947-
void FastImportRepository::Transaction::commitNote(const QByteArray &noteText, bool append, const QByteArray &commit)
980+
bool FastImportRepository::Transaction::commitNote(const QByteArray &noteText, bool append, const QByteArray &commit)
948981
{
949982
QByteArray branchRef = branch;
950983
if (!branchRef.startsWith("refs/"))
984+
{
951985
branchRef.prepend("refs/heads/");
986+
}
952987
const QByteArray &commitRef = commit.isNull() ? branchRef : commit;
953988
QByteArray message = "Adding Git note for current " + commitRef + "\n";
954989
QByteArray text = noteText;
990+
if (noteText[noteText.size() - 1] != '\n')
991+
{
992+
text += '\n';
993+
}
955994

995+
QByteArray branchNote = repository->branchNote(branch);
996+
if (!branchNote.isEmpty() && (branchNote[branchNote.size() - 1] != '\n'))
997+
{
998+
branchNote += '\n';
999+
}
9561000
if (append && commit.isNull() &&
9571001
repository->branchExists(branch) &&
958-
!repository->branchNote(branch).isEmpty())
1002+
!branchNote.isEmpty())
9591003
{
960-
text = repository->branchNote(branch) + text;
1004+
int i = branchNote.indexOf(text);
1005+
if ((i == 0) || ((i != -1) && (branchNote[i - 1] == '\n')))
1006+
{
1007+
// note is already present at the start or somewhere within following a newline
1008+
return false;
1009+
}
1010+
text = branchNote + text;
9611011
message = "Appending Git note for current " + commitRef + "\n";
9621012
}
9631013

@@ -973,9 +1023,12 @@ void FastImportRepository::Transaction::commitNote(const QByteArray &noteText, b
9731023
repository->startFastImport();
9741024
repository->fastImport.write(s);
9751025

976-
if (commit.isNull()) {
1026+
if (commit.isNull())
1027+
{
9771028
repository->setBranchNote(QString::fromUtf8(branch), text);
9781029
}
1030+
1031+
return true;
9791032
}
9801033

9811034
int FastImportRepository::Transaction::commit()

src/repository.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,11 @@ class Repository
112112
virtual void deleteFile(const QString &path) = 0;
113113
virtual QIODevice *addFile(const QString &path, int mode, qint64 length) = 0;
114114

115-
virtual void commitNote(const QByteArray &noteText, bool append,
115+
virtual bool commitNote(const QByteArray &noteText, bool append,
116116
const QByteArray &commit = QByteArray()) = 0;
117117
};
118118
virtual int setupIncremental(int &cutoff) = 0;
119+
virtual void restoreBranchNotes() = 0;
119120
virtual void restoreLog() = 0;
120121
virtual ~Repository() {}
121122

@@ -129,6 +130,7 @@ class Repository
129130
const QByteArray &author, uint dt,
130131
const QByteArray &log) = 0;
131132
virtual void finalizeTags() = 0;
133+
virtual void saveBranchNotes() = 0;
132134
virtual void commit() = 0;
133135

134136
static QByteArray formatMetadataMessage(const QByteArray &svnprefix, int revnum,

0 commit comments

Comments
 (0)