LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2012 The Go Authors. All rights reserved. | 1 // Copyright 2012 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package main | 5 package main |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "encoding/json" | 9 "encoding/json" |
10 "fmt" | 10 "fmt" |
11 "os" | 11 "os" |
12 "os/exec" | 12 "os/exec" |
| 13 "path/filepath" |
13 "regexp" | 14 "regexp" |
14 "strings" | 15 "strings" |
15 ) | 16 ) |
16 | 17 |
17 // A vcsCmd describes how to use a version control system | 18 // A vcsCmd describes how to use a version control system |
18 // like Mercurial, Git, or Subversion. | 19 // like Mercurial, Git, or Subversion. |
19 type vcsCmd struct { | 20 type vcsCmd struct { |
20 name string | 21 name string |
21 cmd string // name of binary to invoke command | 22 cmd string // name of binary to invoke command |
22 | 23 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 prefix string // prefix this description ap
plies to | 263 prefix string // prefix this description ap
plies to |
263 re string // pattern for import path | 264 re string // pattern for import path |
264 repo string // repository to use (expand
with match of re) | 265 repo string // repository to use (expand
with match of re) |
265 vcs string // version control system to
use (expand with match of re) | 266 vcs string // version control system to
use (expand with match of re) |
266 check func(match map[string]string) error // additional checks | 267 check func(match map[string]string) error // additional checks |
267 ping bool // ping for scheme to use to
download repo | 268 ping bool // ping for scheme to use to
download repo |
268 | 269 |
269 regexp *regexp.Regexp // cached compiled form of re | 270 regexp *regexp.Regexp // cached compiled form of re |
270 } | 271 } |
271 | 272 |
| 273 // vcsForDir inspects dir and its parents to determine the |
| 274 // version control system and code repository to use. |
| 275 // On return, root is the import path |
| 276 // corresponding to the root of the repository |
| 277 // (thus root is a prefix of importPath). |
| 278 func vcsForDir(p *Package) (vcs *vcsCmd, root string, err error) { |
| 279 // Clean and double-check that dir is in (a subdirectory of) srcRoot. |
| 280 dir := filepath.Clean(p.Dir) |
| 281 srcRoot := filepath.Clean(p.build.SrcRoot) |
| 282 if len(dir) <= len(srcRoot) || dir[len(srcRoot)] != filepath.Separator { |
| 283 return nil, "", fmt.Errorf("directory %q is outside source root
%q", dir, srcRoot) |
| 284 } |
| 285 |
| 286 for len(dir) > len(srcRoot) { |
| 287 for _, vcs := range vcsList { |
| 288 if fi, err := os.Stat(filepath.Join(dir, "."+vcs.cmd));
err == nil && fi.IsDir() { |
| 289 return vcs, dir[len(srcRoot)+1:], nil |
| 290 } |
| 291 } |
| 292 |
| 293 // Move to parent. |
| 294 ndir := filepath.Dir(dir) |
| 295 if len(ndir) >= len(dir) { |
| 296 // Shouldn't happen, but just in case, stop. |
| 297 break |
| 298 } |
| 299 dir = ndir |
| 300 } |
| 301 |
| 302 return nil, "", fmt.Errorf("directory %q is not using a known version co
ntrol system", dir) |
| 303 } |
| 304 |
272 // vcsForImportPath analyzes importPath to determine the | 305 // vcsForImportPath analyzes importPath to determine the |
273 // version control system, and code repository to use. | 306 // version control system, and code repository to use. |
274 // On return, repo is the repository URL and root is the | 307 // On return, repo is the repository URL and root is the |
275 // import path corresponding to the root of the repository | 308 // import path corresponding to the root of the repository |
276 // (thus root is a prefix of importPath). | 309 // (thus root is a prefix of importPath). |
277 func vcsForImportPath(importPath string) (vcs *vcsCmd, repo, root string, err er
ror) { | 310 func vcsForImportPath(importPath string) (vcs *vcsCmd, repo, root string, err er
ror) { |
278 for _, srv := range vcsPaths { | 311 for _, srv := range vcsPaths { |
279 if !strings.HasPrefix(importPath, srv.prefix) { | 312 if !strings.HasPrefix(importPath, srv.prefix) { |
280 continue | 313 continue |
281 } | 314 } |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 if match["project"] == "" || match["series"] == "" { | 514 if match["project"] == "" || match["series"] == "" { |
482 return nil | 515 return nil |
483 } | 516 } |
484 _, err := httpGET(expand(match, "https://code.launchpad.net/{project}{se
ries}/.bzr/branch-format")) | 517 _, err := httpGET(expand(match, "https://code.launchpad.net/{project}{se
ries}/.bzr/branch-format")) |
485 if err != nil { | 518 if err != nil { |
486 match["root"] = expand(match, "launchpad.net/{project}") | 519 match["root"] = expand(match, "launchpad.net/{project}") |
487 match["repo"] = expand(match, "https://{root}") | 520 match["repo"] = expand(match, "https://{root}") |
488 } | 521 } |
489 return nil | 522 return nil |
490 } | 523 } |
LEFT | RIGHT |