From: Alex Riesen <raa.lkml@gmail.com>
To: Jakub Narebski <jnareb@gmail.com>
Cc: git@vger.kernel.org, Junio C Hamano <junkio@cox.net>,
Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Linus Torvalds <torvalds@linux-foundation.org>
Subject: [PATCH] Introduce pathexpand: syntax-level chdir into the given cwd
Date: Tue, 18 Dec 2007 21:49:47 +0100 [thread overview]
Message-ID: <20071218204947.GE2875@steel.home> (raw)
In-Reply-To: <20071218204752.GD2875@steel.home>
Signed-off-by: Alex Riesen <raa.lkml@gmail.com>
---
This will be used by the following patch. I actually already have sent
this one in, as suggestion for some problem back then. It is a bit
generic, so it gets its own patch.
cache.h | 2 ++
path.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 61 insertions(+), 0 deletions(-)
diff --git a/cache.h b/cache.h
index 054f106..25ce5da 100644
--- a/cache.h
+++ b/cache.h
@@ -383,6 +383,8 @@ static inline int is_absolute_path(const char *path)
return path[0] == '/';
}
const char *make_absolute_path(const char *path);
+/* Returns the analog of "cd path" from a directory "cwd" */
+extern char *pathexpand(const char *cwd, const char *path);
/* Read and unpack a sha1 file into memory, write memory to a sha1 file */
extern int sha1_object_info(const unsigned char *, unsigned long *);
diff --git a/path.c b/path.c
index 4260952..8231cd8 100644
--- a/path.c
+++ b/path.c
@@ -353,3 +353,62 @@ const char *make_absolute_path(const char *path)
return buf;
}
+
+/*
+ * Returns the analog of "cd path" from a directory "cwd".
+ * The root is defined as empty path (instead of "/")
+ * An attempt to go past the root (with "..") leaves the path at root.
+ * The cwd is not expanded.
+ */
+char *pathexpand(const char *cwd, const char *path)
+{
+ static const char SEP[] = "/";
+ if (!*path) /* empty path -> "." (don't move) */
+ path = ".";
+ if (!cwd || !*cwd || *SEP == *path) /* no cwd, or path begins with "/" */
+ cwd = "";
+
+ while (*cwd && *SEP == *cwd)
+ ++cwd;
+
+ size_t len = strlen(cwd);
+ char *out = malloc(len + 1 + strlen(path) + 1);
+ char *p = strcpy(out, cwd) + len;
+
+ for (; *path; ++path)
+ {
+ char *pl;
+ if (p > out && p[-1] != *SEP)
+ *p++ = *SEP;
+ pl = p;
+ while (*path && *SEP != *path)
+ *p++ = *path++;
+ *p = '\0';
+ /* ..."//"... */
+ if (p == pl)
+ ; /* just ignore */
+ /* ..."/./"... */
+ else if ( p - pl == 1 && '.' == *pl )
+ --p; /* just ignore */
+ /* ..."/../"... */
+ else if ( p - pl == 2 && '.' == pl[0] && '.' == pl[1] )
+ {
+ /* drop the last element of the resulting path */
+ if (pl > out && --pl > out)
+ for (--pl; pl > out && *SEP != *pl; --pl)
+ ;
+ p = pl > out ? ++pl: out;
+ }
+ /* ..."/path/"... */
+ else if (*path)
+ *p++ = *path; /* just add the separator */
+
+ if (!*path)
+ break;
+ }
+ if (p > out+1 && *SEP == p[-1])
+ --p;
+ *p = '\0';
+ return out;
+}
+
--
1.5.4.rc0.86.g30f5
next prev parent reply other threads:[~2007-12-18 20:50 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-12-18 17:33 log/show: relative pathnames do not work in rev:path Alex Riesen
2007-12-18 17:50 ` Jakub Narebski
2007-12-18 20:46 ` Alex Riesen
2007-12-18 20:47 ` [PATCH] Simple support for tree entry specification with relative pathnames Alex Riesen
2007-12-18 20:49 ` Alex Riesen [this message]
2007-12-18 20:52 ` [PATCH] Use pathexpand to preparse the relative pathnames in blob references Alex Riesen
2007-12-18 21:06 ` Dana How
2007-12-19 14:37 ` Jeff King
2007-12-18 21:03 ` [PATCH] Simple support for tree entry specification with relative pathnames Dana How
2007-12-18 21:17 ` Alex Riesen
[not found] ` <56b7f5510712181539g27bd4fc9y632ebe74d91b8e82@mail.gmail.com>
2007-12-19 7:36 ` Alex Riesen
2007-12-18 21:24 ` log/show: relative pathnames do not work in rev:path Jakub Narebski
2007-12-18 21:53 ` Linus Torvalds
2007-12-18 22:08 ` Dana How
2007-12-18 22:29 ` Alex Riesen
2007-12-18 22:20 ` Junio C Hamano
2007-12-18 22:30 ` Dana How
2007-12-18 23:02 ` Johannes Schindelin
2007-12-19 20:45 ` Junio C Hamano
2007-12-18 22:20 ` Alex Riesen
2007-12-18 22:43 ` Johannes Schindelin
2007-12-18 23:03 ` Dana How
2007-12-18 23:26 ` Johannes Schindelin
2007-12-19 1:16 ` Linus Torvalds
2007-12-19 1:52 ` Dana How
2007-12-19 7:42 ` Alex Riesen
2007-12-19 11:23 ` Jakub Narebski
2007-12-19 17:21 ` Dana How
2007-12-19 18:47 ` Jakub Narebski
2007-12-19 13:40 ` [PATCH v0] sha1_name: grok <revision>:./<relative-path> Johannes Schindelin
2007-12-19 15:05 ` Jeff King
2007-12-19 17:40 ` Dana How
2007-12-19 18:09 ` Alex Riesen
2007-12-20 1:07 ` Junio C Hamano
2007-12-20 10:51 ` Johannes Schindelin
2007-12-21 14:17 ` Nguyen Thai Ngoc Duy
2007-12-21 17:50 ` Junio C Hamano
2007-12-21 20:15 ` Nguyen Thai Ngoc Duy
2007-12-22 14:33 ` Johannes Schindelin
2007-12-18 23:11 ` log/show: relative pathnames do not work in rev:path Jakub Narebski
2007-12-18 23:15 ` Dana How
2007-12-18 23:18 ` Junio C Hamano
2007-12-18 23:05 ` Jakub Narebski
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=20071218204947.GE2875@steel.home \
--to=raa.lkml@gmail.com \
--cc=Johannes.Schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=jnareb@gmail.com \
--cc=junkio@cox.net \
--cc=torvalds@linux-foundation.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).