Last active
June 14, 2020 17:26
-
-
Save rizsotto/7893973 to your computer and use it in GitHub Desktop.
pygit2 exercise to create tags on certain commits. usage: enter the desired directory and exec the script from there.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
import pygit2 | |
import logging | |
import re | |
import os | |
import os.path | |
def main(): | |
repo = pygit2.init_repository(os.getcwd()) | |
last = repo[repo.head.target] | |
for c in [c for c in repo.walk(last.oid, pygit2.GIT_SORT_TIME) if c.author.name == 'Places CI Bot']: | |
logging.debug('commit: {}'.format(c.hex)) | |
parent = c.parents[0] | |
latest = tagname([p.new_file_path for p in parent.tree.diff_to_tree(c.tree)]) | |
if latest: | |
logging.debug(' added dirname: {}'.format(latest)) | |
try: | |
repo.create_tag(latest, c.hex, pygit2.GIT_OBJ_COMMIT, c.author, c.message) | |
except ValueError as e: | |
logging.debug(' fail to create tag {}: {}'.format(latest, str(e))) | |
def tagname(files): | |
if files == []: | |
return None | |
prefix = os.path.commonprefix(files) | |
while True: | |
p = os.path.dirname(prefix) | |
if p != '': | |
prefix = p | |
continue | |
return prefix | |
if __name__ == '__main__': | |
logging.basicConfig(format='%(levelname)s: %(message)s', level='DEBUG') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment