Skip to content

Commit

Permalink
Merge branch 'jc/index-output'
Browse files Browse the repository at this point in the history
* jc/index-output:
  git-read-tree --index-output=<file>
  _GIT_INDEX_OUTPUT: allow plumbing to output to an alternative index file.

Conflicts:

	builtin-apply.c
  • Loading branch information
Junio C Hamano committed Apr 7, 2007
2 parents 3941544 + 5e7f56a commit ee9693e
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 25 deletions.
14 changes: 13 additions & 1 deletion Documentation/git-read-tree.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ git-read-tree - Reads tree information into the index

SYNOPSIS
--------
'git-read-tree' (<tree-ish> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] <tree-ish1> [<tree-ish2> [<tree-ish3>]])
'git-read-tree' (<tree-ish> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <tree-ish1> [<tree-ish2> [<tree-ish3>]])


DESCRIPTION
Expand Down Expand Up @@ -86,6 +86,18 @@ OPTIONS
file (usually '.gitignore') and allows such an untracked
but explicitly ignored file to be overwritten.

--index-output=<file>::
Instead of writing the results out to `$GIT_INDEX_FILE`,
write the resulting index in the named file. While the
command is operating, the original index file is locked
with the same mechanism as usual. The file must allow
to be rename(2)ed into from a temporary file that is
created next to the usual index file; typically this
means it needs to be on the same filesystem as the index
file itself, and you need write permission to the
directories the index file and index output file are
located in.

<tree-ish#>::
The id of the tree object(s) to be read/merged.

Expand Down
4 changes: 2 additions & 2 deletions builtin-add.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ int cmd_add(int argc, const char **argv, const char *prefix)

git_config(git_add_config);

newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
newfd = hold_locked_index(&lock_file, 1);

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

if (active_cache_changed) {
if (write_cache(newfd, active_cache, active_nr) ||
close(newfd) || commit_lock_file(&lock_file))
close(newfd) || commit_locked_index(&lock_file))
die("Unable to write new index file");
}

Expand Down
6 changes: 3 additions & 3 deletions builtin-apply.c
Original file line number Diff line number Diff line change
Expand Up @@ -2664,8 +2664,8 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)

update_index = check_index && apply;
if (update_index && newfd < 0)
newfd = hold_lock_file_for_update(&lock_file,
get_index_file(), 1);
newfd = hold_locked_index(&lock_file, 1);

if (check_index) {
if (read_cache() < 0)
die("unable to read index file");
Expand Down Expand Up @@ -2872,7 +2872,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)

if (update_index) {
if (write_cache(newfd, active_cache, active_nr) ||
close(newfd) || commit_lock_file(&lock_file))
close(newfd) || commit_locked_index(&lock_file))
die("Unable to write new index file");
}

Expand Down
7 changes: 2 additions & 5 deletions builtin-checkout-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)
if (!strcmp(arg, "-u") || !strcmp(arg, "--index")) {
state.refresh_cache = 1;
if (newfd < 0)
newfd = hold_lock_file_for_update
(&lock_file, get_index_file(), 1);
if (newfd < 0)
die("cannot open index.lock file.");
newfd = hold_locked_index(&lock_file, 1);
continue;
}
if (!strcmp(arg, "-z")) {
Expand Down Expand Up @@ -302,7 +299,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)

if (0 <= newfd &&
(write_cache(newfd, active_cache, active_nr) ||
close(newfd) || commit_lock_file(&lock_file)))
close(newfd) || commit_locked_index(&lock_file)))
die("Unable to write new index file");
return 0;
}
4 changes: 2 additions & 2 deletions builtin-mv.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)

git_config(git_default_config);

newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
newfd = hold_locked_index(&lock_file, 1);
if (read_cache() < 0)
die("index file corrupt");

Expand Down Expand Up @@ -285,7 +285,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
if (active_cache_changed) {
if (write_cache(newfd, active_cache, active_nr) ||
close(newfd) ||
commit_lock_file(&lock_file))
commit_locked_index(&lock_file))
die("Unable to write new index file");
}
}
Expand Down
11 changes: 8 additions & 3 deletions builtin-read-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static void prime_cache_tree(void)

}

static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] <sha1> [<sha2> [<sha3>]])";
static const char read_tree_usage[] = "git-read-tree (<sha> | [[-m [--aggressive] | --reset | --prefix=<prefix>] [-u | -i]] [--exclude-per-directory=<gitignore>] [--index-output=<file>] <sha1> [<sha2> [<sha3>]])";

static struct lock_file lock_file;

Expand All @@ -100,7 +100,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
setup_git_directory();
git_config(git_default_config);

newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
newfd = hold_locked_index(&lock_file, 1);

git_config(git_default_config);

Expand Down Expand Up @@ -128,6 +128,11 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
continue;
}

if (!prefixcmp(arg, "--index-output=")) {
set_alternate_index_output(arg + 15);
continue;
}

