git历史查看工具

  • 2019 年 11 月 22 日
  • 笔记

哈哈。终于把之前公司那个玩意重写了。

效果

配置

vim ~/.bashrc  alias ghis="python2 /workspace/git_history/git_history.py"

注意,代码中可以配置显示详细信息:

class CommitInfo():      def __init__(self):          ...          self.is_simple = True

源码

# coding:utf-8  import os  import subprocess  import sys    filename = sys.argv[1]  find_text = sys.argv[2]    try:      is_blame = sys.argv[3]  except:      is_blame = False      def cmd(cmdstr):      return subprocess.check_output(cmdstr, shell=True)      dir_path = os.getcwd()  filename = dir_path + "/" + filename    # 打印查询结果前后的行  near_lines = 5      class Queue(object):      def __init__(self, max_lines):          self.q = []          self.max_lines = max_lines          self.pos = 0          self.key = ""  # key会被显色加粗        def append(self, val):          if len(self.q) > self.max_lines:              self.q.pop(0)          self.q.append(val)          self.pos += 1        def print_me(self):          for item in self.q:              if self.key:                  item = item.replace(self.key, "33[31;1m{}33[0m".format(self.key))              print(item)        def reset_pos(self):          self.pos = 0        def reset(self):          self.q = []      queue = Queue(2 * near_lines + 1)  queue.key = find_text      def blame():      queue.pos = 65535      ret = cmd("git blame {}".format(filename)).decode("utf-8").split("n")      for line in ret:          queue.append(line)            if find_text in line:              queue.reset_pos()            if queue.pos == near_lines:              queue.print_me()              queue.reset()      class CommitInfo(object):      def __init__(self):          self.q = []          self.is_block = False          self.is_print = False          self.key = "commit"          self.is_simple = True        def append(self, val):          if self.is_block:              return          if self.is_simple:              if val.startswith("Date"):                  self.is_block = True          else:              if val.startswith("+") or val.startswith("-") or val.startswith('diff'):                  self.is_block = True            self.q.append(val)        def print_me(self):          if self.is_print:              return            for item in self.q:              if self.key:                  item = item.replace(self.key, "33[34;1m{}33[0m".format(self.key))              print(item)          self.is_print = True      def history():      queue.pos = 65535      ret = cmd("git log -p {}".format(filename)).decode("utf-8").split("n")        commit_info = None      for line in ret:          if line.startswith("commit "):              commit_info = CommitInfo()            if commit_info and not commit_info.is_block:              commit_info.append(line)          else:              queue.append(line)            if find_text in line and (line.startswith("+") or line.startswith("-")):              # 找到了关键字              queue.reset_pos()              commit_info.print_me()            if queue.pos == near_lines:              queue.print_me()              queue.reset()      if is_blame:      blame()  else:      history()