git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Shourya Shukla <shouryashukla.oo@gmail.com>
To: git@vger.kernel.org
Cc: christian.couder@gmail.com, peff@peff.net, heba.waly@gmail.com,
	Johannes.Schindelin@gmx.de, gitster@pobox.com,
	Shourya Shukla <shouryashukla.oo@gmail.com>
Subject: [PATCH 1/2] submodule.c: update URL in .gitmodules using update_url_in_gitmodules
Date: Fri, 17 Apr 2020 02:34:55 +0530	[thread overview]
Message-ID: <20200416210456.19122-2-shouryashukla.oo@gmail.com> (raw)
In-Reply-To: <20200416210456.19122-1-shouryashukla.oo@gmail.com>

Create a helper function update_url_in_gitmodules() to update URL
of an entry in .gitmodules. Later on, we want to use this function
to aid in the conversion of 'set-url' from shell to C.

Signed-off-by: Shourya Shukla <shouryashukla.oo@gmail.com>
---
 submodule.c | 33 +++++++++++++++++++++++++++++++++
 submodule.h |  2 ++
 2 files changed, 35 insertions(+)

diff --git a/submodule.c b/submodule.c
index c3aadf3fff..0b599dc4e1 100644
--- a/submodule.c
+++ b/submodule.c
@@ -126,6 +126,39 @@ int update_path_in_gitmodules(const char *oldpath, const char *newpath)
 	return ret;
 }
 
+/*
+ * Try to update the "url" entry in the "submodule.<name>" section of the
+ * .gitmodules file. Return 0 only if a .gitmodules file was found, a section
+ * with the correct url=<oldurl> setting was found and we could update it.
+ */
+int update_url_in_gitmodules(const char *path, const char *newurl)
+{
+	struct strbuf entry = STRBUF_INIT;
+	const struct submodule *submodule;
+	int ret;
+
+	/* Do nothing without .gitmodules */
+	if (!file_exists(GITMODULES_FILE))
+		return -1;
+
+	if (is_gitmodules_unmerged(the_repository->index))
+		die(_("Cannot change unmerged .gitmodules, resolve merge conflicts first"));
+
+	submodule = submodule_from_path(the_repository, &null_oid, path);
+	if (!submodule || !submodule->name) {
+		warning(_("Could not find section in .gitmodules where path=%s"), path);
+		return -1;
+	}
+	
+	strbuf_addstr(&entry, "submodule.");
+	strbuf_addstr(&entry, submodule->name);
+	strbuf_addstr(&entry, ".url");
+	ret = config_set_in_gitmodules_file_gently(entry.buf, newurl);
+	strbuf_release(&entry);
+	
+	return ret;
+}
+
 /*
  * Try to remove the "submodule.<name>" section from .gitmodules where the given
  * path is configured. Return 0 only if a .gitmodules file was found, a section
diff --git a/submodule.h b/submodule.h
index 4dad649f94..ea09480433 100644
--- a/submodule.h
+++ b/submodule.h
@@ -49,6 +49,8 @@ void set_diffopt_flags_from_submodule_config(struct diff_options *,
 					     const char *path);
 int git_default_submodule_config(const char *var, const char *value, void *cb);
 
+int update_url_in_gitmodules(const char* path, const char *newurl);
+
 struct option;
 int option_parse_recurse_submodules_worktree_updater(const struct option *opt,
 						     const char *arg, int unset);
-- 
2.20.1


  reply	other threads:[~2020-04-16 21:05 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-16 21:04 [RFC][PATCH 0/2] submodule: port 'set-url' from shell to C Shourya Shukla
2020-04-16 21:04 ` Shourya Shukla [this message]
2020-04-16 21:04 ` [PATCH 2/2] submodule: port subcommand " Shourya Shukla
2020-04-17 22:13   ` Christian Couder
2020-04-17 22:09 ` [RFC][PATCH 0/2] submodule: port " Christian Couder
2020-04-20  8:19 ` Denton Liu
2020-04-27  7:19   ` Shourya Shukla
2020-04-27 10:17     ` Denton Liu

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

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

  git send-email \
    --in-reply-to=20200416210456.19122-2-shouryashukla.oo@gmail.com \
    --to=shouryashukla.oo@gmail.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=heba.waly@gmail.com \
    --cc=peff@peff.net \
    /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.
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 NNTP newsgroup(s).