Skip to contentSkip to conversation

git-find-git: a new helper.

PATCH In review Junio C Hamano 3 participants 5 messages Jun 15, 2016 1 message not in the archive Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 git-find-git: a new helper.

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.

---

 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

Makefile +1 -1
index a97a5d9..dda363a 100644
@@ -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 = \
cache.h +1 -0
index 6ac94c5..3104c59 100644
@@ -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)
find-git.c new file +41 -0

41 lines not on this page · raw message

git-sh-setup.sh +28 -11
index dbb9884..8e30bf6 100755
@@ -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
setup.c +12 -2
index c487d7e..f64ca23 100644
@@ -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

#1 lore raw
Quoted 11 lines · Junio C Hamano

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

#2 lore raw
Quoted 10 lines · Marco Costalba
Quoted 8 lines · Junio C Hamano

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.

Quoted 4 lines

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.

#3 lore raw
In reply to Junio C Hamano
Quoted 5 lines · you

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

#5 lore raw
In reply to a message not in the archive
Quoted 6 lines · you
Quoted 1 line · Josef Weidendorfer

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