~ubuntu-branches/ubuntu/vivid/luarocks/vivid-proposed

« back to all changes in this revision

Viewing changes to src/luarocks/tools/patch.lua

  • Committer: Package Import Robot
  • Author(s): Enrico Tassi
  • Date: 2014-10-11 15:26:47 UTC
  • mfrom: (1.2.6)
  • Revision ID: package-import@ubuntu.com-20141011152647-bkfciayfdz6elvv3
Tags: 2.2.0+dfsg-1
* New upstream release
* patch 0001-Fixed-detection-of-Debian-paths removed (applied upstream)

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
-- Code is heavilly based on the Python-based patch.py version 8.06-1
7
7
--   Copyright (c) 2008 rainforce.org, MIT License
8
8
--   Project home: http://code.google.com/p/python-patch/ .
 
9
--   Version 0.1
9
10
 
10
 
module("luarocks.tools.patch", package.seeall)
 
11
--module("luarocks.tools.patch", package.seeall)
 
12
local patch = {}
11
13
 
12
14
local fs = require("luarocks.fs")
13
 
 
14
 
local version = '0.1'
 
15
local util = require("luarocks.util")
15
16
 
16
17
local io = io
17
18
local os = os
47
48
  return t2
48
49
end
49
50
 
50
 
local function array_contains(t, v)
51
 
  for _,v2 in ipairs(t) do if v == v2 then return true end end
52
 
  return false
53
 
end
54
 
 
55
51
local function exists(filename)
56
52
  local fh = io.open(filename)
57
53
  local result = fh ~= nil
95
90
      eof = false,
96
91
      read = function(self, n)
97
92
         if self.eof then return nil end
98
 
         local chunk = self.str:sub(self.at, self.at+n)
 
93
         local chunk = self.str:sub(self.at, self.at + n - 1)
99
94
         self.at = self.at + n
100
95
         if self.at > self.len then
101
96
            self.eof = true
158
153
  return m1, m2, m3, m4
159
154
end
160
155
 
161
 
function read_patch(filename, data)
 
156
function patch.read_patch(filename, data)
162
157
  -- define possible file regions that will direct the parser flow
163
158
  local state = 'header'
164
159
    -- 'header'    - comments before the patch body
284
279
    local advance
285
280
    if state == 'filenames' then
286
281
      if startswith(line, "--- ") then
287
 
        if array_contains(files.source, nextfileno) then
 
282
        if util.array_contains(files.source, nextfileno) then
288
283
          all_ok = false
289
284
          warning(format("skipping invalid patch for %s",
290
285
                         files.source[nextfileno+1]))
295
290
        -- Accept a space as a terminator, like GNU patch does.
296
291
        -- Breaks patches containing filenames with spaces...
297
292
        -- FIXME Figure out what does GNU patch do in those cases.
298
 
        local match = line:match("^--- ([^\t ]+)")
 
293
        local match = line:match("^%-%-%- ([^ \t\r\n]+)")
299
294
        if not match then
300
295
          all_ok = false
301
296
          warning(format("skipping invalid filename at line %d", lineno+1))
304
299
          table.insert(files.source, match)
305
300
        end
306
301
      elseif not startswith(line, "+++ ") then
307
 
        if array_contains(files.source, nextfileno) then
 
302
        if util.array_contains(files.source, nextfileno) then
308
303
          all_ok = false
309
304
          warning(format("skipping invalid patch with no target for %s",
310
305
                         files.source[nextfileno+1]))
315
310
        end
316
311
        state = 'header'
317
312
      else
318
 
        if array_contains(files.target, nextfileno) then
 
313
        if util.array_contains(files.target, nextfileno) then
319
314
          all_ok = false
320
315
          warning(format("skipping invalid patch - double target at line %d",
321
316
                         lineno+1))
329
324
          -- Accept a space as a terminator, like GNU patch does.
330
325
          -- Breaks patches containing filenames with spaces...
331
326
          -- FIXME Figure out what does GNU patch do in those cases.
332
 
          local re_filename = "^%+%+%+ ([^ \t]+)"
 
327
          local re_filename = "^%+%+%+ ([^ \t\r\n]+)"
333
328
          local match = line:match(re_filename)
334
329
          if not match then
335
330
            all_ok = false
354
349
    if not advance and state == 'hunkhead' then
355
350
      local m1, m2, m3, m4 = match_linerange(line)
356
351
      if not m1 then
357
 
        if not array_contains(files.hunks, nextfileno-1) then
 
352
        if not util.array_contains(files.hunks, nextfileno-1) then
358
353
          all_ok = false
359
354
          warning(format("skipping invalid patch with no hunks for file %s",
360
355
                         files.target[nextfileno]))
401
396
    for i=0,#file do
402
397
      local found = true
403
398
      local location = lineno
404
 
      local total = #h.text - fuzz
405
399
      for l, hline in ipairs(h.text) do
406
400
        if l > fuzz then
407
401
          -- todo: \ No newline at the end of file
452
446
end
453
447
 
454
448
local function find_hunks(file, hunks)
455
 
  local matched = true
456
 
  local lineno = 1
457
 
  local hno = nil
458
449
  for hno, h in ipairs(hunks) do
459
450
    find_hunk(file, h, hno)
460
451
  end
463
454
local function check_patched(file, hunks)
464
455
  local matched = true
465
456
  local lineno = 1
466
 
  local hno = nil
467
457
  local ok, err = pcall(function()
468
458
    if #file == 0 then
469
459
      error 'nomatch'
569
559
  return filename
570
560
end
571
561
 
572
 
function apply_patch(patch, strip)
 
562
function patch.apply_patch(the_patch, strip)
573
563
  local all_ok = true
574
 
  local total = #patch.source
575
 
  for fileno, filename in ipairs(patch.source) do
 
564
  local total = #the_patch.source
 
565
  for fileno, filename in ipairs(the_patch.source) do
576
566
    filename = strip_dirs(filename, strip)
577
567
    local continue
578
568
    local f2patch = filename
579
569
    if not exists(f2patch) then
580
 
      f2patch = strip_dirs(patch.target[fileno], strip)
 
570
      f2patch = strip_dirs(the_patch.target[fileno], strip)
581
571
      f2patch = fs.absolute_name(f2patch)
582
572
      if not exists(f2patch) then  --FIX:if f2patch nil
583
573
        warning(format("source/target file does not exist\n--- %s\n+++ %s",
598
588
    info(format("processing %d/%d:\t %s", fileno, total, filename))
599
589
 
600
590
    -- validate before patching
601
 
    local hunks = patch.hunks[fileno]
 
591
    local hunks = the_patch.hunks[fileno]
602
592
    local file = load_file(filename)
603
593
    local hunkno = 1
604
594
    local hunk = hunks[hunkno]
710
700
  -- todo: check for premature eof
711
701
  return all_ok
712
702
end
 
703
 
 
704
return patch