%  Copyright (C) 2004 David Roundy
%
%  This program is free software; you can redistribute it and/or modify
%  it under the terms of the GNU General Public License as published by
%  the Free Software Foundation; either version 2, or (at your option)
%  any later version.
%
%  This program is distributed in the hope that it will be useful,
%  but WITHOUT ANY WARRANTY; without even the implied warranty of
%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%  GNU General Public License for more details.
%
%  You should have received a copy of the GNU General Public License
%  along with this program; if not, write to the Free Software Foundation,
%  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

\chapter{Configuring darcs}\label{configuring}

There are several ways you can adjust darcs' behavior to suit your needs.
The first is to edit files in the \verb!_darcs/prefs/! directory of a
repository.  Such configuration only applies when working with that
repository.  To configure darcs on a per-user rather than per-repository
basis (but with essentially the same methods), you can edit (or create)
files in the \verb!~/.darcs/! directory.
Finally, the behavior of some darcs commands can be modified by setting
appropriate environment variables.

\paragraph{Microsoft Windows}\label{ms_win}

The global darcs directory is \verb!%APPDATA%\darcs\!.  This typically expands to
\texttt{C:\textbackslash{}Documents And Settings\textbackslash{}\emph{user}\textbackslash{}Application Data\textbackslash{}darcs\textbackslash{}}.
This folder contains the cache, as well as all the per-user
settings files: preferences, boring etc... These will became the new defaults
that can be overridden on per-repository basis.

\section{prefs}

The \verb!_darcs! directory contains a \verb!prefs!  directory.  This
directory exists simply to hold user configuration settings specific to
this repository.  The contents of this directory are intended to be
modifiable by the user, although in some cases a mistake in such a
modification may cause darcs to behave strangely.

\paragraph{defaults}\label{defaults}

Default values for darcs commands can be configured on a per-repository
basis by editing (and possibly creating) the \verb!_darcs/prefs/defaults!
file.  Each line of this file has the following form:
\begin{verbatim}
COMMAND FLAG VALUE
\end{verbatim}
where \verb!COMMAND! is either the name of the command to which the default
applies, or \verb!ALL! to indicate that the default applies to all commands
accepting that flag.  The \verb!FLAG! term is the name of the long argument
option without the ``\verb!--!'', i.e.\ \verb!verbose! rather than
\verb!--verbose!.  Finally, the \verb!VALUE! option can be omitted if the
flag is one such as \verb!verbose! that doesn't involve a value.
If the value has spaces in it, use single quotes, not double quotes, to surround it.
Each line only takes one flag.  To set multiple defaults for the same
command (or for \verb!ALL! commands), use multiple lines.

Note that the use of \verb|ALL| easily can have unpredicted consequences,
especially if commands in newer versions of darcs accepts flags that they
didn't in previous versions. A command like \verb|obliterate| could be
devastating with the ``wrong'' flags (for example --all). Only use safe
flags with \verb|ALL|.

\begin{tabular}{ll}
{\tt \verb!~/.darcs/defaults!} & provides defaults for this user account
                                 (for MS Windows, see ~\ref{ms_win}) \\
{\tt \verb!repo/_darcs/prefs/defaults!} & provides defaults for one project,\\
  & overrules changes per user \\
\end{tabular}

For example, if your system clock is bizarre, you could instruct darcs to
always ignore the file modification times by adding the following line to
your \verb!_darcs/prefs/defaults! file.  (Note that this would have to be
done for each repository!)
\begin{verbatim}
ALL ignore-times
\end{verbatim}

If you never want to run a test when recording to a particular repository
(but still want to do so when running
\verb'check' on that repository), and like to name
all your patches ``Stupid patch'', you could use the following:
\begin{verbatim}
record no-test
record patch-name Stupid patch
\end{verbatim}

If you would like a command to be run every time patches are recorded
in a particular repository (for example if you have one central
repository, that all developers contribute to), then you can set apply
to always run a command when apply is successful.  For example, if you
need to make sure that the files in the repository have the correct
access rights you might use the following.  There are two things
to note about using darcs this way:
\begin{itemize}
\item Without the second line you will get errors, because the sub
      process that runs apply cannot prompt interactively.
