@matheusbsbs / matheus-bagliano / commits / de84f99c12
Fork 0

Add --temp and --stage=all options to checkout-index.

Sometimes it is convient for a Porcelain to be able to checkout all unmerged files in all stages so that an external merge tool can be executed by the Porcelain or the end-user. Using git-unpack-file on each stage individually incurs a rather high penalty due to the need to fork for each file version obtained. git-checkout-index -a --stage=all will now do the same thing, but faster. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn Pearce committed Mar 5, 2006 at 03:24 UTC de84f99c12d1819479116685393afb1ebe99810b
7 files changed +371 -27
Documentation/git-checkout-index.txt
+49 -2
@@ -10,7 +10,8 @@ SYNOPSIS
10 --------
11 [verse]
12 'git-checkout-index' [-u] [-q] [-a] [-f] [-n] [--prefix=<string>]
13 - [--stage=<number>]
13 + [--stage=<number>|all]
14 + [--temp]
15 [-z] [--stdin]
16 [--] [<file>]\*
17
@@ -43,9 +44,15 @@ OPTIONS
44 When creating files, prepend <string> (usually a directory
45 including a trailing /)
46
46 ---stage=<number>::
47 +--stage=<number>|all::
48 Instead of checking out unmerged entries, copy out the
49 files from named stage. <number> must be between 1 and 3.
50 + Note: --stage=all automatically implies --temp.
51 +
52 +--temp::
53 + Instead of copying the files to the working directory
54 + write the content to temporary files. The temporary name
55 + associations will be written to stdout.
56
57 --stdin::
58 Instead of taking list of paths from the command line,
@@ -87,6 +94,46 @@ it will prevent problems with a filename of, for example, `-a`.
94 Using `--` is probably a good policy in scripts.
95
96
97 +Using --temp or --stage=all
98 +---------------------------
99 +When `--temp` is used (or implied by `--stage=all`)
100 +`git-checkout-index` will create a temporary file for each index
101 +entry being checked out. The index will not be updated with stat
102 +information. These options can be useful if the caller needs all
103 +stages of all unmerged entries so that the unmerged files can be
104 +processed by an external merge tool.
105 +
106 +A listing will be written to stdout providing the association of
107 +temporary file names to tracked path names. The listing format
108 +has two variations:
109 +
110 + . tempname TAB path RS
111 ++
112 +The first format is what gets used when `--stage` is omitted or
113 +is not `--stage=all`. The field tempname is the temporary file
114 +name holding the file content and path is the tracked path name in
115 +the index. Only the requested entries are output.
116 +
117 + . stage1temp SP stage2temp SP stage3tmp TAB path RS
118 ++
119 +The second format is what gets used when `--stage=all`. The three
120 +stage temporary fields (stage1temp, stage2temp, stage3temp) list the
121 +name of the temporary file if there is a stage entry in the index
122 +or `.` if there is no stage entry. Paths which only have a stage 0
123 +entry will always be omitted from the output.
124 +
125 +In both formats RS (the record separator) is newline by default
126 +but will be the null byte if -z was passed on the command line.
127 +The temporary file names are always safe strings; they will never
128 +contain directory separators or whitespace characters. The path
129 +field is always relative to the current directory and the temporary
130 +file names are always relative to the top level directory.
131 +
132 +If the object being copied out to a temporary file is a symbolic
133 +link the content of the link will be written to a normal file. It is
134 +up to the end-user or the Porcelain to make use of this information.
135 +
136 +
137 EXAMPLES
138 --------
139 To update and refresh only the files already checked out::
apply.c
+2 -1
@@ -1402,7 +1402,8 @@ static int check_patch(struct patch *patch)
1402 costate.not_new = 0;
1403 costate.refresh_cache = 1;
1404 if (checkout_entry(active_cache[pos],
1405 - &costate) ||
1405 + &costate,
1406 + NULL) ||
1407 lstat(old_name, &st))
1408 return -1;
1409 }
cache.h
+1 -1
@@ -262,7 +262,7 @@ struct checkout {
262 refresh_cache:1;
263 };
264
265 -extern int checkout_entry(struct cache_entry *ce, struct checkout *state);
265 +extern int checkout_entry(struct cache_entry *ce, struct checkout *state, char *topath);
266
267 extern struct alternate_object_database {
268 struct alternate_object_database *next;
checkout-index.c
+74 -12
@@ -40,9 +40,13 @@
40 #include "strbuf.h"
41 #include "quote.h"
42
43 +#define CHECKOUT_ALL 4
44 static const char *prefix;
45 static int prefix_length;
46 +static int line_termination = '\n';
47 static int checkout_stage; /* default to checkout stage0 */
48 +static int to_tempfile;
49 +static char topath[4][MAXPATHLEN+1];
50
51 static struct checkout state = {
52 .base_dir = "",
@@ -53,11 +57,39 @@ static struct checkout state = {
57 .refresh_cache = 0,
58 };
59
60 +static void write_tempfile_record (const char *name)
61 +{
62 + int i;
63 +
64 + if (CHECKOUT_ALL == checkout_stage) {
65 + for (i = 1; i < 4; i++) {
66 + if (i > 1)
67 + putchar(' ');
68 + if (topath[i][0])
69 + fputs(topath[i], stdout);
70 + else
71 + putchar('.');
72 + }
73 + } else
74 + fputs(topath[checkout_stage], stdout);
75 +
76 + putchar('\t');
77 + write_name_quoted("", 0, name + prefix_length,
78 + line_termination, stdout);
79 + putchar(line_termination);
80 +
81 + for (i = 0; i < 4; i++) {
82 + topath[i][0] = 0;
83 + }
84 +}
85 +
86 static int checkout_file(const char *name)
87 {
88 int namelen = strlen(name);
89 int pos = cache_name_pos(name, namelen);
90 int has_same_name = 0;
91 + int did_checkout = 0;
92 + int errs = 0;
93
94 if (pos < 0)
95 pos = -pos - 1;
@@ -68,9 +100,20 @@ static int checkout_file(const char *name)
100 memcmp(ce->name, name, namelen))
101 break;
102 has_same_name = 1;
71 - if (checkout_stage == ce_stage(ce))
72 - return checkout_entry(ce, &state);
103 pos++;
104 + if (ce_stage(ce) != checkout_stage
105 + && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
106 + continue;
107 + did_checkout = 1;
108 + if (checkout_entry(ce, &state,
109 + to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
110 + errs++;
111 + }
112 +
113 + if (did_checkout) {
114 + if (to_tempfile)
115 + write_tempfile_record(name);
116 + return errs > 0 ? -1 : 0;
117 }
118
119 if (!state.quiet) {
@@ -90,18 +133,29 @@ static int checkout_file(const char *name)
133 static int checkout_all(void)
134 {
135 int i, errs = 0;
136 + struct cache_entry* last_ce = 0;
137
138 for (i = 0; i < active_nr ; i++) {
139 struct cache_entry *ce = active_cache[i];
96 - if (ce_stage(ce) != checkout_stage)
140 + if (ce_stage(ce) != checkout_stage
141 + && (CHECKOUT_ALL != checkout_stage || !ce_stage(ce)))
142 continue;
143 if (prefix && *prefix &&
144 (ce_namelen(ce) <= prefix_length ||
145 memcmp(prefix, ce->name, prefix_length)))
146 continue;
102 - if (checkout_entry(ce, &state) < 0)
147 + if (last_ce && to_tempfile) {
148 + if (ce_namelen(last_ce) != ce_namelen(ce)
149 + || memcmp(last_ce->name, ce->name, ce_namelen(ce)))
150 + write_tempfile_record(last_ce->name);
151 + }
152 + if (checkout_entry(ce, &state,
153 + to_tempfile ? topath[ce_stage(ce)] : NULL) < 0)
154 errs++;
155 + last_ce = ce;
156 }
157 + if (last_ce && to_tempfile)
158 + write_tempfile_record(last_ce->name);
159 if (errs)
160 /* we have already done our error reporting.
161 * exit with the same code as die().
@@ -111,7 +165,7 @@ static int checkout_all(void)
165 }
166
167 static const char checkout_cache_usage[] =
114 -"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]] [--prefix=<string>] [--] <file>...";
168 +"git-checkout-index [-u] [-q] [-a] [-f] [-n] [--stage=[123]|all] [--prefix=<string>] [--temp] [--] <file>...";
169
170 static struct cache_file cache_file;
171
@@ -121,7 +175,6 @@ int main(int argc, char **argv)
175 int newfd = -1;
176 int all = 0;
177 int read_from_stdin = 0;
124 - int line_termination = '\n';
178
179 prefix = setup_git_directory();
180 git_config(git_default_config);
@@ -175,17 +228,26 @@ int main(int argc, char **argv)
228 i++; /* do not consider arg as a file name */
229 break;
230 }
231 + if (!strcmp(arg, "--temp")) {
232 + to_tempfile = 1;
233 + continue;
234 + }
235 if (!strncmp(arg, "--prefix=", 9)) {
236 state.base_dir = arg+9;
237 state.base_dir_len = strlen(state.base_dir);
238 continue;
239 }
240 if (!strncmp(arg, "--stage=", 8)) {
184 - int ch = arg[8];
185 - if ('1' <= ch && ch <= '3')
186 - checkout_stage = arg[8] - '0';
187 - else
188 - die("stage should be between 1 and 3");
241 + if (!strcmp(arg + 8, "all")) {
242 + to_tempfile = 1;
243 + checkout_stage = CHECKOUT_ALL;
244 + } else {
245 + int ch = arg[8];
246 + if ('1' <= ch && ch <= '3')
247 + checkout_stage = arg[8] - '0';
248 + else
249 + die("stage should be between 1 and 3 or all");
250 + }
251 continue;
252 }
253 if (arg[0] == '-')
@@ -193,7 +255,7 @@ int main(int argc, char **argv)
255 break;
256 }
257
196 - if (state.base_dir_len) {
258 + if (state.base_dir_len || to_tempfile) {
259 /* when --prefix is specified we do not
260 * want to update cache.
261 */
entry.c
+32 -10
@@ -63,7 +63,7 @@ static int create_file(const char *path, unsigned int mode)
63 return open(path, O_WRONLY | O_CREAT | O_EXCL, mode);
64 }
65
66 -static int write_entry(struct cache_entry *ce, const char *path, struct checkout *state)
66 +static int write_entry(struct cache_entry *ce, char *path, struct checkout *state, int to_tempfile)
67 {
68 int fd;
69 void *new;
@@ -80,7 +80,11 @@ static int write_entry(struct cache_entry *ce, const char *path, struct checkout
80 }
81 switch (ntohl(ce->ce_mode) & S_IFMT) {
82 case S_IFREG:
83 - fd = create_file(path, ntohl(ce->ce_mode));
83 + if (to_tempfile) {
84 + strcpy(path, ".merge_file_XXXXXX");
85 + fd = mkstemp(path);
86 + } else
87 + fd = create_file(path, ntohl(ce->ce_mode));
88 if (fd < 0) {
89 free(new);
90 return error("git-checkout-index: unable to create file %s (%s)",
@@ -93,12 +97,27 @@ static int write_entry(struct cache_entry *ce, const char *path, struct checkout
97 return error("git-checkout-index: unable to write file %s", path);
98 break;
99 case S_IFLNK:
96 - if (symlink(new, path)) {
100 + if (to_tempfile) {
101 + strcpy(path, ".merge_link_XXXXXX");
102 + fd = mkstemp(path);
103 + if (fd < 0) {
104 + free(new);
105 + return error("git-checkout-index: unable to create "
106 + "file %s (%s)", path, strerror(errno));
107 + }
108 + wrote = write(fd, new, size);
109 + close(fd);
110 + free(new);
111 + if (wrote != size)
112 + return error("git-checkout-index: unable to write file %s",
113 + path);
114 + } else {
115 + wrote = symlink(new, path);
116 free(new);
98 - return error("git-checkout-index: unable to create "
99 - "symlink %s (%s)", path, strerror(errno));
117 + if (wrote)
118 + return error("git-checkout-index: unable to create "
119 + "symlink %s (%s)", path, strerror(errno));
120 }
101 - free(new);
121 break;
122 default:
123 free(new);
@@ -113,12 +132,15 @@ static int write_entry(struct cache_entry *ce, const char *path, struct checkout
132 return 0;
133 }
134
116 -int checkout_entry(struct cache_entry *ce, struct checkout *state)
135 +int checkout_entry(struct cache_entry *ce, struct checkout *state, char *topath)
136 {
118 - struct stat st;
137 static char path[MAXPATHLEN+1];
138 + struct stat st;
139 int len = state->base_dir_len;
140
141 + if (topath)
142 + return write_entry(ce, topath, state, 1);
143 +
144 memcpy(path, state->base_dir, len);
145 strcpy(path + len, ce->name);
146
@@ -144,10 +166,10 @@ int checkout_entry(struct cache_entry *ce, struct checkout *state)
166 return error("%s is a directory", path);
167 remove_subtree(path);
168 }
147 - } else if (state->not_new)
169 + } else if (state->not_new)
170 return 0;
171 create_directories(path, state);
150 - return write_entry(ce, path, state);
172 + return write_entry(ce, path, state, 0);
173 }
174
175
read-tree.c
+1 -1
@@ -337,7 +337,7 @@ static void check_updates(struct cache_entry **src, int nr)
337 if (ce->ce_flags & mask) {
338 ce->ce_flags &= ~mask;
339 if (update)
340 - checkout_entry(ce, &state);
340 + checkout_entry(ce, &state, NULL);
341 }
342 }
343 if (total) {
t/t2004-checkout-cache-temp.sh new
+212
@@ -0,0 +1,212 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2006 Shawn Pearce
4 +#
5 +
6 +test_description='git-checkout-index --temp test.
7 +
8 +With --temp flag, git-checkout-index writes to temporary merge files
9 +rather than the tracked path.'
10 +
11 +. ./test-lib.sh
12 +
13 +test_expect_success \
14 +'preparation' '
15 +mkdir asubdir &&
16 +echo tree1path0 >path0 &&
17 +echo tree1path1 >path1 &&
18 +echo tree1path3 >path3 &&
19 +echo tree1path4 >path4 &&
20 +echo tree1asubdir/path5 >asubdir/path5 &&
21 +git-update-index --add path0 path1 path3 path4 asubdir/path5 &&
22 +t1=$(git-write-tree) &&
23 +rm -f path* .merge_* out .git/index &&
24 +echo tree2path0 >path0 &&
25 +echo tree2path1 >path1 &&
26 +echo tree2path2 >path2 &&
27 +echo tree2path4 >path4 &&
28 +git-update-index --add path0 path1 path2 path4 &&
29 +t2=$(git-write-tree) &&
30 +rm -f path* .merge_* out .git/index &&
31 +echo tree2path0 >path0 &&
32 +echo tree3path1 >path1 &&
33 +echo tree3path2 >path2 &&
34 +echo tree3path3 >path3 &&
35 +git-update-index --add path0 path1 path2 path3 &&
36 +t3=$(git-write-tree)'
37 +
38 +test_expect_success \
39 +'checkout one stage 0 to temporary file' '
40 +rm -f path* .merge_* out .git/index &&
41 +git-read-tree $t1 &&
42 +git-checkout-index --temp -- path1 >out &&
43 +test $(wc -l <out) = 1 &&
44 +test $(cut "-d " -f2 out) = path1 &&
45 +p=$(cut "-d " -f1 out) &&
46 +test -f $p &&
47 +test $(cat $p) = tree1path1'
48 +
49 +test_expect_success \
50 +'checkout all stage 0 to temporary files' '
51 +rm -f path* .merge_* out .git/index &&
52 +git-read-tree $t1 &&
53 +git-checkout-index -a --temp >out &&
54 +test $(wc -l <out) = 5 &&
55 +for f in path0 path1 path3 path4 asubdir/path5
56 +do
57 + test $(grep $f out | cut "-d " -f2) = $f &&
58 + p=$(grep $f out | cut "-d " -f1) &&
59 + test -f $p &&
60 + test $(cat $p) = tree1$f
61 +done'
62 +
63 +test_expect_success \
64 +'prepare 3-way merge' '
65 +rm -f path* .merge_* out .git/index &&
66 +git-read-tree -m $t1 $t2 $t3'
67 +
68 +test_expect_success \
69 +'checkout one stage 2 to temporary file' '
70 +rm -f path* .merge_* out &&
71 +git-checkout-index --stage=2 --temp -- path1 >out &&
72 +test $(wc -l <out) = 1 &&
73 +test $(cut "-d " -f2 out) = path1 &&
74 +p=$(cut "-d " -f1 out) &&
75 +test -f $p &&
76 +test $(cat $p) = tree2path1'
77 +
78 +test_expect_success \
79 +'checkout all stage 2 to temporary files' '
80 +rm -f path* .merge_* out &&
81 +git-checkout-index --all --stage=2 --temp >out &&
82 +test $(wc -l <out) = 3 &&
83 +for f in path1 path2 path4
84 +do
85 + test $(grep $f out | cut "-d " -f2) = $f &&
86 + p=$(grep $f out | cut "-d " -f1) &&
87 + test -f $p &&
88 + test $(cat $p) = tree2$f
89 +done'
90 +
91 +test_expect_success \
92 +'checkout all stages/one file to nothing' '
93 +rm -f path* .merge_* out &&
94 +git-checkout-index --stage=all --temp -- path0 >out &&
95 +test $(wc -l <out) = 0'
96 +
97 +test_expect_success \
98 +'checkout all stages/one file to temporary files' '
99 +rm -f path* .merge_* out &&
100 +git-checkout-index --stage=all --temp -- path1 >out &&
101 +test $(wc -l <out) = 1 &&
102 +test $(cut "-d " -f2 out) = path1 &&
103 +cut "-d " -f1 out | (read s1 s2 s3 &&
104 +test -f $s1 &&
105 +test -f $s2 &&
106 +test -f $s3 &&
107 +test $(cat $s1) = tree1path1 &&
108 +test $(cat $s2) = tree2path1 &&
109 +test $(cat $s3) = tree3path1)'
110 +
111 +test_expect_success \
112 +'checkout some stages/one file to temporary files' '
113 +rm -f path* .merge_* out &&
114 +git-checkout-index --stage=all --temp -- path2 >out &&
115 +test $(wc -l <out) = 1 &&
116 +test $(cut "-d " -f2 out) = path2 &&
117 +cut "-d " -f1 out | (read s1 s2 s3 &&
118 +test $s1 = . &&
119 +test -f $s2 &&
120 +test -f $s3 &&
121 +test $(cat $s2) = tree2path2 &&
122 +test $(cat $s3) = tree3path2)'
123 +
124 +test_expect_success \
125 +'checkout all stages/all files to temporary files' '
126 +rm -f path* .merge_* out &&
127 +git-checkout-index -a --stage=all --temp >out &&
128 +test $(wc -l <out) = 5'
129 +
130 +test_expect_success \
131 +'-- path0: no entry' '
132 +test x$(grep path0 out | cut "-d " -f2) = x'
133 +
134 +test_expect_success \
135 +'-- path1: all 3 stages' '
136 +test $(grep path1 out | cut "-d " -f2) = path1 &&
137 +grep path1 out | cut "-d " -f1 | (read s1 s2 s3 &&
138 +test -f $s1 &&
139 +test -f $s2 &&
140 +test -f $s3 &&
141 +test $(cat $s1) = tree1path1 &&
142 +test $(cat $s2) = tree2path1 &&
143 +test $(cat $s3) = tree3path1)'
144 +
145 +test_expect_success \
146 +'-- path2: no stage 1, have stage 2 and 3' '
147 +test $(grep path2 out | cut "-d " -f2) = path2 &&
148 +grep path2 out | cut "-d " -f1 | (read s1 s2 s3 &&
149 +test $s1 = . &&
150 +test -f $s2 &&
151 +test -f $s3 &&
152 +test $(cat $s2) = tree2path2 &&
153 +test $(cat $s3) = tree3path2)'
154 +
155 +test_expect_success \
156 +'-- path3: no stage 2, have stage 1 and 3' '
157 +test $(grep path3 out | cut "-d " -f2) = path3 &&
158 +grep path3 out | cut "-d " -f1 | (read s1 s2 s3 &&
159 +test -f $s1 &&
160 +test $s2 = . &&
161 +test -f $s3 &&
162 +test $(cat $s1) = tree1path3 &&
163 +test $(cat $s3) = tree3path3)'
164 +
165 +test_expect_success \
166 +'-- path4: no stage 3, have stage 1 and 3' '
167 +test $(grep path4 out | cut "-d " -f2) = path4 &&
168 +grep path4 out | cut "-d " -f1 | (read s1 s2 s3 &&
169 +test -f $s1 &&
170 +test -f $s2 &&
171 +test $s3 = . &&
172 +test $(cat $s1) = tree1path4 &&
173 +test $(cat $s2) = tree2path4)'
174 +
175 +test_expect_success \
176 +'-- asubdir/path5: no stage 2 and 3 have stage 1' '
177 +test $(grep asubdir/path5 out | cut "-d " -f2) = asubdir/path5 &&
178 +grep asubdir/path5 out | cut "-d " -f1 | (read s1 s2 s3 &&
179 +test -f $s1 &&
180 +test $s2 = . &&
181 +test $s3 = . &&
182 +test $(cat $s1) = tree1asubdir/path5)'
183 +
184 +test_expect_success \
185 +'checkout --temp within subdir' '
186 +(cd asubdir &&
187 + git-checkout-index -a --stage=all >out &&
188 + test $(wc -l <out) = 1 &&
189 + test $(grep path5 out | cut "-d " -f2) = path5 &&
190 + grep path5 out | cut "-d " -f1 | (read s1 s2 s3 &&
191 + test -f ../$s1 &&
192 + test $s2 = . &&
193 + test $s3 = . &&
194 + test $(cat ../$s1) = tree1asubdir/path5)
195 +)'
196 +
197 +test_expect_success \
198 +'checkout --temp symlink' '
199 +rm -f path* .merge_* out .git/index &&
200 +ln -s b a &&
201 +git-update-index --add a &&
202 +t4=$(git-write-tree) &&
203 +rm -f .git/index &&
204 +git-read-tree $t4 &&
205 +git-checkout-index --temp -a >out &&
206 +test $(wc -l <out) = 1 &&
207 +test $(cut "-d " -f2 out) = a &&
208 +p=$(cut "-d " -f1 out) &&
209 +test -f $p &&
210 +test $(cat $p) = b'
211 +
212 +test_done