@matheusbsbs / matheus-bagliano / commits / 28a4d94044
Fork 0

object name: introduce ':/<oneline prefix>' notation

To name a commit, you can now say $ git rev-parse ':/Initial revision of "git"' and it will return the hash of the youngest commit whose commit message (the oneline) begins with the given prefix. For future extension, a leading exclamation mark is treated specially: if you want to match a commit message starting with a '!', just repeat the exclamation mark. So, to match a commit which starts with '!Hello World', use $ git show ':/!!Hello World' Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Feb 24, 2007 at 03:08 UTC 28a4d940443806412effa246ecc7768a21553ec7
2 files changed +65
Documentation/git-rev-parse.txt
+7
@@ -190,6 +190,13 @@ blobs contained in a commit.
190 and dereference the tag recursively until a non-tag object is
191 found.
192
193 +* A colon, followed by a slash, followed by a text: this names
194 + a commit whose commit message starts with the specified text.
195 + This name returns the youngest matching commit which is
196 + reachable from any ref. If the commit message starts with a
197 + '!', you have to repeat that; the special sequence ':/!',
198 + followed by something else than '!' is reserved for now.
199 +
200 * A suffix ':' followed by a path; this names the blob or tree
201 at the given path in the tree-ish object named by the part
202 before the colon.
sha1_name.c
+58
@@ -577,6 +577,62 @@ static int get_sha1_1(const char *name, int len, unsigned char *sha1)
577 return get_short_sha1(name, len, sha1, 0);
578 }
579
580 +static int handle_one_ref(const char *path,
581 + const unsigned char *sha1, int flag, void *cb_data)
582 +{
583 + struct commit_list **list = cb_data;
584 + struct object *object = parse_object(sha1);
585 + if (!object)
586 + return 0;
587 + if (object->type == OBJ_TAG)
588 + object = deref_tag(object, path, strlen(path));
589 + if (object->type != OBJ_COMMIT)
590 + return 0;
591 + insert_by_date((struct commit *)object, list);
592 + return 0;
593 +}
594 +
595 +/*
596 + * This interprets names like ':/Initial revision of "git"' by searching
597 + * through history and returning the first commit whose message starts
598 + * with the given string.
599 + *
600 + * For future extension, ':/!' is reserved. If you want to match a message
601 + * beginning with a '!', you have to repeat the exclamation mark.
602 + */
603 +
604 +#define ONELINE_SEEN (1u<<20)
605 +int get_sha1_oneline(const char *prefix, unsigned char *sha1)
606 +{
607 + struct commit_list *list = NULL, *backup = NULL, *l;
608 + struct commit *commit;
609 +
610 + if (prefix[0] == '!') {
611 + if (prefix[1] != '!')
612 + die ("Invalid search pattern: %s", prefix);
613 + prefix++;
614 + }
615 + if (!save_commit_buffer)
616 + return error("Could not expand oneline-name.");
617 + for_each_ref(handle_one_ref, &list);
618 + for (l = list; l; l = l->next)
619 + commit_list_insert(l->item, &backup);
620 + while ((commit = pop_most_recent_commit(&list, ONELINE_SEEN))) {
621 + char *p;
622 + parse_object(commit->object.sha1);
623 + if (!commit->buffer || !(p = strstr(commit->buffer, "\n\n")))
624 + continue;
625 + if (!prefixcmp(p + 2, prefix)) {
626 + hashcpy(sha1, commit->object.sha1);
627 + break;
628 + }
629 + }
630 + free_commit_list(list);
631 + for (l = backup; l; l = l->next)
632 + clear_commit_marks(l->item, ONELINE_SEEN);
633 + return commit == NULL;
634 +}
635 +
636 /*
637 * This is like "get_sha1_basic()", except it allows "sha1 expressions",
638 * notably "xyz^" for "parent of xyz"
@@ -600,6 +656,8 @@ int get_sha1(const char *name, unsigned char *sha1)
656 int stage = 0;
657 struct cache_entry *ce;
658 int pos;
659 + if (namelen > 2 && name[1] == '/')
660 + return get_sha1_oneline(name + 2, sha1);
661 if (namelen < 3 ||
662 name[2] != ':' ||
663 name[1] < '0' || '3' < name[1])