首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用图形对齐git日志

如何用图形对齐git日志
EN

Stack Overflow用户
提问于 2017-09-14 22:56:42
回答 5查看 2.3K关注 0票数 5

有了像git log --abbrev-commit --pretty=format:'%h %<(72,trunc)%s %d'这样的东西,您就可以得到一个与图形相当一致的git提交消息。如果您删除--graph选项,它将完全对齐。下面是命令的大致内容

代码语言:javascript
复制
*   40f1481 Commit title message        (HEAD, dev)
|\                                                                     
| * 9b5122d Commit title message        (debug)
| * fe7ddfe Commit title message               
| * 8e4d414 Commit title message      
| * 53affdd Commit title message    
| * a45fbaf Commit title message             
|/                                                                     
* 36b23c3 Commit title message                     
* 5b7bfe1 Commit title message        (master)

问题是,对于图形符号,对齐是混乱的,正如您在前两次提交中看到的那样。理想情况是这样的

代码语言:javascript
复制
*   40f1481 Commit title message        (HEAD, dev)
|\                                                                     
| * 9b5122d Commit title message        (debug)
| * fe7ddfe Commit title message               
| * 8e4d414 Commit title message      
| * 53affdd Commit title message    
| * a45fbaf Commit title message             
|/                                                                     
*   36b23c3 Commit title message                     
*   5b7bfe1 Commit title message        (master)

我的问题是,在使用绘图选项时,是否有获得正确对齐的选项?还是要获得图形的宽度,这样就可以相应地填充日志了?

我知道一种快速的攻击就是按选项卡(%x09)填充第一个选项,而且它应该适用于大多数项目,但我想知道它们是否是一个美观的、优越的、万无一失的选项,可以用最小的填充来完成,但在5还不够的情况下也能工作。下面是选项卡解决方案失败的示例

使用列登录,不使用彩色图形。

完全成功!稍后会尝试更新。

EN

Stack Overflow用户

发布于 2018-12-22 04:59:47

下面的bash函数是我的解决方案,用于将git日志列与包装文本对齐,并支持颜色和图形线。

特性

  • 支持Linux以及macOS/BSD变体。
  • 为支持已安装操作系统和sed版本的最低公共分母而编写的。
  • 自动确定提交散列的适当宽度。
  • 使用或不使用以下git日志选项:--颜色、图形、名称-状态。
  • 通过从包装文本中移除图形线上的颜色来保持图形行的列对齐性,同时使用- color选项。所有其他颜色都变了。
  • 在消息列下面显示标记和其他参考标记,并保留它们的颜色。

已知问题

  • 由于我同时支持sed的BSD和linux变体,所以符合posix的($) sed命令没有利用back引用。这是因为BSD不支持该模式中的反向引用。因此,我使用了4,而不是使用sed命令从图形行中删除颜色。

对于非图形选项,需要:GIT1.9.5或更高版本。当图形出现时,需要比1.9.5略新的版本来正确对齐包装文本

未来改进

  • 自动调整作者的列宽。
  • 考虑另一种解决方案,通过根据所遇到的控制字符数来调整对齐,而不是删除颜色代码,从而保持图形线的颜色。

巴什码

