@matheusbsbs / matheus-bagliano / commits / fdb0c36e90
Fork 0

gitweb: check given hash before trying to create snapshot

Makes things nicer in cases when you hand craft the snapshot URL but make a typo in defining the hash variable (e.g. netx instead of next); you will now get an error message instead of a broken tarball. Tests for t9501 are included to demonstrate added functionality. Signed-off-by: Mark Rada <marada@uwaterloo.ca> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Mark Rada committed Sep 26, 2009 at 13:46 UTC fdb0c36e903d13c184f9a465035c75565c5c072a
2 files changed +44 -2
gitweb/gitweb.perl
+5 -2
@@ -5196,8 +5196,11 @@ sub git_snapshot {
5196 die_error(403, "Unsupported snapshot format");
5197 }
5198
5199 - if (!defined $hash) {
5200 - $hash = git_get_head_hash($project);
5199 + my $type = git_get_type("$hash^{}");
5200 + if (!$type) {
5201 + die_error(404, 'Object does not exist');
5202 + } elsif ($type eq 'blob') {
5203 + die_error(400, 'Object is not a tree-ish');
5204 }
5205
5206 my $name = $project;
t/t9501-gitweb-standalone-http-status.sh
+39
@@ -75,4 +75,43 @@ test_expect_success \
75 test_debug 'cat gitweb.output'
76
77
78 +# ----------------------------------------------------------------------
79 +# snapshot hash ids
80 +
81 +test_expect_success 'snapshots: good tree-ish id' '
82 + gitweb_run "p=.git;a=snapshot;h=master;sf=tgz" &&
83 + grep "Status: 200 OK" gitweb.output
84 +'
85 +test_debug 'cat gitweb.output'
86 +
87 +test_expect_success 'snapshots: bad tree-ish id' '
88 + gitweb_run "p=.git;a=snapshot;h=frizzumFrazzum;sf=tgz" &&
89 + grep "404 - Object does not exist" gitweb.output
90 +'
91 +test_debug 'cat gitweb.output'
92 +
93 +test_expect_success 'snapshots: bad tree-ish id (tagged object)' '
94 + echo object > tag-object &&
95 + git add tag-object &&
96 + git commit -m "Object to be tagged" &&
97 + git tag tagged-object `git hash-object tag-object` &&
98 + gitweb_run "p=.git;a=snapshot;h=tagged-object;sf=tgz" &&
99 + grep "400 - Object is not a tree-ish" gitweb.output
100 +'
101 +test_debug 'cat gitweb.output'
102 +
103 +test_expect_success 'snapshots: good object id' '
104 + ID=`git rev-parse --verify HEAD` &&
105 + gitweb_run "p=.git;a=snapshot;h=$ID;sf=tgz" &&
106 + grep "Status: 200 OK" gitweb.output
107 +'
108 +test_debug 'cat gitweb.output'
109 +
110 +test_expect_success 'snapshots: bad object id' '
111 + gitweb_run "p=.git;a=snapshot;h=abcdef01234;sf=tgz" &&
112 + grep "404 - Object does not exist" gitweb.output
113 +'
114 +test_debug 'cat gitweb.output'
115 +
116 +
117 test_done