\item Whatever script is run by the post apply command should not be
      added to the repository with \verb!darcs add!; doing so would
      allow people to modify that file and then run arbitrary scripts on
      your main repository, possibly damaging or violating security.
\end{itemize}
\begin{verbatim}
apply posthook chmod -R a+r *
apply run-posthook
\end{verbatim}

Similarly, if you need a command to run automatically before darcs
performs an action you can use a prehook.  Using prehooks it could be
possible to canonicalize line endings before recording patches.

There are some options which are meant specifically for use in
\verb!_darcs/prefs/defaults!. One of them is \verb!--disable!. As the name
suggests, this option will disable every command that got it as argument. So,
if you are afraid that you could damage your repositories by inadvertent use of
a command like amend-record, add the following line to
\verb!_darcs/prefs/defaults!:
\begin{verbatim}
amend-record disable
\end{verbatim}

Also, a global preferences file can be created with the name
\verb!.darcs/defaults! in your home directory, on MS Windows~\ref{ms_win}.
Options present there will be added to the repository-specific preferences.
If they conflict with repository-specific options, the repository-specific
ones will take precedence.

\paragraph{repos}
The \verb!_darcs/prefs/repos! file contains a list of repositories you have
pulled from or pushed to, and is used for autocompletion of pull and push
commands in bash.  Feel free to delete any lines from this list that might
get in there, or to delete the file as a whole.

\paragraph{author}\label{author_prefs}
The \verb!_darcs/prefs/author! file contains the email address (or name) to
be used as the author when patches are recorded in this repository,
e.g.\ \verb!David Roundy <droundy@abridgegame.org>!.  This
file overrides the contents of the environment variables
\verb!$DARCS_EMAIL! and \verb!$EMAIL!.

\paragraph{boring}\label{boring}
The \verb!_darcs/prefs/boring! file may contain a list of regular
expressions describing files, such as object files, that you do not expect
to add to your project.  As an example, you could have:
\begin{verbatim}
\.hi$
\.o$
^\.[^/]
^_
~$
(^|/)CVS($|/)
\end{verbatim}
A newly created repository has a longer boring file that
includes many common source control, backup, temporary, and compiled files.

You may want to have the boring file under version
control.  To do this you can use darcs setpref to set the value
``boringfile'' to the name of your desired boring file
(e.g.\ \verb-darcs setpref boringfile .boring-, where \verb-.boring-
is the repository path of a file
that has been
darcs added to your repository).  The boringfile preference overrides
\verb!_darcs/prefs/boring!, so be sure to copy that file to the boringfile.

You can also set up a ``boring'' regexps
file in your home directory, named \verb!~/.darcs/boring!,
(see \ref{ms_win} on MS Windows), which will be
used with all of your darcs repositories.

Any file not already managed by darcs and whose repository path (such
as \verb!manual/index.html!) matches any of
the boring regular expressions is considered boring.  The boring file is
used to filter the files provided to darcs add, to allow you to use a
simple \verb-darcs add newdir newdir/-\verb-*- % cabal haddock barfs on adjacent / *
without accidentally adding a bunch of
object files.  It is also used when the \verb!--look-for-adds! flag is
given to whatsnew or record.
Note that once a file has been added to darcs, it is not considered
boring, even if it matches the boring file filter.


\paragraph{binaries}
The \verb!_darcs/prefs/binaries! file may contain a list of regular
expressions describing files that should be treated as binary files rather
than text files. Darcs automatically treats files containing
\verb!^Z\! or \verb!'\0'! within the first 4096 bytes as being binary files.
You probably will want to have the binaries file under
version control.  To do this you can use darcs setpref to set the value
``binariesfile'' to the name of your desired binaries file
(e.g.\ \verb'darcs setpref binariesfile ./.binaries', where
\verb'.binaries' is a file that has been
darcs added to your repository).  As with the boring file, you can also set
up a \verb!~/.darcs/binaries! file if you like
(see \ref{ms_win} on MS Windows).

\paragraph{email}
The \verb!_darcs/prefs/email! file is used to provide the e-mail address for your
repository that others will use when they \verb!darcs send! a patch back to you.
The contents of the file should simply be an e-mail address.


