git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Add git-name-rev
@ 2005-10-25 23:05 Johannes Schindelin
  2005-10-26  8:36 ` Petr Baudis
  0 siblings, 1 reply; 4+ messages in thread
From: Johannes Schindelin @ 2005-10-25 23:05 UTC (permalink / raw)
  To: git, junkio

git-name-rev tries to find nice symbolic names for commits. It does so by 
walking the commits from the refs. When the symbolic name is ambiguous, 
the following heuristic is applied: Try to avoid too many ~'s, and if two 
ambiguous names have the same count of ~'s, take the one whose last number 
is smaller.

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>

---

	This is a toy which may help you, too. In case it is still not 
	clear what it does:

	$ git-name-rev 33db5f4d9027a10e477ccf054b2c1ab94f74c85a

	33db5f4d9027a10e477ccf054b2c1ab94f74c85a v0.99^0~940

	A thing like this is very useful for debugging git-fetch-pack: If 
	you see that a certain rev is common, you can now understand in 
	more detail where this rev is.

 .gitignore                     |    1 
 Documentation/git-name-rev.txt |   47 ++++++++++++
 Makefile                       |    2 
 name-rev.c                     |  162 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 211 insertions(+), 1 deletions(-)
 create mode 100644 Documentation/git-name-rev.txt
 create mode 100644 name-rev.c

applies-to: 2152f0960114f638d74c4919a850dd597963ac52
bc2caae16f461e3b496d27e3f038cbb6d141706c
diff --git a/.gitignore b/.gitignore
index 52cb9e2..fe16651 100644
--- a/.gitignore
+++ b/.gitignore
@@ -54,6 +54,7 @@ git-merge-recursive
 git-merge-resolve
 git-merge-stupid
 git-mktag
+git-name-rev
 git-octopus
 git-pack-objects
 git-parse-remote
diff --git a/Documentation/git-name-rev.txt b/Documentation/git-name-rev.txt
new file mode 100644
index 0000000..6a7bb07
--- /dev/null
+++ b/Documentation/git-name-rev.txt
@@ -0,0 +1,47 @@
+git-name-rev(1)
+===============
+
+NAME
+----
+git-name-rev - Find symbolic names for given revs.
+
+
+SYNOPSIS
+--------
+'git-name-rev' <commitish>...
+
+DESCRIPTION
+-----------
+Finds symbolic names suitable for human digestion for revisions given in any
+format parsable by git-rev-parse.
+
+
+EXAMPLE
+-------
+
+Given a commit, find out where it is relative to the local refs. Say somebody
+wrote you about that phantastic commit 33db5f4d9027a10e477ccf054b2c1ab94f74c85a.
+Of course, you look into the commit, but that only tells you what happened, but
+not the context.
+
+Enter git-name-rev:
+
+------------
+% git name-rev 33db5f4d9027a10e477ccf054b2c1ab94f74c85a
+------------
+
+Now you are wiser, because you know that it happened 940 revisions before v0.99.
+
+
+Author
+------
+Written by Johannes Schindelin <Johannes.Schindelin@gmx.de>
+
+Documentation
+--------------
+Documentation by Johannes Schindelin.
+
+GIT
+---
+Part of the gitlink:git[7] suite
+
diff --git a/Makefile b/Makefile
index d6fd706..4877abb 100644
--- a/Makefile
+++ b/Makefile
@@ -122,7 +122,7 @@ PROGRAMS = \
 	git-unpack-objects$X git-update-index$X git-update-server-info$X \
 	git-upload-pack$X git-verify-pack$X git-write-tree$X \
 	git-update-ref$X git-symbolic-ref$X git-check-ref-format$X \
-	$(SIMPLE_PROGRAMS)
+	git-name-rev$X $(SIMPLE_PROGRAMS)
 
 # Backward compatibility -- to be removed after 1.0
 PROGRAMS += git-ssh-pull$X git-ssh-push$X
diff --git a/name-rev.c b/name-rev.c
new file mode 100644
index 0000000..a3a095e
--- /dev/null
+++ b/name-rev.c
@@ -0,0 +1,162 @@
+#include <stdlib.h>
+#include "cache.h"
+#include "commit.h"
+#include "tag.h"
+#include "refs.h"
+
+static const char name_rev_usage[] =
+	"git-name-rev commitish [commitish...]\n";
+
+typedef struct rev_name {
+	const char *tip_name;
+	int merge_traversals;
+	int generation;
+} rev_name;
+
+static long cutoff = LONG_MAX;
+
+static void name_rev(struct commit *commit,
+		const char *tip_name, int merge_traversals, int generation,
+		int deref)
+{
+	struct rev_name *name = (struct rev_name *)commit->object.util;
+	struct commit_list *parents;
+	int parent_number = 0;
+
+	if (!commit->object.parsed)
+		parse_commit(commit);
+
+	if (commit->date < cutoff)
+		return;
+
+	if (deref) {
+		char *new_name = xmalloc(strlen(tip_name)+3);
+		strcpy(new_name, tip_name);
+		strcat(new_name, "^0");
+		tip_name = new_name;
+
+		if (generation)
+			die("generation: %d, but deref?", generation);
+	}
+
+	if (name == NULL) {
+		name = xmalloc(sizeof(rev_name));
+		commit->object.util = name;
+		goto copy_data;
+	} else if (name->merge_traversals > merge_traversals ||
+			(name->merge_traversals == merge_traversals &&
+			 name->generation > generation)) {
+copy_data:
+		name->tip_name = tip_name;
+		name->merge_traversals = merge_traversals;
+		name->generation = generation;
+	} else
+		return;
+
+	for (parents = commit->parents;
+			parents;
+			parents = parents->next, parent_number++) {
+		if (parent_number > 0) {
+			char *new_name = xmalloc(strlen(tip_name)+8);
+
+			if (generation > 0)
+				sprintf(new_name, "%s~%d^%d", tip_name,
+						generation, parent_number);
+			else
+				sprintf(new_name, "%s^%d", tip_name, parent_number);
+
+			name_rev(parents->item, new_name,
+				merge_traversals + 1 , 0, 0);
+		} else {
+			name_rev(parents->item, tip_name, merge_traversals,
+				generation + 1, 0);
+		}
+	}
+}
+
+static int name_ref(const char *path, const unsigned char *sha1)
+{
+	struct object *o = parse_object(sha1);
+	int deref = 0;
+
+	while (o && o->type == tag_type) {
+		struct tag *t = (struct tag *) o;
+		if (!t->tagged)
+			break; /* broken repository */
+		o = parse_object(t->tagged->sha1);
+		deref = 1;
+	}
+	if (o && o->type == commit_type) {
+		struct commit *commit = (struct commit *)o;
+		const char *p;
+
+		while ((p = strchr(path, '/')))
+			path = p+1;
+
+		name_rev(commit, strdup(path), 0, 0, deref);
+	}
+	return 0;
+}
+
+int main(int argc, char **argv)
+{
+	struct object_list *revs = NULL;
+	struct object_list **walker = &revs;
+	int as_is = 0;
+
+	setup_git_directory();
+
+	if (argc < 2)
+		usage(name_rev_usage);
+
+	for (--argc, ++argv; argc; --argc, ++argv) {
+		unsigned char sha1[20];
+		struct object *o;
+		struct commit *commit;
+
+		if (!as_is && (*argv)[0] == '-') {
+			if (!strcmp(*argv, "--")) {
+				as_is = 1;
+				continue;
+			}
+
+			usage(name_rev_usage);
+		}
+
+		if (get_sha1(*argv, sha1)) {
+			fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
+					*argv);
+			continue;
+		}
+
+		o = deref_tag(parse_object(sha1));
+		if (!o || o->type != commit_type) {
+			fprintf(stderr, "Could not get commit for %s. Skipping.\n",
+					*argv);
+			continue;
+		}
+
+		commit = (struct commit *)o;
+
+		if (cutoff > commit->date)
+			cutoff = commit->date;
+
+		object_list_append((struct object *)commit, walker);
+		(*walker)->name = *argv;
+		walker = &((*walker)->next);
+	}
+
+	for_each_ref(name_ref);
+
+	for ( ; revs; revs = revs->next) {
+		struct rev_name *n = (struct rev_name *)revs->item->util;
+
+		printf("%s %s", revs->name, n->tip_name);
+		if (n->generation)
+			printf("~%d", n->generation);
+		printf("\n");
+	}
+
+	return 0;
+}
+
---
0.99.8.GIT

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Add git-name-rev
  2005-10-25 23:05 [PATCH] Add git-name-rev Johannes Schindelin
@ 2005-10-26  8:36 ` Petr Baudis
  2005-10-26 13:10   ` Johannes Schindelin
  0 siblings, 1 reply; 4+ messages in thread
From: Petr Baudis @ 2005-10-26  8:36 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git, junkio

Dear diary, on Wed, Oct 26, 2005 at 01:05:59AM CEST, I got a letter
where Johannes Schindelin <Johannes.Schindelin@gmx.de> told me that...
> git-name-rev tries to find nice symbolic names for commits. It does so by 
> walking the commits from the refs. When the symbolic name is ambiguous, 
> the following heuristic is applied: Try to avoid too many ~'s, and if two 
> ambiguous names have the same count of ~'s, take the one whose last number 
> is smaller.
> 
> Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>

I think you should either add this to git-findtags.perl or (better) add
git-findtags.perl's functionality (i.e. a switch to search only in the
tags) to this and obsolete/kill git-findtags.perl. It is pretty new
(from Oct 13) so killing it shouldn't break anything.

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
VI has two modes: the one in which it beeps and the one in which
it doesn't.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Add git-name-rev
  2005-10-26  8:36 ` Petr Baudis
