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

Re: [PATCH 4/9] setup: split off a function to checks working dir for .git file

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Jul 24, 2010, 12:11 UTC
Message-ID
<20100724121157.GB16602@burratino>
In-Reply-To
<AANLkTim7C-byLb9txD11OrB6aC3uO=kBjQx2Q8HGLxL+@mail.gmail.com>
Nguyen Thai Ngoc Duy wrote:
Show 5 quoted lines
> Notice the two "break;" here, which is the only way to reach out the
> code below the loop. Perhaps you should move that code into
> cwd_contains_git_dir() too and change its name appropriately.
> setup_git_directory_gently() would become "discovery only" as real
> setup would be done by three new functions.
Neat.
-- %< --
From: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Subject: setup: split off a function to handle ordinary .git directories

Finish the clean-up of setup_git_directory_gently() by splitting the last case of validation+setup (global variables, prefix, check_format, set_git_dir) into its own function. Now setup_git_git_directory_gently itself takes care of discovery only and the functions that pick up from there are nearby in the source file so they can be easily compared.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
diff --git i/setup.c w/setup.c
index 5734a1f..d19aa7d 100644
--- i/setup.c
+++ w/setup.c
@@ -360,6 +360,28 @@ static int cwd_contains_git_dir(const char **gitfile_dirp)
 	return is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT);
 }
 
+static const char *setup_discovered_git_dir(const char *work_tree_env,
+		int offset, int len, char *cwd, int *nongit_ok)
+{
+	int root_len;
+
+	inside_git_dir = 0;
+	if (!work_tree_env)
+		inside_work_tree = 1;
+	root_len = offset_1st_component(cwd);
+	git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
+	if (check_repository_format_gently(nongit_ok))
+		return NULL;
+	if (offset == len)
+		return NULL;
+
+	/* Make "offset" point to past the '/', and add a '/' at the end */
+	offset++;
+	cwd[len++] = '/';
+	cwd[len] = 0;
+	return cwd + offset;
+}
+
 static const char *setup_bare_git_dir(const char *work_tree_env,
 		int offset, int len, char *cwd, int *nongit_ok)
 {
@@ -411,7 +433,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	static char cwd[PATH_MAX+1];
 	const char *gitdirenv;
 	const char *gitfile_dir;
-	int len, offset, ceil_offset, root_len;
+	int len, offset, ceil_offset;
 	dev_t current_device = 0;
 	int one_filesystem = 1;
 
@@ -456,7 +478,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
 		current_device = get_device_or_die(".", NULL);
 	for (;;) {
 		if (cwd_contains_git_dir(&gitfile_dir))
-			break;
+			return setup_discovered_git_dir(work_tree_env, offset,
+							len, cwd, nongit_ok);
 		if (is_git_directory("."))
 			return setup_bare_git_dir(work_tree_env, offset,
 							len, cwd, nongit_ok);
@@ -482,22 +505,6 @@ const char *setup_git_directory_gently(int *nongit_ok)
 			die_errno("Cannot change to '%s/..'", cwd);
 		}
 	}
-
-	inside_git_dir = 0;
-	if (!work_tree_env)
-		inside_work_tree = 1;
-	root_len = offset_1st_component(cwd);
-	git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
-	if (check_repository_format_gently(nongit_ok))
-		return NULL;
-	if (offset == len)
-		return NULL;
-
-	/* Make "offset" point to past the '/', and add a '/' at the end */
-	offset++;
-	cwd[len++] = '/';
-	cwd[len] = 0;
-	return cwd + offset;
 }
 
 int git_config_perm(const char *var, const char *value)
Previous: Nguyen Thai Ngoc DuyNext: Jonathan Nieder
Message 14 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.