read-tree: --prefix=<path>/ option.
With "--prefix=<path>/" option, read-tree keeps the current index contents, and reads the contents of named tree-ish under directory at `<prefix>`. The original index file cannot have anything at the path `<prefix>` itself, and have nothing in `<prefix>/` directory. This can be used to graft an independent tree into a subdirectory of the current index. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Jan 14, 2006 at 21:46 UTC
f4c6f2d328e2f30ad63fdfca26a5e4a11cef35bf
2 files changed
+75
-8
Documentation/git-read-tree.txt
+10
-1
@@ -8,7 +8,7 @@ git-read-tree - Reads tree information into the index
8
9
SYNOPSIS
10
--------
11
-'git-read-tree' (<tree-ish> | [[-m [--aggressive]| --reset] [-u | -i]] <tree-ish1> [<tree-ish2> [<tree-ish3>]])
11
+'git-read-tree' (<tree-ish> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <tree-ish1> [<tree-ish2> [<tree-ish3>]])
12
13
14
DESCRIPTION
@@ -63,6 +63,15 @@ OPTIONS
63
* when both sides adds a path identically. The resolution
64
is to add that path.
65
66
+--prefix=<prefix>/::
67
+ Keep the current index contents, and read the contents
68
+ of named tree-ish under directory at `<prefix>`. The
69
+ original index file cannot have anything at the path
70
+ `<prefix>` itself, and have nothing in `<prefix>/`
71
+ directory. Note that the `<prefix>/` value must end
72
+ with a slash.
73
+
74
+
75
<tree-ish#>::
76
The id of the tree object(s) to be read/merged.
77
read-tree.c
+65
-7
@@ -21,6 +21,7 @@ static int trivial_merges_only = 0;
21
static int aggressive = 0;
22
static int verbose_update = 0;
23
static volatile int progress_update = 0;
24
+static const char *prefix = NULL;
25
26
static int head_idx = -1;
27
static int merge_size = 0;
@@ -369,7 +370,8 @@ static int unpack_trees(merge_fn_t fn)
370
posns[i] = ((struct tree *) posn->item)->entries;
371
posn = posn->next;
372
}
372
- if (unpack_trees_rec(posns, len, "", fn, &indpos))
373
+ if (unpack_trees_rec(posns, len, prefix ? prefix : "",
374
+ fn, &indpos))
375
return -1;
376
}
377
@@ -677,6 +679,28 @@ static int twoway_merge(struct cache_entry **src)
679
return deleted_entry(oldtree, current);
680
}
681
682
+/*
683
+ * Bind merge.
684
+ *
685
+ * Keep the index entries at stage0, collapse stage1 but make sure
686
+ * stage0 does not have anything in prefix.
687
+ */
688
+static int bind_merge(struct cache_entry **src)
689
+{
690
+ struct cache_entry *old = src[0];
691
+ struct cache_entry *a = src[1];
692
+
693
+ if (merge_size != 1)
694
+ return error("Cannot do a bind merge of %d trees\n",
695
+ merge_size);
696
+ if (!a)
697
+ return merged_entry(old, NULL);
698
+ if (old)
699
+ die("Entry '%s' overlaps. Cannot bind.", a->name);
700
+
701
+ return merged_entry(a, NULL);
702
+}
703
+
704
/*
705
* One-way merge.
706
*
@@ -758,7 +782,7 @@ static void prime_cache_tree(void)
782
783
}
784
761
-static const char read_tree_usage[] = "git-read-tree (<sha> | -m [--aggressive] [-u | -i] <sha1> [<sha2> [<sha3>]])";
785
+static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] <sha1> [<sha2> [<sha3>]])";
786
787
static struct cache_file cache_file;
788
@@ -803,9 +827,24 @@ int main(int argc, char **argv)
827
continue;
828
}
829
830
+ /* "--prefix=<subdirectory>/" means keep the current index
831
+ * entries and put the entries from the tree under the
832
+ * given subdirectory.
833
+ */
834
+ if (!strncmp(arg, "--prefix=", 9)) {
835
+ if (stage || merge || prefix)
836
+ usage(read_tree_usage);
837
+ prefix = arg + 9;
838
+ merge = 1;
839
+ stage = 1;
840
+ if (read_cache_unmerged())
841
+ die("you need to resolve your current index first");
842
+ continue;
843
+ }
844
+
845
/* This differs from "-m" in that we'll silently ignore unmerged entries */
846
if (!strcmp(arg, "--reset")) {
808
- if (stage || merge)
847
+ if (stage || merge || prefix)
848
usage(read_tree_usage);
849
reset = 1;
850
merge = 1;
@@ -826,7 +865,7 @@ int main(int argc, char **argv)
865
866
/* "-m" stands for "merge", meaning we start in stage 1 */
867
if (!strcmp(arg, "-m")) {
829
- if (stage || merge)
868
+ if (stage || merge || prefix)
869
usage(read_tree_usage);
870
if (read_cache_unmerged())
871
die("you need to resolve your current index first");
@@ -848,12 +887,31 @@ int main(int argc, char **argv)
887
if ((update||index_only) && !merge)
888
usage(read_tree_usage);
889
890
+ if (prefix) {
891
+ int pfxlen = strlen(prefix);
892
+ int pos;
893
+ if (prefix[pfxlen-1] != '/')
894
+ die("prefix must end with /");
895
+ if (stage != 2)
896
+ die("binding merge takes only one tree");
897
+ pos = cache_name_pos(prefix, pfxlen);
898
+ if (0 <= pos)
899
+ die("corrupt index file");
900
+ pos = -pos-1;
901
+ if (pos < active_nr &&
902
+ !strncmp(active_cache[pos]->name, prefix, pfxlen))
903
+ die("subdirectory '%s' already exists.", prefix);
904
+ pos = cache_name_pos(prefix, pfxlen-1);
905
+ if (0 <= pos)
906
+ die("file '%.*s' already exists.", pfxlen-1, prefix);
907
+ }
908
+
909
if (merge) {
910
if (stage < 2)
911
die("just how do you expect me to merge %d trees?", stage-1);
912
switch (stage - 1) {
913
case 1:
856
- fn = oneway_merge;
914
+ fn = prefix ? bind_merge : oneway_merge;
915
break;
916
case 2:
917
fn = twoway_merge;
@@ -879,8 +937,8 @@ int main(int argc, char **argv)
937
* valid cache-tree because the index must match exactly
938
* what came from the tree.
939
*/
882
- if (trees->item && (!merge || (stage == 2))) {
883
- cache_tree_free(&active_cache_tree);
940
+ if (trees->item && !prefix && (!merge || (stage == 2))) {
941
+ cache_tree_free(&active_cache_tree);
942
prime_cache_tree();
943
}
944