@ 2005-10-26 13:10   ` Johannes Schindelin
  2005-10-27  8:53     ` Matthias Urlichs
  0 siblings, 1 reply; 4+ messages in thread
From: Johannes Schindelin @ 2005-10-26 13:10 UTC (permalink / raw)
  To: Petr Baudis; +Cc: git, junkio

[PATCH] Add git-name-rev

git-name-rev tries to find nice symbolic names for commits. It does so by
walking the commits from the refs. When the symbolic name is ambiguous, the
following heuristic is applied: Try to avoid too many ~'s, and if two ambiguous
names have the same count of ~'s, take the one whose last number is smaller.

With "--tags", the names are derived only from tags.

With "--stdin", the stdin is parsed, and after every sha1 for which a name
could be found, the name is appended. (Try "git log | git name-rev --stdin".)

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
---

	On Wed, 26 Oct 2005, Petr Baudis wrote:

	> I think you should either add this to git-findtags.perl or 
	> (better) add git-findtags.perl's functionality (i.e. a switch to 
	> search only in the tags) to this and obsolete/kill 
	> git-findtags.perl. It is pretty new (from Oct 13) so killing it 
	> shouldn't break anything.

	Ask, and it shall be given you.

 Documentation/git-name-rev.txt |   66 +++++++++++
 Makefile                       |    2 
 name-rev.c                     |  246 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 313 insertions(+), 1 deletions(-)

