Skip to content

Commit 9a86dd5

Browse files
jnarebgitster
authored andcommitted
gitweb: Split JavaScript for maintability, combining on build
Split originally single gitweb.js file into smaller files, each dealing with single issue / area of responsibility. This move should make gitweb's JavaScript code easier to maintain. For better webapp performance it is recommended[1][2][3] to combine JavaScript files. Do it during build time (in gitweb/Makefile), by straight concatenation of files into gitweb.js file (which is now ignored as being generated). This means that there are no changes to gitweb script itself - it still uses gitweb.js or gitweb.min.js, but now generated. [1]: http://developer.yahoo.com/performance/rules.html "Minimize HTTP Requests" section [2]: http://code.google.com/speed/articles/include-scripts-properly.html "1. Combine external JavaScript files" [3]: http://javascript-reference.info/speed-up-your-javascript-load-time.htm "Combine Your Files" section. See also new gitweb/static/js/README file. Inspired-by-patch-by: John 'Warthog9' Hawley <[email protected]> Signed-off-by: Jakub Narebski <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f09f1d3 commit 9a86dd5

File tree

6 files changed

+269
-206
lines changed

6 files changed

+269
-206
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
/gitk-git/gitk-wish
159159
/gitweb/GITWEB-BUILD-OPTIONS
160160
/gitweb/gitweb.cgi
161+
/gitweb/static/gitweb.js
161162
/gitweb/static/gitweb.min.*
162163
/test-chmtime
163164
/test-ctype

gitweb/Makefile

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ ifndef V
8686
endif
8787
endif
8888

89-
all:: gitweb.cgi
89+
all:: gitweb.cgi static/gitweb.js
9090

9191
GITWEB_PROGRAMS = gitweb.cgi
9292

@@ -112,6 +112,15 @@ endif
112112

113113
GITWEB_FILES += static/git-logo.png static/git-favicon.png
114114

115+
# JavaScript files that are composed (concatenated) to form gitweb.js
116+
#
117+
# js/lib/common-lib.js should be always first, then js/lib/*.js,
118+
# then the rest of files; js/gitweb.js should be last (if it exists)
119+
GITWEB_JSLIB_FILES += static/js/lib/common-lib.js
120+
GITWEB_JSLIB_FILES += static/js/javascript-detection.js
121+
GITWEB_JSLIB_FILES += static/js/blame_incremental.js
122+
123+
115124
GITWEB_REPLACE = \
116125
-e 's|++GIT_VERSION++|$(GIT_VERSION)|g' \
117126
-e 's|++GIT_BINDIR++|$(bindir)|g' \
@@ -146,6 +155,11 @@ gitweb.cgi: gitweb.perl GITWEB-BUILD-OPTIONS
146155
chmod +x $@+ && \
147156
mv $@+ $@
148157

158+
static/gitweb.js: $(GITWEB_JSLIB_FILES)
159+
$(QUIET_GEN)$(RM) $@ $@+ && \
160+
cat $^ >$@+ && \
161+
mv $@+ $@
162+
149163
### Testing rules
150164

151165
test:

gitweb/static/js/README

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
GIT web interface (gitweb) - JavaScript
2+
=======================================
3+
4+
This directory holds JavaScript code used by gitweb (GIT web interface).
5+
Scripts from there would be concatenated together in the order specified
6+
by gitweb/Makefile into gitweb/static/gitweb.js, during building of
7+
gitweb/gitweb.cgi (during gitweb building). The resulting file (or its
8+
minification) would then be installed / deployed together with gitweb.
9+
10+
Scripts in 'lib/' subdirectory compose generic JavaScript library,
11+
providing features required by gitweb but in no way limited to gitweb
12+
only. In the future those scripts could be replaced by some JavaScript
13+
library / framework, like e.g. jQuery, YUI, Prototype, MooTools, Dojo,
14+
ExtJS, Script.aculo.us or SproutCore.
15+
16+
All scripts that manipulate gitweb output should be put outside 'lib/',
17+
directly in this directory ('gitweb/static/js/'). Those scripts would
18+
have to be rewritten if gitweb moves to using some JavaScript library.
19+
20+
See also comments in gitweb/Makefile.

gitweb/static/gitweb.js renamed to gitweb/static/js/blame_incremental.js

Lines changed: 3 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,12 @@
11
// Copyright (C) 2007, Fredrik Kuivinen <[email protected]>
22
// 2007, Petr Baudis <[email protected]>
3-
// 2008-2009, Jakub Narebski <[email protected]>
3+
// 2008-2011, Jakub Narebski <[email protected]>
44

55
/**
6-
* @fileOverview JavaScript code for gitweb (git web interface).
6+
* @fileOverview JavaScript side of Ajax-y 'blame_incremental' view in gitweb
77
* @license GPLv2 or later
88
*/
99

