git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 00/10] teach replace objects to sha1_object_info_extended()
@ 2013-12-07 16:20 Christian Couder
  2013-12-07 16:20 ` [PATCH v2 01/10] Rename READ_SHA1_FILE_REPLACE flag to LOOKUP_REPLACE_OBJECT Christian Couder
                   ` (9 more replies)
  0 siblings, 10 replies; 15+ messages in thread
From: Christian Couder @ 2013-12-07 16:20 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King, Joey Hess

Here is version 2 of a patch series to improve the way
sha1_object_info_extended() behaves when it is passed a
replaced object. The idea is to add a flags argument to it
in the same way as what has been done to read_sha1_file().

This patch series was inspired by a sub thread in this discussion:

http://thread.gmane.org/gmane.comp.version-control.git/238118

Patches 1/10, 2/10 and 3/10 are cleanups, among them only 1/10
is new.
Patches 4/10, 5/10 and 6/10 were also in the previous version.

Patches 7/10, 8/10, 9/10 and 10/10 are new. They add a new
--format option to list replace refs. 'short' (which is the
default), 'medium' and 'full' formats are supported. This could
be considered another patch series, but it is also related,
because it uses sha1_object_info() and it fixes its use in
builtin/replace.c by unsetting the global variable
read_replace_refs in cmd_replace().

Christian Couder (10):
  Rename READ_SHA1_FILE_REPLACE flag to LOOKUP_REPLACE_OBJECT
  replace_object: don't check read_replace_refs twice
  Introduce lookup_replace_object_extended() to pass flags
  Add an "unsigned flags" parameter to sha1_object_info_extended()
  t6050: show that git cat-file --batch fails with replace objects
  sha1_file: perform object replacement in sha1_object_info_extended()
  builtin/replace: teach listing using short, medium or full formats
  t6050: add tests for listing with --format
  builtin/replace: unset read_replace_refs
  Documentation/git-replace: describe --format option

 Documentation/git-replace.txt | 19 ++++++++++++-
 builtin/cat-file.c            |  2 +-
 builtin/replace.c             | 63 ++++++++++++++++++++++++++++++++++++++-----
 cache.h                       | 12 ++++++---
 replace_object.c              |  3 ---
 sha1_file.c                   | 20 +++++++-------
 streaming.c                   |  2 +-
 t/t6050-replace.sh            | 32 ++++++++++++++++++++++
 8 files changed, 127 insertions(+), 26 deletions(-)

-- 
1.8.5.1.102.g090758b

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v2 01/10] Rename READ_SHA1_FILE_REPLACE flag to LOOKUP_REPLACE_OBJECT
  2013-12-07 16:20 [PATCH v2 00/10] teach replace objects to sha1_object_info_extended() Christian Couder
@ 2013-12-07 16:20 ` Christian Couder
  2013-12-07 16:20 ` [PATCH v2 02/10] replace_object: don't check read_replace_refs twice Christian Couder
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 15+ messages in thread
From: Christian Couder @ 2013-12-07 16:20 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King, Joey Hess

The READ_SHA1_FILE_REPLACE flag is more related to using the
lookup_replace_object() function rather than the
read_sha1_file() function.

We also need such a flag to be used with sha1_object_info()
instead of read_sha1_file().

The name LOOKUP_REPLACE_OBJECT is therefore better for this
flag.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 cache.h     | 4 ++--
 sha1_file.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/cache.h b/cache.h
index ce377e1..873a6b5 100644
--- a/cache.h
+++ b/cache.h
@@ -760,11 +760,11 @@ int daemon_avoid_alias(const char *path);
 int offset_1st_component(const char *path);
 
 /* object replacement */
-#define READ_SHA1_FILE_REPLACE 1
+#define LOOKUP_REPLACE_OBJECT 1
 extern void *read_sha1_file_extended(const unsigned char *sha1, enum object_type *type, unsigned long *size, unsigned flag);
 static inline void *read_sha1_file(const unsigned char *sha1, enum object_type *type, unsigned long *size)
 {
-	return read_sha1_file_extended(sha1, type, size, READ_SHA1_FILE_REPLACE);
+	return read_sha1_file_extended(sha1, type, size, LOOKUP_REPLACE_OBJECT);
 }
 extern const unsigned char *do_lookup_replace_object(const unsigned char *sha1);
 static inline const unsigned char *lookup_replace_object(const unsigned char *sha1)