\paragraph{sources}
The \verb!_darcs/prefs/sources! file is used to indicate alternative
locations from which to download patches when using a ``hashed''
repository.  This file contains lines such as:
\begin{verbatim}
cache:/home/droundy/.darcs/cache
readonly:/home/otheruser/.darcs/cache
repo:http://darcs.net
\end{verbatim}
This would indicate that darcs should first look in
\verb!/home/droundy/.darcs/cache! for patches that might be missing, and if
the patch isn't there, it should save a copy there for future use.  In that
case, darcs will look in \verb!/home/otheruser/.darcs/cache! to see if that
user might have downloaded a copy, but won't try to save a copy there, of
course.  Finally, it will look in \verb!http://darcs.net!.  Note that the
\verb!sources! file can also exist in \verb!~/.darcs/!.  Also note that the
sources mentioned in your \verb!sources! file will be tried \emph{before}
the repository you are pulling from.  This can be useful in avoiding
downloading patches multiple times when you pull from a remote repository
to more than one local repository.

A global cache is enabled by default in your home directory.  The cache
allows darcs to avoid re-downloading patches (for example, when doing a
second darcs get of the same repository), and also allows darcs to use hard
links to reduce disk usage.

Note that the cache directory should reside on the same filesystem as your
repositories, so you may need to vary this.  You can also use multiple
cache directories on different filesystems, if you have several filesystems
on which you use darcs.

\paragraph{motd}\label{motd}
The \verb!_darcs/prefs/motd! file may contain a ``message of the day''
which will be displayed to users who get or pull from the repository without the
\verb!--quiet! option.

\section{Environment variables}

There are a few environment variables whose contents affect darcs'
behavior.  Here is a quick list of all the variables and their
documentation in the rest of the manual:

\begin{tabular}{|l|r|}
\hline
\textbf{Variable} & \textbf{Section} \\
\hline
DARCS\_EDITOR, EDITOR, VISUAL & \ref{env:DARCS_EDITOR} \\
DARCS\_PAGER, PAGER &  \ref{env:DARCS_PAGER} \\
HOME & \ref{env:HOME} \\
TERM & \ref{env:TERM} \\
\hline
DARCS\_EMAIL, EMAIL  & \ref{env:DARCS_EMAIL} \\
\hline
DARCS\_APPLY\_FOO & \ref{env:DARCS_X_FOO} \\
DARCS\_GET\_FOO & \ref{env:DARCS_X_FOO} \\
DARCS\_MGET\_FOO & \ref{env:DARCS_X_FOO} \\
DARCS\_MGETMAX & \ref{env:DARCS_MGETMAX} \\
DARCS\_PROXYUSERPWD & \ref{env:DARCS_PROXYUSERPWD} \\
DARCS\_CONNECTION\_TIMEOUT & \ref{env:DARCS_CONNECTION_TIMEOUT}\\
DARCS\_SSH & \ref{env:DARCS_SSH} \\
DARCS\_SCP & \ref{env:DARCS_SCP} \\
DARCS\_SFTP & \ref{env:DARCS_SFTP} \\
SSH\_PORT & \ref{env:SSH_PORT} \\
\hline
DARCS\_ALTERNATIVE\_COLOR & \ref{env:DARCS_ALWAYS_COLOR}\\
DARCS\_ALWAYS\_COLOR & \ref{env:DARCS_ALWAYS_COLOR}\\
DARCS\_DO\_COLOR\_LINES & \ref{env:DARCS_DO_COLOR_LINES}\\
DARCS\_DONT\_COLOR   & \ref{env:DARCS_ALWAYS_COLOR} \\
DARCS\_DONT\_ESCAPE\_TRAILING\_CR     & \ref{env:DARCS_DONT_ESCAPE_white}\\
DARCS\_DONT\_ESCAPE\_TRAILING\_SPACES & \ref{env:DARCS_DONT_ESCAPE_white} \\
DARCS\_DONT\_ESCAPE\_8BIT & \ref{env:DARCS_DONT_ESCAPE_nonascii}\\
DARCS\_DONT\_ESCAPE\_ANYTHING & \ref{env:DARCS_DONT_ESCAPE_nonascii}\\
DARCS\_DONT\_ESCAPE\_ISPRINT & \ref{env:DARCS_DONT_ESCAPE_nonascii}\\
DARCS\_ESCAPE\_EXTRA & \ref{env:DARCS_DONT_ESCAPE_nonascii}\\
DARCS\_DONT\_ESCAPE\_EXTRA & \ref{env:DARCS_DONT_ESCAPE_nonascii}\\
\hline
\end{tabular}

