diff options
author | Johannes Schindelin <Johannes.Schindelin@gmx.de> | 2006-10-30 20:09:06 +0100 |
---|---|---|
committer | Junio C Hamano <junkio@cox.net> | 2006-11-24 15:42:49 -0800 |
commit | ed09aef06fda2ba06a7412e3fa43ab1c3449f723 (patch) | |
tree | ae3de15cc0743c919e541f56a96082cf6c0b7cc8 /shallow.c | |
parent | 9b8dc263e1b0d470cc67a824837d8884ae3e7136 (diff) | |
download | git-ed09aef06fda2ba06a7412e3fa43ab1c3449f723.tar.gz git-ed09aef06fda2ba06a7412e3fa43ab1c3449f723.tar.xz |
support fetching into a shallow repository
A shallow commit is a commit which has parents, which in turn are
"grafted away", i.e. the commit appears as if it were a root.
Since these shallow commits should not be edited by the user, but
only by core git, they are recorded in the file $GIT_DIR/shallow.
A repository containing shallow commits is called shallow.
The advantage of a shallow repository is that even if the upstream
contains lots of history, your local (shallow) repository needs not
occupy much disk space.
The disadvantage is that you might miss a merge base when pulling
some remote branch.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <junkio@cox.net>
Diffstat (limited to 'shallow.c')
-rw-r--r-- | shallow.c | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/shallow.c b/shallow.c new file mode 100644 index 000000000..3cf212713 --- /dev/null +++ b/shallow.c @@ -0,0 +1,97 @@ +#include "cache.h" +#include "commit.h" + +static int is_shallow = -1; + +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); +} + +int is_repository_shallow() +{ + FILE *fp; + char buf[1024]; + + if (is_shallow >= 0) + return is_shallow; + + fp = fopen(git_path("shallow"), "r"); + if (!fp) { + 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; +} + +struct commit_list *get_shallow_commits(struct object_array *heads, int depth) +{ + int i = 0, cur_depth = 0; + struct commit_list *result = NULL; + struct object_array stack = {0, 0, NULL}; + struct commit *commit = NULL; + + while (commit || i < heads->nr || stack.nr) { + struct commit_list *p; + if (!commit) { + if (i < heads->nr) { + commit = (struct commit *) + heads->objects[i++].item; + if (commit->object.type != OBJ_COMMIT) { + commit = NULL; + continue; + } + commit->util = xcalloc(1, sizeof(int)); + cur_depth = 0; + } else { + commit = (struct commit *) + stack.objects[--stack.nr].item; + cur_depth = *(int *)commit->util; + } + } + parse_commit(commit); + cur_depth++; + 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; + } + if (cur_depth < depth) { + if (p->next) + add_object_array(&p->item->object, + NULL, &stack); + else { + commit = p->item; + cur_depth = *(int *)commit->util; + } + } else + commit_list_insert(p->item, &result); + } + } + + return result; +} + |