git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Matthieu Moy <Matthieu.Moy@grenoble-inp.fr>
Cc: clement.poulain@ensimag.imag.fr, git@vger.kernel.org,
	diane.gasselin@ensimag.imag.fr, axel.bonnet@ensimag.imag.fr
Subject: Re: Best way to apply textconv to a working tree file
Date: Tue, 1 Jun 2010 15:50:36 -0400	[thread overview]
Message-ID: <20100601195036.GA18220@sigill.intra.peff.net> (raw)
In-Reply-To: <vpqtypmaceb.fsf@bauges.imag.fr>

On Tue, Jun 01, 2010 at 07:30:36PM +0200, Matthieu Moy wrote:

> > It would be nice if there was some way in the get_sha1* functions to
> > save some context, like tree context and filename. This would be helpful
> > for something like "git show HEAD:foo.txt", which probably should be
> > respecting autocrlf and smudge/clean filters.
> 
> Yup. The code to do the parsing is already there, it "just" needs to
> be made available through a clean API.

I was thinking of something like the patch below, which applies
clean/smudge filters to the output of "cat-file -p".

It keeps a global context for the last sha1 looked up. Probably
get_sha1_with_mode should be folded into get_sha1_with_context, as mode
is really just another case of this exact sort of mid-lookup context.
And then we don't get a proliferation of "get_sha1_with_*" functions,
which doesn't scale.  I'll leave that as an exercise for your students.
:)

You could get fancier, including things like which ref we ended up
looking at to get to the object. I seem to recall running into a
situation where I wanted to know "foo" when looking up "foo^", but I
don't remember where now.  I think it makes sense to keep it simple for
now, and people could add more context elements as needed.

diff --git a/builtin/cat-file.c b/builtin/cat-file.c
index a933eaa..4d1e634 100644
--- a/builtin/cat-file.c
+++ b/builtin/cat-file.c
@@ -87,6 +87,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
 	void *buf;
 	unsigned long size;
 
+	object_resolve_context_init(&the_resolve_context);
 	if (get_sha1(obj_name, sha1))
 		die("Not a valid object name %s", obj_name);
 
@@ -129,6 +130,18 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
 			pprint_tag(sha1, buf, size);
 			return 0;
 		}
+		if (type == OBJ_BLOB) {
+			struct strbuf out = STRBUF_INIT;
+
+			if (!the_resolve_context.path[0])
+				break;
+			if (convert_to_working_tree(the_resolve_context.path,
+						buf, size, &out) < 0)
+				die("unable to prepare blob for printing");
+			free(buf);
+			size = out.len;
+			buf = strbuf_detach(&out, NULL);
+		}
 
 		/* otherwise just spit out the data */
 		break;
diff --git a/cache.h b/cache.h
index c966023..c030083 100644
--- a/cache.h
+++ b/cache.h
@@ -730,6 +730,13 @@ static inline unsigned int hexval(unsigned char c)
 #define MINIMUM_ABBREV 4
 #define DEFAULT_ABBREV 7
 
+struct object_resolve_context {
+	unsigned char tree[20];
+	char path[PATH_MAX];
+};
+extern struct object_resolve_context the_resolve_context;
+void object_resolve_context_init(struct object_resolve_context *orc);
+
 extern int get_sha1(const char *str, unsigned char *sha1);
 extern int get_sha1_with_mode_1(const char *str, unsigned char *sha1, unsigned *mode, int gently, const char *prefix);
 static inline int get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode)
diff --git a/sha1_name.c b/sha1_name.c
index bf92417..cc049bf 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -7,6 +7,13 @@
 #include "refs.h"
 #include "remote.h"
 
+struct object_resolve_context the_resolve_context;
+
+void object_resolve_context_init(struct object_resolve_context *orc)
+{
+	memset(orc, 0, sizeof(*orc));
+}
+
 static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
 {
 	struct alternate_object_database *alt;
@@ -1104,6 +1111,10 @@ int get_sha1_with_mode_1(const char *name, unsigned char *sha1, unsigned *mode,
 							   tree_sha1, object_name);
 				free(object_name);
 			}
+			hashcpy(the_resolve_context.tree, tree_sha1);
+			strncpy(the_resolve_context.path, filename,
+				sizeof(the_resolve_context.path));
+			the_resolve_context.path[sizeof(the_resolve_context.path)] = '\0';
 			return ret;
 		} else {
 			if (!gently)

  reply	other threads:[~2010-06-01 19:50 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-06-01 13:41 Best way to apply textconv to a working tree file Clément Poulain
2010-06-01 16:07 ` Matthieu Moy
2010-06-01 17:14   ` Jeff King
2010-06-01 17:30     ` Matthieu Moy
2010-06-01 19:50       ` Jeff King [this message]
2010-06-02 15:12         ` Clément Poulain
2010-06-01 17:04 ` Jeff King
2010-06-02  9:56   ` Clément Poulain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100601195036.GA18220@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=Matthieu.Moy@grenoble-inp.fr \
    --cc=axel.bonnet@ensimag.imag.fr \
    --cc=clement.poulain@ensimag.imag.fr \
    --cc=diane.gasselin@ensimag.imag.fr \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).