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

[PATCH/RFC v2 5/6] config: add copy config section logic

From
Sahil Dua <sahildua2305@gmail.com>
Date
May 31, 2017, 23:35 UTC
Message-ID
<0102015c60dcf6be-6001eb0c-c085-4972-a25b-2490a062e3f8-000000@eu-west-1.amazonses.com>
In-Reply-To
<0102015c60dcf5f6-057de56f-3355-40dc-a0d3-ee62fa9b8259-000000@eu-west-1.amazonses.com>

Adds implementation for copying the config section while copying a branch.

While we're parsing the config file, we need to make sure we start copying the config section once we find the matching block for our branch1 (for example when running 'git branch -c branch1 branch2').

There is one flag used - 'copying_section' which can take 0/1/2 values. 0 - not copying currently 1 - just started copying section 2 - currently copying I thought of making this flag binary to keep things easier. However, since there was distinction in behavior(adding to currently copied section) depending upon whether it's the first line of config section or not.

The copied section has first line which contains the new branch name (branch2 in our example). This is achieved using store_create_section method.

Once we're done with reading the entire config file, we write our copied section. Hence, literally copying the config section from branch1 to branch2.

However, there's one case which is not handled by this yet - when branch2 already has some configuration and -C command is used, operation should delete the present configuration for branch2.

Signed-off-by: Sahil Dua <sahildua2305@gmail.com>
---
 config.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 52 insertions(+), 15 deletions(-)
