【问题标题】:How to get total number of file changes in each directory using git diff如何使用 git diff 获取每个目录中的文件更改总数
【发布时间】:2020-11-12 04:30:54
【问题描述】:

我想知道每个父目录中更改的文件总数。这是我当前的 git diff 结果:

git diff HEAD^ HEAD
aws_app1_service/app/models/config.ini
aws_app1_service/app/views/api.py
aws_app1_service/app/api.py
aws_app2_service/config/config.ini
aws_app2_service/k8s_resources/deployment.yaml
aws_app2_service/tests/steps/test_api_steps.py
aws_app2_service/tests/test_api2_steps.py
aws_app3_service/index.html
aws_app4_service/container.yaml

谁能告诉我如何使用 git 命令为每个目录获取单独的文件更改数量,如下所示?

aws_app1_service: 3 
aws_app2_service: 4
aws_app3_service: 1
aws_app4_service: 1

提前致谢

【问题讨论】:

  • 请您在答案评论部分回复有关答案是否对您有用的问题,并查看此链接,当有人在 SO stackoverflow.com/help/someone-answers987654321@ 上获得有用的答案时,可以做些什么?

标签: git shell unix


【解决方案1】:

第一种解决方案:请您尝试以下方法。

your_git_command | 
awk '
match($0,/[^/]*/){
  count[substr($0,RSTART,RLENGTH)]++
}
END{
  for(i in count){
    print i,count[i]
  }
}' 

说明:为上述添加详细说明。

your_git_command |                       ##Sending git command output to awk program here.
awk '                                    ##Starting awk program from here.
match($0,/[^/]*/){                       ##Using match function to match regex till / in current line.
  count[substr($0,RSTART,RLENGTH)]++     ##Creating array count with index of sub-string from RSTART till RLENGTH with its count increasing with 1
}
END{                                     ##Starting END block of this code from here.
  for(i in count){                       ##Traversing through count array here.
    print i,count[i]                     ##Printing i and count value here.
  }
}'

第二个解决方案: 上面的解决方案不会关心它们在 Input_file 中的名称顺序,下面的解决方案也会处理它。

your_git_command | 
awk '
match($0,/[^/]*/){
  val=substr($0,RSTART,RLENGTH)
  if(!a[val]++){
    b[++count]=val
  }
  value[val]++
}
END{
  for(i=1;i<=count;i++){
    print b[i],value[b[i]]
  }
}' 

说明:为上述添加详细说明,仅供说明之用。

your_git_command |                     ##Running git command here and sending it to awk command.
awk '                                  ##Starting awk code from here.
match($0,/[^/]*/){                     ##Using match function to match everything till / in current line.
  val=substr($0,RSTART,RLENGTH)        ##Creating val which has sub-string of current line.
  if(!a[val]++){                       ##Checking condition if val is NOT coming in a then do following.
    b[++count]=val                     ##Creating array b with index count increasing value of 1 and setting its value to val here.
  }
  value[val]++                         ##Creating array value with index val with its increasing it with 1 value.
}
END{                                   ##Starting END block of this code here.
  for(i=1;i<=count;i++){               ##Running for loop from 1 to till count.
    print b[i],value[b[i]]             ##Printing array b with variable i as index AND value with index of b[i]
  }
}' 

【讨论】:

    【解决方案2】:

    你已经知道 git 并不关心文件夹,git track changes to path 它打印出更改的完整路径。

    使用diffstatus 或任何其他命令将打印出完整路径。

    您应该编写一个脚本来获取此输入并打印出所需的文件夹。

    这可以使用基本循环或任何其他编程语言来完成。

    【讨论】:

      【解决方案3】:

      试试:

      git diff HEAD^ HEAD  | cut -d'/' -f1 | sort |  uniq -c  
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-14
        • 1970-01-01
        • 2011-02-16
        • 2015-01-11
        • 2011-08-03
        相关资源
        最近更新 更多