add GOT_LOG_DEFAULT_LIMIT env var for setting a got log -l default
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
diff --git a/got/got.1 b/got/got.1
index 5051557..73aea3c 100644
--- a/got/got.1
+++ b/got/got.1
@@ -314,6 +314,11 @@ Merge commits which affected the current branch will be shown but
individual commits which originated on other branches will be omitted.
.It Fl l Ar N
Limit history traversal to a given number of commits.
+If this option is not specified, a default limit value of zero is used,
+which is treated as an unbounded limit.
+The
+.Ev GOT_LOG_DEFAULT_LIMIT
+environment variable may be set to change this default value.
.It Fl p
Display the patch of modifications made in each commit.
.It Fl r Ar repository-path
@@ -1099,6 +1104,11 @@ environment variables with a missing email address.
.It Ev VISUAL , EDITOR
The editor spawned by
.Cm got commit .
+.It Ev GOT_LOG_DEFAULT_LIMIT
+The default limit on the number of commits traversed by
+.Cm got log .
+If set to zero, the limit is unbounded.
+This variable will be silently ignored if it is set to a non-numeric value.
.El
.Sh EXIT STATUS
.Ex -std got
diff --git a/got/got.c b/got/got.c
index 0ebd0d6..5bac0ad 100644
--- a/got/got.c
+++ b/got/got.c
@@ -1551,6 +1551,22 @@ usage_log(void)
exit(1);
}
+static int
+get_default_log_limit(void)
+{
+ const char *got_default_log_limit;
+ long long n;
+ const char *errstr;
+
+ got_default_log_limit = getenv("GOT_LOG_DEFAULT_LIMIT");
+ if (got_default_log_limit == NULL)
+ return 0;
+ n = strtonum(got_default_log_limit, 0, INT_MAX, &errstr);
+ if (errstr != NULL)
+ return 0;
+ return n;
+}
+
static const struct got_error *
cmd_log(int argc, char *argv[])
{
@@ -1575,6 +1591,8 @@ cmd_log(int argc, char *argv[])
err(1, "pledge");
#endif
+ limit = get_default_log_limit();
+
while ((ch = getopt(argc, argv, "b:pc:C:l:fr:")) != -1) {
switch (ch) {
case 'p':
@@ -1590,7 +1608,7 @@ cmd_log(int argc, char *argv[])
err(1, "-C option %s", errstr);
break;
case 'l':
- limit = strtonum(optarg, 1, INT_MAX, &errstr);
+ limit = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr != NULL)
err(1, "-l option %s", errstr);
break;
diff --git a/regress/cmdline/common.sh b/regress/cmdline/common.sh
index e4dc12d..7554600 100644
--- a/regress/cmdline/common.sh
+++ b/regress/cmdline/common.sh
@@ -15,6 +15,7 @@
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
export GOT_AUTHOR="Flan Hacker <flan_hacker@openbsd.org>"
+export GOT_LOG_DEFAULT_LIMIT=0
export MALLOC_OPTIONS=S
diff --git a/regress/cmdline/log.sh b/regress/cmdline/log.sh
index 470a29d..bfe52f0 100755
--- a/regress/cmdline/log.sh
+++ b/regress/cmdline/log.sh
@@ -151,8 +151,86 @@ function test_log_tag {
test_done "$testroot" "$ret"
}
+function test_log_limit {
+ local testroot=`test_init log_limit`
+ local commit_id0=`git_show_head $testroot/repo`
+
+ got checkout $testroot/repo $testroot/wt > /dev/null
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ echo "modified alpha" > $testroot/wt/alpha
+ (cd $testroot/wt && got commit -m 'test log_limit' > /dev/null)
+ local commit_id1=`git_show_head $testroot/repo`
+
+ (cd $testroot/wt && got rm beta >/dev/null)
+ (cd $testroot/wt && got commit -m 'test log_limit' > /dev/null)
+ local commit_id2=`git_show_head $testroot/repo`
+
+ echo "new file" > $testroot/wt/new
+ (cd $testroot/wt && got add new >/dev/null)
+ (cd $testroot/wt && got commit -m 'test log_limit' > /dev/null)
+ local commit_id3=`git_show_head $testroot/repo`
+
+ # -l1 should print the first commit only
+ echo "commit $commit_id3 (master)" > $testroot/stdout.expected
+ (cd $testroot/wt && got log -l1 | grep ^commit > $testroot/stdout)
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ # env var can be used to set a log limit without -l option
+ echo "commit $commit_id3 (master)" > $testroot/stdout.expected
+ echo "commit $commit_id2" >> $testroot/stdout.expected
+ (cd $testroot/wt && env GOT_LOG_DEFAULT_LIMIT=2 got log | \
+ grep ^commit > $testroot/stdout)
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ # non-numeric env var is ignored
+ (cd $testroot/wt && env GOT_LOG_DEFAULT_LIMIT=foobar got log | \
+ grep ^commit > $testroot/stdout)
+ echo "commit $commit_id3 (master)" > $testroot/stdout.expected
+ echo "commit $commit_id2" >> $testroot/stdout.expected
+ echo "commit $commit_id1" >> $testroot/stdout.expected
+ echo "commit $commit_id0" >> $testroot/stdout.expected
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ test_done "$testroot" "$ret"
+ return 1
+ fi
+
+ # -l option takes precedence over env var
+ echo "commit $commit_id3 (master)" > $testroot/stdout.expected
+ echo "commit $commit_id2" >> $testroot/stdout.expected
+ echo "commit $commit_id1" >> $testroot/stdout.expected
+ echo "commit $commit_id0" >> $testroot/stdout.expected
+ (cd $testroot/wt && env GOT_LOG_DEFAULT_LIMIT=1 got log -l0 | \
+ grep ^commit > $testroot/stdout)
+ cmp -s $testroot/stdout.expected $testroot/stdout
+ ret="$?"
+ if [ "$ret" != "0" ]; then
+ diff -u $testroot/stdout.expected $testroot/stdout
+ fi
+ test_done "$testroot" "0"
+}
run_test test_log_in_repo
run_test test_log_in_bare_repo
run_test test_log_in_worktree
run_test test_log_tag
+run_test test_log_limit