Skip to content

Commit

Permalink
abandon incremental db update
Browse files Browse the repository at this point in the history
  • Loading branch information
irascible committed Dec 1, 2015
1 parent 7db490c commit 8be029e
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 193,047 deletions.
2 changes: 0 additions & 2 deletions pri/referencemodel.pri
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
HEADERS += \
src/referencemodel/sqlitereferencemodel.h \
src/referencemodel/referencemodel.h \
src/lib/sqlite3/sqlite3.h

SOURCES += \
src/referencemodel/sqlitereferencemodel.cpp \
src/lib/sqlite3/sqlite3.c
275 changes: 0 additions & 275 deletions src/fapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,13 +797,6 @@ bool FApplication::loadReferenceModel(const QString & databaseName, bool fullLoa
if (file.exists()) {
referenceModel->loadFromDB(dbPath);
if (!referenceModel->sha().isEmpty()) {
CommitPathActionList commitPathActionList;
git_libgit2_init();
walkRevUntil(dir.absolutePath(), referenceModel->sha(), commitPathActionList);
if (commitPathActionList.count()) {
updateParts(dir.absolutePath(), referenceModel, commitPathActionList);
}
git_libgit2_shutdown();
}
}
}
Expand Down Expand Up @@ -2114,271 +2107,3 @@ QString FApplication::getSha(const QString & repoPath) {

return sha;
}

bool FApplication::walkRevUntil(const QString & repoPath, const QString & sha, CommitPathActionList & commitPathActionList) {
git_repository * repository = NULL;
int result = git_repository_open(&repository, repoPath.toUtf8().constData());
if (result) {
return false;
}

bool ok = walkRevUntil(repository, sha, commitPathActionList);

git_repository_free(repository);
return ok;
}

bool FApplication::walkRevUntil(git_repository * repository, const QString & sha, CommitPathActionList & commitPathActionList) {
git_revwalk *revwalk = NULL;
int result = git_revwalk_new(&revwalk, repository);
if (result) {
return false;
}

bool ok = true;
result = git_revwalk_push_head(revwalk);
if (result) {
ok = false;
}
else {
git_revwalk_sorting(revwalk, GIT_SORT_TIME);
walkRevUntil(repository, revwalk, sha, commitPathActionList);
}

git_revwalk_free(revwalk);
return ok;
}

QString gitTimeToString(const git_time & time)
{
char sign;
int offset, hours, minutes;
time_t t;

offset = time.offset;
if (offset < 0) {
sign = '-';
offset = -offset;
} else {
sign = '+';
}

hours = offset / 60;
minutes = offset % 60;

t = (time_t) time.time + (time.offset * 60);

QDateTime dateTime = QDateTime::fromTime_t(t);

return QString("%1 %2%3%4")
.arg(dateTime.toString(Qt::ISODate))
.arg(sign)
.arg(hours, 2, 10, QChar('0'))
.arg(minutes, 2, 10, QChar('0'));
}

void FApplication::walkRevUntil(git_repository *repository, git_revwalk *revwalk, const QString & sha, CommitPathActionList & commitPathActionList)
{
git_oid oid;
git_commit *commit = NULL;

for (; !git_revwalk_next(&oid, revwalk); git_commit_free(commit)) {
CommitPathAction commitPathAction;

char buffer[64];
git_oid_tostr(buffer, sizeof(buffer), &oid);
commitPathAction.sha = QString(buffer);
qDebug() << "sha" << commitPathAction.sha;
if (commitPathAction.sha.startsWith(sha)) {
// we have reached the commit marked in the database
break;
}

int result = git_commit_lookup(&commit, repository, &oid);
if (result) {
// what to do here?
continue;
}

const git_signature *signature = git_commit_author(commit);
QString timeString;
if (signature != NULL) {
timeString = gitTimeToString(signature->when);
}

qDebug() << "\t" << timeString << git_commit_summary(commit);

git_tree *tree;
result = git_commit_tree(&tree, commit);
if (result) {
// what to do here?
}
else {
int parentCount = (int) git_commit_parentcount(commit);
for (int pix = 0; pix < parentCount; pix++) {
diffParent(commit, tree, sha, commitPathAction, pix);
}

if (commitPathAction.pathActions.count()) {
// reverse the order so older commits come first
commitPathActionList.prepend(commitPathAction);
}

git_tree_free(tree);
}
}
}