diff --git a/sha1_file.c b/sha1_file.c
index daacc0c..76e9f32 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -2591,7 +2591,7 @@ void *read_sha1_file_extended(const unsigned char *sha1,
 	void *data;
 	char *path;
 	const struct packed_git *p;
-	const unsigned char *repl = (flag & READ_SHA1_FILE_REPLACE)
+	const unsigned char *repl = (flag & LOOKUP_REPLACE_OBJECT)
 		? lookup_replace_object(sha1) : sha1;
 
 	errno = 0;
-- 
1.8.5.1.102.g090758b

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 02/10] replace_object: don't check read_replace_refs twice
  2013-12-07 16:20 [PATCH v2 00/10] teach replace objects to sha1_object_info_extended() Christian Couder
  2013-12-07 16:20 ` [PATCH v2 01/10] Rename READ_SHA1_FILE_REPLACE flag to LOOKUP_REPLACE_OBJECT Christian Couder
@ 2013-12-07 16:20 ` Christian Couder
  2013-12-07 16:20 ` [PATCH v2 03/10] Introduce lookup_replace_object_extended() to pass flags Christian Couder
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 15+ messages in thread
From: Christian Couder @ 2013-12-07 16:20 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King, Joey Hess

Since e1111cef (inline lookup_replace_object() calls,
May 15 2011) the read_replace_refs global variable is
checked twice, once in lookup_replace_object() and
once again in do_lookup_replace_object().

As do_lookup_replace_object() is called only from
lookup_replace_object(), we can remove the check in
do_lookup_replace_object().

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 replace_object.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/replace_object.c b/replace_object.c
index d0b1548..cdcaf8c 100644
--- a/replace_object.c
+++ b/replace_object.c
@@ -97,9 +97,6 @@ const unsigned char *do_lookup_replace_object(const unsigned char *sha1)
 	int pos, depth = MAXREPLACEDEPTH;
 	const unsigned char *cur = sha1;
 
-	if (!read_replace_refs)
-		return sha1;
-
 	prepare_replace_object();
 
 	/* Try to recursively replace the object */
-- 
1.8.5.1.102.g090758b

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 03/10] Introduce lookup_replace_object_extended() to pass flags
  2013-12-07 16:20 [PATCH v2 00/10] teach replace objects to sha1_object_info_extended() Christian Couder
  2013-12-07 16:20 ` [PATCH v2 01/10] Rename READ_SHA1_FILE_REPLACE flag to LOOKUP_REPLACE_OBJECT Christian Couder
  2013-12-07 16:20 ` [PATCH v2 02/10] replace_object: don't check read_replace_refs twice Christian Couder
@ 2013-12-07 16:20 ` Christian Couder
  2013-12-07 16:20 ` [PATCH v2 04/10] Add an "unsigned flags" parameter to sha1_object_info_extended() Christian Couder
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 15+ messages in thread
From: Christian Couder @ 2013-12-07 16:20 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King, Joey Hess

Currently, there is only one caller to lookup_replace_object()
that can benefit from passing it some flags, but we expect
that there could be more.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 cache.h     | 6 ++++++
 sha1_file.c | 3 +--
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/cache.h b/cache.h
index 873a6b5..563f85f 100644
--- a/cache.h
+++ b/cache.h
@@ -773,6 +773,12 @@ static inline const unsigned char *lookup_replace_object(const unsigned char *sh
 		return sha1;
 	return do_lookup_replace_object(sha1);
 }
+static inline const unsigned char *lookup_replace_object_extended(const unsigned char *sha1, unsigned flag)
+{
+	if (! (flag & LOOKUP_REPLACE_OBJECT))
+		return sha1;
+	return lookup_replace_object(sha1);
+}
 
 /* Read and unpack a sha1 file into memory, write memory to a sha1 file */
 extern int sha1_object_info(const unsigned char *, unsigned long *);
diff --git a/sha1_file.c b/sha1_file.c
index 76e9f32..4fb2f17 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -2591,8 +2591,7 @@ void *read_sha1_file_extended(const unsigned char *sha1,
 	void *data;
 	char *path;
 	const struct packed_git *p;
-	const unsigned char *repl = (flag & LOOKUP_REPLACE_OBJECT)
-		? lookup_replace_object(sha1) : sha1;
+	const unsigned char *repl = lookup_replace_object_extended(sha1, flag);
 
 	errno = 0;
 	data = read_object(repl, type, size);
-- 
1.8.5.1.102.g090758b

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 04/10] Add an "unsigned flags" parameter to sha1_object_info_extended()
  2013-12-07 16:20 [PATCH v2 00/10] teach replace objects to sha1_object_info_extended() Christian Couder
                   ` (2 preceding siblings ...)
  2013-12-07 16:20 ` [PATCH v2 03/10] Introduce lookup_replace_object_extended() to pass flags Christian Couder
@ 2013-12-07 16:20 ` Christian Couder
  2013-12-07 16:21 ` [PATCH v2 05/10] t6050: show that git cat-file --batch fails with replace objects Christian Couder
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 15+ messages in thread
From: Christian Couder @ 2013-12-07 16:20 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King, Joey Hess

This parameter is not used yet, but it will be used to tell
sha1_object_info_extended() if it should perform object
replacement or not.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/cat-file.c | 2 +-
 cache.h            | 2 +-
 sha1_file.c        | 6 +++---
 streaming.c        | 2 +-
 4 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index b2ca775..b15c064 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -238,7 +238,7 @@ static int batch_one_object(const char *obj_name, struct batch_options *opt,
 		return 0;
 	}
 
-	if (sha1_object_info_extended(data->sha1, &data->info) < 0) {
+	if (sha1_object_info_extended(data->sha1, &data->info, LOOKUP_REPLACE_OBJECT) < 0) {
 		printf("%s missing\n", obj_name);
 		fflush(stdout);
 		return 0;
diff --git a/cache.h b/cache.h
index 563f85f..701e42c 100644
--- a/cache.h
+++ b/cache.h
@@ -1104,7 +1104,7 @@ struct object_info {
 		} packed;
 	} u;
 };
-extern int sha1_object_info_extended(const unsigned char *, struct object_info *);
+extern int sha1_object_info_extended(const unsigned char *, struct object_info *, unsigned flags);
 
 /* Dumb servers support */
 extern int update_server_info(int);
diff --git a/sha1_file.c b/sha1_file.c
index 4fb2f17..482037e 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -2443,7 +2443,7 @@ static int sha1_loose_object_info(const unsigned char *sha1,
 	return 0;
 }
 
-int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi)
+int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi, unsigned flags)
 {
 	struct cached_object *co;
 	struct pack_entry e;
@@ -2477,7 +2477,7 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi)
 	rtype = packed_object_info(e.p, e.offset, oi);
 	if (rtype < 0) {
 		mark_bad_packed_object(e.p, sha1);
-		return sha1_object_info_extended(sha1, oi);
+		return sha1_object_info_extended(sha1, oi, 0);
 	} else if (in_delta_base_cache(e.p, e.offset)) {
 		oi->whence = OI_DBCACHED;
 	} else {
@@ -2499,7 +2499,7 @@ int sha1_object_info(const unsigned char *sha1, unsigned long *sizep)
 
 	oi.typep = &type;
 	oi.sizep = sizep;
-	if (sha1_object_info_extended(sha1, &oi) < 0)
+	if (sha1_object_info_extended(sha1, &oi, LOOKUP_REPLACE_OBJECT) < 0)
 		return -1;
 	return type;
 }
diff --git a/streaming.c b/streaming.c
index debe904..9659f18 100644
--- a/streaming.c
+++ b/streaming.c
@@ -113,7 +113,7 @@ static enum input_source istream_source(const unsigned char *sha1,
 
 	oi->typep = type;
 	oi->sizep = &size;
-	status = sha1_object_info_extended(sha1, oi);
+	status = sha1_object_info_extended(sha1, oi, 0);
 	if (status < 0)
 		return stream_error;
 
-- 
1.8.5.1.102.g090758b

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 05/10] t6050: show that git cat-file --batch fails with replace objects
  2013-12-07 16:20 [PATCH v2 00/10] teach replace objects to sha1_object_info_extended() Christian Couder
                   ` (3 preceding siblings ...)
  2013-12-07 16:20 ` [PATCH v2 04/10] Add an "unsigned flags" parameter to sha1_object_info_extended() Christian Couder
@ 2013-12-07 16:21 ` Christian Couder
  2013-12-07 16:21 ` [PATCH v2 06/10] sha1_file: perform object replacement in sha1_object_info_extended() Christian Couder
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 15+ messages in thread
From: Christian Couder @ 2013-12-07 16:21 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King, Joey Hess

When --batch is passed to git cat-file, the sha1_object_info_extended()
function is used to get information about the objects passed to
git cat-file.

Unfortunately sha1_object_info_extended() doesn't take care of
object replacement properly, so it will often fail with a
message like this:

$ echo a3fb2e1845a1aaf129b7975048973414dc172173 | git cat-file --batch
a3fb2e1845a1aaf129b7975048973414dc172173 commit 231
fatal: object a3fb2e1845a1aaf129b7975048973414dc172173 change size!?

The goal of this patch is to show this breakage.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 t/t6050-replace.sh | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh
index 7d47984..b90dbdc 100755
--- a/t/t6050-replace.sh
+++ b/t/t6050-replace.sh
@@ -276,6 +276,11 @@ test_expect_success '-f option bypasses the type check' '
 	git replace -f HEAD^ $BLOB
 '
 
+test_expect_failure 'git cat-file --batch works on replace objects' '
+	git replace | grep $PARA3 &&
+	echo $PARA3 | git cat-file --batch
+'
+
 test_expect_success 'replace ref cleanup' '
 	test -n "$(git replace)" &&
 	git replace -d $(git replace) &&
-- 
1.8.5.1.102.g090758b

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 06/10] sha1_file: perform object replacement in sha1_object_info_extended()
  2013-12-07 16:20 [PATCH v2 00/10] teach replace objects to sha1_object_info_extended() Christian Couder
                   ` (4 preceding siblings ...)
  2013-12-07 16:21 ` [PATCH v2 05/10] t6050: show that git cat-file --batch fails with replace objects Christian Couder
@ 2013-12-07 16:21 ` Christian Couder
  2013-12-07 16:21 ` [PATCH v2 07/10] builtin/replace: teach listing using short, medium or full formats Christian Couder
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 15+ messages in thread
From: Christian Couder @ 2013-12-07 16:21 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King, Joey Hess

sha1_object_info_extended() should perform object replacement
if it is needed.

The simplest way to do that is to make it call
lookup_replace_object_extended().

And now its "unsigned flags" parameter is used as it is passed
to lookup_replace_object_extended().

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 sha1_file.c        | 13 +++++++------
 t/t6050-replace.sh |  2 +-
 2 files changed, 8 insertions(+), 7 deletions(-)

diff --git a/sha1_file.c b/sha1_file.c
index 482037e..ee224e4 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -2448,8 +2448,9 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
 	struct cached_object *co;
 	struct pack_entry e;
 	int rtype;
+	const unsigned char *real = lookup_replace_object_extended(sha1, flags);
 
-	co = find_cached_object(sha1);
+	co = find_cached_object(real);
 	if (co) {
 		if (oi->typep)
 			*(oi->typep) = co->type;
@@ -2461,23 +2462,23 @@ int sha1_object_info_extended(const unsigned char *sha1, struct object_info *oi,
 		return 0;
 	}
 
-	if (!find_pack_entry(sha1, &e)) {
+	if (!find_pack_entry(real, &e)) {
 		/* Most likely it's a loose object. */
-		if (!sha1_loose_object_info(sha1, oi)) {
+		if (!sha1_loose_object_info(real, oi)) {
 			oi->whence = OI_LOOSE;
 			return 0;
 		}
 
 		/* Not a loose object; someone else may have just packed it. */
 		reprepare_packed_git();
-		if (!find_pack_entry(sha1, &e))
+		if (!find_pack_entry(real, &e))
 			return -1;
 	}
 
 	rtype = packed_object_info(e.p, e.offset, oi);
 	if (rtype < 0) {
-		mark_bad_packed_object(e.p, sha1);
-		return sha1_object_info_extended(sha1, oi, 0);
+		mark_bad_packed_object(e.p, real);
+		return sha1_object_info_extended(real, oi, 0);
 	} else if (in_delta_base_cache(e.p, e.offset)) {
 		oi->whence = OI_DBCACHED;
 	} else {
diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh
index b90dbdc..bb785ec 100755
--- a/t/t6050-replace.sh
+++ b/t/t6050-replace.sh
@@ -276,7 +276,7 @@ test_expect_success '-f option bypasses the type check' '
 	git replace -f HEAD^ $BLOB
 '
 
-test_expect_failure 'git cat-file --batch works on replace objects' '
+test_expect_success 'git cat-file --batch works on replace objects' '
 	git replace | grep $PARA3 &&
 	echo $PARA3 | git cat-file --batch
 '
-- 
1.8.5.1.102.g090758b

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 07/10] builtin/replace: teach listing using short, medium or full formats
  2013-12-07 16:20 [PATCH v2 00/10] teach replace objects to sha1_object_info_extended() Christian Couder
                   ` (5 preceding siblings ...)
  2013-12-07 16:21 ` [PATCH v2 06/10] sha1_file: perform object replacement in sha1_object_info_extended() Christian Couder
@ 2013-12-07 16:21 ` Christian Couder
  2013-12-07 16:21 ` [PATCH v2 08/10] t6050: add tests for listing with --format Christian Couder
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 15+ messages in thread
From: Christian Couder @ 2013-12-07 16:21 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King, Joey Hess

By default when listing replace refs, only the sha1 of the
replaced objects are shown.

In many cases, it is much nicer to be able to list all the
sha1 of the replaced objects along with the sha1 of the
replacment objects.

And in other cases it might be interesting to also show the
types of the replaced and replacement objects.

This patch introduce a new --format=<fmt> option where
<fmt> can be any of the following:

	'short': this is the same as when no --format
		option is used, that is only the sha1 of
		the replaced objects are shown
	'medium': this also lists the sha1 of the
		replacement objects
	'full': this shows the sha1 and the type of both
		the replaced and the replacement objects

Some documentation and some tests will follow.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/replace.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 54 insertions(+), 7 deletions(-)

diff --git a/builtin/replace.c b/builtin/replace.c
index b1bd3ef..9f3619a 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -16,27 +16,65 @@
 static const char * const git_replace_usage[] = {
 	N_("git replace [-f] <object> <replacement>"),
 	N_("git replace -d <object>..."),
-	N_("git replace -l [<pattern>]"),
+	N_("git replace [--format=<format>] [-l [<pattern>]]"),
 	NULL
 };
 
+enum repl_fmt { SHORT, MEDIUM, FULL };
+
+struct show_data {
+	const char *pattern;
+	enum repl_fmt fmt;
+};
+
 static int show_reference(const char *refname, const unsigned char *sha1,
 			  int flag, void *cb_data)
 {
-	const char *pattern = cb_data;
+	struct show_data *data = cb_data;
+
+	if (!fnmatch(data->pattern, refname, 0)) {
+		if (data->fmt == SHORT)
+			printf("%s\n", refname);
+		else if (data->fmt == MEDIUM)
+			printf("%s -> %s\n", refname, sha1_to_hex(sha1));
+		else { /* data->fmt == FULL */
+			unsigned char object[20];
+			enum object_type obj_type, repl_type;
 
-	if (!fnmatch(pattern, refname, 0))
-		printf("%s\n", refname);
+			if (get_sha1(refname, object))
+				return error("Failed to resolve '%s' as a valid ref.", refname);
+
+			obj_type = sha1_object_info(object, NULL);
+			repl_type = sha1_object_info(sha1, NULL);
+
+			printf("%s (%s) -> %s (%s)\n", refname, typename(obj_type),
+			       sha1_to_hex(sha1), typename(repl_type));
+		}
+	}
 
 	return 0;
 }
 
-static int list_replace_refs(const char *pattern)
+static int list_replace_refs(const char *pattern, const char *format)
 {
+	struct show_data data;
+
 	if (pattern == NULL)
 		pattern = "*";
+	data.pattern = pattern;
+
+	if (format == NULL || *format == '\0' || !strcmp(format, "short"))
+		data.fmt = SHORT;
+	else if (!strcmp(format, "medium"))
+		data.fmt = MEDIUM;
+	else if (!strcmp(format, "full"))
+		data.fmt = FULL;
+	else
+		die("invalid replace format '%s'\n"
+		    "valid formats are 'short', 'medium' and 'full'\n",
+		    format);
 
-	for_each_replace_ref(show_reference, (void *) pattern);
+	for_each_replace_ref(show_reference, (void *) &data);
 
 	return 0;
 }
@@ -127,10 +165,12 @@ static int replace_object(const char *object_ref, const char *replace_ref,
 int cmd_replace(int argc, const char **argv, const char *prefix)
 {
 	int list = 0, delete = 0, force = 0;
+	const char *format = NULL;
 	struct option options[] = {
 		OPT_BOOL('l', "list", &list, N_("list replace refs")),
 		OPT_BOOL('d', "delete", &delete, N_("delete replace refs")),
 		OPT_BOOL('f', "force", &force, N_("replace the ref if it exists")),
+		OPT_STRING(0, "format", &format, N_("format"), N_("use this format")),
 		OPT_END()
 	};
 
@@ -140,6 +180,10 @@ int cmd_replace(int argc, const char **argv, const char *prefix)
 		usage_msg_opt("-l and -d cannot be used together",
 			      git_replace_usage, options);
 
+	if (format && delete)
+		usage_msg_opt("--format and -d cannot be used together",
+			      git_replace_usage, options);
+
 	if (force && (list || delete))
 		usage_msg_opt("-f cannot be used with -d or -l",
 			      git_replace_usage, options);
@@ -157,6 +201,9 @@ int cmd_replace(int argc, const char **argv, const char *prefix)
 		if (argc != 2)
 			usage_msg_opt("bad number of arguments",
 				      git_replace_usage, options);
+		if (format)
+			usage_msg_opt("--format cannot be used when not listing",
+				      git_replace_usage, options);
 		return replace_object(argv[0], argv[1], force);
 	}
 
@@ -168,5 +215,5 @@ int cmd_replace(int argc, const char **argv, const char *prefix)
 		usage_msg_opt("-f needs some arguments",
 			      git_replace_usage, options);
 
-	return list_replace_refs(argv[0]);
+	return list_replace_refs(argv[0], format);
 }
-- 
1.8.5.1.102.g090758b

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 08/10] t6050: add tests for listing with --format
  2013-12-07 16:20 [PATCH v2 00/10] teach replace objects to sha1_object_info_extended() Christian Couder
                   ` (6 preceding siblings ...)
  2013-12-07 16:21 ` [PATCH v2 07/10] builtin/replace: teach listing using short, medium or full formats Christian Couder
@ 2013-12-07 16:21 ` Christian Couder
  2013-12-09  3:19   ` Eric Sunshine
  2013-12-07 16:21 ` [PATCH v2 09/10] builtin/replace: unset read_replace_refs Christian Couder
  2013-12-07 16:21 ` [PATCH v2 10/10] Documentation/git-replace: describe --format option Christian Couder
  9 siblings, 1 reply; 15+ messages in thread
From: Christian Couder @ 2013-12-07 16:21 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King, Joey Hess

This patch adds tests for "git replace -l --format=<fmt>".
Only tests when <fmt> is 'medium' and 'full' are needed
because 'short' is the same as with no --format option.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 t/t6050-replace.sh | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh
index bb785ec..3627b4c 100755
--- a/t/t6050-replace.sh
+++ b/t/t6050-replace.sh
@@ -281,6 +281,33 @@ test_expect_success 'git cat-file --batch works on replace objects' '
 	echo $PARA3 | git cat-file --batch
 '
 
+test_expect_success 'test --format medium' '
+	H1=$(git --no-replace-objects rev-parse HEAD~1) &&
+	HT=$(git --no-replace-objects rev-parse HEAD^{tree}) &&
+	MYTAG=$(git --no-replace-objects rev-parse mytag) &&
+	{
+		echo "$H1 -> $BLOB" &&
+		echo "$BLOB -> $REPLACED" &&
+		echo "$HT -> $H1" &&
+		echo "$PARA3 -> $S" &&
+		echo "$MYTAG -> $HASH1"
+	} | sort >expected &&
+	git replace -l --format medium | sort > actual &&
+	test_cmp expected actual
+'
+
+test_expect_failure 'test --format full' '
+	{
+		echo "$H1 (commit) -> $BLOB (blob)" &&
+		echo "$BLOB (blob) -> $REPLACED (blob)" &&
+		echo "$HT (tree) -> $H1 (commit)" &&
+		echo "$PARA3 (commit) -> $S (commit)" &&
+		echo "$MYTAG (tag) -> $HASH1 (commit)"
+	} | sort >expected &&
+	git replace --format=full | sort > actual &&
+	test_cmp expected actual
+'
+
 test_expect_success 'replace ref cleanup' '
 	test -n "$(git replace)" &&
 	git replace -d $(git replace) &&
-- 
1.8.5.1.102.g090758b

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 09/10] builtin/replace: unset read_replace_refs
  2013-12-07 16:20 [PATCH v2 00/10] teach replace objects to sha1_object_info_extended() Christian Couder
                   ` (7 preceding siblings ...)
  2013-12-07 16:21 ` [PATCH v2 08/10] t6050: add tests for listing with --format Christian Couder
@ 2013-12-07 16:21 ` Christian Couder
  2013-12-07 19:02   ` Jakub Narebski
  2013-12-07 16:21 ` [PATCH v2 10/10] Documentation/git-replace: describe --format option Christian Couder
  9 siblings, 1 reply; 15+ messages in thread