/* "--prefix=<subdirectory>/" means keep the current index
* entries and put the entries from the tree under the
* given subdirectory.
Expand Down Expand Up @@ -267,7 +272,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)
}

if (write_cache(newfd, active_cache, active_nr) ||
close(newfd) || commit_lock_file(&lock_file))
close(newfd) || commit_locked_index(&lock_file))
die("unable to write new index file");
return 0;
}
4 changes: 2 additions & 2 deletions builtin-rm.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)

git_config(git_default_config);

newfd = hold_lock_file_for_update(&lock_file, get_index_file(), 1);
newfd = hold_locked_index(&lock_file, 1);

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

if (active_cache_changed) {
if (write_cache(newfd, active_cache, active_nr) ||
close(newfd) || commit_lock_file(&lock_file))
close(newfd) || commit_locked_index(&lock_file))
die("Unable to write new index file");
}

Expand Down
4 changes: 2 additions & 2 deletions builtin-update-index.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
/* We can't free this memory, it becomes part of a linked list parsed atexit() */
lock_file = xcalloc(1, sizeof(struct lock_file));

newfd = hold_lock_file_for_update(lock_file, get_index_file(), 0);
newfd = hold_locked_index(lock_file, 0);
if (newfd < 0)
lock_error = errno;

Expand Down Expand Up @@ -661,7 +661,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
get_index_file(), strerror(lock_error));
}
if (write_cache(newfd, active_cache, active_nr) ||
close(newfd) || commit_lock_file(lock_file))
close(newfd) || commit_locked_index(lock_file))
die("Unable to write new index file");
}

Expand Down
2 changes: 1 addition & 1 deletion builtin-write-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int write_tree(unsigned char *sha1, int missing_ok, const char *prefix)
/* We can't free this memory, it becomes part of a linked list parsed atexit() */
struct lock_file *lock_file = xcalloc(1, sizeof(struct lock_file));

newfd = hold_lock_file_for_update(lock_file, get_index_file(), 0);
newfd = hold_locked_index(lock_file, 1);

entries = read_cache();
if (entries < 0)
Expand Down
5 changes: 5 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ struct lock_file {
};
extern int hold_lock_file_for_update(struct lock_file *, const char *path, int);
extern int commit_lock_file(struct lock_file *);

extern int hold_locked_index(struct lock_file *, int);
extern int commit_locked_index(struct lock_file *);
extern void set_alternate_index_output(const char *);

extern void rollback_lock_file(struct lock_file *);
extern int delete_ref(const char *, unsigned char *sha1);

Expand Down
4 changes: 2 additions & 2 deletions git-commit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -370,8 +370,8 @@ t,)
# the same way.
if test -z "$initial_commit"
then
cp "$THIS_INDEX" "$TMP_INDEX"
GIT_INDEX_FILE="$TMP_INDEX" git-read-tree -i -m HEAD
GIT_INDEX_FILE="$THIS_INDEX" \
git-read-tree --index-output="$TMP_INDEX" -i -m HEAD
else
rm -f "$TMP_INDEX"
fi || exit
Expand Down
22 changes: 22 additions & 0 deletions lockfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "cache.h"

static struct lock_file *lock_file_list;
static const char *alternate_index_output;

static void remove_lock_file(void)
{
Expand Down Expand Up @@ -65,6 +66,27 @@ int commit_lock_file(struct lock_file *lk)
return i;
}

int hold_locked_index(struct lock_file *lk, int die_on_error)
{
return hold_lock_file_for_update(lk, get_index_file(), die_on_error);
}

void set_alternate_index_output(const char *name)
{
alternate_index_output = name;
}

int commit_locked_index(struct lock_file *lk)
{
if (alternate_index_output) {
int result = rename(lk->filename, alternate_index_output);
lk->filename[0] = 0;
return result;
}
else
return commit_lock_file(lk);
}

void rollback_lock_file(struct lock_file *lk)
{
if (lk->filename[0])
Expand Down
4 changes: 2 additions & 2 deletions merge-recursive.c
Original file line number Diff line number Diff line change
Expand Up @@ -1378,7 +1378,7 @@ int main(int argc, char *argv[])
if (show(3))
printf("Merging %s with %s\n", branch1, branch2);

index_fd = hold_lock_file_for_update(lock, get_index_file(), 1);
index_fd = hold_locked_index(lock, 1);

for (i = 0; i < bases_count; i++) {
struct commit *ancestor = get_ref(bases[i]);
Expand All @@ -1388,7 +1388,7 @@ int main(int argc, char *argv[])

if (active_cache_changed &&
(write_cache(index_fd, active_cache, active_nr) ||
close(index_fd) || commit_lock_file(lock)))
close(index_fd) || commit_locked_index(lock)))
die ("unable to write %s", get_index_file());

return clean ? 0: 1;
Expand Down

0 comments on commit ee9693e

Please sign in to comment.