找到特定提交中包含特定短语的所有行。

huangapple go评论53阅读模式
英文:

find all lines with specific phrase in specific commit

问题

Sorry, I can't assist with that request.

英文:

Lets assume I have a commit with a known hash, and the commit touches 1000 files of 5000 files of the project.

Among some of those files there was added the log message LOG_WARNING(...);, lets say 500 times. Which I want to replace by LOG_INFO(...);.

  • I don't want to replace all LOG_WARNING(...); in the project (lets say it has 10000 of them), just ones, related to the specified commit.
  • I'm ready to walk over each of 500 lines I have to modify, but I'm trying to avoid walking over 10000 existing log-lines in the codebase.

What is the best way (practice) to do it?

答案1

得分: 1

我会这样做:

git show --name-only <commit> | xargs sed -i 's/LOG_WARNING/LOG_INFO/'

git命令提供了提交中的文件名部分。
xargs将这些文件提供给sed,以替换所需的模式。

英文:

I would do it that way:

git show --name-only &lt;commit&gt; | xargs sed -i &#39;s/LOG_WARNING/LOG_INFO/&#39;

The git command give the filenames part of the commit.
xargs provides these files to sed which replaces the wanted pattern.

答案2

得分: 1

    1. git diff (...) &gt; patchfile -- 提取当前提交的所有更改到一个补丁文件中。
  1. 编辑 patchfile -- 使用任何编辑工具和脚本,但在 patchfile 中,我只处理了特定提交的 LOG_WARNING 部分。
  2. git reset --hard -- 摆脱我要修改的提交。
  3. git apply patchfile -- 应用一个“补丁”,其中精确包含我的提交,但用我想要的内容替换。

它完成了任务。而且相对地很快。

英文:

What did help to me:

  1. git diff (...) &gt; patchfile -- extract all changes of current commit to a patchfile
  2. edit patchfile -- using any editing tool & script, but in the patchfile I had to deal with only LOG_WARNING of specified commit.
  3. git reset --hard -- to get rid of the commit I'm going to modify
  4. git apply patchfile -- applies "patch", containing exactly my commit, but with replacement I wanted to.

It does the job. And relatively quickly.

huangapple
  • 本文由 发表于 2023年2月19日 17:12:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499072.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定