@matheusbsbs / matheus-bagliano / commits / 6bd20358a9
Fork 0

write-tree: --prefix=<path>

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 <junkio@cox.net>

Junio C Hamano committed Apr 26, 2006 at 01:20 UTC 6bd20358a9b831b3b545284188871bc844245c25
5 files changed +67 -6
Documentation/git-write-tree.txt
+7 -1
@@ -8,7 +8,7 @@ git-write-tree - Creates a tree object from the current index
8
9 SYNOPSIS
10 --------
11 -'git-write-tree' [--missing-ok]
11 +'git-write-tree' [--missing-ok] [--prefix=<prefix>/]
12
13 DESCRIPTION
14 -----------
@@ -30,6 +30,12 @@ OPTIONS
30 directory exist in the object database. This option disables this
31 check.
32
33 +--prefix=<prefix>/::
34 + Writes a tree object that represents a subdirectory
35 + `<prefix>`. This can be used to write the tree object
36 + for a subproject that is in the named subdirectory.
37 +
38 +
39 Author
40 ------
41 Written by Linus Torvalds <torvalds@osdl.org>
cache-tree.c
+26
@@ -525,3 +525,29 @@ struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
525 return NULL; /* not the whole tree */
526 return read_one(&buffer, &size);
527 }
528 +
529 +struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
530 +{
531 + while (*path) {
532 + const char *slash;
533 + struct cache_tree_sub *sub;
534 +
535 + slash = strchr(path, '/');
536 + if (!slash)
537 + slash = path + strlen(path);
538 + /* between path and slash is the name of the
539 + * subtree to look for.
540 + */
541 + sub = find_subtree(it, path, slash - path, 0);
542 + if (!sub)
543 + return NULL;
544 + it = sub->cache_tree;
545 + if (slash)
546 + while (*slash && *slash == '/')
547 + slash++;
548 + if (!slash || !*slash)
549 + return it; /* prefix ended with slashes */
550 + path = slash;
551 + }
552 + return it;
553 +}
cache-tree.h
+2
@@ -28,4 +28,6 @@ struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
28 int cache_tree_fully_valid(struct cache_tree *);
29 int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int, int);
30
31 +struct cache_tree *cache_tree_find(struct cache_tree *, const char *);
32 +
33 #endif
t/t0000-basic.sh
+14
@@ -195,6 +195,20 @@ test_expect_success \
195 'git-ls-tree -r output for a known tree.' \
196 'diff current expected'
197
198 +test_expect_success \
199 + 'writing partial tree out with git-write-tree --prefix.' \
200 + 'ptree=$(git-write-tree --prefix=path3)'
201 +test_expect_success \
202 + 'validate object ID for a known tree.' \
203 + 'test "$ptree" = 21ae8269cacbe57ae09138dcc3a2887f904d02b3'
204 +
205 +test_expect_success \
206 + 'writing partial tree out with git-write-tree --prefix.' \
207 + 'ptree=$(git-write-tree --prefix=path3/subp3)'
208 +test_expect_success \
209 + 'validate object ID for a known tree.' \
210 + 'test "$ptree" = 3c5e5399f3a333eddecce7a9b9465b63f65f51e2'
211 +
212 ################################################################
213 rm .git/index
214 test_expect_success \
write-tree.c
+18 -5
@@ -8,8 +8,10 @@
8 #include "cache-tree.h"
9
10 static int missing_ok = 0;
11 +static char *prefix = NULL;
12
12 -static const char write_tree_usage[] = "git-write-tree [--missing-ok]";
13 +static const char write_tree_usage[] =
14 +"git-write-tree [--missing-ok] [--prefix=<prefix>/]";
15
16 static struct cache_file cache_file;
17
@@ -21,13 +23,18 @@ int main(int argc, char **argv)
23
24 newfd = hold_index_file_for_update(&cache_file, get_index_file());
25 entries = read_cache();
24 - if (argc == 2) {
25 - if (!strcmp(argv[1], "--missing-ok"))
26 +
27 + while (1 < argc) {
28 + char *arg = argv[1];
29 + if (!strcmp(arg, "--missing-ok"))
30 missing_ok = 1;
31 + else if (!strncmp(arg, "--prefix=", 9))
32 + prefix = arg + 9;
33 else
34 die(write_tree_usage);
35 + argc--; argv++;
36 }
30 -
37 +
38 if (argc > 2)
39 die("too many options");
40
@@ -54,6 +61,12 @@ int main(int argc, char **argv)
61 * performance penalty and not a big deal.
62 */
63 }
57 - printf("%s\n", sha1_to_hex(active_cache_tree->sha1));
64 + if (prefix) {
65 + struct cache_tree *subtree =
66 + cache_tree_find(active_cache_tree, prefix);
67 + printf("%s\n", sha1_to_hex(subtree->sha1));
68 + }
69 + else
70 + printf("%s\n", sha1_to_hex(active_cache_tree->sha1));
71 return 0;
72 }