Mercurial

From Octave
Revision as of 07:22, 24 February 2021 by Mmuetzel (talk | contribs) (→‎Tips for working with TortoiseHg: Fix some typos and grammar errors.)
Jump to navigation Jump to search

Mercurial (sometimes referred to as hg) is the source code management system used for Octave development.

Everybody is free to run, copy, distribute, study, change and improve[1] Octave's source code, given in the main repository at https://www.octave.org/hg/octave. Use Mercurial to get the latest version of Octave

hg clone https://www.octave.org/hg/octave
Info icon.svg
TortoiseHg is a GUI for Mercurial and it is especially recommended for users doing their first steps with source code management systems. Linux, macOS, and MS Windows are supported.

Creating and submitting patches (changesets)

If you want to share your modifications, for example to fix a nasty bug #42424, you cannot just submit your changes to Octave's main repository. You have to generate a patch (or changeset) so other Octave developers can include them into Octave's source code.

  1. Get the latest version of Octave (or some Octave package)
    hg clone https://www.octave.org/hg/octave
    or when already cloned
    hg pull && hg update
  2. Make your changes (fix bug #42424) and save them. Make sure that your changes don't introduce new bugs! Thus it is recommended to build Octave and to run Octave's test suite before proceeding.
    Warning icon.svg
    Please follow the Contribution guidelines for C/C++ or Octave code files!
  3. Commit your changes
    hg commit
    Mercurial will open your default editor[2] and ask you for a commit message. Please follow the commit message guidelines, e.g.
    help.m: Display relevant topics first (bug #42424)
    
    * scripts/help/help.m: Describe what you changed to display relevant topics
      first.  The maximal line width is 80 characters.
    
  4. Export the changes
    hg export -r tip -o bug42424.patch
    The final patch for submission will look like this
    File: bug42424.patch
    # HG changeset patch
    # User Awesome Hacker <awesome@hacker.com>
    # Date 1591780091 -32400
    #      Wed Jun 10 18:08:11 2020 +0100
    # Node ID 68c698c4f2fd98bf2d48234bd1da99e91763114f
    # Parent  f5c9bb5955e7c9fddef5c3c3f115201e11b43b79
    
    help.m: Display relevant topics first (bug #42424)
    
    * scripts/help/help.m: Describe what you changed to display relevant topics
      first.  The maximal line width is 80 characters.
    
    diff -r f5c9bb5955e7 -r 68c698c4f2fd scripts/help/help.m
    --- a/scripts/help/help.m	Tue Jun 09 14:11:13 2020 -0700
    +++ b/scripts/help/help.m	Wed Jun 10 18:08:11 2020 +0900
    @@ -99,7 +99,7 @@ function retval = help (name)
         endif
     
         ## Get help text
    -    [text, format] = get_help_text (name);
    +    [text, format] = get_better_help_text (name);
     
         ## Take action depending on help text format
         switch (lower (format))
    
  5. Upload bug42424.patch to the bug or patch tracker. If your patch file is larger than the upload limit, you can compress it before uploading. Please use a free format!

Mercurial Tips for SoC students

This section is meant to provide tips for Summer of Code students working on new Octave features.

Students should publish their work as it progresses in a public repository. In this section we use for example public.server.org/octave.

Using bookmarks

Bookmarks are useful for identifying a series of commits. They are a "lightweight" solution to named branches, which are not automatically updated for example. To create a bookmark my-gsoc use

hg clone https://www.octave.org/hg/octave
hg bookmark my-gsoc

To make the bookmark visible in the public repository use

hg push --bookmark ssh://student@public.server.org/octave

Staying up-to-date with the main repository

Octave development does not stand still while the students development proceeds. Octave's main repository gets updated, too. The following commands can be used to get these updated to the students clone of the main repository:

hg pull https://www.octave.org/hg/octave   # Get latest remote "tip"
hg update -r my-gsoc                       # Activate bookmark "my-gsoc"
hg merge tip                               # Merge  "tip" into "my-gsoc"
hg commit -m "maint: merge default to my-gsoc"
hg push ssh://student@public.server.org/octave

Preparing for code reviews

At the time of the mid-term or final review (or whenever the mentor requires it) students should prepare their code for review and possibly inclusion into the main repository.

  1. Create a full log of changes
    hg log --template=changelog --no-merges --user student-name
    If students have been following the Commit message guidelines the output is a good starting point for the commit message in the next step. Some manual post-processing might be necessary:
    • Each touched file should appear only once.
    • Do not mention backed-out commits.
  2. Prepare a singe patch (changeset) including all code that should be submitted for review
    hg pull https://www.octave.org/hg/octave   # Get remote "tip" and "@"
    hg update -r @                             # Activate    bookmark "@"
    hg merge my-gsoc                           # Merge "my-gsoc" into "@"
    hg commit
    hg export -r tip -o mid-term-review.patch
    
    The file mid-term-review.patch can uploaded to the patch tracker.
    Finally, there is a subtle difference between "tip", which is a reference to the (local or remote) changeset added to the repository most recently and the bookmark "@" used by the Octave developers to point to the latest remote changeset. Often both refer to the very same changeset and they can used interchangeably.

Example Mercurial configuration

Place the following file in your home directory, e.g. /home/username/.hgrc.

File: .hgrc
[ui]
username = Your Name <your@email>

[extensions]
color =
histedit =
pager =
rebase =
strip =

[pager]
pager = LESS='FSRX' less
attend = help, annotate, cat, diff, export, glog, log, outgoing, incoming

[diff]
showfunc = True

[color]
mode = terminfo

## Custom colors
color.gray = 244
color.orange = 202
color.lightyellow = 191
color.darkorange = 220
color.brightyellow = 226

status.modified = magenta bold
status.added = green bold
status.removed = red bold
status.deleted = cyan bold
status.unknown = gray bold
status.ignored = gray bold

## Colors for each label
log.branch = cyan
log.summary = lightyellow
log.description = lightyellow
log.bookmark = green
log.tag = darkorange
log.graph = blue

## Colors for each phase
changeset.secret = blue bold
changeset.draft  = red bold
changeset.public = orange

desc.here = bold blue_background

[bookmarks]
track.current = True

[alias]
glog = log --graph
top  = log --graph -l

Tips for working with TortoiseHg

TortoiseHg is a multi-platform graphical user interface for Mercurial repositories. It allows to perform many hg operations using the context menu and toolbar buttons. That might make it easier to get used to working with Mercurial.

Activate the "mq" extension

The "mq" extension allows to modify (local) changesets after they have been committed. It also allows to rebase changes to a new parent or to strip changes completely. The "mq" extension does *not* allow to modify pushed changes.

To activate the "mq" extension in TortoiseSVN, open the settings, select "Extensions" on the global settings tab and activate the checkbox next to "mq".

The most useful feature of that extension is probably to update an existing changeset. For this, select "Modify History" -> "Import to MQ" in the right click menu of the respective changeset. After updating some local files or changing the commit message, hit the "QRefresh" button. Finish the patch by selecting "Modify History" -> "Finish Patch" from the right click menu of the respective changeset.

Rebase a change to a current tip

Sometimes a change in the upstream repository might make it necessary to rebase a changeset to a new parent. There are several ways to achieve this. The ways described here might not be the most elegant ones. Any editor is welcome to add onto this.

Strip and commit:

  1. Pull changes from the upstream repository.
  2. Before updating to the new tip, strip the local changes by selecting "Modify History" -> "Strip..." from the right click menu. In the dialog, select "Do not modify working copy during strip (-k/--keep)".
  3. Update to the new tip (maybe in incremental steps).
  4. Commit the local changes in a "fresh" changeset. This has the drawback that any commit message might be lost. But it often works even if other approaches fail.

Un-apply and re-apply:

  1. Pull changes from the upstream repository.
  2. Before updating to the new tip, import the local changeset to mq by selecting "Modify History" -> "Import to MQ" in the right click menu.
  3. You might want to refresh the changeset with local changes.
  4. Un-apply the patch by selecting "Modify History" -> "Unapply Patch" from the right click menu. If there are other, un-committed changes in the local repository, you might want to select "Tolerate non-conflicting local changes (--keep-changes)" in the "Modify History" -> "MQ Options" dialog from the right click menu beforehand.
  5. Update to the new tip.
  6. Select the previously unapplied patch on top of the revision graph and re-apply it by using the "Reapply Patch" option in the right click menu. This has the advantage that a commit message will be retained. But re-applying the patch might fail if there were changes in the upstream repository that made the patch incompatible.

Rebase:

  1. Pull changes from the upstream repository.
  2. Select the local changeset that you'd like to rebase.
  3. Hold down the Ctrl key and select the changeset that should be the new parent of the local changeset (probably the new tip).
  4. Right-click the changeset of the new parent and select "Rebase...".
  5. The default settings are often times fine. This process has the advantage that a commit message will be retained and it often times resolves conflicts automatically. But it doesn't work if there are any un-committed local changes.

Footnotes

External links