使用过滤器在 git 提交期间替换多个信息。

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

Replace several information during git commit using filter

问题

在我的源代码(".ino" 文件)中,我已经保存了我的Wifi凭据。使用git提交时,我的个人Wifi信息应该被替换为通用信息。因此,我正在使用“clean/smudge过滤器”,可以在[这里](https://developers.redhat.com/articles/2022/02/02/protect-secrets-git-cleansmudge-filter#)和[这里](https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes)找到解释。

在我的工作目录中,我的源代码中有以下行:

```c
/* Wifi definitions */
const char* WIFI_SSID = "My personal Wifi"; // Wifi ssid
const char* WIFI_PSK = "Password1234";      // Wifi psk

在使用git提交后,它应该看起来像这样:

/* Wifi definitions */
const char* WIFI_SSID = "WIFI_SSID";    // Wifi ssid
const char* WIFI_PSK = "WIFI_PSK";      // Wifi psk

因此,我在.git/info文件夹中添加了一个名为"attributes"的文件,其内容如下:
*.ino filter=WifiFilter

另外,我将过滤器定义如下:

git config --global filter.WifiFilter.clean 'sed -e "s/My personal Wifi/WIFI_SSID/g" -e "s/Password1234/WIFI_PSK/g"'
git config --global filter.WifiFilter.smudge 'sed -e "s/WIFI_SSID/My personal Wifi/g" -e "s/WIFI_PSK/Password1234/g"'

不幸的是,只有一个过滤器起作用。提交后,它看起来像这样:

/* Wifi definitions */
const char* WIFI_SSID = "WIFI_SSID";        // Wifi ssid
const char* WIFI_PSK = "Password1234";      // Wifi psk

有没有人知道可能是什么问题?


<details>
<summary>英文:</summary>

In my source code (&quot;.ino&quot; file), I have saved my Wifi credentials. Using git commit, my personal Wifi information should be replaced by general information. Therefore I&#39;m using the &quot;clean/smudge filter&quot; which is explained [here](https://developers.redhat.com/articles/2022/02/02/protect-secrets-git-cleansmudge-filter#) and [here](https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes).



In my working directory, there are the following lines in my source code:

/* Wifi definitions /
const char
WIFI_SSID = "My personal Wifi"; // Wifi ssid
const char* WIFI_PSK = "Password1234"; // Wifi psk


After using git commit, it should look like this:

/* Wifi definitions /
const char
WIFI_SSID = "WIFI_SSID"; // Wifi ssid
const char* WIFI_PSK = "WIFI_PSK"; // Wifi psk


Therefore I added in the .git/info folder the file &quot;attributes&quot; with the following content:
`*.ino filter=WifiFilter`.

Additionally, I defined the filters as follows:

git config --global filter.WifiFilter.clean 'sed -e "s/My personal Wifi/WIFI_SSID/g" -e "s/Password1234/WIFI_PSK/g"'
git config --global filter.WifiFilter.smudge 'sed -e "s/WIFI_SSID/My personal Wifi/g" -e "s/WIFI_PSK/Password1234/g"'

Unfortunately, only one filter works. After the commit, it looks like this:

/* Wifi definitions /
const char
WIFI_SSID = "WIFI_SSID"; // Wifi ssid
const char* WIFI_PSK = "Password1234"; // Wifi psk

Does anyone have an idea what it could be?

</details>


# 答案1
**得分**: 1

已解决。它与上述配置一起运行。我的错误是:我有一个本地的'git config'配置,其中有一个过滤规则,只替换"WIFI_SSID"。在删除本地的'git config'过滤配置后,它可以与全局的'git config'配置一起使用。

<details>
<summary>英文:</summary>

Solved. It works with the aboved mentioned configuration. My mistake was: I had a local &#39;git config&#39; configuration with a filter rule which only replaces the &quot;WIFI_SSID&quot;. After deleting the local &#39;git config&#39; filter configuration, it works with the global &#39;git config&#39; configuration.

</details>



huangapple
  • 本文由 发表于 2023年7月18日 15:46:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76710550.html
匿名

发表评论

匿名网友

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

确定