Documentation ¶
Overview ¶
Package gohg is a Go client library for using the Mercurial dvcs via it's Command Server.
For Mercurial see: http://mercurial.selenic.com.
For the Hg Command Server see: http://mercurial.selenic.com/wiki/CommandServer.
Compatibility ¶
â–ª Mercurial
For Mercurial any version starting from 1.9 should be ok, cause that's the one where the Command Server was introduced. If you send wrong options to it through gohg, or commands or options not yet supported (or obsolete) in your Hg version, you'll simply get back an error from Hg itself, as gohg does not check them. But on the other hand gohg allows issuing new commands, not yet implemented by gohg; see further.
â–ª Go
Currently gohg is currently developed with Go1.2.1. Though I started with the Go1.0 versions, I can't remember having had to change one or two minor things when moving to Go1.1.1. Updating to Go1.1.2 required no changes at all. I had an issue though with Go1.2, on Windows only, causing some tests using os.exec.Command to fail. I'll have to look into that further, to find out if I should report a bug.
â–ª Platform
I'm developing and testing both on Windows 7 and Ubuntu 12.04/13.04/13.10. But I suppose it should work on any other platform that supports Hg and Go.
Dependencies ¶
Only Go and it's standard library. And Mercurial should be installed of course.
Installation ¶
At the commandline type:
go get [-u] bitbucket.org/gohg/gohg go test [-v] bitbucket.org/gohg/gohg
to have gohg available in your GOPATH.
Import the package ¶
Start with importing the gohg package. Examples:
import gohg "bitbucket.org/gohg/gohg" or import hg "bitbucket.org/gohg/gohg"
Connecting the Mercurial Command Server ¶
All interaction with the Mercurial Command Server (Hg CS from now on) happens through the HgClient type, of which you have to create an instance:
hgcl := NewHgClient()
Then you can connect the Hg CS as follows:
err := hgcl.Connect("hg", "~/myrepo", nil, false) 5 1 2 3 4
1. The Hg executable:
The first parameter is the Mercurial command to use (which 'hg'). You can leave it blanc to let the gohg tool use the default Mercurial command on the system. Having a parameter for the Hg command allows for using a different Hg version, for testing purposes for instance.
2. The repository path:
The second parameter is the path to the repository you want to work on. You can leave it blanc to have gohg use the repository it can find for the current path you are running the program in (searching upward in the folder tree eventually).
3. The config for the session:
The third parameter allows to provide extra configuration for the session. Though this is currently not implemented yet.
4. Should gohg create a new repo before connecting?
This fourth parameter allows you to indicate that you want gohg to first create a new Mercurial repo if it does not already exist in the path given by the second parameter. See the documentation for more detailed info.
5. The returnvalue:
The HgClient.Connect() method eventually returns an error, so you can check if the connection succeeded, and if it is safe to go on.
Once the work is done, you can disconnect the Hg CS using a typical Go idiom:
err := hgcl.Connect("hg", "~/myrepo", nil) if err != nil { log.Fatal(err) } defer hgcl.Disconnect() // do the real work here
Config ¶
The gohg tool sets some environment variables for the Hg CS session, to ensure it's good working:
// ensure Hg works in english HGPLAIN=True // Use only the .hg/hgrc from the repo itself. HGRCPATH='' HGENCODING=UTF-8
Commands ¶
Once we have a connection to a Hg CS we can do some work with the repository. This is done with commands, and gohg offers 3 ways to use them.
1. The command methods of the HgClient type.
2. The HgCmd type.
3. The ExecCmd() method of the HgClient type.
Each of which has its own reason of existence.
Commands return a byte slice containing the resulting data, and eventually an error. But there are a few exceptions (see api docs).
log, err := hgcl.Log(nil, nil) // log is a byte slice err := hgcl.Init(nil, "~/mynewrepo") // only returns an error eventually vers, err:= hgcl.Version() // vers is a string of the form '2.4'
If a command fails, the returned error contains 5 elements: 1) the name of the internal routine where the error was trapped, 2) the name of the HgClient command that was run, 3) the returncode by Mercurial, 4) the full command that was passed to the Hg CS, and 5) the eventual error message returned by Mercurial.
So the command
idinfo, err := hgcl.Identify([]hg.HgOption{hg.Verbose(true)}, []string{"C:\\DEV\\myrepo"})
could return something like the following in the err variable when it fails:
runcommand: Identify(): returncode=-1 cmd: identify -v C:\DEV\myrepo hgerr:
The command aliases (like 'id' for 'identify') are not implemented. But there are examples in identify.go and showconfig.go of how you can easily implement them yourself.
Commands - HgClient command methods ¶
This is the easiest way, a kind of convenience. And the most readable too. A con is that as a user you cannot know the exact command that was passed to Hg, without some extra mechanics.
Each command has the same name as the corresponding Hg command, except it starts with a capital letter of course.
An example (also see examples/example1.go):
log, err := hgcl.Log([]hg.HgOption{hg.Limit(2)}, []string("my-file")) if err != nil { fmt.Printf(err) ... } fmt.Printf("%s", log)
Note that these methods all use the HgCmd type internally. As such they are convenience wrappers around that type. You could also consider them as a kind of syntactic sugar. If you just want to simply issue a command, nothing more, they are the way to go.
The only way to obtain the commandstring sent to Hg when using these command methods, is by calling the HgClient.ShowLastCmd() method afterwards before issuing any other commands:
log, err := hgcl.Log([]hg.HgOption{hg.Limit(2)}, []string("my-file")) fmt.Printf("%s", hgcl.ShowLastCmd()) // prints: log --limit 2 my-file
Commands - the HgCmd type ¶
Using the HgCmd type is kind of the standard way. It is a struct that you can instantiate for any command, and for which you can set elements Name, Options and Params (see the api docs for more details). It allows for building the command step by step, and also to query the exact command that will be sent to the Hg CS.
A pro of this method is that it allows you to obtain the exact command string that will be passed to Mercurial before it is performed, by calling the CmdLine() method of HgCmd. This could be handy for logging, or for showing feedback to the user in a GUI program. (You could even call CmdLine() several times, and show the building of the command step by step.)
An example (also see examples/example2.go):
opts := make([]hg.HgOption, 2) var lim Limit = 2 opts[0] = lim var verb Verbose = true opts[1] = verb cmd, _ := hg.NewHgCmd("log", opts, nil, new(hg.logOpts)) cmd.SetOptions(opts) cmdline, _ := cmd.CmdLine(hgcl) fmt.Printf("%s\n", cmdline) // output: log --limit 2 -v cmd.Exec(hgcl)
As you can see, this way requires some more coding.
The source code will also show you that the HgCmd type is indeed used as the underlying type for the convenience HgClient commands, in all the New<hg-command>Cmd() constructors.
Commands - ExecCmd ¶
The HgClient type has an extra method ExecCmd(), allowing you to pass a fully custom built command to Hg. It accepts a string slice that is supposed to contain all the elements of the complete command, as you would type it at the command line.
It could be a convenient way for performing commands that are not yet implemented in gohg, or to make use of extensions to Hg (for which gohg offers no support (yet?)).
An example (also see examples/example3.go):
hgcmd := []string{"log", "--limit", "2"} result, err := hgcl.ExecCmd(hgcmd)
Options and Parameters ¶
Just like on the commandline, options come before parameters.
opts := []hg.HgOption{hg.Verbose(true), hg.Limit(2)} params := []string{"mytool.go"} log, err := hgcl.Log(opts, params)
Options to commands use the same name as the long form of the Mercurial option they represent, but start with the necessary capital letter. An options value can be of type bool, int or string. You just pass the value as the parameter to the option (= type conversion of the value to the option type). You can pass any number of options, as the elements of a slice. Options can occur more than once if appropriate (see the ones marked with '[+]' in the Mercurial help).
log, err := hgcl.Log([]hg.HgOption{hg.Verbose(true)}, nil) // bool log, err := hgcl.Log([]hg.HgOption{hg.Limit(2)}, nil) // int log, err := hgcl.Log([]hg.HgOption{hg.User("John Doe"), hg.User("me")}, nil) // string, repeated option
Parameters are used to provide any arguments for a command that are not options. They are passed in as a string or a slice of strings, depending on the command. These parameters typically contain revisions, paths or filenames and so.
log, err := hgcl.Log(nil, []string{"myfile"}) heads, err := hgcl.Heads(nil, []string{"foobranch"})
The gohg tool only checks if the options the caller gives are valid for that command. It does not check if the values are valid for the combination of that command and that option, as that is done by Mercurial. No need to implement that again. If an option is not valid for a command, it is silently ignored, so it is not passed to the Hg CS.
A few options are not implemented, as they seemed not relevant for use with this tool (for instance: the global --color option, or the --print0 option for status).
Error handling ¶
The gohg tool only returns errors, with an as clear as possible message, and never uses log.Fatal() nor panics, even if those may seem appropriate. It leaves it up to the caller to do that eventually. It's not up to this library to decide whether to do a retry or to abort the complete application.
Limitations ¶
â–ª The following config settings are fixated in the code (at least for now):
encoding=utf-8 ui.interactive=False extensions.color=!
â–ª As mentioned earlier, passing config info is not implemented yet.
â–ª Currently the only support for extensions to Mercurial is through the ExecCmd method.
â–ª If multiple Hg CSs are used against the same repo, it is up to Mercurial to handle this correctly.
â–ª Mercurial is always run in english. Internationalization is not necessary here, as the conversation with Hg is internal to the application.
Feedback ¶
Please note that this tool is still in it's very early stages. If you have suggestions or requests, or experience any problems, please use the issue tracker at https://bitbucket.org/gohg/gohg/issues?status=new&status=open. Or you could send a patch or a pull request.
License ¶
Copyright 2012-2014, The gohg Authors. All rights reserved.
Use of this source code is governed by a BSD style license that can be found in the LICENSE.md file.
Index ¶
- type AccessLog
- type Active
- type AddRemove
- type Added
- type Address
- type After
- type All
- type AllTasks
- type Amend
- type Bookmark
- type Bookmarks
- type Branch
- type Certificate
- type Change
- type Changeset
- type Check
- type Clean
- type CloseBranch
- type Closed
- type CmdServer
- type CompletedTasks
- type Config
- type Copies
- type Cset
- type Cwd
- type Daemon
- type DaemonPipefds
- type Date
- type Debug
- type Deleted
- type DryRun
- type ErrorLog
- type Exclude
- type File
- type Follow
- type Force
- type Git
- type Graph
- type HgClient
- func (hgcl *HgClient) Add(opts []HgOption, files []string) ([]byte, error)
- func (hgcl *HgClient) AddRemove(opts []HgOption, files []string) ([]byte, error)
- func (hgcl *HgClient) Annotate(opts []HgOption, files []string) ([]byte, error)
- func (hgcl *HgClient) Archive(opts []HgOption, dest []string) ([]byte, error)
- func (hgcl *HgClient) Branches(opts []HgOption, params []string) ([]byte, error)
- func (hgcl *HgClient) Capabilities() []string
- func (hgcl *HgClient) Clone(opts []HgOption, fromto []string) error
- func (hgcl *HgClient) Commit(opts []HgOption, files []string) error
- func (hgcl *HgClient) Connect(hgexe string, reponame string, config []string, initrepo bool) error
- func (hgcl *HgClient) Diff(opts []HgOption, files []string) ([]byte, error)
- func (hgcl *HgClient) Disconnect() error
- func (hgcl *HgClient) Encoding() string
- func (hgcl *HgClient) ExecCmd(hgcmd []string) ([]byte, error)
- func (hgcl *HgClient) Export(opts []HgOption, params []string) ([]byte, error)
- func (hgcl *HgClient) Forget(opts []HgOption, files []string) ([]byte, error)
- func (hgcl *HgClient) Heads(opts []HgOption, revs []string) ([]byte, error)
- func (hgcl *HgClient) HgExe() string
- func (hgcl *HgClient) Identify(opts []HgOption, source []string) ([]byte, error)
- func (hgcl *HgClient) Init(opts []HgOption, destpath string) error
- func (hgcl *HgClient) IsConnected() bool
- func (hgcl *HgClient) Log(opts []HgOption, files []string) ([]byte, error)
- func (hgcl *HgClient) Manifest(opts []HgOption, params []string) ([]byte, error)
- func (hgcl *HgClient) Merge(opts []HgOption, rev []string) ([]byte, error)
- func (hgcl *HgClient) Pull(opts []HgOption, source []string) ([]byte, error)
- func (hgcl *HgClient) Push(opts []HgOption, dest []string) ([]byte, error)
- func (hgcl *HgClient) Remove(opts []HgOption, files []string) ([]byte, error)
- func (hgcl *HgClient) RepoRoot() string
- func (hgcl *HgClient) Serve(opts []HgOption, params []string) ([]byte, error)
- func (hgcl *HgClient) ShowConfig(opts []HgOption, configitems []string) ([]byte, error)
- func (hgcl *HgClient) ShowLastCmd() string
- func (hgcl *HgClient) Status(opts []HgOption, files []string) ([]byte, error)
- func (hgcl *HgClient) Summary(opts []HgOption, params []string) ([]byte, error)
- func (hgcl *HgClient) Tags(opts []HgOption, params []string) ([]byte, error)
- func (hgcl *HgClient) Tip(opts []HgOption, params []string) ([]byte, error)
- func (hgcl *HgClient) Update(opts []HgOption, params []string) ([]byte, error)
- func (hgcl *HgClient) Verify(opts []HgOption, params []string) ([]byte, error)
- func (hgcl *HgClient) Version() (string, error)
- type HgCmd
- func NewAddCmd(opts []HgOption, files []string) HgCmd
- func NewAddRemoveCmd(opts []HgOption, files []string) HgCmd
- func NewAnnotateCmd(opts []HgOption, files []string) HgCmd
- func NewArchiveCmd(opts []HgOption, dest []string) HgCmd
- func NewBranchesCmd(opts []HgOption, files []string) HgCmd
- func NewCloneCmd(opts []HgOption, fromto []string) HgCmd
- func NewCommitCmd(opts []HgOption, files []string) HgCmd
- func NewDiffCmd(opts []HgOption, files []string) HgCmd
- func NewExportCmd(opts []HgOption, params []string) HgCmd
- func NewForgetCmd(opts []HgOption, files []string) HgCmd
- func NewHeadsCmd(opts []HgOption, revs []string) HgCmd
- func NewHgCmd(name string, opts []HgOption, params []string, cmdopts interface{}) (*HgCmd, error)
- func NewIdentifyCmd(opts []HgOption, source []string) HgCmd
- func NewInitCmd(opts []HgOption, destpath []string) HgCmd
- func NewLogCmd(opts []HgOption, files []string) HgCmd
- func NewManifestCmd(opts []HgOption, files []string) HgCmd
- func NewMergeCmd(opts []HgOption, rev []string) HgCmd
- func NewPullCmd(opts []HgOption, source []string) HgCmd
- func NewPushCmd(opts []HgOption, dest []string) HgCmd
- func NewRemoveCmd(opts []HgOption, files []string) HgCmd
- func NewServeCmd(opts []HgOption, params []string) HgCmd
- func NewShowConfigCmd(opts []HgOption, configitems []string) HgCmd
- func NewStatusCmd(opts []HgOption, files []string) HgCmd
- func NewSummaryCmd(opts []HgOption, params []string) HgCmd
- func NewTagCmd(opts []HgOption, params []string) HgCmd
- func NewTipCmd(opts []HgOption, params []string) HgCmd
- func NewUpdateCmd(opts []HgOption, files []string) HgCmd
- func NewVerifyCmd(opts []HgOption, params []string) HgCmd
- type HgOption
- type Hidden
- type Id
- type IgnoreAllSpace
- type IgnoreBlankLines
- type IgnoreSpaceChange
- type Ignored
- type Include
- type Insecure
- type Ipv6
- type Keyword
- type Limit
- type LineNumber
- type Logfile
- type Message
- type Modified
- type Name
- type NewBranch
- type NoDates
- type NoDecode
- type NoFollow
- type NoMerges
- type NoStatus
- type NoUpdate
- type NonInteractive
- type Num
- type Number
- type Output
- type Patch
- type PidFile
- type Port
- type Prefix
- type Preview
- type Print0
- type Profile
- type Prune
- type Pull
- type Quiet
- type Rebase
- type Remote
- type RemoteCmd
- type Removed
- type Repository
- type Rev
- type Reverse
- type ShowFunction
- type Similarity
- type Ssh
- type Stat
- type Stdio
- type Style
- type SubRepos
- type SwitchParent
- type Tags
- type Template
- type Templates
- type Text
- type Time
- type Tool
- type Topo
- type Traceback
- type Type
- type Uncompressed
- type Unified
- type Unknown
- type Untrusted
- type Update
- type UpdateRev
- type User
- type Verbose
- type WebConf
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Certificate ¶
type Certificate string // --certificate FILE
type CloseBranch ¶
type CloseBranch bool // --close-branch
type CompletedTasks ¶
type CompletedTasks bool // --completed-tasks
type Cset ¶
type Cset struct { Rev int Node string Tags []string Branch string Author string Time time.Time // always as UTC Desc string Patch []string }
Cset: Should be moved into a separate sourcefile, together with any other Mercurial related stuff.
type DaemonPipefds ¶
type DaemonPipefds int // --daemon-pipefds NUM
type HgClient ¶
type HgClient struct {
// contains filtered or unexported fields
}
Type HgClient acts as the entrypoint through which all interaction with the Mercurial Command Server takes place. If you want to keep a pool of connections to multiple repos, you can create multiple HgClient instances, each connecting to its own Hg CS.
func NewHgClient ¶
func NewHgClient() *HgClient
NewHgClient creates a new instance of the client type for working with the Hg Command Server.
func (*HgClient) Capabilities ¶
Capabilities returns the capabilities of the connected Hg CS.
func (*HgClient) Connect ¶
Connect establishes the connection with the Mercurial Command Server.
Arguments:
hgexe The command to run Mercurial. Optional. The 'hg' command will be used when not provided. This allows to run a specific version of Mercurial. reponame The folder of the Hg repository to work on. Optional. When blanc the folder where the program is run is used (see function locateRepository()). config Configuration settings that will be added to the necessary fixed settings (see composeStartupConfig() for more). Optional. initrepo When a repo exitsts for reponame, then initrepo is ignored. When no repo is found for reponame, and initrepo is true, then Connect() will first create the repository before connecting. When no repo is found for reponame, and initrepo is false, then Connect() will return an error.
Returns an error if the connection could not be established properly.
Example ¶
hc := NewHgClient() if err := hc.Connect("hg", ".", []string{""}, false); err != nil { log.Fatal(err) } defer hc.Disconnect() // Call some useful methods on hc here.
Output:
func (*HgClient) Disconnect ¶
Disconnect ends the connection with the Mercurial Command Server.
func (*HgClient) ExecCmd ¶
ExecCmd allows to pass in a full commandline for the Hg CS by yourself, though in a less Go-like way. No checks are done however; the command is directly passed to the Hg CS as is. See client_test.go for an example. This method could come in handy when you want to use a new Hg command for which the gohg tool is not yet updated. Or for using some extension to Hg. Be sure to add an option and its value separately to hgcmd. (is not ok: ' hgcmd[1] = "--limit 2" ', is ok: ' hgcmd[1] = "--limit"; hgcmd[2] = "2" ')
func (*HgClient) Init ¶
Be aware of the fact that Init() cannot be used to initialize the repo you want the (current) Hg CS to work on, as the Hg CS requires an existing repo before you can connect it. But Init() can be used to create any new repo besides the one the Hg CS is running for.
func (*HgClient) IsConnected ¶
IsConnected tells if there is a connection to a Hg CS.
func (*HgClient) RepoRoot ¶
RepoRoot returns the root of the repository the connected Hg CS is working on.
func (*HgClient) ShowConfig ¶
func (*HgClient) ShowLastCmd ¶
ShowLastCmd produces the commandline for the last command that was submitted to the Hg CS until then. It is a convenience for if you use the hgcl.Identify() way of issuing commands (so not via a HgCmd.Exec(), or via ExecCmd()), and you want to know the exact commandline that was passed to Hg. This one works after all 3 possibilities to pass commands to Hg.
type HgCmd ¶
type HgCmd struct { Name string Options []HgOption Params []string // contains filtered or unexported fields }
HgCmd is the type through which you can create and execute Mercurial commands.
func NewAddRemoveCmd ¶
func NewAnnotateCmd ¶
func NewArchiveCmd ¶
func NewBranchesCmd ¶
func NewCloneCmd ¶
func NewCommitCmd ¶
func NewDiffCmd ¶
func NewExportCmd ¶
func NewForgetCmd ¶
func NewHeadsCmd ¶
func NewIdentifyCmd ¶
func NewInitCmd ¶
func NewManifestCmd ¶
func NewMergeCmd ¶
func NewPullCmd ¶
func NewPushCmd ¶
func NewRemoveCmd ¶
func NewServeCmd ¶
func NewShowConfigCmd ¶
func NewStatusCmd ¶
func NewSummaryCmd ¶
func NewUpdateCmd ¶
func NewVerifyCmd ¶
func (*HgCmd) CmdLine ¶
CmdLine gives you the exact commandline as it will be passed to Mercurial, generated with the data in the HgCmd instance. Handy for logging or showing in a GUI.
func (*HgCmd) Exec ¶
Exec builds the commandline with the data in the HgCmd instance, and then passes the command to the Mercurial Command Server for execution, returning the result or an error.
func (*HgCmd) SetOptions ¶
type HgOption ¶
type HgOption interface {
// contains filtered or unexported methods
}
HgOption is an interface that allows adding options to a command in a more or less controlled way.
type IgnoreAllSpace ¶
type IgnoreAllSpace bool // -w --ignore-all-space
type IgnoreBlankLines ¶
type IgnoreBlankLines bool // -B --ignore-blank-lines
type IgnoreSpaceChange ¶
type IgnoreSpaceChange bool // -b --ignore-space-change
type LineNumber ¶
type LineNumber bool // -l --line-number
type NonInteractive ¶
type NonInteractive bool // -y --noninteractive
type Repository ¶
type Repository string // -R --repository
type ShowFunction ¶
type ShowFunction bool // -p --show-fuction
type Similarity ¶
type Similarity int // -s --similarity SIMILARITY
type SwitchParent ¶
type SwitchParent bool // --switch-parent
type Uncompressed ¶
type Uncompressed bool // --uncompressed
Source Files ¶
- add.go
- addremove.go
- annotate.go
- archive.go
- branches.go
- client.go
- clone.go
- command.go
- commit.go
- diff.go
- doc.go
- export.go
- forget.go
- heads.go
- identify.go
- init.go
- log.go
- manifest.go
- merge.go
- options.go
- pull.go
- push.go
- remove.go
- serve.go
- showconfig.go
- status.go
- summary.go
- tags.go
- tip.go
- update.go
- verify.go
- version.go