2 #include "repository.h"
5 #include "object-store.h"
11 #include "sha1-array.h"
14 #include "commit-slab.h"
15 #include "list-objects.h"
16 #include "commit-reach.h"
18 void set_alternate_shallow_file(struct repository
*r
, const char *path
, int override
)
20 if (r
->parsed_objects
->is_shallow
!= -1)
21 BUG("is_repository_shallow must not be called before set_alternate_shallow_file");
22 if (r
->parsed_objects
->alternate_shallow_file
&& !override
)
24 free(r
->parsed_objects
->alternate_shallow_file
);
25 r
->parsed_objects
->alternate_shallow_file
= xstrdup_or_null(path
);
28 int register_shallow(struct repository
*r
, const struct object_id
*oid
)
30 struct commit_graft
*graft
=
31 xmalloc(sizeof(struct commit_graft
));
32 struct commit
*commit
= lookup_commit(the_repository
, oid
);
34 oidcpy(&graft
->oid
, oid
);
35 graft
->nr_parent
= -1;
36 if (commit
&& commit
->object
.parsed
)
37 commit
->parents
= NULL
;
38 return register_commit_graft(r
, graft
, 0);
41 int is_repository_shallow(struct repository
*r
)
44 * NEEDSWORK: This function updates
45 * r->parsed_objects->{is_shallow,shallow_stat} as a side effect but
46 * there is no corresponding function to clear them when the shallow
52 const char *path
= r
->parsed_objects
->alternate_shallow_file
;
54 if (r
->parsed_objects
->is_shallow
>= 0)
55 return r
->parsed_objects
->is_shallow
;
58 path
= git_path_shallow(r
);
60 * fetch-pack sets '--shallow-file ""' as an indicator that no
61 * shallow file should be used. We could just open it and it
62 * will likely fail. But let's do an explicit check instead.
64 if (!*path
|| (fp
= fopen(path
, "r")) == NULL
) {
65 stat_validity_clear(r
->parsed_objects
->shallow_stat
);
66 r
->parsed_objects
->is_shallow
= 0;
67 return r
->parsed_objects
->is_shallow
;
69 stat_validity_update(r
->parsed_objects
->shallow_stat
, fileno(fp
));
70 r
->parsed_objects
->is_shallow
= 1;
72 while (fgets(buf
, sizeof(buf
), fp
)) {
74 if (get_oid_hex(buf
, &oid
))
75 die("bad shallow line: %s", buf
);
76 register_shallow(r
, &oid
);
79 return r
->parsed_objects
->is_shallow
;
83 * TODO: use "int" elemtype instead of "int *" when/if commit-slab
84 * supports a "valid" flag.
86 define_commit_slab(commit_depth
, int *);
87 static void free_depth_in_slab(int **ptr
)
91 struct commit_list
*get_shallow_commits(struct object_array
*heads
, int depth
,
92 int shallow_flag
, int not_shallow_flag
)
94 int i
= 0, cur_depth
= 0;
95 struct commit_list
*result
= NULL
;
96 struct object_array stack
= OBJECT_ARRAY_INIT
;
97 struct commit
*commit
= NULL
;
98 struct commit_graft
*graft
;
99 struct commit_depth depths
;
101 init_commit_depth(&depths
);
102 while (commit
|| i
< heads
->nr
|| stack
.nr
) {
103 struct commit_list
*p
;
107 commit
= (struct commit
*)
108 deref_tag(the_repository
,
109 heads
->objects
[i
++].item
,
111 if (!commit
|| commit
->object
.type
!= OBJ_COMMIT
) {
115 depth_slot
= commit_depth_at(&depths
, commit
);
117 *depth_slot
= xmalloc(sizeof(int));
121 commit
= (struct commit
*)
122 object_array_pop(&stack
);
123 cur_depth
= **commit_depth_at(&depths
, commit
);
126 parse_commit_or_die(commit
);
128 if ((depth
!= INFINITE_DEPTH
&& cur_depth
>= depth
) ||
129 (is_repository_shallow(the_repository
) && !commit
->parents
&&
130 (graft
= lookup_commit_graft(the_repository
, &commit
->object
.oid
)) != NULL
&&
131 graft
->nr_parent
< 0)) {
132 commit_list_insert(commit
, &result
);
133 commit
->object
.flags
|= shallow_flag
;
137 commit
->object
.flags
|= not_shallow_flag
;
138 for (p
= commit
->parents
, commit
= NULL
; p
; p
= p
->next
) {
139 int **depth_slot
= commit_depth_at(&depths
, p
->item
);
141 *depth_slot
= xmalloc(sizeof(int));
142 **depth_slot
= cur_depth
;
144 if (cur_depth
>= **depth_slot
)
146 **depth_slot
= cur_depth
;
149 add_object_array(&p
->item
->object
,
153 cur_depth
= **commit_depth_at(&depths
, commit
);
157 deep_clear_commit_depth(&depths
, free_depth_in_slab
);
162 static void show_commit(struct commit
*commit
, void *data
)
164 commit_list_insert(commit
, data
);
168 * Given rev-list arguments, run rev-list. All reachable commits
169 * except border ones are marked with not_shallow_flag. Border commits
170 * are marked with shallow_flag. The list of border/shallow commits
173 struct commit_list
*get_shallow_commits_by_rev_list(int ac
, const char **av
,
175 int not_shallow_flag
)
177 struct commit_list
*result
= NULL
, *p
;
178 struct commit_list
*not_shallow_list
= NULL
;
179 struct rev_info revs
;
180 int both_flags
= shallow_flag
| not_shallow_flag
;
183 * SHALLOW (excluded) and NOT_SHALLOW (included) should not be
184 * set at this point. But better be safe than sorry.
186 clear_object_flags(both_flags
);
188 is_repository_shallow(the_repository
); /* make sure shallows are read */
190 repo_init_revisions(the_repository
, &revs
, NULL
);
191 save_commit_buffer
= 0;
192 setup_revisions(ac
, av
, &revs
, NULL
);
194 if (prepare_revision_walk(&revs
))
195 die("revision walk setup failed");
196 traverse_commit_list(&revs
, show_commit
, NULL
, ¬_shallow_list
);
198 if (!not_shallow_list
)
199 die("no commits selected for shallow requests");
201 /* Mark all reachable commits as NOT_SHALLOW */
202 for (p
= not_shallow_list
; p
; p
= p
->next
)
203 p
->item
->object
.flags
|= not_shallow_flag
;
206 * mark border commits SHALLOW + NOT_SHALLOW.
207 * We cannot clear NOT_SHALLOW right now. Imagine border
208 * commit A is processed first, then commit B, whose parent is
209 * A, later. If NOT_SHALLOW on A is cleared at step 1, B
210 * itself is considered border at step 2, which is incorrect.
212 for (p
= not_shallow_list
; p
; p
= p
->next
) {
213 struct commit
*c
= p
->item
;
214 struct commit_list
*parent
;
217 die("unable to parse commit %s",
218 oid_to_hex(&c
->object
.oid
));
220 for (parent
= c
->parents
; parent
; parent
= parent
->next
)
221 if (!(parent
->item
->object
.flags
& not_shallow_flag
)) {
222 c
->object
.flags
|= shallow_flag
;
223 commit_list_insert(c
, &result
);
227 free_commit_list(not_shallow_list
);
230 * Now we can clean up NOT_SHALLOW on border commits. Having
231 * both flags set can confuse the caller.
233 for (p
= result
; p
; p
= p
->next
) {
234 struct object
*o
= &p
->item
->object
;
235 if ((o
->flags
& both_flags
) == both_flags
)
236 o
->flags
&= ~not_shallow_flag
;
241 static void check_shallow_file_for_update(struct repository
*r
)
243 if (r
->parsed_objects
->is_shallow
== -1)
244 BUG("shallow must be initialized by now");
246 if (!stat_validity_check(r
->parsed_objects
->shallow_stat
,
247 git_path_shallow(r
)))
248 die("shallow file has changed since we read it");
255 struct write_shallow_data
{
257 int use_pack_protocol
;
262 static int write_one_shallow(const struct commit_graft
*graft
, void *cb_data
)
264 struct write_shallow_data
*data
= cb_data
;
265 const char *hex
= oid_to_hex(&graft
->oid
);
266 if (graft
->nr_parent
!= -1)
268 if (data
->flags
& QUICK
) {
269 if (!has_object_file(&graft
->oid
))
271 } else if (data
->flags
& SEEN_ONLY
) {
272 struct commit
*c
= lookup_commit(the_repository
, &graft
->oid
);
273 if (!c
|| !(c
->object
.flags
& SEEN
)) {
274 if (data
->flags
& VERBOSE
)
275 printf("Removing %s from .git/shallow\n",
276 oid_to_hex(&c
->object
.oid
));
281 if (data
->use_pack_protocol
)
282 packet_buf_write(data
->out
, "shallow %s", hex
);
284 strbuf_addstr(data
->out
, hex
);
285 strbuf_addch(data
->out
, '\n');
290 static int write_shallow_commits_1(struct strbuf
*out
, int use_pack_protocol
,
291 const struct oid_array
*extra
,
294 struct write_shallow_data data
;
297 data
.use_pack_protocol
= use_pack_protocol
;
300 for_each_commit_graft(write_one_shallow
, &data
);
303 for (i
= 0; i
< extra
->nr
; i
++) {
304 strbuf_addstr(out
, oid_to_hex(extra
->oid
+ i
));
305 strbuf_addch(out
, '\n');
311 int write_shallow_commits(struct strbuf
*out
, int use_pack_protocol
,
312 const struct oid_array
*extra
)
314 return write_shallow_commits_1(out
, use_pack_protocol
, extra
, 0);
317 const char *setup_temporary_shallow(const struct oid_array
*extra
)
319 struct tempfile
*temp
;
320 struct strbuf sb
= STRBUF_INIT
;
322 if (write_shallow_commits(&sb
, 0, extra
)) {
323 temp
= xmks_tempfile(git_path("shallow_XXXXXX"));
325 if (write_in_full(temp
->fd
, sb
.buf
, sb
.len
) < 0 ||
326 close_tempfile_gently(temp
) < 0)
327 die_errno("failed to write to %s",
328 get_tempfile_path(temp
));
330 return get_tempfile_path(temp
);
333 * is_repository_shallow() sees empty string as "no shallow
339 void setup_alternate_shallow(struct lock_file
*shallow_lock
,
340 const char **alternate_shallow_file
,
341 const struct oid_array
*extra
)
343 struct strbuf sb
= STRBUF_INIT
;
346 fd
= hold_lock_file_for_update(shallow_lock
,
347 git_path_shallow(the_repository
),
349 check_shallow_file_for_update(the_repository
);
350 if (write_shallow_commits(&sb
, 0, extra
)) {
351 if (write_in_full(fd
, sb
.buf
, sb
.len
) < 0)
352 die_errno("failed to write to %s",
353 get_lock_file_path(shallow_lock
));
354 *alternate_shallow_file
= get_lock_file_path(shallow_lock
);
357 * is_repository_shallow() sees empty string as "no
360 *alternate_shallow_file
= "";
364 static int advertise_shallow_grafts_cb(const struct commit_graft
*graft
, void *cb
)
367 if (graft
->nr_parent
== -1)
368 packet_write_fmt(fd
, "shallow %s\n", oid_to_hex(&graft
->oid
));
372 void advertise_shallow_grafts(int fd
)
374 if (!is_repository_shallow(the_repository
))
376 for_each_commit_graft(advertise_shallow_grafts_cb
, &fd
);
380 * mark_reachable_objects() should have been run prior to this and all
381 * reachable commits marked as "SEEN", except when quick_prune is non-zero,
382 * in which case lines are excised from the shallow file if they refer to
383 * commits that do not exist (any longer).
385 void prune_shallow(unsigned options
)
387 struct lock_file shallow_lock
= LOCK_INIT
;
388 struct strbuf sb
= STRBUF_INIT
;
389 unsigned flags
= SEEN_ONLY
;
392 if (options
& PRUNE_QUICK
)
395 if (options
& PRUNE_SHOW_ONLY
) {
397 write_shallow_commits_1(&sb
, 0, NULL
, flags
);
401 fd
= hold_lock_file_for_update(&shallow_lock
,
402 git_path_shallow(the_repository
),
404 check_shallow_file_for_update(the_repository
);
405 if (write_shallow_commits_1(&sb
, 0, NULL
, flags
)) {
406 if (write_in_full(fd
, sb
.buf
, sb
.len
) < 0)
407 die_errno("failed to write to %s",
408 get_lock_file_path(&shallow_lock
));
409 commit_lock_file(&shallow_lock
);
411 unlink(git_path_shallow(the_repository
));
412 rollback_lock_file(&shallow_lock
);
417 struct trace_key trace_shallow
= TRACE_KEY_INIT(SHALLOW
);
420 * Step 1, split sender shallow commits into "ours" and "theirs"
421 * Step 2, clean "ours" based on .git/shallow
423 void prepare_shallow_info(struct shallow_info
*info
, struct oid_array
*sa
)
426 trace_printf_key(&trace_shallow
, "shallow: prepare_shallow_info\n");
427 memset(info
, 0, sizeof(*info
));
431 ALLOC_ARRAY(info
->ours
, sa
->nr
);
432 ALLOC_ARRAY(info
->theirs
, sa
->nr
);
433 for (i
= 0; i
< sa
->nr
; i
++) {
434 if (has_object_file(sa
->oid
+ i
)) {
435 struct commit_graft
*graft
;
436 graft
= lookup_commit_graft(the_repository
,
438 if (graft
&& graft
->nr_parent
< 0)
440 info
->ours
[info
->nr_ours
++] = i
;
442 info
->theirs
[info
->nr_theirs
++] = i
;
446 void clear_shallow_info(struct shallow_info
*info
)
452 /* Step 4, remove non-existent ones in "theirs" after getting the pack */
454 void remove_nonexistent_theirs_shallow(struct shallow_info
*info
)
456 struct object_id
*oid
= info
->shallow
->oid
;
458 trace_printf_key(&trace_shallow
, "shallow: remove_nonexistent_theirs_shallow\n");
459 for (i
= dst
= 0; i
< info
->nr_theirs
; i
++) {
461 info
->theirs
[dst
] = info
->theirs
[i
];
462 if (has_object_file(oid
+ info
->theirs
[i
]))
465 info
->nr_theirs
= dst
;
468 define_commit_slab(ref_bitmap
, uint32_t *);
470 #define POOL_SIZE (512 * 1024)
473 struct ref_bitmap ref_bitmap
;
480 static uint32_t *paint_alloc(struct paint_info
*info
)
482 unsigned nr
= DIV_ROUND_UP(info
->nr_bits
, 32);
483 unsigned size
= nr
* sizeof(uint32_t);
485 if (!info
->pool_count
|| size
> info
->end
- info
->free
) {
486 if (size
> POOL_SIZE
)
487 BUG("pool size too small for %d in paint_alloc()",
490 REALLOC_ARRAY(info
->pools
, info
->pool_count
);
491 info
->free
= xmalloc(POOL_SIZE
);
492 info
->pools
[info
->pool_count
- 1] = info
->free
;
493 info
->end
= info
->free
+ POOL_SIZE
;
501 * Given a commit SHA-1, walk down to parents until either SEEN,
502 * UNINTERESTING or BOTTOM is hit. Set the id-th bit in ref_bitmap for
503 * all walked commits.
505 static void paint_down(struct paint_info
*info
, const struct object_id
*oid
,
509 struct commit_list
*head
= NULL
;
510 int bitmap_nr
= DIV_ROUND_UP(info
->nr_bits
, 32);
511 size_t bitmap_size
= st_mult(sizeof(uint32_t), bitmap_nr
);
512 struct commit
*c
= lookup_commit_reference_gently(the_repository
, oid
,
514 uint32_t *tmp
; /* to be freed before return */
520 tmp
= xmalloc(bitmap_size
);
521 bitmap
= paint_alloc(info
);
522 memset(bitmap
, 0, bitmap_size
);
523 bitmap
[id
/ 32] |= (1U << (id
% 32));
524 commit_list_insert(c
, &head
);
526 struct commit_list
*p
;
527 struct commit
*c
= pop_commit(&head
);
528 uint32_t **refs
= ref_bitmap_at(&info
->ref_bitmap
, c
);
530 /* XXX check "UNINTERESTING" from pack bitmaps if available */
531 if (c
->object
.flags
& (SEEN
| UNINTERESTING
))
534 c
->object
.flags
|= SEEN
;
539 memcpy(tmp
, *refs
, bitmap_size
);
540 for (i
= 0; i
< bitmap_nr
; i
++)
542 if (memcmp(tmp
, *refs
, bitmap_size
)) {
543 *refs
= paint_alloc(info
);
544 memcpy(*refs
, tmp
, bitmap_size
);
548 if (c
->object
.flags
& BOTTOM
)
552 die("unable to parse commit %s",
553 oid_to_hex(&c
->object
.oid
));
555 for (p
= c
->parents
; p
; p
= p
->next
) {
556 if (p
->item
->object
.flags
& SEEN
)
558 commit_list_insert(p
->item
, &head
);
562 nr
= get_max_object_index();
563 for (i
= 0; i
< nr
; i
++) {
564 struct object
*o
= get_indexed_object(i
);
565 if (o
&& o
->type
== OBJ_COMMIT
)
572 static int mark_uninteresting(const char *refname
, const struct object_id
*oid
,
573 int flags
, void *cb_data
)
575 struct commit
*commit
= lookup_commit_reference_gently(the_repository
,
579 commit
->object
.flags
|= UNINTERESTING
;
580 mark_parents_uninteresting(commit
);
584 static void post_assign_shallow(struct shallow_info
*info
,
585 struct ref_bitmap
*ref_bitmap
,
588 * Step 6(+7), associate shallow commits with new refs
590 * info->ref must be initialized before calling this function.
592 * If used is not NULL, it's an array of info->shallow->nr
593 * bitmaps. The n-th bit set in the m-th bitmap if ref[n] needs the
594 * m-th shallow commit from info->shallow.
596 * If used is NULL, "ours" and "theirs" are updated. And if ref_status
597 * is not NULL it's an array of ref->nr ints. ref_status[i] is true if
598 * the ref needs some shallow commits from either info->ours or
601 void assign_shallow_commits_to_refs(struct shallow_info
*info
,
602 uint32_t **used
, int *ref_status
)
604 struct object_id
*oid
= info
->shallow
->oid
;
605 struct oid_array
*ref
= info
->ref
;
607 int *shallow
, nr_shallow
= 0;
608 struct paint_info pi
;
610 trace_printf_key(&trace_shallow
, "shallow: assign_shallow_commits_to_refs\n");
611 ALLOC_ARRAY(shallow
, info
->nr_ours
+ info
->nr_theirs
);
612 for (i
= 0; i
< info
->nr_ours
; i
++)
613 shallow
[nr_shallow
++] = info
->ours
[i
];
614 for (i
= 0; i
< info
->nr_theirs
; i
++)
615 shallow
[nr_shallow
++] = info
->theirs
[i
];
618 * Prepare the commit graph to track what refs can reach what
619 * (new) shallow commits.
621 nr
= get_max_object_index();
622 for (i
= 0; i
< nr
; i
++) {
623 struct object
*o
= get_indexed_object(i
);
624 if (!o
|| o
->type
!= OBJ_COMMIT
)
627 o
->flags
&= ~(UNINTERESTING
| BOTTOM
| SEEN
);
630 memset(&pi
, 0, sizeof(pi
));
631 init_ref_bitmap(&pi
.ref_bitmap
);
632 pi
.nr_bits
= ref
->nr
;
635 * "--not --all" to cut short the traversal if new refs
636 * connect to old refs. If not (e.g. force ref updates) it'll
637 * have to go down to the current shallow commits.
639 head_ref(mark_uninteresting
, NULL
);
640 for_each_ref(mark_uninteresting
, NULL
);
642 /* Mark potential bottoms so we won't go out of bound */
643 for (i
= 0; i
< nr_shallow
; i
++) {
644 struct commit
*c
= lookup_commit(the_repository
,
646 c
->object
.flags
|= BOTTOM
;
649 for (i
= 0; i
< ref
->nr
; i
++)
650 paint_down(&pi
, ref
->oid
+ i
, i
);
653 int bitmap_size
= DIV_ROUND_UP(pi
.nr_bits
, 32) * sizeof(uint32_t);
654 memset(used
, 0, sizeof(*used
) * info
->shallow
->nr
);
655 for (i
= 0; i
< nr_shallow
; i
++) {
656 const struct commit
*c
= lookup_commit(the_repository
,
658 uint32_t **map
= ref_bitmap_at(&pi
.ref_bitmap
, c
);
660 used
[shallow
[i
]] = xmemdupz(*map
, bitmap_size
);
663 * unreachable shallow commits are not removed from
664 * "ours" and "theirs". The user is supposed to run
665 * step 7 on every ref separately and not trust "ours"
666 * and "theirs" any more.
669 post_assign_shallow(info
, &pi
.ref_bitmap
, ref_status
);
671 clear_ref_bitmap(&pi
.ref_bitmap
);
672 for (i
= 0; i
< pi
.pool_count
; i
++)
678 struct commit_array
{
679 struct commit
**commits
;
683 static int add_ref(const char *refname
, const struct object_id
*oid
,
684 int flags
, void *cb_data
)
686 struct commit_array
*ca
= cb_data
;
687 ALLOC_GROW(ca
->commits
, ca
->nr
+ 1, ca
->alloc
);
688 ca
->commits
[ca
->nr
] = lookup_commit_reference_gently(the_repository
,
690 if (ca
->commits
[ca
->nr
])
695 static void update_refstatus(int *ref_status
, int nr
, uint32_t *bitmap
)
700 for (i
= 0; i
< nr
; i
++)
701 if (bitmap
[i
/ 32] & (1U << (i
% 32)))
706 * Step 7, reachability test on "ours" at commit level
708 static void post_assign_shallow(struct shallow_info
*info
,
709 struct ref_bitmap
*ref_bitmap
,
712 struct object_id
*oid
= info
->shallow
->oid
;
716 int bitmap_nr
= DIV_ROUND_UP(info
->ref
->nr
, 32);
717 struct commit_array ca
;
719 trace_printf_key(&trace_shallow
, "shallow: post_assign_shallow\n");
721 memset(ref_status
, 0, sizeof(*ref_status
) * info
->ref
->nr
);
723 /* Remove unreachable shallow commits from "theirs" */
724 for (i
= dst
= 0; i
< info
->nr_theirs
; i
++) {
726 info
->theirs
[dst
] = info
->theirs
[i
];
727 c
= lookup_commit(the_repository
, &oid
[info
->theirs
[i
]]);
728 bitmap
= ref_bitmap_at(ref_bitmap
, c
);
731 for (j
= 0; j
< bitmap_nr
; j
++)
733 update_refstatus(ref_status
, info
->ref
->nr
, *bitmap
);
738 info
->nr_theirs
= dst
;
740 memset(&ca
, 0, sizeof(ca
));
741 head_ref(add_ref
, &ca
);
742 for_each_ref(add_ref
, &ca
);
744 /* Remove unreachable shallow commits from "ours" */
745 for (i
= dst
= 0; i
< info
->nr_ours
; i
++) {
747 info
->ours
[dst
] = info
->ours
[i
];
748 c
= lookup_commit(the_repository
, &oid
[info
->ours
[i
]]);
749 bitmap
= ref_bitmap_at(ref_bitmap
, c
);
752 for (j
= 0; j
< bitmap_nr
; j
++)
754 /* Step 7, reachability test at commit level */
755 !in_merge_bases_many(c
, ca
.nr
, ca
.commits
)) {
756 update_refstatus(ref_status
, info
->ref
->nr
, *bitmap
);
766 /* (Delayed) step 7, reachability test at commit level */
767 int delayed_reachability_test(struct shallow_info
*si
, int c
)
769 if (si
->need_reachability_test
[c
]) {
770 struct commit
*commit
= lookup_commit(the_repository
,
771 &si
->shallow
->oid
[c
]);
774 struct commit_array ca
;
776 memset(&ca
, 0, sizeof(ca
));
777 head_ref(add_ref
, &ca
);
778 for_each_ref(add_ref
, &ca
);
779 si
->commits
= ca
.commits
;
780 si
->nr_commits
= ca
.nr
;
783 si
->reachable
[c
] = in_merge_bases_many(commit
,
786 si
->need_reachability_test
[c
] = 0;
788 return si
->reachable
[c
];