2 * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
4 * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
7 * Portions Copyright (C) 1992, Brian Berliner and Jeff Polk
8 * Portions Copyright (C) 1989-1992, Brian Berliner
10 * You may distribute under the terms of the GNU General Public License as
11 * specified in the README file that comes with the CVS source distribution.
18 /* Determine the name of the RCS repository for directory DIR in the
19 current working directory, or for the current working directory
20 itself if DIR is NULL. Returns the name in a newly-malloc'd
21 string. On error, gives a fatal error and does not return.
22 UPDATE_DIR is the path from where cvs was invoked (for use in error
23 messages), and should contain DIR as its last component.
24 UPDATE_DIR can be NULL to signify the directory in which cvs was
28 Name_Repository (dir, update_dir)
30 const char *update_dir;
33 const char *xupdate_dir;
35 size_t repos_allocated = 0;
39 if (update_dir && *update_dir)
40 xupdate_dir = update_dir;
46 tmp = xmalloc (strlen (dir) + sizeof (CVSADM_REP) + 10);
47 (void) sprintf (tmp, "%s/%s", dir, CVSADM_REP);
50 tmp = xstrdup (CVSADM_REP);
53 * The assumption here is that the repository is always contained in the
54 * first line of the "Repository" file.
56 fpin = CVS_FOPEN (tmp, "r");
60 int save_errno = errno;
65 cvsadm = xmalloc (strlen (dir) + sizeof (CVSADM) + 10);
66 (void) sprintf (cvsadm, "%s/%s", dir, CVSADM);
69 cvsadm = xstrdup (CVSADM);
73 error (0, 0, "in directory %s:", xupdate_dir);
74 error (1, 0, "there is no version here; do '%s checkout' first",
79 if (existence_error (save_errno))
81 /* FIXME: This is a very poorly worded error message. It
82 occurs at least in the case where the user manually
83 creates a directory named CVS, so the error message
84 should be more along the lines of "CVS directory found
85 without administrative files; use CVS to create the CVS
86 directory, or rename it to something else if the
87 intention is to store something besides CVS
88 administrative files". */
89 error (0, 0, "in directory %s:", xupdate_dir);
90 error (1, 0, "*PANIC* administration files missing");
93 error (1, save_errno, "cannot open %s", tmp);
96 if (getline (&repos, &repos_allocated, fpin) < 0)
98 /* FIXME: should be checking for end of file separately. */
99 error (0, 0, "in directory %s:", xupdate_dir);
100 error (1, errno, "cannot read %s", CVSADM_REP);
102 if (fclose (fpin) < 0)
103 error (0, errno, "cannot close %s", tmp);
106 if ((cp = strrchr (repos, '\n')) != NULL)
107 *cp = '\0'; /* strip the newline */
110 * If this is a relative repository pathname, turn it into an absolute
111 * one by tacking on the CVSROOT environment variable. If the CVSROOT
112 * environment variable is not set, die now.
114 if (! isabsolute(repos))
118 if (current_parsed_root == NULL)
120 error (0, 0, "in directory %s:", xupdate_dir);
121 error (0, 0, "must set the CVSROOT environment variable\n");
122 error (0, 0, "or specify the '-d' option to %s.", program_name);
123 error (1, 0, "illegal repository setting");
125 if (pathname_levels (repos) > 0)
127 error (0, 0, "in directory %s:", xupdate_dir);
128 error (0, 0, "`..'-relative repositories are not supported.");
129 error (1, 0, "illegal source repository");
131 newrepos = xmalloc (strlen (current_parsed_root->directory)
132 + strlen (repos) + 2);
133 sprintf (newrepos, "%s/%s", current_parsed_root->directory, repos);
138 Sanitize_Repository_Name (repos);
146 * Return a pointer to the repository name relative to CVSROOT from a
147 * possibly fully qualified repository
150 Short_Repository (repository)
151 const char *repository;
153 if (repository == NULL)
156 /* If repository matches CVSroot at the beginning, strip off CVSroot */
157 /* And skip leading '/' in rep, in case CVSroot ended with '/'. */
158 if (strncmp (current_parsed_root->directory, repository,
159 strlen (current_parsed_root->directory)) == 0)
161 const char *rep = repository + strlen (current_parsed_root->directory);
162 return (*rep == '/') ? rep+1 : rep;
170 /* Sanitize the repository name (in place) by removing trailing
171 * slashes and a trailing "." if present. It should be safe for
172 * callers to use strcat and friends to create repository names.
173 * Without this check, names like "/path/to/repos/./foo" and
174 * "/path/to/repos//foo" would be created. For example, one
175 * significant case is the CVSROOT-detection code in commit.c. It
176 * decides whether or not it needs to rebuild the administrative file
177 * database by doing a string compare. If we've done a `cvs co .' to
178 * get the CVSROOT files, "/path/to/repos/./CVSROOT" and
179 * "/path/to/repos/CVSROOT" are the arguments that are compared!
181 * This function ends up being called from the same places as
182 * strip_path, though what it does is much more conservative. Many
183 * comments about this operation (which was scattered around in
184 * several places in the source code) ran thus:
186 * ``repository ends with "/."; omit it. This sort of thing used
187 * to be taken care of by strip_path. Now we try to be more
188 * selective. I suspect that it would be even better to push it
189 * back further someday, so that the trailing "/." doesn't get into
190 * repository in the first place, but we haven't taken things that
191 * far yet.'' --Jim Kingdon (recurse.c, 07-Sep-97)
195 Sanitize_Repository_Name (repository)
200 assert (repository != NULL);
202 strip_trailing_slashes (repository);
204 len = strlen (repository);
206 && repository[len - 1] == '.'
207 && ISDIRSEP (repository[len - 2]))
209 repository[len - 2] = '\0';