From: Christian Couder @ 2013-12-07 16:21 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King, Joey Hess

When checking to see if some objects are of the same type
and when displaying the type of objects, git replace uses
the sha1_object_info() function.

Unfortunately this function by default respects replace
refs, so instead of the type of a replaced object, it
gives the type of the replacement object which might
be different.

To fix this bug, and because git replace should work at a
level before replacement takes place, let's unset the
read_replace_refs global variable at the beginning of
cmd_replace().

Suggested-by: Jeff King <peff@peff.net>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/replace.c  | 2 ++
 t/t6050-replace.sh | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/builtin/replace.c b/builtin/replace.c
index 9f3619a..1672870 100644
--- a/builtin/replace.c
+++ b/builtin/replace.c
@@ -174,6 +174,8 @@ int cmd_replace(int argc, const char **argv, const char *prefix)
 		OPT_END()
 	};
 
+	read_replace_refs = 0;
+
 	argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
 
 	if (list && delete)
diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh
index 3627b4c..f15b805 100755
--- a/t/t6050-replace.sh
+++ b/t/t6050-replace.sh
@@ -296,7 +296,7 @@ test_expect_success 'test --format medium' '
 	test_cmp expected actual
 '
 
-test_expect_failure 'test --format full' '
+test_expect_success 'test --format full' '
 	{
 		echo "$H1 (commit) -> $BLOB (blob)" &&
 		echo "$BLOB (blob) -> $REPLACED (blob)" &&
-- 
1.8.5.1.102.g090758b

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v2 10/10] Documentation/git-replace: describe --format option
  2013-12-07 16:20 [PATCH v2 00/10] teach replace objects to sha1_object_info_extended() Christian Couder
                   ` (8 preceding siblings ...)
  2013-12-07 16:21 ` [PATCH v2 09/10] builtin/replace: unset read_replace_refs Christian Couder
