Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

merge - Making Git retain different section content between branches

I'm developing a Userscript that my employers have asked me to begin to manage via Git.

Right now, I have a stable file and a beta file, so that everyone in the organization can install the stable code but can choose to help test the beta additions instead, if they want. Some portions of that file should remain different, the content and changes should not be merged between branches.

For example, if I convert the Beta file to a Git Branch, and later decide that the Beta changes are stable and merge the Beta back into the Stable code (which will not have changed) the Git Merge process as I understand it will "helpfully" update the Stable Greasemonkey definition headers based on whatever values are on those lines in the Beta branch. This is thoroughly undesirable, as these headers contain an auto-update URL that Greasemonkey will check for updates.

// ==UserScript== (stable)
// @downloadURL  --  StableURL File Location
// ==/UserScript==

// ==UserScript== (beta)
// @downloadURL  --  BetaURL File Location
// ==/UserScript==

>Git Merge<

// ==UserScript== (stable)
// @downloadURL  --  BetaURL File Location
// ==/UserScript==

I want to retain the ability to have distinct URLs between the Beta code and the Stable code, but have not been able to identify a method to make Git's merge process ignore the lines that Greasemonkey needs to do its thing properly, but if I don't have the Beta as a separate Branch, I'm not sure how to use Git to easily migrate changed code from Beta to Stable, which is the stated reason for asking me to adopt Git functionality. (Well, the other reason is to make it easier for others to contribute to and identify the history of the project...)

Any help is much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

All your changes should be merged except changes to those values, which makes them not like the others, not changes to intrinsic content but deployment-specific changes. Those might be best applied in the post-checkout hook. Here's a sample, a per-branch include processor

cat <<EOF >.git/hooks/post-checkout
#!/bin/sh
if branch=`git symbolic-ref HEAD --short -q`; then
    for file in `git ls-files -cix*.@branch`; do
        echo "* making ${file%.@branch} from $file with branch-specific includes"
        echo '/^@include-branch-specific ([a-z/]*)$/ { s//cat 1.'$branch'/e }' 
        | sed -rf- $file >${file%.@branch}
    done
fi
EOF
chmod +x .git/hooks/post-checkout
# testing
git checkout beta

cat  <<EOF >config.@branch
// ==UserScript==
@include-branch-specific config
// ==/UserScript==
EOF

echo >config.stable '// @downloadURL  --  StableURL File Location'
echo >config.beta   '// @downloadURL  --  BetaURL File Location'

git add config.*
# git rm --cached config
git commit -m'setting up per-branch configs'
git checkout

git checkout stable
git cherry-pick beta
git checkout

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...