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);
68 /* The client handles another way, by (after it does its own ignore file
69 processing, and only if !ign_inhibit_server), letting the server
70 know about the files and letting it decide whether to ignore
71 them based on CVSROOOTADM_IGNORE. */
72 if (!current_parsed_root->isremote)
74 char *file = xmalloc (strlen (current_parsed_root->directory) + sizeof (CVSROOTADM)
75 + sizeof (CVSROOTADM_IGNORE) + 10);
76 /* Then add entries found in repository, if it exists */
77 (void) sprintf (file, "%s/%s/%s", current_parsed_root->directory,
78 CVSROOTADM, CVSROOTADM_IGNORE);
79 ign_add_file (file, 0);
83 /* Then add entries found in home dir, (if user has one) and file exists */
84 home_dir = get_homedir ();
85 /* If we can't find a home directory, ignore ~/.cvsignore. This may
86 make tracking down problems a bit of a pain, but on the other
87 hand it might be obnoxious to complain when CVS will function
88 just fine without .cvsignore (and many users won't even know what
92 char *file = strcat_filename_onto_homedir (home_dir, CVSDOTIGNORE);
93 ign_add_file (file, 0);
97 /* Then add entries found in CVSIGNORE environment variable. */
98 ign_add (getenv (IGNORE_ENV), 0);
100 /* Later, add ignore entries found in -I arguments */
106 * Open a file and read lines, feeding each line to a line parser. Arrange
107 * for keeping a temporary list of wildcards at the end, if the "hold"
111 ign_add_file (file, hold)
117 size_t line_allocated = 0;
119 /* restore the saved list (if any) */
120 if (s_ign_list != NULL)
124 for (i = 0; i < s_ign_count; i++)
125 ign_list[i] = s_ign_list[i];
126 ign_count = s_ign_count;
127 ign_list[ign_count] = NULL;
134 /* is this a temporary ignore file? */
137 /* re-set if we had already done a temporary file */
142 for (i = ign_hold; i < ign_count; i++)
144 ign_count = ign_hold;
145 ign_list[ign_count] = NULL;
149 ign_hold = ign_count;
154 fp = CVS_FOPEN (file, "r");
157 if (! existence_error (errno))
158 error (0, errno, "cannot open %s", file);
161 while (getline (&line, &line_allocated, fp) >= 0)
162 ign_add (line, hold);
164 error (0, errno, "cannot read %s", file);
166 error (0, errno, "cannot close %s", file);
172 /* Parse a line of space-separated wildcards and add them to the list. */
186 /* ignore whitespace before the token */
187 if (isspace ((unsigned char) *ign))
190 /* If we have used up all the space, add some more. Do this before
191 processing `!', since an "empty" list still contains the `CVS'
193 if (ign_count >= ign_size)
195 ign_size += IGN_GROW;
196 ign_list = (char **) xrealloc ((char *) ign_list,
197 (ign_size + 1) * sizeof (char *));
201 * if we find a single character !, we must re-set the ignore list
202 * (saving it if necessary). We also catch * as a special case in a
203 * global ignore file as an optimization
205 if ((!*(ign+1) || isspace ((unsigned char) *(ign+1)))
206 && (*ign == '!' || *ign == '*'))
210 /* permanently reset the ignore list */
213 for (i = 0; i < ign_count; i++)
216 /* Always ignore the "CVS" directory. */
217 ign_list[0] = xstrdup("CVS");
220 /* if we are doing a '!', continue; otherwise add the '*' */
223 ign_inhibit_server = 1;
227 else if (*ign == '!')
229 /* temporarily reset the ignore list */
234 for (i = ign_hold; i < ign_count; i++)
240 /* Don't save the ignore list twice - if there are two
241 * bangs in a local .cvsignore file then we don't want to
242 * save the new list the first bang created.
244 * We still need to free the "new" ignore list.
246 for (i = 0; i < ign_count; i++)
251 /* Save the ignore list for later. */
252 s_ign_list = xmalloc (ign_count * sizeof (char *));
253 for (i = 0; i < ign_count; i++)
254 s_ign_list[i] = ign_list[i];
255 s_ign_count = ign_count;
258 /* Always ignore the "CVS" directory. */
259 ign_list[0] = xstrdup ("CVS");
265 /* find the end of this token */
266 for (mark = ign; *mark && !isspace ((unsigned char) *mark); mark++)
272 ign_list[ign_count++] = xstrdup (ign);
273 ign_list[ign_count] = NULL;
285 /* Return true if the given filename should be ignored by update or import,
292 char **cpp = ign_list;
298 if (CVS_FNMATCH (*cpp++, name, 0) == 0)
306 /* FIXME: This list of dirs to ignore stuff seems not to be used.
307 Really? send_dirent_proc and update_dirent_proc both call
308 ignore_directory and do_module calls ign_dir_add. No doubt could
309 use some documentation/testsuite work. */
311 static char **dir_ign_list = NULL;
312 static int dir_ign_max = 0;
313 static int dir_ign_current = 0;
315 /* Add a directory to list of dirs to ignore. */
320 /* Make sure we've got the space for the entry. */
321 if (dir_ign_current <= dir_ign_max)
323 dir_ign_max += IGN_GROW;
325 (char **) xrealloc (dir_ign_list,
326 (dir_ign_max + 1) * sizeof (char *));
329 dir_ign_list[dir_ign_current++] = xstrdup (name);
333 /* Return nonzero if NAME is part of the list of directories to ignore. */
336 ignore_directory (name)
347 if (strncmp (name, dir_ign_list[i], strlen (dir_ign_list[i])+1) == 0)
357 * Process the current directory, looking for files not in ILIST and
358 * not on the global ignore list for this directory. If we find one,
359 * call PROC passing it the name of the file and the update dir.
360 * ENTRIES is the entries list, which is used to identify known
361 * directories. ENTRIES may be NULL, in which case we assume that any
362 * directory with a CVS administration directory is known.
365 ignore_files (ilist, entries, update_dir, proc)
368 const char *update_dir;
380 /* Set SUBDIRS if we have subdirectory information in ENTRIES. */
385 struct stickydirtag *sdtp = entries->list->data;
387 subdirs = sdtp == NULL || sdtp->subdirs;
390 /* we get called with update_dir set to "." sometimes... strip it */
391 if (strcmp (update_dir, ".") == 0)
396 dirp = CVS_OPENDIR (".");
399 error (0, errno, "cannot open current directory");
403 ign_add_file (CVSDOTIGNORE, 1);
404 wrap_add_file (CVSDOTWRAPPER, 1);
406 /* Make a list for the files. */
409 while (errno = 0, (dp = CVS_READDIR (dirp)) != NULL)
412 if (strcmp (file, ".") == 0 || strcmp (file, "..") == 0)
414 if (findnode_fn (ilist, file) != NULL)
420 node = findnode_fn (entries, file);
422 && ((Entnode *) node->data)->type == ENT_SUBDIR)
427 /* For consistency with past behaviour, we only ignore
428 this directory if there is a CVS subdirectory.
429 This will normally be the case, but the user may
430 have messed up the working directory somehow. */
431 p = xmalloc (strlen (file) + sizeof CVSADM + 10);
432 sprintf (p, "%s/%s", file, CVSADM);
440 /* We could be ignoring FIFOs and other files which are neither
441 regular files nor directories here. */
447 dp->d_type != DT_UNKNOWN ||
449 CVS_LSTAT (file, &sb) != -1)
455 || (dp->d_type == DT_UNKNOWN && S_ISDIR (sb.st_mode))
465 temp = xmalloc (strlen (file) + sizeof (CVSADM) + 10);
466 (void) sprintf (temp, "%s/%s", file, CVSADM);
479 || (dp->d_type == DT_UNKNOWN && S_ISLNK(sb.st_mode))
492 p->key = xstrdup (file);
493 (void) addnode (files, p);
496 error (0, errno, "error reading current directory");
497 (void) CVS_CLOSEDIR (dirp);
499 sortlist (files, fsortcmp);
500 for (p = files->list->next; p != files->list; p = p->next)
501 (*proc) (p->key, xdir);