bool FApplication::diffParent(git_commit * commit, git_tree * tree, const QString & sha, CommitPathAction & commitPathAction, int pix)
{
git_commit * parentCommit = NULL;
int result = git_commit_parent(&parentCommit, commit, (size_t) pix);
if (result) return false;

git_tree * parentTree;
result = git_commit_tree(&parentTree, parentCommit);
if (result == 0) {
git_diff * diff;
result = git_diff_tree_to_tree(&diff, git_commit_owner(commit), parentTree, tree, NULL);
if (result == 0) {
int deltaCount = (int) git_diff_num_deltas(diff);
for (int dix = 0; dix < deltaCount; dix++) {
const git_diff_delta * delta = git_diff_get_delta(diff, (size_t) dix);

if (strcmp(delta->old_file.path, delta->new_file.path) != 0) {
qDebug() << "different paths" << sha;
continue;
}

PathAction pathAction;
pathAction.path = delta->new_file.path;
if (!pathAction.path.endsWith(".fzp")) {
// only fzp files update the database
continue;
}

switch (delta->status) {
case GIT_DELTA_ADDED:
pathAction.action = PathAction::ADD_ACTION;
break;
case GIT_DELTA_DELETED:
pathAction.action = PathAction::DELETE_ACTION;
break;
case GIT_DELTA_MODIFIED:
pathAction.action = PathAction::MODIFY_ACTION;
break;
default:
qDebug() << "unhandled status" << delta->status << sha;
continue;
}

qDebug() << "\t" << pathAction.action << pathAction.path;
commitPathAction.pathActions << pathAction;
}

git_diff_free(diff);
}
git_tree_free(parentTree);
}
git_commit_free(parentCommit);

return true;
}

bool FApplication::updateParts(const QString & repoPath, ReferenceModel * referenceModel, const CommitPathActionList & commitPathActionList) {
static int updatePartsCount = 0;
QString dt = QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss_zzz");

git_repository * repository = NULL;
int result = git_repository_open(&repository, repoPath.toUtf8().constData());
if (result) {
FMessageBox::warning(NULL, tr("Regenerating parts database"), tr("Unable to find parts git repository"));
return false;
}

git_object *master_treeish = NULL;
result = git_revparse_single(&master_treeish, repository, "master");
if (result) {
FMessageBox::warning(NULL, tr("Regenerating parts database"), tr("Unable to find master branch"));
git_repository_free(repository);
return false;
}

git_commit * commit = NULL;
git_reference * reference = NULL;
git_checkout_options checkout_options = GIT_CHECKOUT_OPTIONS_INIT;
checkout_options.checkout_strategy = GIT_CHECKOUT_FORCE;
foreach (CommitPathAction commitPathAction, commitPathActionList) {
QString branchName = QString("b%1_%2").arg(dt).arg(updatePartsCount++);
QString fullBranchName = "refs/heads/" + branchName;

git_oid oid;
result = git_oid_fromstr(&oid, commitPathAction.sha.toUtf8().constData());
if (result) {
FMessageBox::warning(NULL, tr("Regenerating parts database"), tr("Commit failure (1)"));
break;
}

result = git_commit_lookup(&commit, repository, &oid);
if (result) {
FMessageBox::warning(NULL, tr("Regenerating parts database"), tr("Commit failure (2)"));
break;
}

result = git_branch_create(&reference, repository, branchName.toUtf8().constData(), commit, true);
if (result) {
FMessageBox::warning(NULL, tr("Regenerating parts database"), tr("Branch failure (1)"));
break;
}

git_commit_free(commit);
commit = NULL;

git_object *treeish = NULL;
result = git_revparse_single(&treeish, repository, branchName.toUtf8().constData());
if (result) {
FMessageBox::warning(NULL, tr("Regenerating parts database"), tr("Branch failure (2)"));
break;
}

result = git_checkout_tree(repository, treeish, &checkout_options);
git_object_free(treeish);
if (result) {
FMessageBox::warning(NULL, tr("Regenerating parts database"), tr("Branch failure (3)"));
break;
}

result = git_repository_set_head(repository, fullBranchName.toUtf8().constData());
if (result) {
FMessageBox::warning(NULL, tr("Regenerating parts database"), tr("Branch failure (3)"));
break;
}

referenceModel->updateParts(repoPath, commitPathAction);

result = git_checkout_tree(repository, master_treeish, &checkout_options);
if (result) {
FMessageBox::warning(NULL, tr("Regenerating parts database"), tr("Unfortunately the parts database is broken. Please download the fritzing application again: www.fritzing.org"));
break;
}

result = git_repository_set_head(repository, "refs/heads/master");
if (result) {
FMessageBox::warning(NULL, tr("Regenerating parts database"), tr("Branch failure (3)"));
break;
}


git_branch_delete(reference);
git_reference_free(reference);
reference = NULL;

}

git_object_free(master_treeish);
git_commit_free(commit);
git_reference_free(reference);
git_repository_free(repository);

return (result == 0);
}
5 changes: 0 additions & 5 deletions src/fapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,6 @@ public slots:
void cleanFzzs();
void initServer();
QString getSha(const QString & repoPath);
bool walkRevUntil(const QString & repoPath, const QString &sha, CommitPathActionList &commitPathActionList);
bool walkRevUntil(struct git_repository * repository, const QString &sha, CommitPathActionList &commitPathActionList);
void walkRevUntil(struct git_repository *repository, struct git_revwalk *revwalk, const QString &sha, CommitPathActionList &commitPathActionList);
bool diffParent(struct git_commit * commit, struct git_tree * tree, const QString &sha, CommitPathAction &commitPathAction, int pix);
bool updateParts(const QString &repoPath, ReferenceModel * referenceModel, const CommitPathActionList & commitPathActionList);

enum ServiceType {
PanelizerService = 1,
Expand Down
Loading

0 comments on commit 8be029e

Please sign in to comment.