forked from errbotio/err-gitbot
/
gittools.py
76 lines (61 loc) · 2.3 KB
/
gittools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from datetime import datetime
from git import *
from errbot.errBot import PLUGIN_DIR
from errbot.utils import human_name_for_git_url
import os
import logging
import shutil
GIT_LOCAL_STORAGE = PLUGIN_DIR + os.sep + 'git_repos' + os.sep
def human_to_path(human_name):
return GIT_LOCAL_STORAGE + human_name
def clone(url):
human_name = human_name_for_git_url(url)
g = Git()
g.clone(url, human_to_path(human_name), bare=True)
return human_name
def remove_repo(human_name):
path = human_to_path(human_name)
shutil.rmtree(path)
def fetch_all_heads(human_name):
path = human_to_path(human_name)
g = Git(path)
logging.debug('fetch_all_heads from %s' % path)
remote_heads_string = g.ls_remote('origin', heads=True)
branches = [line.split('/')[-1] for line in remote_heads_string.split('\n')]
repo = Repo(path)
origin = repo.remotes.origin
result = []
for branch in branches:
logging.debug('fetching = %s' % branch)
result.extend(origin.fetch('%s:%s' % (branch, branch)))
logging.debug('result = %s' % result)
return result
def get_heads_revisions(human_name):
path = human_to_path(human_name)
repo = Repo(path)
heads = repo.heads
return [(h.name, h.commit.binsha) for h in heads]
def history_since_rev(human_name, previous_heads_revisions):
repo = Repo(human_to_path(human_name))
heads = repo.heads
result = {}
for head_name, previous_commit in previous_heads_revisions:
commit_list = []
commit = heads[head_name].commit
while commit.binsha != previous_commit:
commit_list.append(commit)
if commit.parents:
commit = commit.parents[0]
else:
break
result[head_name] = commit_list
logging.debug('%s, found this history_since_rev %s' % (human_name, result))
return result
# Represents a list of commits as a log as a dictionaru of list of strings
def git_log(head_commits):
result = {}
for head in head_commits:
commits = head_commits[head]
result[head] = ["%s %20s %20s -- %s" % (commit.hexsha[:6], commit.author.name, datetime.fromtimestamp(commit.committed_date).isoformat(), commit.summary) for commit in commits]
logging.debug('git log of %s: %s' % (head, result[head]))
return result