forked from tkrajina/git-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-relation
executable file
·103 lines (80 loc) · 3.18 KB
/
git-relation
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright 2013 Tomo Krajina
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Show a relation between two git commits/tags/branches.
"""
import sys as mod_sys
import gitutils as mod_gitutils
mod_gitutils.assert_in_git_repository()
branches = mod_sys.argv[1:]
if not branches:
print 'No branches specified'
mod_sys.exit(1)
if len(branches) == 1:
branches = ['HEAD'] + branches
if len(branches) != 2:
print 'Two branches expected, got %s' % branches
mod_sys.exit(1)
def get_git_sha1(branch_name):
success, sha1 = mod_gitutils.execute_git('log -1 %s --format=%%H' % branch_name,
output=False)
if not success:
print 'Invalid branch %s' % branch_name
mod_sys.exit(1)
return sha1.strip()
def print_log(commit_1, commit_2):
success, log = mod_gitutils.execute_git(
'log %s..%s --format=%%x20%%x20%%x20%%h%%x20%%Cgreen%%an%%Creset%%x20\"%%Cred%%s%%Creset\",%%x20%%ar' % (commit_1, commit_2),
output=False)
if not success:
print 'Error retrieving log %s..%s' % (commit_1, commit_2)
mod_sys.exit(1)
result = log.rstrip()
lines = result.split('\n')
print '%s commits from \033[1;34m%s\033[0m to \033[1;34m%s\033[0m:' % (len(lines), commit_1, commit_2)
if len(lines) < 30:
print result
return
first = lines[:10]
last = lines[-10:]
lines = first + [' ...%s more commits...' % (len(lines) - len(first) - len(last))] + last
print '\n'.join(lines)
branch_1 = branches[0]
branch_2 = branches[1]
branch_1_sha1 = get_git_sha1(branch_1)
branch_2_sha1 = get_git_sha1(branch_2)
success, merge_base = mod_gitutils.execute_git('merge-base %s %s' % (branch_1, branch_2),
output=False)
merge_base = merge_base.strip()
if not success:
print 'Can\'t find merge base for %s and %s' % (branch_1, branch_2)
mod_sys.exit(1)
elif merge_base == branch_1_sha1 and merge_base == branch_2_sha1:
print '\033[1;34m%s\033[0m \033[1;31mEQUALS\033[0m \033[1;34m%s\033[0m' % (branch_1, branch_2)
elif merge_base == branch_1_sha1:
print '\033[1;34m%s\033[0m is \033[1;31mBEHIND\033[0m \033[1;34m%s\033[0m' % (branch_1, branch_2)
print
print_log(branch_1, branch_2)
elif merge_base == branch_2_sha1:
print '\033[1;34m%s\033[0m is \033[1;31mAHEAD\033[0m of \033[1;34m%s\033[0m' % (branch_1, branch_2)
print
print_log(branch_2, branch_1)
else:
print '\033[1;34m%s\033[0m and %s \033[1;31mDIVERGED\033[0m, common point is \033[1;34m%s\033[0m' % (branch_1, branch_2, merge_base)
print
print_log(merge_base, branch_1)
print
print_log(merge_base, branch_2)