Proper usage of branching in [hg]mercurial

Started by
11 comments, last by Servant of the Lord 10 years, 9 months ago

Hey, so, after years and years of avoiding revision control software because of the (admittedly minor) learning curve and misunderstandings on my part, I started using Mercurial earlier this year, and really find it easy to use, easy to understand, and overall really pleasant and friendly. That is, the command-line interface. The TortiousHG interface got in my way too much and I kept stumbling over it*.

I've committed 113 times already (Woot!) I commit at least once a day, but sometimes twice or more, whenever I come to logical points to commit.

Where I've had trouble is with the idea of branching. I understand the concept, and I understand why it would be useful, but suppose I have several development branches: "Development" (or 'trunk' or 'main' or whatever the correct term is) , "ExperimentalFeatureX" and "UnfinishedFeatureY".

Suppose I'm currently in UnfinishedFeatureY working on it. If I fix a bug that's unrelated to FeatureY, I want to make sure that bug is fixed in Development. How can I merge just that one bug fix (or just that one file) into Development?

Here's another situation that worries me. I have all my art assets in the revision control system also. What if I'm working in ExperimentalFeatureX, add some new art, then cancel the experimental branch and close it, forgetting my art assets are in there? Is there a way I can add certain files to every active branch at the same time?

*I always have a weird relationship with tools - I want X amount of control, but don't want Y amount of complexity. Many tools dumb things down so much that the control I want either is blocked (with the tool taking control for itself), or hidden behind dozens of GUI commands that make it more complex to accomplish what should be (and is) an easy task.

Advertisement

I'm no Distributed Version Control System expert, but in my experience with git, I tend to use "main" branches in a way that sort of facilitates the idealized waterfall model of development -- that is, I have a stable, test, and merge branch that run in parallel through the life of the project -- and feature work is branched off individually from the merge branch. So, a feature is completed and tested in isolation, is promoted to the merge branch where any changes necessary to merge the feature happen (will branch those off too), that gets committed, and then promoted to the test branch (new tests written, etc), and when all is well there the whole shebang gets promoted to stable. That's just one of many ways you can use DCVSs.

In general, the idea of DCVSs is that you should be creating branches literally all the time, even as a single developer. New feature or experiment? New branch. If you just do a linear progression of code, the benefits of DVCSs tend to fade, but the flexibility of DVCSs really enables you to use whatever workflow you want -- even one that behaves more like a traditional Centralized VCS.

As to art assets -- I believe pretty firmly that the distributed model of version control systems isn't terribly suited for art assets. As such, I actually prefer to keep them separately in a Subversion repository. This is for a couple reasons: Firstly, the centralized checkin-checkout model seems more suited to artists workflow, secondly, few artists really grok the concept of branch-merge and how it affects their workflow, thirdly, an individual artist doesn't need the source artwork and history for all assets and the amount of data is huge. Finally, you don't have to worry about a non-technical person borking your entire source code tree.

throw table_exception("(? ???)? ? ???");

For the artwork, the reason why I have it in the mercurial repository is merely because my entire project is in there. It's a backup of my entire project that can be rolled forward or rolled back. I don't need the history of individual pieces of art, but using two separate source control systems seems like an unnecessary complication that'd confuse me alot! Would one VCS then contain the other VCS's repository? huh.png

I get what you are saying about a feature getting completed in isolation and then merged - that's what I'd like to do... But what seems to happen to me alot is that if I'm working on the feature in a separate branch, and I make a non-feature-related bug fix or add a new helper function to the common/core library that's unrelated to the feature, I need to somehow make that bugfix or function addition be applied to the main branch immediately, instead of when the feature branch gets merged, if it gets merged at all. How do you handle that kind of situation in your workflow?

Personally, I don't find it too confusing to have a separate VCS for art, but I get what you're saying -- personal preferences and all.

I think the main thing with DVCSs is that you really have to commit to a certain worldview, and be pedantic about it. In your example, if you found that you suddenly needed to make a fix that's not really part of the feature work, the "prescribed" approach would be to quickly make a different branch for that thing (from whatever other branch is appropriate), and then go back to your feature branch and pull that change you made back down into the feature branch. I forget exactly how you do this, but that's the workflow that hg/git pros would probably recommend.

throw table_exception("(? ???)? ? ???");

In your example, if you found that you suddenly needed to make a fix that's not really part of the feature work, the "prescribed" approach would be to quickly make a different branch for that thing (from whatever other branch is appropriate), and then go back to your feature branch and pull that change you made back down into the feature branch.

So, I'm in 'FeatureX'. I have to:
- Commit my current changes in FeatureX (if I forget this step, and I lose work) (hg commit -m "saving my progress")
- Switch over to the stable branch (hg update StableBranch)
- Branch the stable branch into TinyFixQ (hg branch TinyFixQ)
- Change one line of code
- Commit the change (and remember to --close-branch) (hg commit -m "Fixed the thing" --close-branch)
- Change over to the stable branch again (hg update StableBranch)
- Merge TinyFixQ into the stable branch (hg merge TinyFixQ)
- Commit the stable branch (hg commit -m "applied the fix to the stable branch")
- Change back to FeatureX (hg update FeatureX)
- Pull TinyFixQ into FeatureX (hg merge TinyFixQ)
- Commit to FeatureX (hg commit -m "applied the fix to the developmental feature")

