bos@559: bos@559: bos@559: bos@572: bos@559: Managing releases and branchy development bos@559: bos@584: Mercurial provides several mechanisms for you to manage a bos@559: project that is making progress on multiple fronts at once. To bos@559: understand these mechanisms, let's first take a brief look at a bos@559: fairly normal software project structure. bos@559: bos@584: Many software projects issue periodic major bos@559: releases that contain substantial new features. In parallel, they bos@559: may issue minor releases. These are usually bos@559: identical to the major releases off which they're based, but with bos@559: a few bugs fixed. bos@559: bos@584: In this chapter, we'll start by talking about how to keep bos@559: records of project milestones such as releases. We'll then bos@559: continue on to talk about the flow of work between different bos@559: phases of a project, and how Mercurial can help you to isolate and bos@559: manage this work. bos@559: bos@559: bos@559: Giving a persistent name to a revision bos@559: bos@584: Once you decide that you'd like to call a particular bos@559: revision a release, it's a good idea to record bos@559: the identity of that revision. This will let you reproduce that bos@559: release at a later date, for whatever purpose you might need at bos@559: the time (reproducing a bug, porting to a new platform, etc). bos@567: &interaction.tag.init; bos@559: bos@584: Mercurial lets you give a permanent name to any revision bos@559: using the hg tag command. Not bos@567: surprisingly, these names are called tags. bos@567: bos@567: &interaction.tag.tag; bos@559: bos@584: A tag is nothing more than a symbolic name bos@559: for a revision. Tags exist purely for your convenience, so that bos@559: you have a handy permanent way to refer to a revision; Mercurial bos@559: doesn't interpret the tag names you use in any way. Neither bos@559: does Mercurial place any restrictions on the name of a tag, bos@559: beyond a few that are necessary to ensure that a tag can be bos@559: parsed unambiguously. A tag name cannot contain any of the bos@559: following characters: bos@559: bos@584: Colon (ASCII 58, bos@559: :) bos@559: bos@584: Carriage return (ASCII 13, bos@559: \r) bos@559: bos@584: Newline (ASCII 10, bos@559: \n) bos@559: bos@559: bos@584: You can use the hg tags bos@559: command to display the tags present in your repository. In the bos@559: output, each tagged revision is identified first by its name, bos@559: then by revision number, and finally by the unique hash of the bos@567: revision. bos@567: bos@567: &interaction.tag.tags; bos@567: bos@584: Notice that tip is listed in the output bos@567: of hg tags. The bos@567: tip tag is a special floating bos@567: tag, which always identifies the newest revision in the bos@567: repository. bos@559: bos@584: In the output of the hg bos@559: tags command, tags are listed in reverse order, by bos@559: revision number. This usually means that recent tags are listed bos@559: before older tags. It also means that tip is bos@559: always going to be the first tag listed in the output of bos@559: hg tags. bos@559: bos@584: When you run hg log, if it bos@559: displays a revision that has tags associated with it, it will bos@567: print those tags. bos@567: bos@567: &interaction.tag.log; bos@559: bos@584: Any time you need to provide a revision ID to a Mercurial bos@559: command, the command will accept a tag name in its place. bos@559: Internally, Mercurial will translate your tag name into the bos@567: corresponding revision ID, then use that. bos@567: bos@567: &interaction.tag.log.v1.0; bos@559: bos@584: There's no limit on the number of tags you can have in a bos@559: repository, or on the number of tags that a single revision can bos@559: have. As a practical matter, it's not a great idea to have bos@559: too many (a number which will vary from project bos@559: to project), simply because tags are supposed to help you to bos@559: find revisions. If you have lots of tags, the ease of using bos@559: them to identify revisions diminishes rapidly. bos@559: bos@584: For example, if your project has milestones as frequent as bos@559: every few days, it's perfectly reasonable to tag each one of bos@559: those. But if you have a continuous build system that makes bos@559: sure every revision can be built cleanly, you'd be introducing a bos@559: lot of noise if you were to tag every clean build. Instead, you bos@559: could tag failed builds (on the assumption that they're rare!), bos@559: or simply not use tags to track buildability. bos@559: bos@584: If you want to remove a tag that you no longer want, use bos@567: hg tag --remove. bos@567: bos@567: &interaction.tag.remove; bos@567: bos@584: You can also modify a tag at any time, so that it identifies bos@567: a different revision, by simply issuing a new hg tag command. You'll have to use the bos@567: option to tell Mercurial bos@567: that you really want to update the bos@567: tag. bos@567: bos@567: &interaction.tag.replace; bos@567: bos@584: There will still be a permanent record of the previous bos@567: identity of the tag, but Mercurial will no longer use it. bos@567: There's thus no penalty to tagging the wrong revision; all you bos@567: have to do is turn around and tag the correct revision once you bos@567: discover your error. bos@559: bos@584: Mercurial stores tags in a normal revision-controlled file bos@559: in your repository. If you've created any tags, you'll find bos@559: them in a file named .hgtags. When you run the hg tag command, Mercurial modifies bos@559: this file, then automatically commits the change to it. This bos@559: means that every time you run hg bos@559: tag, you'll see a corresponding changeset in the bos@567: output of hg log. bos@567: bos@567: &interaction.tag.tip; bos@559: bos@559: bos@559: Handling tag conflicts during a merge bos@559: bos@584: You won't often need to care about the .hgtags file, but it sometimes bos@559: makes its presence known during a merge. The format of the bos@559: file is simple: it consists of a series of lines. Each line bos@559: starts with a changeset hash, followed by a space, followed by bos@559: the name of a tag. bos@559: bos@584: If you're resolving a conflict in the .hgtags file during a merge, bos@559: there's one twist to modifying the .hgtags file: when Mercurial is bos@559: parsing the tags in a repository, it bos@559: never reads the working copy of the bos@559: .hgtags file. Instead, it bos@559: reads the most recently committed bos@559: revision of the file. bos@559: bos@584: An unfortunate consequence of this design is that you bos@559: can't actually verify that your merged .hgtags file is correct until bos@559: after you've committed a change. So if bos@559: you find yourself resolving a conflict on .hgtags during a merge, be sure to bos@559: run hg tags after you commit. bos@559: If it finds an error in the .hgtags file, it will report the bos@559: location of the error, which you can then fix and commit. You bos@559: should then run hg tags bos@559: again, just to be sure that your fix is correct. bos@559: bos@559: bos@559: bos@559: Tags and cloning bos@559: bos@584: You may have noticed that the hg bos@559: clone command has a option that lets you clone bos@559: an exact copy of the repository as of a particular changeset. bos@559: The new clone will not contain any project history that comes bos@559: after the revision you specified. This has an interaction bos@559: with tags that can surprise the unwary. bos@559: bos@584: Recall that a tag is stored as a revision to the .hgtags file, so that when you bos@559: create a tag, the changeset in which it's recorded necessarily bos@559: refers to an older changeset. When you run hg clone -r foo to clone a bos@559: repository as of tag foo, the new clone bos@559: will not contain the history that created the bos@559: tag that you used to clone the repository. The bos@559: result is that you'll get exactly the right subset of the bos@559: project's history in the new repository, but bos@559: not the tag you might have bos@559: expected. bos@559: bos@559: bos@559: bos@559: When permanent tags are too much bos@559: bos@584: Since Mercurial's tags are revision controlled and carried bos@559: around with a project's history, everyone you work with will bos@559: see the tags you create. But giving names to revisions has bos@559: uses beyond simply noting that revision bos@559: 4237e45506ee is really bos@559: v2.0.2. If you're trying to track down a bos@559: subtle bug, you might want a tag to remind you of something bos@559: like Anne saw the symptoms with this bos@559: revision. bos@559: bos@584: For cases like this, what you might want to use are bos@559: local tags. You can create a local tag bos@559: with the option to the bos@559: hg tag command. This will bos@559: store the tag in a file called .hg/localtags. Unlike .hgtags, .hg/localtags is not revision bos@559: controlled. Any tags you create using remain strictly local to the bos@559: repository you're currently working in. bos@559: bos@559: bos@559: bos@559: bos@559: The flow of changes&emdash;big picture vs. little bos@559: bos@584: To return to the outline I sketched at the beginning of a bos@559: chapter, let's think about a project that has multiple bos@559: concurrent pieces of work under development at once. bos@559: bos@584: There might be a push for a new main release; bos@559: a new minor bugfix release to the last main release; and an bos@559: unexpected hot fix to an old release that is now bos@559: in maintenance mode. bos@559: bos@584: The usual way people refer to these different concurrent bos@559: directions of development is as branches. bos@559: However, we've already seen numerous times that Mercurial treats bos@559: all of history as a series of branches and bos@559: merges. Really, what we have here is two ideas that are bos@559: peripherally related, but which happen to share a name. bos@559: bos@584: Big picture branches represent bos@559: the sweep of a project's evolution; people give them names, bos@559: and talk about them in conversation. bos@559: bos@584: Little picture branches are bos@559: artefacts of the day-to-day activity of developing and bos@559: merging changes. They expose the narrative of how the code bos@559: was developed. bos@559: bos@559: bos@559: bos@559: bos@559: Managing big-picture branches in repositories bos@559: bos@584: The easiest way to isolate a big picture bos@559: branch in Mercurial is in a dedicated repository. If you have bos@559: an existing shared repository&emdash;let's call it bos@559: myproject&emdash;that reaches a bos@559: 1.0 milestone, you can start to prepare for bos@559: future maintenance releases on top of version 1.0 by tagging the bos@567: revision from which you prepared the 1.0 release. bos@567: bos@567: &interaction.branch-repo.tag; bos@567: bos@584: You can then clone a new shared bos@567: myproject-1.0.1 repository as of that bos@567: tag. bos@567: bos@567: &interaction.branch-repo.clone; bos@559: bos@584: Afterwards, if someone needs to work on a bug fix that ought bos@559: to go into an upcoming 1.0.1 minor release, they clone the bos@559: myproject-1.0.1 repository, make their bos@567: changes, and push them back. bos@567: bos@567: &interaction.branch-repo.bugfix; bos@567: bos@584: Meanwhile, development for bos@559: the next major release can continue, isolated and unabated, in bos@567: the myproject repository. bos@567: bos@567: &interaction.branch-repo.new; bos@559: bos@559: bos@559: bos@559: Don't repeat yourself: merging across branches bos@559: bos@584: In many cases, if you have a bug to fix on a maintenance bos@559: branch, the chances are good that the bug exists on your bos@559: project's main branch (and possibly other maintenance branches, bos@559: too). It's a rare developer who wants to fix the same bug bos@559: multiple times, so let's look at a few ways that Mercurial can bos@559: help you to manage these bugfixes without duplicating your bos@559: work. bos@559: bos@584: In the simplest instance, all you need to do is pull changes bos@559: from your maintenance branch into your local clone of the target bos@567: branch. bos@567: bos@567: &interaction.branch-repo.pull; bos@567: bos@584: You'll then need to merge the heads of the two branches, and bos@567: push back to the main branch. bos@567: bos@567: &interaction.branch-repo.merge; bos@559: bos@559: bos@559: bos@559: Naming branches within one repository bos@559: bos@584: In most instances, isolating branches in repositories is the bos@559: right approach. Its simplicity makes it easy to understand; and bos@559: so it's hard to make mistakes. There's a one-to-one bos@559: relationship between branches you're working in and directories bos@559: on your system. This lets you use normal (non-Mercurial-aware) bos@559: tools to work on files within a branch/repository. bos@559: bos@584: If you're more in the power user category bos@559: (and your collaborators are too), there is bos@559: an alternative way of handling branches that you can consider. bos@559: I've already mentioned the human-level distinction between bos@559: small picture and big picture bos@559: branches. While Mercurial works with multiple small bos@559: picture branches in a repository all the time (for bos@559: example after you pull changes in, but before you merge them), bos@559: it can also work with multiple big bos@559: picture branches. bos@559: bos@584: The key to working this way is that Mercurial lets you bos@559: assign a persistent name to a branch. bos@559: There always exists a branch named default. bos@559: Even before you start naming branches yourself, you can find bos@559: traces of the default branch if you look for bos@559: them. bos@559: bos@584: As an example, when you run the hg bos@559: commit command, and it pops up your editor so that bos@559: you can enter a commit message, look for a line that contains bos@559: the text HG: branch default at bos@559: the bottom. This is telling you that your commit will occur on bos@559: the branch named default. bos@559: bos@584: To start working with named branches, use the hg branches command. This command bos@559: lists the named branches already present in your repository, bos@567: telling you which changeset is the tip of each. bos@567: bos@567: &interaction.branch-named.branches; bos@567: bos@584: Since you haven't created any named branches yet, the only bos@567: one that exists is default. bos@559: bos@584: To find out what the current branch is, run bos@559: the hg branch command, giving bos@559: it no arguments. This tells you what branch the parent of the bos@567: current changeset is on. bos@567: bos@567: &interaction.branch-named.branch; bos@559: bos@584: To create a new branch, run the hg bos@559: branch command again. This time, give it one bos@567: argument: the name of the branch you want to create. bos@567: bos@567: &interaction.branch-named.create; bos@559: bos@584: After you've created a branch, you might wonder what effect bos@559: the hg branch command has had. bos@559: What do the hg status and bos@567: hg tip commands report? bos@567: bos@567: &interaction.branch-named.status; bos@567: bos@584: Nothing has changed in the bos@559: working directory, and there's been no new history created. As bos@559: this suggests, running the hg bos@559: branch command has no permanent effect; it only bos@559: tells Mercurial what branch name to use the bos@559: next time you commit a changeset. bos@559: bos@584: When you commit a change, Mercurial records the name of the bos@559: branch on which you committed. Once you've switched from the bos@559: default branch to another and committed, bos@559: you'll see the name of the new branch show up in the output of bos@559: hg log, hg tip, and other commands that bos@567: display the same kind of output. bos@567: bos@567: &interaction.branch-named.commit; bos@567: bos@584: The hg log-like commands bos@567: will print the branch name of every changeset that's not on the bos@559: default branch. As a result, if you never bos@559: use named branches, you'll never see this information. bos@559: bos@584: Once you've named a branch and committed a change with that bos@559: name, every subsequent commit that descends from that change bos@559: will inherit the same branch name. You can change the name of a bos@559: branch at any time, using the hg bos@567: branch command. bos@567: bos@567: &interaction.branch-named.rebranch; bos@567: bos@584: In practice, this is something you won't do very often, as bos@567: branch names tend to have fairly long lifetimes. (This isn't a bos@567: rule, just an observation.) bos@559: bos@559: bos@559: bos@559: Dealing with multiple named branches in a bos@559: repository bos@559: bos@584: If you have more than one named branch in a repository, bos@559: Mercurial will remember the branch that your working directory bos@559: on when you start a command like hg bos@559: update or hg pull bos@559: -u. It will update the working directory to the tip bos@559: of this branch, no matter what the repo-wide tip bos@559: is. To update to a revision that's on a different named branch, bos@559: you may need to use the bos@559: option to hg update. bos@559: bos@584: This behaviour is a little subtle, so let's see it in bos@559: action. First, let's remind ourselves what branch we're bos@567: currently on, and what branches are in our repository. bos@567: bos@567: &interaction.branch-named.parents; bos@567: bos@584: We're on the bar branch, but there also bos@567: exists an older hg foo bos@567: branch. bos@559: bos@584: We can hg update back and bos@559: forth between the tips of the foo and bos@559: bar branches without needing to use the bos@559: option, because this bos@559: only involves going backwards and forwards linearly through our bos@567: change history. bos@567: bos@567: &interaction.branch-named.update-switchy; bos@559: bos@584: If we go back to the foo branch and then bos@559: run hg update, it will keep us bos@559: on foo, not move us to the tip of bos@567: bar. bos@567: bos@567: &interaction.branch-named.update-nothing; bos@559: bos@584: Committing a new change on the foo branch bos@567: introduces a new head. bos@567: bos@567: &interaction.branch-named.foo-commit; bos@559: bos@559: bos@559: bos@559: Branch names and merging bos@559: bos@584: As you've probably noticed, merges in Mercurial are not bos@559: symmetrical. Let's say our repository has two heads, 17 and 23. bos@559: If I hg update to 17 and then bos@559: hg merge with 23, Mercurial bos@559: records 17 as the first parent of the merge, and 23 as the bos@559: second. Whereas if I hg update bos@559: to 23 and then hg merge with bos@559: 17, it records 23 as the first parent, and 17 as the bos@559: second. bos@559: bos@584: This affects Mercurial's choice of branch name when you bos@559: merge. After a merge, Mercurial will retain the branch name of bos@559: the first parent when you commit the result of the merge. If bos@559: your first parent's branch name is foo, and bos@559: you merge with bar, the branch name will bos@559: still be foo after you merge. bos@559: bos@584: It's not unusual for a repository to contain multiple heads, bos@559: each with the same branch name. Let's say I'm working on the bos@559: foo branch, and so are you. We commit bos@559: different changes; I pull your changes; I now have two heads, bos@559: each claiming to be on the foo branch. The bos@559: result of a merge will be a single head on the bos@559: foo branch, as you might hope. bos@559: bos@584: But if I'm working on the bar branch, and bos@559: I merge work from the foo branch, the result bos@567: will remain on the bar branch. bos@567: bos@567: &interaction.branch-named.merge; bos@559: bos@584: To give a more concrete example, if I'm working on the bos@559: bleeding-edge branch, and I want to bring in bos@559: the latest fixes from the stable branch, bos@559: Mercurial will choose the right bos@559: (bleeding-edge) branch name when I pull and bos@559: merge from stable. bos@559: bos@559: bos@559: bos@559: Branch naming is generally useful bos@559: bos@584: You shouldn't think of named branches as applicable only to bos@559: situations where you have multiple long-lived branches bos@559: cohabiting in a single repository. They're very useful even in bos@559: the one-branch-per-repository case. bos@559: bos@584: In the simplest case, giving a name to each branch gives you bos@559: a permanent record of which branch a changeset originated on. bos@559: This gives you more context when you're trying to follow the bos@559: history of a long-lived branchy project. bos@559: bos@584: If you're working with shared repositories, you can set up a bos@559: pretxnchangegroup hook on each bos@559: that will block incoming changes that have the bos@559: wrong branch name. This provides a simple, but bos@559: effective, defence against people accidentally pushing changes bos@559: from a bleeding edge branch to a bos@559: stable branch. Such a hook might look like this bos@559: inside the shared repo's bos@559: /.hgrc. bos@580: [hooks] bos@580: pretxnchangegroup.branch = hg heads --template '{branches} ' | grep mybranch bos@559: bos@559: bos@559: bos@559: bos@559: