]> git.sur5r.net Git - bacula/bacula/blob - bacula/release/bgit.py
Big backport from Enterprise
[bacula/bacula] / bacula / release / bgit.py
1 #!/usr/bin/env python2
2 # this program compare two branche of GIT
3 # and show the differences
4 import sys
5 import os
6 import logging
7 import collections
8 import re
9 import argparse
10 import time
11
12 try:
13     import git
14 except ImportError:
15     print >>sys.stderr, "you must install python-git aka GitPython"
16     sys.exit(1)
17
18
19 def add_console_logger():
20     console=logging.StreamHandler()
21     console.setFormatter(logging.Formatter('%(levelname)-3.3s %(filename)s:%(lineno)d %(message)s', '%H:%M:%S'))
22     console.setLevel(logging.DEBUG) # must be INFO for prod
23     logging.getLogger().addHandler(console)
24     return console
25
26 def add_file_logger(filename):
27     filelog=logging.FileHandler(filename)
28     # %(asctime)s  '%Y-%m-%d %H:%M:%S'
29     filelog.setFormatter(logging.Formatter('%(asctime)s %(levelname)-3.3s %(filename)s:%(lineno)d %(message)s', '%H:%M:%S'))
30     filelog.setLevel(logging.DEBUG)
31     logging.getLogger().addHandler(filelog)
32     return filelog
33
34
35
36 def run_cmp_branch(repo, args):
37     print args.branch1
38     print args.branch2
39 #    for commit in repo.iter_commits(args.branch1, max_count=10):
40 #        print commit.hexsha, commit.committed_date, commit.author.name, commit.message
41
42 #    print dir(repo)
43     commons=repo.merge_base(args.branch1, args.branch2)
44     if len(commons)!=1:
45         print "cannot find the unique common commit between", args.branch1, args.branch2
46
47     common=commons[0]
48     # make a list of all know commit in branch-2
49     commits2=set()
50     for commit in repo.iter_commits(args.branch2):
51         if commit.hexsha==common.hexsha:
52             break
53
54         subject=commit.message.split('\n', 1)[0]
55         commits2.add((commit.authored_date, commit.author.name, subject))
56         #print commit.committed_date, commit.author.name, subject
57
58     # list and compare with commits of branch-&
59     for commit in repo.iter_commits(args.branch1):
60         if commit.hexsha==common.hexsha:
61             break
62         subject=commit.message.split('\n', 1)[0]
63         date=time.strftime("%Y-%m-%d %H:%M", time.gmtime(commit.authored_date))
64         if (commit.authored_date, commit.author.name, subject) in commits2:
65             print "=", date, commit.author.name, subject
66         else:
67             print "+", date, commit.author.name, subject
68
69 mainparser=argparse.ArgumentParser(description='git utility for bacula')
70 subparsers=mainparser.add_subparsers(dest='command', metavar='', title='valid commands')
71
72 git_parser=argparse.ArgumentParser(add_help=False)
73 git_parser.add_argument('--git_dir', metavar='GIT-DIR', type=str, default='.', help='the directory with the .git sub dir')
74
75 parser=subparsers.add_parser('cmp_branch', parents=[git_parser, ], help='compare two branches, highligh commits missing in the second branch')
76
77 parser.add_argument('branch1', metavar='BRANCH-1', help='the first branch')
78 parser.add_argument('branch2', metavar='BRANCH-2', help='the second branch')
79
80 args=mainparser.parse_args()
81
82
83 logging.getLogger().setLevel(logging.DEBUG)
84
85 add_console_logger()
86 print args.git_dir
87 print "logging into gitstat.log"
88 add_file_logger('gitstat.log')
89
90 # search the git repo
91 repo=None
92 if args.git_dir:
93     if args.git_dir=='.':
94         path=os.getcwd()
95         while path and not os.path.isdir(os.path.join(path, '.git')):
96             path=os.path.dirname(path)
97             print path
98
99         if path and os.path.isdir(os.path.join(path, '.git')):
100             try:
101                 repo=git.Repo(path)
102             except git.exc.InvalidGitRepositoryError:
103                 parser.error("git repository not found in %s" % (path,))
104             else:
105                 args.git_dir=path
106         else:
107             parser.error("not .git directory found above %s" % (os.getcwd(),))
108
109     else:
110         try:
111             repo=git.Repo(args.git_dir)
112         except git.exc.InvalidGitRepositoryError:
113             parser.error("git repository not found in %s" % (args.git_dir,))
114
115 if args.command=='cmp_branch':
116     run_cmp_branch(repo, args)
117