And then be back on my merry way! Did I miss a step? You can why I find it easier to just develop on a single branch.

There has to be a more succinct way to do it - I'm sure it's just my inexperience with Mercurial that makes it require so many steps.

Maybe I can use the graft command for this?

- Commit my current changes in FeatureX (if I forget this step, and I lose work) (hg commit -m "saving my progress")

- Write the bugfix

- hg commit -m "Fixed bug" (and remember the revision number)

- hg update StableBranch

- hg graft <revision-number>

- hg commit -m "applied the fix to the stable branch"

- hg update FeatureX

Does that work? Did I get it correct? If so, it saved me a number of steps... but is still too many.

Why can't I just go:

- Commit my current changes in FeatureX (if I forget this step, and I lose work) (hg commit -m "saving my progress")

- Write the bugfix

- hg commit -m "Fixed bug" (and remember the revision number)

- hg graft <revision-number> StableBranch --and-commit (grafting and committing the single revision to a different branch than the one I am in, without me having to change between branches)

The red underlined part is stuff I'm making up. Are there any equivalent commands?

I lack the hg-fu to tell you how to do this easily, but as best as I understand, the whole idea of fast branch and merge is that you have easy atomic sandboxes, and you're meant to use them. They're argued to be lightweight enough that its not a hassle. Its more or less a matter of policy just how related a change has to be to be considered part of an existing atomic unit or its own, but if by whatever standard it is "different" then it ought to be in a different branch logically, otherwise your history is a tangled mess of things.

By that same token, you shouldn't ever be making "intermediate" commits with partially complete features to save your progress -- if you're doing that, you're using the tool in a fundamentally wrong way. One which is not much better than a glorified backup. A commit is all or nothing, it implements a some subset of code completely. You might break a large functional area down (possibly using sub-branches, even) into smaller pieces, but they're always "complete" for some definition of complete -- a commit should never represent an interruption.

If you have some misunderstanding about how to best use these tools, you wouldn't be the first. I'm certainly no authority. But the way of working that's prescribed by DVCSs is markedly different than Centralized VCSs, and to most simple ways that people have collaborated in the past. It sometimes seems completely insane to have to do all these little steps up front to do seemingly simple things, but the argument is that the cost you pay now is less than the delayed cost of heavy branches and endless merge conflicts under the old paradigm.

throw table_exception("(? ???)? ? ???");

I think the workflow could be more like this, that would prevent polluting the repository with temporary things:

hg shelve

hg up stable

edit in the fix

hg commit

hg up devel

hg unshelve

hg commit

hg merge

Btw., branches in mercurial are like svn branches and are meant to be long running things which are not removed, like for example the default branch for developing, a stable branch for the next oldest version which just gets bugfixes or similar. For adding a single feature or bugfix you can use bookmarks, which are more similar to git branches and can be deleted when you dont need them anymore.

I think the workflow could be more like this, that would prevent polluting the repository with temporary things:

Slightly altered from actual quote:

hg shelve
hg update Stable
edit in the fix
hg commit
hg update ExperimentalFeatureX
hg unshelve
hg commit
hg merge


I'm not sure I understand. ('shelve' is a Mercurial extension)

'shelve' just stores away what I was working on without committing, right?

Why not just commit?

And how does the bugfix or feature addition that I'm making to Stable get back into ExperimentalFeatureX at the same time? I want to commit changes made to a specific file into both branches.

Also, what's that merge for at the end? I'm not wanting to merge ExperimentalFeatureX into Stable - at least not yet. Or are you merging Stable into ExpermentalFeatureX, to bring ExperimentalFeatureX up to date? Huh, I *think* I get that.

But why use shelve at all? Why not just:

  1. hg commit
  2. hg update Stable
  3. edit in the fix
  4. hg commit
  5. hg update ExperimentalFeatureX
  6. hg merge

commit means you're putting that interruption into your history. That's not something you want to preserve; right now you care about coming back to that point in your would-be "history" so you can pick back up on feature work, but tomorrow or 6 months from now you're never going to want to come back into the point where you were interrupted. Every time you commit, you're creating a mile-marker that you can return to should ever the need arise. You don't want to litter the roadside with irrelevant markers -- its particularly harmful if you wanted to go back in history to a point *just before* the next feature you add, which you would logically assume is commit X-1 -- but committing the interruption would actually make it commit X-2.

shelve gives you the same kind of workflow without polluting your history.

throw table_exception("(? ???)? ? ???");

Yes, thats why I was suggesting this.

The merge at end could be different things depending on how you want to continue. Possibly merging the fix and then the feature into default development branch, merging the feature into the stable (though that would be a little weird naming for a branch where you add new features) or merging the fix into the feature branch at a logical point when/if you need it there to continue developing that feature. Not completely sure if that works on mercurial but you could also merge over the fix before unshelving to have the branches a little less intermingled.

This topic is closed to new replies.

Advertisement