git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC/PATCH 1/3] git-find-git: a new helper.
@ 2005-11-24  3:36 Junio C Hamano
  2005-11-24  5:41 ` Marco Costalba
  2005-12-01 14:35 ` Josef Weidendorfer
  0 siblings, 2 replies; 5+ messages in thread
From: Junio C Hamano @ 2005-11-24  3:36 UTC (permalink / raw)
  To: git; +Cc: Martin Atukunda

This is part of rev-format check work, which is currently my top
priority that is holding up any new 1.0rc updates to maint
branch.  It adds a new helper, git-find-git command, to the
suite, and rewrites git-sh-setup to use it.

It adds setup_git_directory_gently(), which is a variant of
existing setup_git_directory() but does not die() if we are not
in a git managed repository.  git-find-git uses it to find the
top-level, and if it was run from a subdirectory, outputs shell
script fragments that can be evaled to define the two variables:

    GIT_DIR_PREFIX is a string to be prepended to cwd relative
    paths to make them repository relative.

    GIT_DIR_UPLEVEL is a string (e.g. "../") for the command to
    use to cd to the top-level of repository.

Signed-off-by: Junio C Hamano <junkio@cox.net>

---

 Makefile        |    2 +-
 cache.h         |    1 +
 find-git.c      |   41 +++++++++++++++++++++++++++++++++++++++++
 git-sh-setup.sh |   39 ++++++++++++++++++++++++++++-----------
 setup.c         |   14 ++++++++++++--
 5 files changed, 83 insertions(+), 14 deletions(-)
 create mode 100644 find-git.c

