Find git executable at run time
[git-darcs-import.git] / src / Darcs / Commands / SetPref.lhs
blob9a494cedd2e441a2cb45592de87869fbd3b72828
1 % Copyright (C) 2003 David Roundy
3 % This program is free software; you can redistribute it and/or modify
4 % it under the terms of the GNU General Public License as published by
5 % the Free Software Foundation; either version 2, or (at your option)
6 % any later version.
8 % This program is distributed in the hope that it will be useful,
9 % but WITHOUT ANY WARRANTY; without even the implied warranty of
10 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 % GNU General Public License for more details.
13 % You should have received a copy of the GNU General Public License
14 % along with this program; see the file COPYING. If not, write to
15 % the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 % Boston, MA 02110-1301, USA.
18 \subsection{darcs setpref}
19 \begin{code}
20 {-# OPTIONS_GHC -cpp #-}
21 {-# LANGUAGE CPP #-}
23 module Darcs.Commands.SetPref ( setpref ) where
25 import System.Exit ( exitWith, ExitCode(..) )
26 import Control.Monad (when)
28 import Darcs.Commands ( DarcsCommand(..), nodefaults )
29 import Darcs.Arguments ( DarcsFlag, working_repo_dir, umask_option )
30 import Darcs.Repository ( amInRepository, add_to_pending, withRepoLock, ($-) )
31 import Darcs.Patch ( changepref )
32 import Darcs.Ordered ( FL(..) )
33 import Darcs.Repository.Prefs ( get_prefval, change_prefval, )
34 #include "impossible.h"
35 \end{code}
37 \begin{code}
38 setpref_description :: String
39 setpref_description =
40 "Set a value for a preference (test, predist, ...)."
41 \end{code}
43 \options{setpref}
44 Usage example:
45 \begin{verbatim}
46 % darcs setpref test "echo I am not really testing anything."
47 \end{verbatim}
49 \haskell{setpref_help} If you just want to set the pref value in your
50 repository only, you can just edit ``\verb!_darcs/prefs/prefs!''. Changes
51 you make in that file will be preserved.
53 The ``\verb!_darcs/prefs/prefs!'' holds the only preferences information
54 that can propagate between repositories by pushes and pulls, and the only
55 way this happens is when the setprefs command is used. Note that although
56 prefs settings are included in patches, they are \emph{not} fully version
57 controlled. In particular, depending on the order in which a series of
58 merges is performed, you may end up with a different final prefs
59 configuration. In practice I don't expect this to be a problem, as the
60 prefs usually won't be changed very often.
62 \begin{code}
63 valid_pref_data :: [String]
64 valid_pref_data = ["test", "predist", "boringfile", "binariesfile"]
65 \end{code}
66 The following values are valid preferences options which can be configured
67 using setpref:
68 \begin{itemize}
69 \item ``test'' --- the command to run as a test script.
70 \item ``predist'' --- a command to run prior to tarring up a distribution
71 tarball. Typically this would consist of autoconf and/or automake.
72 \item ``boringfile'' --- the name of a file to read instead of the
73 ``boring'' prefs file.
74 \item ``binariesfile'' --- the name of a file to read instead of the
75 ``binaries'' prefs file.
76 \end{itemize}
78 \begin{code}
79 setpref_help :: String
80 setpref_help =
81 "Setpref allows you to set a preference value in a way that will\n"++
82 "propagate to other repositories.\n\n"++
83 "Valid preferences are: "++unwords valid_pref_data++".\n"
84 \end{code}
86 \begin{code}
87 setpref :: DarcsCommand
88 setpref = DarcsCommand {command_name = "setpref",
89 command_help = setpref_help,
90 command_description = setpref_description,
91 command_extra_args = 2,
92 command_extra_arg_help = ["<PREF>",
93 "<VALUE>"],
94 command_command = setpref_cmd,
95 command_prereq = amInRepository,
96 command_get_arg_possibilities = return valid_pref_data,
97 command_argdefaults = nodefaults,
98 command_advanced_options = [umask_option],
99 command_basic_options =
100 [working_repo_dir]}
101 \end{code}
103 \begin{code}
104 setpref_cmd :: [DarcsFlag] -> [String] -> IO ()
105 setpref_cmd opts [pref,val] = withRepoLock opts $- \repository -> do
106 when (' ' `elem` pref) $ do
107 putStrLn $ "'"++pref++
108 "' is not a valid preference name: no spaces allowed!"
109 exitWith $ ExitFailure 1
110 when (not $ pref `elem` valid_pref_data) $ do
111 putStrLn $ "'"++pref++"' is not a valid preference name!"
112 putStrLn $ "Try one of: "++unwords valid_pref_data++""
113 exitWith $ ExitFailure 1
114 oval <- get_prefval pref
115 old <- case oval of Just v -> return v
116 Nothing -> return ""
117 change_prefval pref old val
118 putStrLn $ "Changing value of "++pref++" from '"++old++"' to '"++val++"'"
119 add_to_pending repository (changepref pref old val :>: NilFL)
120 setpref_cmd _ _ = impossible
121 \end{code}