Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * "git add" builtin command |
| 3 | * |
| 4 | * Copyright (C) 2006 Linus Torvalds |
| 5 | */ |
| 6 | #include <fnmatch.h> |
| 7 | |
| 8 | #include "cache.h" |
| 9 | #include "builtin.h" |
| 10 | #include "dir.h" |
Junio C Hamano | 93872e0 | 2006-05-20 01:28:49 -0700 | [diff] [blame] | 11 | #include "cache-tree.h" |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 12 | |
| 13 | static const char builtin_add_usage[] = |
| 14 | "git-add [-n] [-v] <filepattern>..."; |
| 15 | |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 16 | static void prune_directory(struct dir_struct *dir, const char **pathspec, int prefix) |
| 17 | { |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 18 | char *seen; |
| 19 | int i, specs; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 20 | struct dir_entry **src, **dst; |
| 21 | |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 22 | for (specs = 0; pathspec[specs]; specs++) |
| 23 | /* nothing */; |
Peter Eriksen | 28f7581 | 2006-07-25 09:30:18 +0200 | [diff] [blame] | 24 | seen = xcalloc(specs, 1); |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 25 | |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 26 | src = dst = dir->entries; |
| 27 | i = dir->nr; |
| 28 | while (--i >= 0) { |
| 29 | struct dir_entry *entry = *src++; |
Linus Torvalds | 3c6a370 | 2006-05-19 16:07:51 -0700 | [diff] [blame] | 30 | if (!match_pathspec(pathspec, entry->name, entry->len, prefix, seen)) { |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 31 | free(entry); |
| 32 | continue; |
| 33 | } |
| 34 | *dst++ = entry; |
| 35 | } |
| 36 | dir->nr = dst - dir->entries; |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 37 | |
| 38 | for (i = 0; i < specs; i++) { |
| 39 | struct stat st; |
| 40 | const char *match; |
| 41 | if (seen[i]) |
| 42 | continue; |
| 43 | |
| 44 | /* Existing file? We must have ignored it */ |
| 45 | match = pathspec[i]; |
Junio C Hamano | e8f990b | 2006-05-18 01:29:36 -0700 | [diff] [blame] | 46 | if (!match[0] || !lstat(match, &st)) |
Linus Torvalds | f259339 | 2006-05-17 13:23:19 -0700 | [diff] [blame] | 47 | continue; |
| 48 | die("pathspec '%s' did not match any files", match); |
| 49 | } |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static void fill_directory(struct dir_struct *dir, const char **pathspec) |
| 53 | { |
| 54 | const char *path, *base; |
| 55 | int baselen; |
| 56 | |
| 57 | /* Set up the default git porcelain excludes */ |
| 58 | memset(dir, 0, sizeof(*dir)); |
| 59 | dir->exclude_per_dir = ".gitignore"; |
| 60 | path = git_path("info/exclude"); |
| 61 | if (!access(path, R_OK)) |
| 62 | add_excludes_from_file(dir, path); |
| 63 | |
| 64 | /* |
| 65 | * Calculate common prefix for the pathspec, and |
| 66 | * use that to optimize the directory walk |
| 67 | */ |
| 68 | baselen = common_prefix(pathspec); |
| 69 | path = "."; |
| 70 | base = ""; |
| 71 | if (baselen) { |
| 72 | char *common = xmalloc(baselen + 1); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 73 | memcpy(common, *pathspec, baselen); |
| 74 | common[baselen] = 0; |
| 75 | path = base = common; |
| 76 | } |
| 77 | |
| 78 | /* Read the directory and prune it */ |
| 79 | read_directory(dir, path, base, baselen); |
| 80 | if (pathspec) |
| 81 | prune_directory(dir, pathspec, baselen); |
| 82 | } |
| 83 | |
Junio C Hamano | 021b6e4 | 2006-06-06 12:51:49 -0700 | [diff] [blame] | 84 | static struct lock_file lock_file; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 85 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 86 | int cmd_add(int argc, const char **argv, const char *prefix) |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 87 | { |
| 88 | int i, newfd; |
| 89 | int verbose = 0, show_only = 0; |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 90 | const char **pathspec; |
| 91 | struct dir_struct dir; |
| 92 | |
| 93 | git_config(git_default_config); |
| 94 | |
Junio C Hamano | 40aaae8 | 2006-08-12 01:03:47 -0700 | [diff] [blame] | 95 | newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 96 | |
| 97 | if (read_cache() < 0) |
| 98 | die("index file corrupt"); |
| 99 | |
| 100 | for (i = 1; i < argc; i++) { |
| 101 | const char *arg = argv[i]; |
| 102 | |
| 103 | if (arg[0] != '-') |
| 104 | break; |
| 105 | if (!strcmp(arg, "--")) { |
| 106 | i++; |
| 107 | break; |
| 108 | } |
| 109 | if (!strcmp(arg, "-n")) { |
| 110 | show_only = 1; |
| 111 | continue; |
| 112 | } |
| 113 | if (!strcmp(arg, "-v")) { |
| 114 | verbose = 1; |
| 115 | continue; |
| 116 | } |
Ramsay Allan Jones | 8cdf336 | 2006-08-03 16:48:41 +0100 | [diff] [blame] | 117 | usage(builtin_add_usage); |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 118 | } |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 119 | pathspec = get_pathspec(prefix, argv + i); |
| 120 | |
| 121 | fill_directory(&dir, pathspec); |
| 122 | |
| 123 | if (show_only) { |
| 124 | const char *sep = "", *eof = ""; |
| 125 | for (i = 0; i < dir.nr; i++) { |
| 126 | printf("%s%s", sep, dir.entries[i]->name); |
| 127 | sep = " "; |
| 128 | eof = "\n"; |
| 129 | } |
| 130 | fputs(eof, stdout); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | for (i = 0; i < dir.nr; i++) |
| 135 | add_file_to_index(dir.entries[i]->name, verbose); |
| 136 | |
| 137 | if (active_cache_changed) { |
| 138 | if (write_cache(newfd, active_cache, active_nr) || |
Johannes Schindelin | 6244b24 | 2006-07-08 10:56:28 +0200 | [diff] [blame] | 139 | close(newfd) || commit_lock_file(&lock_file)) |
Linus Torvalds | 0d78153 | 2006-05-17 09:33:32 -0700 | [diff] [blame] | 140 | die("Unable to write new index file"); |
| 141 | } |
| 142 | |
| 143 | return 0; |
| 144 | } |