2 # this program compare two branche of GIT
3 # and show the differences
15 print >>sys.stderr, "you must install python-git aka GitPython"
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)
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)
36 def run_cmp_branch(repo, args):
39 # for commit in repo.iter_commits(args.branch1, max_count=10):
40 # print commit.hexsha, commit.committed_date, commit.author.name, commit.message
43 commons=repo.merge_base(args.branch1, args.branch2)
45 print "cannot find the unique common commit between", args.branch1, args.branch2
48 # make a list of all know commit in branch-2
50 for commit in repo.iter_commits(args.branch2):
51 if commit.hexsha==common.hexsha:
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
58 # list and compare with commits of branch-&
59 for commit in repo.iter_commits(args.branch1):
60 if commit.hexsha==common.hexsha:
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
67 print "+", date, commit.author.name, subject
69 mainparser=argparse.ArgumentParser(description='git utility for bacula')
70 subparsers=mainparser.add_subparsers(dest='command', metavar='', title='valid commands')
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')
75 parser=subparsers.add_parser('cmp_branch', parents=[git_parser, ], help='compare two branches, highligh commits missing in the second branch')
77 parser.add_argument('branch1', metavar='BRANCH-1', help='the first branch')
78 parser.add_argument('branch2', metavar='BRANCH-2', help='the second branch')
80 args=mainparser.parse_args()
83 logging.getLogger().setLevel(logging.DEBUG)
87 print "logging into gitstat.log"
88 add_file_logger('gitstat.log')
95 while path and not os.path.isdir(os.path.join(path, '.git')):
96 path=os.path.dirname(path)
99 if path and os.path.isdir(os.path.join(path, '.git')):
102 except git.exc.InvalidGitRepositoryError:
103 parser.error("git repository not found in %s" % (path,))
107 parser.error("not .git directory found above %s" % (os.getcwd(),))
111 repo=git.Repo(args.git_dir)
112 except git.exc.InvalidGitRepositoryError:
113 parser.error("git repository not found in %s" % (args.git_dir,))
115 if args.command=='cmp_branch':
116 run_cmp_branch(repo, args)