Show a patch.

GET /api/1.1/patches/2652/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 2652,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/2652/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/2652/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20200116182603.108966-3-nicolas@ndufresne.ca>",
    "date": "2020-01-16T18:26:03",
    "name": "[libcamera-devel,2/2] checkstyle: Add support for pre-commit hook",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "17dae14c8e5bf4e1c3776b895168adff549bc2bb",
    "submitter": {
        "id": 30,
        "url": "https://patchwork.libcamera.org/api/1.1/people/30/?format=api",
        "name": "Nicolas Dufresne",
        "email": "nicolas@ndufresne.ca"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/2652/mbox/",
    "series": [
        {
            "id": 628,
            "url": "https://patchwork.libcamera.org/api/1.1/series/628/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=628",
            "date": "2020-01-16T18:26:01",
            "name": "Add the ability to do pre-commit style check",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/628/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/2652/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/2652/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<nicolas@ndufresne.ca>",
        "Received": [
            "from bhuna.collabora.co.uk (bhuna.collabora.co.uk [46.235.227.227])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 46E1260705\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 16 Jan 2020 19:26:31 +0100 (CET)",
            "from nicolas-tpx395.localdomain (unknown [IPv6:2610:98:8005::127])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256\n\tbits))\n\t(No client certificate requested) (Authenticated sender: nicolas)\n\tby bhuna.collabora.co.uk (Postfix) with ESMTPSA id C7048294522;\n\tThu, 16 Jan 2020 18:26:30 +0000 (GMT)"
        ],
        "From": "nicolas@ndufresne.ca",
        "To": "libcamera-devel@lists.libcamera.org",
        "Cc": "Nicolas Dufresne <nicolas.dufresne@collabora.com>",
        "Date": "Thu, 16 Jan 2020 13:26:03 -0500",
        "Message-Id": "<20200116182603.108966-3-nicolas@ndufresne.ca>",
        "X-Mailer": "git-send-email 2.24.1",
        "In-Reply-To": "<20200116182603.108966-1-nicolas@ndufresne.ca>",
        "References": "<20200116182603.108966-1-nicolas@ndufresne.ca>",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH 2/2] checkstyle: Add support for\n\tpre-commit hook",
        "X-BeenThere": "libcamera-devel@lists.libcamera.org",
        "X-Mailman-Version": "2.1.29",
        "Precedence": "list",
        "List-Id": "<libcamera-devel.lists.libcamera.org>",
        "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>",
        "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>",
        "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>",
        "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>",
        "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>",
        "X-List-Received-Date": "Thu, 16 Jan 2020 18:26:31 -0000"
    },
    "content": "From: Nicolas Dufresne <nicolas.dufresne@collabora.com>\n\nThis adds support for pre-commit hook workflow. In pre-commit hook we\ncheck the style on the changes currently staged. Note that this patch\nalso includes a bit of sugar to support --amend.\n\nSigned-off-by: Nicolas Dufresne <nicolas.dufresne@collabora.com>\n---\n utils/checkstyle.py    | 55 +++++++++++++++++++++++++++++-------------\n utils/hooks/pre-commit | 16 ++++++++++++\n 2 files changed, 54 insertions(+), 17 deletions(-)\n mode change 100644 => 100755 utils/checkstyle.py\n create mode 100755 utils/hooks/pre-commit",
    "diff": "diff --git a/utils/checkstyle.py b/utils/checkstyle.py\nold mode 100644\nnew mode 100755\nindex 7edea25..23eb3f6\n--- a/utils/checkstyle.py\n+++ b/utils/checkstyle.py\n@@ -458,11 +458,16 @@ class StripTrailingSpaceFormatter(Formatter):\n # Style checking\n #\n \n-def check_file(top_level, commit, filename):\n+def check_file(top_level, commit, filename, staged):\n     # Extract the line numbers touched by the commit.\n-    diff = subprocess.run(['git', 'diff', '%s~..%s' % (commit, commit), '--',\n-                           '%s/%s' % (top_level, filename)],\n-                          stdout=subprocess.PIPE).stdout\n+    if staged:\n+        diff = subprocess.run(['git', 'diff', '--staged', commit, '--',\n+                               '%s/%s' % (top_level, filename)],\n+                              stdout=subprocess.PIPE).stdout\n+    else:\n+        diff = subprocess.run(['git', 'diff', '%s~..%s' % (commit, commit), '--',\n+                               '%s/%s' % (top_level, filename)],\n+                              stdout=subprocess.PIPE).stdout\n     diff = diff.decode('utf-8').splitlines(True)\n     commit_diff = parse_diff(diff)\n \n@@ -476,8 +481,12 @@ def check_file(top_level, commit, filename):\n \n     # Format the file after the commit with all formatters and compute the diff\n     # between the unformatted and formatted contents.\n-    after = subprocess.run(['git', 'show', '%s:%s' % (commit, filename)],\n-                           stdout=subprocess.PIPE).stdout\n+    if staged:\n+        after = subprocess.run(['git', 'show', ':%s' % (filename)],\n+                               stdout=subprocess.PIPE).stdout\n+    else:\n+        after = subprocess.run(['git', 'show', '%s:%s' % (commit, filename)],\n+                               stdout=subprocess.PIPE).stdout\n     after = after.decode('utf-8')\n \n     formatted = after\n@@ -521,13 +530,20 @@ def check_file(top_level, commit, filename):\n     return len(formatted_diff) + len(issues)\n \n \n-def check_style(top_level, commit):\n-    # Get the commit title and list of files.\n-    ret = subprocess.run(['git', 'show', '--pretty=oneline','--name-only', commit],\n-                         stdout=subprocess.PIPE)\n-    files = ret.stdout.decode('utf-8').splitlines()\n-    title = files[0]\n-    files = files[1:]\n+def check_style(top_level, commit, staged):\n+    if staged:\n+        # Get list of staged changed files\n+        ret = subprocess.run(['git', 'diff', '--staged', '--name-only', commit],\n+                             stdout=subprocess.PIPE)\n+        files = ret.stdout.decode('utf-8').splitlines()\n+        title = \"Pre-commit\"\n+    else:\n+        # Get the commit title and list of files.\n+        ret = subprocess.run(['git', 'show', '--pretty=oneline', '--name-only', commit],\n+                             stdout=subprocess.PIPE)\n+        files = ret.stdout.decode('utf-8').splitlines()\n+        title = files[0]\n+        files = files[1:]\n \n     separator = '-' * len(title)\n     print(separator)\n@@ -541,11 +557,11 @@ def check_style(top_level, commit):\n     files = [f for f in files if len([p for p in patterns if fnmatch.fnmatch(os.path.basename(f), p)])]\n     if len(files) == 0:\n         print(\"Commit doesn't touch source files, skipping\")\n-        return\n+        return 0\n \n     issues = 0\n     for f in files:\n-        issues += check_file(top_level, commit, f)\n+        issues += check_file(top_level, commit, f, staged)\n \n     if issues == 0:\n         print(\"No style issue detected\")\n@@ -554,6 +570,8 @@ def check_style(top_level, commit):\n         print(\"%u potential style %s detected, please review\" % \\\n                 (issues, 'issue' if issues == 1 else 'issues'))\n \n+    return issues\n+\n \n def extract_revlist(revs):\n     \"\"\"Extract a list of commits on which to operate from a revision or revision\n@@ -595,6 +613,8 @@ def main(argv):\n     parser = argparse.ArgumentParser()\n     parser.add_argument('--formatter', '-f', type=str, choices=['astyle', 'clang-format'],\n                         help='Code formatter. Default to clang-format if not specified.')\n+    parser.add_argument('--staged', '-s', action='store_true',\n+                        help='Looks at the staged changes, used for pre-commit, defaults to False')\n     parser.add_argument('revision_range', type=str, default='HEAD', nargs='?',\n                         help='Revision range (as defined by git rev-parse). Defaults to HEAD if not specified.')\n     args = parser.parse_args(argv[1:])\n@@ -632,11 +652,12 @@ def main(argv):\n \n     revlist = extract_revlist(args.revision_range)\n \n+    issues = 0\n     for commit in revlist:\n-        check_style(top_level, commit)\n+        issues += check_style(top_level, commit, args.staged)\n         print('')\n \n-    return 0\n+    return issues\n \n \n if __name__ == '__main__':\ndiff --git a/utils/hooks/pre-commit b/utils/hooks/pre-commit\nnew file mode 100755\nindex 0000000..57d23ef\n--- /dev/null\n+++ b/utils/hooks/pre-commit\n@@ -0,0 +1,16 @@\n+#!/bin/sh\n+\n+# Execute the checkstyle script before committing any code, failing the commit\n+# if needed. With this workflow, false positive can be ignored using:\n+#   git commit -n\n+#\n+# To utilise this hook, install this file with:\n+#   cp utils/hooks/pre-commit .git/hooks/pre-commit\n+\n+commit=\n+if ps -ocommand= -p $PPID | grep -- \"--amend\"\n+then\n+   commit=\"HEAD~\"\n+fi\n+\n+./utils/checkstyle.py --staged $commit\n",
    "prefixes": [
        "libcamera-devel",
        "2/2"
    ]
}