7 #include "sha1-array.h"
10 #include "commit-slab.h"
12 static int is_shallow
= -1;
13 static struct stat shallow_stat
;
14 static char *alternate_shallow_file
;
16 void set_alternate_shallow_file(const char *path
, int override
)
19 die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file");
20 if (alternate_shallow_file
&& !override
)
22 free(alternate_shallow_file
);
23 alternate_shallow_file
= path
? xstrdup(path
) : NULL
;
26 int register_shallow(const unsigned char *sha1
)
28 struct commit_graft
*graft
=
29 xmalloc(sizeof(struct commit_graft
));
30 struct commit
*commit
= lookup_commit(sha1
);
32 hashcpy(graft
->sha1
, sha1
);
33 graft
->nr_parent
= -1;
34 if (commit
&& commit
->object
.parsed
)
35 commit
->parents
= NULL
;
36 return register_commit_graft(graft
, 0);
39 int is_repository_shallow(void)
43 const char *path
= alternate_shallow_file
;
49 path
= git_path("shallow");
51 * fetch-pack sets '--shallow-file ""' as an indicator that no
52 * shallow file should be used. We could just open it and it
53 * will likely fail. But let's do an explicit check instead.
56 stat(path
, &shallow_stat
) ||
57 (fp
= fopen(path
, "r")) == NULL
) {
63 while (fgets(buf
, sizeof(buf
), fp
)) {
64 unsigned char sha1
[20];
65 if (get_sha1_hex(buf
, sha1
))
66 die("bad shallow line: %s", buf
);
67 register_shallow(sha1
);
73 struct commit_list
*get_shallow_commits(struct object_array
*heads
, int depth
,
74 int shallow_flag
, int not_shallow_flag
)
76 int i
= 0, cur_depth
= 0;
77 struct commit_list
*result
= NULL
;
78 struct object_array stack
= OBJECT_ARRAY_INIT
;
79 struct commit
*commit
= NULL
;
80 struct commit_graft
*graft
;
82 while (commit
|| i
< heads
->nr
|| stack
.nr
) {
83 struct commit_list
*p
;
86 commit
= (struct commit
*)
87 deref_tag(heads
->objects
[i
++].item
, NULL
, 0);
88 if (!commit
|| commit
->object
.type
!= OBJ_COMMIT
) {
93 commit
->util
= xmalloc(sizeof(int));
94 *(int *)commit
->util
= 0;
97 commit
= (struct commit
*)
98 stack
.objects
[--stack
.nr
].item
;
99 cur_depth
= *(int *)commit
->util
;
102 parse_commit_or_die(commit
);
104 if ((depth
!= INFINITE_DEPTH
&& cur_depth
>= depth
) ||
105 (is_repository_shallow() && !commit
->parents
&&
106 (graft
= lookup_commit_graft(commit
->object
.sha1
)) != NULL
&&
107 graft
->nr_parent
< 0)) {
108 commit_list_insert(commit
, &result
);
109 commit
->object
.flags
|= shallow_flag
;
113 commit
->object
.flags
|= not_shallow_flag
;
114 for (p
= commit
->parents
, commit
= NULL
; p
; p
= p
->next
) {
115 if (!p
->item
->util
) {
116 int *pointer
= xmalloc(sizeof(int));
117 p
->item
->util
= pointer
;
118 *pointer
= cur_depth
;
120 int *pointer
= p
->item
->util
;
121 if (cur_depth
>= *pointer
)
123 *pointer
= cur_depth
;
126 add_object_array(&p
->item
->object
,
130 cur_depth
= *(int *)commit
->util
;
138 void check_shallow_file_for_update(void)
144 else if (is_shallow
== -1)
145 die("BUG: shallow must be initialized by now");
147 if (stat(git_path("shallow"), &st
))
148 die("shallow file was removed during fetch");
149 else if (st
.st_mtime
!= shallow_stat
.st_mtime
151 || ST_MTIME_NSEC(st
) != ST_MTIME_NSEC(shallow_stat
)
154 die("shallow file was changed during fetch");
160 struct write_shallow_data
{
162 int use_pack_protocol
;
167 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
169 struct write_shallow_data
*data
= cb_data
;
170 const char *hex
= sha1_to_hex(graft
->sha1
);
171 if (graft
->nr_parent
!= -1)
173 if (data
->flags
& SEEN_ONLY
) {
174 struct commit
*c
= lookup_commit(graft
->sha1
);
175 if (!c
|| !(c
->object
.flags
& SEEN
)) {
176 if (data
->flags
& VERBOSE
)
177 printf("Removing %s from .git/shallow\n",
178 sha1_to_hex(c
->object
.sha1
));
183 if (data
->use_pack_protocol
)
184 packet_buf_write(data
->out
, "shallow %s", hex
);
186 strbuf_addstr(data
->out
, hex
);
187 strbuf_addch(data
->out
, '\n');
192 static int write_shallow_commits_1(struct strbuf
*out
, int use_pack_protocol
,
193 const struct sha1_array
*extra
,
196 struct write_shallow_data data
;
199 data
.use_pack_protocol
= use_pack_protocol
;
202 for_each_commit_graft(write_one_shallow
, &data
);
205 for (i
= 0; i
< extra
->nr
; i
++) {
206 strbuf_addstr(out
, sha1_to_hex(extra
->sha1
[i
]));
207 strbuf_addch(out
, '\n');
213 int write_shallow_commits(struct strbuf
*out
, int use_pack_protocol
,
214 const struct sha1_array
*extra
)
216 return write_shallow_commits_1(out
, use_pack_protocol
, extra
, 0);
219 char *setup_temporary_shallow(const struct sha1_array
*extra
)
221 struct strbuf sb
= STRBUF_INIT
;
224 if (write_shallow_commits(&sb
, 0, extra
)) {
225 struct strbuf path
= STRBUF_INIT
;
226 strbuf_addstr(&path
, git_path("shallow_XXXXXX"));
227 fd
= xmkstemp(path
.buf
);
228 if (write_in_full(fd
, sb
.buf
, sb
.len
) != sb
.len
)
229 die_errno("failed to write to %s",
233 return strbuf_detach(&path
, NULL
);
236 * is_repository_shallow() sees empty string as "no shallow
242 void setup_alternate_shallow(struct lock_file
*shallow_lock
,
243 const char **alternate_shallow_file
,
244 const struct sha1_array
*extra
)
246 struct strbuf sb
= STRBUF_INIT
;
249 check_shallow_file_for_update();
250 fd
= hold_lock_file_for_update(shallow_lock
, git_path("shallow"),
252 if (write_shallow_commits(&sb
, 0, extra
)) {
253 if (write_in_full(fd
, sb
.buf
, sb
.len
) != sb
.len
)
254 die_errno("failed to write to %s",
255 shallow_lock
->filename
);
256 *alternate_shallow_file
= shallow_lock
->filename
;
259 * is_repository_shallow() sees empty string as "no
262 *alternate_shallow_file
= "";
266 static int advertise_shallow_grafts_cb(const struct commit_graft
*graft
, void *cb
)
269 if (graft
->nr_parent
== -1)
270 packet_write(fd
, "shallow %s\n", sha1_to_hex(graft
->sha1
));
274 void advertise_shallow_grafts(int fd
)
276 if (!is_repository_shallow())
278 for_each_commit_graft(advertise_shallow_grafts_cb
, &fd
);
282 * mark_reachable_objects() should have been run prior to this and all
283 * reachable commits marked as "SEEN".
285 void prune_shallow(int show_only
)
287 static struct lock_file shallow_lock
;
288 struct strbuf sb
= STRBUF_INIT
;
292 write_shallow_commits_1(&sb
, 0, NULL
, SEEN_ONLY
| VERBOSE
);
296 check_shallow_file_for_update();
297 fd
= hold_lock_file_for_update(&shallow_lock
, git_path("shallow"),
299 if (write_shallow_commits_1(&sb
, 0, NULL
, SEEN_ONLY
)) {
300 if (write_in_full(fd
, sb
.buf
, sb
.len
) != sb
.len
)
301 die_errno("failed to write to %s",
302 shallow_lock
.filename
);
303 commit_lock_file(&shallow_lock
);
305 unlink(git_path("shallow"));
306 rollback_lock_file(&shallow_lock
);
311 #define TRACE_KEY "GIT_TRACE_SHALLOW"
314 * Step 1, split sender shallow commits into "ours" and "theirs"
315 * Step 2, clean "ours" based on .git/shallow
317 void prepare_shallow_info(struct shallow_info
*info
, struct sha1_array
*sa
)
320 trace_printf_key(TRACE_KEY
, "shallow: prepare_shallow_info\n");
321 memset(info
, 0, sizeof(*info
));
325 info
->ours
= xmalloc(sizeof(*info
->ours
) * sa
->nr
);
326 info
->theirs
= xmalloc(sizeof(*info
->theirs
) * sa
->nr
);
327 for (i
= 0; i
< sa
->nr
; i
++) {
328 if (has_sha1_file(sa
->sha1
[i
])) {
329 struct commit_graft
*graft
;
330 graft
= lookup_commit_graft(sa
->sha1
[i
]);
331 if (graft
&& graft
->nr_parent
< 0)
333 info
->ours
[info
->nr_ours
++] = i
;
335 info
->theirs
[info
->nr_theirs
++] = i
;
339 void clear_shallow_info(struct shallow_info
*info
)
345 /* Step 4, remove non-existent ones in "theirs" after getting the pack */
347 void remove_nonexistent_theirs_shallow(struct shallow_info
*info
)
349 unsigned char (*sha1
)[20] = info
->shallow
->sha1
;
351 trace_printf_key(TRACE_KEY
, "shallow: remove_nonexistent_theirs_shallow\n");
352 for (i
= dst
= 0; i
< info
->nr_theirs
; i
++) {
354 info
->theirs
[dst
] = info
->theirs
[i
];
355 if (has_sha1_file(sha1
[info
->theirs
[i
]]))
358 info
->nr_theirs
= dst
;
361 define_commit_slab(ref_bitmap
, uint32_t *);
364 struct ref_bitmap ref_bitmap
;
371 static uint32_t *paint_alloc(struct paint_info
*info
)
373 unsigned nr
= (info
->nr_bits
+ 31) / 32;
374 unsigned size
= nr
* sizeof(uint32_t);
376 if (!info
->slab_count
|| info
->free
+ size
> info
->end
) {
378 info
->slab
= xrealloc(info
->slab
,
379 info
->slab_count
* sizeof(*info
->slab
));
380 info
->free
= xmalloc(COMMIT_SLAB_SIZE
);
381 info
->slab
[info
->slab_count
- 1] = info
->free
;
382 info
->end
= info
->free
+ COMMIT_SLAB_SIZE
;
390 * Given a commit SHA-1, walk down to parents until either SEEN,
391 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
392 * all walked commits.
394 static void paint_down(struct paint_info
*info
, const unsigned char *sha1
,
398 struct commit_list
*head
= NULL
;
399 int bitmap_nr
= (info
->nr_bits
+ 31) / 32;
400 int bitmap_size
= bitmap_nr
* sizeof(uint32_t);
401 uint32_t *tmp
= xmalloc(bitmap_size
); /* to be freed before return */
402 uint32_t *bitmap
= paint_alloc(info
);
403 struct commit
*c
= lookup_commit_reference_gently(sha1
, 1);
406 memset(bitmap
, 0, bitmap_size
);
407 bitmap
[id
/ 32] |= (1 << (id
% 32));
408 commit_list_insert(c
, &head
);
410 struct commit_list
*p
;
411 struct commit
*c
= head
->item
;
412 uint32_t **refs
= ref_bitmap_at(&info
->ref_bitmap
, c
);
418 /* XXX check "UNINTERESTING" from pack bitmaps if available */
419 if (c
->object
.flags
& (SEEN
| UNINTERESTING
))
422 c
->object
.flags
|= SEEN
;
427 memcpy(tmp
, *refs
, bitmap_size
);
428 for (i
= 0; i
< bitmap_nr
; i
++)
430 if (memcmp(tmp
, *refs
, bitmap_size
)) {
431 *refs
= paint_alloc(info
);
432 memcpy(*refs
, tmp
, bitmap_size
);
436 if (c
->object
.flags
& BOTTOM
)
440 die("unable to parse commit %s",
441 sha1_to_hex(c
->object
.sha1
));
443 for (p
= c
->parents
; p
; p
= p
->next
) {
444 uint32_t **p_refs
= ref_bitmap_at(&info
->ref_bitmap
,
446 if (p
->item
->object
.flags
& SEEN
)
448 if (*p_refs
== NULL
|| *p_refs
== *refs
)
450 commit_list_insert(p
->item
, &head
);
454 nr
= get_max_object_index();
455 for (i
= 0; i
< nr
; i
++) {
456 struct object
*o
= get_indexed_object(i
);
457 if (o
&& o
->type
== OBJ_COMMIT
)
464 static int mark_uninteresting(const char *refname
,
465 const unsigned char *sha1
,
466 int flags
, void *cb_data
)
468 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
471 commit
->object
.flags
|= UNINTERESTING
;
472 mark_parents_uninteresting(commit
);
476 static void post_assign_shallow(struct shallow_info
*info
,
477 struct ref_bitmap
*ref_bitmap
,
480 * Step 6(+7), associate shallow commits with new refs
482 * info->ref must be initialized before calling this function.
484 * If used is not NULL, it's an array of info->shallow->nr
485 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
486 * m-th shallow commit from info->shallow.
488 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
489 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
490 * the ref needs some shallow commits from either info->ours or
493 void assign_shallow_commits_to_refs(struct shallow_info
*info
,
494 uint32_t **used
, int *ref_status
)
496 unsigned char (*sha1
)[20] = info
->shallow
->sha1
;
497 struct sha1_array
*ref
= info
->ref
;
499 int *shallow
, nr_shallow
= 0;
500 struct paint_info pi
;
502 trace_printf_key(TRACE_KEY
, "shallow: assign_shallow_commits_to_refs\n");
503 shallow
= xmalloc(sizeof(*shallow
) * (info
->nr_ours
+ info
->nr_theirs
));
504 for (i
= 0; i
< info
->nr_ours
; i
++)
505 shallow
[nr_shallow
++] = info
->ours
[i
];
506 for (i
= 0; i
< info
->nr_theirs
; i
++)
507 shallow
[nr_shallow
++] = info
->theirs
[i
];
510 * Prepare the commit graph to track what refs can reach what
511 * (new) shallow commits.
513 nr
= get_max_object_index();
514 for (i
= 0; i
< nr
; i
++) {
515 struct object
*o
= get_indexed_object(i
);
516 if (!o
|| o
->type
!= OBJ_COMMIT
)
519 o
->flags
&= ~(UNINTERESTING
| BOTTOM
| SEEN
);
522 memset(&pi
, 0, sizeof(pi
));
523 init_ref_bitmap(&pi
.ref_bitmap
);
524 pi
.nr_bits
= ref
->nr
;
527 * "--not --all" to cut short the traversal if new refs
528 * connect to old refs. If not (e.g. force ref updates) it'll
529 * have to go down to the current shallow commits.
531 head_ref(mark_uninteresting
, NULL
);
532 for_each_ref(mark_uninteresting
, NULL
);
534 /* Mark potential bottoms so we won't go out of bound */
535 for (i
= 0; i
< nr_shallow
; i
++) {
536 struct commit
*c
= lookup_commit(sha1
[shallow
[i
]]);
537 c
->object
.flags
|= BOTTOM
;
540 for (i
= 0; i
< ref
->nr
; i
++)
541 paint_down(&pi
, ref
->sha1
[i
], i
);
544 int bitmap_size
= ((pi
.nr_bits
+ 31) / 32) * sizeof(uint32_t);
545 memset(used
, 0, sizeof(*used
) * info
->shallow
->nr
);
546 for (i
= 0; i
< nr_shallow
; i
++) {
547 const struct commit
*c
= lookup_commit(sha1
[shallow
[i
]]);
548 uint32_t **map
= ref_bitmap_at(&pi
.ref_bitmap
, c
);
550 used
[shallow
[i
]] = xmemdupz(*map
, bitmap_size
);
553 * unreachable shallow commits are not removed from
554 * "ours" and "theirs". The user is supposed to run
555 * step 7 on every ref separately and not trust "ours"
556 * and "theirs" any more.
559 post_assign_shallow(info
, &pi
.ref_bitmap
, ref_status
);
561 clear_ref_bitmap(&pi
.ref_bitmap
);
562 for (i
= 0; i
< pi
.slab_count
; i
++)
568 struct commit_array
{
569 struct commit
**commits
;
573 static int add_ref(const char *refname
,
574 const unsigned char *sha1
, int flags
, void *cb_data
)
576 struct commit_array
*ca
= cb_data
;
577 ALLOC_GROW(ca
->commits
, ca
->nr
+ 1, ca
->alloc
);
578 ca
->commits
[ca
->nr
] = lookup_commit_reference_gently(sha1
, 1);
579 if (ca
->commits
[ca
->nr
])
584 static void update_refstatus(int *ref_status
, int nr
, uint32_t *bitmap
)
589 for (i
= 0; i
< nr
; i
++)
590 if (bitmap
[i
/ 32] & (1 << (i
% 32)))
595 * Step 7, reachability test on "ours" at commit level
597 static void post_assign_shallow(struct shallow_info
*info
,
598 struct ref_bitmap
*ref_bitmap
,
601 unsigned char (*sha1
)[20] = info
->shallow
->sha1
;
605 int bitmap_nr
= (info
->ref
->nr
+ 31) / 32;
606 struct commit_array ca
;
608 trace_printf_key(TRACE_KEY
, "shallow: post_assign_shallow\n");
610 memset(ref_status
, 0, sizeof(*ref_status
) * info
->ref
->nr
);
612 /* Remove unreachable shallow commits from "theirs" */
613 for (i
= dst
= 0; i
< info
->nr_theirs
; i
++) {
615 info
->theirs
[dst
] = info
->theirs
[i
];
616 c
= lookup_commit(sha1
[info
->theirs
[i
]]);
617 bitmap
= ref_bitmap_at(ref_bitmap
, c
);
620 for (j
= 0; j
< bitmap_nr
; j
++)
622 update_refstatus(ref_status
, info
->ref
->nr
, *bitmap
);
627 info
->nr_theirs
= dst
;
629 memset(&ca
, 0, sizeof(ca
));
630 head_ref(add_ref
, &ca
);
631 for_each_ref(add_ref
, &ca
);
633 /* Remove unreachable shallow commits from "ours" */
634 for (i
= dst
= 0; i
< info
->nr_ours
; i
++) {
636 info
->ours
[dst
] = info
->ours
[i
];
637 c
= lookup_commit(sha1
[info
->ours
[i
]]);
638 bitmap
= ref_bitmap_at(ref_bitmap
, c
);
641 for (j
= 0; j
< bitmap_nr
; j
++)
643 /* Step 7, reachability test at commit level */
644 !in_merge_bases_many(c
, ca
.nr
, ca
.commits
)) {
645 update_refstatus(ref_status
, info
->ref
->nr
, *bitmap
);
655 /* (Delayed) step 7, reachability test at commit level */
656 int delayed_reachability_test(struct shallow_info
*si
, int c
)
658 if (si
->need_reachability_test
[c
]) {
659 struct commit
*commit
= lookup_commit(si
->shallow
->sha1
[c
]);
662 struct commit_array ca
;
663 memset(&ca
, 0, sizeof(ca
));
664 head_ref(add_ref
, &ca
);
665 for_each_ref(add_ref
, &ca
);
666 si
->commits
= ca
.commits
;
667 si
->nr_commits
= ca
.nr
;
670 si
->reachable
[c
] = in_merge_bases_many(commit
,
673 si
->need_reachability_test
[c
] = 0;
675 return si
->reachable
[c
];