diff --git a/Documentation/git-name-rev.txt b/Documentation/git-name-rev.txt
new file mode 100644
index 0000000..e37b0b8
--- /dev/null
+++ b/Documentation/git-name-rev.txt
@@ -0,0 +1,66 @@
+git-name-rev(1)
+===============
+
+NAME
+----
+git-name-rev - Find symbolic names for given revs.
+
+
+SYNOPSIS
+--------
+'git-name-rev' [--tags] ( --all | --stdin | <commitish>... )
+
+DESCRIPTION
+-----------
+Finds symbolic names suitable for human digestion for revisions given in any
+format parsable by git-rev-parse.
+
+
+OPTIONS
+-------
+
+--tags::
+	Do not use branch names, but only tags to name the commits
+
+--all::
+	List all commits reachable from all refs
+
+--stdin::
+	Read from stdin, append "(<rev_name>)" to all sha1's of name'able
+	commits, and pass to stdout
+
+EXAMPLE
+-------
+
+Given a commit, find out where it is relative to the local refs. Say somebody
+wrote you about that phantastic commit 33db5f4d9027a10e477ccf054b2c1ab94f74c85a.
+Of course, you look into the commit, but that only tells you what happened, but
+not the context.
+
+Enter git-name-rev:
+
+------------
+% git name-rev 33db5f4d9027a10e477ccf054b2c1ab94f74c85a
+------------
+
+Now you are wiser, because you know that it happened 940 revisions before v0.99.
+
+Another nice thing you can do is:
+
+------------
+% git log | git name-rev --stdin
+------------
+
+
+Author
+------
+Written by Johannes Schindelin <Johannes.Schindelin@gmx.de>
+
+Documentation
+--------------
+Documentation by Johannes Schindelin.
+
+GIT
+---
+Part of the gitlink:git[7] suite
+
diff --git a/Makefile b/Makefile
--- a/Makefile
+++ b/Makefile
@@ -122,7 +122,7 @@ PROGRAMS = \
 	git-unpack-objects$X git-update-index$X git-update-server-info$X \
 	git-upload-pack$X git-verify-pack$X git-write-tree$X \
 	git-update-ref$X git-symbolic-ref$X git-check-ref-format$X \
