@matheusbsbs / matheus-bagliano / commits / 3e09cdfd11
Fork 0

Use core.filemode.

With "[core] filemode = false", you can tell git to ignore differences in the working tree file only in executable bit. * "git-update-index --refresh" does not say "needs update" if index entry and working tree file differs only in executable bit. * "git-update-index" on an existing path takes executable bit from the existing index entry, if the path and index entry are both regular files. * "git-diff-files" and "git-diff-index" without --cached flag pretend the path on the filesystem has the same executable bit as the existing index entry, if the path and index entry are both regular files. If you are on a filesystem with unreliable mode bits, you may need to force the executable bit after registering the path in the index. * "git-update-index --chmod=+x foo" flips the executable bit of the index file entry for path "foo" on. Use "--chmod=-x" to flip it off. Note that --chmod only works in index file and does not look at nor update the working tree. So if you are on a filesystem and do not have working executable bit, you would do: 1. set the appropriate .git/config option; 2. "git-update-index --add new-file.c" 3. "git-ls-files --stage new-file.c" to see if it has the desired mode bits. If not, e.g. to drop executable bit picked up from the filesystem, say "git-update-index --chmod=-x new-file.c". Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Oct 11, 2005 at 18:45 UTC 3e09cdfd114651fc61656dbd45d5ec3d9352cb2b
4 files changed +70 -9
diff-files.c
+8 -2
@@ -81,7 +81,7 @@ int main(int argc, const char **argv)
81
82 for (i = 0; i < entries; i++) {
83 struct stat st;
84 - unsigned int oldmode;
84 + unsigned int oldmode, newmode;
85 struct cache_entry *ce = active_cache[i];
86 int changed;
87
@@ -111,7 +111,13 @@ int main(int argc, const char **argv)
111 if (!changed && !diff_options.find_copies_harder)
112 continue;
113 oldmode = ntohl(ce->ce_mode);
114 - show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
114 +
115 + newmode = DIFF_FILE_CANON_MODE(st.st_mode);
116 + if (!trust_executable_bit &&
117 + S_ISREG(newmode) && S_ISREG(oldmode) &&
118 + ((newmode ^ oldmode) == 0111))
119 + newmode = oldmode;
120 + show_modified(oldmode, newmode,
121 ce->sha1, (changed ? null_sha1 : ce->sha1),
122 ce->name);
123 }
diff-index.c
+9 -2
@@ -15,7 +15,7 @@ static void show_file(const char *prefix,
15 }
16
17 static int get_stat_data(struct cache_entry *ce,
18 - unsigned char **sha1p, unsigned int *modep)
18 + unsigned char ** sha1p, unsigned int *modep)
19 {
20 unsigned char *sha1 = ce->sha1;
21 unsigned int mode = ce->ce_mode;
@@ -35,6 +35,10 @@ static int get_stat_data(struct cache_entry *ce,
35 changed = ce_match_stat(ce, &st);
36 if (changed) {
37 mode = create_ce_mode(st.st_mode);
38 + if (!trust_executable_bit &&
39 + S_ISREG(mode) && S_ISREG(ce->ce_mode) &&
40 + ((mode ^ ce->ce_mode) == 0111))
41 + mode = ce->ce_mode;
42 sha1 = no_sha1;
43 }
44 }
@@ -49,7 +53,9 @@ static void show_new_file(struct cache_entry *new)
53 unsigned char *sha1;
54 unsigned int mode;
55
52 - /* New file in the index: it might actually be different in the working copy */
56 + /* New file in the index: it might actually be different in
57 + * the working copy.
58 + */
59 if (get_stat_data(new, &sha1, &mode) < 0)
60 return;
61
@@ -174,6 +180,7 @@ int main(int argc, const char **argv)
180 int allow_options = 1;
181 int i;
182
183 + git_config(git_default_config);
184 diff_setup(&diff_options);
185 for (i = 1; i < argc; i++) {
186 const char *arg = argv[i];
read-cache.c
+8 -4
@@ -36,8 +36,11 @@ int ce_match_stat(struct cache_entry *ce, struct stat *st)
36 switch (ntohl(ce->ce_mode) & S_IFMT) {
37 case S_IFREG:
38 changed |= !S_ISREG(st->st_mode) ? TYPE_CHANGED : 0;
39 - /* We consider only the owner x bit to be relevant for "mode changes" */
40 - if (0100 & (ntohl(ce->ce_mode) ^ st->st_mode))
39 + /* We consider only the owner x bit to be relevant for
40 + * "mode changes"
41 + */
42 + if (trust_executable_bit &&
43 + (0100 & (ntohl(ce->ce_mode) ^ st->st_mode)))
44 changed |= MODE_CHANGED;
45 break;
46 case S_IFLNK:
@@ -393,7 +396,7 @@ int add_cache_entry(struct cache_entry *ce, int option)
396 int skip_df_check = option & ADD_CACHE_SKIP_DFCHECK;
397 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
398
396 - /* existing match? Just replace it */
399 + /* existing match? Just replace it. */
400 if (pos >= 0) {
401 active_cache_changed = 1;
402 active_cache[pos] = ce;
@@ -416,7 +419,8 @@ int add_cache_entry(struct cache_entry *ce, int option)
419 if (!ok_to_add)
420 return -1;
421
419 - if (!skip_df_check && check_file_directory_conflict(ce, pos, ok_to_replace)) {
422 + if (!skip_df_check &&
423 + check_file_directory_conflict(ce, pos, ok_to_replace)) {
424 if (!ok_to_replace)
425 return -1;
426 pos = cache_name_pos(ce->name, ntohs(ce->ce_flags));
update-index.c
+45 -1
@@ -67,13 +67,23 @@ static int add_file_to_cache(const char *path)
67 return error("lstat(\"%s\"): %s", path,
68 strerror(errno));
69 }
70 +
71 namelen = strlen(path);
72 size = cache_entry_size(namelen);
73 ce = xmalloc(size);
74 memset(ce, 0, size);
75 memcpy(ce->name, path, namelen);
76 fill_stat_cache_info(ce, &st);
77 +
78 ce->ce_mode = create_ce_mode(st.st_mode);
79 + if (!trust_executable_bit) {
80 + /* If there is an existing entry, pick the mode bits
81 + * from it.
82 + */
83 + int pos = cache_name_pos(path, namelen);
84 + if (0 <= pos)
85 + ce->ce_mode = active_cache[pos]->ce_mode;
86 + }
87 ce->ce_flags = htons(namelen);
88
89 if (index_path(ce->sha1, path, &st, !info_only))
@@ -253,8 +263,32 @@ static int add_cacheinfo(const char *arg1, const char *arg2, const char *arg3)
263 return add_cache_entry(ce, option);
264 }
265
256 -static struct cache_file cache_file;
266 +static int chmod_path(int flip, const char *path)
267 +{
268 + int pos;
269 + struct cache_entry *ce;
270 + unsigned int mode;
271
272 + pos = cache_name_pos(path, strlen(path));
273 + if (pos < 0)
274 + return -1;
275 + ce = active_cache[pos];
276 + mode = ntohl(ce->ce_mode);
277 + if (!S_ISREG(mode))
278 + return -1;
279 + switch (flip) {
280 + case '+':
281 + ce->ce_mode |= htonl(0111); break;
282 + case '-':
283 + ce->ce_mode &= htonl(~0111); break;
284 + default:
285 + return -1;
286 + }
287 + active_cache_changed = 1;
288 + return 0;
289 +}
290 +
291 +static struct cache_file cache_file;
292
293 static void update_one(const char *path, const char *prefix, int prefix_length)
294 {
@@ -328,6 +362,8 @@ int main(int argc, const char **argv)
362 const char *prefix = setup_git_directory();
363 int prefix_length = prefix ? strlen(prefix) : 0;
364
365 + git_config(git_default_config);
366 +
367 newfd = hold_index_file_for_update(&cache_file, get_index_file());
368 if (newfd < 0)
369 die("unable to create new cachefile");
@@ -376,6 +412,14 @@ int main(int argc, const char **argv)
412 i += 3;
413 continue;
414 }
415 + if (!strcmp(path, "--chmod=-x") ||
416 + !strcmp(path, "--chmod=+x")) {
417 + if (argc <= i+1)
418 + die("git-update-index: %s <path>", path);
419 + if (chmod_path(path[8], argv[++i]))
420 + die("git-update-index: %s cannot chmod %s", path, argv[i]);
421 + continue;
422 + }
423 if (!strcmp(path, "--info-only")) {
424 info_only = 1;
425 continue;