# [PATCH 1/2] userdiff: add support for R programming language

9 messages from 2025-05-25 to 2025-06-03. Participants: Rodrigo Carvalho, Eric Sunshine, Johannes Sixt, Junio C Hamano.
Thread: https://gitlist.dev/t/63523

## Rodrigo Carvalho, 2025-05-25 21:02

Subject: [PATCH 1/2] userdiff: add support for R programming language
Message-ID: <20250525210236.116342-1-rodrigorsdc@gmail.com>
URL: https://gitlist.dev/e/20250525210236.116342-1-rodrigorsdc%40gmail.com

```
The patch appends userdiff.c file in order to support R programming
language function header. This will be useful for those who use Git
for versioning .R files.

Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
---
 userdiff.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/userdiff.c b/userdiff.c
index da75625020..d1d31ea67e 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -317,6 +317,10 @@ PATTERNS("python",
 	 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
 	 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
 	 /* -- */
+PATTERNS("r",
+	"^[ \t]*([a-zA-z][a-zA-Z0-9_.]*[ \t]*<-[ \t]*function.*)$",
+	/* -- */
+	"[a-zA-Z_][a-zA-Z0-9_.]*"),
 PATTERNS("ruby",
 	 "^[ \t]*((class|module|def)[ \t].*)$",
 	 /* -- */
-- 
2.43.0


```

## Rodrigo Carvalho, 2025-05-25 21:02

Subject: [PATCH 2/2] t4018: add two userdiff tests for R language
Message-ID: <20250525210236.116342-2-rodrigorsdc@gmail.com>
URL: https://gitlist.dev/e/20250525210236.116342-2-rodrigorsdc%40gmail.com
In-Reply-To: <20250525210236.116342-1-rodrigorsdc@gmail.com>

```
This patch adds two userdiff tests for R programming language
files. These files define a simple function, with and without
indentation.

Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
---
 t/t4018/r-indent   | 6 ++++++
 t/t4018/r-noindent | 6 ++++++
 2 files changed, 12 insertions(+)
 create mode 100644 t/t4018/r-indent
 create mode 100644 t/t4018/r-noindent

diff --git a/t/t4018/r-indent b/t/t4018/r-indent
new file mode 100644
index 0000000000..9df440f2a4
--- /dev/null
+++ b/t/t4018/r-indent
@@ -0,0 +1,6 @@
+RIGHT <- function(a, b) {
+    c = mean(a, b)
+    d = c + 2
+    ChangeMe()
+    return (d)
+}
diff --git a/t/t4018/r-noindent b/t/t4018/r-noindent
new file mode 100644
index 0000000000..6d9b01ffe3
--- /dev/null
+++ b/t/t4018/r-noindent
@@ -0,0 +1,6 @@
+RIGHT <- function(a, b) {
+c = mean(a, b)
+d = c + 2
+ChangeMe()
+return (c)
+}
-- 
2.43.0


```

## Eric Sunshine, 2025-05-25 22:42

Subject: Re: [PATCH 2/2] t4018: add two userdiff tests for R language
Message-ID: <CAPig+cQ07SMbfD8Er1Ukdpih_8AL_exbz7U6WzZ3b2js2ge1cg@mail.gmail.com>
URL: https://gitlist.dev/e/CAPig%2BcQ07SMbfD8Er1Ukdpih_8AL_exbz7U6WzZ3b2js2ge1cg%40mail.gmail.com
In-Reply-To: <20250525210236.116342-2-rodrigorsdc@gmail.com>

```
On Sun, May 25, 2025 at 5:03 PM Rodrigo Carvalho <rodrigorsdc@gmail.com> wrote:
> This patch adds two userdiff tests for R programming language
> files. These files define a simple function, with and without
> indentation.
>
> Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
> ---
> diff --git a/t/t4018/r-indent b/t/t4018/r-indent
> @@ -0,0 +1,6 @@
> +RIGHT <- function(a, b) {
> +    c = mean(a, b)
> +    d = c + 2
> +    ChangeMe()
> +    return (d)
> +}

Although nontraditional, do we care that some people define functions
using `=` rather than `<-`?

    foo = function(x) {...}

```

## Johannes Sixt, 2025-05-26 05:57

Subject: Re: [PATCH 1/2] userdiff: add support for R programming language
Message-ID: <385d7189-98fa-4863-8bcf-905c76eccbc8@kdbg.org>
URL: https://gitlist.dev/e/385d7189-98fa-4863-8bcf-905c76eccbc8%40kdbg.org
In-Reply-To: <20250525210236.116342-1-rodrigorsdc@gmail.com>

