【问题标题】:Find directories that had changes between commits查找在提交之间发生更改的目录
【发布时间】:2017-10-30 15:12:02
【问题描述】:

我正在设置一个 jenkins 作业,它遍历项目中的目录,并在每个目录中执行类似的操作。

目前,由于我的目录数量很少,因此浏览所有目录并不是什么大问题。但我只想遍历在GIT_PREVIOUS_COMMITGIT_COMMIT 之间进行更改的目录。

我目前的工作是做什么的:

for dir in */
do
  cd $dir
  # test a script exists
  # execute the script
  # upload the result.zip to an S3 bucket
  cd ..
done

【问题讨论】:

    标签: git jenkins subdirectory git-diff


    【解决方案1】:

    您可以将git diff--name-only 选项一起使用。这将列出更改的文件:

    $ git diff --name-only GIT_COMMIT..GIT_PREVIOUS_COMMIT
    
    app/controllers/some_controller.rb
    app/models/some_model.rb
    app/views/some/show.html.erb
    app/views/some/index.html.erb
    

    然后,您可以将其通过管道传递给 sed 以删除文件名。然后 sort & uniq 以获得您感兴趣的目录的唯一列表:

    sed 's,/*[^/]\+/*$,,' | sort | uniq 
    
    app/controllers
    app/models
    app/views/some
    

    【讨论】:

    • 我做了这个,稍作改动,使用cut,因为这样会更好。 git diff --name-only ${GIT_COMMIT}..${GIT_PREVIOUS_COMMIT} | cut '/' -f 1 | sort | uniq
    【解决方案2】:

    您可以为此类活动添加自己的别名。它非常方便!

    在你的 .gitconfig 中添加这些:

      38   # Difference between HEAD and HEAD^
      39   dbh = "!git ls --numstat -1"
      40   # Difference between two commits
      41   dbc  = "!f() { git diff --name-status $1 $2; }; f"
    

    然后您可以将它们用作:

     git dbc ab68a12 fc83334 # for diff between two commits.
    

    git dbh # for diff between previous and current HEAD.
    

    【讨论】:

    • Ls 是另一个别名,我必须以漂亮的格式显示日志。您可以简单地将 ls 替换为 log --decorate --date=relative
    猜你喜欢
    • 2016-09-29
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 2015-03-17
    • 2011-08-17
    • 1970-01-01
    相关资源
    最近更新 更多