commit-in-tree
Check whether a commit exists in the local branch by not relying just on the
sha1.
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
diff --git a/README b/README
index fdf607f..caeae44 100644
--- a/README
+++ b/README
@@ -13,3 +13,24 @@
export STABLE_MAJ_VER="3" # The major version of the tree I'm maintaining
export STABLE_MIN_VER="18" # The minor version of the tree I'm maintaining
export OTHER_STABLE_TREES="stable/linux-3.10.y stable/linux-3.14.y stable/linux-4.1.y stable/linux-4.2.y" # Other -stable trees that live within the same repository.
+
+Commands:
+
+1) stable commit-in-tree <commit sha1>
+
+This is useful to find if a given commit exists in the local branch. While in
+the trivial case it would be enough to just check whether the sha1 exists in
+the current branch, but in reality this is slightly more complicated.
+
+Consider the case we're looking at a mainline commit and see "Fixes: <sha1>
+("description"), there's a possiblity that we've pulled in the commit pointed
+by the "fixes" tag earlier, which means that it'll have a different sha1 in
+our branch, in which case we won't see it if we follow the trivial path.
+
+Instead, we'll look up the commit's subject and search by that.
+
+There is still a problem here: since we can't look only in subject lines,
+grepping for a subject line might instead point to a different commit that
+only contains the subject line of the commit we're looking for in it's own
+commit message. In that case, we must do a more expensive search and evaluate
+all commits containing the given subject line.
diff --git a/stable-commit-in-tree b/stable-commit-in-tree
new file mode 100755
index 0000000..4900f48
--- /dev/null
+++ b/stable-commit-in-tree
@@ -0,0 +1,37 @@
+#!/bin/bash
+#
+# Check if a given commit is in the current branch, based on the subject
+# rather than commit sha1.
+#
+
+if [ "$#" -ne 1 ]; then
+ echo "Usage: stable commit-in-tree <commit sha1>"
+ exit 1
+fi
+
+# Grab the subject, since commit sha1 is different between branches we
+# have to look it up based on subject.
+subj=$(git log -1 --pretty="%s" $1)
+if [ $? -gt 0 ]; then
+ exit 0
+fi
+
+# Let's hope for the best: either no commits, or one commit with the same
+# subject. This should be the case most of the time.
+cursubj=$(git log -1 --format="%s" -F --grep "$subj")
+if [ "$cursubj" = "" ]; then
+ exit 0
+fi
+
+if [ "$cursubj" = "$subj" ]; then
+ exit 1
+fi
+
+# Try and find if there's a commit with given subject the hard way
+for i in $(git log --pretty="%H" -F --grep "$subj"); do
+ cursubj=$(git log -1 --format="%s" $i)
+ if [ "$cursubj" = "$subj" ]; then
+ exit 1
+ fi
+done
+exit 0