```
Am 25.05.25 um 23:02 schrieb Rodrigo Carvalho:
> The patch appends userdiff.c file in order to support R programming
> language function header. This will be useful for those who use Git
> for versioning .R files.
> 
> Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
> ---
>  userdiff.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/userdiff.c b/userdiff.c
> index da75625020..d1d31ea67e 100644
> --- a/userdiff.c
> +++ b/userdiff.c
> @@ -317,6 +317,10 @@ PATTERNS("python",
>  	 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
>  	 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
>  	 /* -- */
> +PATTERNS("r",

Rant mode: I am not a fan of the name "R". It is so un-searchable. It
hijacks a single character for its own purpose.

I don't see a negative impact in this case, though.

> +	"^[ \t]*([a-zA-z][a-zA-Z0-9_.]*[ \t]*<-[ \t]*function.*)$",

I wonder how useful this is in practice. Unlike C or Java for example,
code can live outside of functions in R scripts. If you have a script
without any functions, there would not be any hunk headers. If you have
a script with a mix of functions and code outside of functions, the code
after a function would be attributed to the function. I'm not saying
that this is bad, but just asking if this is part of the plan.

> +	/* -- */
> +	"[a-zA-Z_][a-zA-Z0-9_.]*"),

This singles out identifiers. Every single other characters would be its
own word. I'd consider this a disimprovement. If you are not prepared to
provide worddiff patterns, I recommend to use "[^ \t]+", which roughly
amounts to the default behavior. It can be improved incrementally in
later patches.

>  PATTERNS("ruby",
>  	 "^[ \t]*((class|module|def)[ \t].*)$",
>  	 /* -- */

Please squash the test cases into this patch. Don't forget to test an
indented function, and while at it, test a function definition *nested*
in a function definition: that documents what the expected outcome is.

-- Hannes


```

## Junio C Hamano, 2025-05-27 15:04

Subject: Re: [PATCH 1/2] userdiff: add support for R programming language
Message-ID: <xmqq4ix6rt75.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqq4ix6rt75.fsf%40gitster.g
In-Reply-To: <385d7189-98fa-4863-8bcf-905c76eccbc8@kdbg.org>

```
Johannes Sixt <j6t@kdbg.org> writes:

>> +	"^[ \t]*([a-zA-z][a-zA-Z0-9_.]*[ \t]*<-[ \t]*function.*)$",
>
> I wonder how useful this is in practice. Unlike C or Java for example,
> code can live outside of functions in R scripts. If you have a script
> without any functions, there would not be any hunk headers. If you have
> a script with a mix of functions and code outside of functions, the code
> after a function would be attributed to the function. I'm not saying
> that this is bad, but just asking if this is part of the plan.

Isn't it the same as shell, perl, python, e-lisp and perhaps others?

If we can reliably detect that we are outside of any function and
set it to an empty string that would be great ;-).

>> +	/* -- */
>> +	"[a-zA-Z_][a-zA-Z0-9_.]*"),
>
> This singles out identifiers. Every single other characters would be its
> own word. I'd consider this a disimprovement. If you are not prepared to
> provide worddiff patterns, I recommend to use "[^ \t]+", which roughly
> amounts to the default behavior. It can be improved incrementally in
> later patches.

Good point.

> Please squash the test cases into this patch. Don't forget to test an
> indented function, and while at it, test a function definition *nested*
> in a function definition: that documents what the expected outcome is.

Again, good point.

Thanks.

```

## Rodrigo Carvalho, 2025-05-29 21:36

Subject: Re: [PATCH 1/2] userdiff: add support for R programming language
Message-ID: <CAOeBkLo-Df7p-ndr06vA8MGHd42GEB6X3i66PnBt=MrQk7GJjw@mail.gmail.com>
URL: https://gitlist.dev/e/CAOeBkLo-Df7p-ndr06vA8MGHd42GEB6X3i66PnBt%3DMrQk7GJjw%40mail.gmail.com
In-Reply-To: <xmqq4ix6rt75.fsf@gitster.g>

```
> > This singles out identifiers. Every single other characters would be its
> > own word. I'd consider this a disimprovement. If you are not prepared to
> > provide worddiff patterns, I recommend to use "[^ \t]+", which roughly
> > amounts to the default behavior. It can be improved incrementally in
> > later patches.
>
> Good point.

Thanks for the remark. I'll change to this approach in v2


> > Please squash the test cases into this patch. Don't forget to test an
> > indented function, and while at it, test a function definition *nested*
> > in a function definition: that documents what the expected outcome is.
>
> Again, good point.

Sure, that is an important feature that a userdiff driver should
address. Thanks again

```

## Rodrigo Carvalho, 2025-05-29 22:16

Subject: [PATCH v2] userdiff: add support for R programming language
Message-ID: <20250529221805.97036-1-rodrigorsdc@gmail.com>
URL: https://gitlist.dev/e/20250529221805.97036-1-rodrigorsdc%40gmail.com
In-Reply-To: <20250525210236.116342-1-rodrigorsdc@gmail.com>

