Skip to content

Commit 734aab7

Browse files
author
Linus Torvalds
committed
Make the cache stat information comparator public.
Like the cache filename finder, it's a generically useful function, rather than something specific to the current "show-diff" thing.
1 parent eb38c22 commit 734aab7

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

cache.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,17 @@ unsigned int active_nr, active_alloc;
7171

7272
#define alloc_nr(x) (((x)+16)*3/2)
7373

74-
/* Initialize the cache information */
74+
/* Initialize and use the cache information */
7575
extern int read_cache(void);
7676
extern int cache_name_pos(const char *name, int namelen);
77+
extern int cache_match_stat(struct cache_entry *ce, struct stat *st);
78+
79+
#define MTIME_CHANGED 0x0001
80+
#define CTIME_CHANGED 0x0002
81+
#define OWNER_CHANGED 0x0004
82+
#define MODE_CHANGED 0x0008
83+
#define INODE_CHANGED 0x0010
84+
#define DATA_CHANGED 0x0020
7785

7886
/* Return a statically allocated filename matching the sha1 signature */
7987
extern char *sha1_file_name(unsigned char *sha1);

read-cache.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,29 @@ static int error(const char * string)
222222
return -1;
223223
}
224224

225+
int cache_match_stat(struct cache_entry *ce, struct stat *st)
226+
{
227+
unsigned int changed = 0;
228+
229+
if (ce->mtime.sec != (unsigned int)st->st_mtim.tv_sec ||
230+
ce->mtime.nsec != (unsigned int)st->st_mtim.tv_nsec)
231+
changed |= MTIME_CHANGED;
232+
if (ce->ctime.sec != (unsigned int)st->st_ctim.tv_sec ||
233+
ce->ctime.nsec != (unsigned int)st->st_ctim.tv_nsec)
234+
changed |= CTIME_CHANGED;
235+
if (ce->st_uid != (unsigned int)st->st_uid ||
236+
ce->st_gid != (unsigned int)st->st_gid)
237+
changed |= OWNER_CHANGED;
238+
if (ce->st_mode != (unsigned int)st->st_mode)
239+
changed |= MODE_CHANGED;
240+
if (ce->st_dev != (unsigned int)st->st_dev ||
241+
ce->st_ino != (unsigned int)st->st_ino)
242+
changed |= INODE_CHANGED;
243+
if (ce->st_size != (unsigned int)st->st_size)
244+
changed |= DATA_CHANGED;
245+
return changed;
246+
}
247+
225248
static int cache_name_compare(const char *name1, int len1, const char *name2, int len2)
226249
{
227250
int len = len1 < len2 ? len1 : len2;

show-diff.c

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,6 @@
55
*/
66
#include "cache.h"
77

8-
#define MTIME_CHANGED 0x0001
9-
#define CTIME_CHANGED 0x0002
10-
#define OWNER_CHANGED 0x0004
11-
#define MODE_CHANGED 0x0008
12-
#define INODE_CHANGED 0x0010
13-
#define DATA_CHANGED 0x0020
14-
15-
static int match_stat(struct cache_entry *ce, struct stat *st)
16-
{
17-
unsigned int changed = 0;
18-
19-
if (ce->mtime.sec != (unsigned int)st->st_mtim.tv_sec ||
20-
ce->mtime.nsec != (unsigned int)st->st_mtim.tv_nsec)
21-
changed |= MTIME_CHANGED;
22-
if (ce->ctime.sec != (unsigned int)st->st_ctim.tv_sec ||
23-
ce->ctime.nsec != (unsigned int)st->st_ctim.tv_nsec)
24-
changed |= CTIME_CHANGED;
25-
if (ce->st_uid != (unsigned int)st->st_uid ||
26-
ce->st_gid != (unsigned int)st->st_gid)
27-
changed |= OWNER_CHANGED;
28-
if (ce->st_mode != (unsigned int)st->st_mode)
29-
changed |= MODE_CHANGED;
30-
if (ce->st_dev != (unsigned int)st->st_dev ||
31-
ce->st_ino != (unsigned int)st->st_ino)
32-
changed |= INODE_CHANGED;
33-
if (ce->st_size != (unsigned int)st->st_size)
34-
changed |= DATA_CHANGED;
35-
return changed;
36-
}
37-
388
static void show_differences(struct cache_entry *ce, struct stat *cur,
399
void *old_contents, unsigned long long old_size)
4010
{
@@ -68,7 +38,7 @@ int main(int argc, char **argv)
6838
printf("%s: %s\n", ce->name, strerror(errno));
6939
continue;
7040
}
71-
changed = match_stat(ce, &st);
41+
changed = cache_match_stat(ce, &st);
7242
if (!changed) {
7343
printf("%s: ok\n", ce->name);
7444
continue;

0 commit comments

Comments
 (0)