1 /* This program is free software; you can redistribute it and/or modify
2 it under the terms of the GNU General Public License as published by
3 the Free Software Foundation; either version 2, or (at your option)
6 This program is distributed in the hope that it will be useful,
7 but WITHOUT ANY WARRANTY; without even the implied warranty of
8 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 GNU General Public License for more details. */
12 * .cvsignore file support contributed by David G. Grubbs <dgg@odi.com>
19 * Ignore file section.
21 * "!" may be included any time to reset the list (i.e. ignore nothing);
22 * "*" may be specified to ignore everything. It stays as the first
23 * element forever, unless a "!" clears it out.
26 static char **ign_list; /* List of files to ignore in update
28 static char **s_ign_list = NULL;
29 static int ign_count; /* Number of active entries */
30 static int s_ign_count = 0;
31 static int ign_size; /* This many slots available (plus
33 static int ign_hold = -1; /* Index where first "temporary" item
36 const char *ign_default = ". .. core RCSLOG tags TAGS RCS SCCS .make.state\
37 .nse_depinfo #* .#* cvslog.* ,* CVS CVS.adm .del-* *.a *.olb *.o *.obj\
38 *.so *.Z *~ *.old *.elc *.ln *.bak *.BAK *.orig *.rej *.exe _$* *$";
40 #define IGN_GROW 16 /* grow the list by 16 elements at a
43 /* Nonzero if we have encountered an -I ! directive, which means one should
44 no longer ask the server about what is in CVSROOTADM_IGNORE. */
45 int ign_inhibit_server;
50 * To the "ignore list", add the hard-coded default ignored wildcards above,
51 * the wildcards found in $CVSROOT/CVSROOT/cvsignore, the wildcards found in
52 * ~/.cvsignore and the wildcards found in the CVSIGNORE environment
61 ign_inhibit_server = 0;
63 /* Start with default list and special case */
64 tmp = xstrdup (ign_default);
69 /* The client handles another way, by (after it does its own ignore file
70 processing, and only if !ign_inhibit_server), letting the server
71 know about the files and letting it decide whether to ignore
72 them based on CVSROOOTADM_IGNORE. */
73 if (!current_parsed_root->isremote)
76 char *file = xmalloc (strlen (current_parsed_root->directory) + sizeof (CVSROOTADM)
77 + sizeof (CVSROOTADM_IGNORE) + 10);
78 /* Then add entries found in repository, if it exists */
79 (void) sprintf (file, "%s/%s/%s", current_parsed_root->directory,
80 CVSROOTADM, CVSROOTADM_IGNORE);
81 ign_add_file (file, 0);
85 /* Then add entries found in home dir, (if user has one) and file exists */
86 home_dir = get_homedir ();
87 /* If we can't find a home directory, ignore ~/.cvsignore. This may
88 make tracking down problems a bit of a pain, but on the other
89 hand it might be obnoxious to complain when CVS will function
90 just fine without .cvsignore (and many users won't even know what
94 char *file = strcat_filename_onto_homedir (home_dir, CVSDOTIGNORE);
95 ign_add_file (file, 0);
99 /* Then add entries found in CVSIGNORE environment variable. */
100 ign_add (getenv (IGNORE_ENV), 0);
102 /* Later, add ignore entries found in -I arguments */
108 * Open a file and read lines, feeding each line to a line parser. Arrange
109 * for keeping a temporary list of wildcards at the end, if the "hold"
113 ign_add_file (file, hold)
119 size_t line_allocated = 0;
121 /* restore the saved list (if any) */
122 if (s_ign_list != NULL)
126 for (i = 0; i < s_ign_count; i++)
127 ign_list[i] = s_ign_list[i];
128 ign_count = s_ign_count;
129 ign_list[ign_count] = NULL;
136 /* is this a temporary ignore file? */
139 /* re-set if we had already done a temporary file */
144 for (i = ign_hold; i < ign_count; i++)
146 ign_count = ign_hold;
147 ign_list[ign_count] = NULL;
151 ign_hold = ign_count;
156 fp = CVS_FOPEN (file, "r");
159 if (! existence_error (errno))
160 error (0, errno, "cannot open %s", file);
163 while (getline (&line, &line_allocated, fp) >= 0)
164 ign_add (line, hold);
166 error (0, errno, "cannot read %s", file);
168 error (0, errno, "cannot close %s", file);
174 /* Parse a line of space-separated wildcards and add them to the list. */
188 /* ignore whitespace before the token */
189 if (isspace ((unsigned char) *ign))
192 /* If we have used up all the space, add some more. Do this before
193 processing `!', since an "empty" list still contains the `CVS'
195 if (ign_count >= ign_size)
197 ign_size += IGN_GROW;
198 ign_list = (char **) xrealloc ((char *) ign_list,
199 (ign_size + 1) * sizeof (char *));
203 * if we find a single character !, we must re-set the ignore list
204 * (saving it if necessary). We also catch * as a special case in a
205 * global ignore file as an optimization
207 if ((!*(ign+1) || isspace ((unsigned char) *(ign+1)))
208 && (*ign == '!' || *ign == '*'))
212 /* permanently reset the ignore list */
215 for (i = 0; i < ign_count; i++)
218 /* Always ignore the "CVS" directory. */
219 ign_list[0] = xstrdup("CVS");
222 /* if we are doing a '!', continue; otherwise add the '*' */
225 ign_inhibit_server = 1;
229 else if (*ign == '!')
231 /* temporarily reset the ignore list */
236 for (i = ign_hold; i < ign_count; i++)
240 s_ign_list = (char **) xmalloc (ign_count * sizeof (char *));
241 for (i = 0; i < ign_count; i++)
242 s_ign_list[i] = ign_list[i];
243 s_ign_count = ign_count;
245 /* Always ignore the "CVS" directory. */
246 ign_list[0] = xstrdup ("CVS");
252 /* find the end of this token */
253 for (mark = ign; *mark && !isspace ((unsigned char) *mark); mark++)
259 ign_list[ign_count++] = xstrdup (ign);
260 ign_list[ign_count] = NULL;
272 /* Return true if the given filename should be ignored by update or import,
279 char **cpp = ign_list;
285 if (CVS_FNMATCH (*cpp++, name, 0) == 0)
293 /* FIXME: This list of dirs to ignore stuff seems not to be used.
294 Really? send_dirent_proc and update_dirent_proc both call
295 ignore_directory and do_module calls ign_dir_add. No doubt could
296 use some documentation/testsuite work. */
298 static char **dir_ign_list = NULL;
299 static int dir_ign_max = 0;
300 static int dir_ign_current = 0;
302 /* Add a directory to list of dirs to ignore. */
307 /* Make sure we've got the space for the entry. */
308 if (dir_ign_current <= dir_ign_max)
310 dir_ign_max += IGN_GROW;
312 (char **) xrealloc (dir_ign_list,
313 (dir_ign_max + 1) * sizeof (char *));
316 dir_ign_list[dir_ign_current++] = xstrdup (name);
320 /* Return nonzero if NAME is part of the list of directories to ignore. */
323 ignore_directory (name)
334 if (strncmp (name, dir_ign_list[i], strlen (dir_ign_list[i])) == 0)
344 * Process the current directory, looking for files not in ILIST and
345 * not on the global ignore list for this directory. If we find one,
346 * call PROC passing it the name of the file and the update dir.
347 * ENTRIES is the entries list, which is used to identify known
348 * directories. ENTRIES may be NULL, in which case we assume that any
349 * directory with a CVS administration directory is known.
352 ignore_files (ilist, entries, update_dir, proc)
355 const char *update_dir;
367 /* Set SUBDIRS if we have subdirectory information in ENTRIES. */
372 struct stickydirtag *sdtp = entries->list->data;
374 subdirs = sdtp == NULL || sdtp->subdirs;
377 /* we get called with update_dir set to "." sometimes... strip it */
378 if (strcmp (update_dir, ".") == 0)
383 dirp = CVS_OPENDIR (".");
386 error (0, errno, "cannot open current directory");
390 ign_add_file (CVSDOTIGNORE, 1);
391 wrap_add_file (CVSDOTWRAPPER, 1);
393 /* Make a list for the files. */
396 while (errno = 0, (dp = CVS_READDIR (dirp)) != NULL)
399 if (strcmp (file, ".") == 0 || strcmp (file, "..") == 0)
401 if (findnode_fn (ilist, file) != NULL)
407 node = findnode_fn (entries, file);
409 && ((Entnode *) node->data)->type == ENT_SUBDIR)
414 /* For consistency with past behaviour, we only ignore
415 this directory if there is a CVS subdirectory.
416 This will normally be the case, but the user may
417 have messed up the working directory somehow. */
418 p = xmalloc (strlen (file) + sizeof CVSADM + 10);
419 sprintf (p, "%s/%s", file, CVSADM);
427 /* We could be ignoring FIFOs and other files which are neither
428 regular files nor directories here. */
434 dp->d_type != DT_UNKNOWN ||
436 CVS_LSTAT (file, &sb) != -1)
442 || (dp->d_type == DT_UNKNOWN && S_ISDIR (sb.st_mode))
452 temp = xmalloc (strlen (file) + sizeof (CVSADM) + 10);
453 (void) sprintf (temp, "%s/%s", file, CVSADM);
466 || (dp->d_type == DT_UNKNOWN && S_ISLNK(sb.st_mode))
479 p->key = xstrdup (file);
480 (void) addnode (files, p);
483 error (0, errno, "error reading current directory");
484 (void) CVS_CLOSEDIR (dirp);
486 sortlist (files, fsortcmp);
487 for (p = files->list->next; p != files->list; p = p->next)
488 (*proc) (p->key, xdir);