Created
December 8, 2015 15:03
-
-
Save spoorcc/ef07e09484bb8d3b3c5f to your computer and use it in GitHub Desktop.
Print git log -g for Cleartool
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
#!/usr/bin/env python | |
import subprocess | |
class ParsedLine(object): | |
def __init__(self, timestamp="", name="", object_type="", comment="", stream="", other_stream=""): | |
self.timestamp = timestamp | |
self.name = name | |
self.object_type = object_type | |
self.comment = comment | |
self.stream = stream | |
self.other_stream = other_stream | |
self.parent_stream = "" | |
class bcolors: | |
PURPLE = '\033[95m' | |
BLUE = '\033[94m' | |
GREEN = '\033[92m' | |
YELLOW = '\033[93m' | |
RED = '\033[91m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
ENDC = '\033[0m' | |
#---------- Main printing function --------------------- | |
def print_graph(lines): | |
parsed_list = parse(lines) | |
print_parsed(parsed_list) | |
#---------- Parsing stuff --------------------- | |
def parse(lines): | |
parsed_stuff = [] | |
for line in lines: | |
try: | |
parsed_stuff += [parse_line(line)] | |
except TypeError: | |
pass | |
return parsed_stuff | |
def parse_line(line): | |
if "\t" in line: | |
separate_items = line.split("\t") | |
parsed_line = ParsedLine(timestamp=separate_items[0], | |
name=separate_items[1], | |
object_type=separate_items[2], | |
comment=separate_items[3], | |
stream=separate_items[4]) | |
if parsed_line.comment.startswith("deliver"): | |
parsed_line.object_type = "deliver activity" | |
parsed_line.other_stream = parsed_line.stream | |
parsed_line.stream = parsed_line.comment.split(" ")[1] | |
if parsed_line.comment.startswith("rebase"): | |
parsed_line.object_type = "rebase activity" | |
parsed_line.other_stream = parsed_line.comment.split(" ")[1] | |
if parsed_line.object_type == "stream": | |
parsed_line.stream = parsed_line.name | |
else: | |
raise TypeError | |
return parsed_line | |
#---------- Printing stuff --------------------- | |
def print_parsed(parsed_list): | |
streams = [] | |
for item in parsed_list: | |
if item.object_type == 'stream': | |
# Since new streams change more often, add them after int stream | |
try: | |
streams = [streams[0]] + [item.name] + streams[1:] | |
except IndexError: | |
streams = [item.name] + streams | |
item.other_stream = streams[0] | |
print_new_stream(streams, item) | |
elif item.object_type == 'activity': | |
print_activity(streams, item) | |
elif item.object_type == 'deliver activity': | |
print_deliver_activity(streams, item) | |
elif item.object_type == 'rebase activity': | |
print_rebase_activity(streams, item) | |
def print_new_stream(streams, stream): | |
number_of_streams = len(streams) | |
result_string ="" | |
index = streams.index(stream.name) | |
# Line above only needed when a branch should be drawn | |
if number_of_streams > 1: | |
# Swap labels, so it will be printed on parent stream | |
stream.stream, stream.other_stream = stream.other_stream, stream.stream | |
print_activity([s for s in streams if s != stream.name], stream) | |
stream.stream, stream.other_stream = stream.other_stream, stream.stream | |
result_string = "| " * index | |
result_string = result_string.strip(" ") | |
result_string += "\\" | |
result_string += " \\" * (len(streams)-(index+1)) | |
print(result_string) | |
print_activity(streams, stream) | |
def print_activity(streams, activity): | |
index = streams.index(activity.stream) | |
msg = "{name} {comment}".format(name=activity.name, comment=activity.comment) | |
result_str = token_over_row(index+1, len(streams), "*", msg=msg) | |
print result_str | |
def print_deliver_activity(streams, activity): | |
from_index = streams.index(activity.stream) -1 | |
to_index = streams.index(activity.other_stream) | |
result_str = "" | |
while from_index >= to_index: | |
result_str += token_in_row(from_index+1, len(streams), "/", msg="\n") | |
if from_index != to_index: | |
result_str += token_over_row(from_index+1, len(streams), "/", msg="\n") | |
from_index -= 1 | |
if result_str: | |
print result_str.strip('\n') | |
activity.stream = activity.other_stream | |
print_activity(streams, activity) | |
def print_rebase_activity(streams, activity): | |
# Assuming first stream is always the source | |
#from_index = streams.index(activity.other_stream) | |
from_index = 1 | |
to_index = streams.index(activity.stream) | |
result_str = "" | |
while from_index <= to_index: | |
result_str += token_in_row(from_index, len(streams), "\\", msg="\n") | |
if from_index != to_index: | |
result_str += token_over_row(from_index+1, len(streams), "\\", msg="\n") | |
from_index += 1 | |
if result_str: | |
print result_str.strip('\n') | |
print_activity(streams, activity) | |
def token_in_row(index, width, token, msg=""): | |
result_str = "| " * (index-1) | |
result_str += "|{token}".format(token=token) | |
result_str += "| " * (width-index) | |
result_str += msg | |
return result_str | |
def token_over_row(index, width, token, msg=""): | |
result_str = "| " * (index-1) | |
result_str += "{token} ".format(token=token) | |
result_str += "| " * (width-index) | |
result_str += msg | |
return result_str | |
if __name__ == '__main__': | |
cleartool_data = subprocess.check_output(['cleartool','lsstream','-tree','-fmt','"%Nd\t%n\t%m\t%[headline]p\t%[stream]p\n"']) | |
ct_data_list = cleartool_data.split("\n") | |
ct_data_list = [line.strip("\"") for line in ct_data_list] | |
ct_data_list.sort() | |
print_graph(ct_data_list) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment