Templates
Learn how to use templates to super-charge your Git-Tool configuration.
Git-Tool uses Go templates to allow applications and services you add to your config to access information about the repositories and scratchpads they are targeting. This lets you do some pretty powerful stuff, including handling repositories and scratchpads differently if you want to.

Interpolation

The most basic construct in Go's templating language is the interpolation block, which is replaced with the value of the property it references.
1
{{ .Target.Name }}
Copied!
The properties available to you are described in the context section below.
This is used extensively when writing services as it allows you to inject information about the repository in question into things like the URLs used by git. You can also use it to inject information into your applications through any combination of their command, args and environment.

Context

The context available within the template evaluator depends on whether you are dealing with a scratchpad, a repo whose service is not in your config, or a repo with a matching service.
The .Target property will always be available with all of its children, so it is the safest thing to use if you just need some quick information or wish to support scratchpads.
If you are targeting repositories (either because you're writing a service entry, in which case this is implied), or because your application only makes sense within the context of a repository, then the .Service and .Repo properties will be available.
Go's template language allows you to conditionally use properties, if they exist, with the following construct:
1
{{ with .Repo }}
2
# Note that . now refers to .Repo, so this is the same as .Repo.FullName
3
{{ .FullName }}
4
{{ else }}
5
{{ .Target.Name }}
6
{{ end }}
Copied!
    Target
      Name: SierraSoftworks/git-tool
      Path: /home/bpannell/dev/github.com/SierraSoftworks/git-tool
      Exists: true
    Service (optional)
      Domain: github.com
      Pattern: */*
      DirectoryGlob: */*
    Repo (optional)
      Service
        Domain: github.com
        Pattern: */*
        DirectoryGlob: */*
      Domain: github.com
      FullName: SierraSoftworks/git-tool
      Name: git-tool
      Namespace: SierraSoftworks
      Path: /home/bpannell/dev/github.com/SierraSoftworks/git-tool
      Exists: true
      Valid: true
      Website: https://github.com/SierraSoftworks/git-tool
      GitURL: [email protected]:SierraSoftworks/git-tool.git
      HttpURL: https://github.com/SierraSoftworks/git-tool.git
Last modified 20d ago
Copy link