git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 7/9] setup: split off get_device_or_die helper

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Jul 24, 2010, 11:27 UTC
Message-ID
<20100724112758.GG16054@burratino>
In-Reply-To
<20100724111505.GC7150@burratino>

This does not eliminate any code, but it skims some off of the main loop of setup_git_directory_gently so that can be understood more easily.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 setup.c |   25 ++++++++++++++-----------
 1 files changed, 14 insertions(+), 11 deletions(-)
diff --git a/setup.c b/setup.c
index 9fc05e2..5fb9c54 100644
--- a/setup.c
+++ b/setup.c
@@ -388,6 +388,16 @@ static const char *setup_nongit(const char *cwd, int *nongit_ok)
 	return NULL;
 }
 
+static dev_t get_device_or_die(const char *path, const char *prefix)
+{
+	struct stat buf;
+	if (stat(path, &buf))
+		die_errno("failed to stat '%s%s%s'",
+				prefix ? prefix : "",
+				prefix ? "/" : "", path);
+	return buf.st_dev;
+}
+
 /*
  * We cannot decide in this function whether we are in the work tree or
  * not, since the config can only be read _after_ this function was called.
@@ -402,7 +412,6 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	int len, offset, ceil_offset, root_len;
 	dev_t current_device = 0;
 	int one_filesystem = 1;
-	struct stat buf;
 
 	/*
 	 * Let's assume that we are in a git repository.
@@ -441,11 +450,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	 */
 	offset = len = strlen(cwd);
 	one_filesystem = !git_env_bool("GIT_DISCOVERY_ACROSS_FILESYSTEM", 0);
-	if (one_filesystem) {
-		if (stat(".", &buf))
-			die_errno("failed to stat '.'");
-		current_device = buf.st_dev;
-	}
+	if (one_filesystem)
+		current_device = get_device_or_die(".", NULL);
 	for (;;) {
 		if (cwd_contains_git_dir(&gitfile_dir))
 			break;
@@ -456,11 +462,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
 		if (offset <= ceil_offset)
 			return setup_nongit(cwd, nongit_ok);
 		if (one_filesystem) {
-			if (stat("..", &buf)) {
-				cwd[offset] = '\0';
-				die_errno("failed to stat '%s/..'", cwd);
-			}
-			if (buf.st_dev != current_device) {
+			dev_t parent_device = get_device_or_die("..", cwd);
+			if (parent_device != current_device) {
 				if (nongit_ok) {
 					if (chdir(cwd))
 						die_errno("Cannot come back to cwd");
-- 
1.7.2.rc3.593.g19611.dirty
Previous: Jonathan NiederNext: Jonathan Nieder
Message 17 of 20 in “worktree setup: return to original cwd if prefix is set NULL”
  1. 1/2 worktree setup: return to original cwd if prefix is set NULLNguyễn Thái Ngọc Duy, Jul 23, 2010
  2. 2/2 Revert "rehabilitate 'git index-pack' inside the object store"Nguyễn Thái Ngọc Duy, Jul 23, 2010
  3. Ævar Arnfjörð BjarmasonJul 23, 2010
  4. Nguyen Thai Ngoc DuyJul 24, 2010
  5. Ævar Arnfjörð BjarmasonJul 24, 2010
  6. 1/2 worktree setup: return to original cwd if prefix is set NULLNguyễn Thái Ngọc Duy, Jul 23, 2010
  7. 0/9 setup_git_directory(): return to original cwd upon reaching .gitJonathan Nieder, Jul 24, 2010
  8. 1/9 t1501 (rev-parse): clarifyJonathan Nieder, Jul 24, 2010
  9. 2/9 tests: try git apply from subdir of toplevelJonathan Nieder, Jul 24, 2010
  10. 3/9 setup: split off $GIT_DIR-set case from setup_git_directory_gentlyJonathan Nieder, Jul 24, 2010
  11. Nguyen Thai Ngoc DuyJul 24, 2010
  12. 4/9 setup: split off a function to checks working dir for .git fileJonathan Nieder, Jul 24, 2010
  13. Nguyen Thai Ngoc DuyJul 24, 2010
  14. Jonathan NiederJul 24, 2010
  15. 5/9 setup: split off code to handle stumbling upon a repositoryJonathan Nieder, Jul 24, 2010
  16. 6/9 setup: split off a function to handle hitting ceiling in repo searchJonathan Nieder, Jul 24, 2010
  17. 7/9 setup: split off get_device_or_die helperJonathan Nieder, Jul 24, 2010
  18. 8/9 setup: do not forget working dir from subdir of gitdirJonathan Nieder, Jul 24, 2010
  19. 9/9 Revert "rehabilitate 'git index-pack' inside the object store"Jonathan Nieder, Jul 24, 2010
  20. Nguyen Thai Ngoc DuyJul 24, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.