@ 2013-12-07 16:21 ` Christian Couder
  9 siblings, 0 replies; 15+ messages in thread
From: Christian Couder @ 2013-12-07 16:21 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Jeff King, Joey Hess

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 Documentation/git-replace.txt | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/Documentation/git-replace.txt b/Documentation/git-replace.txt
index f373ab4..7a07828 100644
--- a/Documentation/git-replace.txt
+++ b/Documentation/git-replace.txt
@@ -10,7 +10,7 @@ SYNOPSIS
 [verse]
 'git replace' [-f] <object> <replacement>
 'git replace' -d <object>...
-'git replace' -l [<pattern>]
+'git replace' [--format=<format>] [-l [<pattern>]]
 
 DESCRIPTION
 -----------
@@ -70,6 +70,23 @@ OPTIONS
 	Typing "git replace" without arguments, also lists all replace
 	refs.
 
+--format=<format>::
+	When listing, use the specified <format>, which can be one of
+	'short', 'medium' and 'full'. When omitted, the format
+	defaults to 'short'.
+
+FORMATS
+-------
+
+The following format are available:
+
+* 'short':
+	<replaced sha1>
+* 'medium':
+	<replaced sha1> -> <replacement sha1>
+* 'full'
+	<replaced sha1> (<replaced type>) -> <replacement sha1> (<replacement type>)
+
 CREATING REPLACEMENT OBJECTS
 ----------------------------
 
-- 
1.8.5.1.102.g090758b

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 09/10] builtin/replace: unset read_replace_refs
  2013-12-07 16:21 ` [PATCH v2 09/10] builtin/replace: unset read_replace_refs Christian Couder
