Skip to content

Commit 30ca07a

Browse files
author
Junio C Hamano
committed
_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 <[email protected]>
1 parent 89815ca commit 30ca07a

13 files changed

+50
-23
lines changed

Documentation/git.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,14 @@ git so take care if using Cogito etc.
315315
index file. If not specified, the default of `$GIT_DIR/index`
316316
is used.
317317

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+
318326
'GIT_OBJECT_DIRECTORY'::
319327
If the object storage directory is specified via this
320328
environment variable then the sha1 directories are created

builtin-add.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
133133

134134
git_config(git_add_config);
135135

136-
newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
136+
newfd = hold_locked_index(&lock_file, 1);
137137

138138
for (i = 1; i < argc; i++) {
139139
const char *arg = argv[i];
@@ -209,7 +209,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)
209209

210210
if (active_cache_changed) {
211211
if (write_cache(newfd, active_cache, active_nr) ||
212-
close(newfd) || commit_lock_file(&lock_file))
212+
close(newfd) || commit_locked_index(&lock_file))
213213
die("Unable to write new index file");
214214
}
215215

builtin-apply.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2664,8 +2664,8 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
26642664

26652665
write_index = check_index && apply;
26662666
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+
26692669
if (check_index) {
26702670
if (read_cache() < 0)
26712671
die("unable to read index file");
@@ -2872,7 +2872,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
28722872

28732873
if (write_index) {
28742874
if (write_cache(newfd, active_cache, active_nr) ||
2875-
close(newfd) || commit_lock_file(&lock_file))
2875+
close(newfd) || commit_locked_index(&lock_file))
28762876
die("Unable to write new index file");
28772877
}
28782878

builtin-checkout-index.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
202202
if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
203203
state.refresh_cache = 1;
204204
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);
209206
continue;
210207
}
211208
if (!strcmp(arg, "-z")) {
@@ -302,7 +299,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
302299

303300
if (0 <= newfd &&
304301
(write_cache(newfd, active_cache, active_nr) ||
305-
close(newfd) || commit_lock_file(&lock_file)))
302+
close(newfd) || commit_locked_index(&lock_file)))
306303
die("Unable to write new index file");
307304
return 0;
308305
}

builtin-mv.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
7777

7878
git_config(git_default_config);
7979

80-
newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
80+
newfd = hold_locked_index(&lock_file, 1);
8181
if (read_cache() < 0)
8282
die("index file corrupt");
8383

@@ -285,7 +285,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
285285
if (active_cache_changed) {
286286
if (write_cache(newfd, active_cache, active_nr) ||
287287
close(newfd) ||
288-
commit_lock_file(&lock_file))
288+
commit_locked_index(&lock_file))
289289
die("Unable to write new index file");
290290
}
291291
}

builtin-read-tree.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
100100
setup_git_directory();
101101
git_config(git_default_config);
102102

103-
newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
103+
newfd = hold_locked_index(&lock_file, 1);
104104

105105
git_config(git_default_config);
106106

@@ -267,7 +267,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
267267
}
268268

269269
if (write_cache(newfd, active_cache, active_nr) ||
270-
close(newfd) || commit_lock_file(&lock_file))
270+
close(newfd) || commit_locked_index(&lock_file))
271271
die("unable to write new index file");
272272
return 0;
273273
}

builtin-rm.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
110110

111111
git_config(git_default_config);
112112

113-
newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
113+
newfd = hold_locked_index(&lock_file, 1);
114114

115115
if (read_cache() < 0)
116116
die("index file corrupt");
@@ -220,7 +220,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
220220

221221
if (active_cache_changed) {
222222
if (write_cache(newfd, active_cache, active_nr) ||
223-
close(newfd) || commit_lock_file(&lock_file))
223+
close(newfd) || commit_locked_index(&lock_file))
224224
die("Unable to write new index file");
225225
}
226226

builtin-update-index.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
495495
/* We can't free this memory, it becomes part of a linked list parsed atexit() */
496496
lock_file = xcalloc(1, sizeof(struct lock_file));
497497

498-
newfd = hold_lock_file_for_update(lock_file, get_index_file(), 0);
498+
newfd = hold_locked_index(lock_file, 0);
499499
if (newfd < 0)
500500
lock_error = errno;
501501

@@ -661,7 +661,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
661661
get_index_file(), strerror(lock_error));
662662
}
663663
if (write_cache(newfd, active_cache, active_nr) ||
664-
close(newfd) || commit_lock_file(lock_file))
664+
close(newfd) || commit_locked_index(lock_file))
665665
die("Unable to write new index file");
666666
}
667667

builtin-write-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ int write_tree(unsigned char *sha1, int missing_ok, const char *prefix)
1818
/* We can't free this memory, it becomes part of a linked list parsed atexit() */
1919
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));
2020

21-
newfd = hold_lock_file_for_update(lock_file, get_index_file(), 0);
21+
newfd = hold_locked_index(lock_file, 1);
2222

2323
entries = read_cache();
2424
if (entries < 0)

cache.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ enum object_type {
147147
#define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
148148
#define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
149149
#define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
150+
#define INDEX_OUTPUT_ENVIRONMENT "_GIT_INDEX_OUTPUT"
150151
#define GRAFT_ENVIRONMENT "GIT_GRAFT_FILE"
151152
#define TEMPLATE_DIR_ENVIRONMENT "GIT_TEMPLATE_DIR"
152153
#define CONFIG_ENVIRONMENT "GIT_CONFIG"
@@ -212,6 +213,10 @@ struct lock_file {
212213
};
213214
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
214215
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+
215220
extern void rollback_lock_file(struct lock_file *);
216221
extern int delete_ref(const char *, unsigned char *sha1);
217222

0 commit comments

Comments
 (0)