@matheusbsbs / matheus-bagliano / commits / 30ca07a249
Fork 0

_GIT_INDEX_OUTPUT: allow plumbing to output to an alternative index file.

When defined, this allows plumbing commands that update the index (add, apply, checkout-index, merge-recursive, mv, read-tree, rm, update-index, and write-tree) to write their resulting index to an alternative index file while holding a lock to the original index file. With this, git-commit that jumps the index does not have to make an extra copy of the index file, and more importantly, it can do the update while holding the lock on the index. However, I think the interface to let an environment variable specify the output is a mistake, as shown in the documentation. If a curious user has the environment variable set to something other than the file GIT_INDEX_FILE points at, almost everything will break. This should instead be a command line parameter to tell these plumbing commands to write the result in the named file, to prevent stupid mistakes. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Mar 31, 2007 at 23:09 UTC 30ca07a249744e57163c02250fca420cea364299
13 files changed +50 -23
Documentation/git.txt
+8
@@ -315,6 +315,14 @@ git so take care if using Cogito etc.
315 index file. If not specified, the default of `$GIT_DIR/index`
316 is used.
317
318 +'_GIT_INDEX_OUTPUT'::
319 + When this environment is defined, plumbing level
320 + commands that update the index writes the resulting
321 + index to this file, instead of `$GIT_INDEX_FILE` (or its
322 + default `$GIT_DIR/index`). This is solely meant to be
323 + used by Porcelain to drive low-level plumbing. Defining
324 + this in user's environment is always an error.
325 +
326 'GIT_OBJECT_DIRECTORY'::
327 If the object storage directory is specified via this
328 environment variable then the sha1 directories are created
builtin-add.c
+2 -2
@@ -133,7 +133,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
133
134 git_config(git_add_config);
135
136 - newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
136 + newfd = hold_locked_index(&lock_file, 1);
137
138 for (i = 1; i < argc; i++) {
139 const char *arg = argv[i];
@@ -209,7 +209,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
209
210 if (active_cache_changed) {
211 if (write_cache(newfd, active_cache, active_nr) ||
212 - close(newfd) || commit_lock_file(&lock_file))
212 + close(newfd) || commit_locked_index(&lock_file))
213 die("Unable to write new index file");
214 }
215
builtin-apply.c
+3 -3
@@ -2664,8 +2664,8 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
2664
2665 write_index = check_index && apply;
2666 if (write_index && newfd < 0)
2667 - newfd = hold_lock_file_for_update(&lock_file,
2668 - get_index_file(), 1);
2667 + newfd = hold_locked_index(&lock_file, 1);
2668 +
2669 if (check_index) {
2670 if (read_cache() < 0)
2671 die("unable to read index file");
@@ -2872,7 +2872,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
2872
2873 if (write_index) {
2874 if (write_cache(newfd, active_cache, active_nr) ||
2875 - close(newfd) || commit_lock_file(&lock_file))
2875 + close(newfd) || commit_locked_index(&lock_file))
2876 die("Unable to write new index file");
2877 }
2878
builtin-checkout-index.c
+2 -5
@@ -202,10 +202,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
202 if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
203 state.refresh_cache = 1;
204 if (newfd < 0)
205 - newfd = hold_lock_file_for_update
206 - (&lock_file, get_index_file(), 1);
207 - if (newfd < 0)
208 - die("cannot open index.lock file.");
205 + newfd = hold_locked_index(&lock_file, 1);
206 continue;
207 }
208 if (!strcmp(arg, "-z")) {
@@ -302,7 +299,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
299
300 if (0 <= newfd &&
301 (write_cache(newfd, active_cache, active_nr) ||
305 - close(newfd) || commit_lock_file(&lock_file)))
302 + close(newfd) || commit_locked_index(&lock_file)))
303 die("Unable to write new index file");
304 return 0;
305 }
builtin-mv.c
+2 -2
@@ -77,7 +77,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
77
78 git_config(git_default_config);
79
80 - newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
80 + newfd = hold_locked_index(&lock_file, 1);
81 if (read_cache() < 0)
82 die("index file corrupt");
83
@@ -285,7 +285,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
285 if (active_cache_changed) {
286 if (write_cache(newfd, active_cache, active_nr) ||
287 close(newfd) ||
288 - commit_lock_file(&lock_file))
288 + commit_locked_index(&lock_file))
289 die("Unable to write new index file");
290 }
291 }
builtin-read-tree.c
+2 -2
@@ -100,7 +100,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
100 setup_git_directory();
101 git_config(git_default_config);
102
103 - newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
103 + newfd = hold_locked_index(&lock_file, 1);
104
105 git_config(git_default_config);
106
@@ -267,7 +267,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
267 }
268
269 if (write_cache(newfd, active_cache, active_nr) ||
270 - close(newfd) || commit_lock_file(&lock_file))
270 + close(newfd) || commit_locked_index(&lock_file))
271 die("unable to write new index file");
272 return 0;
273 }
builtin-rm.c
+2 -2
@@ -110,7 +110,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
110
111 git_config(git_default_config);
112
113 - newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
113 + newfd = hold_locked_index(&lock_file, 1);
114
115 if (read_cache() < 0)
116 die("index file corrupt");
@@ -220,7 +220,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
220
221 if (active_cache_changed) {
222 if (write_cache(newfd, active_cache, active_nr) ||
223 - close(newfd) || commit_lock_file(&lock_file))
223 + close(newfd) || commit_locked_index(&lock_file))
224 die("Unable to write new index file");
225 }
226
builtin-update-index.c
+2 -2
@@ -495,7 +495,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
495 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
496 lock_file = xcalloc(1, sizeof(struct lock_file));
497
498 - newfd = hold_lock_file_for_update(lock_file, get_index_file(), 0);
498 + newfd = hold_locked_index(lock_file, 0);
499 if (newfd < 0)
500 lock_error = errno;
501
@@ -661,7 +661,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
661 get_index_file(), strerror(lock_error));
662 }
663 if (write_cache(newfd, active_cache, active_nr) ||
664 - close(newfd) || commit_lock_file(lock_file))
664 + close(newfd) || commit_locked_index(lock_file))
665 die("Unable to write new index file");
666 }
667
builtin-write-tree.c
+1 -1
@@ -18,7 +18,7 @@ int write_tree(unsigned char *sha1, int missing_ok, const char *prefix)
18 /* We can't free this memory, it becomes part of a linked list parsed atexit() */
19 struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
20
21 - newfd = hold_lock_file_for_update(lock_file, get_index_file(), 0);
21 + newfd = hold_locked_index(lock_file, 1);
22
23 entries = read_cache();
24 if (entries < 0)
cache.h
+5
@@ -147,6 +147,7 @@ enum object_type {
147 #define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
148 #define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
149 #define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
150 +#define INDEX_OUTPUT_ENVIRONMENT "_GIT_INDEX_OUTPUT"
151 #define GRAFT_ENVIRONMENT "GIT_GRAFT_FILE"
152 #define TEMPLATE_DIR_ENVIRONMENT "GIT_TEMPLATE_DIR"
153 #define CONFIG_ENVIRONMENT "GIT_CONFIG"
@@ -212,6 +213,10 @@ struct lock_file {
213 };
214 extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
215 extern int commit_lock_file(struct lock_file *);
216 +
217 +extern int hold_locked_index(struct lock_file *, int);
218 +extern int commit_locked_index(struct lock_file *);
219 +
220 extern void rollback_lock_file(struct lock_file *);
221 extern int delete_ref(const char *, unsigned char *sha1);
222
git-commit.sh
+2 -2
@@ -370,8 +370,8 @@ t,)
370 # the same way.
371 if test -z "$initial_commit"
372 then
373 - cp "$THIS_INDEX" "$TMP_INDEX"
374 - GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -i -m HEAD
373 + _GIT_INDEX_OUTPUT="$TMP_INDEX" \
374 + GIT_INDEX_FILE="$THIS_INDEX" git-read-tree -i -m HEAD
375 else
376 rm -f "$TMP_INDEX"
377 fi || exit
lockfile.c
+17
@@ -65,6 +65,23 @@ int commit_lock_file(struct lock_file *lk)
65 return i;
66 }
67
68 +int hold_locked_index(struct lock_file *lk, int die_on_error)
69 +{
70 + return hold_lock_file_for_update(lk, get_index_file(), die_on_error);
71 +}
72 +
73 +int commit_locked_index(struct lock_file *lk)
74 +{
75 + char *output = getenv(INDEX_OUTPUT_ENVIRONMENT);
76 + if (output && *output) {
77 + int result = rename(lk->filename, output);
78 + lk->filename[0] = 0;
79 + return result;
80 + }
81 + else
82 + return commit_lock_file(lk);
83 +}
84 +
85 void rollback_lock_file(struct lock_file *lk)
86 {
87 if (lk->filename[0])
merge-recursive.c
+2 -2
@@ -1378,7 +1378,7 @@ int main(int argc, char *argv[])
1378 if (show(3))
1379 printf("Merging %s with %s\n", branch1, branch2);
1380
1381 - index_fd = hold_lock_file_for_update(lock, get_index_file(), 1);
1381 + index_fd = hold_locked_index(lock, 1);
1382
1383 for (i = 0; i < bases_count; i++) {
1384 struct commit *ancestor = get_ref(bases[i]);
@@ -1388,7 +1388,7 @@ int main(int argc, char *argv[])
1388
1389 if (active_cache_changed &&
1390 (write_cache(index_fd, active_cache, active_nr) ||
1391 - close(index_fd) || commit_lock_file(lock)))
1391 + close(index_fd) || commit_locked_index(lock)))
1392 die ("unable to write %s", get_index_file());
1393
1394 return clean ? 0: 1;