@ 2013-12-07 19:02   ` Jakub Narebski
  2013-12-07 19:37     ` Christian Couder
  0 siblings, 1 reply; 15+ messages in thread
From: Jakub Narebski @ 2013-12-07 19:02 UTC (permalink / raw)
  To: git

Christian Couder <chriscool <at> tuxfamily.org> writes:

> 
> When checking to see if some objects are of the same type
> and when displaying the type of objects, git replace uses
> the sha1_object_info() function.
> 
> Unfortunately this function by default respects replace
> refs, so instead of the type of a replaced object, it
> gives the type of the replacement object which might
> be different.

Wasn't replace mechanism tightened, so that replacing object
must be the same type as replaced object?

Is there a situation where you might want replace object
with different type of object?

-- 
Jakub Narębski

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 09/10] builtin/replace: unset read_replace_refs
  2013-12-07 19:02   ` Jakub Narebski
@ 2013-12-07 19:37     ` Christian Couder
  0 siblings, 0 replies; 15+ messages in thread
From: Christian Couder @ 2013-12-07 19:37 UTC (permalink / raw)
  To: Jakub Narebski; +Cc: git

On Sat, Dec 7, 2013 at 8:02 PM, Jakub Narebski <jnareb@gmail.com> wrote:
>
> Wasn't replace mechanism tightened, so that replacing object
> must be the same type as replaced object?

