问题
Suppose I have a project implemented in a specific programming language whose use is fragmented into two (or more) versions, for any reason.
These two versions provide different mechanisms to implement some functionality of the project. Thus, the same code is not portable between versions.
How to organize this situation in a git repository? Nevertheless, Keep directories for specific versions within a branch? Duplicates branchs in the same repository? Or use different repositories for each version?
回答1:
It depends a bit on the programming language, but if the difference between the two versions results in a significant difference in your code, then I would want to:
- define an interface representing the functionality whose implementation needs to differ.
- write two implementations of that interface.
- select the right one either at build time or run time, as appropriate to the language and the project.
Once you've separated the two different implementations, you can keep them in the same branch just as you'd keep any two different implementations of the same interface in a branch.
For a trivial difference, for instance if all you need is to pass slightly different flags to some function, then I'd probably not go to all that trouble. Instead I'd just do the equivalent of:
#if NEW_PLATFORM
// enable useful new flag 2
#define FLAGS 0x11
#elif OLD_PLATFORM
// we can live without flag 2 if it's not supported
#define FLAGS 0x1
#else
#error what platform even is this?
#endif
But beware, insignificant differences can grow as the code develops, and you end up with a mess of platform detection code. You should aim to test the platform in at most one place, or even better just control the whole thing with a build option. In the above case you could call that USE_USEFUL_NEW_FLAG
rather than explicitly coding that it depends on a particular platform version. Then it's up to your build configuration to know which platforms support which features.
回答2:
You can attain this with branches. You need to determine during a merge what can and cannot change between them.
git merge --no-commit
will do the merge but not do the final commit step. This allows you to investigate if the solution still works. Once you have it working, complete the merge with
git merge
there should be the default message there. You can leave it as is or add more information about what you did to keep this specific version working.
来源:https://stackoverflow.com/questions/8882503/what-is-the-proper-way-to-maintain-a-project-that-meets-two-versions-of-a-platfo