分支之间的git smudge / clean filter [英] git smudge/clean filter between branches
问题描述
具体来说,我已经阅读了大部分这些答案的页面链接回来:
p>tl ; dr
这是一个详细的问题,但总结如下:
- Can我将一个分支上的
DEBUG = false
存储在一个文件中,另一个分支中存储DEBUG = true
,使用smudge / clean过滤器来管理该文件?如何?
背景
我在bitbucket上有各种远程回收站。我在Win8上使用SourceTree,将远程回购克隆到我的笔记本电脑上。我为开发,功能,发布等创建了不同的分支(遵循一个成功的Git分支
我有一个名为 Dbug.java
的Android java类,包含一个布尔值,用于打开/关闭各种调试日志,嘲笑我的代码中的功能。
public static final boolean DEBUG = false ;
我想这个值是 false
在我的生产(主)分支上,并且在我的功能分支上是 true
。
- 这是可能使用过滤器,还是我已经误解了用例?
- 我不确定过滤器是否像这样在2个分支之间工作相同的本地托管回购,或者过滤器只能在2次回购之间进行。
创建过滤器
在本地工作时,我检查了生产分支。我创建了一个名为 debug_flag.txt
的测试文件,内容如下:
//生产分支上的false
//其他分支上的true
DEBUG = false;
我在本地仓库的根目录下创建了一个名为 .gitattributes
并添加过滤器引用:
debug_flag.txt filter = debug_on_off
code>
我使用过滤器定义更新了 .git / config
文件:
[filterdebug_on_off]
clean = sed -e's / DEBUG = true / DEBUG = false /'
smudge = sed -s's / DEBUG = false / DEBUG = true /'
- 在我的理解中,这应该确保我的文件在生产中总是有一个
的false值,但是当我从
生产分支时,它将具有真实值。 - 这是正确的理解吗?
b
测试过滤器
我使用以下命令创建了一个新分支 test
git checkout -b test
我检查了我的文件内容:
$ cat debug_flag.txt
//生产分支上为false
//其他分支上为true
DEBUG = false;
- 我期望看到
true
- 检出文件时不应该运行smudge过滤器吗?
我在文件中添加了一行,并提交。然后我切换回生产分支,这就是奇怪的地方。
如果我在SourceTree中查看文件,该分支自创建以来没有任何更改。这是我所期望的,因为唯一的变化是在另一个分支上进行的。
如果我在终端或Notepad ++中查看文件,我会看到我的值已改变:
$ cat debug_flag.txt
//制作分支上的false
//在其他分支上为true
DEBUG = true;
我还没有从测试分支合并更改,我还没有提交生成分支,但文件已更改。
- 它看起来像是在文件内运行了涂抹过滤器此分支,但不跨越分行。
这个难题,希望它是简单的,可以被有经验的人发现。
我敢打赌,这是对这个概念的一个简单误解。
>请提示您输入缺少的信息...
VonC的回复
设置基本过滤器效果很好。在 config
文件中定义过滤器为:
[filterdebug_on_off ]
clean = sed -e's / DEBUG = true / DEBUG = false /'
smudge = sed -s'/ DEBUG = false / DEBUG = true /'
创建一个新分支修复false - > true,合并返回的改变true - > false。
将更改限制为生产(主)分支需要知道正在运行的分支的自定义脚本。因此 config
文件变为:
[filterdebug_on_off]
clean = ./scripts/master_clean.sh
smudge = ./scripts/master_smudge.sh
master_clean.sh:
#!/ bin / sh
branch = $(git rev-parse --symbolic --abbrev-ref HEAD)
if [master=$ branch];然后
sed -es / DEBUG = true / DEBUG = false / $ 1
else
cat $ 1
fi
master_smudge.sh:
#!/ bin / sh
branch = $(git rev-parse --symbolic --abbrev-ref HEAD)
if [master=$ branch];那么
sed -es / DEBUG = false / DEBUG = true / $ 1
else
cat $ 1
fi
此时,我遇到了SourceTree看到的内容与记事本++中显示的调试文件内容之间的不一致之处。 SourceTree正在显示更改,但Notepad ++不是。
我接受 VonC's答案,因为它回答了我提出的基本问题。 然而,我可能会实施我写的解决方案 ,因为它解决了我试图解决的潜在问题,以一种更简单的方式(对我来说):在不同的分支上保留不同的配置文件。 解决方案
您刚创建的文件一个新的分支,未检出其内容(其内容与您所在的分支相同)
要强制涂抹,请在回购:
git checkout HEAD -
我还没有将测试分支中的变化合并到一起,我没有在生产分支上进行提交,但文件已经发生变化。 p>
这是内容过滤器驱动程序的概念:它修改内容,而不影响 git status
(它仍然会将修改后的文件报告为不变)。
建议通过查看当前分支的名称来调用脚本。
请参阅我的旧回答中的示例最佳实践 - Git + Build自动化 - 保持配置分离。
#!/ bin / sh
branch = $(git rev-parse --symbolic --abbrev-ref HEAD)
There are many related questions involving smudge/clean filters - I have spent some hours reading them, and trying various options, but still failing. I hope I can ask in a way that I get an answer that works for me.
Specifically, I have read the page most of these answers link back to:
tl;dr
Its a detailed question, but the summary is:
- Can I store
DEBUG = false
in a file on one branch, andDEBUG = true
in another branch, using smudge/clean filters to manage that file? And how?
Background
I have various remote repos hosted at bitbucket. I am using SourceTree on Win8, to clone the remote repos to my laptop. I create different branches for development, features, releases etc (following A successful Git branching model for better or worse).
I have an Android java class called Dbug.java
that contains a boolean which turns on/off various debug logging, mocking etc features in my code.
public static final boolean DEBUG = false;
I would like this value to be false
on my "production" (master) branch, and to be true
on my feature branches.
- Is this possible using filters, or have I already misunderstood the use case?
- I am unsure if filters work like this between 2 branches of the same locally hosted repo, or if the filters only work between 2 repos.
Creating the filters
Working locally, I checked out the production branch. I created a test file called debug_flag.txt
with the following contents:
// false on production branch
// true on other branches
DEBUG = false;
I created a file in the root of my local repo called .gitattributes
and added the filter reference to it:
debug_flag.txt filter=debug_on_off
I updated the .git/config
file with the filter definition:
[filter "debug_on_off"]
clean = sed -e 's/DEBUG = true/DEBUG = false/'
smudge = sed -s 's/DEBUG = false/DEBUG = true/'
- In my understanding, this should ensure that my file always has a false value in production, but will have a true value when I branch from production.
- Is this a correct understanding?
Testing the filters
I created a new branch test
using:
git checkout -b test
I checked the contents of my file:
$ cat debug_flag.txt
// false on production branch
// true on other branches
DEBUG = false;
- I expected to see the value
true
in the file - Shouldn't the "smudge" filter have run when I checked out the file?
I added a new line to the file, and committed. I then switched back to the production branch, and this is where things get weird.
If I look at the file in SourceTree, there are no changes on this branch since it was created. That is what I would expect, since the only change was made on a different branch.
If I look at the file in the terminal, or Notepad++, I see my value has changed:
$ cat debug_flag.txt
// false on production branch
// true on other branches
DEBUG = true;
I have not yet merged the change across from the test branch, I have not made a commit on the production branch, yet the file has changed.
- it looks like the smudge filter was run on the file within this branch, but not across branches.
I'm missing a vital piece of the puzzle, and hopefully it is something simple that can be spotted by someone with experience doing this.
My bet is this is a simple misunderstanding of the concept.
Pls prompt for any missing info...
Update based on VonC's reply
Setting up the basic filters worked quite well. Defined the filter in the config
file as:
[filter "debug_on_off"]
clean = sed -e 's/DEBUG = true/DEBUG = false/'
smudge = sed -s 's/DEBUG = false/DEBUG = true/'
Creating a new branch fixes false -> true, merging back changes true -> false.
Confining the change to just the production (master) branch required custom scripts that were aware of the branch they are being run from. So the config
file became:
[filter "debug_on_off"]
clean = ./scripts/master_clean.sh
smudge = ./scripts/master_smudge.sh
master_clean.sh:
#!/bin/sh
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)
if [ "master" = "$branch" ]; then
sed -e s/DEBUG = true/DEBUG = false/ $1
else
cat $1
fi
master_smudge.sh:
#!/bin/sh
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)
if [ "master" = "$branch" ]; then
sed -e s/DEBUG = false/DEBUG = true/ $1
else
cat $1
fi
At this point, I am running into inconsistencies between what SourceTree is seeing, and what is being shown in Notepad++ for the contents of the debug file. SourceTree is showing the changes, but Notepad++ is not.
I am accepting VonC's answer, since it answers the basic question I posed.
However, I will likely be implementing the solution I wrote, since it solves the underlying problem that I am trying to solve, in an easier way (for me): retaining a different config file on separate branches.
I expected to see the value true in the file
You just created a new branch, not checked out its content (sice its content is the same as the branch you were in)
To force the smudge to run, do at the top of the repo:
git checkout HEAD --
I have not yet merged the change across from the test branch, I have not made a commit on the production branch, yet the file has changed.
That is the idea of a content filter driver: it modifies the content, without affecting git status
(which still reports the modified file as "unchanged").
To have a smudge acting differently per branch, I would recommend calling a script which starts by looking the name of the current branch.
See an example in my older answer "Best practice - Git + Build automation - Keeping configs separate".
#!/bin/sh
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)
这篇关于分支之间的git smudge / clean filter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!