Yes, but if --force is used or if the replace ref is created with git
update-ref, then it is not enforced.

> Is there a situation where you might want replace object
> with different type of object?

This was discussed:

http://thread.gmane.org/gmane.comp.version-control.git/233157/

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 08/10] t6050: add tests for listing with --format
  2013-12-07 16:21 ` [PATCH v2 08/10] t6050: add tests for listing with --format Christian Couder
@ 2013-12-09  3:19   ` Eric Sunshine
  2013-12-09  5:58     ` Christian Couder
  0 siblings, 1 reply; 15+ messages in thread
From: Eric Sunshine @ 2013-12-09  3:19 UTC (permalink / raw)
  To: Christian Couder; +Cc: Junio C Hamano, Git List, Jeff King, Joey Hess

On Sat, Dec 7, 2013 at 11:21 AM, Christian Couder
<chriscool@tuxfamily.org> wrote:
> This patch adds tests for "git replace -l --format=<fmt>".
> Only tests when <fmt> is 'medium' and 'full' are needed
> because 'short' is the same as with no --format option.

Nevertheless, don't you want to test that it behaves in the expected
manner when given --format=short and other legal or illegal
combinations?

  --format
  --format short
  --format bogus
  ...etc...

> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  t/t6050-replace.sh | 27 +++++++++++++++++++++++++++
>  1 file changed, 27 insertions(+)
>
> diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh
> index bb785ec..3627b4c 100755
> --- a/t/t6050-replace.sh
> +++ b/t/t6050-replace.sh
> @@ -281,6 +281,33 @@ test_expect_success 'git cat-file --batch works on replace objects' '
>         echo $PARA3 | git cat-file --batch
>  '
>
> +test_expect_success 'test --format medium' '
> +       H1=$(git --no-replace-objects rev-parse HEAD~1) &&
> +       HT=$(git --no-replace-objects rev-parse HEAD^{tree}) &&
> +       MYTAG=$(git --no-replace-objects rev-parse mytag) &&
> +       {
> +               echo "$H1 -> $BLOB" &&
> +               echo "$BLOB -> $REPLACED" &&
> +               echo "$HT -> $H1" &&
> +               echo "$PARA3 -> $S" &&
> +               echo "$MYTAG -> $HASH1"
> +       } | sort >expected &&
> +       git replace -l --format medium | sort > actual &&
> +       test_cmp expected actual
> +'
> +
> +test_expect_failure 'test --format full' '
> +       {
> +               echo "$H1 (commit) -> $BLOB (blob)" &&
> +               echo "$BLOB (blob) -> $REPLACED (blob)" &&
> +               echo "$HT (tree) -> $H1 (commit)" &&
> +               echo "$PARA3 (commit) -> $S (commit)" &&
> +               echo "$MYTAG (tag) -> $HASH1 (commit)"
> +       } | sort >expected &&
> +       git replace --format=full | sort > actual &&
> +       test_cmp expected actual
> +'
> +
>  test_expect_success 'replace ref cleanup' '
>         test -n "$(git replace)" &&
>         git replace -d $(git replace) &&
> --
> 1.8.5.1.102.g090758b
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2 08/10] t6050: add tests for listing with --format
  2013-12-09  3:19   ` Eric Sunshine
