git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andy Parkins <andyparkins@gmail.com>
To: git@vger.kernel.org
Subject: [PATCH/RFC] Add support for push globbed refspecs
Date: Mon, 11 Dec 2006 21:33:41 +0000	[thread overview]
Message-ID: <200612112133.42060.andyparkins@gmail.com> (raw)

Just as we now have glob support in fetch refspecs, this patch adds
support for globs in the push refspecs.  In other words you can do:

[remote "globs"]
	url = proto://host/repo.git
	push = refs/heads/*:refs/remotes/from_me/*

With this, a git-push will then do a normal directory glob on the local
ref path; whatever it finds will have the matching part extracted and
inserted into the remote half of the refspec.

Note the following:
 - subdirectories aren't supported
 - globs with more than one "*" will probably go /very/ wrong

Signed-off-by: Andy Parkins <andyparkins@gmail.com>
---
I'm not sure this is the right way to have done this.

My feeling is that perhaps a for_each_ref() call and some string
manipulation would have been better.  What'd you all reckon?


 builtin-push.c |   70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 70 insertions(+), 0 deletions(-)

diff --git a/builtin-push.c b/builtin-push.c
index d23974e..e60db71 100644
--- a/builtin-push.c
+++ b/builtin-push.c
@@ -5,6 +5,7 @@
 #include "refs.h"
 #include "run-command.h"
 #include "builtin.h"
+#include <glob.h>
 
 #define MAX_URI (16)
 
@@ -22,6 +23,75 @@ static int refspec_nr;
 static void add_refspec(const char *ref)
 {
 	int nr = refspec_nr + 1;
+	char *globpos_local = NULL,
+		 *globpos_remote = NULL,
+		 *separator = NULL;
+	char *buffer;
+
+	/* See if the ref is a globref */
+	buffer = xmalloc(strlen(ref)+1);
+	strcpy(buffer, ref);
+	separator = strstr(buffer, ":");
+	if (separator != NULL)
+		globpos_remote = strstr(separator, "*");
+	globpos_local = strstr(buffer, "*");
+
+	/* If globpos is set, then we have enough information to perform
+	 * the expansion */
+	if( globpos_remote != NULL && globpos_local != NULL) {
+		glob_t globres;
+		int ret;
+		unsigned int i;
+		unsigned int gitpathlen;
+		char *newrefspec;
+
+		/* Break the refspec into chunks:
+		 *  <localref>:<remote_prefix>*<remoteref_suffix>
+		 */
+		*separator = '\0';
+		separator++;
+		*globpos_remote = '\0';
+		globpos_remote++;
+		/* Now buffer points at localref; separator points at
+		 * remoteref prefix, globpos_remote points at remoteref suffix */
+
+		/* Fetch all the matching local refs */
+		globres.gl_offs = 0;
+		ret = glob(git_path("%s", buffer), GLOB_ERR, NULL, &globres);
+		gitpathlen = strlen( git_path("") );
+
+		if (ret == 0) {
+			for (i = 0; i < globres.gl_pathc; i++) {
+				char *p, *q;
+
+				p = globres.gl_pathv[i] + gitpathlen + (globpos_local - buffer);
+				q = p + strlen(p) - strlen(globpos_local+1);
+
+				newrefspec = xmalloc(
+						strlen(globres.gl_pathv[i] + gitpathlen)
+						+ strlen(separator)
+						+ (q-p)
+						+ strlen( globpos_remote)
+						+ 1 + 1 );
+
+				sprintf( newrefspec, "%s:%s%.*s%s",
+						globres.gl_pathv[i] + gitpathlen,
+						separator,
+						q-p, p,
+						globpos_remote );
+
+				add_refspec( newrefspec );
+			}
+		}
+
+		/* tidy up */
+		globfree( &globres );
+
+		/* Don't add the globspec */
+		return;
+	}
+	free(buffer);
+
 	refspec = xrealloc(refspec, nr * sizeof(char *));
 	refspec[nr-1] = ref;
 	refspec_nr = nr;
-- 
1.4.4.1.GIT

                 reply	other threads:[~2006-12-11 21:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=200612112133.42060.andyparkins@gmail.com \
    --to=andyparkins@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.
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).