10-
/* ============================================================ */
11-
/* functions for generic gitweb actions and views */
12-
13-
/**
14-
* used to check if link has 'js' query parameter already (at end),
15-
* and other reasons to not add 'js=1' param at the end of link
16-
* @constant
17-
*/
18-
var jsExceptionsRe = /[;?]js=[01]$/;
19-
20-
/**
21-
* Add '?js=1' or ';js=1' to the end of every link in the document
22-
* that doesn't have 'js' query parameter set already.
23-
*
24-
* Links with 'js=1' lead to JavaScript version of given action, if it
25-
* exists (currently there is only 'blame_incremental' for 'blame')
26-
*
27-
* @globals jsExceptionsRe
28-
*/
29-
function fixLinks() {
30-
var allLinks = document.getElementsByTagName("a") || document.links;
31-
for (var i = 0, len = allLinks.length; i < len; i++) {
32-
var link = allLinks[i];
33-
if (!jsExceptionsRe.test(link)) { // =~ /[;?]js=[01]$/;
34-
link.href +=
35-
(link.href.indexOf('?') === -1 ? '?' : ';') + 'js=1';
36-
}
37-
}
38-
}
39-
40-
41-
/* ============================================================ */
4210

4311
/*
4412
* This code uses DOM methods instead of (nonstandard) innerHTML
@@ -58,71 +26,6 @@ function fixLinks() {
5826
*/
5927

6028

61-
/* ============================================================ */
62-
/* generic utility functions */
63-
64-
65-
/**
66-
* pad number N with nonbreakable spaces on the left, to WIDTH characters
67-
* example: padLeftStr(12, 3, '\u00A0') == '\u00A012'
68-
* ('\u00A0' is nonbreakable space)
69-
*
70-
* @param {Number|String} input: number to pad
71-
* @param {Number} width: visible width of output
72-
* @param {String} str: string to prefix to string, e.g. '\u00A0'
73-
* @returns {String} INPUT prefixed with (WIDTH - INPUT.length) x STR
74-
*/
75-
function padLeftStr(input, width, str) {
76-
var prefix = '';
77-
78-
width -= input.toString().length;
79-
while (width > 0) {
80-
prefix += str;
81-
width--;
82-
}
83-
return prefix + input;
84-
}
85-
86-
/**
87-
* Pad INPUT on the left to SIZE width, using given padding character CH,
88-
* for example padLeft('a', 3, '_') is '__a'.
89-
*
90-
* @param {String} input: input value converted to string.
91-
* @param {Number} width: desired length of output.
92-
* @param {String} ch: single character to prefix to string.
93-
*
94-
* @returns {String} Modified string, at least SIZE length.
95-
*/
96-
function padLeft(input, width, ch) {
97-
var s = input + "";
98-
while (s.length < width) {
99-
s = ch + s;
100-
}
101-
return s;
102-
}
103-
104-
/**
105-
* Create XMLHttpRequest object in cross-browser way
106-
* @returns XMLHttpRequest object, or null
107-
*/
108-
function createRequestObject() {
109-
try {
110-
return new XMLHttpRequest();
111-
} catch (e) {}
112-
try {
113-
return window.createRequest();
114-
} catch (e) {}
115-
try {
116-
return new ActiveXObject("Msxml2.XMLHTTP");
117-
} catch (e) {}
118-
try {
119-
return new ActiveXObject("Microsoft.XMLHTTP");
120-
} catch (e) {}
121-
122-
return null;
123-
}
124-
125-
12629
/* ============================================================ */
12730
/* utility/helper functions (and variables) */
12831

@@ -392,111 +295,6 @@ function fixColorsAndGroups() {
392295
}
393296
}
394297