-	$(SIMPLE_PROGRAMS)
+	git-name-rev$X $(SIMPLE_PROGRAMS)
 
 # Backward compatibility -- to be removed after 1.0
 PROGRAMS += git-ssh-pull$X git-ssh-push$X
diff --git a/name-rev.c b/name-rev.c
new file mode 100644
index 0000000..21fecdf
--- /dev/null
+++ b/name-rev.c
@@ -0,0 +1,246 @@
+#include <stdlib.h>
+#include "cache.h"
+#include "commit.h"
+#include "tag.h"
+#include "refs.h"
+
+static const char name_rev_usage[] =
+	"git-name-rev [--tags] ( --all | --stdin | commitish [commitish...] )\n";
+
+typedef struct rev_name {
+	const char *tip_name;
+	int merge_traversals;
+	int generation;
+} rev_name;
+
+static long cutoff = LONG_MAX;
+
+static void name_rev(struct commit *commit,
+		const char *tip_name, int merge_traversals, int generation,
+		int deref)
+{
+	struct rev_name *name = (struct rev_name *)commit->object.util;
+	struct commit_list *parents;
+	int parent_number = 0;
+
+	if (!commit->object.parsed)
+		parse_commit(commit);
+
+	if (commit->date < cutoff)
+		return;
+
+	if (deref) {
+		char *new_name = xmalloc(strlen(tip_name)+3);
+		strcpy(new_name, tip_name);
+		strcat(new_name, "^0");
+		tip_name = new_name;
+
+		if (generation)
+			die("generation: %d, but deref?", generation);
+	}
+
+	if (name == NULL) {
+		name = xmalloc(sizeof(rev_name));
+		commit->object.util = name;
+		goto copy_data;
+	} else if (name->merge_traversals > merge_traversals ||
+			(name->merge_traversals == merge_traversals &&
+			 name->generation > generation)) {
+copy_data:
+		name->tip_name = tip_name;
+		name->merge_traversals = merge_traversals;
+		name->generation = generation;
+	} else
+		return;
+
+	for (parents = commit->parents;
+			parents;
+			parents = parents->next, parent_number++) {
+		if (parent_number > 0) {
+			char *new_name = xmalloc(strlen(tip_name)+8);
+
+			if (generation > 0)
+				sprintf(new_name, "%s~%d^%d", tip_name,
+						generation, parent_number);
+			else
+				sprintf(new_name, "%s^%d", tip_name, parent_number);
+
+			name_rev(parents->item, new_name,
+				merge_traversals + 1 , 0, 0);
+		} else {
+			name_rev(parents->item, tip_name, merge_traversals,
+				generation + 1, 0);
+		}
+	}
+}
+
+static int tags_only = 0;
+
+static int name_ref(const char *path, const unsigned char *sha1)
+{
+	struct object *o = parse_object(sha1);
+	int deref = 0;
+
+	if (tags_only && strncmp(path, "refs/tags/", 10))
+		return 0;
+
+	while (o && o->type == tag_type) {
+		struct tag *t = (struct tag *) o;
+		if (!t->tagged)
+			break; /* broken repository */
+		o = parse_object(t->tagged->sha1);
+		deref = 1;
+	}
+	if (o && o->type == commit_type) {
+		struct commit *commit = (struct commit *)o;
+		const char *p;
+
+		while ((p = strchr(path, '/')))
+			path = p+1;
+
+		name_rev(commit, strdup(path), 0, 0, deref);
+	}
+	return 0;
+}
+
+/* returns a static buffer */
+static const char* get_rev_name(struct object *o)
+{
+	static char buffer[1024];
+	struct rev_name *n = (struct rev_name *)o->util;
+	if (!n)
+		return "undefined";
+
+	if (!n->generation)
+		return n->tip_name;
+
+	snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
+
+	return buffer;
+}
+	
+int main(int argc, char **argv)
+{
+	struct object_list *revs = NULL;
+	struct object_list **walker = &revs;
+	int as_is = 0, all = 0, transform_stdin = 0;
+
+	setup_git_directory();
+
+	if (argc < 2)
+		usage(name_rev_usage);
+
+	for (--argc, ++argv; argc; --argc, ++argv) {
+		unsigned char sha1[20];
+		struct object *o;
+		struct commit *commit;
+
+		if (!as_is && (*argv)[0] == '-') {
+			if (!strcmp(*argv, "--")) {
+				as_is = 1;
+				continue;
+			} else if (!strcmp(*argv, "--tags")) {
+				tags_only = 1;
+				continue;
+			} else if (!strcmp(*argv, "--all")) {
+				if (argc > 1)
+					die("Specify either a list, or --all, not both!");
+				all = 1;
+				cutoff = 0;
+				continue;
+			} else if (!strcmp(*argv, "--stdin")) {
+				if (argc > 1)
+					die("Specify either a list, or --stdin, not both!");
+				transform_stdin = 1;
+				cutoff = 0;
+				continue;
+			}
+			usage(name_rev_usage);
+		}
+
+		if (get_sha1(*argv, sha1)) {
+			fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
+					*argv);
+			continue;
+		}
+
+		o = deref_tag(parse_object(sha1));
+		if (!o || o->type != commit_type) {
+			fprintf(stderr, "Could not get commit for %s. Skipping.\n",
+					*argv);
+			continue;
+		}
+
+		commit = (struct commit *)o;
+
+		if (cutoff > commit->date)
+			cutoff = commit->date;
+
+		object_list_append((struct object *)commit, walker);
+		(*walker)->name = *argv;
+		walker = &((*walker)->next);
+	}
+
+	for_each_ref(name_ref);
+
+	if (transform_stdin) {
+		char buffer[2048];
+		char *p, *p_start;
+
+		while (!feof(stdin)) {
+			int forty = 0;
+			p = fgets(buffer, sizeof(buffer), stdin);
+			if (!p)
+				break;
+
+			for (p_start = p; *p; p++) {
+#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
+				if (!ishex(*p))
+					forty = 0;
+				else if (++forty == 40 &&
+						!ishex(*(p+1))) {
+					unsigned char sha1[40];
+					const char *name = "undefined";
+					char c = *(p+1);
+
+					forty = 0;
+
+					*(p+1) = 0;
+					if (!get_sha1(p - 39, sha1)) {
+						struct object *o =
+							lookup_object(sha1);
+						if (o)
+							name = get_rev_name(o);
+					}
+					*(p+1) = c;
+
+					if (!strcmp(name, "undefined"))
+						continue;
+
+					fwrite(p_start, p - p_start, 1, stdout);
+					fputc('(', stdout);
+					fputs(name, stdout);
+					fputc(')', stdout);
+					p_start = p + 1;
+				}
+			}
+
+			/* flush */
+			if (p_start != p)
+				fwrite(p_start, p - p_start, 1, stdout);
+		}
+	} else if (all) {
+		extern struct object **objs;
+		extern int nr_objs;
+		int i;
+
+		for (i = 0; i < nr_objs; i++)
+			printf("%s %s\n", sha1_to_hex(objs[i]->sha1),
+					get_rev_name(objs[i]));
+	} else
+		for ( ; revs; revs = revs->next)
+			printf("%s %s\n", revs->name, get_rev_name(revs->item));
+
+	return 0;
+}
+

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Add git-name-rev
  2005-10-26 13:10   ` Johannes Schindelin
@ 2005-10-27  8:53     ` Matthias Urlichs
  0 siblings, 0 replies; 4+ messages in thread
From: Matthias Urlichs @ 2005-10-27  8:53 UTC (permalink / raw)
  To: git

Hi, Johannes Schindelin wrote:

> [PATCH] Add git-name-rev

Please update the "git" manpage when you add a new command.
(I bet that others are missing too..?)

-- 
Matthias Urlichs   |   {M:U} IT Design @ m-u-it.de   |  smurf@smurf.noris.de
Disclaimer: The quote was selected randomly. Really. | http://smurf.noris.de
 - -
Let the machine do the dirty work.
		-- "Elements of Programming Style", Kernighan and Ritchie

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2005-10-27  9:02 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-10-25 23:05 [PATCH] Add git-name-rev Johannes Schindelin
2005-10-26  8:36 ` Petr Baudis
2005-10-26 13:10   ` Johannes Schindelin
2005-10-27  8:53     ` Matthias Urlichs

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).