shallow.c 3.4 KB
Newer Older
1 2
#include "cache.h"
#include "commit.h"
3
#include "tag.h"
4 5

static int is_shallow = -1;
6 7 8 9 10 11 12 13 14 15
static struct stat shallow_stat;
static char *alternate_shallow_file;

void set_alternate_shallow_file(const char *path)
{
	if (is_shallow != -1)
		die("BUG: is_repository_shallow must not be called before set_alternate_shallow_file");
	free(alternate_shallow_file);
	alternate_shallow_file = path ? xstrdup(path) : NULL;
}
16 17 18 19 20 21 22 23 24 25 26 27 28 29

int register_shallow(const unsigned char *sha1)
{
	struct commit_graft *graft =
		xmalloc(sizeof(struct commit_graft));
	struct commit *commit = lookup_commit(sha1);

	hashcpy(graft->sha1, sha1);
	graft->nr_parent = -1;
	if (commit && commit->object.parsed)
		commit->parents = NULL;
	return register_commit_graft(graft, 0);
}

30
int is_repository_shallow(void)
31 32 33
{
	FILE *fp;
	char buf[1024];
34
	const char *path = alternate_shallow_file;
35 36 37 38

	if (is_shallow >= 0)
		return is_shallow;

39 40 41 42 43 44 45 46 47 48
	if (!path)
		path = git_path("shallow");
	/*
	 * fetch-pack sets '--shallow-file ""' as an indicator that no
	 * shallow file should be used. We could just open it and it
	 * will likely fail. But let's do an explicit check instead.
	 */
	if (!*path ||
	    stat(path, &shallow_stat) ||
	    (fp = fopen(path, "r")) == NULL) {
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
		is_shallow = 0;
		return is_shallow;
	}
	is_shallow = 1;

	while (fgets(buf, sizeof(buf), fp)) {
		unsigned char sha1[20];
		if (get_sha1_hex(buf, sha1))
			die("bad shallow line: %s", buf);
		register_shallow(sha1);
	}
	fclose(fp);
	return is_shallow;
}

64 65
struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
		int shallow_flag, int not_shallow_flag)
66 67 68
{
	int i = 0, cur_depth = 0;
	struct commit_list *result = NULL;
69
	struct object_array stack = OBJECT_ARRAY_INIT;
70 71 72 73 74 75 76
	struct commit *commit = NULL;

	while (commit || i < heads->nr || stack.nr) {
		struct commit_list *p;
		if (!commit) {
			if (i < heads->nr) {
				commit = (struct commit *)
77
					deref_tag(heads->objects[i++].item, NULL, 0);
78
				if (!commit || commit->object.type != OBJ_COMMIT) {
79 80 81
					commit = NULL;
					continue;
				}
82 83 84
				if (!commit->util)
					commit->util = xmalloc(sizeof(int));
				*(int *)commit->util = 0;
85 86 87 88 89 90 91
				cur_depth = 0;
			} else {
				commit = (struct commit *)
					stack.objects[--stack.nr].item;
				cur_depth = *(int *)commit->util;
			}
		}
92 93
		if (parse_commit(commit))
			die("invalid commit");
94
		cur_depth++;
95 96 97 98 99 100 101
		if (cur_depth >= depth) {
			commit_list_insert(commit, &result);
			commit->object.flags |= shallow_flag;
			commit = NULL;
			continue;
		}
		commit->object.flags |= not_shallow_flag;
102 103 104 105 106 107 108 109 110 111 112
		for (p = commit->parents, commit = NULL; p; p = p->next) {
			if (!p->item->util) {
				int *pointer = xmalloc(sizeof(int));
				p->item->util = pointer;
				*pointer =  cur_depth;
			} else {
				int *pointer = p->item->util;
				if (cur_depth >= *pointer)
					continue;
				*pointer = cur_depth;
			}
113 114 115 116 117 118
			if (p->next)
				add_object_array(&p->item->object,
						NULL, &stack);
			else {
				commit = p->item;
				cur_depth = *(int *)commit->util;
119
			}
120 121 122 123 124
		}
	}

	return result;
}
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143

void check_shallow_file_for_update(void)
{
	struct stat st;

	if (!is_shallow)
		return;
	else if (is_shallow == -1)
		die("BUG: shallow must be initialized by now");

	if (stat(git_path("shallow"), &st))
		die("shallow file was removed during fetch");
	else if (st.st_mtime != shallow_stat.st_mtime
#ifdef USE_NSEC
		 || ST_MTIME_NSEC(st) != ST_MTIME_NSEC(shallow_stat)
#endif
		   )
		die("shallow file was changed during fetch");
}