代码语言:javascript
复制
function gitlog
{

   # Check for the existence of certain git log arguments 
   local using_graph=false
   local arg_i
   for arg_i in "$@"
   do 
      if [ "$arg_i" = "--graph" ]; then
         using_graph=true
      fi 
      if [ "$arg_i" = "--oneline" ]; then
         printf -- "--oneline not allowed for this shell function.\n  use regular git log for oneline.\n" 
         return 1
      fi 
   done

   # Set the OS dependant options
   # my_system_type can be set by sourcing my .bashrc
   local sed_regex_sym="r"
   if [ ! -z "$my_system_type" ]; then
      if [ "$my_system_type" = Mac ]; then
         sed_regex_sym="E"
      fi
   fi

   # Set the pre-determined column sizes here
   local N_AUTH_DATE_CHARS=11
   local N_AUTH_NAME_CHARS=15

   # Determine where to place columns for the various content
   local scrn_sz_arr=( $(stty size) )  #stty not tput in case of missing TERMINFO
   local N_SCRN_COLS=${scrn_sz_arr[1]}
   local N_HASH_CHARS=$(git log --pretty=format:%h -1 | tr -d '\n' | wc -c | tr -d '[:space:]')
   if [ "$using_graph" = true ]; then 
      local N_GRPH_MAX_CHARS=$(expr $N_SCRN_COLS / 5) #Use no more then 20% of screen
      local N_GRPH_ACT_CHARS=$(git log --graph --pretty=format:"%<(1,trunc)%ad" | tr -d '..' | \
                               awk '{print length}' | sort -nr | head -1 | tr -d '[:space:]')
      if [ "$N_GRPH_ACT_CHARS" -lt "$N_GRPH_MAX_CHARS" ]; then
         local N_GRPH_RSRV_CHARS=$N_GRPH_ACT_CHARS
      else
         local N_GRPH_RSRV_CHARS=$N_GRPH_MAX_CHARS
      fi
      #Extend space of N_HASH_CHARS to keep alignment when --graph option used.
      N_HASH_CHARS=$(expr $N_HASH_CHARS + $N_GRPH_RSRV_CHARS)
   fi
   local N_MSG_INDENT_CHARS=$(expr $N_HASH_CHARS + 1 + $N_AUTH_DATE_CHARS + $N_AUTH_NAME_CHARS + 1)
   local N_STAT_INDENT_CHARS=$(expr $N_MSG_INDENT_CHARS + 2)

   # Check that there is sufficient room to place all the content
   local N_MIN_COLS=$(expr $N_STAT_INDENT_CHARS + 12)
   if [ "$N_MIN_COLS" -gt "$N_SCRN_COLS" ]; then
      printf -- "Terminal window too narrow.\nNeed at least $N_MIN_COLS cols for this mode.\n"
      return 1
   fi

   # Git log logic below is as follows
   #   - use date=short to minimize space used by that column.  Linked with 
   #        N_AUTH_DATE_CHARS
   #   - use --pretty-format to specify columns as 
   #        hash--author date--author name--commit message.
   #   - first 4 sed statement removes color (if present) from graph lines 
   #        containing wrapped pretty-print text. Done for aligment of wrapped 
   #        text. Was one sed using back references, but that was not compatible
   #        with BSD (OSX) sed with -E.
   #   - fifth sed blanks out graph lines from newlines that were introduced 
   #        between commits by tformat.
   #   - sixth sed aligns wrapped text from pretty-print into message column.
   #   - last three sed statements insert a tab as a delimiter (recall tabs 
   #        removed by expand) between any other text and the --name-status text.
   #        This is in anticipation of the awk statement at the next pipe 
   #   - awk used to right pad the first column up to N_STAT_INDENT_CHARS and
   #        place any --name-status fields present to its right in order to 
   #        align it with the message text.
   #   - use tr to delete the inserted tabs that were used for column alignment
   #   - sed to remove any trailing white space.
   #   - awk removes extraneous blank lines
   #   - finally pipe to less -R to force the color option
   git log \
           --date=short \
           --pretty=tformat:"%C(auto)%w($N_SCRN_COLS,0,$N_MSG_INDENT_CHARS)%>|($N_HASH_CHARS)%h%x09%<($N_AUTH_DATE_CHARS,trunc)%ad%<($N_AUTH_NAME_CHARS,trunc)%an%x09%C(bold blue)%s%C(auto)%+d" \
           $* | \
           expand -t 1 | \
           sed -$sed_regex_sym $'/.*[*]/!{/.*[|]{1}/s/\x1b\\[([0-9](;[0-9])*)*[mGK]([_])\x1b\\[([0-9](;[0-9])*)*[mGK]/_/g;}' | \
           sed -$sed_regex_sym $'/.*[*]/!{/.*[|]{1}/s/\x1b\\[([0-9](;[0-9])*)*[mGK]([|])\x1b\\[([0-9](;[0-9])*)*[mGK]/|/g;}' | \
           sed -$sed_regex_sym $'/.*[*]/!{/.*[|]{1}/s/\x1b\\[([0-9](;[0-9])*)*[mGK]([\\])\x1b\\[([0-9](;[0-9])*)*[mGK]/\\\/g;}' | \
           sed -$sed_regex_sym $'/.*[*]/!{/.*[|]{1}/s/\x1b\\[([0-9](;[0-9])*)*[mGK]([\/])\x1b\\[([0-9](;[0-9])*)*[mGK]/\//g;}' | \
           sed -$sed_regex_sym "/^[|[:space:]]+[^[:alnum:]\\\/]+$/s/[|]//g" | \
           sed -$sed_regex_sym "/^[|]/s/^(.{$N_MSG_INDENT_CHARS})[[:space:]]*/\1/" | \
           sed -$sed_regex_sym "s/^([[:space:]_|\\\/]{0,$N_HASH_CHARS})([A-Z][[:space:]])/\1$(printf -- '\t')\2/" | \
           sed -$sed_regex_sym "s/^([[:space:]_|\\\/]{0,$N_HASH_CHARS})([R][0-9][0-9][0-9][[:space:]])/\1$(printf -- '\t')\2/" | \
           sed -$sed_regex_sym "s/^([[:space:]_|\\\/]{0,$N_HASH_CHARS})([C][0-9][0-9][[:space:]])/\1$(printf -- '\t')\2/" | \
           awk 'BEGIN{ FS="\t" }{ printf "%-'$N_STAT_INDENT_CHARS's%s\n", $1, $2 }' | \
           tr -d '\t' | \
           sed -$sed_regex_sym 's/[[:space:]]*$//' | \
           awk NF | \
           less -R --chop-long-lines
}

示例输出

  1. 彩色图

  1. gitlog --颜色--名称-状态

票数 0
EN
查看全部 5 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46229359

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档