@ 2013-12-09  5:58     ` Christian Couder
  0 siblings, 0 replies; 15+ messages in thread
From: Christian Couder @ 2013-12-09  5:58 UTC (permalink / raw)
  To: sunshine; +Cc: gitster, git, peff, joey

From: Eric Sunshine <sunshine@sunshineco.com>
>
> On Sat, Dec 7, 2013 at 11:21 AM, Christian Couder
> <chriscool@tuxfamily.org> wrote:
>> This patch adds tests for "git replace -l --format=<fmt>".
>> Only tests when <fmt> is 'medium' and 'full' are needed
>> because 'short' is the same as with no --format option.
> 
> Nevertheless, don't you want to test that it behaves in the expected
> manner when given --format=short and other legal or illegal
> combinations?
> 
>   --format
>   --format short
>   --format bogus
>   ...etc...

Ok, I will add more tests.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2013-12-09  5:59 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-07 16:20 [PATCH v2 00/10] teach replace objects to sha1_object_info_extended() Christian Couder
2013-12-07 16:20 ` [PATCH v2 01/10] Rename READ_SHA1_FILE_REPLACE flag to LOOKUP_REPLACE_OBJECT Christian Couder
2013-12-07 16:20 ` [PATCH v2 02/10] replace_object: don't check read_replace_refs twice Christian Couder
2013-12-07 16:20 ` [PATCH v2 03/10] Introduce lookup_replace_object_extended() to pass flags Christian Couder
2013-12-07 16:20 ` [PATCH v2 04/10] Add an "unsigned flags" parameter to sha1_object_info_extended() Christian Couder
2013-12-07 16:21 ` [PATCH v2 05/10] t6050: show that git cat-file --batch fails with replace objects Christian Couder
2013-12-07 16:21 ` [PATCH v2 06/10] sha1_file: perform object replacement in sha1_object_info_extended() Christian Couder
2013-12-07 16:21 ` [PATCH v2 07/10] builtin/replace: teach listing using short, medium or full formats Christian Couder
2013-12-07 16:21 ` [PATCH v2 08/10] t6050: add tests for listing with --format Christian Couder
2013-12-09  3:19   ` Eric Sunshine
2013-12-09  5:58     ` Christian Couder
2013-12-07 16:21 ` [PATCH v2 09/10] builtin/replace: unset read_replace_refs Christian Couder
2013-12-07 19:02   ` Jakub Narebski
2013-12-07 19:37     ` Christian Couder
2013-12-07 16:21 ` [PATCH v2 10/10] Documentation/git-replace: describe --format option Christian Couder

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).