diff --git a/config.c b/config.c
index 155274f03b2b6..2bf711ca3e0da 100644
--- a/config.c
+++ b/config.c
@@ -2642,13 +2642,14 @@ static int section_name_is_ok(const char *name)
 int git_config_copy_or_rename_section_in_file(const char *config_filename,
 				      const char *old_name, const char *new_name, int copy)
 {
-	int ret = 0, remove = 0;
+	int ret = 0, remove = 0, copying_section = 0, copied_section_length;
 	char *filename_buf = NULL;
 	struct lock_file *lock;
 	int out_fd;
 	char buf[1024];
 	FILE *config_file = NULL;
 	struct stat st;
+	struct strbuf copied_section;
 
 	if (new_name && !section_name_is_ok(new_name)) {
 		ret = error("invalid section name: %s", new_name);
@@ -2689,6 +2690,13 @@ int git_config_copy_or_rename_section_in_file(const char *config_filename,
 			; /* do nothing */
 		if (buf[i] == '[') {
 			/* it's a section */
+			if (copying_section) {
+				/* Mark the end of copying the matching
+				 * section, as this is the beginning
+				 * of the new section
+				 */
+				copying_section = 0;
+			}
 			int offset = section_name_match(&buf[i], old_name);
 			if (offset > 0) {
 				ret++;
@@ -2696,26 +2704,41 @@ int git_config_copy_or_rename_section_in_file(const char *config_filename,
 					remove = 1;
 					continue;
 				}
-				store.baselen = strlen(new_name);
-				if (!store_write_section(out_fd, new_name)) {
-					ret = write_error(get_lock_file_path(lock));
-					goto out;
+				if (!copy) {
+					store.baselen = strlen(new_name);
+					if (!store_write_section(out_fd, new_name)) {
+						ret = write_error(get_lock_file_path(lock));
+						goto out;
+					}
+				} else {
+					/* Mark the beginning of copying the matching section */
+					copying_section = 1;
+
+					/* TODO: Make this work for the
+					 * case when there are multiple
+					 * matching sections
+					 */
+					/* Create a section with new branch name */
+					store.baselen = strlen(new_name);
+					copied_section = store_create_section(new_name);
 				}
 				/*
 				 * We wrote out the new section, with
 				 * a newline, now skip the old
 				 * section's length
 				 */
-				output += offset + i;
-				if (strlen(output) > 0) {
-					/*
-					 * More content means there's
-					 * a declaration to put on the
-					 * next line; indent with a
-					 * tab
-					 */
-					output -= 1;
-					output[0] = '\t';
+				if (!copy) {
+					output += offset + i;
+					if (strlen(output) > 0) {
+						/*
+						 * More content means there's
+						 * a declaration to put on the
+						 * next line; indent with a
+						 * tab
+						 */
+						output -= 1;
+						output[0] = '\t';
+					}
 				}
 			}
 			remove = 0;
@@ -2723,11 +2746,25 @@ int git_config_copy_or_rename_section_in_file(const char *config_filename,
 		if (remove)
 			continue;
 		length = strlen(output);
+
+		if (copying_section > 1) {
+			strbuf_addf(&copied_section, "%s", output);
+		} else if (copying_section == 1) {
+			copying_section = 2;
+		}
 		if (write_in_full(out_fd, output, length) != length) {
 			ret = write_error(get_lock_file_path(lock));
 			goto out;
 		}
 	}
+
+	if (copy && copied_section.len > 0) {
+		copied_section_length = strlen(copied_section.buf);
+		if (write_in_full(out_fd, copied_section.buf, copied_section_length) != copied_section_length) {
+			ret = write_error(get_lock_file_path(lock));
+			goto out;
+		}
+	}
 	fclose(config_file);
 	config_file = NULL;
 commit_and_out:

--
https://github.com/git/git/pull/363
Previous: Sahil DuaNext: Sahil Dua
Message 8 of 39 in “branch: add tests for new copy branch feature”
  1. 1/6 branch: add tests for new copy branch featureSahil Dua, May 31, 2017
  2. 4/6 config: modify function signature to include copy argumentSahil Dua, May 31, 2017
  3. 6/6 branch: don't copy or rename config when same branch nameSahil Dua, May 31, 2017
  4. 3/6 config: abstract out create section from key logicSahil Dua, May 31, 2017
  5. 2/6 branch: add copy branch optionSahil Dua, May 31, 2017
  6. Junio C HamanoJun 1, 2017
  7. Sahil DuaJun 1, 2017
  8. 5/6 config: add copy config section logicSahil Dua, May 31, 2017
  9. 1/3 branch: add tests for new copy branch featureSahil Dua, Jun 1, 2017
  10. 2/3 config: abstract out create section from key logicSahil Dua, Jun 1, 2017
  11. 3/3 branch: add copy branch feature implementationSahil Dua, Jun 1, 2017
  12. Ævar Arnfjörð BjarmasonJun 1, 2017
  13. Sahil DuaJun 1, 2017
  14. 1/3 branch: add tests for new copy branch featureSahil Dua, Jun 5, 2017
  15. 2/3 config: abstract out create section from key logicSahil Dua, Jun 5, 2017
  16. 3/3 branch: add copy branch feature implementationSahil Dua, Jun 5, 2017
  17. Sahil DuaJun 5, 2017
  18. Junio C HamanoJun 6, 2017
  19. Junio C HamanoJun 6, 2017
  20. Ævar Arnfjörð BjarmasonJun 6, 2017
  21. Sahil DuaJun 6, 2017
  22. Junio C HamanoJun 6, 2017
  23. 1/3 config: create a function to format section headersSahil Dua, Jun 13, 2017
  24. 2/3 branch: add test for -m renaming multiple config sectionsSahil Dua, Jun 13, 2017
  25. Junio C HamanoJun 13, 2017
  26. Ævar Arnfjörð BjarmasonJun 13, 2017
  27. Junio C HamanoJun 13, 2017
  28. Ævar Arnfjörð BjarmasonJun 13, 2017
  29. 2/3 branch: add test for -m renaming multiple config sectionsSahil Dua, Jun 18, 2017
  30. 3/3 branch: add a --copy (-c) option to go with --move (-m)Sahil Dua, Jun 13, 2017
  31. Junio C HamanoJun 13, 2017
  32. Junio C HamanoJun 13, 2017
  33. Sahil DuaJun 14, 2017
  34. 3/3 branch: add a --copy (-c) option to go with --move (-m)Sahil Dua, Jun 18, 2017
  35. Junio C HamanoJun 13, 2017
  36. Ævar Arnfjörð BjarmasonJun 13, 2017
  37. 1/3 config: create a function to format section headersSahil Dua, Jun 18, 2017
  38. Ramsay JonesJun 19, 2017
  39. Sahil DuaJun 19, 2017

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.