From 83e77a25dc194933c0fb7908ab6d9fb84a5045e2 Mon Sep 17 00:00:00 2001
From: Junio C Hamano <junkio@cox.net>
Date: Fri, 5 May 2006 17:40:47 -0700
Subject: [PATCH 1/3] update-index --again

After running 'git-update-index' for some paths, you may want to
do the update on the same set of paths again.

The new flag --again checks the paths whose index entries are
are different from the HEAD commit and updates them from the
working tree contents.

This was brought up by Carl Worth on #git.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 Documentation/git-update-index.txt |  6 ++-
 t/t2101-update-index-reupdate.sh   | 73 ++++++++++++++++++++++++++++++
 update-index.c                     | 56 +++++++++++++++++++++--
 3 files changed, 131 insertions(+), 4 deletions(-)
 create mode 100755 t/t2101-update-index-reupdate.sh

diff --git a/Documentation/git-update-index.txt b/Documentation/git-update-index.txt
index 57177c7fb6..d043e86a77 100644
--- a/Documentation/git-update-index.txt
+++ b/Documentation/git-update-index.txt
@@ -15,7 +15,7 @@ SYNOPSIS
 	     [--cacheinfo <mode> <object> <file>]\*
 	     [--chmod=(+|-)x]
 	     [--assume-unchanged | --no-assume-unchanged]
-	     [--really-refresh] [--unresolve]
+	     [--really-refresh] [--unresolve] [--again]
 	     [--info-only] [--index-info]
 	     [-z] [--stdin]
 	     [--verbose]
@@ -80,6 +80,10 @@ OPTIONS
 	filesystem that has very slow lstat(2) system call
 	(e.g. cifs).
 
+--again::
+	Runs `git-update-index` itself on the paths whose index
+	entries are different from those from the `HEAD` commit.
+
 --unresolve::
 	Restores the 'unmerged' or 'needs updating' state of a
 	file during a merge if it was cleared by accident.
diff --git a/t/t2101-update-index-reupdate.sh b/t/t2101-update-index-reupdate.sh
new file mode 100755
index 0000000000..5c505c6ac8
--- /dev/null
+++ b/t/t2101-update-index-reupdate.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='git-update-index --again test.
+'
+
+. ./test-lib.sh
+
+test_expect_success 'update-index --add' \
+	'echo hello world >file1 &&
+	 echo goodbye people >file2 &&
+	 git-update-index --add file1 file2 &&
+	 git-ls-files -s >current &&
+	 cmp current - <<\EOF
+100644 3b18e512dba79e4c8300dd08aeb37f8e728b8dad 0	file1
+100644 9db8893856a8a02eaa73470054b7c1c5a7c82e47 0	file2
+EOF'
+
+test_expect_success 'update-index --again' \
+	'rm -f file1 &&
+	echo hello everybody >file2 &&
+	if git-update-index --again
+	then
+		echo should have refused to remove file1
+		exit 1
+	else
+		echo happy - failed as expected
+	fi &&
+	 git-ls-files -s >current &&
+	 cmp current - <<\EOF
+100644 3b18e512dba79e4c8300dd08aeb37f8e728b8dad 0	file1
+100644 9db8893856a8a02eaa73470054b7c1c5a7c82e47 0	file2
+EOF'
+
+test_expect_success 'update-index --remove --again' \
+	'git-update-index --remove --again &&
+	 git-ls-files -s >current &&
+	 cmp current - <<\EOF
+100644 0f1ae1422c2bf43f117d3dbd715c988a9ed2103f 0	file2
+EOF'
+
+test_expect_success 'first commit' 'git-commit -m initial'
+
+test_expect_success 'update-index again' \
+	'mkdir -p dir1 &&
+	echo hello world >dir1/file3 &&
+	echo goodbye people >file2 &&
+	git-update-index --add file2 dir1/file3 &&
+	echo hello everybody >file2
+	echo happy >dir1/file3 &&
+	git-update-index --again &&
+	git-ls-files -s >current &&
+	cmp current - <<\EOF
+100644 53ab446c3f4e42ce9bb728a0ccb283a101be4979 0	dir1/file3
+100644 0f1ae1422c2bf43f117d3dbd715c988a9ed2103f 0	file2
+EOF'
+
+test_expect_success 'update-index --update from subdir' \
+	'echo not so happy >file2 &&
+	cd dir1 &&
+	cat ../file2 >file3 &&
+	git-update-index --again &&
+	cd .. &&
+	git-ls-files -s >current &&
+	cmp current - <<\EOF
+100644 d7fb3f695f06c759dbf3ab00046e7cc2da22d10f 0	dir1/file3
+100644 0f1ae1422c2bf43f117d3dbd715c988a9ed2103f 0	file2
+EOF'
+
+test_done
+
diff --git a/update-index.c b/update-index.c
index d63f8ac998..5496d68ae9 100644
--- a/update-index.c
+++ b/update-index.c
@@ -476,7 +476,7 @@ static void read_index_info(int line_termination)
 }
 
 static const char update_index_usage[] =
