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

[PATCH] Introduce pathexpand: syntax-level chdir into the given cwd

From
Alex Riesen <raa.lkml@gmail.com>
Date
Dec 18, 2007, 20:49 UTC
Message-ID
<20071218204947.GE2875@steel.home>
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
Previous: Alex RiesenNext: Alex Riesen
Message 5 of 43 in “log/show: relative pathnames do not work in rev:path”
  1. Alex RiesenDec 18, 2007
  2. Jakub NarebskiDec 18, 2007
  3. Alex RiesenDec 18, 2007
  4. Simple support for tree entry specification with relative pathnamesAlex Riesen, Dec 18, 2007
  5. Introduce pathexpand: syntax-level chdir into the given cwdAlex Riesen, Dec 18, 2007
  6. Use pathexpand to preparse the relative pathnames in blob referencesAlex Riesen, Dec 18, 2007
  7. Dana HowDec 18, 2007
  8. Jeff KingDec 19, 2007
  9. Dana HowDec 18, 2007
  10. Alex RiesenDec 18, 2007
  11. Jakub NarebskiDec 18, 2007
  12. Linus TorvaldsDec 18, 2007
  13. Dana HowDec 18, 2007
  14. Alex RiesenDec 18, 2007
  15. Junio C HamanoDec 18, 2007
  16. Dana HowDec 18, 2007
  17. Johannes SchindelinDec 18, 2007
  18. Junio C HamanoDec 19, 2007
  19. Alex RiesenDec 18, 2007
  20. Johannes SchindelinDec 18, 2007
  21. Dana HowDec 18, 2007
  22. Johannes SchindelinDec 18, 2007
  23. Linus TorvaldsDec 19, 2007
  24. Dana HowDec 19, 2007
  25. Alex RiesenDec 19, 2007
  26. Jakub NarebskiDec 19, 2007
  27. Dana HowDec 19, 2007
  28. Jakub NarebskiDec 19, 2007
  29. sha1_name: grok <revision>:./<relative-path>Johannes Schindelin, Dec 19, 2007
  30. Jeff KingDec 19, 2007
  31. Dana HowDec 19, 2007
  32. Alex RiesenDec 19, 2007
  33. Junio C HamanoDec 20, 2007
  34. Johannes SchindelinDec 20, 2007
  35. Nguyen Thai Ngoc DuyDec 21, 2007
  36. Junio C HamanoDec 21, 2007
  37. Nguyen Thai Ngoc DuyDec 21, 2007
  38. Johannes SchindelinDec 22, 2007
  39. Jakub NarebskiDec 18, 2007
  40. Dana HowDec 18, 2007
  41. Junio C HamanoDec 18, 2007
  42. Jakub NarebskiDec 18, 2007
  43. Alex RiesenDec 19, 2007

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.