Read repository source code files through the Ruby git gem? - ruby

I want to get access to a repository's source code through the git gem. My goal is to go through the source code files and read their content.
From the examples it's unclear to me how I do this? I get the sense that I need to use Git.open, as shown here:
g = Git.open(working_dir, :log => Logger.new(STDOUT))
But it's not clear to me what working_dir is. If I was to target the github.com/ruby-git/ruby-git project there are many ways to reference it. It could be any of these:
git#github.com:ruby-git/ruby-git.git
https://github.com/ruby-git/ruby-git.git
https://github.com/ruby-git/ruby-git
So please let me know if you know how I read the source code files from a repository.

working_dir is directory of your project with Git.
Says your project is in the /home/john_smith_1976/awesome_project folder.
In this case use
require 'git'
g = Git.open('/home/john_smith_1976/awesome_project')
After that for example you can use g.grep('some_code') etc.

Related

specify source files when using snakemake remote prefix

From the docs:
https://snakemake.readthedocs.io/en/stable/executable.html
Currently, this mode requires that the Snakemake workflow is stored in a git repository. Snakemake uses git to query necessary source files (the Snakefile, scripts, config, …) for workflow execution and encodes them into the kubernetes job.
However, what is not clear to me is how to specify source files as stated. srcdir returns the absolute path with the specified prefix prepended.
Could someone please point me to the relevant area of the docs please!
Thanks
I haven't worked with kubernetics and Snakemake, but I am assuming using the 'default' structure should work. You can easily generate this structure with the snakemake cookiecutter repo:
https://github.com/snakemake-workflows/cookiecutter-snakemake-workflow
Just make sure cookiecutter is installed and you can run this on the command-line:
cookiecutter https://github.com/snakemake-workflows/cookiecutter-snakemake-workflow
Or you can always just take a look at where files are stored in that repo and copy the structure.

Go dep and forks of libraries

I'm trying to understand how to work with Golang and forks. The situation is the following, I'm writing a library project which depends on library github.com/other/some_dependency, which isn't mine.
Because some_dependency is missing some methods that I need, I fork it to github.com/me/some_dependency. However, I can't just do go get github.com/me/some_dependency, the library references itself so it breaks.
In this article they give a possible solution:
go get github.com/other/some_dependency
cd $GOPATH/src/github.com/other/some_dependency
git remote add fork git#github.com:me/some_dependency
git rebase fork/master
Now, this is hacky at best. There is no way from the code of the library to know that the dependency is coming from a different repo. Anyone doing go get of my library wouldn't manage to make it work.
As dep is expected to be the official dependency manager. I've found how to fix the version:
dep ensure -add github.com/foo/bar#v1.0.0
But I cannot find how to set a different remote. Is is possible to do it?
As an example, in Node.js with npm it is dead simple:
npm install git+https://git#github.com/visionmedia/express.git
If you look at the help you will see this:
<import path>[:alt source URL][#<constraint>]
So to add github.com/foo/bar from location github.com/fork/bar you have to add it like this:
dep ensure -add github.com/foo/bar:github.com/fork/bar
The source location will be added as source attribute in the Gopkg.toml.
Gopkg docs for dependency rules constraint and override

Modifying an imported library in Go

My Problem
Elastic Beats is an open source project for log shippers written in Go. It features several log outputs, including console, Elasticsearch and Redis. I would like to add an output of my own - to AWS Kinesis.
I have cloned the repo to ~/github/beats, and tried building it:
$ cd filebeat; go build main.go
However, it failed due to a missing library which is a part of the project:
main.go:6:2: cannot find package "github.com/elastic/beats/filebeat/cmd" in any of:
/usr/local/go/src/github.com/elastic/beats/filebeat/cmd (from $GOROOT)
/Users/adam/go/src/github.com/elastic/beats/filebeat/cmd (from $GOPATH)
A directory of the project is dependent on a package from the same repo, but instead of looking one directory up the hierarchy it looks in the GOPATH.
So, go get github.com/elastic/beats/filebeat/cmd fetched the code, and now go build main.go works. Changing the code in my GOPATH is reflected in these builds.
This leaves me with an structural inconvenience. Some of my code is at a working directory, and some of it is at my GOPATH and included by the code in my working directory.
I would like to have all my code in a single directory for various reasons, not the least being keeping everything under version control.
What Have I Tried
Mostly searching for the problem. I am quite new to Go, so I might have missed the correct terminology.
My Question
What is the right way to edit the code of an imported library in Go?
One of the recommended ways to work with other's packages is:
Get the sources of the original package:
go get github.com/elastic/beats
As a result you will clone project's git repository to the folder
$GOPATH/src/github.com/elastic/beats
Make some fixes, compile code, fix, compile... When you make go install package will be compiled and installed to your system. When you need merge updates from original repository you can git pull them.
Everything is OK. What's next? How to share your work with others?
Fork project on github, suppose it will be github.com/username/beats
Add this fork as another remote mycopy (or any other name you like) to your local repository
git remote add mycopy git://github.com/username/beats.git
When all is done you can push updated sources to your repo on github
git push mycopy
and then open a pull-request to original sources. This way you can share your work with others. And keep your changes in sync with mainstream.
Previous answers to this question are obsolete when developing projects that using Go Modules.
For projects that using Go Modules, one may use the following command to replace an imported library(eg. example.com/imported/module) with a local module(eg. ./local/module):
go mod edit -replace=example.com/imported/module=./local/module
Or by adding the following line into the go.mod file:
replace example.com/imported/module => ./local/module
Reference Docs: https://golang.org/doc/modules/managing-dependencies#unpublished
A project working copy should be checked out into $GOPATH/src/package/import/path - for example, this project should be checked out into /Users/adam/go/src/github.com/elastic/beats. With the project in the correct location, the go tooling will be able to operate on it normally; otherwise, it will not be able to resolve imports correctly. See go help gopath for more info.

wercker.yml in a subdirectory

having a Bitbucket repository, there is a project in a subdirectory that should be built. I'd like to have the wercker.yml in this subdirectory as well.
I tried to add an environment variable WERCKER_ROOT to the pipeline containing the subdirectory: /here-is-werckerfile, according to wercker devcenter which seemed to have no effect.
Any ideas?
As far as I know you can't change this environment. It's not used by wrecker, it's only meant to provide a read only information in case you need to know where your code is located.
To answer your question: once again as far as I know you can't use a wercker.yml from a subdirectory as there's no way to set it's location for wercker - it'll already search for it in your repository root.
edit however you can change the source dir, as mentioned here: http://old-devcenter.wercker.com/articles/steps/variables.html - with the WERCKER_SOURCE_DIR environment, so you can put your wercker.yml in the repo root but use the sub directory for building.

Yard relative link to extra file

I want to link to another extra file from my README using Yard.
For example, I have the following line:
...detailed instructions [here](contributing.md) on how to contribute
I want this to link to my file contributing.md in the same directory. I can include the extra file in my .yardopts file, and it will show up in the file list as it should.
So then I found that I can use the yard DSL to make the link work:
...detailed instructions {file:contributing.md here} on how to contribute
However this will not work if the README is being read from Github. Am I naive to want to have it both ways?
Is there a way to link to another .md extra file in markdown using Yard?
I am not sure what you mean with 'if the README is being read from Github'.
The following works for me locally
I add a file to the README via {file:my_file.md test}
I tell yard via the --files command-line option that there is an extra file. The link to 'test' appears in the README. For example in the following way:
bundle exec yard --files my_file.md

Resources