-"git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--ignore-missing] [-z] [--verbose] [--] <file>...";
+"git-update-index [-q] [--add] [--replace] [--remove] [--unmerged] [--refresh] [--really-refresh] [--cacheinfo] [--chmod=(+|-)x] [--assume-unchanged] [--info-only] [--force-remove] [--stdin] [--index-info] [--unresolve] [--again] [--ignore-missing] [-z] [--verbose] [--] <file>...";
 
 static unsigned char head_sha1[20];
 static unsigned char merge_head_sha1[20];
@@ -491,11 +491,13 @@ static struct cache_entry *read_one_ent(const char *which,
 	struct cache_entry *ce;
 
 	if (get_tree_entry(ent, path, sha1, &mode)) {
-		error("%s: not in %s branch.", path, which);
+		if (which)
+			error("%s: not in %s branch.", path, which);
 		return NULL;
 	}
 	if (mode == S_IFDIR) {
-		error("%s: not a blob in %s branch.", path, which);
+		if (which)
+			error("%s: not a blob in %s branch.", path, which);
 		return NULL;
 	}
 	size = cache_entry_size(namelen);
@@ -600,6 +602,47 @@ static int do_unresolve(int ac, const char **av,
 	return err;
 }
 
+static int do_reupdate(int ac, const char **av,
+		       const char *prefix, int prefix_length)
+{
+	/* Read HEAD and run update-index on paths that are
+	 * merged and already different between index and HEAD.
+	 */
+	int pos;
+	int has_head = 1;
+
+	if (read_ref(git_path("HEAD"), head_sha1))
+		/* If there is no HEAD, that means it is an initial
+		 * commit.  Update everything in the index.
+		 */
+		has_head = 0;
+ redo:
+	for (pos = 0; pos < active_nr; pos++) {
+		struct cache_entry *ce = active_cache[pos];
+		struct cache_entry *old = NULL;
+		int save_nr;
+		if (ce_stage(ce))
+			continue;
+		if (has_head)
+			old = read_one_ent(NULL, head_sha1,
+					   ce->name, ce_namelen(ce), 0);
+		if (old && ce->ce_mode == old->ce_mode &&
+		    !memcmp(ce->sha1, old->sha1, 20)) {
+			free(old);
+			continue; /* unchanged */
+		}
+		/* Be careful.  The working tree may not have the
+		 * path anymore, in which case, under 'allow_remove',
+		 * or worse yet 'allow_replace', active_nr may decrease.
+		 */
+		save_nr = active_nr;
+		update_one(ce->name + prefix_length, prefix, prefix_length);
+		if (save_nr != active_nr)
+			goto redo;
+	}
+	return 0;
+}
+
 int main(int argc, const char **argv)
 {
 	int i, newfd, entries, has_errors = 0, line_termination = '\n';
@@ -717,6 +760,13 @@ int main(int argc, const char **argv)
 					active_cache_changed = 0;
 				goto finish;
 			}
+			if (!strcmp(path, "--again")) {
+				has_errors = do_reupdate(argc - i, argv + i,
+							 prefix, prefix_length);
+				if (has_errors)
+					active_cache_changed = 0;
+				goto finish;
+			}
 			if (!strcmp(path, "--ignore-missing")) {
 				not_new = 1;
 				continue;

From 22293b9c41778bb60f3b07355e1b8e421a503702 Mon Sep 17 00:00:00 2001
From: Junio C Hamano <junkio@cox.net>
Date: Fri, 5 May 2006 23:09:05 -0700
Subject: [PATCH 2/3] update-index --again: take optional pathspecs

When pathspecs are given, update-index --again further limits
the set of paths to be updated to those that match them.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 t/t2101-update-index-reupdate.sh | 11 ++++++++++-
 update-index.c                   |  4 +++-
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/t/t2101-update-index-reupdate.sh b/t/t2101-update-index-reupdate.sh
index 5c505c6ac8..77aed8d800 100755
--- a/t/t2101-update-index-reupdate.sh
+++ b/t/t2101-update-index-reupdate.sh
@@ -69,5 +69,14 @@ test_expect_success 'update-index --update from subdir' \
 100644 0f1ae1422c2bf43f117d3dbd715c988a9ed2103f 0	file2
 EOF'
 
-test_done
+test_expect_success 'update-index --update with pathspec' \
+	'echo very happy >file2 &&
+	cat file2 >dir1/file3 &&
+	git-update-index --again dir1/ &&
+	git-ls-files -s >current &&
+	cmp current - <<\EOF
+100644 594fb5bb1759d90998e2bf2a38261ae8e243c760 0	dir1/file3
+100644 0f1ae1422c2bf43f117d3dbd715c988a9ed2103f 0	file2
+EOF'
 
+test_done
diff --git a/update-index.c b/update-index.c
index 5496d68ae9..184b2526da 100644
--- a/update-index.c
+++ b/update-index.c
@@ -610,6 +610,7 @@ static int do_reupdate(int ac, const char **av,
 	 */
 	int pos;
 	int has_head = 1;
+	char **pathspec = get_pathspec(prefix, av + 1);
 
 	if (read_ref(git_path("HEAD"), head_sha1))
 		/* If there is no HEAD, that means it is an initial
@@ -621,7 +622,8 @@ static int do_reupdate(int ac, const char **av,
 		struct cache_entry *ce = active_cache[pos];
 		struct cache_entry *old = NULL;
 		int save_nr;
-		if (ce_stage(ce))
+
+		if (ce_stage(ce) || !ce_path_match(ce, pathspec))
 			continue;
 		if (has_head)
 			old = read_one_ent(NULL, head_sha1,

From 0cc9e70c4c9adb3e79b4a492f2f989c611d85966 Mon Sep 17 00:00:00 2001
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Date: Sun, 7 May 2006 00:02:53 +0200
Subject: [PATCH 3/3] Fix users of prefix_path() to free() only when necessary

Unfortunately, prefix_path() sometimes returns a newly xmalloc()ed buffer,
and in other cases it returns a substring!

For example, when calling

	git update-index ./hello.txt

prefix_path() returns "hello.txt", but does not allocate a new buffer. The
original code only checked if the result of prefix_path() was different from
what was passed in, and thusly trigger a segmentation fault.

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 checkout-index.c | 4 ++--
 update-index.c   | 8 ++++----
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/checkout-index.c b/checkout-index.c
index 0b9cabc61c..cc3a745c14 100644
--- a/checkout-index.c
+++ b/checkout-index.c
@@ -277,7 +277,7 @@ int main(int argc, char **argv)
 			die("git-checkout-index: don't mix '--stdin' and explicit filenames");
 		p = prefix_path(prefix, prefix_length, arg);
 		checkout_file(p);
-		if (p != arg)
+		if (p < arg || p > arg + strlen(arg))
 			free((char*)p);
 	}
 
@@ -299,7 +299,7 @@ int main(int argc, char **argv)
 				path_name = buf.buf;
 			p = prefix_path(prefix, prefix_length, path_name);
 			checkout_file(p);
-			if (p != path_name)
+			if (p < path_name || p > path_name + strlen(path_name))
 				free((char *)p);
 			if (path_name != buf.buf)
 				free(path_name);
diff --git a/update-index.c b/update-index.c
index 184b2526da..3d7e02db2c 100644
--- a/update-index.c
+++ b/update-index.c
@@ -382,7 +382,7 @@ static void update_one(const char *path, const char *prefix, int prefix_length)
 		die("Unable to process file %s", path);
 	report("add '%s'", path);
  free_return:
-	if (p != path)
+	if (p < path || p > path + strlen(path))
 		free((char*)p);
 }
 
@@ -596,7 +596,7 @@ static int do_unresolve(int ac, const char **av,
 		const char *arg = av[i];
 		const char *p = prefix_path(prefix, prefix_length, arg);
 		err |= unresolve_one(p);
-		if (p != arg)
+		if (p < arg || p > arg + strlen(arg))
 			free((char*)p);
 	}
 	return err;
@@ -610,7 +610,7 @@ static int do_reupdate(int ac, const char **av,
 	 */
 	int pos;
 	int has_head = 1;
-	char **pathspec = get_pathspec(prefix, av + 1);
+	const char **pathspec = get_pathspec(prefix, av + 1);
 
 	if (read_ref(git_path("HEAD"), head_sha1))
 		/* If there is no HEAD, that means it is an initial
@@ -802,7 +802,7 @@ int main(int argc, const char **argv)
 			update_one(p, NULL, 0);
 			if (set_executable_bit)
 				chmod_path(set_executable_bit, p);
-			if (p != path_name)
+			if (p < path_name || p > path_name + strlen(path_name))
 				free((char*) p);
 			if (path_name != buf.buf)
 				free(path_name);