\section{General-purpose variables}

\darcsEnv{DARCS_EDITOR}
\darcsEnv{DARCS_PAGER}
\darcsEnv{DARCS_TMPDIR}
\darcsEnv{DARCS_KEEP_TMPDIR}
\darcsEnv{HOME}

\section{Remote repositories}
\paragraph{DARCS\_CONNECTION\_TIMEOUT}
\label{env:DARCS_CONNECTION_TIMEOUT}
Set the maximum time in seconds that darcs allows and connection to
take. If the variable is not specified the default are 30 seconds. This
option only works with curl.
\darcsEnv{DARCS_SSH}
\darcsEnv{DARCS_SCP}
\darcsEnv{SSH_PORT}
\darcsEnv{HTTP_PROXY}
\darcsEnv{DARCS_PROXYUSERPWD}

\paragraph{DARCS\_GET\_FOO, DARCS\_MGET\_FOO and DARCS\_APPLY\_FOO}
\label{env:DARCS_X_FOO}
When trying to access a repository with a URL beginning foo://,
darcs will invoke the program specified by the DARCS\_GET\_FOO
environment variable (if defined) to download each file, and the
command specified by the DARCS\_APPLY\_FOO environment variable (if
defined) when pushing to a foo:// URL.  

This method overrides all other ways of getting \verb!foo://xxx! URLs.

Note that each command should be constructed so that it sends the downloaded
content to STDOUT, and the next argument to it should be the URL\@.  Here are some
examples that should work for DARCS\_GET\_HTTP:

\begin{verbatim}
fetch -q -o -  
curl -s -f
lynx -source 
wget -q -O -
\end{verbatim}

Apart from such toy examples, it is likely that you will need to
manipulate the argument before passing it to the actual fetcher
program.  For example, consider the problem of getting read access to
a repository on a CIFS (SMB) share without mount privileges:

\begin{verbatim}
export DARCS_GET_SMB="smbclient -c get"
darcs get smb://fs/twb/Desktop/hello-world
\end{verbatim}

The above command will not work for several reasons.  Firstly, Darcs
will pass it an argument beginning with `smb:', which smbclient does
not understand.  Secondly, the host and share `//fs/twb' must be
presented as a separate argument to the path `Desktop/hello-world'.
Thirdly, smbclient requires that `get' and the path be a single
argument (including a space), rather than two separate arguments.
Finally, smbclient's `get' command writes the file to disk, while
Darcs expects it to be printed to standard output.

In principle, we could get around such problems by making the variable
contain a shell script, e.g.

\begin{verbatim}
export DARCS_GET_SMB='sh -c "...; smbclient $x -c \"get $y\""'
\end{verbatim}

