rcscmds.c revision 25839
1/*
2 * Copyright (c) 1992, Brian Berliner and Jeff Polk
3 * Copyright (c) 1989-1992, Brian Berliner
4 *
5 * You may distribute under the terms of the GNU General Public License as
6 * specified in the README file that comes with the CVS 1.4 kit.
7 *
8 * The functions in this file provide an interface for performing
9 * operations directly on RCS files.
10 */
11
12#include "cvs.h"
13#include <assert.h>
14
15/* This file, rcs.h, and rcs.c, are intended to define our interface
16   to RCS files.  As of July, 1996, there are still a few places that
17   still exec RCS commands directly.  The intended long-term direction
18   is to have CVS access RCS files via an RCS library (rcs.c can be
19   considered a start at one), for performance, cleanliness (CVS has
20   some awful hacks to work around RCS behaviors which don't make
21   sense for CVS), installation hassles, ease of implementing the CVS
22   server (I don't think that the output-out-of-order bug can be
23   completely fixed as long as CVS calls RCS), and perhaps other
24   reasons.
25
26   Whether there will also be a version of RCS which uses this
27   library, or whether the library will be packaged for uses beyond
28   CVS or RCS (many people would like such a thing) is an open
29   question.  Some considerations:
30
31   1.  An RCS library for CVS must have the capabilities of the
32   existing CVS code which accesses RCS files.  In particular, simple
33   approaches will often be slow.
34
35   2.  An RCS library should not use the code from the current RCS
36   (5.7 and its ancestors).  The code has many problems.  Too few
37   comments, too many layers of abstraction, too many global variables
38   (the correct number for a library is zero), too much intricately
39   interwoven functionality, and too many clever hacks.  Paul Eggert,
40   the current RCS maintainer, agrees.
41
42   3.  More work needs to be done in terms of separating out the RCS
43   library from the rest of CVS (for example, cvs_output should be
44   replaced by a callback, and the declarations should be centralized
45   into rcs.h, and probably other such cleanups).
46
47   4.  To be useful for RCS and perhaps for other uses, the library
48   may need features beyond those needed by CVS.
49
50   5.  Any changes to the RCS file format *must* be compatible.  Many,
51   many tools (not just CVS and RCS) can at least import this format.
52   RCS and CVS must preserve the current ability to import/export it
53   (preferably improved--magic branches are currently a roadblock).
54   See doc/RCSFILES in the CVS distribution for documentation of this
55   file format.
56
57   On somewhat related notes:
58
59   1.  A library for diff is an obvious idea.  The one thing which I'm
60   not so sure about is that I think CVS probably wants the ability to
61   allow arbitrarily-bizarre (and possibly customized for particular
62   file formats) external diff programs.
63
64   2.  A library for patch is another such idea.  CVS's needs are
65   smaller than the functionality of the standalone patch program (it
66   only calls patch in the client, and only needs to be able to patch
67   unmodified versions, which is something that RCS_deltas already
68   does in a different context).  But it is silly for CVS to be making
69   people install patch as well as CVS for such a simple purpose.  */
70
71/* For RCS file PATH, make symbolic tag TAG point to revision REV.
72   This validates that TAG is OK for a user to use.  Return value is
73   -1 for error (and errno is set to indicate the error), positive for
74   error (and an error message has been printed), or zero for success.  */
75
76int
77RCS_exec_settag(path, tag, rev)
78    const char *path;
79    const char *tag;
80    const char *rev;
81{
82    run_setup ("%s%s -x,v/ -q -N%s:%s", Rcsbin, RCS, tag, rev);
83    run_arg (path);
84    return run_exec (RUN_TTY, RUN_TTY, RUN_TTY, RUN_NORMAL);
85}
86
87/* NOERR is 1 to suppress errors--FIXME it would
88   be better to avoid the errors or some cleaner solution.  */
89int
90RCS_exec_deltag(path, tag, noerr)
91    const char *path;
92    const char *tag;
93    int noerr;
94{
95    run_setup ("%s%s -x,v/ -q -N%s", Rcsbin, RCS, tag);
96    run_arg (path);
97    return run_exec (RUN_TTY, RUN_TTY, noerr ? DEVNULL : RUN_TTY, RUN_NORMAL);
98}
99
100/* set RCS branch to REV */
101int
102RCS_exec_setbranch(path, rev)
103    const char *path;
104    const char *rev;
105{
106    run_setup ("%s%s -x,v/ -q -b%s", Rcsbin, RCS, rev ? rev : "");
107    run_arg (path);
108    return run_exec (RUN_TTY, RUN_TTY, RUN_TTY, RUN_NORMAL);
109}
110
111/* Lock revision REV.  NOERR is 1 to suppress errors--FIXME it would
112   be better to avoid the errors or some cleaner solution.  */
113int
114RCS_exec_lock(path, rev, noerr)
115    const char *path;
116    const char *rev;
117    int noerr;
118{
119    run_setup ("%s%s -x,v/ -q -l%s", Rcsbin, RCS, rev ? rev : "");
120    run_arg (path);
121    return run_exec (RUN_TTY, RUN_TTY, noerr ? DEVNULL : RUN_TTY, RUN_NORMAL);
122}
123
124/* Unlock revision REV.  NOERR is 1 to suppress errors--FIXME it would
125   be better to avoid the errors or some cleaner solution.  */
126int
127RCS_exec_unlock(path, rev, noerr)
128    const char *path;
129    const char *rev;
130    int noerr;
131{
132    run_setup ("%s%s -x,v/ -q -u%s", Rcsbin, RCS, rev ? rev : "");
133    run_arg (path);
134    return run_exec (RUN_TTY, RUN_TTY, noerr ? DEVNULL : RUN_TTY, RUN_NORMAL);
135}
136
137/* Merge revisions REV1 and REV2. */
138int
139RCS_merge(path, options, rev1, rev2)
140     const char *path;
141     const char *options;
142     const char *rev1;
143     const char *rev2;
144{
145    int status;
146
147    /* XXX - Do merge by hand instead of using rcsmerge, due to -k handling */
148
149    run_setup ("%s%s -x,v/ %s -r%s -r%s %s", Rcsbin, RCS_RCSMERGE,
150	       options, rev1, rev2, path);
151    status = run_exec (RUN_TTY, RUN_TTY, RUN_TTY, RUN_NORMAL);
152#ifndef HAVE_RCS5
153    if (status == 0)
154    {
155	error (1, 0, "CVS no longer supports RCS versions older than RCS5");
156	/* This case needs to call file_has_markers to see if the file
157	   contains conflict indicators.  But is anyone using the !HAVE_RCS5
158	   code any more?  */
159    }
160#endif
161    return status;
162}
163
164/* Check in to RCSFILE with revision REV (which must be greater than the
165   largest revision) and message MESSAGE (which is checked for legality).
166   If FLAGS & RCS_FLAGS_DEAD, check in a dead revision.  If FLAGS &
167   RCS_FLAGS_QUIET, tell ci to be quiet.  If FLAGS & RCS_FLAGS_MODTIME,
168   use the working file's modification time for the checkin time.
169   WORKFILE is the working file to check in from, or NULL to use the usual
170   RCS rules for deriving it from the RCSFILE.
171
172   Return value is -1 for error (and errno is set to indicate the
173   error), positive for error (and an error message has been printed),
174   or zero for success.  */
175int
176RCS_checkin (rcsfile, workfile, message, rev, flags)
177    char *rcsfile;
178    char *workfile;
179    char *message;
180    char *rev;
181    int flags;
182{
183    /* The desired behavior regarding permissions is to preserve the
184       permissions on RCSFILE if it already exists.  Based on looking
185       at the RCS 5.7 source, it would appear that RCS_CI does this
186       except when it is creating RCSFILE (reasonable), or when
187       RCSFILE was created with rcs -i (this is strange, and quite
188       possibly unintentional).  In those two cases it copies the
189       permissions from the workfile.
190
191       Anyway, the fix is simple enough: we preserve the mode ourself.  */
192    struct stat sb;
193    int fix_mode = 1;
194    int retval;
195
196    if (CVS_STAT (rcsfile, &sb) < 0)
197    {
198	fix_mode = 0;
199	if (!existence_error (errno))
200	    error (0, errno, "warning: cannot stat %s", rcsfile);
201    }
202    run_setup ("%s%s -x,v/ -f %s%s", Rcsbin, RCS_CI,
203	       rev ? "-r" : "", rev ? rev : "");
204    if (flags & RCS_FLAGS_DEAD)
205	run_arg ("-sdead");
206    if (flags & RCS_FLAGS_QUIET)
207	run_arg ("-q");
208    if (flags & RCS_FLAGS_MODTIME)
209	run_arg ("-d");
210    run_args ("-m%s", make_message_rcslegal (message));
211    if (workfile != NULL)
212	run_arg (workfile);
213    run_arg (rcsfile);
214    retval = run_exec (RUN_TTY, RUN_TTY, RUN_TTY, RUN_NORMAL);
215    if (retval == 0 && fix_mode)
216    {
217	if (chmod (rcsfile, sb.st_mode) < 0)
218	    error (0, errno, "warning: cannot change permissions on %s",
219		   rcsfile);
220    }
221    return retval;
222}
223