forked from importers/git-importer
		
	Use proper user info in commits
This commit is contained in:
		
							
								
								
									
										17
									
								
								lib/git.py
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								lib/git.py
									
									
									
									
									
								
							| @@ -73,10 +73,8 @@ class Git: | |||||||
|         committer=None, |         committer=None, | ||||||
|         committer_email=None, |         committer_email=None, | ||||||
|         committer_time=None, |         committer_time=None, | ||||||
|         allow_empty=False, |  | ||||||
|     ): |     ): | ||||||
|         """Add all the files and create a new commit in the current HEAD""" |         """Add all the files and create a new commit in the current HEAD""" | ||||||
|         assert allow_empty or self.is_dirty() |  | ||||||
|  |  | ||||||
|         if not committer: |         if not committer: | ||||||
|             committer = self.committer if self.committer else self.user |             committer = self.committer if self.committer else self.user | ||||||
| @@ -85,33 +83,20 @@ class Git: | |||||||
|             ) |             ) | ||||||
|             committer_time = committer_time if committer_time else user_time |             committer_time = committer_time if committer_time else user_time | ||||||
|  |  | ||||||
|         try: |         if self.is_dirty(): | ||||||
|             self.repo.index.add_all() |             self.repo.index.add_all() | ||||||
|         except pygit2.GitError as e: |  | ||||||
|             if not allow_empty: |  | ||||||
|                 raise e |  | ||||||
|  |  | ||||||
|         self.repo.index.write() |         self.repo.index.write() | ||||||
|         author = pygit2.Signature(user, user_email, int(user_time.timestamp())) |         author = pygit2.Signature(user, user_email, int(user_time.timestamp())) | ||||||
|         committer = pygit2.Signature( |         committer = pygit2.Signature( | ||||||
|             committer, committer_email, int(committer_time.timestamp()) |             committer, committer_email, int(committer_time.timestamp()) | ||||||
|         ) |         ) | ||||||
|         if not parents: |  | ||||||
|             try: |  | ||||||
|                 parents = [self.repo.head.target] |  | ||||||
|             except pygit2.GitError as e: |  | ||||||
|                 parents = [] |  | ||||||
|                 if not allow_empty: |  | ||||||
|                     raise e |  | ||||||
|  |  | ||||||
|         tree = self.repo.index.write_tree() |         tree = self.repo.index.write_tree() | ||||||
|         return self.repo.create_commit( |         return self.repo.create_commit( | ||||||
|             "HEAD", author, committer, message, tree, parents |             "HEAD", author, committer, message, tree, parents | ||||||
|         ) |         ) | ||||||
|  |  | ||||||
|     def merge_abort(self): |  | ||||||
|         self.repo.state_cleanup() |  | ||||||
|  |  | ||||||
|     def last_commit(self): |     def last_commit(self): | ||||||
|         try: |         try: | ||||||
|             return self.repo.head.target |             return self.repo.head.target | ||||||
|   | |||||||
| @@ -7,8 +7,9 @@ from lib.binary import is_binary_or_large | |||||||
| from lib.db import DB | from lib.db import DB | ||||||
| from lib.git import Git | from lib.git import Git | ||||||
| from lib.obs import OBS | from lib.obs import OBS | ||||||
| from lib.proxy_sha256 import ProxySHA256, md5 | from lib.proxy_sha256 import ProxySHA256 | ||||||
| from lib.tree_builder import TreeBuilder | from lib.tree_builder import TreeBuilder | ||||||
|  | from lib.user import User | ||||||
|  |  | ||||||
|  |  | ||||||
| class GitExporter: | class GitExporter: | ||||||
| @@ -31,12 +32,7 @@ class GitExporter: | |||||||
|     def set_gc_interval(self, gc): |     def set_gc_interval(self, gc): | ||||||
|         self.gc_interval = gc |         self.gc_interval = gc | ||||||
|  |  | ||||||
|     def export_as_git(self): |     def check_repo_state(self, flats, branch_state): | ||||||
|         db = DB() |  | ||||||
|         tree = TreeBuilder(db).build(self.project, self.package) |  | ||||||
|         flats = tree.as_flat_list() |  | ||||||
|  |  | ||||||
|         branch_state = {"factory": None, "devel": None} |  | ||||||
|         state_data = dict() |         state_data = dict() | ||||||
|         if os.path.exists(self.state_file): |         if os.path.exists(self.state_file): | ||||||
|             with open(self.state_file, "r") as f: |             with open(self.state_file, "r") as f: | ||||||
| @@ -57,19 +53,38 @@ class GitExporter: | |||||||
|                     found_state = True |                     found_state = True | ||||||
|             if not found_state: |             if not found_state: | ||||||
|                 left_to_commit.append(flat) |                 left_to_commit.append(flat) | ||||||
|  |         return left_to_commit | ||||||
|  |  | ||||||
|  |     def export_as_git(self): | ||||||
|  |         db = DB() | ||||||
|  |         tree = TreeBuilder(db).build(self.project, self.package) | ||||||
|  |         flats = tree.as_flat_list() | ||||||
|  |  | ||||||
|  |         branch_state = {"factory": None, "devel": None} | ||||||
|  |         left_to_commit = self.check_repo_state(flats, branch_state) | ||||||
|  |  | ||||||
|  |         if not left_to_commit: | ||||||
|  |             return | ||||||
|  |  | ||||||
|  |         logging.info(f"Commiting into {self.git.path}") | ||||||
|  |         self.run_gc() | ||||||
|  |         users = dict() | ||||||
|  |  | ||||||
|         gc_cnt = self.gc_interval |  | ||||||
|         if len(left_to_commit) > 0: |  | ||||||
|             logging.info(f"Commiting into {self.git.path}") |  | ||||||
|             self.git.gc() |  | ||||||
|         for flat in left_to_commit: |         for flat in left_to_commit: | ||||||
|             gc_cnt -= 1 |             if flat.commit.userid not in users: | ||||||
|             if gc_cnt <= 0 and self.gc_interval: |                 users[flat.commit.userid] = User(db, flat.commit.userid) | ||||||
|                 self.git.gc() |             flat.user = users[flat.commit.userid] | ||||||
|                 gc_cnt = self.gc_interval |             logging.debug(f"USER {flat.user}") | ||||||
|  |             self.gc_cnt -= 1 | ||||||
|  |             if self.gc_cnt <= 0 and self.gc_interval: | ||||||
|  |                 self.run_gc() | ||||||
|             logging.debug(f"Committing {flat}") |             logging.debug(f"Committing {flat}") | ||||||
|             self.commit_flat(flat, branch_state) |             self.commit_flat(flat, branch_state) | ||||||
|  |  | ||||||
|  |     def run_gc(self): | ||||||
|  |         self.gc_cnt = self.gc_interval | ||||||
|  |         self.git.gc() | ||||||
|  |  | ||||||
|     def commit_file(self, flat, file, size, md5): |     def commit_file(self, flat, file, size, md5): | ||||||
|         # have such files been detected as text mimetype before? |         # have such files been detected as text mimetype before? | ||||||
|         is_text = self.proxy_sha256.is_text(flat.commit.package, file.name) |         is_text = self.proxy_sha256.is_text(flat.commit.package, file.name) | ||||||
| @@ -107,7 +122,7 @@ class GitExporter: | |||||||
|  |  | ||||||
|         if flat.parent1: |         if flat.parent1: | ||||||
|             if not self.branch_fits_parent1(flat, branch_state): |             if not self.branch_fits_parent1(flat, branch_state): | ||||||
|                 logging.info(f"Reset {flat.branch} onto {flat.parent1.short_string()}") |                 logging.debug(f"Reset {flat.branch} onto {flat.parent1.short_string()}") | ||||||
|                 assert flat.parent1.git_commit |                 assert flat.parent1.git_commit | ||||||
|                 self.git.set_branch_head(flat.branch, flat.parent1.git_commit) |                 self.git.set_branch_head(flat.branch, flat.parent1.git_commit) | ||||||
|                 self.git.checkout(flat.branch) |                 self.git.checkout(flat.branch) | ||||||
| @@ -126,12 +141,11 @@ class GitExporter: | |||||||
|             self.commit_file(flat, file, size, md5) |             self.commit_file(flat, file, size, md5) | ||||||
|  |  | ||||||
|         commit = self.git.commit( |         commit = self.git.commit( | ||||||
|             f"OBS User {flat.commit.userid}", |             flat.user.realname, | ||||||
|             "null@suse.de", |             flat.user.email, | ||||||
|             flat.commit.commit_time, |             flat.commit.commit_time, | ||||||
|             # TODO: Normalize better the commit message |             # TODO: Normalize better the commit message | ||||||
|             f"{flat.commit.comment}\n\n{flat.commit}", |             f"{flat.commit.comment}\n\n{flat.commit}", | ||||||
|             allow_empty=True, |  | ||||||
|             parents=parents, |             parents=parents, | ||||||
|         ) |         ) | ||||||
|         flat.commit.git_commit = commit |         flat.commit.git_commit = commit | ||||||
|   | |||||||
							
								
								
									
										13
									
								
								lib/user.py
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								lib/user.py
									
									
									
									
									
								
							| @@ -15,6 +15,19 @@ FAKE_ACCOUNTS = ( | |||||||
|  |  | ||||||
|  |  | ||||||
| class User: | class User: | ||||||
|  |     def __init__(self, db, userid) -> None: | ||||||
|  |         row = User.lookup(db, userid) | ||||||
|  |         self.userid = userid | ||||||
|  |         if row: | ||||||
|  |             (_, _, self.email, self.realname) = row | ||||||
|  |         else: | ||||||
|  |             self.email = "" | ||||||
|  |             self.realname = "" | ||||||
|  |         if not self.email: | ||||||
|  |             self.email = "null@suse.de" | ||||||
|  |         if not self.realname: | ||||||
|  |             self.realname = f"OBS User {userid}" | ||||||
|  |  | ||||||
|     def parse(self, xml, userid): |     def parse(self, xml, userid): | ||||||
|         self.userid = userid |         self.userid = userid | ||||||
|         self.realname = xml.find("realname").text |         self.realname = xml.find("realname").text | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user