* [PATCH] scripts: add helper for generating Origin-URL references
@ 2025-12-17 10:35 Ahmad Fatoum
2025-12-17 10:59 ` Jonas Rebmann
2025-12-18 8:01 ` Sascha Hauer
0 siblings, 2 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2025-12-17 10:35 UTC (permalink / raw)
To: barebox; +Cc: Ahmad Fatoum, Jonas Rebmann
To improve tracking where code originated from, e.g. when porting from
Linux or U-Boot, some recent commits have started using Origin-URL
comments. Add a script that can generate them.
Examples in Vim:
# add into highlight URL pointing at specified file
V:!git origin-url ../linux/lib/ucs2_string.c
# as above, but consult buffer extension for comment style
V:!git origin-url ../linux/lib/ucs2_string.c %
# generate in snippet syntax
V:!git origin-url -s ../linux/lib/ucs2_string.c
Cc: Jonas Rebmann <jre@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
I've been using this for my recent ports and I think it's generally
useful and helps with deciding on a common format.
---
scripts/git-origin-url.sh | 94 +++++++++++++++++++++++++++++++++++++++
1 file changed, 94 insertions(+)
create mode 100755 scripts/git-origin-url.sh
diff --git a/scripts/git-origin-url.sh b/scripts/git-origin-url.sh
new file mode 100755
index 000000000000..d31008b1b761
--- /dev/null
+++ b/scripts/git-origin-url.sh
@@ -0,0 +1,94 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: BSD-0-Clause
+
+set -euo pipefail
+
+declare -A repos=(
+ ["a3ffa97f40dc81f2d6b07ee964f2340fe0c1ba97"]="https://git.pengutronix.de/cgit/barebox"
+ ["1da177e4c3f41524e886b7f1b8a0c1fc7321cac2"]="https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git"
+ ["cc8ed39b240180b58810784f844e253263594ac3"]="https://git.busybox.net/busybox"
+ ["7e492d8258182e31c988bbf9917d4a3d41949d56"]="https://github.com/u-boot/u-boot"
+ ["afe1be4dbdf142513f6ac1d92e6a20bdc4b20c80"]="https://github.com/systemd/systemd"
+)
+
+err() { echo "error: $* " >&2; exit 1; }
+usage="Usage: $0 <path-to-file> [destination-file]"
+
+snippet=""
+while getopts "sh" opt; do
+ case "$opt" in
+ s)
+ snippet="Snippet-"
+ ;;
+ h)
+ echo "$usage"
+ exit 0
+ ;;
+ *)
+ err "$usage"
+ ;;
+ esac
+done
+
+shift $((OPTIND-1))
+
+if [ $# -ne 1 ] && [ $# -ne 2 ]; then
+ err "$usage"
+fi
+
+# Optional second argument to allow e.g. !git origin-url $remote_file % im vim
+# In cases where local file has different extension then the original
+dstbasename="$(basename ${2:-$1})"
+
+[ -e "$1" ] || err "File not found"
+
+filepath="$(realpath "$1")"
+
+repo_root="$(git -C "$(dirname "$filepath")" rev-parse --show-toplevel 2>/dev/null \
+ || err "Not inside a Git repository: $filepath")"
+
+rel_path="$(realpath --relative-to="$repo_root" "$filepath")"
+
+# newest commit that touched this file
+file_commit="$(git -C "$repo_root" log -n1 --format=%H -- "$rel_path")"
+
+origin_url=""
+
+# find which repo this file ultimately came from
+for commit in "${!repos[@]}"; do
+ if git -C "$repo_root" merge-base --is-ancestor "$commit" "$file_commit" 2>/dev/null; then
+ origin_url="${repos[$commit]}"
+ break
+ fi
+done
+
+[ -z "$origin_url" ] && err "No matching repo base found for file commit $file_commit"
+
+# output uses the newest file commit, not the origin commit
+if [[ $origin_url == https://github.com/* ]] ; then
+ tree_url="$origin_url/blob/$file_commit/$rel_path"
+else
+ # assume cgit as fallback
+ tree_url="$origin_url/tree/$rel_path?id=$file_commit"
+fi
+
+case "${dstbasename##*.}" in
+ h|S|imxcfg|y|l|lds)
+ comment_begin='/* '
+ comment_end=' */'
+ ;;
+ c|dts*)
+ comment_begin='// '
+ comment_end=''
+ ;;
+ *)
+ comment_begin='# '
+ comment_end=''
+ ;;
+esac
+
+comment() { echo "${comment_begin}${*}${comment_end}"; }
+
+[ -z "${snippet}" ] || comment "SPDX-SnippetBegin"
+comment "SPDX-${snippet}Comment: Origin-URL: ${tree_url}"
+[ -z "${snippet}" ] || comment "SPDX-SnippetEnd"
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] scripts: add helper for generating Origin-URL references
2025-12-17 10:35 [PATCH] scripts: add helper for generating Origin-URL references Ahmad Fatoum
@ 2025-12-17 10:59 ` Jonas Rebmann
2025-12-18 8:04 ` Sascha Hauer
2025-12-18 8:01 ` Sascha Hauer
1 sibling, 1 reply; 4+ messages in thread
From: Jonas Rebmann @ 2025-12-17 10:59 UTC (permalink / raw)
To: Ahmad Fatoum, barebox
Hi Ahmad,
On 2025-12-17 11:35, Ahmad Fatoum wrote:
> To improve tracking where code originated from, e.g. when porting from
> Linux or U-Boot, some recent commits have started using Origin-URL
> comments. Add a script that can generate them.
>
> Examples in Vim:
>
> # add into highlight URL pointing at specified file
> V:!git origin-url ../linux/lib/ucs2_string.c
>
> # as above, but consult buffer extension for comment style
> V:!git origin-url ../linux/lib/ucs2_string.c %
>
> # generate in snippet syntax
> V:!git origin-url -s ../linux/lib/ucs2_string.c
>
> Cc: Jonas Rebmann <jre@pengutronix.de>
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
> I've been using this for my recent ports and I think it's generally
> useful and helps with deciding on a common format.
> ---
> scripts/git-origin-url.sh | 94 +++++++++++++++++++++++++++++++++++++++
> 1 file changed, 94 insertions(+)
> create mode 100755 scripts/git-origin-url.sh
>
> diff --git a/scripts/git-origin-url.sh b/scripts/git-origin-url.sh
> new file mode 100755
> index 000000000000..d31008b1b761
> --- /dev/null
> +++ b/scripts/git-origin-url.sh
> @@ -0,0 +1,94 @@
> +#!/usr/bin/env bash
> +# SPDX-License-Identifier: BSD-0-Clause
> +
> +set -euo pipefail
> +
> +declare -A repos=(
Maybe mention that these are the initial commits of the repositories
> + ["a3ffa97f40dc81f2d6b07ee964f2340fe0c1ba97"]="https://git.pengutronix.de/cgit/barebox"
> + ["1da177e4c3f41524e886b7f1b8a0c1fc7321cac2"]="https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git"
> + ["cc8ed39b240180b58810784f844e253263594ac3"]="https://git.busybox.net/busybox"
> + ["7e492d8258182e31c988bbf9917d4a3d41949d56"]="https://github.com/u-boot/u-boot"
The official upstream for u-boot according to its readme is the gitlab at
https://source.denx.de/u-boot/u-boot
> + ["afe1be4dbdf142513f6ac1d92e6a20bdc4b20c80"]="https://github.com/systemd/systemd"
> +)
> +
> +err() { echo "error: $* " >&2; exit 1; }
> +usage="Usage: $0 <path-to-file> [destination-file]"
usage="Usage: $0 [-s] <path-to-file> [destination-file]"
> +
> +snippet=""
> +while getopts "sh" opt; do
> + case "$opt" in
> + s)
> + snippet="Snippet-"
> + ;;
> + h)
> + echo "$usage"
> + exit 0
> + ;;
> + *)
> + err "$usage"
> + ;;
> + esac
> +done
> +
> +shift $((OPTIND-1))
> +
> +if [ $# -ne 1 ] && [ $# -ne 2 ]; then
> + err "$usage"
> +fi
> +
> +# Optional second argument to allow e.g. !git origin-url $remote_file % im vim
> +# In cases where local file has different extension then the original
> +dstbasename="$(basename ${2:-$1})"
> +
> +[ -e "$1" ] || err "File not found"
> +
> +filepath="$(realpath "$1")"
> +
> +repo_root="$(git -C "$(dirname "$filepath")" rev-parse --show-toplevel 2>/dev/null \
> + || err "Not inside a Git repository: $filepath")"
> +
> +rel_path="$(realpath --relative-to="$repo_root" "$filepath")"
> +
> +# newest commit that touched this file
> +file_commit="$(git -C "$repo_root" log -n1 --format=%H -- "$rel_path")"
So the idea is that the Origin-URL is generated for the commit to which
<path-to-file> is checked out at the time? Maybe you could state that
more clearly e.g. in the top of the script or in the usage message.
We don't want people to add Origin-URLs referencing just any commit.
Once I get to it, I want to add Origin-URLs to some existing code and
reconstructing which exact commit something was originally based on is
the annoying part of that.
> +
> +origin_url=""
> +
> +# find which repo this file ultimately came from
> +for commit in "${!repos[@]}"; do
> + if git -C "$repo_root" merge-base --is-ancestor "$commit" "$file_commit" 2>/dev/null; then
> + origin_url="${repos[$commit]}"
> + break
> + fi
> +done
> +
> +[ -z "$origin_url" ] && err "No matching repo base found for file commit $file_commit"
> +
> +# output uses the newest file commit, not the origin commit
> +if [[ $origin_url == https://github.com/* ]] ; then
> + tree_url="$origin_url/blob/$file_commit/$rel_path"
> +else
> + # assume cgit as fallback
> + tree_url="$origin_url/tree/$rel_path?id=$file_commit"
> +fi
> +
> +case "${dstbasename##*.}" in
> + h|S|imxcfg|y|l|lds)
> + comment_begin='/* '
> + comment_end=' */'
> + ;;
> + c|dts*)
> + comment_begin='// '
> + comment_end=''
> + ;;
> + *)
> + comment_begin='# '
> + comment_end=''
> + ;;
> +esac
> +
> +comment() { echo "${comment_begin}${*}${comment_end}"; }
> +
> +[ -z "${snippet}" ] || comment "SPDX-SnippetBegin"
> +comment "SPDX-${snippet}Comment: Origin-URL: ${tree_url}"
> +[ -z "${snippet}" ] || comment "SPDX-SnippetEnd"
Regards,
Jonas
--
Pengutronix e.K. | Jonas Rebmann |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-9 |
^ permalink raw reply [flat|nested] 4+ messages in thread* Re: [PATCH] scripts: add helper for generating Origin-URL references
2025-12-17 10:59 ` Jonas Rebmann
@ 2025-12-18 8:04 ` Sascha Hauer
0 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2025-12-18 8:04 UTC (permalink / raw)
To: Jonas Rebmann; +Cc: Ahmad Fatoum, barebox
On Wed, Dec 17, 2025 at 11:59:43AM +0100, Jonas Rebmann wrote:
> > +# Optional second argument to allow e.g. !git origin-url $remote_file % im vim
> > +# In cases where local file has different extension then the original
> > +dstbasename="$(basename ${2:-$1})"
> > +
> > +[ -e "$1" ] || err "File not found"
> > +
> > +filepath="$(realpath "$1")"
> > +
> > +repo_root="$(git -C "$(dirname "$filepath")" rev-parse --show-toplevel 2>/dev/null \
> > + || err "Not inside a Git repository: $filepath")"
> > +
> > +rel_path="$(realpath --relative-to="$repo_root" "$filepath")"
> > +
> > +# newest commit that touched this file
> > +file_commit="$(git -C "$repo_root" log -n1 --format=%H -- "$rel_path")"
>
> So the idea is that the Origin-URL is generated for the commit to which
> <path-to-file> is checked out at the time? Maybe you could state that
> more clearly e.g. in the top of the script or in the usage message.
>
> We don't want people to add Origin-URLs referencing just any commit.
Ah, should have read that before writing a mail ;)
Sascha
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] scripts: add helper for generating Origin-URL references
2025-12-17 10:35 [PATCH] scripts: add helper for generating Origin-URL references Ahmad Fatoum
2025-12-17 10:59 ` Jonas Rebmann
@ 2025-12-18 8:01 ` Sascha Hauer
1 sibling, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2025-12-18 8:01 UTC (permalink / raw)
To: Ahmad Fatoum; +Cc: barebox, Jonas Rebmann
On Wed, Dec 17, 2025 at 11:35:46AM +0100, Ahmad Fatoum wrote:
> To improve tracking where code originated from, e.g. when porting from
> Linux or U-Boot, some recent commits have started using Origin-URL
> comments. Add a script that can generate them.
>
> Examples in Vim:
>
> # add into highlight URL pointing at specified file
> V:!git origin-url ../linux/lib/ucs2_string.c
>
> # as above, but consult buffer extension for comment style
> V:!git origin-url ../linux/lib/ucs2_string.c %
>
> # generate in snippet syntax
> V:!git origin-url -s ../linux/lib/ucs2_string.c
It's worth mentioning that the repository you point at here needs to be
checked out at the very same state that it was when you copied the file.
In other words, there is no magic that tries to match the barebox
version of the file to the closest linux/u-boot/... revision of the
file.
It's a thing to remember when somebody reminds you of adding the
origin-URL in a review and you call the script a week later when your
Linux repository already has another revision checked out.
Sascha
--
Pengutronix e.K. | |
Steuerwalder Str. 21 | http://www.pengutronix.de/ |
31137 Hildesheim, Germany | Phone: +49-5121-206917-0 |
Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2025-12-18 8:05 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-12-17 10:35 [PATCH] scripts: add helper for generating Origin-URL references Ahmad Fatoum
2025-12-17 10:59 ` Jonas Rebmann
2025-12-18 8:04 ` Sascha Hauer
2025-12-18 8:01 ` Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox