5 static int is_shallow
= -1;
6 static struct stat shallow_stat
;
7 static char *alternate_shallow_file
;
9 void set_alternate_shallow_file(const char *path
)
12 die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file");
13 free(alternate_shallow_file
);
14 alternate_shallow_file
= path
? xstrdup(path
) : NULL
;
17 int register_shallow(const unsigned char *sha1
)
19 struct commit_graft
*graft
=
20 xmalloc(sizeof(struct commit_graft
));
21 struct commit
*commit
= lookup_commit(sha1
);
23 hashcpy(graft
->sha1
, sha1
);
24 graft
->nr_parent
= -1;
25 if (commit
&& commit
->object
.parsed
)
26 commit
->parents
= NULL
;
27 return register_commit_graft(graft
, 0);
30 int is_repository_shallow(void)
34 const char *path
= alternate_shallow_file
;
40 path
= git_path("shallow");
42 * fetch-pack sets '--shallow-file ""' as an indicator that no
43 * shallow file should be used. We could just open it and it
44 * will likely fail. But let's do an explicit check instead.
47 stat(path
, &shallow_stat
) ||
48 (fp
= fopen(path
, "r")) == NULL
) {
54 while (fgets(buf
, sizeof(buf
), fp
)) {
55 unsigned char sha1
[20];
56 if (get_sha1_hex(buf
, sha1
))
57 die("bad shallow line: %s", buf
);
58 register_shallow(sha1
);
64 struct commit_list
*get_shallow_commits(struct object_array
*heads
, int depth
,
65 int shallow_flag
, int not_shallow_flag
)
67 int i
= 0, cur_depth
= 0;
68 struct commit_list
*result
= NULL
;
69 struct object_array stack
= OBJECT_ARRAY_INIT
;
70 struct commit
*commit
= NULL
;
72 while (commit
|| i
< heads
->nr
|| stack
.nr
) {
73 struct commit_list
*p
;
76 commit
= (struct commit
*)
77 deref_tag(heads
->objects
[i
++].item
, NULL
, 0);
78 if (!commit
|| commit
->object
.type
!= OBJ_COMMIT
) {
83 commit
->util
= xmalloc(sizeof(int));
84 *(int *)commit
->util
= 0;
87 commit
= (struct commit
*)
88 stack
.objects
[--stack
.nr
].item
;
89 cur_depth
= *(int *)commit
->util
;
92 parse_commit_or_die(commit
);
94 if (cur_depth
>= depth
) {
95 commit_list_insert(commit
, &result
);
96 commit
->object
.flags
|= shallow_flag
;
100 commit
->object
.flags
|= not_shallow_flag
;
101 for (p
= commit
->parents
, commit
= NULL
; p
; p
= p
->next
) {
102 if (!p
->item
->util
) {
103 int *pointer
= xmalloc(sizeof(int));
104 p
->item
->util
= pointer
;
105 *pointer
= cur_depth
;
107 int *pointer
= p
->item
->util
;
108 if (cur_depth
>= *pointer
)
110 *pointer
= cur_depth
;
113 add_object_array(&p
->item
->object
,
117 cur_depth
= *(int *)commit
->util
;
125 void check_shallow_file_for_update(void)
131 else if (is_shallow
== -1)
132 die("BUG: shallow must be initialized by now");
134 if (stat(git_path("shallow"), &st
))
135 die("shallow file was removed during fetch");
136 else if (st
.st_mtime
!= shallow_stat
.st_mtime
138 || ST_MTIME_NSEC(st
) != ST_MTIME_NSEC(shallow_stat
)
141 die("shallow file was changed during fetch");