|
|
|
@ -3,14 +3,15 @@ import sqlite3
@@ -3,14 +3,15 @@ import sqlite3
|
|
|
|
|
import getopt |
|
|
|
|
import pygit2 |
|
|
|
|
import sys |
|
|
|
|
import time |
|
|
|
|
|
|
|
|
|
if len(sys.argv) != 5: |
|
|
|
|
print("usage: store_history.py -i repo_file -d database.db") |
|
|
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
|
optlist, args = getopt.getopt(sys.argv[1:], 'i:d:') |
|
|
|
|
repopath = "" |
|
|
|
|
dbpath = "" |
|
|
|
|
repopath = "." |
|
|
|
|
dbpath = "example.db" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for o, a in optlist: |
|
|
|
@ -20,8 +21,8 @@ for o, a in optlist:
@@ -20,8 +21,8 @@ for o, a in optlist:
|
|
|
|
|
dbpath = a |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print(repopath) |
|
|
|
|
print(dbpath) |
|
|
|
|
print("repo path:", repopath) |
|
|
|
|
print("databse path:", dbpath) |
|
|
|
|
|
|
|
|
|
repo = pygit2.Repository(repopath) |
|
|
|
|
|
|
|
|
@ -38,12 +39,14 @@ CREATE TABLE IF NOT EXISTS commits(
@@ -38,12 +39,14 @@ CREATE TABLE IF NOT EXISTS commits(
|
|
|
|
|
init_query2 = """ |
|
|
|
|
CREATE TABLE IF NOT EXISTS deltas( |
|
|
|
|
id INTEGER PRIMARY KEY, |
|
|
|
|
sha TEXT, |
|
|
|
|
from_sha TEXT, |
|
|
|
|
to_sha TEXT, |
|
|
|
|
new_file_name TEXT, |
|
|
|
|
addition_lines INTEGER, |
|
|
|
|
deletion_lines INTEGER, |
|
|
|
|
type TEXT, |
|
|
|
|
FOREIGN KEY (sha) REFERENCES commits(sha) |
|
|
|
|
FOREIGN KEY (from_sha) REFERENCES commits(sha), |
|
|
|
|
FOREIGN KEY (to_sha) REFERENCES commits(sha) |
|
|
|
|
); |
|
|
|
|
""" |
|
|
|
|
|
|
|
|
@ -62,9 +65,9 @@ insert_query = """
@@ -62,9 +65,9 @@ insert_query = """
|
|
|
|
|
""" |
|
|
|
|
|
|
|
|
|
insert_delta_query = """ |
|
|
|
|
INSERT INTO deltas(sha, new_file_name, addition_lines, deletion_lines, type) |
|
|
|
|
INSERT INTO deltas(from_sha, to_sha, new_file_name, addition_lines, deletion_lines, type) |
|
|
|
|
VALUES ( |
|
|
|
|
?, ?, ?, ?, ? |
|
|
|
|
?, ?, ?, ?, ?, ? |
|
|
|
|
); |
|
|
|
|
""" |
|
|
|
|
|
|
|
|
@ -82,34 +85,55 @@ data = []
@@ -82,34 +85,55 @@ data = []
|
|
|
|
|
deltas = [] |
|
|
|
|
|
|
|
|
|
last_commit = None |
|
|
|
|
for commit in repo.walk(repo.head.target, pygit2.GIT_SORT_REVERSE): |
|
|
|
|
first_time = None |
|
|
|
|
last_time = 0 |
|
|
|
|
for commit in repo.walk(repo.head.target, pygit2.GIT_SORT_TIME): |
|
|
|
|
time_t = commit.commit_time |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sha = str(commit.id) |
|
|
|
|
author_name = commit.author.name |
|
|
|
|
author_email = commit.author.email |
|
|
|
|
time_t = commit.commit_time |
|
|
|
|
message = commit.message |
|
|
|
|
data.append((sha, author_name, author_email, time_t, message)) |
|
|
|
|
print("Found commit", sha, "for", time.asctime(time.gmtime(time_t))) |
|
|
|
|
|
|
|
|
|
if last_commit is not None: |
|
|
|
|
diff = repo.diff(last_commit, commit) |
|
|
|
|
if first_time is None: |
|
|
|
|
first_time = time_t |
|
|
|
|
|
|
|
|
|
for patch in diff: |
|
|
|
|
file_name = str(patch.delta.new_file.path) |
|
|
|
|
status_char = patch.delta.status_char() |
|
|
|
|
_, additions, deletions = patch.line_stats |
|
|
|
|
deltas.append( |
|
|
|
|
(sha, file_name, additions, deletions, status_char) |
|
|
|
|
) |
|
|
|
|
# stop after one year |
|
|
|
|
if (abs(first_time - time_t) > 31536000): |
|
|
|
|
break |
|
|
|
|
|
|
|
|
|
last_commit = commit |
|
|
|
|
message = commit.message |
|
|
|
|
data.append((sha, author_name, author_email, time_t, message)) |
|
|
|
|
|
|
|
|
|
if (len(data) > 1000): |
|
|
|
|
if (abs(last_time - time_t) > 24 * 60 * 60): |
|
|
|
|
last_time = time_t |
|
|
|
|
if last_commit is not None: |
|
|
|
|
print("Finding deltas") |
|
|
|
|
diff = repo.diff(last_commit, commit) |
|
|
|
|
|
|
|
|
|
for patch in diff: |
|
|
|
|
file_name = str(patch.delta.new_file.path) |
|
|
|
|
status_char = patch.delta.status_char() |
|
|
|
|
_, additions, deletions = patch.line_stats |
|
|
|
|
deltas.append( |
|
|
|
|
(str(last_commit.id), sha, file_name, additions, deletions, status_char) |
|
|
|
|
) |
|
|
|
|
last_commit = commit |
|
|
|
|
|
|
|
|
|
if (last_commit is None): |
|
|
|
|
last_commit = commit |
|
|
|
|
|
|
|
|
|
if (len(data) + len(deltas) >= 1000): |
|
|
|
|
print("Storing", len(deltas) + len(data), "rows.") |
|
|
|
|
cursor.executemany(insert_query, data) |
|
|
|
|
cursor.executemany(insert_delta_query, deltas) |
|
|
|
|
con.commit() |
|
|
|
|
data = [] |
|
|
|
|
deltas = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Storing", len(deltas) + len(data), "rows.") |
|
|
|
|
cursor.executemany(insert_delta_query, deltas) |
|
|
|
|
cursor.executemany(insert_query, data) |
|
|
|
|
con.commit() |
|
|
|
|