Skip to content

Commit

Permalink
write-tree: --prefix=<path>
Browse files Browse the repository at this point in the history
The "bind" commit can express an aggregation of multiple
projects into a single commit.

In such an organization, there would be one project, root of
whose tree object is at the same level of the root of the
aggregated projects, and other projects have their toplevel in
separate subdirectories.  Let's call that root level project the
"primary project", and call other ones just "subprojects".

You would first read-tree the primary project, and then graft
the subprojects under their appropriate location using read-tree
--prefix=<subdir>/ repeatedly.

To write out a tree object from such an index for a subproject,
write-tree --prefix=<subdir>/ is used.

Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
Junio C Hamano committed May 2, 2006
1 parent f4c6f2d commit 6bd2035
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
8 changes: 7 additions & 1 deletion Documentation/git-write-tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ git-write-tree - Creates a tree object from the current index

SYNOPSIS
--------
'git-write-tree' [--missing-ok]
'git-write-tree' [--missing-ok] [--prefix=<prefix>/]

DESCRIPTION
-----------
Expand All @@ -30,6 +30,12 @@ OPTIONS
directory exist in the object database. This option disables this
check.

--prefix=<prefix>/::
Writes a tree object that represents a subdirectory
`<prefix>`. This can be used to write the tree object
for a subproject that is in the named subdirectory.


Author
------
Written by Linus Torvalds <[email protected]>
Expand Down
26 changes: 26 additions & 0 deletions cache-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,29 @@ struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
return NULL; /* not the whole tree */
return read_one(&buffer, &size);
}

struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
{
while (*path) {
const char *slash;
struct cache_tree_sub *sub;

slash = strchr(path, '/');
if (!slash)
slash = path + strlen(path);
/* between path and slash is the name of the
* subtree to look for.
*/
sub = find_subtree(it, path, slash - path, 0);
if (!sub)
return NULL;
it = sub->cache_tree;
if (slash)
while (*slash && *slash == '/')
slash++;
if (!slash || !*slash)
return it; /* prefix ended with slashes */
path = slash;
}
return it;
}
2 changes: 2 additions & 0 deletions cache-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
int cache_tree_fully_valid(struct cache_tree *);
int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int, int);

struct cache_tree *cache_tree_find(struct cache_tree *, const char *);

#endif
14 changes: 14 additions & 0 deletions t/t0000-basic.sh
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ test_expect_success \
'git-ls-tree -r output for a known tree.' \
'diff current expected'

test_expect_success \
'writing partial tree out with git-write-tree --prefix.' \
'ptree=$(git-write-tree --prefix=path3)'
test_expect_success \
'validate object ID for a known tree.' \
'test "$ptree" = 21ae8269cacbe57ae09138dcc3a2887f904d02b3'

test_expect_success \
'writing partial tree out with git-write-tree --prefix.' \
'ptree=$(git-write-tree --prefix=path3/subp3)'
test_expect_success \
'validate object ID for a known tree.' \
'test "$ptree" = 3c5e5399f3a333eddecce7a9b9465b63f65f51e2'

################################################################
rm .git/index
test_expect_success \
Expand Down
23 changes: 18 additions & 5 deletions write-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#include "cache-tree.h"

static int missing_ok = 0;
static char *prefix = NULL;

static const char write_tree_usage[] = "git-write-tree [--missing-ok]";
static const char write_tree_usage[] =
"git-write-tree [--missing-ok] [--prefix=<prefix>/]";

static struct cache_file cache_file;

Expand All @@ -21,13 +23,18 @@ int main(int argc, char **argv)

newfd = hold_index_file_for_update(&cache_file, get_index_file());
entries = read_cache();
if (argc == 2) {
if (!strcmp(argv[1], "--missing-ok"))

while (1 < argc) {
char *arg = argv[1];
if (!strcmp(arg, "--missing-ok"))
missing_ok = 1;
else if (!strncmp(arg, "--prefix=", 9))
prefix = arg + 9;
else
die(write_tree_usage);
argc--; argv++;
}

if (argc > 2)
die("too many options");

Expand All @@ -54,6 +61,12 @@ int main(int argc, char **argv)
* performance penalty and not a big deal.
*/
}
printf("%s\n", sha1_to_hex(active_cache_tree->sha1));
if (prefix) {
struct cache_tree *subtree =
cache_tree_find(active_cache_tree, prefix);
printf("%s\n", sha1_to_hex(subtree->sha1));
}
else
printf("%s\n", sha1_to_hex(active_cache_tree->sha1));
return 0;
}

0 comments on commit 6bd2035

Please sign in to comment.