|
|
Motley: Branches are too complicated. The last thing we need is a copy of the code that has to be maintained in two or more places! |
|
|
Maven: Branches are source code copies with a tie back to a mainline set of code that allow for easy integration between copy and original set (and vice versa). Branches bring a level of isolation from mainline as well as keep the quality of mainline (and the daily builds) high. |
______________________________
[Context: The company has been having lots of trouble with build breaks in mainline of late. General quality of the product mid-way through the development cycle is lower than expected]
Motley: Somebody is going to get crazy glue stuck to their cup! The quality of our daily builds has been crap lately. Too many people checking in lousy code. Too may conflicts. Not enough time to get our mainline build stable and of high quality before the next check-in goes in. Something has to be done.
Maven: Crazy glue? Ouch. If some moron did that to me I would pay them back big time - when they least expect it. Anyway, I agree with you - having a daily build of poor quality slows down everyone. The key is to isolate all the feature teams from one another giving them freedom to check-in yet keep the main build high quality.
Motley: Thanks for stating the obvious, Mave. Your haircut sucks. Now I stated the obvious too.
Maven: I thought I fixed that crooked sideburn! Ugh. There is a good way to accomplish this isolation. If you have a good source control system (like we do) such as Perforce or CVS, you can leverage a branch.
Motley: Branches are for trees. What do they have to do with source code? Should I start working outside in the shade?
Maven: Think of a branch as a more-involved copy of a collection of source code.
Motley: Why would I want to make a copy? Copies are fraught with problems, such as keeping multiple copies synchronized. And to think, I was expecting a good idea from you. Although pigs don't fly quite yet.
Maven: The digs keep coming! A branch is more than a copy. When you branch a source tree, you still maintain the tie to the original source code. Think of it like the trunk that a tree branch is connected to. You can bring forward changes in mainline to your branch, and bring back changes in your branch to mainline relatively easily. You can even work on the same source files in both places and merge the changes together in either direction.
Motley: And how would I do that? Keep them tied together, I mean.
Maven: You bring code forward to your branch from mainline and back to mainline from your branch through an integration, or more specifically, a forward integration (FI) and reverse integration (RI) respectively. Here is an example of a tree structure I used in a previous role.

Mainline: The main source code depot where daily builds take place from
Feature Branches: As many of these as necessary to support isolated feature teams
Feature Sub-branches: Use only as necessary (e.g. a developer making major changes as part of a feature team)
Motley: That looks more complicated your morning routine! Do the benefits of a structure like that really outweigh the costs of doing all those integrations?
Maven: As the consultant always says, "it depends". For a small overall team where not too many people are checking in to mainline, perhaps the structure is too much overhead. However, for larger teams, branches bring a level of isolation allowing features teams the freedom to do what they need to get the job done. In addition, a central build team could maintain mainline and have a very high quality bar for check-ins to mainline. Teams are only allowed to reverse integrate into mainline when they meet that bar.
Motley: And what does that bar typically involve?
Maven: It depends on the team. For us, we may have a quality gate in place that mandates the following when an RI is done:
- All changes must have been code reviewed
- All changes must have been gone through a test pass
- Test automation is in place for new features (if applicable)
- New features have been stress and performance tested
- No memory leaks have been detected by our tools
- FxCop has been run and is violation-free on all changed code
- Unit tests have been executed and pass at 100%
You can basically go as thorough as you like. As you move up the branch hierarchy, the criteria to check-in gets lighter and lighter so as not to slow down the team. RI operations get stricter and stricter as you get closer to mainline.
Motley: Should every team have its own branch?
Maven: Teams making changes to an isolated part of the code base with relatively few check-ins may not need a branch. Teams working on radical and risky changes or in common parts of the code base would likely benefit from a branch. Keep in mind, though, that the more levels of branching you have, the more overhead there is for integration operations. I recommend trying to stick to 1-2 levels of branching at most.
Motley: What about builds? We lose the advantage of having a central build team generate a daily build?
Maven: Yes, that can be true. Hopefully you can take their infrastructure on a couple of machines that your feature team owns and duplicate the build there so that you have your own daily build and verification going for your feature. For small features, this may not be necessary. For large features, it is a huge benefit as you nail down problems quicker. If a build break happens due to a bad check-in, you catch it long before you hit mainline. As a result, you block fewer people. Everyone wins!
Motley: Ok, Einstein. I have a you now. What if feature team A is working on some changes that feature team B needs to make further progress? You cannot go across branches. Hah!
Maven: You have several options:
- Feature team A can pack up the changes and pass them to Feature team B. Most source control systems support packing up a change list for distribution.
- You can do what's called a baseless merge. I don't recommend doing this as it loses your source history. Essentially you do an integration across branches but lose the tie to mainline. Yuck.
- You can RI the changes of note to mainline with a test pass and then FI them up into Feature team B's branch. This doesn't work as well if the changes are in progress and not fully baked.
Motley: I can think of another problem - my work in my branch can get out-of-date with what is going on with other teams. That may cause problems later.
Maven: True. The onus is on the feature team to ensure it FIs on a regular basis to keep up. Additionally, teams should not "go dark" from mainline for too long. Regular RIs to mainline should be happening, say, at the end of each sprint. For feature teams practicing Scrum, the code at the end of each sprint should be of shippable quality and can be RId back to mainline.
Motley: Also, merging in conflicting changes could be a real pain!
Maven: Yes, but not if you have the right tools. A good merge tool automatically merges isolated changes in the same file (the majority case), and allows you to easily resolve conflicting changes.
Motley: You seem to have an answer for everything. Let me see if I understand this branching thing:
- Branching helps keep mainline quality high
- A feature teams considers a branch to isolate its changes
- Branching provides more freedom for check-ins
- Quality gates are more strict the closer your branch is to mainline
- Branching ensures you don't lose ties to the original source code via integrations
- A good tool makes integrations and merges relatively painless
Maven: Nice summary. Why don't you go approach our beloved development manager and propose that our team undertake a branching model?
Motley: I think I'll do just that.
______________________________
James' Pointer: I am always amazed at how little discussion there is around source code branching. Branches are an incredibly useful software tool employed all over Microsoft for the reasons discussed above. Branches are particularly useful when you are an agile team in a large waterfall-based organization. They give you that level of isolation needed to follow your own effective processes yet still merge your changes in with mainline on a regular basis. Our team would be much less efficient without branches.
Maven's Resources: