~ubuntu-branches/ubuntu/intrepid/git-core/intrepid-updates

« back to all changes in this revision

Viewing changes to dir.c

  • Committer: Package Import Robot
  • Author(s): Gerrit Pape
  • Date: 2007-10-04 08:27:01 UTC
  • mfrom: (1.1.23)
  • Revision ID: package-import@ubuntu.com-20071004082701-rsd058ontoqz4i30
Tags: 1:1.5.3.4-1
new upstream point release (closes: #445188).

Show diffs side-by-side

added added

removed removed

Lines of Context:
271
271
        return 0;
272
272
}
273
273
 
274
 
struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
275
 
{
 
274
static struct dir_entry *dir_entry_new(const char *pathname, int len) {
276
275
        struct dir_entry *ent;
277
276
 
278
 
        if (cache_name_pos(pathname, len) >= 0)
279
 
                return NULL;
280
 
 
281
 
        if (dir->nr == dir->alloc) {
282
 
                int alloc = alloc_nr(dir->alloc);
283
 
                dir->alloc = alloc;
284
 
                dir->entries = xrealloc(dir->entries, alloc*sizeof(ent));
285
 
        }
286
277
        ent = xmalloc(sizeof(*ent) + len + 1);
287
 
        ent->ignored = ent->ignored_dir = 0;
288
278
        ent->len = len;
289
279
        memcpy(ent->name, pathname, len);
290
280
        ent->name[len] = 0;
291
 
        dir->entries[dir->nr++] = ent;
292
281
        return ent;
293
282
}
294
283
 
 
284
struct dir_entry *dir_add_name(struct dir_struct *dir, const char *pathname, int len)
 
285
{
 
286
        if (cache_name_pos(pathname, len) >= 0)
 
287
                return NULL;
 
288
 
 
289
        ALLOC_GROW(dir->entries, dir->nr+1, dir->alloc);
 
290
        return dir->entries[dir->nr++] = dir_entry_new(pathname, len);
 
291
}
 
292
 
 
293
struct dir_entry *dir_add_ignored(struct dir_struct *dir, const char *pathname, int len)
 
294
{
 
295
        if (cache_name_pos(pathname, len) >= 0)
 
296
                return NULL;
 
297
 
 
298
        ALLOC_GROW(dir->ignored, dir->ignored_nr+1, dir->ignored_alloc);
 
299
        return dir->ignored[dir->ignored_nr++] = dir_entry_new(pathname, len);
 
300
}
 
301
 
295
302
enum exist_status {
296
303
        index_nonexistent = 0,
297
304
        index_directory,
321
328
                        break;
322
329
                if (endchar == '/')
323
330
                        return index_directory;
324
 
                if (!endchar && S_ISDIRLNK(ntohl(ce->ce_mode)))
 
331
                if (!endchar && S_ISGITLINK(ntohl(ce->ce_mode)))
325
332
                        return index_gitdir;
326
333
        }
327
334
        return index_nonexistent;
356
363
 *      also true and the directory is empty, in which case
357
364
 *      we just ignore it entirely.
358
365
 *  (b) if it looks like a git directory, and we don't have
359
 
 *      'no_dirlinks' set we treat it as a gitlink, and show it
 
366
 *      'no_gitlinks' set we treat it as a gitlink, and show it
360
367
 *      as a directory.
361
368
 *  (c) otherwise, we recurse into it.
362
369
 */
383
390
        case index_nonexistent:
384
391
                if (dir->show_other_directories)
385
392
                        break;
386
 
                if (!dir->no_dirlinks) {
 
393
                if (!dir->no_gitlinks) {
387
394
                        unsigned char sha1[20];
388
395
                        if (resolve_gitlink_ref(dirname, "HEAD", sha1) == 0)
389
396
                                return show_directory;
424
431
        return 0;
425
432
}
426
433
 
 
434
static int in_pathspec(const char *path, int len, const struct path_simplify *simplify)
 
435
{
 
436
        if (simplify) {
 
437
                for (; simplify->path; simplify++) {
 
438
                        if (len == simplify->len
 
439
                            && !memcmp(path, simplify->path, len))
 
440
                                return 1;
 
441
                }
 
442
        }
 
443
        return 0;
 
444
}
 
445
 
427
446
/*
428
447
 * Read a directory tree. We currently ignore anything but
429
448
 * directories, regular files and symlinks. That's because git
464
483
                                continue;
465
484
 
466
485
                        exclude = excluded(dir, fullname);
 
486
                        if (exclude && dir->collect_ignored
 
487
                            && in_pathspec(fullname, baselen + len, simplify))
 
488
                                dir_add_ignored(dir, fullname, baselen + len);
467
489
                        if (exclude != dir->show_ignored) {
468
490
                                if (!dir->show_ignored || DTYPE(de) != DT_DIR) {
469
491
                                        continue;
610
632
        read_directory_recursive(dir, path, base, baselen, 0, simplify);
611
633
        free_simplify(simplify);
612
634
        qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
 
635
        qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
613
636
        return dir->nr;
614
637
}
615
638
 
619
642
  struct stat sb;
620
643
  return stat(f, &sb) == 0;
621
644
}
 
645
 
 
646
/*
 
647
 * get_relative_cwd() gets the prefix of the current working directory
 
648
 * relative to 'dir'.  If we are not inside 'dir', it returns NULL.
 
649
 *
 
650
 * As a convenience, it also returns NULL if 'dir' is already NULL.  The
 
651
 * reason for this behaviour is that it is natural for functions returning
 
652
 * directory names to return NULL to say "this directory does not exist"
 
653
 * or "this directory is invalid".  These cases are usually handled the
 
654
 * same as if the cwd is not inside 'dir' at all, so get_relative_cwd()
 
655
 * returns NULL for both of them.
 
656
 *
 
657
 * Most notably, get_relative_cwd(buffer, size, get_git_work_tree())
 
658
 * unifies the handling of "outside work tree" with "no work tree at all".
 
659
 */
 
660
char *get_relative_cwd(char *buffer, int size, const char *dir)
 
661
{
 
662
        char *cwd = buffer;
 
663
 
 
664
        if (!dir)
 
665
                return NULL;
 
666
        if (!getcwd(buffer, size))
 
667
                die("can't find the current directory: %s", strerror(errno));
 
668
 
 
669
        if (!is_absolute_path(dir))
 
670
                dir = make_absolute_path(dir);
 
671
 
 
672
        while (*dir && *dir == *cwd) {
 
673
                dir++;
 
674
                cwd++;
 
675
        }
 
676
        if (*dir)
 
677
                return NULL;
 
678
        if (*cwd == '/')
 
679
                return cwd + 1;
 
680
        return cwd;
 
681
}
 
682
 
 
683
int is_inside_dir(const char *dir)
 
684
{
 
685
        char buffer[PATH_MAX];
 
686
        return get_relative_cwd(buffer, sizeof(buffer), dir) != NULL;
 
687
}