applies-to: 10227b43e837a38d95d472a0b7c400749fbd46e9
15d4e27b74f4ff0c9faa5bbbfe8e915398f3be97
diff --git a/Makefile b/Makefile
index a97a5d9..dda363a 100644
--- a/Makefile
+++ b/Makefile
@@ -110,7 +110,7 @@ SCRIPTS = $(patsubst %.sh,%,$(SCRIPT_SH)
 # The ones that do not have to link with lcrypto nor lz.
 SIMPLE_PROGRAMS = \
 	git-get-tar-commit-id$X git-mailinfo$X git-mailsplit$X \
-	git-stripspace$X git-daemon$X
+	git-stripspace$X git-daemon$X git-find-git$X
 
 # ... and all the rest
 PROGRAMS = \
diff --git a/cache.h b/cache.h
index 6ac94c5..3104c59 100644
--- a/cache.h
+++ b/cache.h
@@ -148,6 +148,7 @@ extern char *get_graft_file(void);
 
 extern const char **get_pathspec(const char *prefix, const char **pathspec);
 extern const char *setup_git_directory(void);
+extern const char *setup_git_directory_gently(int *);
 extern const char *prefix_path(const char *prefix, int len, const char *path);
 
 #define alloc_nr(x) (((x)+16)*3/2)
diff --git a/find-git.c b/find-git.c
new file mode 100644
index 0000000..e063425
--- /dev/null
+++ b/find-git.c
@@ -0,0 +1,41 @@
+#include "cache.h"
+#include "quote.h"
+
+int main(int ac, char **av)
+{
+	int not_a_git = 0;
+	const char *prefix = setup_git_directory_gently(&not_a_git);
+
+	if (!prefix && not_a_git)
+		exit(1);
+
+	/* This can die with malformed configuration file --
+	 * exit code from die() is 128.
+	 */
+	git_config(git_default_config);
+
+	/* Later we check repository version and exit with non-zero
+	 * status after issuing an error message here.
+	 */
+
+	if (!prefix)
+		/* we are at the project toplevel or GIT_DIR is set.
+		 * either case we do not have to muck with the
+		 * environment further.
+		 */
+		exit(0);
+
+	/* this leaks but we do not care ;-) */
+	printf("GIT_DIR_PREFIX=%s\n", sq_quote(prefix));
+	printf("GIT_DIR_UPLEVEL='");
+	while (prefix) {
+		prefix = strchr(prefix, '/');
+		if (prefix) {
+			prefix++;
+			printf("../");
+		}
+	}
+	printf("'\n");
+	printf("export GIT_DIR_PREFIX GIT_DIR_UPLEVEL\n");
+	exit(0);
+}
diff --git a/git-sh-setup.sh b/git-sh-setup.sh
index dbb9884..8e30bf6 100755
--- a/git-sh-setup.sh
+++ b/git-sh-setup.sh
@@ -3,13 +3,11 @@
 # Set up GIT_DIR and GIT_OBJECT_DIRECTORY
 # and return true if everything looks ok
 #
-: ${GIT_DIR=.git}
-: ${GIT_OBJECT_DIRECTORY="$GIT_DIR/objects"}
 
 # Having this variable in your environment would break scripts because
-# you would cause "cd" to be be taken to unexpected places.  If you
-# like CDPATH, define it for your interactive shell sessions without
-# exporting it.
+# you would cause "cd" to be taken to unexpected places.  If you line
+# CDPATH, define it for your interactive shell sessions without exporting
+# it.
 unset CDPATH
 
 die() {
@@ -17,9 +15,28 @@ die() {
 	exit 1
 }
 
-case "$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD 2>/dev/null)" in
-refs/*)	: ;;
-*)	false ;;
-esac &&
-[ -d "$GIT_DIR/refs" ] &&
-[ -d "$GIT_OBJECT_DIRECTORY/" ]
+eval "`git-find-git`"
+case "$?" in
+0)
+	: ${GIT_DIR=.git}
+	: ${GIT_OBJECT_DIRECTORY="$GIT_DIR/objects"}
+
+	case "$(GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD 2>/dev/null)" in
+	refs/*)	: ;;
+	*)	false ;;
+	esac &&
+	[ -d "$GIT_DIR/refs" ] &&
+	[ -d "$GIT_OBJECT_DIRECTORY/" ]
+	;;
+*)
+	# (1) we did not find a git directory.  This is sometimes OK.
+	# ls-remote (and parse-remote called from it) uses sh-set to
+	# pick up remotes shortcut if available, but outside git the
+	# user would want to use the command with explicitly spelled
+	# out URL.
+
+	# (128) git-find-git died -- malformed configuration and that
+	# is really fatal.  We have already given the error message.
+
+	;;
+esac
diff --git a/setup.c b/setup.c
index c487d7e..f64ca23 100644
--- a/setup.c
+++ b/setup.c
@@ -92,7 +92,7 @@ static int is_toplevel_directory(void)
 	return 1;
 }
 
-const char *setup_git_directory(void)
+const char *setup_git_directory_gently(int *just_fail_on_ungit)
 {
 	static char cwd[PATH_MAX+1];
 	int len, offset;
@@ -113,8 +113,13 @@ const char *setup_git_directory(void)
 			break;
 		chdir("..");
 		do {
-			if (!offset)
+			if (!offset) {
+				if (just_fail_on_ungit) {
+					*just_fail_on_ungit = 1;
+					return NULL;
+				}
 				die("Not a git repository");
+			}
 		} while (cwd[--offset] != '/');
 	}
 
@@ -127,3 +132,8 @@ const char *setup_git_directory(void)
 	cwd[len] = 0;
 	return cwd + offset;
 }
+
+const char *setup_git_directory(void)
+{
+	return setup_git_directory_gently(NULL);
+}
---
0.99.9.GIT

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

* Re: [RFC/PATCH 1/3] git-find-git: a new helper.
  2005-11-24  3:36 [RFC/PATCH 1/3] git-find-git: a new helper Junio C Hamano
@ 2005-11-24  5:41 ` Marco Costalba
  2005-11-24  6:21   ` Junio C Hamano
  2005-12-01 14:35 ` Josef Weidendorfer
  1 sibling, 1 reply; 5+ messages in thread
From: Marco Costalba @ 2005-11-24  5:41 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git, Martin Atukunda

Junio C Hamano wrote:

>  git-find-git uses it to find the
> top-level, and if it was run from a subdirectory, outputs shell
> script fragments that can be evaled to define the two variables:
> 
>     GIT_DIR_PREFIX is a string to be prepended to cwd relative
>     paths to make them repository relative.
> 
>     GIT_DIR_UPLEVEL is a string (e.g. "../") for the command to
>     use to cd to the top-level of repository.
> 
>
Sounds very good.

I did't had the time to test it but I think I can now
easily update qgit to run from a subdirectory with this new helper.

Marco

	

	
		
___________________________________ 
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB 
http://mail.yahoo.it

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

* Re: [RFC/PATCH 1/3] git-find-git: a new helper.
  2005-11-24  5:41 ` Marco Costalba
@ 2005-11-24  6:21   ` Junio C Hamano
  0 siblings, 0 replies; 5+ messages in thread
From: Junio C Hamano @ 2005-11-24  6:21 UTC (permalink / raw)
  To: Marco Costalba; +Cc: git

Marco Costalba <mcostalba@yahoo.it> writes:

> Junio C Hamano wrote:
>
>>  git-find-git uses it to find the
>> top-level, and if it was run from a subdirectory, outputs shell
>> script fragments that can be evaled to define the two variables:
>>     GIT_DIR_PREFIX is a string to be prepended to cwd relative
>>     paths to make them repository relative.
>>     GIT_DIR_UPLEVEL is a string (e.g. "../") for the command to
>>     use to cd to the top-level of repository.
>>
> Sounds very good.
>
> I did't had the time to test it but I think I can now
> easily update qgit to run from a subdirectory with this new helper.

The helper might be good, but it turns out that the idea of
using it from git-sh-setup was not that great, so please
disregard the third part of the patch.  I think I need a bit
more thinking about this.

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

* Re: [RFC/PATCH 1/3] git-find-git: a new helper.
  2005-11-24  3:36 [RFC/PATCH 1/3] git-find-git: a new helper Junio C Hamano
  2005-11-24  5:41 ` Marco Costalba
@ 2005-12-01 14:35 ` Josef Weidendorfer
       [not found]   ` <7vr78wirj7.fsf@assigned-by-dhcp.cox.net>
  1 sibling, 1 reply; 5+ messages in thread
From: Josef Weidendorfer @ 2005-12-01 14:35 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Thursday 24 November 2005 04:36, you wrote:
>     GIT_DIR_PREFIX is a string to be prepended to cwd relative
>     paths to make them repository relative.
> 
>     GIT_DIR_UPLEVEL is a string (e.g. "../") for the command to
>     use to cd to the top-level of repository.

Hi,

can we revive this idea?
I thought about how to make work

 $top/Documentation> git-mv ../file .

and also detection of trying to move to the outside.
Requirements IMHO:
(1) helper which is also useful from PERL
(2) git-mv checks against version controlled files via git-ls-files.
Thus, I need to run git-ls-files from the toplevel, to be able to
see $top/file. 
(3) translation of cwd relative into toplevel relative paths.

Or is there already a way to do this with current git?

Josef

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

* Re: [RFC/PATCH 1/3] git-find-git: a new helper.
       [not found]   ` <7vr78wirj7.fsf@assigned-by-dhcp.cox.net>
@ 2005-12-01 20:33     ` Josef Weidendorfer
  0 siblings, 0 replies; 5+ messages in thread
From: Josef Weidendorfer @ 2005-12-01 20:33 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: git

On Thursday 01 December 2005 19:46, you wrote:
> Josef Weidendorfer <Josef.Weidendorfer@gmx.de> writes:
> 
> > can we revive this idea?
> 
> I forgot git-rev-parse had --show-prefix and git-rev-parse --git-dir
> already when I did that one.

Ah, thanks.
By the way, the documentation ofr --git-dir is missing.

It would be nice to have some docu about "How to write a porcelain", and
this should go under the section "Making commands work in subdirs of
a working copy"...

Josef

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

end of thread, other threads:[~2005-12-01 20:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-11-24  3:36 [RFC/PATCH 1/3] git-find-git: a new helper Junio C Hamano
2005-11-24  5:41 ` Marco Costalba
2005-11-24  6:21   ` Junio C Hamano
2005-12-01 14:35 ` Josef Weidendorfer
     [not found]   ` <7vr78wirj7.fsf@assigned-by-dhcp.cox.net>
2005-12-01 20:33     ` Josef Weidendorfer

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).