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.
10 * Adds a file or directory to the RCS source repository. For a file,
11 * the entry is marked as "needing to be added" in the user's own CVS
12 * directory, and really added to the repository when it is committed.
13 * For a directory, it is added at the appropriate place in the source
14 * repository and a CVS directory is generated within the directory.
16 * The -m option is currently the only supported option. Some may wish to
17 * supply standard "rcs" options here, but I've found that this causes more
18 * trouble than anything else.
20 * The user files or directories must already exist. For a directory, it must
21 * not already have a CVS file in it.
23 * An "add" on a file that has been "remove"d but not committed will cause the
24 * file to be resurrected.
31 static int add_directory PROTO ((struct file_info *finfo));
32 static int build_entry PROTO((char *repository, char *user, char *options,
33 char *message, List * entries, char *tag));
35 static const char *const add_usage[] =
37 "Usage: %s %s [-k rcs-kflag] [-m message] files...\n",
38 "\t-k\tUse \"rcs-kflag\" to add the file with the specified kflag.\n",
39 "\t-m\tUse \"message\" for the creation log.\n",
40 "(Specify the --help global option for a list of other help options)\n",
59 /* Nonzero if we found a slash, and are thus adding files in a
63 if (argc == 1 || argc == -1)
70 while ((c = getopt (argc, argv, "+k:m:")) != -1)
77 options = RCS_check_kflag (optarg);
81 message = xstrdup (optarg);
95 /* First some sanity checks. I know that the CVS case is (sort of)
96 also handled by add_directory, but we need to check here so the
97 client won't get all confused in send_file_names. */
98 for (i = 0; i < argc; i++)
102 /* If it were up to me I'd probably make this a fatal error.
103 But some people are really fond of their "cvs add *", and
104 don't seem to object to the warnings.
106 strip_trailing_slashes (argv[i]);
107 if (strcmp (argv[i], ".") == 0
108 || strcmp (argv[i], "..") == 0
109 || fncmp (argv[i], CVSADM) == 0)
111 error (0, 0, "cannot add special file `%s'; skipping", argv[i]);
133 /* FIXME: We don't do anything about free'ing argv[i]. But
134 the problem is that it is only sometimes allocated (see
137 for (j = i; j < argc - 1; ++j)
138 argv[j] = argv[j + 1];
140 /* Check the new argv[i] again. */
146 #ifdef CLIENT_SUPPORT
152 /* We snipped out all the arguments in the above sanity
153 check. We can just forget the whole thing (and we
154 better, because if we fired up the server and passed it
155 nothing, it would spit back a usage message). */
160 if (options) send_arg(options);
161 option_with_arg ("-m", message);
163 /* If !found_slash, refrain from sending "Directory", for
164 CVS 1.9 compatibility. If we only tried to deal with servers
165 which are at least CVS 1.9.26 or so, we wouldn't have to
166 special-case this. */
169 repository = Name_Repository (NULL, NULL);
170 send_a_repository ("", repository, "");
174 for (i = 0; i < argc; ++i)
175 /* FIXME: Does this erroneously call Create_Admin in error
176 conditions which are only detected once the server gets its
186 /* This is some mungeable storage into which we can point
187 with p and/or update_dir. */
193 filedir = xstrdup (argv[i]);
194 p = last_component (filedir);
202 update_dir = filedir;
203 if (CVS_CHDIR (update_dir) < 0)
205 "could not chdir to %s", update_dir);
208 /* find the repository associated with our current dir */
209 repository = Name_Repository (NULL, update_dir);
211 /* before we do anything else, see if we have any
212 per-directory tags */
213 ParseTag (&tag, &date, &nonbranch);
215 rcsdir = xmalloc (strlen (repository) + strlen (p) + 5);
216 sprintf (rcsdir, "%s/%s", repository, p);
218 Create_Admin (p, argv[i], rcsdir, tag, date,
222 send_a_repository ("", repository, update_dir);
224 if (restore_cwd (&cwd, NULL))
235 Subdir_Register ((List *) NULL, (char *) NULL, argv[i]);
238 Subdir_Register ((List *) NULL, update_dir, p);
243 send_file_names (argc, argv, SEND_EXPAND_WILD);
244 send_files (argc, argv, 0, 0, SEND_BUILD_DIRS | SEND_NO_CONTENTS);
245 send_to_server ("add\012", 0);
248 return err + get_responses_and_close ();
252 /* walk the arg list adding files/dirs */
253 for (i = 0; i < argc; i++)
256 #ifdef SERVER_SUPPORT
257 int begin_added_files = added_files;
259 struct file_info finfo;
262 memset (&finfo, 0, sizeof finfo);
267 finfo.fullname = xstrdup (argv[i]);
268 p = last_component (argv[i]);
271 finfo.update_dir = "";
277 finfo.update_dir = argv[i];
279 if (CVS_CHDIR (finfo.update_dir) < 0)
280 error (1, errno, "could not chdir to %s", finfo.update_dir);
283 /* Add wrappers for this directory. They exist only until
284 the next call to wrap_add_file. */
285 wrap_add_file (CVSDOTWRAPPER, 1);
289 /* Find the repository associated with our current dir. */
290 repository = Name_Repository (NULL, finfo.update_dir);
292 entries = Entries_Open (0, NULL);
294 finfo.repository = repository;
295 finfo.entries = entries;
297 /* We pass force_tag_match as 1. If the directory has a
298 sticky branch tag, and there is already an RCS file which
299 does not have that tag, then the head revision is
300 meaningless to us. */
301 vers = Version_TS (&finfo, options, NULL, NULL, 1, 0);
302 if (vers->vn_user == NULL)
304 /* No entry available, ts_rcs is invalid */
305 if (vers->vn_rcs == NULL)
307 /* There is no RCS file either */
308 if (vers->ts_user == NULL)
310 /* There is no user file either */
311 error (0, 0, "nothing known about %s", finfo.fullname);
314 else if (!isdir (finfo.file)
315 || wrap_name_has (finfo.file, WRAP_TOCVS))
318 * See if a directory exists in the repository with
319 * the same name. If so, blow this request off.
321 char *dname = xmalloc (strlen (repository)
322 + strlen (finfo.file)
324 (void) sprintf (dname, "%s/%s", repository, finfo.file);
328 "cannot add file `%s' since the directory",
330 error (0, 0, "`%s' already exists in the repository",
332 error (1, 0, "illegal filename overlap");
336 if (vers->options == NULL || *vers->options == '\0')
338 /* No options specified on command line (or in
339 rcs file if it existed, e.g. the file exists
340 on another branch). Check for a value from
341 the wrapper stuff. */
342 if (wrap_name_has (finfo.file, WRAP_RCSOPTION))
345 free (vers->options);
346 vers->options = wrap_rcsoption (finfo.file, 1);
353 "cannot add file on non-branch tag %s",
359 /* There is a user file, so build the entry for it */
360 if (build_entry (repository, finfo.file, vers->options,
361 message, entries, vers->tag) != 0)
370 scheduling %s `%s' for addition on branch `%s'",
371 (wrap_name_has (finfo.file,
375 finfo.fullname, vers->tag);
378 "scheduling %s `%s' for addition",
379 (wrap_name_has (finfo.file,
389 else if (RCS_isdead (vers->srcfile, vers->vn_rcs))
391 if (isdir (finfo.file)
392 && !wrap_name_has (finfo.file, WRAP_TOCVS))
395 the directory `%s' cannot be added because a file of the", finfo.fullname);
397 same name already exists in the repository.");
404 "cannot add file on non-branch tag %s",
412 file `%s' will be added on branch `%s' from version %s",
413 finfo.fullname, vers->tag, vers->vn_rcs);
415 /* I'm not sure that mentioning
416 vers->vn_rcs makes any sense here; I
417 can't think of a way to word the
418 message which is not confusing. */
420 re-adding file %s (in place of dead revision %s)",
421 finfo.fullname, vers->vn_rcs);
422 Register (entries, finfo.file, "0", vers->ts_user,
424 vers->tag, NULL, NULL);
432 * There is an RCS file already, so somebody else must've
435 error (0, 0, "%s added independently by second party",
440 else if (vers->vn_user[0] == '0' && vers->vn_user[1] == '\0')
444 * An entry for a new-born file, ts_rcs is dummy, but that is
447 error (0, 0, "%s has already been entered", finfo.fullname);
450 else if (vers->vn_user[0] == '-')
452 /* An entry for a removed file, ts_rcs is invalid */
453 if (vers->ts_user == NULL)
455 /* There is no user file (as it should be) */
456 if (vers->vn_rcs == NULL)
460 * There is no RCS file, so somebody else must've removed
464 cannot resurrect %s; RCS file removed by second party", finfo.fullname);
471 * There is an RCS file, so remove the "-" from the
472 * version number and restore the file
474 char *tmp = xmalloc (strlen (finfo.file) + 50);
476 (void) strcpy (tmp, vers->vn_user + 1);
477 (void) strcpy (vers->vn_user, tmp);
478 (void) sprintf (tmp, "Resurrected %s", finfo.file);
479 Register (entries, finfo.file, vers->vn_user, tmp,
481 vers->tag, vers->date, vers->ts_conflict);
484 /* XXX - bugs here; this really resurrect the head */
485 /* Note that this depends on the Register above actually
486 having written Entries, or else it won't really
487 check the file out. */
488 if (update (2, argv + i - 1) == 0)
490 error (0, 0, "%s, version %s, resurrected",
496 error (0, 0, "could not resurrect %s", finfo.fullname);
503 /* The user file shouldn't be there */
505 %s should be removed and is still there (or is back again)", finfo.fullname);
511 /* A normal entry, ts_rcs is valid, so it must already be there */
512 error (0, 0, "%s already exists, with version number %s",
519 /* passed all the checks. Go ahead and add it if its a directory */
521 && isdir (finfo.file)
522 && !wrap_name_has (finfo.file, WRAP_TOCVS))
524 err += add_directory (&finfo);
528 #ifdef SERVER_SUPPORT
529 if (server_active && begin_added_files != added_files)
530 server_checked_in (finfo.file, finfo.update_dir, repository);
534 Entries_Close (entries);
536 if (restore_cwd (&cwd, NULL))
540 free (finfo.fullname);
543 error (0, 0, "use '%s commit' to add %s permanently",
545 (added_files == 1) ? "this file" : "these files");
554 * The specified user file is really a directory. So, let's make sure that
555 * it is created in the RCS source repository, and that the user's directory
556 * is updated to include a CVS directory.
558 * Returns 1 on failure, 0 on success.
561 add_directory (finfo)
562 struct file_info *finfo;
564 char *repository = finfo->repository;
565 List *entries = finfo->entries;
566 char *dir = finfo->file;
569 struct saved_cwd cwd;
570 char *message = NULL;
575 if (strchr (dir, '/') != NULL)
577 /* "Can't happen". */
579 "directory %s not added; must be a direct sub-directory", dir);
582 if (fncmp (dir, CVSADM) == 0)
584 error (0, 0, "cannot add a `%s' directory", CVSADM);
588 /* before we do anything else, see if we have any per-directory tags */
589 ParseTag (&tag, &date, &nonbranch);
591 /* Remember the default attributes from this directory, so we can apply
592 them to the new directory. */
593 fileattr_startdir (repository);
594 attrs = fileattr_getall (NULL);
597 /* now, remember where we were, so we can get back */
600 if ( CVS_CHDIR (dir) < 0)
602 error (0, errno, "cannot chdir to %s", finfo->fullname);
605 #ifdef SERVER_SUPPORT
606 if (!server_active && isfile (CVSADM))
611 error (0, 0, "%s/%s already exists", finfo->fullname, CVSADM);
615 rcsdir = xmalloc (strlen (repository) + strlen (dir) + 5);
616 sprintf (rcsdir, "%s/%s", repository, dir);
617 if (isfile (rcsdir) && !isdir (rcsdir))
619 error (0, 0, "%s is not a directory; %s not added", rcsdir,
624 /* setup the log message */
625 message = xmalloc (strlen (rcsdir)
627 + (tag == NULL ? 0 : strlen (tag) + 80)
628 + (date == NULL ? 0 : strlen (date) + 80));
629 (void) sprintf (message, "Directory %s added to the repository\n", rcsdir);
632 (void) strcat (message, "--> Using per-directory sticky tag `");
633 (void) strcat (message, tag);
634 (void) strcat (message, "'\n");
638 (void) strcat (message, "--> Using per-directory sticky date `");
639 (void) strcat (message, date);
640 (void) strcat (message, "'\n");
648 struct logfile_info *li;
650 /* There used to be some code here which would prompt for
651 whether to add the directory. The details of that code had
652 bitrotted, but more to the point it can't work
653 client/server, doesn't ask in the right way for GUIs, etc.
654 A better way of making it harder to accidentally add
655 directories would be to have to add and commit directories
656 like for files. The code was #if 0'd at least since CVS 1.5. */
660 omask = umask (cvsumask);
661 if (CVS_MKDIR (rcsdir, 0777) < 0)
663 error (0, errno, "cannot mkdir %s", rcsdir);
664 (void) umask (omask);
667 (void) umask (omask);
670 /* Now set the default file attributes to the ones we inherited
671 from the parent directory. */
672 fileattr_startdir (rcsdir);
673 fileattr_setall (NULL, attrs);
680 * Set up an update list with a single title node for Update_Logfile
685 p->delproc = update_delproc;
686 p->key = xstrdup ("- New directory");
687 li = (struct logfile_info *) xmalloc (sizeof (struct logfile_info));
689 li->tag = xstrdup (tag);
690 li->rev_old = li->rev_new = NULL;
691 p->data = (char *) li;
692 (void) addnode (ulist, p);
693 Update_Logfile (rcsdir, message, (FILE *) NULL, ulist);
697 #ifdef SERVER_SUPPORT
699 Create_Admin (".", finfo->fullname, rcsdir, tag, date, nonbranch, 0);
701 Create_Admin (".", finfo->fullname, rcsdir, tag, date, nonbranch, 0);
708 if (restore_cwd (&cwd, NULL))
712 Subdir_Register (entries, (char *) NULL, dir);
714 cvs_output (message, 0);
722 if (restore_cwd (&cwd, NULL))
731 * Builds an entry for a new file and sets up "CVS/file",[pt] by
732 * interrogating the user. Returns non-zero on error.
735 build_entry (repository, user, options, message, entries, tag)
751 * The requested log is read directly from the user and stored in the
752 * file user,t. If the "message" argument is set, use it as the
753 * initial creation log (which typically describes the file).
755 fname = xmalloc (strlen (user) + 80);
756 (void) sprintf (fname, "%s/%s%s", CVSADM, user, CVSEXT_LOG);
757 fp = open_file (fname, "w+");
758 if (message && fputs (message, fp) == EOF)
759 error (1, errno, "cannot write to %s", fname);
760 if (fclose(fp) == EOF)
761 error(1, errno, "cannot close %s", fname);
765 * Create the entry now, since this allows the user to interrupt us above
766 * without needing to clean anything up (well, we could clean up the
767 * ,t file, but who cares).
769 line = xmalloc (strlen (user) + 20);
770 (void) sprintf (line, "Initial %s", user);
771 Register (entries, user, "0", line, options, tag, (char *) 0, (char *) 0);