```
The patch appends userdiff.c file in order to support R programming
language function header. This will be useful for those who use Git
for versioning .R files.

Also, add three userdiff tests for R programming language
files. These files define simple function and nested function,
with and without indentation.

Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
---
Changes in v2:
- Add support for function defined by '='
- Replace word regex to '[^ \t']
- Add new test that handles with nested function

 t/t4018/r-indent        |  6 ++++++
 t/t4018/r-indent-nested | 10 ++++++++++
 t/t4018/r-noindent      |  6 ++++++
 userdiff.c              |  4 ++++
 4 files changed, 26 insertions(+)
 create mode 100644 t/t4018/r-indent
 create mode 100644 t/t4018/r-indent-nested
 create mode 100644 t/t4018/r-noindent

diff --git a/t/t4018/r-indent b/t/t4018/r-indent
new file mode 100644
index 0000000000..9df440f2a4
--- /dev/null
+++ b/t/t4018/r-indent
@@ -0,0 +1,6 @@
+RIGHT <- function(a, b) {
+    c = mean(a, b)
+    d = c + 2
+    ChangeMe()
+    return (d)
+}
diff --git a/t/t4018/r-indent-nested b/t/t4018/r-indent-nested
new file mode 100644
index 0000000000..30412e6c79
--- /dev/null
+++ b/t/t4018/r-indent-nested
@@ -0,0 +1,10 @@
+LEFT = function(a, b) {
+    c = mean(a, b)
+    RIGHT = function(d, e) {
+        f = var(d, e)
+        g = f + 1
+        ChangeMe()
+        return (g)
+    }
+    return (RIGHT(2, 3))
+}
diff --git a/t/t4018/r-noindent b/t/t4018/r-noindent
new file mode 100644
index 0000000000..6d9b01ffe3
--- /dev/null
+++ b/t/t4018/r-noindent
@@ -0,0 +1,6 @@
+RIGHT <- function(a, b) {
+c = mean(a, b)
+d = c + 2
+ChangeMe()
+return (c)
+}
diff --git a/userdiff.c b/userdiff.c
index da75625020..64e5005682 100644
--- a/userdiff.c
+++ b/userdiff.c
@@ -317,6 +317,10 @@ PATTERNS("python",
 	 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
 	 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
 	 /* -- */
+PATTERNS("r",
+	"^[ \t]*([a-zA-z][a-zA-Z0-9_.]*[ \t]*(<-|=)[ \t]*function.*)$",
+	/* -- */
+	"[^ \t]+"),
 PATTERNS("ruby",
 	 "^[ \t]*((class|module|def)[ \t].*)$",
 	 /* -- */
-- 
2.43.0


```

## Junio C Hamano, 2025-06-03 18:20

Subject: Re: [PATCH v2] userdiff: add support for R programming language
Message-ID: <xmqq8qm8k7p8.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqq8qm8k7p8.fsf%40gitster.g
In-Reply-To: <20250529221805.97036-1-rodrigorsdc@gmail.com>

```
Rodrigo Carvalho <rodrigorsdc@gmail.com> writes:

> The patch appends userdiff.c file in order to support R programming
> language function header. This will be useful for those who use Git
> for versioning .R files.
>
> Also, add three userdiff tests for R programming language
> files. These files define simple function and nested function,
> with and without indentation.
>
> Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
> ---
> Changes in v2:
> - Add support for function defined by '='
> - Replace word regex to '[^ \t']
> - Add new test that handles with nested function

Sorry, but this fell of the radar.

It seems all the reviewer comments on the previous round have been
addressed.  If everybody is happy with this version, let's merge it
down to 'next', for the next cycle.

Thanks.

```

## Johannes Sixt, 2025-06-03 21:15

Subject: Re: [PATCH v2] userdiff: add support for R programming language
Message-ID: <61ee9fac-303e-435d-ab0f-8338c23f2d5d@kdbg.org>
URL: https://gitlist.dev/e/61ee9fac-303e-435d-ab0f-8338c23f2d5d%40kdbg.org
In-Reply-To: <xmqq8qm8k7p8.fsf@gitster.g>

```
Am 03.06.25 um 20:20 schrieb Junio C Hamano:
> Rodrigo Carvalho <rodrigorsdc@gmail.com> writes:
> 
>> The patch appends userdiff.c file in order to support R programming
>> language function header. This will be useful for those who use Git
>> for versioning .R files.
>>
>> Also, add three userdiff tests for R programming language
>> files. These files define simple function and nested function,
>> with and without indentation.
>>
>> Signed-off-by: Rodrigo Carvalho <rodrigorsdc@gmail.com>
>> ---
>> Changes in v2:
>> - Add support for function defined by '='
>> - Replace word regex to '[^ \t']
>> - Add new test that handles with nested function
> 
> Sorry, but this fell of the radar.
> 
> It seems all the reviewer comments on the previous round have been
> addressed.  If everybody is happy with this version, let's merge it
> down to 'next', for the next cycle.
> 
> Thanks.

Sorry for being slow. This round looks good to me, too.

-- Hannes


```
