英文:
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 (".ino" file), I have saved my Wifi credentials. Using git commit, my personal Wifi information should be replaced by general information. Therefore I'm using the "clean/smudge filter" 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 "attributes" 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 'git config' configuration with a filter rule which only replaces the "WIFI_SSID". After deleting the local 'git config' filter configuration, it works with the global 'git config' configuration.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论