395-
/* ............................................................ */
396-
/* time and data */
397-
398-
/**
399-
* used to extract hours and minutes from timezone info, e.g '-0900'
400-
* @constant
401-
*/
402-
var tzRe = /^([+-])([0-9][0-9])([0-9][0-9])$/;
403-
404-
/**
405-
* convert numeric timezone +/-ZZZZ to offset from UTC in seconds
406-
*
407-
* @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
408-
* @returns {Number} offset from UTC in seconds for timezone
409-
*
410-
* @globals tzRe
411-
*/
412-
function timezoneOffset(timezoneInfo) {
413-
var match = tzRe.exec(timezoneInfo);
414-
var tz_sign = (match[1] === '-' ? -1 : +1);
415-
var tz_hour = parseInt(match[2],10);
416-
var tz_min = parseInt(match[3],10);
417-
418-
return tz_sign*(((tz_hour*60) + tz_min)*60);
419-
}
420-
421-
/**
422-
* return date in local time formatted in iso-8601 like format
423-
* 'yyyy-mm-dd HH:MM:SS +/-ZZZZ' e.g. '2005-08-07 21:49:46 +0200'
424-
*
425-
* @param {Number} epoch: seconds since '00:00:00 1970-01-01 UTC'
426-
* @param {String} timezoneInfo: numeric timezone '(+|-)HHMM'
427-
* @returns {String} date in local time in iso-8601 like format
428-
*/
429-
function formatDateISOLocal(epoch, timezoneInfo) {
430-
// date corrected by timezone
431-
var localDate = new Date(1000 * (epoch +
432-
timezoneOffset(timezoneInfo)));
433-
var localDateStr = // e.g. '2005-08-07'
434-
localDate.getUTCFullYear() + '-' +
435-
padLeft(localDate.getUTCMonth()+1, 2, '0') + '-' +
436-
padLeft(localDate.getUTCDate(), 2, '0');
437-
var localTimeStr = // e.g. '21:49:46'
438-
padLeft(localDate.getUTCHours(), 2, '0') + ':' +
439-
padLeft(localDate.getUTCMinutes(), 2, '0') + ':' +
440-
padLeft(localDate.getUTCSeconds(), 2, '0');
441-
442-
return localDateStr + ' ' + localTimeStr + ' ' + timezoneInfo;
443-
}
444-
445-
/* ............................................................ */
446-
/* unquoting/unescaping filenames */
447-
448-
/**#@+
449-
* @constant
450-
*/
451-
var escCodeRe = /\\([^0-7]|[0-7]{1,3})/g;
452-
var octEscRe = /^[0-7]{1,3}$/;
453-
var maybeQuotedRe = /^\"(.*)\"$/;
454-
/**#@-*/
455-
456-
/**
457-
* unquote maybe git-quoted filename
458-
* e.g. 'aa' -> 'aa', '"a\ta"' -> 'a a'
459-
*
460-
* @param {String} str: git-quoted string
461-
* @returns {String} Unquoted and unescaped string
462-
*
463-
* @globals escCodeRe, octEscRe, maybeQuotedRe
464-
*/
465-
function unquote(str) {
466-
function unq(seq) {
467-
var es = {
468-
// character escape codes, aka escape sequences (from C)
469-
// replacements are to some extent JavaScript specific
470-
t: "\t", // tab (HT, TAB)
471-
n: "\n", // newline (NL)
472-
r: "\r", // return (CR)
473-
f: "\f", // form feed (FF)
474-
b: "\b", // backspace (BS)
475-
a: "\x07", // alarm (bell) (BEL)
476-
e: "\x1B", // escape (ESC)
477-
v: "\v" // vertical tab (VT)
478-
};
479-
480-
if (seq.search(octEscRe) !== -1) {
481-
// octal char sequence
482-
return String.fromCharCode(parseInt(seq, 8));
483-
} else if (seq in es) {
484-
// C escape sequence, aka character escape code
485-
return es[seq];
486-
}
487-
// quoted ordinary character
488-
return seq;
489-
}
490-
491-
var match = str.match(maybeQuotedRe);
492-
if (match) {
493-
str = match[1];
494-
// perhaps str = eval('"'+str+'"'); would be enough?
495-
str = str.replace(escCodeRe,
496-
function (substr, p1, offset, s) { return unq(p1); });
497-
}
498-
return str;
499-
}
500298

501299
/* ============================================================ */
502300
/* main part: parsing response */
@@ -886,4 +684,4 @@ function startBlame(blamedataUrl, bUrl) {
886684
pollTimer = setInterval(xhr.onreadystatechange, 1000);
887685
}
888686

889-
// end of gitweb.js
687+
/* end of blame_incremental.js */
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (C) 2007, Fredrik Kuivinen <[email protected]>
2+
// 2007, Petr Baudis <[email protected]>
3+
// 2008-2011, Jakub Narebski <[email protected]>
4+
5+
/**
6+
* @fileOverview Detect if JavaScript is enabled, and pass it to server-side
7+
* @license GPLv2 or later
8+
*/
9+
10+
11+
/* ============================================================ */
12+
/* Manipulating links */
13+
14+
/**
15+
* used to check if link has 'js' query parameter already (at end),
16+
* and other reasons to not add 'js=1' param at the end of link
17+
* @constant
18+
*/
19+
var jsExceptionsRe = /[;?]js=[01]$/;
20+
21+
/**
22+
* Add '?js=1' or ';js=1' to the end of every link in the document
23+
* that doesn't have 'js' query parameter set already.
24+
*
25+
* Links with 'js=1' lead to JavaScript version of given action, if it
26+
* exists (currently there is only 'blame_incremental' for 'blame')
27+
*
28+
* To be used as `window.onload` handler
29+
*
30+
* @globals jsExceptionsRe
31+
*/
32+
function fixLinks() {
33+
var allLinks = document.getElementsByTagName("a") || document.links;
34+
for (var i = 0, len = allLinks.length; i < len; i++) {
35+
var link = allLinks[i];
36+
if (!jsExceptionsRe.test(link)) { // =~ /[;?]js=[01]$/;
37+
link.href +=
38+
(link.href.indexOf('?') === -1 ? '?' : ';') + 'js=1';
39+
}
40+
}
41+
}
42+
43+
/* end of javascript-detection.js */

0 commit comments

Comments
 (0)