Unfortunately, Darcs splits the command on whitespace and does not
understand that quotation or escaping, so there is no way to make
Darcs pass the text after `-c' to sh as a single argument.  Therefore,
we instead need to put such one-liners in separate, executable scripts.

Continuing our smbclient example, we create an executable script
\verb|~/.darcs/libexec/get_smb| with the following contents:

\begin{verbatim}
#!/bin/bash -e
IFS=/ read host share file <<<"${1#smb://}"
smbclient //$host/$share -c "get $file -"
\end{verbatim}

And at last we can say

\begin{verbatim}
export DARCS_GET_SMB=~/.darcs/libexec/get_smb
darcs get smb://fs/twb/Desktop/hello-world
\end{verbatim}


If set, DARCS\_MGET\_FOO
will be used to fetch many files from a single repository simultaneously.
Replace FOO and foo as appropriate to handle other URL schemes.
These commands are \emph{not} interpreted by a shell, so you cannot
use shell metacharacters, and the first word in the command must
be the name of an executable located in your path. The GET command
will be called with a URL for each file.  The MGET command will be
invoked with a number of URLs and is expected to download the files
to the current directory, preserving the file name but not the path.
The APPLY command will be called with a darcs patchfile piped into
its standard input. Example:

\begin{verbatim}
wget -q 
\end{verbatim}

\paragraph{DARCS\_MGETMAX}
\label{env:DARCS_MGETMAX}
When invoking a DARCS\_MGET\_FOO command, darcs will limit the
number of URLs presented to the command to the value of this variable,
if set, or 200.

These commands are \emph{not} interpreted by a shell, so you cannot use shell
meta-characters.

\section{Highlighted output}
\label{env:DARCS_ALWAYS_COLOR}
\label{env:DARCS_DO_COLOR_LINES}
\label{env:DARCS_DONT_ESCAPE_white}

If the terminal understands ANSI color escape sequences,
darcs will highlight certain keywords and delimiters when printing patches.
This can be turned off by setting the environment variable DARCS\_DONT\_COLOR to 1.
If you use a pager that happens to understand ANSI colors, like \verb!less -R!,
darcs can be forced always to highlight the output
by setting DARCS\_ALWAYS\_COLOR to 1.
If you can't see colors you can set DARCS\_ALTERNATIVE\_COLOR to 1,
and darcs will use ANSI codes for bold and reverse video instead of colors.
In addition, there is an extra-colorful mode, which is not enabled by
default, which can be activated with DARCS\_DO\_COLOR\_LINES.

By default darcs will escape (by highlighting if possible) any kind of spaces at the end of lines
when showing patch contents.
If you don't want this you can turn it off by setting
DARCS\_DONT\_ESCAPE\_TRAILING\_SPACES to 1.
A special case exists for only carriage returns:
DARCS\_DONT\_ESCAPE\_TRAILING\_CR.


\section{Character escaping and non-ASCII character encodings}
\label{env:DARCS_DONT_ESCAPE_nonascii}

Darcs needs to escape certain characters when printing patch contents to a terminal.
Characters like \emph{backspace} can otherwise hide patch content from the user,
and other character sequences can even in some cases redirect commands to the shell
if the terminal allows it.

By default darcs will only allow printable 7-bit ASCII characters (including space),
and the two control characters \emph{tab} and \emph{newline}.
(See the last paragraph in this section for a way to tailor this behavior.)
All other octets are printed in quoted form (as \verb!^<control letter>! or
\verb!\!\verb!<hex code>!).

Darcs has some limited support for locales.
If the system's locale is a single-byte character encoding,
like the Latin encodings,
you can set the environment variable DARCS\_DONT\_ESCAPE\_ISPRINT to 1
and darcs will display all the printables in the current system locale
instead of just the ASCII ones.
NOTE: This curently does not work on some architectures if darcs is
compiled with GHC~6.4 or later. Some non-ASCII control characters might be printed
and can possibly spoof the terminal.

For multi-byte character encodings things are less smooth.
UTF-8 will work if you set DARCS\_DONT\_ESCAPE\_8BIT to 1,
but non-printables outside the 7-bit ASCII range are no longer escaped.
E.g., the extra control characters from Latin-1
might leave your terminal at the mercy of the patch contents.
Space characters outside the 7-bit ASCII range are no longer recognized
and will not be properly escaped at line endings.

As a last resort you can set DARCS\_DONT\_ESCAPE\_ANYTHING to 1.
Then everything that doesn't flip code sets should work,
and so will all the bells and whistles in your terminal.
This environment variable can also be handy
if you pipe the output to a pager or external filter
that knows better than darcs how to handle your encoding.
Note that \emph{all} escaping,
including the special escaping of any line ending spaces,
will be turned off by this setting.

There are two environment variables you can set
to explicitly tell darcs to not escape or escape octets.
They are
DARCS\_DONT\_ESCAPE\_EXTRA and DARCS\_ESCAPE\_EXTRA.
Their values should be strings consisting of the verbatim octets in question.
The do-escapes take precedence over the dont-escapes.
Space characters are still escaped at line endings though.
The special environment variable DARCS\_DONT\_ESCAPE\_TRAILING\_CR
turns off escaping of carriage return last on the line (DOS style).
