git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Sahil Dua <sahildua2305@gmail.com>
To: git@vger.kernel.org
Subject: [PATCH/RFC v2 5/6] config: add copy config section logic
Date: Wed, 31 May 2017 23:35:12 +0000	[thread overview]
Message-ID: <0102015c60dcf6be-6001eb0c-c085-4972-a25b-2490a062e3f8-000000@eu-west-1.amazonses.com> (raw)
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

  parent reply	other threads:[~2017-05-31 23:35 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-28 22:56 [PATCH/RFC] branch: add tests for new copy branch feature Sahil Dua
2017-05-28 23:30 ` Ævar Arnfjörð Bjarmason
2017-05-29 20:41   ` Sahil Dua
2017-05-29 20:50     ` Ævar Arnfjörð Bjarmason
2017-05-29 22:23       ` Sahil Dua
2017-06-13 17:55       ` Jonathan Nieder
2017-06-13 18:01         ` Ævar Arnfjörð Bjarmason
2017-06-13 18:08           ` Jonathan Nieder
2017-05-29  2:09 ` Junio C Hamano
2017-05-29 19:39   ` Sahil Dua
2017-05-31 23:35 ` [PATCH/RFC v2 1/6] " Sahil Dua
2017-05-31 23:35   ` [PATCH/RFC v2 6/6] branch: don't copy or rename config when same branch name Sahil Dua
2017-05-31 23:35   ` [PATCH/RFC v2 3/6] config: abstract out create section from key logic Sahil Dua
2017-05-31 23:35   ` [PATCH/RFC v2 2/6] branch: add copy branch option Sahil Dua
2017-06-01  1:50     ` Junio C Hamano
2017-06-01 16:09       ` Sahil Dua
2017-05-31 23:35   ` [PATCH/RFC v2 4/6] config: modify function signature to include copy argument Sahil Dua
2017-05-31 23:35   ` Sahil Dua [this message]
2017-06-01 18:35   ` [PATCH/RFC v3 1/3] branch: add tests for new copy branch feature Sahil Dua
2017-06-01 18:35     ` [PATCH/RFC v3 2/3] config: abstract out create section from key logic Sahil Dua
2017-06-01 18:35     ` [PATCH/RFC v3 3/3] branch: add copy branch feature implementation Sahil Dua
2017-06-01 18:59       ` Ævar Arnfjörð Bjarmason
2017-06-01 22:05         ` Sahil Dua
2017-06-05 20:40     ` [PATCH/RFC v4 1/3] branch: add tests for new copy branch feature Sahil Dua
2017-06-05 20:40       ` [PATCH/RFC v4 2/3] config: abstract out create section from key logic Sahil Dua
2017-06-05 20:40       ` [PATCH/RFC v4 3/3] branch: add copy branch feature implementation Sahil Dua
2017-06-05 20:52         ` Sahil Dua
2017-06-06  0:10           ` Junio C Hamano
2017-06-06  0:14             ` Junio C Hamano
2017-06-06  7:39             ` Ævar Arnfjörð Bjarmason
2017-06-06 10:13               ` Sahil Dua
2017-06-06 12:03               ` Junio C Hamano
2017-06-13 16:17       ` [PATCH 1/3] config: create a function to format section headers Sahil Dua
2017-06-13 16:17         ` [PATCH 2/3] branch: add test for -m renaming multiple config sections Sahil Dua
2017-06-13 17:10           ` Junio C Hamano
2017-06-13 17:31             ` Ævar Arnfjörð Bjarmason
2017-06-13 17:39               ` Junio C Hamano
2017-06-13 17:53                 ` Ævar Arnfjörð Bjarmason
2017-06-18 21:17           ` [PATCH v2 " Sahil Dua
2017-06-13 16:17         ` [PATCH 3/3] branch: add a --copy (-c) option to go with --move (-m) Sahil Dua
2017-06-13 17:05           ` Junio C Hamano
2017-06-13 17:30             ` Junio C Hamano
2017-06-14  8:01               ` Sahil Dua
2017-06-18 21:19           ` [PATCH v2 " Sahil Dua
2017-06-13 17:06         ` [PATCH 1/3] config: create a function to format section headers Junio C Hamano
2017-06-13 17:09         ` Ævar Arnfjörð Bjarmason
2017-06-18 21:16         ` [PATCH v2 " Sahil Dua
2017-06-19 12:08           ` Ramsay Jones
2017-06-19 14:51             ` Sahil Dua

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0102015c60dcf6be-6001eb0c-c085-4972-a25b-2490a062e3f8-000000@eu-west-1.amazonses.com \
    --to=sahildua2305@gmail.com \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).