Rewriting import paths?

3,161 views
Skip to first unread message

Kamil Kisiel

unread,
Feb 12, 2013, 6:41:05 PM2/12/13
to golan...@googlegroups.com
I'm currently in the process of importing some dependencies in to a local revision control server. That means I'll be needing to rewrite a bunch of import paths in several projects.

Is there anything that works like gofmt -r (or maybe you can do it with gofmt -r ?) that would let me do this in an automated fashion.

Basically I want to perform transformations like:


on a bunch of sources. I know I can do it with sed, but it would be nice if it took the code semantics in to account and only changed it in import statements.

Dan Kortschak

unread,
Feb 12, 2013, 6:47:33 PM2/12/13
to Kamil Kisiel, golan...@googlegroups.com
There is a gofix module that does this. I made a version of it for a
broad rename I did ages ago, it's pretty easy and the rules could be
automated. The file is here:

https://code.google.com/p/go/source/browse/src/cmd/fix/go1pkgrename.go

Andy Balholm

unread,
Feb 12, 2013, 7:00:55 PM2/12/13
to golan...@googlegroups.com
On Tuesday, February 12, 2013 3:41:05 PM UTC-8, Kamil Kisiel wrote:
I'm currently in the process of importing some dependencies in to a local revision control server. That means I'll be needing to rewrite a bunch of import paths in several projects.

Is there anything that works like gofmt -r (or maybe you can do it with gofmt -r ?) that would let me do this in an automated fashion.

I just updated a couple of projects with:

gofmt -w -r '"exp/html" -> "code.google.com/p/go.net/html"' *.go

(Note that the whole argument to -r is in single quotes, with double quotes inside it.) 

Kamil Kisiel

unread,
Feb 12, 2013, 8:19:18 PM2/12/13
to golan...@googlegroups.com
Thanks, that works! I did try it with what I'm pretty sure was the same syntax but it didn't, so I must have done something wrong the first time.

Kamil Kisiel

unread,
Feb 12, 2013, 10:52:01 PM2/12/13
to golan...@googlegroups.com
On Tuesday, February 12, 2013 6:18:32 PM UTC-8, gary b wrote:
On Tuesday, February 12, 2013 3:41:05 PM UTC-8, Kamil Kisiel wrote:
I'm currently in the process of importing some dependencies in to a local revision control server. That means I'll be needing to rewrite a bunch of import paths in several projects.

You don't need to rewrite the source if you clone the local repo to the workspace directory corresponding to the upstream repo,.

That is true, but I want my coworkers to be able to "go get" my projects and just have them work, without accidentally fetching upstream sources. I don't want to have to provide another script to do the fetch and place manually. 

Eli Janssen

unread,
Feb 12, 2013, 11:49:09 PM2/12/13
to Kamil Kisiel, golan...@googlegroups.com
I have also seen https://github.com/kr/goven mentioned a few times in this list at various times too.

roger peppe

unread,
Feb 13, 2013, 5:51:47 AM2/13/13
to Kamil Kisiel, golang-nuts
i wrote a command to deal with this a little while ago,
reaching towards an easier way to manage package
versioning in go:

http://godoc.org/launchpad.net/govers

it's optimised towards changing version numbers
of imported packages (assuming the version number
is embedded in the path), but can be used
for your kind of task too.

govers's main attraction is that it recursively checks
dependencies to make sure that you don't end up
with your code using multiple copies of a given package
because it's imported twice with different names.

it's also a little less prescriptive than goven - it does
not assume that you want to copy a remote dependency
into the local directory.

your example using govers could be something like this:

$ cd my-package
$ govers -m 'github\.com/gorilla' hg.example.com/go/github/com/gorilla
$

note that this will change all imports of packages under githup.com/gorilla
(which is usually what you want). if you want an exact match, you
can anchor the match pattern:

$ govers -m 'github\.com/gorilla/mux$'
hg.example.com/go/github.com/gorilla/mux.hg

hope this helps,
rog.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Reply all
Reply to author
Forward
Message has been deleted
0 new messages