2 * Copyright (c) 1992, Brian Berliner and Jeff Polk
3 * Copyright (c) 1989-1992, Brian Berliner
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 source distribution.
13 /* Determine the name of the RCS repository for directory DIR in the
14 current working directory, or for the current working directory
15 itself if DIR is NULL. Returns the name in a newly-malloc'd
16 string. On error, gives a fatal error and does not return.
17 UPDATE_DIR is the path from where cvs was invoked (for use in error
18 messages), and should contain DIR as its last component.
19 UPDATE_DIR can be NULL to signify the directory in which cvs was
23 Name_Repository (dir, update_dir)
25 const char *update_dir;
28 const char *xupdate_dir;
30 size_t repos_allocated = 0;
34 if (update_dir && *update_dir)
35 xupdate_dir = update_dir;
41 tmp = xmalloc (strlen (dir) + sizeof (CVSADM_REP) + 10);
42 (void) sprintf (tmp, "%s/%s", dir, CVSADM_REP);
45 tmp = xstrdup (CVSADM_REP);
48 * The assumption here is that the repository is always contained in the
49 * first line of the "Repository" file.
51 fpin = CVS_FOPEN (tmp, "r");
55 int save_errno = errno;
60 cvsadm = xmalloc (strlen (dir) + sizeof (CVSADM) + 10);
61 (void) sprintf (cvsadm, "%s/%s", dir, CVSADM);
64 cvsadm = xstrdup (CVSADM);
68 error (0, 0, "in directory %s:", xupdate_dir);
69 error (1, 0, "there is no version here; do '%s checkout' first",
74 if (existence_error (save_errno))
76 /* FIXME: This is a very poorly worded error message. It
77 occurs at least in the case where the user manually
78 creates a directory named CVS, so the error message
79 should be more along the lines of "CVS directory found
80 without administrative files; use CVS to create the CVS
81 directory, or rename it to something else if the
82 intention is to store something besides CVS
83 administrative files". */
84 error (0, 0, "in directory %s:", xupdate_dir);
85 error (1, 0, "*PANIC* administration files missing");
88 error (1, save_errno, "cannot open %s", tmp);
91 if (getline (&repos, &repos_allocated, fpin) < 0)
93 /* FIXME: should be checking for end of file separately. */
94 error (0, 0, "in directory %s:", xupdate_dir);
95 error (1, errno, "cannot read %s", CVSADM_REP);
97 if (fclose (fpin) < 0)
98 error (0, errno, "cannot close %s", tmp);
101 if ((cp = strrchr (repos, '\n')) != NULL)
102 *cp = '\0'; /* strip the newline */
105 * If this is a relative repository pathname, turn it into an absolute
106 * one by tacking on the CVSROOT environment variable. If the CVSROOT
107 * environment variable is not set, die now.
109 if (! isabsolute(repos))
113 if (current_parsed_root == NULL)
115 error (0, 0, "in directory %s:", xupdate_dir);
116 error (0, 0, "must set the CVSROOT environment variable\n");
117 error (0, 0, "or specify the '-d' option to %s.", program_name);
118 error (1, 0, "illegal repository setting");
120 if (pathname_levels (repos) > 0)
122 error (0, 0, "in directory %s:", xupdate_dir);
123 error (0, 0, "`..'-relative repositories are not supported.");
124 error (1, 0, "illegal source repository");
126 newrepos = xmalloc (strlen (current_parsed_root->directory)
127 + strlen (repos) + 2);
128 sprintf (newrepos, "%s/%s", current_parsed_root->directory, repos);
133 Sanitize_Repository_Name (repos);
141 * Return a pointer to the repository name relative to CVSROOT from a
142 * possibly fully qualified repository
145 Short_Repository (repository)
146 const char *repository;
148 if (repository == NULL)
151 /* If repository matches CVSroot at the beginning, strip off CVSroot */
152 /* And skip leading '/' in rep, in case CVSroot ended with '/'. */
153 if (strncmp (current_parsed_root->directory, repository,
154 strlen (current_parsed_root->directory)) == 0)
156 const char *rep = repository + strlen (current_parsed_root->directory);
157 return (*rep == '/') ? rep+1 : rep;
165 /* Sanitize the repository name (in place) by removing trailing
166 * slashes and a trailing "." if present. It should be safe for
167 * callers to use strcat and friends to create repository names.
168 * Without this check, names like "/path/to/repos/./foo" and
169 * "/path/to/repos//foo" would be created. For example, one
170 * significant case is the CVSROOT-detection code in commit.c. It
171 * decides whether or not it needs to rebuild the administrative file
172 * database by doing a string compare. If we've done a `cvs co .' to
173 * get the CVSROOT files, "/path/to/repos/./CVSROOT" and
174 * "/path/to/repos/CVSROOT" are the arguments that are compared!
176 * This function ends up being called from the same places as
177 * strip_path, though what it does is much more conservative. Many
178 * comments about this operation (which was scattered around in
179 * several places in the source code) ran thus:
181 * ``repository ends with "/."; omit it. This sort of thing used
182 * to be taken care of by strip_path. Now we try to be more
183 * selective. I suspect that it would be even better to push it
184 * back further someday, so that the trailing "/." doesn't get into
185 * repository in the first place, but we haven't taken things that
186 * far yet.'' --Jim Kingdon (recurse.c, 07-Sep-97)
190 Sanitize_Repository_Name (repository)
195 assert (repository != NULL);
197 strip_trailing_slashes (repository);
199 len = strlen (repository);
201 && repository[len - 1] == '.'
202 && ISDIRSEP (repository[len - 2]))
204 repository[len - 2] = '\0';